-
Notifications
You must be signed in to change notification settings - Fork 1
/
GCP_Select.h
166 lines (140 loc) · 4.56 KB
/
GCP_Select.h
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#ifndef GCP_SelectH
#define GCP_SelectH
#include "GCP_FormComponent.h"
#include "GCP_Button.h"
namespace gcp
{
//ÂÛÏÀÄÀÞÙÈÉ ÑÏÈÑÎÊ
class GCP_Select : public GCP_FormComponent
{
private:
GCP_SPointer<GCP_Button> _dropdownButton;
GCP_Vector<gcp_spButton> items;
int _iSelectedItem;
bool _isDrawingDropDown;
string _sSelectedText;
public:
GCP_Select()
{
_iSelectedItem = -1;
_isDrawingDropDown = false;
_dropdownButton = GCP_SPointer<GCP_Button>(new GCP_Button());
_dropdownButton->setWidthHeight(15, 15);
_dropdownButton->setCaption("\\/");
_dropdownButton->setOnMouseLeftClick(this, &GCP_Select::OnDropDown);
}
~GCP_Select()
{
//delete _dropdownButton;
/*for(int i=0; i<items.size(); i++)
delete items.at(i);*/
}
void addItem(string s)
{
gcp_spButton button = GCP_SPointer<GCP_Button>(new GCP_Button());
button->setWidthHeight(_position.width(), _position.height());
button->setStyle(GCP_ButtonBlackStyle);
button->setCaption(s);
button->setVisible(true);
button->setOnMouseLeftClick(this, &GCP_Select::OnDroppedButtonClick);
items.push_back(button);
}
void selectItem(unsigned int i)
{
if (i < items.size() && i >= 0)
{
_iSelectedItem = i;
_sSelectedText = items.at(_iSelectedItem)->getCaption();
}
}
const string& getSelectedItem()
{
static string emptyStr = "";
if (_iSelectedItem != -1)
return items.at(_iSelectedItem)->getCaption();
return emptyStr;
}
int getSelectedItemIndex()
{
return _iSelectedItem;
}
void OnDroppedButtonClick(void *obj)
{
GCP_SPointer<GCP_Button> button = GCP_SPointer<GCP_Button>((GCP_Button*)obj);
if (button != 0){
_sSelectedText = button->getCaption();
_isDrawingDropDown = false;
_iSelectedItem = -1;
for (unsigned int i = 0; i < items.size(); i++)
{
if (items.at(i) == button)
_iSelectedItem = i;
}
}
}
void OnDropDown(void* obj)
{
_isDrawingDropDown = !_isDrawingDropDown;
if (!_isDrawingDropDown)
for (unsigned int i = 0; i < items.size(); i++)
items.at(i)->setVisible(false);
}
void OnDraw(const GCP_Event &event)
{
if (!isVisible())
return;
_dropdownButton->setWidthHeight(_position.height(), _position.height());
_dropdownButton->setPosition(_position.x() + _position.width() - _dropdownButton->getPosition().width(), _position.y());
_dropdownButton->setStyle(GCP_ButtonBlackStyle); //!!!REV
GCP_Draw::Render()->Draw_FillRound(_position, 1, getStyle()->borderColor);
int iBorderWidth = getStyle()->borderWidth;
GCP_Draw::Render()->Draw_FillRound(_position.x() + iBorderWidth, _position.y() + iBorderWidth, _position.width() - iBorderWidth * 2, _position.height() - iBorderWidth * 2, 1, getStyle()->backgroundColor);
if (_isDrawingDropDown){
GCP_Draw::Render()->Draw_FillRect(_position.x(), _position.y() + 25, _position.width(), 25 * (items.size()), getStyle()->backgroundColor);
for (unsigned int i = 0; i < items.size(); i++)
{
items.at(i)->setVisible(true);
items.at(i)->setPosition(_position.x(), _position.y() + 25 * (i + 1));
items.at(i)->OnDraw(event);
}
}
else
for (unsigned int i = 0; i < items.size(); i++)
items.at(i)->setVisible(false);
iBorderWidth = getStyle()->borderWidth;
GCP_Draw::Render()->Draw_Text(_sSelectedText, GCP_Rect<int>(_position.x() + iBorderWidth * 2, _position.y() + iBorderWidth * 2, _position.width(), 20), getStyle(), &drawdata);
_dropdownButton->OnDraw(event);
basicOnDraw(event);
}
gcp_formEvent OnEvent(const GCP_Event &event)
{
gcp_formEvent evt;
evt.isEventInsideForm = false;
evt.isEventOnFormHead = false;
evt.isFormDragged = false;
if (!isVisible())
return evt;
if (event.eventType % 2 != 0) //if Event is Global
{
_dropdownButton->OnEvent(event);
if (!event.isPassingOnlyGlobalEvent)
if (_dropdownButton->checkCollisionBox(event.mousex, event.mousey))
_dropdownButton->OnEvent(MakeEventLocal(event));
for (unsigned int i = 0; i < items.size(); i++){
items.at(i)->OnEvent(event);
if (!event.isPassingOnlyGlobalEvent)
if (items.at(i)->isVisible())
if (items.at(i)->checkCollisionBox(event.mousex, event.mousey))
{
items.at(i)->OnEvent(MakeEventLocal(event));
}
}
if (event.eventType == GCP_ON_MOUSE_GUP)
_isDrawingDropDown = false;
}
basicOnEvent(event);
return evt;
}
};
}
#endif