option_button.cpp 20 KB

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