option_button.cpp 20 KB

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