option_button.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*************************************************************************/
  2. /* option_button.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "option_button.h"
  31. #include "core/string/print_string.h"
  32. static const int NONE_SELECTED = -1;
  33. Size2 OptionButton::get_minimum_size() const {
  34. Size2 minsize;
  35. if (fit_to_longest_item) {
  36. minsize = _cached_size;
  37. } else {
  38. minsize = Button::get_minimum_size();
  39. }
  40. if (has_theme_icon(SNAME("arrow"))) {
  41. const Size2 padding = theme_cache.normal->get_minimum_size();
  42. const Size2 arrow_size = theme_cache.arrow_icon->get_size();
  43. Size2 content_size = minsize - padding;
  44. content_size.width += arrow_size.width + MAX(0, theme_cache.h_separation);
  45. content_size.height = MAX(content_size.height, arrow_size.height);
  46. minsize = content_size + padding;
  47. }
  48. return minsize;
  49. }
  50. void OptionButton::_update_theme_item_cache() {
  51. Button::_update_theme_item_cache();
  52. theme_cache.normal = get_theme_stylebox(SNAME("normal"));
  53. theme_cache.font_color = get_theme_color(SNAME("font_color"));
  54. theme_cache.font_focus_color = get_theme_color(SNAME("font_focus_color"));
  55. theme_cache.font_pressed_color = get_theme_color(SNAME("font_pressed_color"));
  56. theme_cache.font_hover_color = get_theme_color(SNAME("font_hover_color"));
  57. theme_cache.font_hover_pressed_color = get_theme_color(SNAME("font_hover_pressed_color"));
  58. theme_cache.font_disabled_color = get_theme_color(SNAME("font_disabled_color"));
  59. theme_cache.h_separation = get_theme_constant(SNAME("h_separation"));
  60. theme_cache.arrow_icon = get_theme_icon(SNAME("arrow"));
  61. theme_cache.arrow_margin = get_theme_constant(SNAME("arrow_margin"));
  62. theme_cache.modulate_arrow = get_theme_constant(SNAME("modulate_arrow"));
  63. }
  64. void OptionButton::_notification(int p_what) {
  65. switch (p_what) {
  66. case NOTIFICATION_POSTINITIALIZE: {
  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. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  114. popup->set_layout_direction((Window::LayoutDirection)get_layout_direction());
  115. [[fallthrough]];
  116. }
  117. case NOTIFICATION_THEME_CHANGED: {
  118. if (has_theme_icon(SNAME("arrow"))) {
  119. if (is_layout_rtl()) {
  120. _set_internal_margin(SIDE_LEFT, theme_cache.arrow_icon->get_width());
  121. _set_internal_margin(SIDE_RIGHT, 0.f);
  122. } else {
  123. _set_internal_margin(SIDE_LEFT, 0.f);
  124. _set_internal_margin(SIDE_RIGHT, theme_cache.arrow_icon->get_width());
  125. }
  126. }
  127. _refresh_size_cache();
  128. } break;
  129. case NOTIFICATION_VISIBILITY_CHANGED: {
  130. if (!is_visible_in_tree()) {
  131. popup->hide();
  132. }
  133. } break;
  134. }
  135. }
  136. bool OptionButton::_set(const StringName &p_name, const Variant &p_value) {
  137. Vector<String> components = String(p_name).split("/", true, 2);
  138. if (components.size() >= 2 && components[0] == "popup") {
  139. String property = components[2];
  140. if (property != "text" && property != "icon" && property != "id" && property != "disabled" && property != "separator") {
  141. return false;
  142. }
  143. bool valid;
  144. popup->set(String(p_name).trim_prefix("popup/"), p_value, &valid);
  145. int idx = components[1].get_slice("_", 1).to_int();
  146. if (idx == current) {
  147. // Force refreshing currently displayed item.
  148. current = NONE_SELECTED;
  149. _select(idx, false);
  150. }
  151. if (property == "text" || property == "icon") {
  152. _queue_refresh_cache();
  153. }
  154. return valid;
  155. }
  156. return false;
  157. }
  158. bool OptionButton::_get(const StringName &p_name, Variant &r_ret) const {
  159. Vector<String> components = String(p_name).split("/", true, 2);
  160. if (components.size() >= 2 && components[0] == "popup") {
  161. String property = components[2];
  162. if (property != "text" && property != "icon" && property != "id" && property != "disabled" && property != "separator") {
  163. return false;
  164. }
  165. bool valid;
  166. r_ret = popup->get(String(p_name).trim_prefix("popup/"), &valid);
  167. return valid;
  168. }
  169. return false;
  170. }
  171. void OptionButton::_get_property_list(List<PropertyInfo> *p_list) const {
  172. for (int i = 0; i < popup->get_item_count(); i++) {
  173. p_list->push_back(PropertyInfo(Variant::STRING, vformat("popup/item_%d/text", i)));
  174. PropertyInfo pi = PropertyInfo(Variant::OBJECT, vformat("popup/item_%d/icon", i), PROPERTY_HINT_RESOURCE_TYPE, "Texture2D");
  175. pi.usage &= ~(popup->get_item_icon(i).is_null() ? PROPERTY_USAGE_STORAGE : 0);
  176. p_list->push_back(pi);
  177. pi = PropertyInfo(Variant::INT, vformat("popup/item_%d/id", i), PROPERTY_HINT_RANGE, "0,10,1,or_greater");
  178. p_list->push_back(pi);
  179. pi = PropertyInfo(Variant::BOOL, vformat("popup/item_%d/disabled", i));
  180. pi.usage &= ~(!popup->is_item_disabled(i) ? PROPERTY_USAGE_STORAGE : 0);
  181. p_list->push_back(pi);
  182. pi = PropertyInfo(Variant::BOOL, vformat("popup/item_%d/separator", i));
  183. pi.usage &= ~(!popup->is_item_separator(i) ? PROPERTY_USAGE_STORAGE : 0);
  184. p_list->push_back(pi);
  185. }
  186. }
  187. void OptionButton::_focused(int p_which) {
  188. emit_signal(SNAME("item_focused"), p_which);
  189. }
  190. void OptionButton::_selected(int p_which) {
  191. _select(p_which, true);
  192. }
  193. void OptionButton::pressed() {
  194. if (popup->is_visible()) {
  195. popup->hide();
  196. return;
  197. }
  198. show_popup();
  199. }
  200. void OptionButton::add_icon_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id) {
  201. bool first_selectable = !has_selectable_items();
  202. popup->add_icon_radio_check_item(p_icon, p_label, p_id);
  203. if (first_selectable) {
  204. select(get_item_count() - 1);
  205. }
  206. _queue_refresh_cache();
  207. }
  208. void OptionButton::add_item(const String &p_label, int p_id) {
  209. bool first_selectable = !has_selectable_items();
  210. popup->add_radio_check_item(p_label, p_id);
  211. if (first_selectable) {
  212. select(get_item_count() - 1);
  213. }
  214. _queue_refresh_cache();
  215. }
  216. void OptionButton::set_item_text(int p_idx, const String &p_text) {
  217. popup->set_item_text(p_idx, p_text);
  218. if (current == p_idx) {
  219. set_text(p_text);
  220. }
  221. _queue_refresh_cache();
  222. }
  223. void OptionButton::set_item_icon(int p_idx, const Ref<Texture2D> &p_icon) {
  224. popup->set_item_icon(p_idx, p_icon);
  225. if (current == p_idx) {
  226. set_icon(p_icon);
  227. }
  228. _queue_refresh_cache();
  229. }
  230. void OptionButton::set_item_id(int p_idx, int p_id) {
  231. popup->set_item_id(p_idx, p_id);
  232. }
  233. void OptionButton::set_item_metadata(int p_idx, const Variant &p_metadata) {
  234. popup->set_item_metadata(p_idx, p_metadata);
  235. }
  236. void OptionButton::set_item_tooltip(int p_idx, const String &p_tooltip) {
  237. popup->set_item_tooltip(p_idx, p_tooltip);
  238. }
  239. void OptionButton::set_item_disabled(int p_idx, bool p_disabled) {
  240. popup->set_item_disabled(p_idx, p_disabled);
  241. }
  242. String OptionButton::get_item_text(int p_idx) const {
  243. return popup->get_item_text(p_idx);
  244. }
  245. Ref<Texture2D> OptionButton::get_item_icon(int p_idx) const {
  246. return popup->get_item_icon(p_idx);
  247. }
  248. int OptionButton::get_item_id(int p_idx) const {
  249. if (p_idx == NONE_SELECTED) {
  250. return NONE_SELECTED;
  251. }
  252. return popup->get_item_id(p_idx);
  253. }
  254. int OptionButton::get_item_index(int p_id) const {
  255. return popup->get_item_index(p_id);
  256. }
  257. Variant OptionButton::get_item_metadata(int p_idx) const {
  258. return popup->get_item_metadata(p_idx);
  259. }
  260. String OptionButton::get_item_tooltip(int p_idx) const {
  261. return popup->get_item_tooltip(p_idx);
  262. }
  263. bool OptionButton::is_item_disabled(int p_idx) const {
  264. return popup->is_item_disabled(p_idx);
  265. }
  266. bool OptionButton::is_item_separator(int p_idx) const {
  267. return popup->is_item_separator(p_idx);
  268. }
  269. void OptionButton::set_item_count(int p_count) {
  270. ERR_FAIL_COND(p_count < 0);
  271. int count_old = get_item_count();
  272. if (p_count == count_old) {
  273. return;
  274. }
  275. popup->set_item_count(p_count);
  276. if (p_count > count_old) {
  277. for (int i = count_old; i < p_count; i++) {
  278. popup->set_item_as_radio_checkable(i, true);
  279. }
  280. }
  281. _refresh_size_cache();
  282. notify_property_list_changed();
  283. }
  284. bool OptionButton::has_selectable_items() const {
  285. for (int i = 0; i < get_item_count(); i++) {
  286. if (!is_item_disabled(i) && !is_item_separator(i)) {
  287. return true;
  288. }
  289. }
  290. return false;
  291. }
  292. int OptionButton::get_selectable_item(bool p_from_last) const {
  293. if (!p_from_last) {
  294. for (int i = 0; i < get_item_count(); i++) {
  295. if (!is_item_disabled(i) && !is_item_separator(i)) {
  296. return i;
  297. }
  298. }
  299. } else {
  300. for (int i = get_item_count() - 1; i >= 0; i--) {
  301. if (!is_item_disabled(i) && !is_item_separator(i)) {
  302. return i;
  303. }
  304. }
  305. }
  306. return -1;
  307. }
  308. int OptionButton::get_item_count() const {
  309. return popup->get_item_count();
  310. }
  311. void OptionButton::set_fit_to_longest_item(bool p_fit) {
  312. if (p_fit == fit_to_longest_item) {
  313. return;
  314. }
  315. fit_to_longest_item = p_fit;
  316. _refresh_size_cache();
  317. }
  318. bool OptionButton::is_fit_to_longest_item() const {
  319. return fit_to_longest_item;
  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) {
  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_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_icon(popup->get_item_icon(current));
  349. }
  350. if (is_inside_tree() && p_emit) {
  351. emit_signal(SNAME("item_selected"), current);
  352. }
  353. }
  354. void OptionButton::_select_int(int p_which) {
  355. if (p_which < NONE_SELECTED || p_which >= popup->get_item_count()) {
  356. return;
  357. }
  358. _select(p_which, false);
  359. }
  360. void OptionButton::_refresh_size_cache() {
  361. cache_refresh_pending = false;
  362. if (!fit_to_longest_item) {
  363. return;
  364. }
  365. _cached_size = Vector2();
  366. for (int i = 0; i < get_item_count(); i++) {
  367. _cached_size = _cached_size.max(get_minimum_size_for_text_and_icon(get_item_text(i), get_item_icon(i)));
  368. }
  369. update_minimum_size();
  370. }
  371. void OptionButton::_queue_refresh_cache() {
  372. if (cache_refresh_pending) {
  373. return;
  374. }
  375. cache_refresh_pending = true;
  376. callable_mp(this, &OptionButton::_refresh_size_cache).call_deferred();
  377. }
  378. void OptionButton::select(int p_idx) {
  379. _select(p_idx, false);
  380. }
  381. int OptionButton::get_selected() const {
  382. return current;
  383. }
  384. int OptionButton::get_selected_id() const {
  385. return get_item_id(current);
  386. }
  387. Variant OptionButton::get_selected_metadata() const {
  388. int idx = get_selected();
  389. if (idx < 0) {
  390. return Variant();
  391. }
  392. return get_item_metadata(current);
  393. }
  394. void OptionButton::remove_item(int p_idx) {
  395. popup->remove_item(p_idx);
  396. if (current == p_idx) {
  397. _select(NONE_SELECTED);
  398. }
  399. _queue_refresh_cache();
  400. }
  401. PopupMenu *OptionButton::get_popup() const {
  402. return popup;
  403. }
  404. void OptionButton::show_popup() {
  405. if (!get_viewport()) {
  406. return;
  407. }
  408. Size2 size = get_size() * get_viewport()->get_canvas_transform().get_scale();
  409. popup->set_position(get_screen_position() + Size2(0, size.height * get_global_transform().get_scale().y));
  410. popup->set_size(Size2(size.width, 0));
  411. // If not triggered by the mouse, start the popup with the checked item (or the first enabled one) focused.
  412. if (current != NONE_SELECTED && !popup->is_item_disabled(current)) {
  413. if (!_was_pressed_by_mouse()) {
  414. popup->set_focused_item(current);
  415. } else {
  416. popup->scroll_to_item(current);
  417. }
  418. } else {
  419. for (int i = 0; i < popup->get_item_count(); i++) {
  420. if (!popup->is_item_disabled(i)) {
  421. if (!_was_pressed_by_mouse()) {
  422. popup->set_focused_item(i);
  423. } else {
  424. popup->scroll_to_item(i);
  425. }
  426. break;
  427. }
  428. }
  429. }
  430. popup->popup();
  431. }
  432. void OptionButton::get_translatable_strings(List<String> *p_strings) const {
  433. popup->get_translatable_strings(p_strings);
  434. }
  435. void OptionButton::_validate_property(PropertyInfo &p_property) const {
  436. if (p_property.name == "text" || p_property.name == "icon") {
  437. p_property.usage = PROPERTY_USAGE_NONE;
  438. }
  439. }
  440. void OptionButton::_bind_methods() {
  441. ClassDB::bind_method(D_METHOD("add_item", "label", "id"), &OptionButton::add_item, DEFVAL(-1));
  442. ClassDB::bind_method(D_METHOD("add_icon_item", "texture", "label", "id"), &OptionButton::add_icon_item, DEFVAL(-1));
  443. ClassDB::bind_method(D_METHOD("set_item_text", "idx", "text"), &OptionButton::set_item_text);
  444. ClassDB::bind_method(D_METHOD("set_item_icon", "idx", "texture"), &OptionButton::set_item_icon);
  445. ClassDB::bind_method(D_METHOD("set_item_disabled", "idx", "disabled"), &OptionButton::set_item_disabled);
  446. ClassDB::bind_method(D_METHOD("set_item_id", "idx", "id"), &OptionButton::set_item_id);
  447. ClassDB::bind_method(D_METHOD("set_item_metadata", "idx", "metadata"), &OptionButton::set_item_metadata);
  448. ClassDB::bind_method(D_METHOD("set_item_tooltip", "idx", "tooltip"), &OptionButton::set_item_tooltip);
  449. ClassDB::bind_method(D_METHOD("get_item_text", "idx"), &OptionButton::get_item_text);
  450. ClassDB::bind_method(D_METHOD("get_item_icon", "idx"), &OptionButton::get_item_icon);
  451. ClassDB::bind_method(D_METHOD("get_item_id", "idx"), &OptionButton::get_item_id);
  452. ClassDB::bind_method(D_METHOD("get_item_index", "id"), &OptionButton::get_item_index);
  453. ClassDB::bind_method(D_METHOD("get_item_metadata", "idx"), &OptionButton::get_item_metadata);
  454. ClassDB::bind_method(D_METHOD("get_item_tooltip", "idx"), &OptionButton::get_item_tooltip);
  455. ClassDB::bind_method(D_METHOD("is_item_disabled", "idx"), &OptionButton::is_item_disabled);
  456. ClassDB::bind_method(D_METHOD("is_item_separator", "idx"), &OptionButton::is_item_separator);
  457. ClassDB::bind_method(D_METHOD("add_separator", "text"), &OptionButton::add_separator, DEFVAL(String()));
  458. ClassDB::bind_method(D_METHOD("clear"), &OptionButton::clear);
  459. ClassDB::bind_method(D_METHOD("select", "idx"), &OptionButton::select);
  460. ClassDB::bind_method(D_METHOD("get_selected"), &OptionButton::get_selected);
  461. ClassDB::bind_method(D_METHOD("get_selected_id"), &OptionButton::get_selected_id);
  462. ClassDB::bind_method(D_METHOD("get_selected_metadata"), &OptionButton::get_selected_metadata);
  463. ClassDB::bind_method(D_METHOD("remove_item", "idx"), &OptionButton::remove_item);
  464. ClassDB::bind_method(D_METHOD("_select_int", "idx"), &OptionButton::_select_int);
  465. ClassDB::bind_method(D_METHOD("get_popup"), &OptionButton::get_popup);
  466. ClassDB::bind_method(D_METHOD("show_popup"), &OptionButton::show_popup);
  467. ClassDB::bind_method(D_METHOD("set_item_count", "count"), &OptionButton::set_item_count);
  468. ClassDB::bind_method(D_METHOD("get_item_count"), &OptionButton::get_item_count);
  469. ClassDB::bind_method(D_METHOD("has_selectable_items"), &OptionButton::has_selectable_items);
  470. ClassDB::bind_method(D_METHOD("get_selectable_item", "from_last"), &OptionButton::get_selectable_item, DEFVAL(false));
  471. ClassDB::bind_method(D_METHOD("set_fit_to_longest_item", "fit"), &OptionButton::set_fit_to_longest_item);
  472. ClassDB::bind_method(D_METHOD("is_fit_to_longest_item"), &OptionButton::is_fit_to_longest_item);
  473. // "selected" property must come after "item_count", otherwise GH-10213 occurs.
  474. ADD_ARRAY_COUNT("Items", "item_count", "set_item_count", "get_item_count", "popup/item_");
  475. ADD_PROPERTY(PropertyInfo(Variant::INT, "selected"), "_select_int", "get_selected");
  476. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fit_to_longest_item"), "set_fit_to_longest_item", "is_fit_to_longest_item");
  477. ADD_SIGNAL(MethodInfo("item_selected", PropertyInfo(Variant::INT, "index")));
  478. ADD_SIGNAL(MethodInfo("item_focused", PropertyInfo(Variant::INT, "index")));
  479. }
  480. OptionButton::OptionButton(const String &p_text) :
  481. Button(p_text) {
  482. set_toggle_mode(true);
  483. set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  484. set_action_mode(ACTION_MODE_BUTTON_PRESS);
  485. popup = memnew(PopupMenu);
  486. popup->hide();
  487. add_child(popup, false, INTERNAL_MODE_FRONT);
  488. popup->connect("index_pressed", callable_mp(this, &OptionButton::_selected));
  489. popup->connect("id_focused", callable_mp(this, &OptionButton::_focused));
  490. popup->connect("popup_hide", callable_mp((BaseButton *)this, &BaseButton::set_pressed).bind(false));
  491. _refresh_size_cache();
  492. }
  493. OptionButton::~OptionButton() {
  494. }