popup_menu.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815
  1. /*************************************************************************/
  2. /* popup_menu.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "popup_menu.h"
  31. #include "core/input/input.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/os/os.h"
  34. #include "core/string/print_string.h"
  35. #include "core/string/translation.h"
  36. String PopupMenu::_get_accel_text(const Item &p_item) const {
  37. if (p_item.shortcut.is_valid()) {
  38. return p_item.shortcut->get_as_text();
  39. } else if (p_item.accel) {
  40. return keycode_get_string(p_item.accel);
  41. }
  42. return String();
  43. }
  44. Size2 PopupMenu::_get_contents_minimum_size() const {
  45. int vseparation = get_theme_constant(SNAME("vseparation"));
  46. int hseparation = get_theme_constant(SNAME("hseparation"));
  47. Size2 minsize = get_theme_stylebox(SNAME("panel"))->get_minimum_size(); // Accounts for margin in the margin container
  48. minsize.x += scroll_container->get_v_scrollbar()->get_size().width * 2; // Adds a buffer so that the scrollbar does not render over the top of content
  49. float max_w = 0.0;
  50. float icon_w = 0.0;
  51. int check_w = MAX(get_theme_icon(SNAME("checked"))->get_width(), get_theme_icon(SNAME("radio_checked"))->get_width()) + hseparation;
  52. int accel_max_w = 0;
  53. bool has_check = false;
  54. for (int i = 0; i < items.size(); i++) {
  55. Size2 size;
  56. Size2 icon_size = items[i].get_icon_size();
  57. size.height = _get_item_height(i);
  58. icon_w = MAX(icon_size.width, icon_w);
  59. size.width += items[i].h_ofs;
  60. if (items[i].checkable_type) {
  61. has_check = true;
  62. }
  63. size.width += items[i].text_buf->get_size().x;
  64. size.height += vseparation;
  65. if (items[i].accel || (items[i].shortcut.is_valid() && items[i].shortcut->has_valid_event())) {
  66. int accel_w = hseparation * 2;
  67. accel_w += items[i].accel_text_buf->get_size().x;
  68. accel_max_w = MAX(accel_w, accel_max_w);
  69. }
  70. if (items[i].submenu != "") {
  71. size.width += get_theme_icon(SNAME("submenu"))->get_width();
  72. }
  73. max_w = MAX(max_w, size.width);
  74. minsize.height += size.height;
  75. }
  76. int item_side_padding = get_theme_constant(SNAME("item_start_padding")) + get_theme_constant(SNAME("item_end_padding"));
  77. minsize.width += max_w + icon_w + accel_max_w + item_side_padding;
  78. if (has_check) {
  79. minsize.width += check_w;
  80. }
  81. if (is_inside_tree()) {
  82. int height_limit = get_usable_parent_rect().size.height;
  83. if (minsize.height > height_limit) {
  84. minsize.height = height_limit;
  85. }
  86. }
  87. return minsize;
  88. }
  89. int PopupMenu::_get_item_height(int p_item) const {
  90. ERR_FAIL_INDEX_V(p_item, items.size(), 0);
  91. ERR_FAIL_COND_V(p_item < 0, 0);
  92. int icon_height = items[p_item].get_icon_size().height;
  93. if (items[p_item].checkable_type) {
  94. icon_height = MAX(icon_height, MAX(get_theme_icon(SNAME("checked"))->get_height(), get_theme_icon(SNAME("radio_checked"))->get_height()));
  95. }
  96. int text_height = items[p_item].text_buf->get_size().height;
  97. if (text_height == 0 && !items[p_item].separator) {
  98. text_height = get_theme_font(SNAME("font"))->get_height(get_theme_font_size(SNAME("font_size")));
  99. }
  100. int separator_height = 0;
  101. if (items[p_item].separator) {
  102. separator_height = MAX(get_theme_stylebox(SNAME("separator"))->get_minimum_size().height, MAX(get_theme_stylebox(SNAME("labeled_separator_left"))->get_minimum_size().height, get_theme_stylebox(SNAME("labeled_separator_right"))->get_minimum_size().height));
  103. }
  104. return MAX(separator_height, MAX(text_height, icon_height));
  105. }
  106. int PopupMenu::_get_items_total_height() const {
  107. int vsep = get_theme_constant(SNAME("vseparation"));
  108. // Get total height of all items by taking max of icon height and font height
  109. int items_total_height = 0;
  110. for (int i = 0; i < items.size(); i++) {
  111. items_total_height += _get_item_height(i) + vsep;
  112. }
  113. // Subtract a separator which is not needed for the last item.
  114. return items_total_height - vsep;
  115. }
  116. void PopupMenu::_scroll_to_item(int p_item) {
  117. ERR_FAIL_INDEX(p_item, items.size());
  118. ERR_FAIL_COND(p_item < 0);
  119. // Scroll item into view (upwards)
  120. if (items[p_item]._ofs_cache < -control->get_position().y) {
  121. int amnt_over = items[p_item]._ofs_cache + control->get_position().y;
  122. scroll_container->set_v_scroll(scroll_container->get_v_scroll() + amnt_over);
  123. }
  124. // Scroll item into view (downwards)
  125. if (items[p_item]._ofs_cache + items[p_item]._height_cache > -control->get_position().y + scroll_container->get_size().height) {
  126. int amnt_over = items[p_item]._ofs_cache + items[p_item]._height_cache + control->get_position().y - scroll_container->get_size().height;
  127. scroll_container->set_v_scroll(scroll_container->get_v_scroll() + amnt_over);
  128. }
  129. }
  130. int PopupMenu::_get_mouse_over(const Point2 &p_over) const {
  131. if (p_over.x < 0 || p_over.x >= get_size().width) {
  132. return -1;
  133. }
  134. Ref<StyleBox> style = get_theme_stylebox(SNAME("panel")); // Accounts for margin in the margin container
  135. int vseparation = get_theme_constant(SNAME("vseparation"));
  136. Point2 ofs = style->get_offset() + Point2(0, vseparation / 2);
  137. if (ofs.y > p_over.y) {
  138. return -1;
  139. }
  140. for (int i = 0; i < items.size(); i++) {
  141. ofs.y += i > 0 ? vseparation : (float)vseparation / 2;
  142. ofs.y += _get_item_height(i);
  143. if (p_over.y - control->get_position().y < ofs.y) {
  144. return i;
  145. }
  146. }
  147. return -1;
  148. }
  149. void PopupMenu::_activate_submenu(int p_over) {
  150. Node *n = get_node(items[p_over].submenu);
  151. ERR_FAIL_COND_MSG(!n, "Item subnode does not exist: " + items[p_over].submenu + ".");
  152. Popup *submenu_popup = Object::cast_to<Popup>(n);
  153. ERR_FAIL_COND_MSG(!submenu_popup, "Item subnode is not a Popup: " + items[p_over].submenu + ".");
  154. if (submenu_popup->is_visible()) {
  155. return; //already visible!
  156. }
  157. Ref<StyleBox> style = get_theme_stylebox(SNAME("panel"));
  158. int vsep = get_theme_constant(SNAME("vseparation"));
  159. Point2 this_pos = get_position();
  160. Rect2 this_rect(this_pos, get_size());
  161. float scroll_offset = control->get_position().y;
  162. Point2 submenu_pos;
  163. Size2 submenu_size = submenu_popup->get_size();
  164. if (control->is_layout_rtl()) {
  165. submenu_pos = this_pos + Point2(-submenu_size.width, items[p_over]._ofs_cache + scroll_offset);
  166. } else {
  167. submenu_pos = this_pos + Point2(this_rect.size.width, items[p_over]._ofs_cache + scroll_offset);
  168. }
  169. // Fix pos if going outside parent rect
  170. if (submenu_pos.x < get_parent_rect().position.x) {
  171. submenu_pos.x = this_pos.x + submenu_size.width;
  172. }
  173. if (submenu_pos.x + submenu_size.width > get_parent_rect().size.width) {
  174. submenu_pos.x = this_pos.x - submenu_size.width;
  175. }
  176. submenu_popup->set_close_on_parent_focus(false);
  177. submenu_popup->set_position(submenu_pos);
  178. submenu_popup->set_as_minsize(); // Shrink the popup size to its contents.
  179. submenu_popup->popup();
  180. // Set autohide areas
  181. PopupMenu *submenu_pum = Object::cast_to<PopupMenu>(submenu_popup);
  182. if (submenu_pum) {
  183. submenu_pum->take_mouse_focus();
  184. // Make the position of the parent popup relative to submenu popup
  185. this_rect.position = this_rect.position - submenu_pum->get_position();
  186. // Autohide area above the submenu item
  187. submenu_pum->clear_autohide_areas();
  188. submenu_pum->add_autohide_area(Rect2(this_rect.position.x, this_rect.position.y, this_rect.size.x, items[p_over]._ofs_cache + scroll_offset + style->get_offset().height - vsep / 2));
  189. // If there is an area below the submenu item, add an autohide area there.
  190. if (items[p_over]._ofs_cache + items[p_over]._height_cache + scroll_offset <= control->get_size().height) {
  191. int from = items[p_over]._ofs_cache + items[p_over]._height_cache + scroll_offset + vsep / 2 + style->get_offset().height;
  192. submenu_pum->add_autohide_area(Rect2(this_rect.position.x, this_rect.position.y + from, this_rect.size.x, this_rect.size.y - from));
  193. }
  194. }
  195. }
  196. void PopupMenu::_submenu_timeout() {
  197. if (mouse_over == submenu_over) {
  198. _activate_submenu(mouse_over);
  199. }
  200. submenu_over = -1;
  201. }
  202. void PopupMenu::gui_input(const Ref<InputEvent> &p_event) {
  203. ERR_FAIL_COND(p_event.is_null());
  204. if (p_event->is_action("ui_down") && p_event->is_pressed()) {
  205. int search_from = mouse_over + 1;
  206. if (search_from >= items.size()) {
  207. search_from = 0;
  208. }
  209. bool match_found = false;
  210. for (int i = search_from; i < items.size(); i++) {
  211. if (!items[i].separator && !items[i].disabled) {
  212. mouse_over = i;
  213. emit_signal(SNAME("id_focused"), i);
  214. _scroll_to_item(i);
  215. control->update();
  216. set_input_as_handled();
  217. match_found = true;
  218. break;
  219. }
  220. }
  221. if (!match_found) {
  222. // If the last item is not selectable, try re-searching from the start.
  223. for (int i = 0; i < search_from; i++) {
  224. if (!items[i].separator && !items[i].disabled) {
  225. mouse_over = i;
  226. emit_signal(SNAME("id_focused"), i);
  227. _scroll_to_item(i);
  228. control->update();
  229. set_input_as_handled();
  230. break;
  231. }
  232. }
  233. }
  234. } else if (p_event->is_action("ui_up") && p_event->is_pressed()) {
  235. int search_from = mouse_over - 1;
  236. if (search_from < 0) {
  237. search_from = items.size() - 1;
  238. }
  239. bool match_found = false;
  240. for (int i = search_from; i >= 0; i--) {
  241. if (!items[i].separator && !items[i].disabled) {
  242. mouse_over = i;
  243. emit_signal(SNAME("id_focused"), i);
  244. _scroll_to_item(i);
  245. control->update();
  246. set_input_as_handled();
  247. match_found = true;
  248. break;
  249. }
  250. }
  251. if (!match_found) {
  252. // If the first item is not selectable, try re-searching from the end.
  253. for (int i = items.size() - 1; i >= search_from; i--) {
  254. if (!items[i].separator && !items[i].disabled) {
  255. mouse_over = i;
  256. emit_signal(SNAME("id_focused"), i);
  257. _scroll_to_item(i);
  258. control->update();
  259. set_input_as_handled();
  260. break;
  261. }
  262. }
  263. }
  264. } else if (p_event->is_action("ui_left") && p_event->is_pressed()) {
  265. Node *n = get_parent();
  266. if (n && Object::cast_to<PopupMenu>(n)) {
  267. hide();
  268. set_input_as_handled();
  269. }
  270. } else if (p_event->is_action("ui_right") && p_event->is_pressed()) {
  271. if (mouse_over >= 0 && mouse_over < items.size() && !items[mouse_over].separator && items[mouse_over].submenu != "" && submenu_over != mouse_over) {
  272. _activate_submenu(mouse_over);
  273. set_input_as_handled();
  274. }
  275. } else if (p_event->is_action("ui_accept") && p_event->is_pressed()) {
  276. if (mouse_over >= 0 && mouse_over < items.size() && !items[mouse_over].separator) {
  277. if (items[mouse_over].submenu != "" && submenu_over != mouse_over) {
  278. _activate_submenu(mouse_over);
  279. } else {
  280. activate_item(mouse_over);
  281. }
  282. set_input_as_handled();
  283. }
  284. }
  285. // Make an area which does not include v scrollbar, so that items are not activated when dragging scrollbar.
  286. Rect2 item_clickable_area = scroll_container->get_rect();
  287. if (scroll_container->get_v_scrollbar()->is_visible_in_tree()) {
  288. if (is_layout_rtl()) {
  289. item_clickable_area.position.x += scroll_container->get_v_scrollbar()->get_size().width;
  290. } else {
  291. item_clickable_area.size.width -= scroll_container->get_v_scrollbar()->get_size().width;
  292. }
  293. }
  294. Ref<InputEventMouseButton> b = p_event;
  295. if (b.is_valid()) {
  296. if (!item_clickable_area.has_point(b->get_position())) {
  297. return;
  298. }
  299. int button_idx = b->get_button_index();
  300. if (!b->is_pressed()) {
  301. // Activate the item on release of either the left mouse button or
  302. // any mouse button held down when the popup was opened.
  303. // This allows for opening the popup and triggering an action in a single mouse click.
  304. if (button_idx == MOUSE_BUTTON_LEFT || (initial_button_mask & (1 << (button_idx - 1)))) {
  305. bool was_during_grabbed_click = during_grabbed_click;
  306. during_grabbed_click = false;
  307. initial_button_mask = 0;
  308. // Disable clicks under a time threshold to avoid selection right when opening the popup.
  309. uint64_t now = OS::get_singleton()->get_ticks_msec();
  310. uint64_t diff = now - popup_time_msec;
  311. if (diff < 100) {
  312. return;
  313. }
  314. int over = _get_mouse_over(b->get_position());
  315. if (over < 0) {
  316. if (!was_during_grabbed_click) {
  317. hide();
  318. }
  319. return;
  320. }
  321. if (items[over].separator || items[over].disabled) {
  322. return;
  323. }
  324. if (items[over].submenu != "") {
  325. _activate_submenu(over);
  326. return;
  327. }
  328. activate_item(over);
  329. }
  330. }
  331. }
  332. Ref<InputEventMouseMotion> m = p_event;
  333. if (m.is_valid()) {
  334. for (const Rect2 &E : autohide_areas) {
  335. if (!Rect2(Point2(), get_size()).has_point(m->get_position()) && E.has_point(m->get_position())) {
  336. _close_pressed();
  337. return;
  338. }
  339. }
  340. if (!item_clickable_area.has_point(m->get_position())) {
  341. return;
  342. }
  343. int over = _get_mouse_over(m->get_position());
  344. int id = (over < 0 || items[over].separator || items[over].disabled) ? -1 : (items[over].id >= 0 ? items[over].id : over);
  345. if (id < 0) {
  346. mouse_over = -1;
  347. control->update();
  348. return;
  349. }
  350. if (items[over].submenu != "" && submenu_over != over) {
  351. submenu_over = over;
  352. submenu_timer->start();
  353. }
  354. if (over != mouse_over) {
  355. mouse_over = over;
  356. control->update();
  357. }
  358. }
  359. Ref<InputEventKey> k = p_event;
  360. if (allow_search && k.is_valid() && k->get_unicode() && k->is_pressed()) {
  361. uint64_t now = OS::get_singleton()->get_ticks_msec();
  362. uint64_t diff = now - search_time_msec;
  363. uint64_t max_interval = uint64_t(GLOBAL_DEF("gui/timers/incremental_search_max_interval_msec", 2000));
  364. search_time_msec = now;
  365. if (diff > max_interval) {
  366. search_string = "";
  367. }
  368. if (String::chr(k->get_unicode()) != search_string) {
  369. search_string += String::chr(k->get_unicode());
  370. }
  371. for (int i = mouse_over + 1; i <= items.size(); i++) {
  372. if (i == items.size()) {
  373. if (mouse_over <= 0) {
  374. break;
  375. } else {
  376. i = 0;
  377. }
  378. }
  379. if (i == mouse_over) {
  380. break;
  381. }
  382. if (items[i].text.findn(search_string) == 0) {
  383. mouse_over = i;
  384. emit_signal(SNAME("id_focused"), i);
  385. _scroll_to_item(i);
  386. control->update();
  387. set_input_as_handled();
  388. break;
  389. }
  390. }
  391. }
  392. }
  393. void PopupMenu::_draw_items() {
  394. control->set_custom_minimum_size(Size2(0, _get_items_total_height()));
  395. RID ci = control->get_canvas_item();
  396. Size2 margin_size;
  397. margin_size.width = margin_container->get_theme_constant(SNAME("margin_right")) + margin_container->get_theme_constant(SNAME("margin_left"));
  398. margin_size.height = margin_container->get_theme_constant(SNAME("margin_top")) + margin_container->get_theme_constant(SNAME("margin_bottom"));
  399. // Space between the item content and the sides of popup menu.
  400. int item_start_padding = get_theme_constant(SNAME("item_start_padding"));
  401. int item_end_padding = get_theme_constant(SNAME("item_end_padding"));
  402. bool rtl = control->is_layout_rtl();
  403. Ref<StyleBox> style = get_theme_stylebox(SNAME("panel"));
  404. Ref<StyleBox> hover = get_theme_stylebox(SNAME("hover"));
  405. // In Item::checkable_type enum order (less the non-checkable member)
  406. Ref<Texture2D> check[] = { get_theme_icon(SNAME("checked")), get_theme_icon(SNAME("radio_checked")) };
  407. Ref<Texture2D> uncheck[] = { get_theme_icon(SNAME("unchecked")), get_theme_icon(SNAME("radio_unchecked")) };
  408. Ref<Texture2D> submenu;
  409. if (rtl) {
  410. submenu = get_theme_icon(SNAME("submenu_mirrored"));
  411. } else {
  412. submenu = get_theme_icon(SNAME("submenu"));
  413. }
  414. Ref<StyleBox> separator = get_theme_stylebox(SNAME("separator"));
  415. Ref<StyleBox> labeled_separator_left = get_theme_stylebox(SNAME("labeled_separator_left"));
  416. Ref<StyleBox> labeled_separator_right = get_theme_stylebox(SNAME("labeled_separator_right"));
  417. int vseparation = get_theme_constant(SNAME("vseparation"));
  418. int hseparation = get_theme_constant(SNAME("hseparation"));
  419. Color font_color = get_theme_color(SNAME("font_color"));
  420. Color font_disabled_color = get_theme_color(SNAME("font_disabled_color"));
  421. Color font_accelerator_color = get_theme_color(SNAME("font_accelerator_color"));
  422. Color font_hover_color = get_theme_color(SNAME("font_hover_color"));
  423. Color font_separator_color = get_theme_color(SNAME("font_separator_color"));
  424. float scroll_width = scroll_container->get_v_scrollbar()->is_visible_in_tree() ? scroll_container->get_v_scrollbar()->get_size().width : 0;
  425. float display_width = control->get_size().width - scroll_width;
  426. // Find the widest icon and whether any items have a checkbox, and store the offsets for each.
  427. float icon_ofs = 0.0;
  428. bool has_check = false;
  429. for (int i = 0; i < items.size(); i++) {
  430. icon_ofs = MAX(items[i].get_icon_size().width, icon_ofs);
  431. if (items[i].checkable_type) {
  432. has_check = true;
  433. }
  434. }
  435. if (icon_ofs > 0.0) {
  436. icon_ofs += hseparation;
  437. }
  438. float check_ofs = 0.0;
  439. if (has_check) {
  440. check_ofs = MAX(get_theme_icon(SNAME("checked"))->get_width(), get_theme_icon(SNAME("radio_checked"))->get_width()) + hseparation;
  441. }
  442. Point2 ofs = Point2();
  443. // Loop through all items and draw each.
  444. for (int i = 0; i < items.size(); i++) {
  445. // For the first item only add half a separation. For all other items, add a whole separation to the offset.
  446. ofs.y += i > 0 ? vseparation : (float)vseparation / 2;
  447. _shape_item(i);
  448. Point2 item_ofs = ofs;
  449. Size2 icon_size = items[i].get_icon_size();
  450. float h = _get_item_height(i);
  451. if (i == mouse_over) {
  452. if (rtl) {
  453. hover->draw(ci, Rect2(item_ofs + Point2(scroll_width, -vseparation / 2), Size2(display_width, h + vseparation)));
  454. } else {
  455. hover->draw(ci, Rect2(item_ofs + Point2(0, -vseparation / 2), Size2(display_width, h + vseparation)));
  456. }
  457. }
  458. String text = items[i].xl_text;
  459. // Separator
  460. item_ofs.x += items[i].h_ofs;
  461. if (items[i].separator) {
  462. int sep_h = separator->get_center_size().height + separator->get_minimum_size().height;
  463. int sep_ofs = Math::floor((h - sep_h) / 2.0);
  464. if (text != String()) {
  465. int text_size = items[i].text_buf->get_size().width;
  466. int text_center = display_width / 2;
  467. int text_left = text_center - text_size / 2;
  468. int text_right = text_center + text_size / 2;
  469. if (text_left > item_ofs.x) {
  470. labeled_separator_left->draw(ci, Rect2(item_ofs + Point2(0, sep_ofs), Size2(MAX(0, text_left - item_ofs.x), sep_h)));
  471. }
  472. if (text_right < display_width) {
  473. labeled_separator_right->draw(ci, Rect2(Point2(text_right, item_ofs.y + sep_ofs), Size2(MAX(0, display_width - text_right), sep_h)));
  474. }
  475. } else {
  476. separator->draw(ci, Rect2(item_ofs + Point2(0, sep_ofs), Size2(display_width, sep_h)));
  477. }
  478. }
  479. Color icon_color(1, 1, 1, items[i].disabled ? 0.5 : 1);
  480. // For non-separator items, add some padding for the content.
  481. item_ofs.x += item_start_padding;
  482. // Checkboxes
  483. if (items[i].checkable_type) {
  484. Texture2D *icon = (items[i].checked ? check[items[i].checkable_type - 1] : uncheck[items[i].checkable_type - 1]).ptr();
  485. if (rtl) {
  486. icon->draw(ci, Size2(control->get_size().width - item_ofs.x - icon->get_width(), item_ofs.y) + Point2(0, Math::floor((h - icon->get_height()) / 2.0)), icon_color);
  487. } else {
  488. icon->draw(ci, item_ofs + Point2(0, Math::floor((h - icon->get_height()) / 2.0)), icon_color);
  489. }
  490. }
  491. // Icon
  492. if (!items[i].icon.is_null()) {
  493. if (rtl) {
  494. items[i].icon->draw(ci, Size2(control->get_size().width - item_ofs.x - check_ofs - icon_size.width, item_ofs.y) + Point2(0, Math::floor((h - icon_size.height) / 2.0)), icon_color);
  495. } else {
  496. items[i].icon->draw(ci, item_ofs + Size2(check_ofs, 0) + Point2(0, Math::floor((h - icon_size.height) / 2.0)), icon_color);
  497. }
  498. }
  499. // Submenu arrow on right hand side
  500. if (items[i].submenu != "") {
  501. if (rtl) {
  502. submenu->draw(ci, Point2(scroll_width + style->get_margin(SIDE_LEFT) + item_end_padding, item_ofs.y + Math::floor(h - submenu->get_height()) / 2), icon_color);
  503. } else {
  504. submenu->draw(ci, Point2(display_width - style->get_margin(SIDE_RIGHT) - submenu->get_width() - item_end_padding, item_ofs.y + Math::floor(h - submenu->get_height()) / 2), icon_color);
  505. }
  506. }
  507. // Text
  508. Color font_outline_color = get_theme_color(SNAME("font_outline_color"));
  509. int outline_size = get_theme_constant(SNAME("outline_size"));
  510. if (items[i].separator) {
  511. if (text != String()) {
  512. int center = (display_width - items[i].text_buf->get_size().width) / 2;
  513. Vector2 text_pos = Point2(center, item_ofs.y + Math::floor((h - items[i].text_buf->get_size().y) / 2.0));
  514. if (outline_size > 0 && font_outline_color.a > 0) {
  515. items[i].text_buf->draw_outline(ci, text_pos, outline_size, font_outline_color);
  516. }
  517. items[i].text_buf->draw(ci, text_pos, font_separator_color);
  518. }
  519. } else {
  520. item_ofs.x += icon_ofs + check_ofs;
  521. if (rtl) {
  522. Vector2 text_pos = Size2(control->get_size().width - items[i].text_buf->get_size().width - item_ofs.x, item_ofs.y) + Point2(0, Math::floor((h - items[i].text_buf->get_size().y) / 2.0));
  523. if (outline_size > 0 && font_outline_color.a > 0) {
  524. items[i].text_buf->draw_outline(ci, text_pos, outline_size, font_outline_color);
  525. }
  526. items[i].text_buf->draw(ci, text_pos, items[i].disabled ? font_disabled_color : (i == mouse_over ? font_hover_color : font_color));
  527. } else {
  528. Vector2 text_pos = item_ofs + Point2(0, Math::floor((h - items[i].text_buf->get_size().y) / 2.0));
  529. if (outline_size > 0 && font_outline_color.a > 0) {
  530. items[i].text_buf->draw_outline(ci, text_pos, outline_size, font_outline_color);
  531. }
  532. items[i].text_buf->draw(ci, text_pos, items[i].disabled ? font_disabled_color : (i == mouse_over ? font_hover_color : font_color));
  533. }
  534. }
  535. // Accelerator / Shortcut
  536. if (items[i].accel || (items[i].shortcut.is_valid() && items[i].shortcut->has_valid_event())) {
  537. if (rtl) {
  538. item_ofs.x = scroll_width + style->get_margin(SIDE_LEFT) + item_end_padding;
  539. } else {
  540. item_ofs.x = display_width - style->get_margin(SIDE_RIGHT) - items[i].accel_text_buf->get_size().x - item_end_padding;
  541. }
  542. Vector2 text_pos = item_ofs + Point2(0, Math::floor((h - items[i].text_buf->get_size().y) / 2.0));
  543. if (outline_size > 0 && font_outline_color.a > 0) {
  544. items[i].accel_text_buf->draw_outline(ci, text_pos, outline_size, font_outline_color);
  545. }
  546. items[i].accel_text_buf->draw(ci, text_pos, i == mouse_over ? font_hover_color : font_accelerator_color);
  547. }
  548. // Cache the item vertical offset from the first item and the height
  549. items.write[i]._ofs_cache = ofs.y;
  550. items.write[i]._height_cache = h;
  551. ofs.y += h;
  552. }
  553. }
  554. void PopupMenu::_draw_background() {
  555. Ref<StyleBox> style = get_theme_stylebox(SNAME("panel"));
  556. RID ci2 = margin_container->get_canvas_item();
  557. style->draw(ci2, Rect2(Point2(), margin_container->get_size()));
  558. }
  559. void PopupMenu::_minimum_lifetime_timeout() {
  560. close_allowed = true;
  561. // If the mouse still isn't in this popup after timer expires, close.
  562. if (!get_visible_rect().has_point(get_mouse_position())) {
  563. _close_pressed();
  564. }
  565. }
  566. void PopupMenu::_close_pressed() {
  567. // Only apply minimum lifetime to submenus.
  568. PopupMenu *parent_pum = Object::cast_to<PopupMenu>(get_parent());
  569. if (!parent_pum) {
  570. Popup::_close_pressed();
  571. return;
  572. }
  573. // If the timer has expired, close. If timer is still running, do nothing.
  574. if (close_allowed) {
  575. close_allowed = false;
  576. Popup::_close_pressed();
  577. } else if (minimum_lifetime_timer->is_stopped()) {
  578. minimum_lifetime_timer->start();
  579. }
  580. }
  581. void PopupMenu::_shape_item(int p_item) {
  582. if (items.write[p_item].dirty) {
  583. items.write[p_item].text_buf->clear();
  584. Ref<Font> font = get_theme_font(SNAME("font"));
  585. int font_size = get_theme_font_size(SNAME("font_size"));
  586. if (items[p_item].text_direction == Control::TEXT_DIRECTION_INHERITED) {
  587. items.write[p_item].text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  588. } else {
  589. items.write[p_item].text_buf->set_direction((TextServer::Direction)items[p_item].text_direction);
  590. }
  591. items.write[p_item].text_buf->add_string(items.write[p_item].xl_text, font, font_size, items[p_item].opentype_features, (items[p_item].language != "") ? items[p_item].language : TranslationServer::get_singleton()->get_tool_locale());
  592. items.write[p_item].accel_text_buf->clear();
  593. items.write[p_item].accel_text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  594. items.write[p_item].accel_text_buf->add_string(_get_accel_text(items.write[p_item]), font, font_size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale());
  595. items.write[p_item].dirty = false;
  596. }
  597. }
  598. void PopupMenu::_notification(int p_what) {
  599. switch (p_what) {
  600. case NOTIFICATION_ENTER_TREE: {
  601. PopupMenu *pm = Object::cast_to<PopupMenu>(get_parent());
  602. if (pm) {
  603. // Inherit submenu's popup delay time from parent menu
  604. float pm_delay = pm->get_submenu_popup_delay();
  605. set_submenu_popup_delay(pm_delay);
  606. }
  607. } break;
  608. case NOTIFICATION_THEME_CHANGED:
  609. case Control::NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  610. case NOTIFICATION_TRANSLATION_CHANGED: {
  611. for (int i = 0; i < items.size(); i++) {
  612. items.write[i].xl_text = atr(items[i].text);
  613. items.write[i].dirty = true;
  614. _shape_item(i);
  615. }
  616. child_controls_changed();
  617. control->update();
  618. } break;
  619. case NOTIFICATION_WM_MOUSE_ENTER: {
  620. grab_focus();
  621. } break;
  622. case NOTIFICATION_WM_MOUSE_EXIT: {
  623. if (mouse_over >= 0 && (items[mouse_over].submenu == "" || submenu_over != -1)) {
  624. mouse_over = -1;
  625. control->update();
  626. }
  627. } break;
  628. case NOTIFICATION_POST_POPUP: {
  629. initial_button_mask = Input::get_singleton()->get_mouse_button_mask();
  630. during_grabbed_click = (bool)initial_button_mask;
  631. } break;
  632. case NOTIFICATION_WM_SIZE_CHANGED: {
  633. } break;
  634. case NOTIFICATION_INTERNAL_PROCESS: {
  635. //only used when using operating system windows
  636. if (!is_embedded() && autohide_areas.size()) {
  637. Point2 mouse_pos = DisplayServer::get_singleton()->mouse_get_position();
  638. mouse_pos -= get_position();
  639. for (const Rect2 &E : autohide_areas) {
  640. if (!Rect2(Point2(), get_size()).has_point(mouse_pos) && E.has_point(mouse_pos)) {
  641. _close_pressed();
  642. return;
  643. }
  644. }
  645. }
  646. } break;
  647. case NOTIFICATION_VISIBILITY_CHANGED: {
  648. if (!is_visible()) {
  649. if (mouse_over >= 0) {
  650. mouse_over = -1;
  651. control->update();
  652. }
  653. for (int i = 0; i < items.size(); i++) {
  654. if (items[i].submenu == "") {
  655. continue;
  656. }
  657. Node *n = get_node(items[i].submenu);
  658. if (!n) {
  659. continue;
  660. }
  661. PopupMenu *pm = Object::cast_to<PopupMenu>(n);
  662. if (!pm || !pm->is_visible()) {
  663. continue;
  664. }
  665. pm->hide();
  666. }
  667. set_process_internal(false);
  668. } else {
  669. if (!is_embedded()) {
  670. set_process_internal(true);
  671. }
  672. // Set margin on the margin container
  673. Ref<StyleBox> panel_style = get_theme_stylebox(SNAME("panel"));
  674. margin_container->add_theme_constant_override("margin_top", panel_style->get_margin(Side::SIDE_TOP));
  675. margin_container->add_theme_constant_override("margin_bottom", panel_style->get_margin(Side::SIDE_BOTTOM));
  676. margin_container->add_theme_constant_override("margin_left", panel_style->get_margin(Side::SIDE_LEFT));
  677. margin_container->add_theme_constant_override("margin_right", panel_style->get_margin(Side::SIDE_RIGHT));
  678. }
  679. } break;
  680. }
  681. }
  682. /* Methods to add items with or without icon, checkbox, shortcut.
  683. * Be sure to keep them in sync when adding new properties in the Item struct.
  684. */
  685. #define ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel) \
  686. item.text = p_label; \
  687. item.xl_text = atr(p_label); \
  688. item.id = p_id == -1 ? items.size() : p_id; \
  689. item.accel = p_accel;
  690. void PopupMenu::add_item(const String &p_label, int p_id, uint32_t p_accel) {
  691. Item item;
  692. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  693. items.push_back(item);
  694. _shape_item(items.size() - 1);
  695. control->update();
  696. child_controls_changed();
  697. notify_property_list_changed();
  698. }
  699. void PopupMenu::add_icon_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id, uint32_t p_accel) {
  700. Item item;
  701. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  702. item.icon = p_icon;
  703. items.push_back(item);
  704. _shape_item(items.size() - 1);
  705. control->update();
  706. child_controls_changed();
  707. notify_property_list_changed();
  708. }
  709. void PopupMenu::add_check_item(const String &p_label, int p_id, uint32_t p_accel) {
  710. Item item;
  711. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  712. item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX;
  713. items.push_back(item);
  714. _shape_item(items.size() - 1);
  715. control->update();
  716. child_controls_changed();
  717. }
  718. void PopupMenu::add_icon_check_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id, uint32_t p_accel) {
  719. Item item;
  720. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  721. item.icon = p_icon;
  722. item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX;
  723. items.push_back(item);
  724. _shape_item(items.size() - 1);
  725. control->update();
  726. child_controls_changed();
  727. }
  728. void PopupMenu::add_radio_check_item(const String &p_label, int p_id, uint32_t p_accel) {
  729. Item item;
  730. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  731. item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON;
  732. items.push_back(item);
  733. _shape_item(items.size() - 1);
  734. control->update();
  735. child_controls_changed();
  736. }
  737. void PopupMenu::add_icon_radio_check_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id, uint32_t p_accel) {
  738. Item item;
  739. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  740. item.icon = p_icon;
  741. item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON;
  742. items.push_back(item);
  743. _shape_item(items.size() - 1);
  744. control->update();
  745. child_controls_changed();
  746. }
  747. void PopupMenu::add_multistate_item(const String &p_label, int p_max_states, int p_default_state, int p_id, uint32_t p_accel) {
  748. Item item;
  749. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  750. item.max_states = p_max_states;
  751. item.state = p_default_state;
  752. items.push_back(item);
  753. _shape_item(items.size() - 1);
  754. control->update();
  755. child_controls_changed();
  756. }
  757. #define ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global) \
  758. ERR_FAIL_COND_MSG(p_shortcut.is_null(), "Cannot add item with invalid Shortcut."); \
  759. _ref_shortcut(p_shortcut); \
  760. item.text = p_shortcut->get_name(); \
  761. item.xl_text = atr(item.text); \
  762. item.id = p_id == -1 ? items.size() : p_id; \
  763. item.shortcut = p_shortcut; \
  764. item.shortcut_is_global = p_global;
  765. void PopupMenu::add_shortcut(const Ref<Shortcut> &p_shortcut, int p_id, bool p_global) {
  766. Item item;
  767. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global);
  768. items.push_back(item);
  769. _shape_item(items.size() - 1);
  770. control->update();
  771. child_controls_changed();
  772. }
  773. void PopupMenu::add_icon_shortcut(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id, bool p_global) {
  774. Item item;
  775. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global);
  776. item.icon = p_icon;
  777. items.push_back(item);
  778. _shape_item(items.size() - 1);
  779. control->update();
  780. child_controls_changed();
  781. }
  782. void PopupMenu::add_check_shortcut(const Ref<Shortcut> &p_shortcut, int p_id, bool p_global) {
  783. Item item;
  784. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global);
  785. item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX;
  786. items.push_back(item);
  787. _shape_item(items.size() - 1);
  788. control->update();
  789. child_controls_changed();
  790. }
  791. void PopupMenu::add_icon_check_shortcut(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id, bool p_global) {
  792. Item item;
  793. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global);
  794. item.icon = p_icon;
  795. item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX;
  796. items.push_back(item);
  797. _shape_item(items.size() - 1);
  798. control->update();
  799. child_controls_changed();
  800. }
  801. void PopupMenu::add_radio_check_shortcut(const Ref<Shortcut> &p_shortcut, int p_id, bool p_global) {
  802. Item item;
  803. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global);
  804. item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON;
  805. items.push_back(item);
  806. _shape_item(items.size() - 1);
  807. control->update();
  808. child_controls_changed();
  809. }
  810. void PopupMenu::add_icon_radio_check_shortcut(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id, bool p_global) {
  811. Item item;
  812. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global);
  813. item.icon = p_icon;
  814. item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON;
  815. items.push_back(item);
  816. _shape_item(items.size() - 1);
  817. control->update();
  818. child_controls_changed();
  819. }
  820. void PopupMenu::add_submenu_item(const String &p_label, const String &p_submenu, int p_id) {
  821. Item item;
  822. item.text = p_label;
  823. item.xl_text = atr(p_label);
  824. item.id = p_id == -1 ? items.size() : p_id;
  825. item.submenu = p_submenu;
  826. items.push_back(item);
  827. _shape_item(items.size() - 1);
  828. control->update();
  829. child_controls_changed();
  830. }
  831. #undef ITEM_SETUP_WITH_ACCEL
  832. #undef ITEM_SETUP_WITH_SHORTCUT
  833. /* Methods to modify existing items. */
  834. void PopupMenu::set_item_text(int p_idx, const String &p_text) {
  835. ERR_FAIL_INDEX(p_idx, items.size());
  836. items.write[p_idx].text = p_text;
  837. items.write[p_idx].xl_text = atr(p_text);
  838. _shape_item(p_idx);
  839. control->update();
  840. child_controls_changed();
  841. }
  842. void PopupMenu::set_item_text_direction(int p_item, Control::TextDirection p_text_direction) {
  843. ERR_FAIL_INDEX(p_item, items.size());
  844. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  845. if (items[p_item].text_direction != p_text_direction) {
  846. items.write[p_item].text_direction = p_text_direction;
  847. items.write[p_item].dirty = true;
  848. control->update();
  849. }
  850. }
  851. void PopupMenu::clear_item_opentype_features(int p_item) {
  852. ERR_FAIL_INDEX(p_item, items.size());
  853. items.write[p_item].opentype_features.clear();
  854. items.write[p_item].dirty = true;
  855. control->update();
  856. }
  857. void PopupMenu::set_item_opentype_feature(int p_item, const String &p_name, int p_value) {
  858. ERR_FAIL_INDEX(p_item, items.size());
  859. int32_t tag = TS->name_to_tag(p_name);
  860. if (!items[p_item].opentype_features.has(tag) || (int)items[p_item].opentype_features[tag] != p_value) {
  861. items.write[p_item].opentype_features[tag] = p_value;
  862. items.write[p_item].dirty = true;
  863. control->update();
  864. }
  865. }
  866. void PopupMenu::set_item_language(int p_item, const String &p_language) {
  867. ERR_FAIL_INDEX(p_item, items.size());
  868. if (items[p_item].language != p_language) {
  869. items.write[p_item].language = p_language;
  870. items.write[p_item].dirty = true;
  871. control->update();
  872. }
  873. }
  874. void PopupMenu::set_item_icon(int p_idx, const Ref<Texture2D> &p_icon) {
  875. ERR_FAIL_INDEX(p_idx, items.size());
  876. items.write[p_idx].icon = p_icon;
  877. control->update();
  878. child_controls_changed();
  879. }
  880. void PopupMenu::set_item_checked(int p_idx, bool p_checked) {
  881. ERR_FAIL_INDEX(p_idx, items.size());
  882. items.write[p_idx].checked = p_checked;
  883. control->update();
  884. child_controls_changed();
  885. }
  886. void PopupMenu::set_item_id(int p_idx, int p_id) {
  887. ERR_FAIL_INDEX(p_idx, items.size());
  888. items.write[p_idx].id = p_id;
  889. control->update();
  890. child_controls_changed();
  891. }
  892. void PopupMenu::set_item_accelerator(int p_idx, uint32_t p_accel) {
  893. ERR_FAIL_INDEX(p_idx, items.size());
  894. items.write[p_idx].accel = p_accel;
  895. items.write[p_idx].dirty = true;
  896. control->update();
  897. child_controls_changed();
  898. }
  899. void PopupMenu::set_item_metadata(int p_idx, const Variant &p_meta) {
  900. ERR_FAIL_INDEX(p_idx, items.size());
  901. items.write[p_idx].metadata = p_meta;
  902. control->update();
  903. child_controls_changed();
  904. }
  905. void PopupMenu::set_item_disabled(int p_idx, bool p_disabled) {
  906. ERR_FAIL_INDEX(p_idx, items.size());
  907. items.write[p_idx].disabled = p_disabled;
  908. control->update();
  909. child_controls_changed();
  910. }
  911. void PopupMenu::set_item_submenu(int p_idx, const String &p_submenu) {
  912. ERR_FAIL_INDEX(p_idx, items.size());
  913. items.write[p_idx].submenu = p_submenu;
  914. control->update();
  915. child_controls_changed();
  916. }
  917. void PopupMenu::toggle_item_checked(int p_idx) {
  918. ERR_FAIL_INDEX(p_idx, items.size());
  919. items.write[p_idx].checked = !items[p_idx].checked;
  920. control->update();
  921. child_controls_changed();
  922. }
  923. String PopupMenu::get_item_text(int p_idx) const {
  924. ERR_FAIL_INDEX_V(p_idx, items.size(), "");
  925. return items[p_idx].text;
  926. }
  927. Control::TextDirection PopupMenu::get_item_text_direction(int p_item) const {
  928. ERR_FAIL_INDEX_V(p_item, items.size(), Control::TEXT_DIRECTION_INHERITED);
  929. return items[p_item].text_direction;
  930. }
  931. int PopupMenu::get_item_opentype_feature(int p_item, const String &p_name) const {
  932. ERR_FAIL_INDEX_V(p_item, items.size(), -1);
  933. int32_t tag = TS->name_to_tag(p_name);
  934. if (!items[p_item].opentype_features.has(tag)) {
  935. return -1;
  936. }
  937. return items[p_item].opentype_features[tag];
  938. }
  939. String PopupMenu::get_item_language(int p_item) const {
  940. ERR_FAIL_INDEX_V(p_item, items.size(), "");
  941. return items[p_item].language;
  942. }
  943. int PopupMenu::get_item_idx_from_text(const String &text) const {
  944. for (int idx = 0; idx < items.size(); idx++) {
  945. if (items[idx].text == text) {
  946. return idx;
  947. }
  948. }
  949. return -1;
  950. }
  951. Ref<Texture2D> PopupMenu::get_item_icon(int p_idx) const {
  952. ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<Texture2D>());
  953. return items[p_idx].icon;
  954. }
  955. uint32_t PopupMenu::get_item_accelerator(int p_idx) const {
  956. ERR_FAIL_INDEX_V(p_idx, items.size(), 0);
  957. return items[p_idx].accel;
  958. }
  959. Variant PopupMenu::get_item_metadata(int p_idx) const {
  960. ERR_FAIL_INDEX_V(p_idx, items.size(), Variant());
  961. return items[p_idx].metadata;
  962. }
  963. bool PopupMenu::is_item_disabled(int p_idx) const {
  964. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  965. return items[p_idx].disabled;
  966. }
  967. bool PopupMenu::is_item_checked(int p_idx) const {
  968. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  969. return items[p_idx].checked;
  970. }
  971. int PopupMenu::get_item_id(int p_idx) const {
  972. ERR_FAIL_INDEX_V(p_idx, items.size(), 0);
  973. return items[p_idx].id;
  974. }
  975. int PopupMenu::get_item_index(int p_id) const {
  976. for (int i = 0; i < items.size(); i++) {
  977. if (items[i].id == p_id) {
  978. return i;
  979. }
  980. }
  981. return -1;
  982. }
  983. String PopupMenu::get_item_submenu(int p_idx) const {
  984. ERR_FAIL_INDEX_V(p_idx, items.size(), "");
  985. return items[p_idx].submenu;
  986. }
  987. String PopupMenu::get_item_tooltip(int p_idx) const {
  988. ERR_FAIL_INDEX_V(p_idx, items.size(), "");
  989. return items[p_idx].tooltip;
  990. }
  991. Ref<Shortcut> PopupMenu::get_item_shortcut(int p_idx) const {
  992. ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<Shortcut>());
  993. return items[p_idx].shortcut;
  994. }
  995. int PopupMenu::get_item_state(int p_idx) const {
  996. ERR_FAIL_INDEX_V(p_idx, items.size(), -1);
  997. return items[p_idx].state;
  998. }
  999. void PopupMenu::set_item_as_separator(int p_idx, bool p_separator) {
  1000. ERR_FAIL_INDEX(p_idx, items.size());
  1001. items.write[p_idx].separator = p_separator;
  1002. control->update();
  1003. }
  1004. bool PopupMenu::is_item_separator(int p_idx) const {
  1005. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  1006. return items[p_idx].separator;
  1007. }
  1008. void PopupMenu::set_item_as_checkable(int p_idx, bool p_checkable) {
  1009. ERR_FAIL_INDEX(p_idx, items.size());
  1010. items.write[p_idx].checkable_type = p_checkable ? Item::CHECKABLE_TYPE_CHECK_BOX : Item::CHECKABLE_TYPE_NONE;
  1011. control->update();
  1012. }
  1013. void PopupMenu::set_item_as_radio_checkable(int p_idx, bool p_radio_checkable) {
  1014. ERR_FAIL_INDEX(p_idx, items.size());
  1015. items.write[p_idx].checkable_type = p_radio_checkable ? Item::CHECKABLE_TYPE_RADIO_BUTTON : Item::CHECKABLE_TYPE_NONE;
  1016. control->update();
  1017. }
  1018. void PopupMenu::set_item_tooltip(int p_idx, const String &p_tooltip) {
  1019. ERR_FAIL_INDEX(p_idx, items.size());
  1020. items.write[p_idx].tooltip = p_tooltip;
  1021. control->update();
  1022. }
  1023. void PopupMenu::set_item_shortcut(int p_idx, const Ref<Shortcut> &p_shortcut, bool p_global) {
  1024. ERR_FAIL_INDEX(p_idx, items.size());
  1025. if (items[p_idx].shortcut.is_valid()) {
  1026. _unref_shortcut(items[p_idx].shortcut);
  1027. }
  1028. items.write[p_idx].shortcut = p_shortcut;
  1029. items.write[p_idx].shortcut_is_global = p_global;
  1030. items.write[p_idx].dirty = true;
  1031. if (items[p_idx].shortcut.is_valid()) {
  1032. _ref_shortcut(items[p_idx].shortcut);
  1033. }
  1034. control->update();
  1035. }
  1036. void PopupMenu::set_item_h_offset(int p_idx, int p_offset) {
  1037. ERR_FAIL_INDEX(p_idx, items.size());
  1038. items.write[p_idx].h_ofs = p_offset;
  1039. control->update();
  1040. child_controls_changed();
  1041. }
  1042. void PopupMenu::set_item_multistate(int p_idx, int p_state) {
  1043. ERR_FAIL_INDEX(p_idx, items.size());
  1044. items.write[p_idx].state = p_state;
  1045. control->update();
  1046. }
  1047. void PopupMenu::set_item_shortcut_disabled(int p_idx, bool p_disabled) {
  1048. ERR_FAIL_INDEX(p_idx, items.size());
  1049. items.write[p_idx].shortcut_is_disabled = p_disabled;
  1050. control->update();
  1051. }
  1052. void PopupMenu::toggle_item_multistate(int p_idx) {
  1053. ERR_FAIL_INDEX(p_idx, items.size());
  1054. if (0 >= items[p_idx].max_states) {
  1055. return;
  1056. }
  1057. ++items.write[p_idx].state;
  1058. if (items.write[p_idx].max_states <= items[p_idx].state) {
  1059. items.write[p_idx].state = 0;
  1060. }
  1061. control->update();
  1062. }
  1063. bool PopupMenu::is_item_checkable(int p_idx) const {
  1064. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  1065. return items[p_idx].checkable_type;
  1066. }
  1067. bool PopupMenu::is_item_radio_checkable(int p_idx) const {
  1068. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  1069. return items[p_idx].checkable_type == Item::CHECKABLE_TYPE_RADIO_BUTTON;
  1070. }
  1071. bool PopupMenu::is_item_shortcut_disabled(int p_idx) const {
  1072. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  1073. return items[p_idx].shortcut_is_disabled;
  1074. }
  1075. int PopupMenu::get_current_index() const {
  1076. return mouse_over;
  1077. }
  1078. void PopupMenu::set_item_count(int p_count) {
  1079. ERR_FAIL_COND(p_count < 0);
  1080. items.resize(p_count);
  1081. control->update();
  1082. child_controls_changed();
  1083. notify_property_list_changed();
  1084. }
  1085. int PopupMenu::get_item_count() const {
  1086. return items.size();
  1087. }
  1088. bool PopupMenu::activate_item_by_event(const Ref<InputEvent> &p_event, bool p_for_global_only) {
  1089. Key code = KEY_NONE;
  1090. Ref<InputEventKey> k = p_event;
  1091. if (k.is_valid()) {
  1092. code = k->get_keycode();
  1093. if (code == KEY_NONE) {
  1094. code = (Key)k->get_unicode();
  1095. }
  1096. if (k->is_ctrl_pressed()) {
  1097. code |= KEY_MASK_CTRL;
  1098. }
  1099. if (k->is_alt_pressed()) {
  1100. code |= KEY_MASK_ALT;
  1101. }
  1102. if (k->is_meta_pressed()) {
  1103. code |= KEY_MASK_META;
  1104. }
  1105. if (k->is_shift_pressed()) {
  1106. code |= KEY_MASK_SHIFT;
  1107. }
  1108. }
  1109. for (int i = 0; i < items.size(); i++) {
  1110. if (is_item_disabled(i) || items[i].shortcut_is_disabled) {
  1111. continue;
  1112. }
  1113. if (items[i].shortcut.is_valid() && items[i].shortcut->matches_event(p_event) && (items[i].shortcut_is_global || !p_for_global_only)) {
  1114. activate_item(i);
  1115. return true;
  1116. }
  1117. if (code != 0 && items[i].accel == code) {
  1118. activate_item(i);
  1119. return true;
  1120. }
  1121. if (items[i].submenu != "") {
  1122. Node *n = get_node(items[i].submenu);
  1123. if (!n) {
  1124. continue;
  1125. }
  1126. PopupMenu *pm = Object::cast_to<PopupMenu>(n);
  1127. if (!pm) {
  1128. continue;
  1129. }
  1130. if (pm->activate_item_by_event(p_event, p_for_global_only)) {
  1131. return true;
  1132. }
  1133. }
  1134. }
  1135. return false;
  1136. }
  1137. void PopupMenu::activate_item(int p_item) {
  1138. ERR_FAIL_INDEX(p_item, items.size());
  1139. ERR_FAIL_COND(items[p_item].separator);
  1140. int id = items[p_item].id >= 0 ? items[p_item].id : p_item;
  1141. //hide all parent PopupMenus
  1142. Node *next = get_parent();
  1143. PopupMenu *pop = Object::cast_to<PopupMenu>(next);
  1144. while (pop) {
  1145. // We close all parents that are chained together,
  1146. // with hide_on_item_selection enabled
  1147. if (items[p_item].checkable_type) {
  1148. if (!hide_on_checkable_item_selection || !pop->is_hide_on_checkable_item_selection()) {
  1149. break;
  1150. }
  1151. } else if (0 < items[p_item].max_states) {
  1152. if (!hide_on_multistate_item_selection || !pop->is_hide_on_multistate_item_selection()) {
  1153. break;
  1154. }
  1155. } else if (!hide_on_item_selection || !pop->is_hide_on_item_selection()) {
  1156. break;
  1157. }
  1158. pop->hide();
  1159. next = next->get_parent();
  1160. pop = Object::cast_to<PopupMenu>(next);
  1161. }
  1162. // Hides popup by default; unless otherwise specified
  1163. // by using set_hide_on_item_selection and set_hide_on_checkable_item_selection
  1164. bool need_hide = true;
  1165. if (items[p_item].checkable_type) {
  1166. if (!hide_on_checkable_item_selection) {
  1167. need_hide = false;
  1168. }
  1169. } else if (0 < items[p_item].max_states) {
  1170. if (!hide_on_multistate_item_selection) {
  1171. need_hide = false;
  1172. }
  1173. } else if (!hide_on_item_selection) {
  1174. need_hide = false;
  1175. }
  1176. emit_signal(SNAME("id_pressed"), id);
  1177. emit_signal(SNAME("index_pressed"), p_item);
  1178. if (need_hide) {
  1179. hide();
  1180. }
  1181. }
  1182. void PopupMenu::remove_item(int p_idx) {
  1183. ERR_FAIL_INDEX(p_idx, items.size());
  1184. if (items[p_idx].shortcut.is_valid()) {
  1185. _unref_shortcut(items[p_idx].shortcut);
  1186. }
  1187. items.remove(p_idx);
  1188. control->update();
  1189. child_controls_changed();
  1190. }
  1191. void PopupMenu::add_separator(const String &p_text, int p_id) {
  1192. Item sep;
  1193. sep.separator = true;
  1194. sep.id = p_id;
  1195. if (p_text != String()) {
  1196. sep.text = p_text;
  1197. sep.xl_text = atr(p_text);
  1198. }
  1199. items.push_back(sep);
  1200. control->update();
  1201. }
  1202. void PopupMenu::clear() {
  1203. for (int i = 0; i < items.size(); i++) {
  1204. if (items[i].shortcut.is_valid()) {
  1205. _unref_shortcut(items[i].shortcut);
  1206. }
  1207. }
  1208. items.clear();
  1209. mouse_over = -1;
  1210. control->update();
  1211. child_controls_changed();
  1212. notify_property_list_changed();
  1213. }
  1214. void PopupMenu::_ref_shortcut(Ref<Shortcut> p_sc) {
  1215. if (!shortcut_refcount.has(p_sc)) {
  1216. shortcut_refcount[p_sc] = 1;
  1217. p_sc->connect("changed", callable_mp((CanvasItem *)this, &CanvasItem::update));
  1218. } else {
  1219. shortcut_refcount[p_sc] += 1;
  1220. }
  1221. }
  1222. void PopupMenu::_unref_shortcut(Ref<Shortcut> p_sc) {
  1223. ERR_FAIL_COND(!shortcut_refcount.has(p_sc));
  1224. shortcut_refcount[p_sc]--;
  1225. if (shortcut_refcount[p_sc] == 0) {
  1226. p_sc->disconnect("changed", callable_mp((CanvasItem *)this, &CanvasItem::update));
  1227. shortcut_refcount.erase(p_sc);
  1228. }
  1229. }
  1230. // Hide on item selection determines whether or not the popup will close after item selection
  1231. void PopupMenu::set_hide_on_item_selection(bool p_enabled) {
  1232. hide_on_item_selection = p_enabled;
  1233. }
  1234. bool PopupMenu::is_hide_on_item_selection() const {
  1235. return hide_on_item_selection;
  1236. }
  1237. void PopupMenu::set_hide_on_checkable_item_selection(bool p_enabled) {
  1238. hide_on_checkable_item_selection = p_enabled;
  1239. }
  1240. bool PopupMenu::is_hide_on_checkable_item_selection() const {
  1241. return hide_on_checkable_item_selection;
  1242. }
  1243. void PopupMenu::set_hide_on_multistate_item_selection(bool p_enabled) {
  1244. hide_on_multistate_item_selection = p_enabled;
  1245. }
  1246. bool PopupMenu::is_hide_on_multistate_item_selection() const {
  1247. return hide_on_multistate_item_selection;
  1248. }
  1249. void PopupMenu::set_submenu_popup_delay(float p_time) {
  1250. if (p_time <= 0) {
  1251. p_time = 0.01;
  1252. }
  1253. submenu_timer->set_wait_time(p_time);
  1254. }
  1255. float PopupMenu::get_submenu_popup_delay() const {
  1256. return submenu_timer->get_wait_time();
  1257. }
  1258. void PopupMenu::set_allow_search(bool p_allow) {
  1259. allow_search = p_allow;
  1260. }
  1261. bool PopupMenu::get_allow_search() const {
  1262. return allow_search;
  1263. }
  1264. String PopupMenu::get_tooltip(const Point2 &p_pos) const {
  1265. int over = _get_mouse_over(p_pos);
  1266. if (over < 0 || over >= items.size()) {
  1267. return "";
  1268. }
  1269. return items[over].tooltip;
  1270. }
  1271. void PopupMenu::set_parent_rect(const Rect2 &p_rect) {
  1272. parent_rect = p_rect;
  1273. }
  1274. void PopupMenu::get_translatable_strings(List<String> *p_strings) const {
  1275. for (int i = 0; i < items.size(); i++) {
  1276. if (items[i].xl_text != "") {
  1277. p_strings->push_back(items[i].xl_text);
  1278. }
  1279. }
  1280. }
  1281. void PopupMenu::add_autohide_area(const Rect2 &p_area) {
  1282. autohide_areas.push_back(p_area);
  1283. }
  1284. void PopupMenu::clear_autohide_areas() {
  1285. autohide_areas.clear();
  1286. }
  1287. void PopupMenu::take_mouse_focus() {
  1288. ERR_FAIL_COND(!is_inside_tree());
  1289. if (get_parent()) {
  1290. get_parent()->get_viewport()->pass_mouse_focus_to(this, control);
  1291. }
  1292. }
  1293. bool PopupMenu::_set(const StringName &p_name, const Variant &p_value) {
  1294. Vector<String> components = String(p_name).split("/", true, 2);
  1295. if (components.size() >= 2 && components[0].begins_with("item_") && components[0].trim_prefix("item_").is_valid_int()) {
  1296. int item_index = components[0].trim_prefix("item_").to_int();
  1297. String property = components[1];
  1298. if (property == "text") {
  1299. set_item_text(item_index, p_value);
  1300. return true;
  1301. } else if (property == "icon") {
  1302. set_item_icon(item_index, p_value);
  1303. return true;
  1304. } else if (property == "checkable") {
  1305. bool radio_checkable = (int)p_value == Item::CHECKABLE_TYPE_RADIO_BUTTON;
  1306. if (radio_checkable) {
  1307. set_item_as_radio_checkable(item_index, true);
  1308. } else {
  1309. bool checkable = p_value;
  1310. set_item_as_checkable(item_index, checkable);
  1311. }
  1312. return true;
  1313. } else if (property == "checked") {
  1314. set_item_checked(item_index, p_value);
  1315. return true;
  1316. } else if (property == "id") {
  1317. set_item_id(item_index, p_value);
  1318. return true;
  1319. } else if (components[1] == "disabled") {
  1320. set_item_disabled(item_index, p_value);
  1321. return true;
  1322. } else if (property == "separator") {
  1323. set_item_as_separator(item_index, p_value);
  1324. return true;
  1325. }
  1326. }
  1327. #ifndef DISABLE_DEPRECATED
  1328. // Compatibility.
  1329. if (p_name == "items") {
  1330. Array arr = p_value;
  1331. ERR_FAIL_COND_V(arr.size() % 10, false);
  1332. clear();
  1333. for (int i = 0; i < arr.size(); i += 10) {
  1334. String text = arr[i + 0];
  1335. Ref<Texture2D> icon = arr[i + 1];
  1336. // For compatibility, use false/true for no/checkbox and integers for other values
  1337. bool checkable = arr[i + 2];
  1338. bool radio_checkable = (int)arr[i + 2] == Item::CHECKABLE_TYPE_RADIO_BUTTON;
  1339. bool checked = arr[i + 3];
  1340. bool disabled = arr[i + 4];
  1341. int id = arr[i + 5];
  1342. int accel = arr[i + 6];
  1343. Variant meta = arr[i + 7];
  1344. String subm = arr[i + 8];
  1345. bool sep = arr[i + 9];
  1346. int idx = get_item_count();
  1347. add_item(text, id);
  1348. set_item_icon(idx, icon);
  1349. if (checkable) {
  1350. if (radio_checkable) {
  1351. set_item_as_radio_checkable(idx, true);
  1352. } else {
  1353. set_item_as_checkable(idx, true);
  1354. }
  1355. }
  1356. set_item_checked(idx, checked);
  1357. set_item_disabled(idx, disabled);
  1358. set_item_id(idx, id);
  1359. set_item_metadata(idx, meta);
  1360. set_item_as_separator(idx, sep);
  1361. set_item_accelerator(idx, accel);
  1362. set_item_submenu(idx, subm);
  1363. }
  1364. }
  1365. #endif
  1366. return false;
  1367. }
  1368. bool PopupMenu::_get(const StringName &p_name, Variant &r_ret) const {
  1369. Vector<String> components = String(p_name).split("/", true, 2);
  1370. if (components.size() >= 2 && components[0].begins_with("item_") && components[0].trim_prefix("item_").is_valid_int()) {
  1371. int item_index = components[0].trim_prefix("item_").to_int();
  1372. String property = components[1];
  1373. if (property == "text") {
  1374. r_ret = get_item_text(item_index);
  1375. return true;
  1376. } else if (property == "icon") {
  1377. r_ret = get_item_icon(item_index);
  1378. return true;
  1379. } else if (property == "checkable") {
  1380. r_ret = this->items[item_index].checkable_type;
  1381. return true;
  1382. } else if (property == "checked") {
  1383. r_ret = is_item_checked(item_index);
  1384. return true;
  1385. } else if (property == "id") {
  1386. r_ret = get_item_id(item_index);
  1387. return true;
  1388. } else if (components[1] == "disabled") {
  1389. r_ret = is_item_disabled(item_index);
  1390. return true;
  1391. } else if (property == "separator") {
  1392. r_ret = is_item_separator(item_index);
  1393. return true;
  1394. }
  1395. }
  1396. return false;
  1397. }
  1398. void PopupMenu::_get_property_list(List<PropertyInfo> *p_list) const {
  1399. for (int i = 0; i < items.size(); i++) {
  1400. p_list->push_back(PropertyInfo(Variant::STRING, vformat("item_%d/text", i)));
  1401. PropertyInfo pi = PropertyInfo(Variant::OBJECT, vformat("item_%d/icon", i), PROPERTY_HINT_RESOURCE_TYPE, "Texture2D");
  1402. pi.usage &= ~(get_item_icon(i).is_null() ? PROPERTY_USAGE_STORAGE : 0);
  1403. p_list->push_back(pi);
  1404. pi = PropertyInfo(Variant::INT, vformat("item_%d/checkable", i), PROPERTY_HINT_ENUM, "No,As checkbox,As radio button");
  1405. pi.usage &= ~(!is_item_checkable(i) ? PROPERTY_USAGE_STORAGE : 0);
  1406. p_list->push_back(pi);
  1407. pi = PropertyInfo(Variant::BOOL, vformat("item_%d/checked", i));
  1408. pi.usage &= ~(!is_item_checked(i) ? PROPERTY_USAGE_STORAGE : 0);
  1409. p_list->push_back(pi);
  1410. pi = PropertyInfo(Variant::INT, vformat("item_%d/id", i), PROPERTY_HINT_RANGE, "1,10,1,or_greater");
  1411. p_list->push_back(pi);
  1412. pi = PropertyInfo(Variant::BOOL, vformat("item_%d/disabled", i));
  1413. pi.usage &= ~(!is_item_disabled(i) ? PROPERTY_USAGE_STORAGE : 0);
  1414. p_list->push_back(pi);
  1415. pi = PropertyInfo(Variant::BOOL, vformat("item_%d/separator", i));
  1416. pi.usage &= ~(!is_item_separator(i) ? PROPERTY_USAGE_STORAGE : 0);
  1417. p_list->push_back(pi);
  1418. }
  1419. }
  1420. void PopupMenu::_bind_methods() {
  1421. ClassDB::bind_method(D_METHOD("add_item", "label", "id", "accel"), &PopupMenu::add_item, DEFVAL(-1), DEFVAL(0));
  1422. ClassDB::bind_method(D_METHOD("add_icon_item", "texture", "label", "id", "accel"), &PopupMenu::add_icon_item, DEFVAL(-1), DEFVAL(0));
  1423. ClassDB::bind_method(D_METHOD("add_check_item", "label", "id", "accel"), &PopupMenu::add_check_item, DEFVAL(-1), DEFVAL(0));
  1424. ClassDB::bind_method(D_METHOD("add_icon_check_item", "texture", "label", "id", "accel"), &PopupMenu::add_icon_check_item, DEFVAL(-1), DEFVAL(0));
  1425. ClassDB::bind_method(D_METHOD("add_radio_check_item", "label", "id", "accel"), &PopupMenu::add_radio_check_item, DEFVAL(-1), DEFVAL(0));
  1426. ClassDB::bind_method(D_METHOD("add_icon_radio_check_item", "texture", "label", "id", "accel"), &PopupMenu::add_icon_radio_check_item, DEFVAL(-1), DEFVAL(0));
  1427. ClassDB::bind_method(D_METHOD("add_multistate_item", "label", "max_states", "default_state", "id", "accel"), &PopupMenu::add_multistate_item, DEFVAL(0), DEFVAL(-1), DEFVAL(0));
  1428. ClassDB::bind_method(D_METHOD("add_shortcut", "shortcut", "id", "global"), &PopupMenu::add_shortcut, DEFVAL(-1), DEFVAL(false));
  1429. ClassDB::bind_method(D_METHOD("add_icon_shortcut", "texture", "shortcut", "id", "global"), &PopupMenu::add_icon_shortcut, DEFVAL(-1), DEFVAL(false));
  1430. ClassDB::bind_method(D_METHOD("add_check_shortcut", "shortcut", "id", "global"), &PopupMenu::add_check_shortcut, DEFVAL(-1), DEFVAL(false));
  1431. ClassDB::bind_method(D_METHOD("add_icon_check_shortcut", "texture", "shortcut", "id", "global"), &PopupMenu::add_icon_check_shortcut, DEFVAL(-1), DEFVAL(false));
  1432. ClassDB::bind_method(D_METHOD("add_radio_check_shortcut", "shortcut", "id", "global"), &PopupMenu::add_radio_check_shortcut, DEFVAL(-1), DEFVAL(false));
  1433. ClassDB::bind_method(D_METHOD("add_icon_radio_check_shortcut", "texture", "shortcut", "id", "global"), &PopupMenu::add_icon_radio_check_shortcut, DEFVAL(-1), DEFVAL(false));
  1434. ClassDB::bind_method(D_METHOD("add_submenu_item", "label", "submenu", "id"), &PopupMenu::add_submenu_item, DEFVAL(-1));
  1435. ClassDB::bind_method(D_METHOD("set_item_text", "idx", "text"), &PopupMenu::set_item_text);
  1436. ClassDB::bind_method(D_METHOD("set_item_text_direction", "idx", "direction"), &PopupMenu::set_item_text_direction);
  1437. ClassDB::bind_method(D_METHOD("set_item_opentype_feature", "idx", "tag", "value"), &PopupMenu::set_item_opentype_feature);
  1438. ClassDB::bind_method(D_METHOD("set_item_language", "idx", "language"), &PopupMenu::set_item_language);
  1439. ClassDB::bind_method(D_METHOD("set_item_icon", "idx", "icon"), &PopupMenu::set_item_icon);
  1440. ClassDB::bind_method(D_METHOD("set_item_checked", "idx", "checked"), &PopupMenu::set_item_checked);
  1441. ClassDB::bind_method(D_METHOD("set_item_id", "idx", "id"), &PopupMenu::set_item_id);
  1442. ClassDB::bind_method(D_METHOD("set_item_accelerator", "idx", "accel"), &PopupMenu::set_item_accelerator);
  1443. ClassDB::bind_method(D_METHOD("set_item_metadata", "idx", "metadata"), &PopupMenu::set_item_metadata);
  1444. ClassDB::bind_method(D_METHOD("set_item_disabled", "idx", "disabled"), &PopupMenu::set_item_disabled);
  1445. ClassDB::bind_method(D_METHOD("set_item_submenu", "idx", "submenu"), &PopupMenu::set_item_submenu);
  1446. ClassDB::bind_method(D_METHOD("set_item_as_separator", "idx", "enable"), &PopupMenu::set_item_as_separator);
  1447. ClassDB::bind_method(D_METHOD("set_item_as_checkable", "idx", "enable"), &PopupMenu::set_item_as_checkable);
  1448. ClassDB::bind_method(D_METHOD("set_item_as_radio_checkable", "idx", "enable"), &PopupMenu::set_item_as_radio_checkable);
  1449. ClassDB::bind_method(D_METHOD("set_item_tooltip", "idx", "tooltip"), &PopupMenu::set_item_tooltip);
  1450. ClassDB::bind_method(D_METHOD("set_item_shortcut", "idx", "shortcut", "global"), &PopupMenu::set_item_shortcut, DEFVAL(false));
  1451. ClassDB::bind_method(D_METHOD("set_item_multistate", "idx", "state"), &PopupMenu::set_item_multistate);
  1452. ClassDB::bind_method(D_METHOD("set_item_shortcut_disabled", "idx", "disabled"), &PopupMenu::set_item_shortcut_disabled);
  1453. ClassDB::bind_method(D_METHOD("toggle_item_checked", "idx"), &PopupMenu::toggle_item_checked);
  1454. ClassDB::bind_method(D_METHOD("toggle_item_multistate", "idx"), &PopupMenu::toggle_item_multistate);
  1455. ClassDB::bind_method(D_METHOD("get_item_text", "idx"), &PopupMenu::get_item_text);
  1456. ClassDB::bind_method(D_METHOD("get_item_text_direction", "idx"), &PopupMenu::get_item_text_direction);
  1457. ClassDB::bind_method(D_METHOD("get_item_opentype_feature", "idx", "tag"), &PopupMenu::get_item_opentype_feature);
  1458. ClassDB::bind_method(D_METHOD("clear_item_opentype_features", "idx"), &PopupMenu::clear_item_opentype_features);
  1459. ClassDB::bind_method(D_METHOD("get_item_language", "idx"), &PopupMenu::get_item_language);
  1460. ClassDB::bind_method(D_METHOD("get_item_icon", "idx"), &PopupMenu::get_item_icon);
  1461. ClassDB::bind_method(D_METHOD("is_item_checked", "idx"), &PopupMenu::is_item_checked);
  1462. ClassDB::bind_method(D_METHOD("get_item_id", "idx"), &PopupMenu::get_item_id);
  1463. ClassDB::bind_method(D_METHOD("get_item_index", "id"), &PopupMenu::get_item_index);
  1464. ClassDB::bind_method(D_METHOD("get_item_accelerator", "idx"), &PopupMenu::get_item_accelerator);
  1465. ClassDB::bind_method(D_METHOD("get_item_metadata", "idx"), &PopupMenu::get_item_metadata);
  1466. ClassDB::bind_method(D_METHOD("is_item_disabled", "idx"), &PopupMenu::is_item_disabled);
  1467. ClassDB::bind_method(D_METHOD("get_item_submenu", "idx"), &PopupMenu::get_item_submenu);
  1468. ClassDB::bind_method(D_METHOD("is_item_separator", "idx"), &PopupMenu::is_item_separator);
  1469. ClassDB::bind_method(D_METHOD("is_item_checkable", "idx"), &PopupMenu::is_item_checkable);
  1470. ClassDB::bind_method(D_METHOD("is_item_radio_checkable", "idx"), &PopupMenu::is_item_radio_checkable);
  1471. ClassDB::bind_method(D_METHOD("is_item_shortcut_disabled", "idx"), &PopupMenu::is_item_shortcut_disabled);
  1472. ClassDB::bind_method(D_METHOD("get_item_tooltip", "idx"), &PopupMenu::get_item_tooltip);
  1473. ClassDB::bind_method(D_METHOD("get_item_shortcut", "idx"), &PopupMenu::get_item_shortcut);
  1474. ClassDB::bind_method(D_METHOD("get_current_index"), &PopupMenu::get_current_index);
  1475. ClassDB::bind_method(D_METHOD("set_item_count", "count"), &PopupMenu::set_item_count);
  1476. ClassDB::bind_method(D_METHOD("get_item_count"), &PopupMenu::get_item_count);
  1477. ClassDB::bind_method(D_METHOD("remove_item", "idx"), &PopupMenu::remove_item);
  1478. ClassDB::bind_method(D_METHOD("add_separator", "label", "id"), &PopupMenu::add_separator, DEFVAL(String()), DEFVAL(-1));
  1479. ClassDB::bind_method(D_METHOD("clear"), &PopupMenu::clear);
  1480. ClassDB::bind_method(D_METHOD("set_hide_on_item_selection", "enable"), &PopupMenu::set_hide_on_item_selection);
  1481. ClassDB::bind_method(D_METHOD("is_hide_on_item_selection"), &PopupMenu::is_hide_on_item_selection);
  1482. ClassDB::bind_method(D_METHOD("set_hide_on_checkable_item_selection", "enable"), &PopupMenu::set_hide_on_checkable_item_selection);
  1483. ClassDB::bind_method(D_METHOD("is_hide_on_checkable_item_selection"), &PopupMenu::is_hide_on_checkable_item_selection);
  1484. ClassDB::bind_method(D_METHOD("set_hide_on_state_item_selection", "enable"), &PopupMenu::set_hide_on_multistate_item_selection);
  1485. ClassDB::bind_method(D_METHOD("is_hide_on_state_item_selection"), &PopupMenu::is_hide_on_multistate_item_selection);
  1486. ClassDB::bind_method(D_METHOD("set_submenu_popup_delay", "seconds"), &PopupMenu::set_submenu_popup_delay);
  1487. ClassDB::bind_method(D_METHOD("get_submenu_popup_delay"), &PopupMenu::get_submenu_popup_delay);
  1488. ClassDB::bind_method(D_METHOD("set_allow_search", "allow"), &PopupMenu::set_allow_search);
  1489. ClassDB::bind_method(D_METHOD("get_allow_search"), &PopupMenu::get_allow_search);
  1490. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_on_item_selection"), "set_hide_on_item_selection", "is_hide_on_item_selection");
  1491. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_on_checkable_item_selection"), "set_hide_on_checkable_item_selection", "is_hide_on_checkable_item_selection");
  1492. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_on_state_item_selection"), "set_hide_on_state_item_selection", "is_hide_on_state_item_selection");
  1493. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "submenu_popup_delay"), "set_submenu_popup_delay", "get_submenu_popup_delay");
  1494. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_search"), "set_allow_search", "get_allow_search");
  1495. ADD_ARRAY_COUNT("Items", "items_count", "set_item_count", "get_item_count", "item_");
  1496. ADD_SIGNAL(MethodInfo("id_pressed", PropertyInfo(Variant::INT, "id")));
  1497. ADD_SIGNAL(MethodInfo("id_focused", PropertyInfo(Variant::INT, "id")));
  1498. ADD_SIGNAL(MethodInfo("index_pressed", PropertyInfo(Variant::INT, "index")));
  1499. }
  1500. void PopupMenu::popup(const Rect2 &p_bounds) {
  1501. moved = Vector2();
  1502. popup_time_msec = OS::get_singleton()->get_ticks_msec();
  1503. Popup::popup(p_bounds);
  1504. }
  1505. PopupMenu::PopupMenu() {
  1506. // Margin Container
  1507. margin_container = memnew(MarginContainer);
  1508. margin_container->set_anchors_and_offsets_preset(Control::PRESET_WIDE);
  1509. add_child(margin_container, false, INTERNAL_MODE_FRONT);
  1510. margin_container->connect("draw", callable_mp(this, &PopupMenu::_draw_background));
  1511. // Scroll Container
  1512. scroll_container = memnew(ScrollContainer);
  1513. scroll_container->set_clip_contents(true);
  1514. margin_container->add_child(scroll_container);
  1515. // The control which will display the items
  1516. control = memnew(Control);
  1517. control->set_clip_contents(false);
  1518. control->set_anchors_and_offsets_preset(Control::PRESET_WIDE);
  1519. control->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1520. control->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1521. scroll_container->add_child(control, false, INTERNAL_MODE_FRONT);
  1522. control->connect("draw", callable_mp(this, &PopupMenu::_draw_items));
  1523. connect("window_input", callable_mp(this, &PopupMenu::gui_input));
  1524. submenu_timer = memnew(Timer);
  1525. submenu_timer->set_wait_time(0.3);
  1526. submenu_timer->set_one_shot(true);
  1527. submenu_timer->connect("timeout", callable_mp(this, &PopupMenu::_submenu_timeout));
  1528. add_child(submenu_timer, false, INTERNAL_MODE_FRONT);
  1529. minimum_lifetime_timer = memnew(Timer);
  1530. minimum_lifetime_timer->set_wait_time(0.3);
  1531. minimum_lifetime_timer->set_one_shot(true);
  1532. minimum_lifetime_timer->connect("timeout", callable_mp(this, &PopupMenu::_minimum_lifetime_timeout));
  1533. add_child(minimum_lifetime_timer, false, INTERNAL_MODE_FRONT);
  1534. }
  1535. PopupMenu::~PopupMenu() {
  1536. }