1 /*************************************************************************/
2 /*  option_button.cpp                                                    */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 
31 #include "option_button.h"
32 #include "core/print_string.h"
33 
get_minimum_size() const34 Size2 OptionButton::get_minimum_size() const {
35 
36 	Size2 minsize = Button::get_minimum_size();
37 
38 	if (has_icon("arrow")) {
39 		const Size2 padding = get_stylebox("normal")->get_minimum_size();
40 		const Size2 arrow_size = Control::get_icon("arrow")->get_size();
41 
42 		Size2 content_size = minsize - padding;
43 		content_size.width += arrow_size.width + get_constant("hseparation");
44 		content_size.height = MAX(content_size.height, arrow_size.height);
45 
46 		minsize = content_size + padding;
47 	}
48 
49 	return minsize;
50 }
51 
_notification(int p_what)52 void OptionButton::_notification(int p_what) {
53 
54 	switch (p_what) {
55 		case NOTIFICATION_DRAW: {
56 
57 			if (!has_icon("arrow"))
58 				return;
59 
60 			RID ci = get_canvas_item();
61 			Ref<Texture> arrow = Control::get_icon("arrow");
62 			Color clr = Color(1, 1, 1);
63 			if (get_constant("modulate_arrow")) {
64 				switch (get_draw_mode()) {
65 					case DRAW_PRESSED:
66 						clr = get_color("font_color_pressed");
67 						break;
68 					case DRAW_HOVER:
69 						clr = get_color("font_color_hover");
70 						break;
71 					case DRAW_DISABLED:
72 						clr = get_color("font_color_disabled");
73 						break;
74 					default:
75 						clr = get_color("font_color");
76 				}
77 			}
78 
79 			Size2 size = get_size();
80 
81 			Point2 ofs(size.width - arrow->get_width() - get_constant("arrow_margin"), int(Math::abs((size.height - arrow->get_height()) / 2)));
82 			arrow->draw(ci, ofs, clr);
83 		} break;
84 		case NOTIFICATION_THEME_CHANGED: {
85 
86 			if (has_icon("arrow")) {
87 				_set_internal_margin(MARGIN_RIGHT, Control::get_icon("arrow")->get_width());
88 			}
89 		} break;
90 		case NOTIFICATION_VISIBILITY_CHANGED: {
91 
92 			if (!is_visible_in_tree()) {
93 				popup->hide();
94 			}
95 		} break;
96 	}
97 }
98 
_focused(int p_which)99 void OptionButton::_focused(int p_which) {
100 	emit_signal("item_focused", p_which);
101 }
102 
_selected(int p_which)103 void OptionButton::_selected(int p_which) {
104 
105 	_select(p_which, true);
106 }
107 
pressed()108 void OptionButton::pressed() {
109 
110 	Size2 size = get_size();
111 	popup->set_global_position(get_global_position() + Size2(0, size.height * get_global_transform().get_scale().y));
112 	popup->set_size(Size2(size.width, 0));
113 	popup->set_scale(get_global_transform().get_scale());
114 	popup->popup();
115 }
116 
add_icon_item(const Ref<Texture> & p_icon,const String & p_label,int p_id)117 void OptionButton::add_icon_item(const Ref<Texture> &p_icon, const String &p_label, int p_id) {
118 
119 	popup->add_icon_radio_check_item(p_icon, p_label, p_id);
120 	if (popup->get_item_count() == 1)
121 		select(0);
122 }
add_item(const String & p_label,int p_id)123 void OptionButton::add_item(const String &p_label, int p_id) {
124 
125 	popup->add_radio_check_item(p_label, p_id);
126 	if (popup->get_item_count() == 1)
127 		select(0);
128 }
129 
set_item_text(int p_idx,const String & p_text)130 void OptionButton::set_item_text(int p_idx, const String &p_text) {
131 
132 	popup->set_item_text(p_idx, p_text);
133 
134 	if (current == p_idx)
135 		set_text(p_text);
136 }
set_item_icon(int p_idx,const Ref<Texture> & p_icon)137 void OptionButton::set_item_icon(int p_idx, const Ref<Texture> &p_icon) {
138 
139 	popup->set_item_icon(p_idx, p_icon);
140 
141 	if (current == p_idx)
142 		set_icon(p_icon);
143 }
set_item_id(int p_idx,int p_id)144 void OptionButton::set_item_id(int p_idx, int p_id) {
145 
146 	popup->set_item_id(p_idx, p_id);
147 }
148 
set_item_metadata(int p_idx,const Variant & p_metadata)149 void OptionButton::set_item_metadata(int p_idx, const Variant &p_metadata) {
150 
151 	popup->set_item_metadata(p_idx, p_metadata);
152 }
153 
set_item_disabled(int p_idx,bool p_disabled)154 void OptionButton::set_item_disabled(int p_idx, bool p_disabled) {
155 
156 	popup->set_item_disabled(p_idx, p_disabled);
157 }
158 
get_item_text(int p_idx) const159 String OptionButton::get_item_text(int p_idx) const {
160 
161 	return popup->get_item_text(p_idx);
162 }
163 
get_item_icon(int p_idx) const164 Ref<Texture> OptionButton::get_item_icon(int p_idx) const {
165 
166 	return popup->get_item_icon(p_idx);
167 }
168 
get_item_id(int p_idx) const169 int OptionButton::get_item_id(int p_idx) const {
170 
171 	return popup->get_item_id(p_idx);
172 }
173 
get_item_index(int p_id) const174 int OptionButton::get_item_index(int p_id) const {
175 
176 	return popup->get_item_index(p_id);
177 }
178 
get_item_metadata(int p_idx) const179 Variant OptionButton::get_item_metadata(int p_idx) const {
180 
181 	return popup->get_item_metadata(p_idx);
182 }
183 
is_item_disabled(int p_idx) const184 bool OptionButton::is_item_disabled(int p_idx) const {
185 
186 	return popup->is_item_disabled(p_idx);
187 }
188 
get_item_count() const189 int OptionButton::get_item_count() const {
190 
191 	return popup->get_item_count();
192 }
193 
add_separator()194 void OptionButton::add_separator() {
195 
196 	popup->add_separator();
197 }
198 
clear()199 void OptionButton::clear() {
200 
201 	popup->clear();
202 	set_text("");
203 	current = -1;
204 }
205 
_select(int p_which,bool p_emit)206 void OptionButton::_select(int p_which, bool p_emit) {
207 
208 	if (p_which < 0)
209 		return;
210 	if (p_which == current)
211 		return;
212 
213 	ERR_FAIL_INDEX(p_which, popup->get_item_count());
214 
215 	for (int i = 0; i < popup->get_item_count(); i++) {
216 
217 		popup->set_item_checked(i, i == p_which);
218 	}
219 
220 	current = p_which;
221 	set_text(popup->get_item_text(current));
222 	set_icon(popup->get_item_icon(current));
223 
224 	if (is_inside_tree() && p_emit)
225 		emit_signal("item_selected", current);
226 }
227 
_select_int(int p_which)228 void OptionButton::_select_int(int p_which) {
229 
230 	if (p_which < 0 || p_which >= popup->get_item_count())
231 		return;
232 	_select(p_which, false);
233 }
234 
select(int p_idx)235 void OptionButton::select(int p_idx) {
236 
237 	_select(p_idx, false);
238 }
239 
get_selected() const240 int OptionButton::get_selected() const {
241 
242 	return current;
243 }
244 
get_selected_id() const245 int OptionButton::get_selected_id() const {
246 
247 	int idx = get_selected();
248 	if (idx < 0)
249 		return 0;
250 	return get_item_id(current);
251 }
get_selected_metadata() const252 Variant OptionButton::get_selected_metadata() const {
253 
254 	int idx = get_selected();
255 	if (idx < 0)
256 		return Variant();
257 	return get_item_metadata(current);
258 }
259 
remove_item(int p_idx)260 void OptionButton::remove_item(int p_idx) {
261 
262 	popup->remove_item(p_idx);
263 }
264 
get_popup() const265 PopupMenu *OptionButton::get_popup() const {
266 
267 	return popup;
268 }
269 
_get_items() const270 Array OptionButton::_get_items() const {
271 
272 	Array items;
273 	for (int i = 0; i < get_item_count(); i++) {
274 
275 		items.push_back(get_item_text(i));
276 		items.push_back(get_item_icon(i));
277 		items.push_back(is_item_disabled(i));
278 		items.push_back(get_item_id(i));
279 		items.push_back(get_item_metadata(i));
280 	}
281 
282 	return items;
283 }
_set_items(const Array & p_items)284 void OptionButton::_set_items(const Array &p_items) {
285 
286 	ERR_FAIL_COND(p_items.size() % 5);
287 	clear();
288 
289 	for (int i = 0; i < p_items.size(); i += 5) {
290 
291 		String text = p_items[i + 0];
292 		Ref<Texture> icon = p_items[i + 1];
293 		bool disabled = p_items[i + 2];
294 		int id = p_items[i + 3];
295 		Variant meta = p_items[i + 4];
296 
297 		int idx = get_item_count();
298 		add_item(text, id);
299 		set_item_icon(idx, icon);
300 		set_item_disabled(idx, disabled);
301 		set_item_metadata(idx, meta);
302 	}
303 }
304 
get_translatable_strings(List<String> * p_strings) const305 void OptionButton::get_translatable_strings(List<String> *p_strings) const {
306 
307 	popup->get_translatable_strings(p_strings);
308 }
309 
_bind_methods()310 void OptionButton::_bind_methods() {
311 
312 	ClassDB::bind_method(D_METHOD("_selected"), &OptionButton::_selected);
313 	ClassDB::bind_method(D_METHOD("_focused"), &OptionButton::_focused);
314 
315 	ClassDB::bind_method(D_METHOD("add_item", "label", "id"), &OptionButton::add_item, DEFVAL(-1));
316 	ClassDB::bind_method(D_METHOD("add_icon_item", "texture", "label", "id"), &OptionButton::add_icon_item, DEFVAL(-1));
317 	ClassDB::bind_method(D_METHOD("set_item_text", "idx", "text"), &OptionButton::set_item_text);
318 	ClassDB::bind_method(D_METHOD("set_item_icon", "idx", "texture"), &OptionButton::set_item_icon);
319 	ClassDB::bind_method(D_METHOD("set_item_disabled", "idx", "disabled"), &OptionButton::set_item_disabled);
320 	ClassDB::bind_method(D_METHOD("set_item_id", "idx", "id"), &OptionButton::set_item_id);
321 	ClassDB::bind_method(D_METHOD("set_item_metadata", "idx", "metadata"), &OptionButton::set_item_metadata);
322 	ClassDB::bind_method(D_METHOD("get_item_text", "idx"), &OptionButton::get_item_text);
323 	ClassDB::bind_method(D_METHOD("get_item_icon", "idx"), &OptionButton::get_item_icon);
324 	ClassDB::bind_method(D_METHOD("get_item_id", "idx"), &OptionButton::get_item_id);
325 	ClassDB::bind_method(D_METHOD("get_item_index", "id"), &OptionButton::get_item_index);
326 	ClassDB::bind_method(D_METHOD("get_item_metadata", "idx"), &OptionButton::get_item_metadata);
327 	ClassDB::bind_method(D_METHOD("is_item_disabled", "idx"), &OptionButton::is_item_disabled);
328 	ClassDB::bind_method(D_METHOD("get_item_count"), &OptionButton::get_item_count);
329 	ClassDB::bind_method(D_METHOD("add_separator"), &OptionButton::add_separator);
330 	ClassDB::bind_method(D_METHOD("clear"), &OptionButton::clear);
331 	ClassDB::bind_method(D_METHOD("select", "idx"), &OptionButton::select);
332 	ClassDB::bind_method(D_METHOD("get_selected"), &OptionButton::get_selected);
333 	ClassDB::bind_method(D_METHOD("get_selected_id"), &OptionButton::get_selected_id);
334 	ClassDB::bind_method(D_METHOD("get_selected_metadata"), &OptionButton::get_selected_metadata);
335 	ClassDB::bind_method(D_METHOD("remove_item", "idx"), &OptionButton::remove_item);
336 	ClassDB::bind_method(D_METHOD("_select_int"), &OptionButton::_select_int);
337 
338 	ClassDB::bind_method(D_METHOD("get_popup"), &OptionButton::get_popup);
339 
340 	ClassDB::bind_method(D_METHOD("_set_items"), &OptionButton::_set_items);
341 	ClassDB::bind_method(D_METHOD("_get_items"), &OptionButton::_get_items);
342 
343 	ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "items", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_items", "_get_items");
344 	// "selected" property must come after "items", otherwise GH-10213 occurs.
345 	ADD_PROPERTY(PropertyInfo(Variant::INT, "selected"), "_select_int", "get_selected");
346 	ADD_SIGNAL(MethodInfo("item_selected", PropertyInfo(Variant::INT, "index")));
347 	ADD_SIGNAL(MethodInfo("item_focused", PropertyInfo(Variant::INT, "index")));
348 }
349 
OptionButton()350 OptionButton::OptionButton() {
351 
352 	current = -1;
353 	set_toggle_mode(true);
354 	set_text_align(ALIGN_LEFT);
355 	set_action_mode(ACTION_MODE_BUTTON_PRESS);
356 	if (has_icon("arrow")) {
357 		_set_internal_margin(MARGIN_RIGHT, Control::get_icon("arrow")->get_width());
358 	}
359 
360 	popup = memnew(PopupMenu);
361 	popup->hide();
362 	add_child(popup);
363 	popup->set_pass_on_modal_close_click(false);
364 	popup->set_notify_transform(true);
365 	popup->set_allow_search(true);
366 	popup->connect("index_pressed", this, "_selected");
367 	popup->connect("id_focused", this, "_focused");
368 	popup->connect("popup_hide", this, "set_pressed", varray(false));
369 }
370 
~OptionButton()371 OptionButton::~OptionButton() {
372 }
373