option_button.cpp 12 KB

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