option_button.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. } else if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  65. if (!is_visible_in_tree()) {
  66. popup->hide();
  67. }
  68. }
  69. }
  70. void OptionButton::_focused(int p_which) {
  71. emit_signal("item_focused", p_which);
  72. }
  73. void OptionButton::_selected(int p_which) {
  74. int selid = -1;
  75. for (int i = 0; i < popup->get_item_count(); i++) {
  76. bool is_clicked = popup->get_item_id(i) == p_which;
  77. if (is_clicked) {
  78. selid = i;
  79. break;
  80. }
  81. }
  82. if (selid == -1 && p_which >= 0 && p_which < popup->get_item_count()) {
  83. _select(p_which, true);
  84. } else {
  85. ERR_FAIL_COND(selid == -1);
  86. _select(selid, true);
  87. }
  88. }
  89. void OptionButton::pressed() {
  90. Size2 size = get_size();
  91. popup->set_global_position(get_global_position() + Size2(0, size.height * get_global_transform().get_scale().y));
  92. popup->set_size(Size2(size.width, 0));
  93. popup->set_scale(get_global_transform().get_scale());
  94. popup->popup();
  95. }
  96. void OptionButton::add_icon_item(const Ref<Texture> &p_icon, const String &p_label, int p_id) {
  97. popup->add_icon_radio_check_item(p_icon, p_label, p_id);
  98. if (popup->get_item_count() == 1)
  99. select(0);
  100. }
  101. void OptionButton::add_item(const String &p_label, int p_id) {
  102. popup->add_radio_check_item(p_label, p_id);
  103. if (popup->get_item_count() == 1)
  104. select(0);
  105. }
  106. void OptionButton::set_item_text(int p_idx, const String &p_text) {
  107. popup->set_item_text(p_idx, p_text);
  108. }
  109. void OptionButton::set_item_icon(int p_idx, const Ref<Texture> &p_icon) {
  110. popup->set_item_icon(p_idx, p_icon);
  111. }
  112. void OptionButton::set_item_id(int p_idx, int p_id) {
  113. popup->set_item_id(p_idx, p_id);
  114. }
  115. void OptionButton::set_item_metadata(int p_idx, const Variant &p_metadata) {
  116. popup->set_item_metadata(p_idx, p_metadata);
  117. }
  118. void OptionButton::set_item_disabled(int p_idx, bool p_disabled) {
  119. popup->set_item_disabled(p_idx, p_disabled);
  120. }
  121. String OptionButton::get_item_text(int p_idx) const {
  122. return popup->get_item_text(p_idx);
  123. }
  124. Ref<Texture> OptionButton::get_item_icon(int p_idx) const {
  125. return popup->get_item_icon(p_idx);
  126. }
  127. int OptionButton::get_item_id(int p_idx) const {
  128. return popup->get_item_id(p_idx);
  129. }
  130. int OptionButton::get_item_index(int p_id) const {
  131. return popup->get_item_index(p_id);
  132. }
  133. Variant OptionButton::get_item_metadata(int p_idx) const {
  134. return popup->get_item_metadata(p_idx);
  135. }
  136. bool OptionButton::is_item_disabled(int p_idx) const {
  137. return popup->is_item_disabled(p_idx);
  138. }
  139. int OptionButton::get_item_count() const {
  140. return popup->get_item_count();
  141. }
  142. void OptionButton::add_separator() {
  143. popup->add_separator();
  144. }
  145. void OptionButton::clear() {
  146. popup->clear();
  147. set_text("");
  148. current = -1;
  149. }
  150. void OptionButton::_select(int p_which, bool p_emit) {
  151. if (p_which < 0)
  152. return;
  153. if (p_which == current)
  154. return;
  155. ERR_FAIL_INDEX(p_which, popup->get_item_count());
  156. for (int i = 0; i < popup->get_item_count(); i++) {
  157. popup->set_item_checked(i, i == p_which);
  158. }
  159. current = p_which;
  160. set_text(popup->get_item_text(current));
  161. set_icon(popup->get_item_icon(current));
  162. if (is_inside_tree() && p_emit)
  163. emit_signal("item_selected", current);
  164. }
  165. void OptionButton::_select_int(int p_which) {
  166. if (p_which < 0 || p_which >= popup->get_item_count())
  167. return;
  168. _select(p_which, false);
  169. }
  170. void OptionButton::select(int p_idx) {
  171. _select(p_idx, false);
  172. }
  173. int OptionButton::get_selected() const {
  174. return current;
  175. }
  176. int OptionButton::get_selected_id() const {
  177. int idx = get_selected();
  178. if (idx < 0)
  179. return 0;
  180. return get_item_id(current);
  181. }
  182. Variant OptionButton::get_selected_metadata() const {
  183. int idx = get_selected();
  184. if (idx < 0)
  185. return Variant();
  186. return get_item_metadata(current);
  187. }
  188. void OptionButton::remove_item(int p_idx) {
  189. popup->remove_item(p_idx);
  190. }
  191. PopupMenu *OptionButton::get_popup() const {
  192. return popup;
  193. }
  194. Array OptionButton::_get_items() const {
  195. Array items;
  196. for (int i = 0; i < get_item_count(); i++) {
  197. items.push_back(get_item_text(i));
  198. items.push_back(get_item_icon(i));
  199. items.push_back(is_item_disabled(i));
  200. items.push_back(get_item_id(i));
  201. items.push_back(get_item_metadata(i));
  202. }
  203. return items;
  204. }
  205. void OptionButton::_set_items(const Array &p_items) {
  206. ERR_FAIL_COND(p_items.size() % 5);
  207. clear();
  208. for (int i = 0; i < p_items.size(); i += 5) {
  209. String text = p_items[i + 0];
  210. Ref<Texture> icon = p_items[i + 1];
  211. bool disabled = p_items[i + 2];
  212. int id = p_items[i + 3];
  213. Variant meta = p_items[i + 4];
  214. int idx = get_item_count();
  215. add_item(text, id);
  216. set_item_icon(idx, icon);
  217. set_item_disabled(idx, disabled);
  218. set_item_metadata(idx, meta);
  219. }
  220. }
  221. void OptionButton::get_translatable_strings(List<String> *p_strings) const {
  222. return popup->get_translatable_strings(p_strings);
  223. }
  224. void OptionButton::_bind_methods() {
  225. ClassDB::bind_method(D_METHOD("_selected"), &OptionButton::_selected);
  226. ClassDB::bind_method(D_METHOD("_focused"), &OptionButton::_focused);
  227. ClassDB::bind_method(D_METHOD("add_item", "label", "id"), &OptionButton::add_item, DEFVAL(-1));
  228. ClassDB::bind_method(D_METHOD("add_icon_item", "texture", "label", "id"), &OptionButton::add_icon_item, DEFVAL(-1));
  229. ClassDB::bind_method(D_METHOD("set_item_text", "idx", "text"), &OptionButton::set_item_text);
  230. ClassDB::bind_method(D_METHOD("set_item_icon", "idx", "texture"), &OptionButton::set_item_icon);
  231. ClassDB::bind_method(D_METHOD("set_item_disabled", "idx", "disabled"), &OptionButton::set_item_disabled);
  232. ClassDB::bind_method(D_METHOD("set_item_id", "idx", "id"), &OptionButton::set_item_id);
  233. ClassDB::bind_method(D_METHOD("set_item_metadata", "idx", "metadata"), &OptionButton::set_item_metadata);
  234. ClassDB::bind_method(D_METHOD("get_item_text", "idx"), &OptionButton::get_item_text);
  235. ClassDB::bind_method(D_METHOD("get_item_icon", "idx"), &OptionButton::get_item_icon);
  236. ClassDB::bind_method(D_METHOD("get_item_id", "idx"), &OptionButton::get_item_id);
  237. ClassDB::bind_method(D_METHOD("get_item_index", "id"), &OptionButton::get_item_index);
  238. ClassDB::bind_method(D_METHOD("get_item_metadata", "idx"), &OptionButton::get_item_metadata);
  239. ClassDB::bind_method(D_METHOD("is_item_disabled", "idx"), &OptionButton::is_item_disabled);
  240. ClassDB::bind_method(D_METHOD("get_item_count"), &OptionButton::get_item_count);
  241. ClassDB::bind_method(D_METHOD("add_separator"), &OptionButton::add_separator);
  242. ClassDB::bind_method(D_METHOD("clear"), &OptionButton::clear);
  243. ClassDB::bind_method(D_METHOD("select", "idx"), &OptionButton::select);
  244. ClassDB::bind_method(D_METHOD("get_selected"), &OptionButton::get_selected);
  245. ClassDB::bind_method(D_METHOD("get_selected_id"), &OptionButton::get_selected_id);
  246. ClassDB::bind_method(D_METHOD("get_selected_metadata"), &OptionButton::get_selected_metadata);
  247. ClassDB::bind_method(D_METHOD("remove_item", "idx"), &OptionButton::remove_item);
  248. ClassDB::bind_method(D_METHOD("_select_int"), &OptionButton::_select_int);
  249. ClassDB::bind_method(D_METHOD("get_popup"), &OptionButton::get_popup);
  250. ClassDB::bind_method(D_METHOD("_set_items"), &OptionButton::_set_items);
  251. ClassDB::bind_method(D_METHOD("_get_items"), &OptionButton::_get_items);
  252. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "items", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_items", "_get_items");
  253. // "selected" property must come after "items", otherwise GH-10213 occurs
  254. ADD_PROPERTY(PropertyInfo(Variant::INT, "selected"), "_select_int", "get_selected");
  255. ADD_SIGNAL(MethodInfo("item_selected", PropertyInfo(Variant::INT, "id")));
  256. ADD_SIGNAL(MethodInfo("item_focused", PropertyInfo(Variant::INT, "id")));
  257. }
  258. OptionButton::OptionButton() {
  259. current = -1;
  260. set_toggle_mode(true);
  261. set_text_align(ALIGN_LEFT);
  262. set_action_mode(ACTION_MODE_BUTTON_PRESS);
  263. popup = memnew(PopupMenu);
  264. popup->hide();
  265. add_child(popup);
  266. popup->set_pass_on_modal_close_click(false);
  267. popup->set_notify_transform(true);
  268. popup->connect("id_pressed", this, "_selected");
  269. popup->connect("id_focused", this, "_focused");
  270. popup->connect("popup_hide", this, "set_pressed", varray(false));
  271. }
  272. OptionButton::~OptionButton() {
  273. }