-
Notifications
You must be signed in to change notification settings - Fork 0
/
RadioButton.cpp
132 lines (102 loc) · 3.28 KB
/
RadioButton.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
/*
* File: RadioButton.cpp
* Author: paulocanedo
*
* Created on 9 de Outubro de 2012, 11:14
*/
#include "RadioButton.h"
#include <iostream>
vector<RadioButton*> RadioButton::buttons;
RadioButton::RadioButton() {
label = new Label();
label->setAlign(Component::ComponentAlign::MiddleLeft);
label->setParent(this);
this->selected = false;
this->setText("RadioButton");
this->paintState = PaintState::Normal;
this->groupId = 0;
buttons.push_back(this);
}
RadioButton::RadioButton(const RadioButton& orig) {
}
RadioButton::~RadioButton() {
}
string RadioButton::getComponentName() {
return "RadioButton";
}
bool RadioButton::isSelected() {
return selected;
}
RadioButton* RadioButton::querySelectedRadioButton() {
return querySelectedRadioButton(this->groupId);
}
RadioButton* RadioButton::querySelectedRadioButton(int groupId) {
for (vector<RadioButton*>::iterator it = buttons.begin(); it != buttons.end(); ++it) {
if (groupId == (*it)->groupId && (*it)->selected)
return (*it);
}
return NULL;
}
void RadioButton::clearGroupSelection() {
clearGroupSelection(this->groupId);
}
void RadioButton::clearGroupSelection(int groupId) {
for (vector<RadioButton*>::iterator it = buttons.begin(); it != buttons.end(); ++it) {
if (groupId == (*it)->groupId)
(*it)->selected = false;
}
}
void RadioButton::setSelected(bool selected) {
if (this->groupId > 0) {
clearGroupSelection(this->groupId);
}
this->selected = selected;
}
string RadioButton::getText() {
return label->getText();
}
void RadioButton::setText(string text) {
label->setText(text);
int radius = label->getFont()->lineHeight() * 0.7;
label->setLocation(radius * 2, 0);
if (isAutoDimension()) {
setDimension(getCalculatedDimension());
}
}
void RadioButton::actionExecuted() {
setSelected(!isSelected());
}
vec2 RadioButton::getCalculatedDimension() {
int radius = label->getFont()->lineHeight() * 0.7;
int gap = radius / 2;
vec2 dimension;
dimension.w = label->getDimension().w + radius + gap * 3;
dimension.h = label->getDimension().h + gap;
return dimension;
}
void RadioButton::paintButtonIcon(Graphics *graphics, bool selected, bool pressed, bool hover, int x, int y, int maxDiameter) {
int diameter = maxDiameter * 0.6;
int space = diameter * 0.25;
int gap = space * 2;
int cy = y + (maxDiameter - diameter) / 2;
if (selected || pressed) {
graphics->setColor(pcglBootstrapBlue);
graphics->fillOval(x + gap, cy, diameter, diameter);
if (selected && !pressed) {
graphics->setColor(0.2, 0.2, 0.2, 0.7);
graphics->fillOval(x + gap + space, cy + space, diameter - space * 2, diameter - space * 2);
}
}
graphics->setColor(hover || selected ? pcglBlack : pcglGray);
graphics->drawOval(x + gap, cy, diameter, diameter);
}
void RadioButton::render(Graphics* graphics) {
int x = getLocation().x;
int y = getLocation().y;
int w = getDimension().w;
int h = getDimension().h;
paintBackground(graphics);
paintButtonIcon(graphics, isSelected(), isStatePressed(), isStateHover(), x, y, min(w, h));
label->setDimension(label->getDimension().x, h);
label->render(graphics);
}