1 #include "select.h"
2
UISelect()3 UISelect::UISelect() : UISolidLayout(true) {
4
5 label = new UILabel("Select", false, 150.0f);
6 addElement(label);
7
8 options_layout = new UISolidLayout(false);
9 options_layout->setMargin(3.0f);
10 options_layout->parent = this;
11
12 setMargin(vec4(3.0f));
13 selected_option = 0;
14
15 open = false;
16 selectable = true;
17
18 selectex = texturemanager.grab("ui/select.png", false);
19 selectex->bind();
20 selectex->setFiltering(GL_NEAREST, GL_NEAREST);
21 selectex->setWrapStyle(GL_CLAMP);
22 }
23
setUI(UI * ui)24 void UISelect::setUI(UI* ui) {
25 UISolidLayout::setUI(ui);
26 options_layout->setUI(ui);
27 }
28
~UISelect()29 UISelect::~UISelect() {
30 delete options_layout;
31 if(selectex!=0) texturemanager.release(selectex);
32 }
33
click(const vec2 & pos)34 void UISelect::click(const vec2& pos) {
35 open = !open;
36 }
37
getSelectedOption()38 UIOptionLabel* UISelect::getSelectedOption() {
39 return selected_option;
40 }
41
selectOption(UIOptionLabel * option,bool submit)42 void UISelect::selectOption(UIOptionLabel* option, bool submit) {
43 label->setText(option->text);
44 selected_option = option;
45 if(submit) selected_option->submit();
46 open = false;
47 }
48
addOption(const std::string & name,const std::string & value,bool select_option)49 UIOptionLabel* UISelect::addOption(const std::string& name, const std::string& value, bool select_option) {
50 UIOptionLabel* option = new UIOptionLabel(this, name, value);
51
52 options_layout->addElement(option);
53
54 if(select_option) selectOption(option);
55
56 // if we have no selected option select option but dont submit
57 if(!selected_option) selectOption(option, false);
58
59 return option;
60 }
61
addOption(const std::string & name,UIAction * action,bool select_option)62 UIOptionLabel* UISelect::addOption(const std::string& name, UIAction* action, bool select_option) {
63 UIOptionLabel* option = new UIOptionLabel(this, name, action);
64
65 options_layout->addElement(option);
66
67 if(select_option) selectOption(option);
68
69 // if we have no selected option select option but dont submit
70 if(!selected_option) selectOption(option, false);
71
72 return option;
73 }
74
elementsAt(const vec2 & pos,std::list<UIElement * > & elements_found)75 void UISelect::elementsAt(const vec2& pos, std::list<UIElement*>& elements_found) {
76
77 UIElement* found = 0;
78
79 if(open) {
80 options_layout->elementsAt(pos, elements_found);
81 }
82
83 UISolidLayout::elementsAt(pos, elements_found);
84 }
85
updatePos(const vec2 & pos)86 void UISelect::updatePos(const vec2& pos) {
87
88 vec2 adjusted_pos = pos;
89
90 if(open && parent && adjusted_pos.y + options_layout->getRect().y > parent->getRect().y) {
91 adjusted_pos.y -= options_layout->getRect().y - this->getRect().y;
92 }
93
94 UISolidLayout::updatePos(adjusted_pos);
95 options_layout->updatePos(adjusted_pos);
96 }
97
update(float dt)98 void UISelect::update(float dt) {
99
100 updateZIndex();
101
102 if(open) options_layout->zindex = this->zindex + 1;
103 else options_layout->zindex = this->zindex;
104
105 UISolidLayout::update(dt);
106 options_layout->update(dt);
107 }
108
draw()109 void UISelect::draw() {
110 if(!open) {
111 UISolidLayout::draw();
112
113 selectex->bind();
114 glColor4fv(glm::value_ptr(ui->getSolidColour()));
115 drawQuad(pos + vec2(rect.x-16.0f,1.0f), vec2(16.0f, 16.0f), vec4(0.0f, 0.0f, 1.0f, 1.0f));
116 }
117 else {
118 options_layout->draw();
119 }
120 }
121
UIIntSelectAction(int * field,int value,UIAction * action)122 UIIntSelectAction::UIIntSelectAction(int* field, int value, UIAction* action) : field(field), value(value), action(action) {
123 }
124
perform()125 void UIIntSelectAction::perform() {
126 *field = value;
127 if(action != 0) {
128 action->perform();
129 }
130 }
131
132 // UIOptionLabel
133
UIOptionLabel(UISelect * select,const std::string & text,const std::string & value,UIAction * action)134 UIOptionLabel::UIOptionLabel(UISelect* select, const std::string& text, const std::string& value, UIAction* action)
135 : select(select), value(value), action(action), UILabel(text, false, 150.0f) {
136 selectable = true;
137 }
138
UIOptionLabel(UISelect * select,const std::string & text,UIAction * action)139 UIOptionLabel::UIOptionLabel(UISelect* select, const std::string& text, UIAction* action)
140 : select(select), value(text), action(action), UILabel(text, false, 150.0f) {
141 selectable = true;
142 }
143
submit()144 bool UIOptionLabel::submit() {
145
146 if(action!=0) {
147 action->perform();
148 return true;
149 }
150
151 return false;
152 }
153
click(const vec2 & pos)154 void UIOptionLabel::click(const vec2& pos) {
155 select->selectOption(this);
156 }
157