-
Notifications
You must be signed in to change notification settings - Fork 0
/
button.cpp
139 lines (112 loc) · 2.57 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <windows.h>
#include <GL/gl.h>
#include "button.h"
button::button()
{
active = false;
hoverActive = false;
renderable = true;
location.x = 275;
location.y = 75;
size.x = 12;
size.y = 12;
bgColor.R = 0.5f;
bgColor.G = 0.5f;
bgColor.B = 0.5f;
bgColor_active.R = 0.5f;
bgColor_active.G = 0.5f;
bgColor_active.B = 0.9f;
bgColor_hoverActive.R = 0.5f;
bgColor_hoverActive.G = 0.5f;
bgColor_hoverActive.B = 0.7f;
useLabelName = false;
l_name.location.x = location.x;
l_name.location.y = location.y;
l_name.alignment = ALIGN_RIGHT;
l_name.txtSize = (float)size.y;
}
button::~button()
{
}
int button::draw()
{
if (!renderable) return -1;
glBegin(GL_QUADS);
//BackGround
glColor3f(
active ? bgColor_active.R : hoverActive ? bgColor_hoverActive.R : bgColor.R,
active ? bgColor_active.G : hoverActive ? bgColor_hoverActive.G : bgColor.G,
active ? bgColor_active.B : hoverActive ? bgColor_hoverActive.B : bgColor.B );
glVertex2f((GLfloat)location.x, (GLfloat)location.y);
glVertex2f((GLfloat)location.x + size.x,(GLfloat)location.y);
glVertex2f((GLfloat)location.x + size.x,(GLfloat)location.y + size.y);
glVertex2f((GLfloat)location.x, (GLfloat)location.y + size.y);
glEnd();
if (useLabelName) l_name.draw();
return 0;
}
int button::forceUnActive()
{
active = false;
hoverActive = false;
return 0;
}
int button::setOnClick(std::function<void()> p)
{
onClick = p;
return 0;
}
int button::eventHandler(UINT message,WPARAM key,int mousex,int mousey)
{
(void) key;
if (message == WM_LBUTTONDOWN)
{
active = true;
hoverActive = false;
}
if (message == WM_MOUSEMOVE)
{
if (active)
{
// Move slider position to mouse x if can! calc value
}
else // Hovering? How do we detect left hover?
{
if (inBounds(mousex,mousey)) hoverActive = true;
else hoverActive = false;
}
}
if (message == WM_LBUTTONUP) // Unset Active
{
active = false;
if (inBounds(mousex,mousey))
{
hoverActive = true;
// Run external code here?
if (onClick) onClick();
}
//bgColor.B = 0.5f;
}
return 0;
}
void button::setColor(float red,float green, float blue, int style)
{
switch (style)
{
case BUTTON_COLOR:
bgColor.R = red;
bgColor.G = green;
bgColor.B = blue;
return;
case BUTTON_COLOR_ACTIVE:
bgColor_active.R = red;
bgColor_active.G = green;
bgColor_active.B = blue;
return;
case BUTTON_COLOR_HOVERACTIVE:
bgColor_hoverActive.R = red;
bgColor_hoverActive.G = green;
bgColor_hoverActive.B = blue;
return;
}
}