option_button.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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 "scene/theme/theme_db.h"
  32. static const int NONE_SELECTED = -1;
  33. void OptionButton::shortcut_input(const Ref<InputEvent> &p_event) {
  34. ERR_FAIL_COND(p_event.is_null());
  35. if (disable_shortcuts) {
  36. return;
  37. }
  38. if (p_event->is_pressed() && !p_event->is_echo() && !is_disabled() && is_visible_in_tree() && popup->activate_item_by_event(p_event, false)) {
  39. accept_event();
  40. return;
  41. }
  42. Button::shortcut_input(p_event);
  43. }
  44. Size2 OptionButton::get_minimum_size() const {
  45. Size2 minsize;
  46. if (fit_to_longest_item) {
  47. minsize = _cached_size;
  48. } else {
  49. minsize = Button::get_minimum_size();
  50. }
  51. if (has_theme_icon(SNAME("arrow"))) {
  52. const Size2 padding = _get_largest_stylebox_size();
  53. const Size2 arrow_size = theme_cache.arrow_icon->get_size();
  54. Size2 content_size = minsize - padding;
  55. content_size.width += arrow_size.width + MAX(0, theme_cache.h_separation);
  56. content_size.height = MAX(content_size.height, arrow_size.height);
  57. minsize = content_size + padding;
  58. }
  59. return minsize;
  60. }
  61. void OptionButton::_notification(int p_what) {
  62. switch (p_what) {
  63. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  64. RID ae = get_accessibility_element();
  65. ERR_FAIL_COND(ae.is_null());
  66. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_BUTTON);
  67. DisplayServer::get_singleton()->accessibility_update_set_popup_type(ae, DisplayServer::AccessibilityPopupType::POPUP_LIST);
  68. } break;
  69. case NOTIFICATION_POSTINITIALIZE: {
  70. _refresh_size_cache();
  71. if (has_theme_icon(SNAME("arrow"))) {
  72. if (is_layout_rtl()) {
  73. _set_internal_margin(SIDE_LEFT, theme_cache.arrow_icon->get_width());
  74. } else {
  75. _set_internal_margin(SIDE_RIGHT, theme_cache.arrow_icon->get_width());
  76. }
  77. }
  78. } break;
  79. case NOTIFICATION_DRAW: {
  80. if (!has_theme_icon(SNAME("arrow"))) {
  81. return;
  82. }
  83. RID ci = get_canvas_item();
  84. Color clr = Color(1, 1, 1);
  85. if (theme_cache.modulate_arrow) {
  86. switch (get_draw_mode()) {
  87. case DRAW_PRESSED:
  88. clr = theme_cache.font_pressed_color;
  89. break;
  90. case DRAW_HOVER:
  91. clr = theme_cache.font_hover_color;
  92. break;
  93. case DRAW_HOVER_PRESSED:
  94. clr = theme_cache.font_hover_pressed_color;
  95. break;
  96. case DRAW_DISABLED:
  97. clr = theme_cache.font_disabled_color;
  98. break;
  99. default:
  100. if (has_focus()) {
  101. clr = theme_cache.font_focus_color;
  102. } else {
  103. clr = theme_cache.font_color;
  104. }
  105. }
  106. }
  107. Size2 size = get_size();
  108. Point2 ofs;
  109. if (is_layout_rtl()) {
  110. ofs = Point2(theme_cache.arrow_margin, int(Math::abs((size.height - theme_cache.arrow_icon->get_height()) / 2)));
  111. } else {
  112. ofs = Point2(size.width - theme_cache.arrow_icon->get_width() - theme_cache.arrow_margin, int(Math::abs((size.height - theme_cache.arrow_icon->get_height()) / 2)));
  113. }
  114. theme_cache.arrow_icon->draw(ci, ofs, clr);
  115. } break;
  116. case NOTIFICATION_TRANSLATION_CHANGED:
  117. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  118. popup->set_layout_direction((Window::LayoutDirection)get_layout_direction());
  119. [[fallthrough]];
  120. }
  121. case NOTIFICATION_THEME_CHANGED: {
  122. if (has_theme_icon(SNAME("arrow"))) {
  123. if (is_layout_rtl()) {
  124. _set_internal_margin(SIDE_LEFT, theme_cache.arrow_icon->get_width());
  125. _set_internal_margin(SIDE_RIGHT, 0.f);
  126. } else {
  127. _set_internal_margin(SIDE_LEFT, 0.f);
  128. _set_internal_margin(SIDE_RIGHT, theme_cache.arrow_icon->get_width());
  129. }
  130. }
  131. _refresh_size_cache();
  132. } break;
  133. case NOTIFICATION_VISIBILITY_CHANGED: {
  134. if (!is_visible_in_tree()) {
  135. popup->hide();
  136. }
  137. } break;
  138. }
  139. }
  140. bool OptionButton::_set(const StringName &p_name, const Variant &p_value) {
  141. int index;
  142. const String sname = p_name;
  143. if (property_helper.is_property_valid(sname, &index)) {
  144. bool valid;
  145. popup->set(sname.trim_prefix("popup/"), p_value, &valid);
  146. if (index == current) {
  147. // Force refreshing currently displayed item.
  148. current = NONE_SELECTED;
  149. _select(index, false);
  150. }
  151. const String property = sname.get_slicec('/', 2);
  152. if (property == "text" || property == "icon") {
  153. _queue_update_size_cache();
  154. }
  155. return valid;
  156. }
  157. return false;
  158. }
  159. void OptionButton::_focused(int p_which) {
  160. emit_signal(SNAME("item_focused"), popup->get_item_index(p_which));
  161. }
  162. void OptionButton::_selected(int p_which) {
  163. _select(p_which, true);
  164. }
  165. void OptionButton::pressed() {
  166. if (popup->is_visible()) {
  167. popup->hide();
  168. return;
  169. }
  170. show_popup();
  171. }
  172. void OptionButton::add_icon_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id) {
  173. bool first_selectable = !has_selectable_items();
  174. popup->add_icon_radio_check_item(p_icon, p_label, p_id);
  175. if (first_selectable) {
  176. select(get_item_count() - 1);
  177. }
  178. _queue_update_size_cache();
  179. }
  180. void OptionButton::add_item(const String &p_label, int p_id) {
  181. bool first_selectable = !has_selectable_items();
  182. popup->add_radio_check_item(p_label, p_id);
  183. if (first_selectable) {
  184. select(get_item_count() - 1);
  185. }
  186. _queue_update_size_cache();
  187. }
  188. void OptionButton::set_item_text(int p_idx, const String &p_text) {
  189. popup->set_item_text(p_idx, p_text);
  190. if (current == p_idx) {
  191. set_text(p_text);
  192. }
  193. _queue_update_size_cache();
  194. }
  195. void OptionButton::set_item_icon(int p_idx, const Ref<Texture2D> &p_icon) {
  196. popup->set_item_icon(p_idx, p_icon);
  197. if (current == p_idx) {
  198. set_button_icon(p_icon);
  199. }
  200. _queue_update_size_cache();
  201. }
  202. void OptionButton::set_item_id(int p_idx, int p_id) {
  203. popup->set_item_id(p_idx, p_id);
  204. }
  205. void OptionButton::set_item_metadata(int p_idx, const Variant &p_metadata) {
  206. popup->set_item_metadata(p_idx, p_metadata);
  207. }
  208. void OptionButton::set_item_tooltip(int p_idx, const String &p_tooltip) {
  209. popup->set_item_tooltip(p_idx, p_tooltip);
  210. }
  211. void OptionButton::set_item_auto_translate_mode(int p_idx, AutoTranslateMode p_mode) {
  212. if (p_idx < 0) {
  213. p_idx += get_item_count();
  214. }
  215. if (popup->get_item_auto_translate_mode(p_idx) == p_mode) {
  216. return;
  217. }
  218. popup->set_item_auto_translate_mode(p_idx, p_mode);
  219. if (current == p_idx) {
  220. set_text(popup->get_item_text(p_idx));
  221. }
  222. _queue_update_size_cache();
  223. }
  224. void OptionButton::set_item_disabled(int p_idx, bool p_disabled) {
  225. popup->set_item_disabled(p_idx, p_disabled);
  226. }
  227. String OptionButton::get_item_text(int p_idx) const {
  228. return popup->get_item_text(p_idx);
  229. }
  230. Ref<Texture2D> OptionButton::get_item_icon(int p_idx) const {
  231. return popup->get_item_icon(p_idx);
  232. }
  233. int OptionButton::get_item_id(int p_idx) const {
  234. if (p_idx == NONE_SELECTED) {
  235. return NONE_SELECTED;
  236. }
  237. return popup->get_item_id(p_idx);
  238. }
  239. int OptionButton::get_item_index(int p_id) const {
  240. return popup->get_item_index(p_id);
  241. }
  242. Variant OptionButton::get_item_metadata(int p_idx) const {
  243. return popup->get_item_metadata(p_idx);
  244. }
  245. String OptionButton::get_item_tooltip(int p_idx) const {
  246. return popup->get_item_tooltip(p_idx);
  247. }
  248. Node::AutoTranslateMode OptionButton::get_item_auto_translate_mode(int p_idx) const {
  249. return popup->get_item_auto_translate_mode(p_idx);
  250. }
  251. bool OptionButton::is_item_disabled(int p_idx) const {
  252. return popup->is_item_disabled(p_idx);
  253. }
  254. bool OptionButton::is_item_separator(int p_idx) const {
  255. return popup->is_item_separator(p_idx);
  256. }
  257. void OptionButton::set_item_count(int p_count) {
  258. ERR_FAIL_COND(p_count < 0);
  259. int count_old = get_item_count();
  260. if (p_count == count_old) {
  261. return;
  262. }
  263. popup->set_item_count(p_count);
  264. if (p_count > count_old) {
  265. for (int i = count_old; i < p_count; i++) {
  266. popup->set_item_as_radio_checkable(i, true);
  267. }
  268. }
  269. if (!initialized) {
  270. if (queued_current != current) {
  271. current = queued_current;
  272. }
  273. initialized = true;
  274. }
  275. _refresh_size_cache();
  276. notify_property_list_changed();
  277. }
  278. bool OptionButton::has_selectable_items() const {
  279. for (int i = 0; i < get_item_count(); i++) {
  280. if (!is_item_disabled(i) && !is_item_separator(i)) {
  281. return true;
  282. }
  283. }
  284. return false;
  285. }
  286. int OptionButton::get_selectable_item(bool p_from_last) const {
  287. if (!p_from_last) {
  288. for (int i = 0; i < get_item_count(); i++) {
  289. if (!is_item_disabled(i) && !is_item_separator(i)) {
  290. return i;
  291. }
  292. }
  293. } else {
  294. for (int i = get_item_count() - 1; i >= 0; i--) {
  295. if (!is_item_disabled(i) && !is_item_separator(i)) {
  296. return i;
  297. }
  298. }
  299. }
  300. return -1;
  301. }
  302. int OptionButton::get_item_count() const {
  303. return popup->get_item_count();
  304. }
  305. void OptionButton::set_fit_to_longest_item(bool p_fit) {
  306. if (p_fit == fit_to_longest_item) {
  307. return;
  308. }
  309. fit_to_longest_item = p_fit;
  310. _refresh_size_cache();
  311. }
  312. bool OptionButton::is_fit_to_longest_item() const {
  313. return fit_to_longest_item;
  314. }
  315. void OptionButton::set_allow_reselect(bool p_allow) {
  316. allow_reselect = p_allow;
  317. }
  318. bool OptionButton::get_allow_reselect() const {
  319. return allow_reselect;
  320. }
  321. void OptionButton::add_separator(const String &p_text) {
  322. popup->add_separator(p_text);
  323. }
  324. void OptionButton::clear() {
  325. popup->clear();
  326. set_text("");
  327. current = NONE_SELECTED;
  328. _refresh_size_cache();
  329. }
  330. void OptionButton::_select(int p_which, bool p_emit) {
  331. if (p_which == current && !allow_reselect) {
  332. return;
  333. }
  334. if (p_which == NONE_SELECTED) {
  335. for (int i = 0; i < popup->get_item_count(); i++) {
  336. popup->set_item_checked(i, false);
  337. }
  338. current = NONE_SELECTED;
  339. set_text("");
  340. set_button_icon(nullptr);
  341. } else {
  342. ERR_FAIL_INDEX(p_which, popup->get_item_count());
  343. for (int i = 0; i < popup->get_item_count(); i++) {
  344. popup->set_item_checked(i, i == p_which);
  345. }
  346. current = p_which;
  347. set_text(popup->get_item_text(current));
  348. set_button_icon(popup->get_item_icon(current));
  349. }
  350. if (is_inside_tree() && p_emit) {
  351. emit_signal(SceneStringName(item_selected), current);
  352. }
  353. }
  354. void OptionButton::_select_int(int p_which) {
  355. if (p_which < NONE_SELECTED) {
  356. return;
  357. }
  358. if (p_which >= popup->get_item_count()) {
  359. if (!initialized) {
  360. queued_current = p_which;
  361. }
  362. return;
  363. }
  364. _select(p_which, false);
  365. }
  366. void OptionButton::_refresh_size_cache() {
  367. cache_refresh_pending = false;
  368. if (fit_to_longest_item) {
  369. _cached_size = theme_cache.normal->get_minimum_size();
  370. for (int i = 0; i < get_item_count(); i++) {
  371. _cached_size = _cached_size.max(get_minimum_size_for_text_and_icon(popup->get_item_xl_text(i), get_item_icon(i)));
  372. }
  373. }
  374. update_minimum_size();
  375. }
  376. void OptionButton::_queue_update_size_cache() {
  377. if (cache_refresh_pending) {
  378. return;
  379. }
  380. cache_refresh_pending = true;
  381. callable_mp(this, &OptionButton::_refresh_size_cache).call_deferred();
  382. }
  383. String OptionButton::_get_translated_text(const String &p_text) const {
  384. if (0 <= current && current < popup->get_item_count()) {
  385. AutoTranslateMode mode = popup->get_item_auto_translate_mode(current);
  386. switch (mode) {
  387. case AUTO_TRANSLATE_MODE_INHERIT: {
  388. return atr(p_text);
  389. } break;
  390. case AUTO_TRANSLATE_MODE_ALWAYS: {
  391. return tr(p_text);
  392. } break;
  393. case AUTO_TRANSLATE_MODE_DISABLED: {
  394. return p_text;
  395. } break;
  396. }
  397. ERR_FAIL_V_MSG(atr(p_text), "Unexpected auto translate mode: " + itos(mode));
  398. }
  399. return atr(p_text);
  400. }
  401. void OptionButton::select(int p_idx) {
  402. _select(p_idx, false);
  403. }
  404. int OptionButton::get_selected() const {
  405. return current;
  406. }
  407. int OptionButton::get_selected_id() const {
  408. return get_item_id(current);
  409. }
  410. Variant OptionButton::get_selected_metadata() const {
  411. int idx = get_selected();
  412. if (idx < 0) {
  413. return Variant();
  414. }
  415. return get_item_metadata(current);
  416. }
  417. void OptionButton::remove_item(int p_idx) {
  418. popup->remove_item(p_idx);
  419. if (current == p_idx) {
  420. _select(NONE_SELECTED);
  421. }
  422. _queue_update_size_cache();
  423. }
  424. PopupMenu *OptionButton::get_popup() const {
  425. return popup;
  426. }
  427. void OptionButton::show_popup() {
  428. if (!get_viewport()) {
  429. return;
  430. }
  431. // If not triggered by the mouse, start the popup with the checked item (or the first enabled one) focused.
  432. if (current != NONE_SELECTED && !popup->is_item_disabled(current)) {
  433. if (!_was_pressed_by_mouse()) {
  434. popup->set_focused_item(current);
  435. } else {
  436. popup->scroll_to_item(current);
  437. }
  438. } else {
  439. for (int i = 0; i < popup->get_item_count(); i++) {
  440. if (!popup->is_item_disabled(i)) {
  441. if (!_was_pressed_by_mouse()) {
  442. popup->set_focused_item(i);
  443. } else {
  444. popup->scroll_to_item(i);
  445. }
  446. break;
  447. }
  448. }
  449. }
  450. Rect2 rect = get_screen_rect();
  451. rect.position.y += rect.size.height;
  452. rect.size.height = 0;
  453. popup->popup(rect);
  454. }
  455. void OptionButton::_validate_property(PropertyInfo &p_property) const {
  456. if (p_property.name == "text" || p_property.name == "icon") {
  457. p_property.usage = PROPERTY_USAGE_NONE;
  458. }
  459. }
  460. void OptionButton::_bind_methods() {
  461. ClassDB::bind_method(D_METHOD("add_item", "label", "id"), &OptionButton::add_item, DEFVAL(-1));
  462. ClassDB::bind_method(D_METHOD("add_icon_item", "texture", "label", "id"), &OptionButton::add_icon_item, DEFVAL(-1));
  463. ClassDB::bind_method(D_METHOD("set_item_text", "idx", "text"), &OptionButton::set_item_text);
  464. ClassDB::bind_method(D_METHOD("set_item_icon", "idx", "texture"), &OptionButton::set_item_icon);
  465. ClassDB::bind_method(D_METHOD("set_item_disabled", "idx", "disabled"), &OptionButton::set_item_disabled);
  466. ClassDB::bind_method(D_METHOD("set_item_id", "idx", "id"), &OptionButton::set_item_id);
  467. ClassDB::bind_method(D_METHOD("set_item_metadata", "idx", "metadata"), &OptionButton::set_item_metadata);
  468. ClassDB::bind_method(D_METHOD("set_item_tooltip", "idx", "tooltip"), &OptionButton::set_item_tooltip);
  469. ClassDB::bind_method(D_METHOD("set_item_auto_translate_mode", "idx", "mode"), &OptionButton::set_item_auto_translate_mode);
  470. ClassDB::bind_method(D_METHOD("get_item_text", "idx"), &OptionButton::get_item_text);
  471. ClassDB::bind_method(D_METHOD("get_item_icon", "idx"), &OptionButton::get_item_icon);
  472. ClassDB::bind_method(D_METHOD("get_item_id", "idx"), &OptionButton::get_item_id);
  473. ClassDB::bind_method(D_METHOD("get_item_index", "id"), &OptionButton::get_item_index);
  474. ClassDB::bind_method(D_METHOD("get_item_metadata", "idx"), &OptionButton::get_item_metadata);
  475. ClassDB::bind_method(D_METHOD("get_item_tooltip", "idx"), &OptionButton::get_item_tooltip);
  476. ClassDB::bind_method(D_METHOD("get_item_auto_translate_mode", "idx"), &OptionButton::get_item_auto_translate_mode);
  477. ClassDB::bind_method(D_METHOD("is_item_disabled", "idx"), &OptionButton::is_item_disabled);
  478. ClassDB::bind_method(D_METHOD("is_item_separator", "idx"), &OptionButton::is_item_separator);
  479. ClassDB::bind_method(D_METHOD("add_separator", "text"), &OptionButton::add_separator, DEFVAL(String()));
  480. ClassDB::bind_method(D_METHOD("clear"), &OptionButton::clear);
  481. ClassDB::bind_method(D_METHOD("select", "idx"), &OptionButton::select);
  482. ClassDB::bind_method(D_METHOD("get_selected"), &OptionButton::get_selected);
  483. ClassDB::bind_method(D_METHOD("get_selected_id"), &OptionButton::get_selected_id);
  484. ClassDB::bind_method(D_METHOD("get_selected_metadata"), &OptionButton::get_selected_metadata);
  485. ClassDB::bind_method(D_METHOD("remove_item", "idx"), &OptionButton::remove_item);
  486. ClassDB::bind_method(D_METHOD("_select_int", "idx"), &OptionButton::_select_int);
  487. ClassDB::bind_method(D_METHOD("get_popup"), &OptionButton::get_popup);
  488. ClassDB::bind_method(D_METHOD("show_popup"), &OptionButton::show_popup);
  489. ClassDB::bind_method(D_METHOD("set_item_count", "count"), &OptionButton::set_item_count);
  490. ClassDB::bind_method(D_METHOD("get_item_count"), &OptionButton::get_item_count);
  491. ClassDB::bind_method(D_METHOD("has_selectable_items"), &OptionButton::has_selectable_items);
  492. ClassDB::bind_method(D_METHOD("get_selectable_item", "from_last"), &OptionButton::get_selectable_item, DEFVAL(false));
  493. ClassDB::bind_method(D_METHOD("set_fit_to_longest_item", "fit"), &OptionButton::set_fit_to_longest_item);
  494. ClassDB::bind_method(D_METHOD("is_fit_to_longest_item"), &OptionButton::is_fit_to_longest_item);
  495. ClassDB::bind_method(D_METHOD("set_allow_reselect", "allow"), &OptionButton::set_allow_reselect);
  496. ClassDB::bind_method(D_METHOD("get_allow_reselect"), &OptionButton::get_allow_reselect);
  497. ClassDB::bind_method(D_METHOD("set_disable_shortcuts", "disabled"), &OptionButton::set_disable_shortcuts);
  498. ADD_PROPERTY(PropertyInfo(Variant::INT, "selected"), "_select_int", "get_selected");
  499. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fit_to_longest_item"), "set_fit_to_longest_item", "is_fit_to_longest_item");
  500. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_reselect"), "set_allow_reselect", "get_allow_reselect");
  501. ADD_ARRAY_COUNT("Items", "item_count", "set_item_count", "get_item_count", "popup/item_");
  502. ADD_SIGNAL(MethodInfo("item_selected", PropertyInfo(Variant::INT, "index")));
  503. ADD_SIGNAL(MethodInfo("item_focused", PropertyInfo(Variant::INT, "index")));
  504. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, OptionButton, normal);
  505. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_color);
  506. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_focus_color);
  507. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_pressed_color);
  508. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_hover_color);
  509. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_hover_pressed_color);
  510. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_disabled_color);
  511. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, OptionButton, h_separation);
  512. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, OptionButton, arrow_icon, "arrow");
  513. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, OptionButton, arrow_margin);
  514. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, OptionButton, modulate_arrow);
  515. PopupMenu::Item defaults(true);
  516. base_property_helper.set_prefix("popup/item_");
  517. base_property_helper.set_array_length_getter(&OptionButton::get_item_count);
  518. base_property_helper.register_property(PropertyInfo(Variant::STRING, "text"), defaults.text, &OptionButton::_dummy_setter, &OptionButton::get_item_text);
  519. base_property_helper.register_property(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), defaults.icon, &OptionButton::_dummy_setter, &OptionButton::get_item_icon);
  520. base_property_helper.register_property(PropertyInfo(Variant::INT, "id", PROPERTY_HINT_RANGE, "0,10,1,or_greater", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL), defaults.id, &OptionButton::_dummy_setter, &OptionButton::get_item_id);
  521. base_property_helper.register_property(PropertyInfo(Variant::BOOL, "disabled"), defaults.disabled, &OptionButton::_dummy_setter, &OptionButton::is_item_disabled);
  522. base_property_helper.register_property(PropertyInfo(Variant::BOOL, "separator"), defaults.separator, &OptionButton::_dummy_setter, &OptionButton::is_item_separator);
  523. PropertyListHelper::register_base_helper(&base_property_helper);
  524. }
  525. void OptionButton::set_disable_shortcuts(bool p_disabled) {
  526. disable_shortcuts = p_disabled;
  527. }
  528. #ifdef TOOLS_ENABLED
  529. PackedStringArray OptionButton::get_configuration_warnings() const {
  530. PackedStringArray warnings = Button::get_configuration_warnings();
  531. warnings.append_array(popup->get_configuration_warnings());
  532. return warnings;
  533. }
  534. #endif
  535. OptionButton::OptionButton(const String &p_text) :
  536. Button(p_text) {
  537. set_toggle_mode(true);
  538. set_process_shortcut_input(true);
  539. set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  540. set_action_mode(ACTION_MODE_BUTTON_PRESS);
  541. popup = memnew(PopupMenu);
  542. popup->hide();
  543. add_child(popup, false, INTERNAL_MODE_FRONT);
  544. popup->connect("index_pressed", callable_mp(this, &OptionButton::_selected));
  545. popup->connect("id_focused", callable_mp(this, &OptionButton::_focused));
  546. popup->connect("popup_hide", callable_mp((BaseButton *)this, &BaseButton::set_pressed).bind(false));
  547. property_helper.setup_for_instance(base_property_helper, this);
  548. }
  549. OptionButton::~OptionButton() {
  550. }