option_button.cpp 13 KB

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