option_button.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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. if (current > p_count - 1) {
  264. _select(p_count - 1, false);
  265. }
  266. popup->set_item_count(p_count);
  267. if (p_count > count_old) {
  268. for (int i = count_old; i < p_count; i++) {
  269. popup->set_item_as_radio_checkable(i, true);
  270. }
  271. }
  272. if (!initialized) {
  273. if (queued_current != current) {
  274. current = queued_current;
  275. }
  276. initialized = true;
  277. }
  278. _refresh_size_cache();
  279. notify_property_list_changed();
  280. }
  281. bool OptionButton::has_selectable_items() const {
  282. for (int i = 0; i < get_item_count(); i++) {
  283. if (!is_item_disabled(i) && !is_item_separator(i)) {
  284. return true;
  285. }
  286. }
  287. return false;
  288. }
  289. int OptionButton::get_selectable_item(bool p_from_last) const {
  290. if (!p_from_last) {
  291. for (int i = 0; i < get_item_count(); i++) {
  292. if (!is_item_disabled(i) && !is_item_separator(i)) {
  293. return i;
  294. }
  295. }
  296. } else {
  297. for (int i = get_item_count() - 1; i >= 0; i--) {
  298. if (!is_item_disabled(i) && !is_item_separator(i)) {
  299. return i;
  300. }
  301. }
  302. }
  303. return -1;
  304. }
  305. int OptionButton::get_item_count() const {
  306. return popup->get_item_count();
  307. }
  308. void OptionButton::set_fit_to_longest_item(bool p_fit) {
  309. if (p_fit == fit_to_longest_item) {
  310. return;
  311. }
  312. fit_to_longest_item = p_fit;
  313. _refresh_size_cache();
  314. }
  315. bool OptionButton::is_fit_to_longest_item() const {
  316. return fit_to_longest_item;
  317. }
  318. void OptionButton::set_allow_reselect(bool p_allow) {
  319. allow_reselect = p_allow;
  320. }
  321. bool OptionButton::get_allow_reselect() const {
  322. return allow_reselect;
  323. }
  324. void OptionButton::add_separator(const String &p_text) {
  325. popup->add_separator(p_text);
  326. }
  327. void OptionButton::clear() {
  328. popup->clear();
  329. set_text("");
  330. current = NONE_SELECTED;
  331. _refresh_size_cache();
  332. }
  333. void OptionButton::_select(int p_which, bool p_emit) {
  334. if (p_which == current && !allow_reselect) {
  335. return;
  336. }
  337. if (p_which == NONE_SELECTED) {
  338. for (int i = 0; i < popup->get_item_count(); i++) {
  339. popup->set_item_checked(i, false);
  340. }
  341. current = NONE_SELECTED;
  342. set_text("");
  343. set_button_icon(nullptr);
  344. } else {
  345. ERR_FAIL_INDEX(p_which, popup->get_item_count());
  346. for (int i = 0; i < popup->get_item_count(); i++) {
  347. popup->set_item_checked(i, i == p_which);
  348. }
  349. current = p_which;
  350. set_text(popup->get_item_text(current));
  351. set_button_icon(popup->get_item_icon(current));
  352. }
  353. if (is_inside_tree() && p_emit) {
  354. emit_signal(SceneStringName(item_selected), current);
  355. }
  356. }
  357. void OptionButton::_select_int(int p_which) {
  358. if (p_which < NONE_SELECTED) {
  359. return;
  360. }
  361. if (p_which >= popup->get_item_count()) {
  362. if (!initialized) {
  363. queued_current = p_which;
  364. }
  365. return;
  366. }
  367. _select(p_which, false);
  368. }
  369. void OptionButton::_refresh_size_cache() {
  370. cache_refresh_pending = false;
  371. if (fit_to_longest_item) {
  372. _cached_size = theme_cache.normal->get_minimum_size();
  373. for (int i = 0; i < get_item_count(); i++) {
  374. _cached_size = _cached_size.max(get_minimum_size_for_text_and_icon(popup->get_item_xl_text(i), get_item_icon(i)));
  375. }
  376. }
  377. update_minimum_size();
  378. }
  379. void OptionButton::_queue_update_size_cache() {
  380. if (cache_refresh_pending) {
  381. return;
  382. }
  383. cache_refresh_pending = true;
  384. callable_mp(this, &OptionButton::_refresh_size_cache).call_deferred();
  385. }
  386. String OptionButton::_get_translated_text(const String &p_text) const {
  387. if (0 <= current && current < popup->get_item_count()) {
  388. AutoTranslateMode mode = popup->get_item_auto_translate_mode(current);
  389. switch (mode) {
  390. case AUTO_TRANSLATE_MODE_INHERIT: {
  391. return atr(p_text);
  392. } break;
  393. case AUTO_TRANSLATE_MODE_ALWAYS: {
  394. return tr(p_text);
  395. } break;
  396. case AUTO_TRANSLATE_MODE_DISABLED: {
  397. return p_text;
  398. } break;
  399. }
  400. ERR_FAIL_V_MSG(atr(p_text), "Unexpected auto translate mode: " + itos(mode));
  401. }
  402. return atr(p_text);
  403. }
  404. void OptionButton::select(int p_idx) {
  405. _select(p_idx, false);
  406. }
  407. int OptionButton::get_selected() const {
  408. return current;
  409. }
  410. int OptionButton::get_selected_id() const {
  411. return get_item_id(current);
  412. }
  413. Variant OptionButton::get_selected_metadata() const {
  414. int idx = get_selected();
  415. if (idx < 0) {
  416. return Variant();
  417. }
  418. return get_item_metadata(current);
  419. }
  420. void OptionButton::remove_item(int p_idx) {
  421. popup->remove_item(p_idx);
  422. if (current == p_idx) {
  423. _select(NONE_SELECTED);
  424. }
  425. _queue_update_size_cache();
  426. }
  427. PopupMenu *OptionButton::get_popup() const {
  428. return popup;
  429. }
  430. void OptionButton::show_popup() {
  431. if (!get_viewport()) {
  432. return;
  433. }
  434. // If not triggered by the mouse, start the popup with the checked item (or the first enabled one) focused.
  435. if (current != NONE_SELECTED && !popup->is_item_disabled(current)) {
  436. if (!_was_pressed_by_mouse()) {
  437. popup->set_focused_item(current);
  438. } else {
  439. popup->scroll_to_item(current);
  440. }
  441. } else {
  442. for (int i = 0; i < popup->get_item_count(); i++) {
  443. if (!popup->is_item_disabled(i)) {
  444. if (!_was_pressed_by_mouse()) {
  445. popup->set_focused_item(i);
  446. } else {
  447. popup->scroll_to_item(i);
  448. }
  449. break;
  450. }
  451. }
  452. }
  453. Rect2 rect = get_screen_rect();
  454. rect.position.y += rect.size.height;
  455. if (get_viewport()->is_embedding_subwindows() && popup->get_force_native()) {
  456. Transform2D xform = get_viewport()->get_popup_base_transform_native();
  457. rect = xform.xform(rect);
  458. }
  459. rect.size.height = 0;
  460. popup->popup(rect);
  461. }
  462. void OptionButton::_validate_property(PropertyInfo &p_property) const {
  463. if (p_property.name == "text" || p_property.name == "icon") {
  464. p_property.usage = PROPERTY_USAGE_NONE;
  465. }
  466. }
  467. void OptionButton::_bind_methods() {
  468. ClassDB::bind_method(D_METHOD("add_item", "label", "id"), &OptionButton::add_item, DEFVAL(-1));
  469. ClassDB::bind_method(D_METHOD("add_icon_item", "texture", "label", "id"), &OptionButton::add_icon_item, DEFVAL(-1));
  470. ClassDB::bind_method(D_METHOD("set_item_text", "idx", "text"), &OptionButton::set_item_text);
  471. ClassDB::bind_method(D_METHOD("set_item_icon", "idx", "texture"), &OptionButton::set_item_icon);
  472. ClassDB::bind_method(D_METHOD("set_item_disabled", "idx", "disabled"), &OptionButton::set_item_disabled);
  473. ClassDB::bind_method(D_METHOD("set_item_id", "idx", "id"), &OptionButton::set_item_id);
  474. ClassDB::bind_method(D_METHOD("set_item_metadata", "idx", "metadata"), &OptionButton::set_item_metadata);
  475. ClassDB::bind_method(D_METHOD("set_item_tooltip", "idx", "tooltip"), &OptionButton::set_item_tooltip);
  476. ClassDB::bind_method(D_METHOD("set_item_auto_translate_mode", "idx", "mode"), &OptionButton::set_item_auto_translate_mode);
  477. ClassDB::bind_method(D_METHOD("get_item_text", "idx"), &OptionButton::get_item_text);
  478. ClassDB::bind_method(D_METHOD("get_item_icon", "idx"), &OptionButton::get_item_icon);
  479. ClassDB::bind_method(D_METHOD("get_item_id", "idx"), &OptionButton::get_item_id);
  480. ClassDB::bind_method(D_METHOD("get_item_index", "id"), &OptionButton::get_item_index);
  481. ClassDB::bind_method(D_METHOD("get_item_metadata", "idx"), &OptionButton::get_item_metadata);
  482. ClassDB::bind_method(D_METHOD("get_item_tooltip", "idx"), &OptionButton::get_item_tooltip);
  483. ClassDB::bind_method(D_METHOD("get_item_auto_translate_mode", "idx"), &OptionButton::get_item_auto_translate_mode);
  484. ClassDB::bind_method(D_METHOD("is_item_disabled", "idx"), &OptionButton::is_item_disabled);
  485. ClassDB::bind_method(D_METHOD("is_item_separator", "idx"), &OptionButton::is_item_separator);
  486. ClassDB::bind_method(D_METHOD("add_separator", "text"), &OptionButton::add_separator, DEFVAL(String()));
  487. ClassDB::bind_method(D_METHOD("clear"), &OptionButton::clear);
  488. ClassDB::bind_method(D_METHOD("select", "idx"), &OptionButton::select);
  489. ClassDB::bind_method(D_METHOD("get_selected"), &OptionButton::get_selected);
  490. ClassDB::bind_method(D_METHOD("get_selected_id"), &OptionButton::get_selected_id);
  491. ClassDB::bind_method(D_METHOD("get_selected_metadata"), &OptionButton::get_selected_metadata);
  492. ClassDB::bind_method(D_METHOD("remove_item", "idx"), &OptionButton::remove_item);
  493. ClassDB::bind_method(D_METHOD("_select_int", "idx"), &OptionButton::_select_int);
  494. ClassDB::bind_method(D_METHOD("get_popup"), &OptionButton::get_popup);
  495. ClassDB::bind_method(D_METHOD("show_popup"), &OptionButton::show_popup);
  496. ClassDB::bind_method(D_METHOD("set_item_count", "count"), &OptionButton::set_item_count);
  497. ClassDB::bind_method(D_METHOD("get_item_count"), &OptionButton::get_item_count);
  498. ClassDB::bind_method(D_METHOD("has_selectable_items"), &OptionButton::has_selectable_items);
  499. ClassDB::bind_method(D_METHOD("get_selectable_item", "from_last"), &OptionButton::get_selectable_item, DEFVAL(false));
  500. ClassDB::bind_method(D_METHOD("set_fit_to_longest_item", "fit"), &OptionButton::set_fit_to_longest_item);
  501. ClassDB::bind_method(D_METHOD("is_fit_to_longest_item"), &OptionButton::is_fit_to_longest_item);
  502. ClassDB::bind_method(D_METHOD("set_allow_reselect", "allow"), &OptionButton::set_allow_reselect);
  503. ClassDB::bind_method(D_METHOD("get_allow_reselect"), &OptionButton::get_allow_reselect);
  504. ClassDB::bind_method(D_METHOD("set_disable_shortcuts", "disabled"), &OptionButton::set_disable_shortcuts);
  505. ADD_PROPERTY(PropertyInfo(Variant::INT, "selected"), "_select_int", "get_selected");
  506. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fit_to_longest_item"), "set_fit_to_longest_item", "is_fit_to_longest_item");
  507. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_reselect"), "set_allow_reselect", "get_allow_reselect");
  508. ADD_ARRAY_COUNT("Items", "item_count", "set_item_count", "get_item_count", "popup/item_");
  509. ADD_SIGNAL(MethodInfo("item_selected", PropertyInfo(Variant::INT, "index")));
  510. ADD_SIGNAL(MethodInfo("item_focused", PropertyInfo(Variant::INT, "index")));
  511. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, OptionButton, normal);
  512. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_color);
  513. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_focus_color);
  514. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_pressed_color);
  515. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_hover_color);
  516. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_hover_pressed_color);
  517. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_disabled_color);
  518. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, OptionButton, h_separation);
  519. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, OptionButton, arrow_icon, "arrow");
  520. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, OptionButton, arrow_margin);
  521. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, OptionButton, modulate_arrow);
  522. PopupMenu::Item defaults(true);
  523. base_property_helper.set_prefix("popup/item_");
  524. base_property_helper.set_array_length_getter(&OptionButton::get_item_count);
  525. base_property_helper.register_property(PropertyInfo(Variant::STRING, "text"), defaults.text, &OptionButton::_dummy_setter, &OptionButton::get_item_text);
  526. base_property_helper.register_property(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), defaults.icon, &OptionButton::_dummy_setter, &OptionButton::get_item_icon);
  527. 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);
  528. base_property_helper.register_property(PropertyInfo(Variant::BOOL, "disabled"), defaults.disabled, &OptionButton::_dummy_setter, &OptionButton::is_item_disabled);
  529. base_property_helper.register_property(PropertyInfo(Variant::BOOL, "separator"), defaults.separator, &OptionButton::_dummy_setter, &OptionButton::is_item_separator);
  530. PropertyListHelper::register_base_helper(&base_property_helper);
  531. ADD_CLASS_DEPENDENCY("PopupMenu");
  532. }
  533. void OptionButton::set_disable_shortcuts(bool p_disabled) {
  534. disable_shortcuts = p_disabled;
  535. }
  536. #ifdef TOOLS_ENABLED
  537. PackedStringArray OptionButton::get_configuration_warnings() const {
  538. PackedStringArray warnings = Button::get_configuration_warnings();
  539. warnings.append_array(popup->get_configuration_warnings());
  540. return warnings;
  541. }
  542. #endif
  543. OptionButton::OptionButton(const String &p_text) :
  544. Button(p_text) {
  545. set_toggle_mode(true);
  546. set_process_shortcut_input(true);
  547. set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  548. set_action_mode(ACTION_MODE_BUTTON_PRESS);
  549. popup = memnew(PopupMenu);
  550. popup->hide();
  551. add_child(popup, false, INTERNAL_MODE_FRONT);
  552. popup->connect("index_pressed", callable_mp(this, &OptionButton::_selected));
  553. popup->connect("id_focused", callable_mp(this, &OptionButton::_focused));
  554. popup->connect("popup_hide", callable_mp((BaseButton *)this, &BaseButton::set_pressed).bind(false));
  555. property_helper.setup_for_instance(base_property_helper, this);
  556. }
  557. OptionButton::~OptionButton() {
  558. }