-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.cpp
130 lines (125 loc) · 2.72 KB
/
gui.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
#include "gui.h"
#include <iostream>
#include <cmath>
Slider::Slider() {}
Slider::~Slider() {}
Slider::Slider(int x, int y, int min, int max, int inc) {
this->x = x;
offsetx = x;
this->y = y;
this->min = min;
this->max = max;
this->inc = inc;
hovering = clicked = false;
lastX = (max / inc) * SLIDER_RADIUS;
value = min;
}
bool Slider::checkMouse(int mousex, int mousey) {
float xdiff = mousex - offsetx;
float ydiff = mousey - y;
float dist = sqrt(xdiff*xdiff + ydiff*ydiff);
if(dist <= SLIDER_RADIUS) {
hovering = true;
return true;
} else {
hovering = false;
return false;
}
}
bool Slider::slide(int mousex) {
if(clicked) {
if(mousex > offsetx) {
// Sliding right
while(offsetx + SLIDER_RADIUS < mousex && value < max) {
offsetx += SLIDER_RADIUS;
value += inc;
}
} else if(mousex < offsetx) {
// Sliding left
while(offsetx - SLIDER_RADIUS > mousex && value > min) {
offsetx -= SLIDER_RADIUS;
value -= inc;
}
}
return true;
}
return false;
}
void Slider::render(X11& x11) {
x11.setColor(255,255,255);
x11.drawLine(x, y, x + lastX, y);
x11.setColor(0,255,0);
x11.fillCircle(offsetx, y, SLIDER_RADIUS);
std::string val = std::to_string(value);
x11.setColor(0,0,0);
x11.drawString(offsetx-(SLIDER_RADIUS/2), y, val);
}
void Slider::setClicked(bool b) {
clicked = b;
}
void Slider::setHovering(bool b) {
hovering = b;
}
int Slider::getValue() {
return value;
}
int Slider::getClicked() {
return clicked;
}
int Slider::getHovering() {
return hovering;
}
int Slider::getLastX() {
return lastX;
}
template <class T>
Button<T>::Button() {
}
template <class T>
Button<T>::Button(int x,int y,float w,float h,T val) {
this->x = x;
this->y = y;
this->w = w;
this->h = h;
this->val = val;
}
template <class T>
Button<T>::~Button() {
}
template <class T>
void Button<T>::init(int x,int y,float w,float h,T val) {
this->x = x;
this->y = y;
this->w = w;
this->h = h;
this->val = val;
}
template <class T>
bool Button<T>::checkMouse(int mousex,int mousey) {
if(mousex >= x && mousex <= x + w && mousey >= y && mousey <= y + h) {
return true;
} else {
return false;
}
}
template <class T>
int Button<T>::getX() {
return x;
}
template <class T>
int Button<T>::getY() {
return y;
}
template <class T>
T Button<T>::getValue() {
return val;
}
template <class T>
float Button<T>::getWidth() {
return w;
}
template <class T>
float Button<T>::getHeight() {
return h;
}
template class Button<int>;