-
Notifications
You must be signed in to change notification settings - Fork 34
/
example3_button.cpp
63 lines (51 loc) · 2.19 KB
/
example3_button.cpp
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
#define IMM2D_IMPLEMENTATION
#include "immediate2d.h"
//
// Example 3 - Button
//
// Draws two buttons. Clicking either will fill most of the screen with that button's color.
//
// Exercises:
// 1. Create a Button struct that contains a position and color
// 2. Migrate "blueButtonX", etc. over to use the new struct: "Button button1 = { 10, 90, Blue };"
// 3. Consolidate the repeated drawing code (before the loop) into a separate "void DrawButton(Button b)" function
// 4. Consolidate the repeated hit-testing code (inside the loop) into a separate "bool TestButton(Button b, int x, int y)" function
// 5. Add another button using your new functions
//
const int ButtonSize = 20;
void run()
{
int blueButtonX = 10;
int blueButtonY = 90;
int greenButtonX = 40;
int greenButtonY = 90;
// Draw our buttons with a nice bevel effect
DrawRectangle(blueButtonX, blueButtonY, ButtonSize, ButtonSize, DarkGray);
DrawRectangle(blueButtonX, blueButtonY, ButtonSize - 1, ButtonSize - 1, White);
DrawRectangle(blueButtonX + 1, blueButtonY + 1, ButtonSize - 2, ButtonSize - 2, Blue);
DrawRectangle(greenButtonX, greenButtonY, ButtonSize, ButtonSize, DarkGray);
DrawRectangle(greenButtonX, greenButtonY, ButtonSize - 1, ButtonSize - 1, White);
DrawRectangle(greenButtonX + 1, greenButtonY + 1, ButtonSize - 2, ButtonSize - 2, Green);
// Loop forever
while (true)
{
// Adding a short wait between "frames" is a good idea so the CPU doesn't max out at 100%
Wait(1);
// Don't do anything unless the mouse button is pressed
if (!LeftMousePressed()) continue;
int x = MouseX();
int y = MouseY();
// Check if we're clicking the blue button
if (x >= blueButtonX && x < blueButtonX + ButtonSize
&& y >= blueButtonY && y < blueButtonY + ButtonSize)
{
DrawRectangle(0, 0, Width, 80, Blue);
}
// Check if we're clicking the green button
if (x >= greenButtonX && x < greenButtonX + ButtonSize
&& y >= greenButtonY && y < greenButtonY + ButtonSize)
{
DrawRectangle(0, 0, Width, 80, Green);
}
}
}