option_button.cpp 10.0 KB

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