-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.cpp
54 lines (48 loc) · 1.47 KB
/
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
#include "button.h"
#include "define.h"
Button::Button(int top,LPCTSTR path_img_idle, LPCTSTR path_img_hovered, LPCTSTR path_img_pushed){
init_region(top);
loadimage(&img_idle, path_img_idle);
loadimage(&img_hovered, path_img_hovered);
loadimage(&img_pushed, path_img_pushed);
}
void Button::draw(){
switch (status) {
case Status::IDEL:
putimage(region.left, region.top, &img_idle);
break;
case Status::HOVERED:
putimage(region.left, region.top, &img_hovered);
break;
case Status::PUSHED:
putimage(region.left, region.top, &img_pushed);
break;
}
}
void Button::process_event(const ExMessage& msg){
switch (msg.message) {
case WM_MOUSEMOVE:
if (status == Status::IDEL && check_mouse_position(msg.x, msg.y))
status = Status::HOVERED;
else if (status == Status::HOVERED && !check_mouse_position(msg.x, msg.y))
status = Status::IDEL;
break;
case WM_LBUTTONDOWN:
if (check_mouse_position(msg.x, msg.y))
status = Status::PUSHED;
break;
case WM_LBUTTONUP:
if (status == Status::PUSHED && check_mouse_position(msg.x, msg.y))
on_click();
else status = Status::IDEL;
}
}
void Button::init_region(int top){
region.left = (WINDOW_WIDTH - BUTTON_WIDTH) / 2;
region.right = region.left + BUTTON_WIDTH;
region.top = top;
region.bottom = region.top + BUTTON_HEIGHT;
}
bool Button::check_mouse_position(int x, int y){
return region.left <= x && x <= region.right && region.top <= y && y <= region.bottom;
}