option_button.cpp 11 KB

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