| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*  option_button.cpp                                                    */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							| 
									
										
										
										
											2017-08-27 14:16:55 +02:00
										 |  |  | /*                      https://godotengine.org                          */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2022-01-03 21:27:34 +01:00
										 |  |  | /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md).   */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the       */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including   */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,   */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to    */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to */ | 
					
						
							|  |  |  | /* the following conditions:                                             */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be        */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.       */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | #include "option_button.h"
 | 
					
						
							| 
									
										
										
										
											2020-02-21 23:26:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-07 19:33:38 -03:00
										 |  |  | #include "core/string/print_string.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-28 17:03:45 -05:00
										 |  |  | static const int NONE_SELECTED = -1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | Size2 OptionButton::get_minimum_size() const { | 
					
						
							|  |  |  | 	Size2 minsize = Button::get_minimum_size(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 	if (has_theme_icon(SNAME("arrow"))) { | 
					
						
							|  |  |  | 		const Size2 padding = get_theme_stylebox(SNAME("normal"))->get_minimum_size(); | 
					
						
							|  |  |  | 		const Size2 arrow_size = Control::get_theme_icon(SNAME("arrow"))->get_size(); | 
					
						
							| 
									
										
										
										
											2020-01-27 14:52:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		Size2 content_size = minsize - padding; | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 		content_size.width += arrow_size.width + get_theme_constant(SNAME("hseparation")); | 
					
						
							| 
									
										
										
										
											2020-01-27 14:52:04 +08:00
										 |  |  | 		content_size.height = MAX(content_size.height, arrow_size.height); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		minsize = content_size + padding; | 
					
						
							| 
									
										
										
										
											2019-12-28 12:03:04 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return minsize; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void OptionButton::_notification(int p_what) { | 
					
						
							| 
									
										
										
										
											2019-07-29 16:21:54 +02:00
										 |  |  | 	switch (p_what) { | 
					
						
							|  |  |  | 		case NOTIFICATION_DRAW: { | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 			if (!has_theme_icon(SNAME("arrow"))) { | 
					
						
							| 
									
										
										
										
											2019-07-29 16:21:54 +02:00
										 |  |  | 				return; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-07-29 16:21:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			RID ci = get_canvas_item(); | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 			Ref<Texture2D> arrow = Control::get_theme_icon(SNAME("arrow")); | 
					
						
							| 
									
										
										
										
											2019-07-29 16:21:54 +02:00
										 |  |  | 			Color clr = Color(1, 1, 1); | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 			if (get_theme_constant(SNAME("modulate_arrow"))) { | 
					
						
							| 
									
										
										
										
											2019-07-29 16:21:54 +02:00
										 |  |  | 				switch (get_draw_mode()) { | 
					
						
							|  |  |  | 					case DRAW_PRESSED: | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 						clr = get_theme_color(SNAME("font_pressed_color")); | 
					
						
							| 
									
										
										
										
											2019-07-29 16:21:54 +02:00
										 |  |  | 						break; | 
					
						
							|  |  |  | 					case DRAW_HOVER: | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 						clr = get_theme_color(SNAME("font_hover_color")); | 
					
						
							| 
									
										
										
										
											2019-07-29 16:21:54 +02:00
										 |  |  | 						break; | 
					
						
							|  |  |  | 					case DRAW_DISABLED: | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 						clr = get_theme_color(SNAME("font_disabled_color")); | 
					
						
							| 
									
										
										
										
											2019-07-29 16:21:54 +02:00
										 |  |  | 						break; | 
					
						
							|  |  |  | 					default: | 
					
						
							| 
									
										
										
										
											2021-10-26 17:28:12 +03:00
										 |  |  | 						if (has_focus()) { | 
					
						
							|  |  |  | 							clr = get_theme_color(SNAME("font_focus_color")); | 
					
						
							|  |  |  | 						} else { | 
					
						
							|  |  |  | 							clr = get_theme_color(SNAME("font_color")); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2019-07-29 16:21:54 +02:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-12-25 13:24:10 -02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-29 16:21:54 +02:00
										 |  |  | 			Size2 size = get_size(); | 
					
						
							| 
									
										
										
										
											2017-12-25 13:24:10 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-03 14:22:16 +03:00
										 |  |  | 			Point2 ofs; | 
					
						
							|  |  |  | 			if (is_layout_rtl()) { | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 				ofs = Point2(get_theme_constant(SNAME("arrow_margin")), int(Math::abs((size.height - arrow->get_height()) / 2))); | 
					
						
							| 
									
										
										
										
											2020-09-03 14:22:16 +03:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 				ofs = Point2(size.width - arrow->get_width() - get_theme_constant(SNAME("arrow_margin")), int(Math::abs((size.height - arrow->get_height()) / 2))); | 
					
						
							| 
									
										
										
										
											2020-09-03 14:22:16 +03:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-07-29 16:21:54 +02:00
										 |  |  | 			arrow->draw(ci, ofs, clr); | 
					
						
							|  |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2022-02-15 18:06:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-03 14:22:16 +03:00
										 |  |  | 		case NOTIFICATION_TRANSLATION_CHANGED: | 
					
						
							| 
									
										
										
										
											2022-02-06 23:07:08 -03:00
										 |  |  | 		case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: { | 
					
						
							|  |  |  | 			popup->set_layout_direction((Window::LayoutDirection)get_layout_direction()); | 
					
						
							|  |  |  | 			[[fallthrough]]; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-12-28 12:03:04 -03:00
										 |  |  | 		case NOTIFICATION_THEME_CHANGED: { | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 			if (has_theme_icon(SNAME("arrow"))) { | 
					
						
							| 
									
										
										
										
											2020-09-03 14:22:16 +03:00
										 |  |  | 				if (is_layout_rtl()) { | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 					_set_internal_margin(SIDE_LEFT, Control::get_theme_icon(SNAME("arrow"))->get_width()); | 
					
						
							| 
									
										
										
										
											2020-12-22 16:24:29 +00:00
										 |  |  | 					_set_internal_margin(SIDE_RIGHT, 0.f); | 
					
						
							| 
									
										
										
										
											2020-09-03 14:22:16 +03:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2020-12-22 16:24:29 +00:00
										 |  |  | 					_set_internal_margin(SIDE_LEFT, 0.f); | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 					_set_internal_margin(SIDE_RIGHT, Control::get_theme_icon(SNAME("arrow"))->get_width()); | 
					
						
							| 
									
										
										
										
											2020-09-03 14:22:16 +03:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-12-28 12:03:04 -03:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2022-02-15 18:06:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-29 16:21:54 +02:00
										 |  |  | 		case NOTIFICATION_VISIBILITY_CHANGED: { | 
					
						
							|  |  |  | 			if (!is_visible_in_tree()) { | 
					
						
							|  |  |  | 				popup->hide(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-11 16:13:16 +01:00
										 |  |  | bool OptionButton::_set(const StringName &p_name, const Variant &p_value) { | 
					
						
							|  |  |  | 	Vector<String> components = String(p_name).split("/", true, 2); | 
					
						
							|  |  |  | 	if (components.size() >= 2 && components[0] == "popup") { | 
					
						
							| 
									
										
										
										
											2022-02-05 23:47:00 -03:00
										 |  |  | 		String property = components[2]; | 
					
						
							|  |  |  | 		if (property != "text" && property != "icon" && property != "id" && property != "disabled" && property != "separator") { | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-11 16:13:16 +01:00
										 |  |  | 		bool valid; | 
					
						
							|  |  |  | 		popup->set(String(p_name).trim_prefix("popup/"), p_value, &valid); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		int idx = components[1].get_slice("_", 1).to_int(); | 
					
						
							|  |  |  | 		if (idx == current) { | 
					
						
							|  |  |  | 			// Force refreshing currently displayed item.
 | 
					
						
							| 
									
										
										
										
											2022-01-28 17:03:45 -05:00
										 |  |  | 			current = NONE_SELECTED; | 
					
						
							| 
									
										
										
										
											2021-12-11 16:13:16 +01:00
										 |  |  | 			_select(idx, false); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return valid; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool OptionButton::_get(const StringName &p_name, Variant &r_ret) const { | 
					
						
							|  |  |  | 	Vector<String> components = String(p_name).split("/", true, 2); | 
					
						
							|  |  |  | 	if (components.size() >= 2 && components[0] == "popup") { | 
					
						
							| 
									
										
										
										
											2022-02-05 23:47:00 -03:00
										 |  |  | 		String property = components[2]; | 
					
						
							|  |  |  | 		if (property != "text" && property != "icon" && property != "id" && property != "disabled" && property != "separator") { | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-11 16:13:16 +01:00
										 |  |  | 		bool valid; | 
					
						
							|  |  |  | 		r_ret = popup->get(String(p_name).trim_prefix("popup/"), &valid); | 
					
						
							|  |  |  | 		return valid; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void OptionButton::_get_property_list(List<PropertyInfo> *p_list) const { | 
					
						
							|  |  |  | 	for (int i = 0; i < popup->get_item_count(); i++) { | 
					
						
							|  |  |  | 		p_list->push_back(PropertyInfo(Variant::STRING, vformat("popup/item_%d/text", i))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		PropertyInfo pi = PropertyInfo(Variant::OBJECT, vformat("popup/item_%d/icon", i), PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"); | 
					
						
							|  |  |  | 		pi.usage &= ~(popup->get_item_icon(i).is_null() ? PROPERTY_USAGE_STORAGE : 0); | 
					
						
							|  |  |  | 		p_list->push_back(pi); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-28 17:03:45 -05:00
										 |  |  | 		pi = PropertyInfo(Variant::INT, vformat("popup/item_%d/id", i), PROPERTY_HINT_RANGE, "0,10,1,or_greater"); | 
					
						
							| 
									
										
										
										
											2021-12-11 16:13:16 +01:00
										 |  |  | 		p_list->push_back(pi); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		pi = PropertyInfo(Variant::BOOL, vformat("popup/item_%d/disabled", i)); | 
					
						
							|  |  |  | 		pi.usage &= ~(!popup->is_item_disabled(i) ? PROPERTY_USAGE_STORAGE : 0); | 
					
						
							|  |  |  | 		p_list->push_back(pi); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		pi = PropertyInfo(Variant::BOOL, vformat("popup/item_%d/separator", i)); | 
					
						
							|  |  |  | 		pi.usage &= ~(!popup->is_item_separator(i) ? PROPERTY_USAGE_STORAGE : 0); | 
					
						
							|  |  |  | 		p_list->push_back(pi); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-12 02:53:35 +01:00
										 |  |  | void OptionButton::_focused(int p_which) { | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 	emit_signal(SNAME("item_focused"), p_which); | 
					
						
							| 
									
										
										
										
											2018-01-12 02:53:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | void OptionButton::_selected(int p_which) { | 
					
						
							| 
									
										
										
										
											2019-06-09 14:44:36 -05:00
										 |  |  | 	_select(p_which, true); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void OptionButton::pressed() { | 
					
						
							| 
									
										
										
										
											2021-08-30 08:42:33 -07:00
										 |  |  | 	Size2 size = get_size() * get_viewport()->get_canvas_transform().get_scale(); | 
					
						
							| 
									
										
										
										
											2020-03-12 09:37:40 -03:00
										 |  |  | 	popup->set_position(get_screen_position() + Size2(0, size.height * get_global_transform().get_scale().y)); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	popup->set_size(Size2(size.width, 0)); | 
					
						
							| 
									
										
										
										
											2022-01-20 08:44:27 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// If not triggered by the mouse, start the popup with the checked item selected.
 | 
					
						
							| 
									
										
										
										
											2022-02-05 23:47:00 -03:00
										 |  |  | 	if (popup->get_item_count() > 0) { | 
					
						
							|  |  |  | 		if ((get_action_mode() == ActionMode::ACTION_MODE_BUTTON_PRESS && Input::get_singleton()->is_action_just_pressed("ui_accept")) || | 
					
						
							|  |  |  | 				(get_action_mode() == ActionMode::ACTION_MODE_BUTTON_RELEASE && Input::get_singleton()->is_action_just_released("ui_accept"))) { | 
					
						
							|  |  |  | 			popup->set_current_index(current > -1 ? current : 0); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			popup->scroll_to_item(current > -1 ? current : 0); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-01-20 08:44:27 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	popup->popup(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-11 15:43:37 -03:00
										 |  |  | void OptionButton::add_icon_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id) { | 
					
						
							| 
									
										
										
										
											2019-05-09 05:21:49 -04:00
										 |  |  | 	popup->add_icon_radio_check_item(p_icon, p_label, p_id); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (popup->get_item_count() == 1) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		select(0); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-09 05:21:49 -04:00
										 |  |  | void OptionButton::add_item(const String &p_label, int p_id) { | 
					
						
							|  |  |  | 	popup->add_radio_check_item(p_label, p_id); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (popup->get_item_count() == 1) { | 
					
						
							| 
									
										
										
										
											2016-03-09 00:00:52 +01:00
										 |  |  | 		select(0); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void OptionButton::set_item_text(int p_idx, const String &p_text) { | 
					
						
							|  |  |  | 	popup->set_item_text(p_idx, p_text); | 
					
						
							| 
									
										
										
										
											2019-09-05 22:11:03 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (current == p_idx) { | 
					
						
							| 
									
										
										
										
											2019-09-05 22:11:03 -07:00
										 |  |  | 		set_text(p_text); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-11 15:43:37 -03:00
										 |  |  | void OptionButton::set_item_icon(int p_idx, const Ref<Texture2D> &p_icon) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	popup->set_item_icon(p_idx, p_icon); | 
					
						
							| 
									
										
										
										
											2019-09-05 22:11:03 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (current == p_idx) { | 
					
						
							| 
									
										
										
										
											2019-09-05 22:11:03 -07:00
										 |  |  | 		set_icon(p_icon); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-09 05:21:49 -04:00
										 |  |  | void OptionButton::set_item_id(int p_idx, int p_id) { | 
					
						
							|  |  |  | 	popup->set_item_id(p_idx, p_id); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void OptionButton::set_item_metadata(int p_idx, const Variant &p_metadata) { | 
					
						
							|  |  |  | 	popup->set_item_metadata(p_idx, p_metadata); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void OptionButton::set_item_disabled(int p_idx, bool p_disabled) { | 
					
						
							|  |  |  | 	popup->set_item_disabled(p_idx, p_disabled); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | String OptionButton::get_item_text(int p_idx) const { | 
					
						
							|  |  |  | 	return popup->get_item_text(p_idx); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-11 15:43:37 -03:00
										 |  |  | Ref<Texture2D> OptionButton::get_item_icon(int p_idx) const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	return popup->get_item_icon(p_idx); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-07 17:17:31 +07:00
										 |  |  | int OptionButton::get_item_id(int p_idx) const { | 
					
						
							| 
									
										
										
										
											2022-01-28 17:03:45 -05:00
										 |  |  | 	if (p_idx == NONE_SELECTED) { | 
					
						
							|  |  |  | 		return NONE_SELECTED; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-07 17:17:31 +07:00
										 |  |  | 	return popup->get_item_id(p_idx); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-01-28 12:31:24 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | int OptionButton::get_item_index(int p_id) const { | 
					
						
							|  |  |  | 	return popup->get_item_index(p_id); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | Variant OptionButton::get_item_metadata(int p_idx) const { | 
					
						
							|  |  |  | 	return popup->get_item_metadata(p_idx); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool OptionButton::is_item_disabled(int p_idx) const { | 
					
						
							|  |  |  | 	return popup->is_item_disabled(p_idx); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-11 16:13:16 +01:00
										 |  |  | void OptionButton::set_item_count(int p_count) { | 
					
						
							|  |  |  | 	ERR_FAIL_COND(p_count < 0); | 
					
						
							| 
									
										
										
										
											2022-02-05 23:47:00 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	int count_old = get_item_count(); | 
					
						
							|  |  |  | 	if (p_count == count_old) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-11 16:13:16 +01:00
										 |  |  | 	popup->set_item_count(p_count); | 
					
						
							| 
									
										
										
										
											2022-02-05 23:47:00 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (p_count > count_old) { | 
					
						
							|  |  |  | 		for (int i = count_old; i < p_count; i++) { | 
					
						
							|  |  |  | 			popup->set_item_as_radio_checkable(i, true); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-11 16:13:16 +01:00
										 |  |  | 	notify_property_list_changed(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | int OptionButton::get_item_count() const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	return popup->get_item_count(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void OptionButton::add_separator() { | 
					
						
							|  |  |  | 	popup->add_separator(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void OptionButton::clear() { | 
					
						
							|  |  |  | 	popup->clear(); | 
					
						
							|  |  |  | 	set_text(""); | 
					
						
							| 
									
										
										
										
											2022-01-28 17:03:45 -05:00
										 |  |  | 	current = NONE_SELECTED; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-11 15:10:05 -04:00
										 |  |  | void OptionButton::_select(int p_which, bool p_emit) { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (p_which == current) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-28 17:03:45 -05:00
										 |  |  | 	if (p_which == NONE_SELECTED) { | 
					
						
							|  |  |  | 		for (int i = 0; i < popup->get_item_count(); i++) { | 
					
						
							|  |  |  | 			popup->set_item_checked(i, false); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-28 17:03:45 -05:00
										 |  |  | 		current = NONE_SELECTED; | 
					
						
							|  |  |  | 		set_text(""); | 
					
						
							| 
									
										
										
										
											2022-02-05 23:47:00 -03:00
										 |  |  | 		set_icon(nullptr); | 
					
						
							| 
									
										
										
										
											2022-01-28 17:03:45 -05:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		ERR_FAIL_INDEX(p_which, popup->get_item_count()); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-28 17:03:45 -05:00
										 |  |  | 		for (int i = 0; i < popup->get_item_count(); i++) { | 
					
						
							|  |  |  | 			popup->set_item_checked(i, i == p_which); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		current = p_which; | 
					
						
							|  |  |  | 		set_text(popup->get_item_text(current)); | 
					
						
							|  |  |  | 		set_icon(popup->get_item_icon(current)); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (is_inside_tree() && p_emit) { | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 		emit_signal(SNAME("item_selected"), current); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void OptionButton::_select_int(int p_which) { | 
					
						
							| 
									
										
										
										
											2022-01-28 17:03:45 -05:00
										 |  |  | 	if (p_which < NONE_SELECTED || p_which >= popup->get_item_count()) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	_select(p_which, false); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void OptionButton::select(int p_idx) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	_select(p_idx, false); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int OptionButton::get_selected() const { | 
					
						
							|  |  |  | 	return current; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-07 17:17:31 +07:00
										 |  |  | int OptionButton::get_selected_id() const { | 
					
						
							|  |  |  | 	return get_item_id(current); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | Variant OptionButton::get_selected_metadata() const { | 
					
						
							|  |  |  | 	int idx = get_selected(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (idx < 0) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		return Variant(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	return get_item_metadata(current); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void OptionButton::remove_item(int p_idx) { | 
					
						
							|  |  |  | 	popup->remove_item(p_idx); | 
					
						
							| 
									
										
										
										
											2022-01-28 17:03:45 -05:00
										 |  |  | 	if (current == p_idx) { | 
					
						
							|  |  |  | 		_select(NONE_SELECTED); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-25 13:24:10 -02:00
										 |  |  | PopupMenu *OptionButton::get_popup() const { | 
					
						
							|  |  |  | 	return popup; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | void OptionButton::get_translatable_strings(List<String> *p_strings) const { | 
					
						
							| 
									
										
										
										
											2019-07-01 12:59:42 +02:00
										 |  |  | 	popup->get_translatable_strings(p_strings); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void OptionButton::_bind_methods() { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("add_item", "label", "id"), &OptionButton::add_item, DEFVAL(-1)); | 
					
						
							| 
									
										
										
										
											2018-05-08 13:35:28 +08:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("add_icon_item", "texture", "label", "id"), &OptionButton::add_icon_item, DEFVAL(-1)); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_item_text", "idx", "text"), &OptionButton::set_item_text); | 
					
						
							| 
									
										
										
										
											2017-08-09 13:19:41 +02:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_item_icon", "idx", "texture"), &OptionButton::set_item_icon); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_item_disabled", "idx", "disabled"), &OptionButton::set_item_disabled); | 
					
						
							| 
									
										
										
										
											2017-08-07 17:17:31 +07:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_item_id", "idx", "id"), &OptionButton::set_item_id); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_item_metadata", "idx", "metadata"), &OptionButton::set_item_metadata); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_item_text", "idx"), &OptionButton::get_item_text); | 
					
						
							| 
									
										
										
										
											2017-08-09 13:19:41 +02:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("get_item_icon", "idx"), &OptionButton::get_item_icon); | 
					
						
							| 
									
										
										
										
											2017-08-07 17:17:31 +07:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("get_item_id", "idx"), &OptionButton::get_item_id); | 
					
						
							| 
									
										
										
										
											2019-01-28 12:31:24 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("get_item_index", "id"), &OptionButton::get_item_index); | 
					
						
							| 
									
										
										
										
											2017-08-09 13:19:41 +02:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("get_item_metadata", "idx"), &OptionButton::get_item_metadata); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("is_item_disabled", "idx"), &OptionButton::is_item_disabled); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("add_separator"), &OptionButton::add_separator); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("clear"), &OptionButton::clear); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("select", "idx"), &OptionButton::select); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_selected"), &OptionButton::get_selected); | 
					
						
							| 
									
										
										
										
											2017-08-07 17:17:31 +07:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("get_selected_id"), &OptionButton::get_selected_id); | 
					
						
							| 
									
										
										
										
											2017-08-09 13:19:41 +02:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("get_selected_metadata"), &OptionButton::get_selected_metadata); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("remove_item", "idx"), &OptionButton::remove_item); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("_select_int"), &OptionButton::_select_int); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-25 13:24:10 -02:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("get_popup"), &OptionButton::get_popup); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-07 13:58:28 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_item_count", "count"), &OptionButton::set_item_count); | 
					
						
							| 
									
										
										
										
											2021-12-11 16:13:16 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("get_item_count"), &OptionButton::get_item_count); | 
					
						
							| 
									
										
										
										
											2022-01-07 13:58:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-11 16:13:16 +01:00
										 |  |  | 	// "selected" property must come after "item_count", otherwise GH-10213 occurs.
 | 
					
						
							|  |  |  | 	ADD_ARRAY_COUNT("Items", "item_count", "set_item_count", "get_item_count", "popup/item_"); | 
					
						
							| 
									
										
										
										
											2018-02-16 11:29:19 +03:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::INT, "selected"), "_select_int", "get_selected"); | 
					
						
							| 
									
										
										
										
											2020-03-25 12:07:18 -05:00
										 |  |  | 	ADD_SIGNAL(MethodInfo("item_selected", PropertyInfo(Variant::INT, "index"))); | 
					
						
							|  |  |  | 	ADD_SIGNAL(MethodInfo("item_focused", PropertyInfo(Variant::INT, "index"))); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | OptionButton::OptionButton() { | 
					
						
							| 
									
										
										
										
											2018-09-21 02:18:40 -03:00
										 |  |  | 	set_toggle_mode(true); | 
					
						
							| 
									
										
										
										
											2021-11-24 20:58:47 -06:00
										 |  |  | 	set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT); | 
					
						
							| 
									
										
										
										
											2020-09-03 14:22:16 +03:00
										 |  |  | 	if (is_layout_rtl()) { | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 		if (has_theme_icon(SNAME("arrow"))) { | 
					
						
							|  |  |  | 			_set_internal_margin(SIDE_LEFT, Control::get_theme_icon(SNAME("arrow"))->get_width()); | 
					
						
							| 
									
										
										
										
											2020-09-03 14:22:16 +03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 		if (has_theme_icon(SNAME("arrow"))) { | 
					
						
							|  |  |  | 			_set_internal_margin(SIDE_RIGHT, Control::get_theme_icon(SNAME("arrow"))->get_width()); | 
					
						
							| 
									
										
										
										
											2020-09-03 14:22:16 +03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-12-28 12:03:04 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-09-03 14:22:16 +03:00
										 |  |  | 	set_action_mode(ACTION_MODE_BUTTON_PRESS); | 
					
						
							| 
									
										
										
										
											2017-12-25 13:24:10 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	popup = memnew(PopupMenu); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	popup->hide(); | 
					
						
							| 
									
										
										
										
											2021-08-25 15:49:30 +02:00
										 |  |  | 	add_child(popup, false, INTERNAL_MODE_FRONT); | 
					
						
							| 
									
										
										
										
											2020-02-21 18:28:45 +01:00
										 |  |  | 	popup->connect("index_pressed", callable_mp(this, &OptionButton::_selected)); | 
					
						
							|  |  |  | 	popup->connect("id_focused", callable_mp(this, &OptionButton::_focused)); | 
					
						
							| 
									
										
										
										
											2020-02-21 23:26:13 +01:00
										 |  |  | 	popup->connect("popup_hide", callable_mp((BaseButton *)this, &BaseButton::set_pressed), varray(false)); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | OptionButton::~OptionButton() { | 
					
						
							|  |  |  | } |