-
Notifications
You must be signed in to change notification settings - Fork 6
/
GUIDropDownMenu.cpp
211 lines (143 loc) · 5.51 KB
/
GUIDropDownMenu.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//
// GUIDropDown.cpp
// Deep
//
// Created by Nathan Daly on 8/22/13.
// Copyright (c) 2013 Lions Entertainment. All rights reserved.
//
#include "GUIDropDownMenu.h"
#include "GUIButton.h"
#include "GUITextViews.h"
using std::list;
using std::string;
namespace GUI {
class MenuEntry : public TextButton {
public:
MenuEntry(const string &text, int text_size, DropDownMenu &menu_, int index_)
:TextButton(text), menu(menu_), index(index_)
{
button_text->set_text_size(text_size);
set_image(GUIImage("GUIImages/dropdown_row.bmp"));
set_hovered_image(GUIImage("GUIImages/dropdown_row_hl.bmp"));
set_clicked_image(GUIImage("GUIImages/dropdown_row_hl.bmp"));
}
virtual void operation() {
menu.selected_entry_idx = index;
}
// overridden to return false so Menu can receive click
virtual bool handle_mouse_down(DispPoint coord) {
// run parent mouse up to call operation();
TextButton::handle_mouse_down(coord);
// return false so Menu can be clicked.
return false;
}
// overridden to return false so Menu can receive click
virtual bool handle_mouse_up(DispPoint coord) {
// run parent mouse up to call operation();
TextButton::handle_mouse_up(coord);
// return false so Menu can be clicked.
return false;
}
// overridden to not highlight if menu isn't open.
virtual bool handle_mouse_motion(DispPoint coord, DispPoint rel_motion) {
if (!menu.clicked) {
return false;
}
return TextButton::handle_mouse_motion(coord, rel_motion);
}
private:
DropDownMenu &menu;
int index;
};
const int vertical_padding = 5; // extra space above/below text
DropDownMenu::DropDownMenu(int w, int text_size_)
:View(w, 1)
{
init_menu_view(text_size_);
}
void DropDownMenu::init_menu_view(int text_size_) {
// Init main view
resize(get_w(), text_size_ + vertical_padding*2);
text_size = text_size_;
selected_entry_idx = 0;
menu = 0; clicked = false;
set_clear_color(green_color_c);
fill_with_color(get_clear_color());
// Init menu view
int row_size = (text_size + vertical_padding*2);
menu = new View(get_w(), (int)(entries.size() ? entries.size() : 1) * row_size);
// always display at least one entry
menu->draw_onto_self(GUIImage("GUIImages/dropdown_row.bmp"), DispPoint());
// display full menu
int i = 0;
for (list<string>::iterator it = entries.begin(); it != entries.end(); ++it, ++i) {
// menu->attach_subview(new MenuEntry(*it, text_size, *this, i),
// DispPoint(0, i * row_size));
menu->draw_onto_self(GUIImage("GUIImages/dropdown_row.bmp"),
DispPoint(0, i * row_size));
menu->attach_subview(createTextView(*it, text_size),
DispPoint(0, i * row_size));
}
attach_subview(menu, DispPoint());
}
void DropDownMenu::update_menu_view() {
// always display at least one entry
menu->draw_onto_self(GUIImage("GUIImages/dropdown_row.bmp"), DispPoint());
// display full menu
int row_size = (text_size + vertical_padding*2);
for (int i = 0; i < entries.size(); ++i) {
menu->draw_onto_self(GUIImage("GUIImages/dropdown_row.bmp"),
DispPoint(0, i * row_size));
}
}
// Mouse Events. Following three functions all work the same:
// Returns true if the mouse-event is finished being handled.
// If returns false, handling will continue up the chain.
// May optionally call capture_focus() to become the target for keypresses.
bool DropDownMenu::handle_mouse_down(DispPoint coord) {
capture_focus();
if (clicked) {
return false;
}
clicked = true;
pre_click_pos = get_rel_pos();
resize(get_w(), menu->get_h());
fill_with_color(get_clear_color());
int vert_adj = -selected_entry_idx * (text_size + vertical_padding*2); // (up)
move_subview(menu, DispPoint()); // reset menu subview
get_parent()->move_subview(this, get_rel_pos() + DispPoint(0,vert_adj));
return true;
}
bool DropDownMenu::handle_mouse_up(DispPoint coord) {
if (!clicked) {
return false;
}
clicked = false;
// Find clicked row
int row_size = text_size + vertical_padding*2;
selected_entry_idx = coord.y / row_size;
// order matters incase entries is empty to avoid -1
if (selected_entry_idx >= entries.size())
{ selected_entry_idx = entries.size()-1; }
if (selected_entry_idx <= 0)
{ selected_entry_idx = 0; }
update_menu_view();
resize(get_w(), row_size);
int vert_adj = -selected_entry_idx * row_size; // (move subview up)
move_subview(menu, DispPoint(0, vert_adj)); // move correct entry to visible
get_parent()->move_subview(this, pre_click_pos);
lose_focus();
return true;
}
bool DropDownMenu::handle_mouse_motion(DispPoint coord, DispPoint rel_motion) {
if (!clicked) {
return false;
}
int row_size = (text_size + vertical_padding*2);
int row_to_hl = coord.y / row_size;
update_menu_view();
menu->draw_onto_self(GUIImage("GUIImages/dropdown_row_hl.bmp"),
DispPoint(0, row_to_hl * row_size));
return true;
}
} // namespace GUI