-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshapes_rectangle_scaling.php
115 lines (93 loc) · 3.6 KB
/
shapes_rectangle_scaling.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
declare(strict_types=1);
use Nawarian\Raylib\Raylib;
use Nawarian\Raylib\Types\{Color, Rectangle, Vector2};
use function Nawarian\Raylib\{
BeginDrawing,
CheckCollisionPointRec,
ClearBackground,
CloseWindow,
DrawRectangleLinesEx,
DrawRectangleRec,
DrawText,
DrawTriangle,
EndDrawing,
Fade,
GetMousePosition,
InitWindow,
IsMouseButtonPressed,
IsMouseButtonReleased,
SetTargetFPS,
WindowShouldClose
};
require_once __DIR__ . '/../../vendor/autoload.php';
const MOUSE_SCALE_MARK_SIZE = 12;
// Initialization
//--------------------------------------------------------------------------------------
$screenWidth = 800;
$screenHeight = 450;
InitWindow($screenWidth, $screenHeight, 'raylib [shapes] example - rectangle scaling mouse');
$rec = new Rectangle(100, 100, 200, 80);
$mouseScaleMode = false;
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) { // Detect window close button or ESC key
// Update
//----------------------------------------------------------------------------------
$mousePosition = GetMousePosition();
if (
CheckCollisionPointRec($mousePosition, $rec)
&& CheckCollisionPointRec($mousePosition, new Rectangle(
$rec->x + $rec->width - MOUSE_SCALE_MARK_SIZE,
$rec->y + $rec->height - MOUSE_SCALE_MARK_SIZE,
MOUSE_SCALE_MARK_SIZE,
MOUSE_SCALE_MARK_SIZE,
))
) {
$mouseScaleReady = true;
if (IsMouseButtonPressed(Raylib::MOUSE_LEFT_BUTTON)) {
$mouseScaleMode = true;
}
} else {
$mouseScaleReady = false;
}
if ($mouseScaleMode) {
$mouseScaleReady = true;
$rec->width = ($mousePosition->x - $rec->x);
$rec->height = ($mousePosition->y - $rec->y);
if ($rec->width < MOUSE_SCALE_MARK_SIZE) {
$rec->width = MOUSE_SCALE_MARK_SIZE;
}
if ($rec->height < MOUSE_SCALE_MARK_SIZE) {
$rec->height = MOUSE_SCALE_MARK_SIZE;
}
if (IsMouseButtonReleased(Raylib::MOUSE_LEFT_BUTTON)) {
$mouseScaleMode = false;
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color::rayWhite());
// phpcs:disable Generic.WhiteSpace.ScopeIndent.IncorrectExact
DrawText('Scale rectangle dragging from bottom-right corner!', 10, 10, 20, Color::gray());
DrawRectangleRec($rec, Fade(Color::green(), 0.5));
if ($mouseScaleReady) {
DrawRectangleLinesEx($rec, 1, Color::red());
DrawTriangle(
new Vector2($rec->x + $rec->width - MOUSE_SCALE_MARK_SIZE, $rec->y + $rec->height),
new Vector2($rec->x + $rec->width, $rec->y + $rec->height),
new Vector2($rec->x + $rec->width, $rec->y + $rec->height - MOUSE_SCALE_MARK_SIZE),
Color::red()
);
}
// phpcs:enable Generic.WhiteSpace.ScopeIndent.IncorrectExact
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------