-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWidgetButton.cpp
More file actions
103 lines (86 loc) · 2.3 KB
/
Copy pathWidgetButton.cpp
File metadata and controls
103 lines (86 loc) · 2.3 KB
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
#include "WidgetButton.h"
void Button::onButtonPress(int x, int y, int buttonCode) {
pressed = true;
std::cout << "Button pressed ID = " << componentID << std::endl;
}
Button::Button(int TLx, int TLy, int BRx, int BRy) :
TLPosition(TLx, TLy), BRPosition(BRx, BRy), GUIComponentBase(TLx, TLy) {
width = BRPosition.x - TLPosition.x;
height = BRPosition.y - TLPosition.y;
clickCallback = NULL;
buttonPressCallback = NULL;
drawCallBack = NULL;
dragCallBack = NULL;
pressed = false;
focused = false;
}
Point2i Button::getTLPosition() {
return TLPosition;
}
void Button::setTLPosition(int x, int y) {
TLPosition.x = x;
TLPosition.y = y;
}
void Button::drawDefaultButton(cairo_t *context) {
if (pressed)
cairo_set_source_rgba(context, 0.5, 0.5, 1, 1);
else if (focused)
cairo_set_source_rgba(context, 0.5, 0.8, 1, 1);
else
cairo_set_source_rgba(context, 0.5, 0.5, 1, 0.5);
cairo_rectangle(context, TLPosition.x, TLPosition.y, width, height);
cairo_fill(context);
}
void Button::onClick(int x, int y) {
pressed = false;
if (clickCallback)
clickCallback(this, x, y);
std::cout << "Button clicked ID = " << componentID << std::endl;
}
void Button::onDraw(cairo_t* context) {
if (drawCallBack)
drawCallBack(this, context);
else {
drawDefaultButton(context);
}
//std::cout<<"Button virtually drawn ID = "<<componentID<<std::endl;
}
void Button::onRemap(uint_fast8_t* map) {
for (int i = TLPosition.x; i < BRPosition.x; i++) {
for (int j = TLPosition.y; j < BRPosition.y; j++) {
map[TranslateMap(i, j)] = componentID;
}
}
}
void Button::regOnDraw(void (*drawCB)(Button*, cairo_t*)) {
drawCallBack = drawCB;
}
void Button::regOnDrag(void (*dragCB)(Button*, int, int)) {
dragCallBack = dragCB;
}
void Button::onDrag(int x, int y) {
if (dragCallBack)
dragCallBack(this, x, y);
else {
if (x > TLPosition.x && x < BRPosition.x && y > TLPosition.y
&& y < BRPosition.y)
pressed = true;
else
pressed = false;
}
}
void Button::onDrop(int x, int y) {
if (idAtGUImapLocation(x, y) == componentID)
onClick(x, y);
}
void Button::onFocus() {
focused = true;
}
void Button::onUnFocus() {
focused = false;
}