option_button.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*************************************************************************/
  2. /* option_button.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. #include "option_button.h"
  31. #include "core/print_string.h"
  32. Size2 OptionButton::get_minimum_size() const {
  33. Size2 minsize = Button::get_minimum_size();
  34. if (has_icon("arrow")) {
  35. const Size2 padding = get_stylebox("normal")->get_minimum_size();
  36. const Size2 arrow_size = Control::get_icon("arrow")->get_size();
  37. Size2 content_size = minsize - padding;
  38. content_size.width += arrow_size.width + get_constant("hseparation");
  39. content_size.height = MAX(content_size.height, arrow_size.height);
  40. minsize = content_size + padding;
  41. }
  42. return minsize;
  43. }
  44. void OptionButton::_notification(int p_what) {
  45. switch (p_what) {
  46. case NOTIFICATION_DRAW: {
  47. if (!has_icon("arrow")) {
  48. return;
  49. }
  50. RID ci = get_canvas_item();
  51. Ref<Texture> arrow = Control::get_icon("arrow");
  52. Color clr = Color(1, 1, 1);
  53. if (get_constant("modulate_arrow")) {
  54. switch (get_draw_mode()) {
  55. case DRAW_PRESSED:
  56. clr = get_color("font_color_pressed");
  57. break;
  58. case DRAW_HOVER:
  59. clr = get_color("font_color_hover");
  60. break;
  61. case DRAW_DISABLED:
  62. clr = get_color("font_color_disabled");
  63. break;
  64. default:
  65. if (has_focus()) {
  66. clr = get_color("font_color_focus");
  67. } else {
  68. clr = get_color("font_color");
  69. }
  70. }
  71. }
  72. Size2 size = get_size();
  73. Point2 ofs(size.width - arrow->get_width() - get_constant("arrow_margin"), int(Math::abs((size.height - arrow->get_height()) / 2)));
  74. arrow->draw(ci, ofs, clr);
  75. } break;
  76. case NOTIFICATION_THEME_CHANGED: {
  77. if (has_icon("arrow")) {
  78. _set_internal_margin(MARGIN_RIGHT, Control::get_icon("arrow")->get_width());
  79. }
  80. } break;
  81. case NOTIFICATION_VISIBILITY_CHANGED: {
  82. if (!is_visible_in_tree()) {
  83. popup->hide();
  84. }
  85. } break;
  86. }
  87. }
  88. void OptionButton::_focused(int p_which) {
  89. emit_signal("item_focused", p_which);
  90. }
  91. void OptionButton::_selected(int p_which) {
  92. _select(p_which, true);
  93. }
  94. void OptionButton::pressed() {
  95. Size2 size = get_size();
  96. popup->set_global_position(get_global_position() + Size2(0, size.height * get_global_transform().get_scale().y));
  97. popup->set_size(Size2(size.width, 0));
  98. popup->set_scale(get_global_transform().get_scale());
  99. popup->popup();
  100. }
  101. void OptionButton::add_icon_item(const Ref<Texture> &p_icon, const String &p_label, int p_id) {
  102. popup->add_icon_radio_check_item(p_icon, p_label, p_id);
  103. if (popup->get_item_count() == 1) {
  104. select(0);
  105. }
  106. }
  107. void OptionButton::add_item(const String &p_label, int p_id) {
  108. popup->add_radio_check_item(p_label, p_id);
  109. if (popup->get_item_count() == 1) {
  110. select(0);
  111. }
  112. }
  113. void OptionButton::set_item_text(int p_idx, const String &p_text) {
  114. popup->set_item_text(p_idx, p_text);
  115. if (current == p_idx) {
  116. set_text(p_text);
  117. }
  118. }
  119. void OptionButton::set_item_icon(int p_idx, const Ref<Texture> &p_icon) {
  120. popup->set_item_icon(p_idx, p_icon);
  121. if (current == p_idx) {
  122. set_icon(p_icon);
  123. }
  124. }
  125. void OptionButton::set_item_id(int p_idx, int p_id) {
  126. popup->set_item_id(p_idx, p_id);
  127. }
  128. void OptionButton::set_item_metadata(int p_idx, const Variant &p_metadata) {
  129. popup->set_item_metadata(p_idx, p_metadata);
  130. }
  131. void OptionButton::set_item_disabled(int p_idx, bool p_disabled) {
  132. popup->set_item_disabled(p_idx, p_disabled);
  133. }
  134. String OptionButton::get_item_text(int p_idx) const {
  135. return popup->get_item_text(p_idx);
  136. }
  137. Ref<Texture> OptionButton::get_item_icon(int p_idx) const {
  138. return popup->get_item_icon(p_idx);
  139. }
  140. int OptionButton::get_item_id(int p_idx) const {
  141. return popup->get_item_id(p_idx);
  142. }
  143. int OptionButton::get_item_index(int p_id) const {
  144. return popup->get_item_index(p_id);
  145. }
  146. Variant OptionButton::get_item_metadata(int p_idx) const {
  147. return popup->get_item_metadata(p_idx);
  148. }
  149. bool OptionButton::is_item_disabled(int p_idx) const {
  150. return popup->is_item_disabled(p_idx);
  151. }
  152. int OptionButton::get_item_count() const {
  153. return popup->get_item_count();
  154. }
  155. void OptionButton::add_separator() {
  156. popup->add_separator();
  157. }
  158. void OptionButton::clear() {
  159. popup->clear();
  160. set_text("");
  161. current = -1;
  162. }
  163. void OptionButton::_select(int p_which, bool p_emit) {
  164. if (p_which < 0) {
  165. return;
  166. }
  167. if (p_which == current) {
  168. return;
  169. }
  170. ERR_FAIL_INDEX(p_which, popup->get_item_count());
  171. for (int i = 0; i < popup->get_item_count(); i++) {
  172. popup->set_item_checked(i, i == p_which);
  173. }
  174. current = p_which;
  175. set_text(popup->get_item_text(current));
  176. set_icon(popup->get_item_icon(current));
  177. if (is_inside_tree() && p_emit) {
  178. emit_signal("item_selected", current);
  179. }
  180. }
  181. void OptionButton::_select_int(int p_which) {
  182. if (p_which < 0 || p_which >= popup->get_item_count()) {
  183. return;
  184. }
  185. _select(p_which, false);
  186. }
  187. void OptionButton::select(int p_idx) {
  188. _select(p_idx, false);
  189. }
  190. int OptionButton::get_selected() const {
  191. return current;
  192. }
  193. int OptionButton::get_selected_id() const {
  194. int idx = get_selected();
  195. if (idx < 0) {
  196. return 0;
  197. }
  198. return get_item_id(current);
  199. }
  200. Variant OptionButton::get_selected_metadata() const {
  201. int idx = get_selected();
  202. if (idx < 0) {
  203. return Variant();
  204. }
  205. return get_item_metadata(current);
  206. }
  207. void OptionButton::remove_item(int p_idx) {
  208. popup->remove_item(p_idx);
  209. }
  210. PopupMenu *OptionButton::get_popup() const {
  211. return popup;
  212. }
  213. Array OptionButton::_get_items() const {
  214. Array items;
  215. for (int i = 0; i < get_item_count(); i++) {
  216. items.push_back(get_item_text(i));
  217. items.push_back(get_item_icon(i));
  218. items.push_back(is_item_disabled(i));
  219. items.push_back(get_item_id(i));
  220. items.push_back(get_item_metadata(i));
  221. }
  222. return items;
  223. }
  224. void OptionButton::_set_items(const Array &p_items) {
  225. ERR_FAIL_COND(p_items.size() % 5);
  226. clear();
  227. for (int i = 0; i < p_items.size(); i += 5) {
  228. String text = p_items[i + 0];
  229. Ref<Texture> icon = p_items[i + 1];
  230. bool disabled = p_items[i + 2];
  231. int id = p_items[i + 3];
  232. Variant meta = p_items[i + 4];
  233. int idx = get_item_count();
  234. add_item(text, id);
  235. set_item_icon(idx, icon);
  236. set_item_disabled(idx, disabled);
  237. set_item_metadata(idx, meta);
  238. }
  239. }
  240. void OptionButton::get_translatable_strings(List<String> *p_strings) const {
  241. popup->get_translatable_strings(p_strings);
  242. }
  243. void OptionButton::_bind_methods() {
  244. ClassDB::bind_method(D_METHOD("_selected"), &OptionButton::_selected);
  245. ClassDB::bind_method(D_METHOD("_focused"), &OptionButton::_focused);
  246. ClassDB::bind_method(D_METHOD("add_item", "label", "id"), &OptionButton::add_item, DEFVAL(-1));
  247. ClassDB::bind_method(D_METHOD("add_icon_item", "texture", "label", "id"), &OptionButton::add_icon_item, DEFVAL(-1));
  248. ClassDB::bind_method(D_METHOD("set_item_text", "idx", "text"), &OptionButton::set_item_text);
  249. ClassDB::bind_method(D_METHOD("set_item_icon", "idx", "texture"), &OptionButton::set_item_icon);
  250. ClassDB::bind_method(D_METHOD("set_item_disabled", "idx", "disabled"), &OptionButton::set_item_disabled);
  251. ClassDB::bind_method(D_METHOD("set_item_id", "idx", "id"), &OptionButton::set_item_id);
  252. ClassDB::bind_method(D_METHOD("set_item_metadata", "idx", "metadata"), &OptionButton::set_item_metadata);
  253. ClassDB::bind_method(D_METHOD("get_item_text", "idx"), &OptionButton::get_item_text);
  254. ClassDB::bind_method(D_METHOD("get_item_icon", "idx"), &OptionButton::get_item_icon);
  255. ClassDB::bind_method(D_METHOD("get_item_id", "idx"), &OptionButton::get_item_id);
  256. ClassDB::bind_method(D_METHOD("get_item_index", "id"), &OptionButton::get_item_index);
  257. ClassDB::bind_method(D_METHOD("get_item_metadata", "idx"), &OptionButton::get_item_metadata);
  258. ClassDB::bind_method(D_METHOD("is_item_disabled", "idx"), &OptionButton::is_item_disabled);
  259. ClassDB::bind_method(D_METHOD("get_item_count"), &OptionButton::get_item_count);
  260. ClassDB::bind_method(D_METHOD("add_separator"), &OptionButton::add_separator);
  261. ClassDB::bind_method(D_METHOD("clear"), &OptionButton::clear);
  262. ClassDB::bind_method(D_METHOD("select", "idx"), &OptionButton::select);
  263. ClassDB::bind_method(D_METHOD("get_selected"), &OptionButton::get_selected);
  264. ClassDB::bind_method(D_METHOD("get_selected_id"), &OptionButton::get_selected_id);
  265. ClassDB::bind_method(D_METHOD("get_selected_metadata"), &OptionButton::get_selected_metadata);
  266. ClassDB::bind_method(D_METHOD("remove_item", "idx"), &OptionButton::remove_item);
  267. ClassDB::bind_method(D_METHOD("_select_int"), &OptionButton::_select_int);
  268. ClassDB::bind_method(D_METHOD("get_popup"), &OptionButton::get_popup);
  269. ClassDB::bind_method(D_METHOD("_set_items"), &OptionButton::_set_items);
  270. ClassDB::bind_method(D_METHOD("_get_items"), &OptionButton::_get_items);
  271. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "items", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_items", "_get_items");
  272. // "selected" property must come after "items", otherwise GH-10213 occurs.
  273. ADD_PROPERTY(PropertyInfo(Variant::INT, "selected"), "_select_int", "get_selected");
  274. ADD_SIGNAL(MethodInfo("item_selected", PropertyInfo(Variant::INT, "index")));
  275. ADD_SIGNAL(MethodInfo("item_focused", PropertyInfo(Variant::INT, "index")));
  276. }
  277. OptionButton::OptionButton() {
  278. current = -1;
  279. set_toggle_mode(true);
  280. set_text_align(ALIGN_LEFT);
  281. set_action_mode(ACTION_MODE_BUTTON_PRESS);
  282. if (has_icon("arrow")) {
  283. _set_internal_margin(MARGIN_RIGHT, Control::get_icon("arrow")->get_width());
  284. }
  285. popup = memnew(PopupMenu);
  286. popup->hide();
  287. add_child(popup);
  288. popup->set_pass_on_modal_close_click(false);
  289. popup->set_notify_transform(true);
  290. popup->set_allow_search(true);
  291. popup->connect("index_pressed", this, "_selected");
  292. popup->connect("id_focused", this, "_focused");
  293. popup->connect("popup_hide", this, "set_pressed", varray(false));
  294. }
  295. OptionButton::~OptionButton() {
  296. }