1 | /******************************************************************************************* |
2 | * |
3 | * raylib [shapes] example - rectangle scaling by mouse |
4 | * |
5 | * This example has been created using raylib 2.5 (www.raylib.com) |
6 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
7 | * |
8 | * Example contributed by Vlad Adrian (@demizdor) and reviewed by Ramon Santamaria (@raysan5) |
9 | * |
10 | * Copyright (c) 2018 Vlad Adrian (@demizdor) and Ramon Santamaria (@raysan5) |
11 | * |
12 | ********************************************************************************************/ |
13 | |
14 | #include "raylib.h" |
15 | |
16 | #define MOUSE_SCALE_MARK_SIZE 12 |
17 | |
18 | int main(void) |
19 | { |
20 | // Initialization |
21 | //-------------------------------------------------------------------------------------- |
22 | const int screenWidth = 800; |
23 | const int screenHeight = 450; |
24 | |
25 | InitWindow(screenWidth, screenHeight, "raylib [shapes] example - rectangle scaling mouse" ); |
26 | |
27 | Rectangle rec = { 100, 100, 200, 80 }; |
28 | |
29 | Vector2 mousePosition = { 0 }; |
30 | |
31 | bool mouseScaleReady = false; |
32 | bool mouseScaleMode = false; |
33 | |
34 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
35 | //-------------------------------------------------------------------------------------- |
36 | |
37 | // Main game loop |
38 | while (!WindowShouldClose()) // Detect window close button or ESC key |
39 | { |
40 | // Update |
41 | //---------------------------------------------------------------------------------- |
42 | mousePosition = GetMousePosition(); |
43 | |
44 | if (CheckCollisionPointRec(mousePosition, rec) && |
45 | CheckCollisionPointRec(mousePosition, (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 })) |
46 | { |
47 | mouseScaleReady = true; |
48 | if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) mouseScaleMode = true; |
49 | } |
50 | else mouseScaleReady = false; |
51 | |
52 | if (mouseScaleMode) |
53 | { |
54 | mouseScaleReady = true; |
55 | |
56 | rec.width = (mousePosition.x - rec.x); |
57 | rec.height = (mousePosition.y - rec.y); |
58 | |
59 | if (rec.width < MOUSE_SCALE_MARK_SIZE) rec.width = MOUSE_SCALE_MARK_SIZE; |
60 | if (rec.height < MOUSE_SCALE_MARK_SIZE) rec.height = MOUSE_SCALE_MARK_SIZE; |
61 | |
62 | if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) mouseScaleMode = false; |
63 | } |
64 | //---------------------------------------------------------------------------------- |
65 | |
66 | // Draw |
67 | //---------------------------------------------------------------------------------- |
68 | BeginDrawing(); |
69 | |
70 | ClearBackground(RAYWHITE); |
71 | |
72 | DrawText("Scale rectangle dragging from bottom-right corner!" , 10, 10, 20, GRAY); |
73 | |
74 | DrawRectangleRec(rec, Fade(GREEN, 0.5f)); |
75 | |
76 | if (mouseScaleReady) |
77 | { |
78 | DrawRectangleLinesEx(rec, 1, RED); |
79 | DrawTriangle((Vector2){ rec.x + rec.width - MOUSE_SCALE_MARK_SIZE, rec.y + rec.height }, |
80 | (Vector2){ rec.x + rec.width, rec.y + rec.height }, |
81 | (Vector2){ rec.x + rec.width, rec.y + rec.height - MOUSE_SCALE_MARK_SIZE }, RED); |
82 | } |
83 | |
84 | EndDrawing(); |
85 | //---------------------------------------------------------------------------------- |
86 | } |
87 | |
88 | // De-Initialization |
89 | //-------------------------------------------------------------------------------------- |
90 | CloseWindow(); // Close window and OpenGL context |
91 | //-------------------------------------------------------------------------------------- |
92 | |
93 | return 0; |
94 | } |