popup_menu.cpp 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889
  1. /**************************************************************************/
  2. /* popup_menu.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "popup_menu.h"
  31. #include "popup_menu.compat.inc"
  32. #include "core/config/project_settings.h"
  33. #include "core/input/input.h"
  34. #include "core/os/keyboard.h"
  35. #include "core/os/os.h"
  36. #include "core/string/print_string.h"
  37. #include "core/string/translation.h"
  38. #include "scene/gui/menu_bar.h"
  39. #include "scene/theme/theme_db.h"
  40. HashMap<String, PopupMenu *> PopupMenu::system_menus;
  41. bool PopupMenu::_set_item_accelerator(int p_index, const Ref<InputEventKey> &p_ie) {
  42. DisplayServer *ds = DisplayServer::get_singleton();
  43. if (p_ie->get_physical_keycode() == Key::NONE && p_ie->get_keycode() == Key::NONE && p_ie->get_key_label() != Key::NONE) {
  44. ds->global_menu_set_item_accelerator(global_menu_name, p_index, p_ie->get_key_label_with_modifiers());
  45. return true;
  46. } else if (p_ie->get_keycode() != Key::NONE) {
  47. ds->global_menu_set_item_accelerator(global_menu_name, p_index, p_ie->get_keycode_with_modifiers());
  48. return true;
  49. } else if (p_ie->get_physical_keycode() != Key::NONE) {
  50. ds->global_menu_set_item_accelerator(global_menu_name, p_index, ds->keyboard_get_keycode_from_physical(p_ie->get_physical_keycode_with_modifiers()));
  51. return true;
  52. }
  53. return false;
  54. }
  55. String PopupMenu::bind_global_menu() {
  56. #ifdef TOOLS_ENABLED
  57. if (is_part_of_edited_scene()) {
  58. return String();
  59. }
  60. #endif
  61. if (!DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_GLOBAL_MENU)) {
  62. return String();
  63. }
  64. if (!global_menu_name.is_empty()) {
  65. return global_menu_name; // Already bound;
  66. }
  67. global_menu_name = "__PopupMenu#" + itos(get_instance_id());
  68. if (system_menu_name.length() > 0) {
  69. if (system_menus.has(system_menu_name)) {
  70. WARN_PRINT(vformat("Attempting to bind PopupMenu to the special menu %s, but another menu is already bound to it. This menu: %s, current menu: %s", system_menu_name, this->get_description(), system_menus[system_menu_name]->get_description()));
  71. } else {
  72. const Dictionary &supported_special_names = DisplayServer::get_singleton()->global_menu_get_system_menu_roots();
  73. if (supported_special_names.has(system_menu_name)) {
  74. system_menus[system_menu_name] = this;
  75. global_menu_name = system_menu_name;
  76. }
  77. }
  78. }
  79. DisplayServer *ds = DisplayServer::get_singleton();
  80. ds->global_menu_set_popup_callbacks(global_menu_name, callable_mp(this, &PopupMenu::_about_to_popup), callable_mp(this, &PopupMenu::_about_to_close));
  81. for (int i = 0; i < items.size(); i++) {
  82. Item &item = items.write[i];
  83. if (item.separator) {
  84. ds->global_menu_add_separator(global_menu_name);
  85. } else {
  86. int index = ds->global_menu_add_item(global_menu_name, item.xl_text, callable_mp(this, &PopupMenu::activate_item), item.shortcut_is_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), i);
  87. if (!item.submenu.is_empty()) {
  88. PopupMenu *pm = Object::cast_to<PopupMenu>(get_node_or_null(item.submenu));
  89. if (pm) {
  90. String submenu_name = pm->bind_global_menu();
  91. ds->global_menu_set_item_submenu(global_menu_name, index, submenu_name);
  92. item.submenu_bound = true;
  93. }
  94. }
  95. if (item.checkable_type == Item::CHECKABLE_TYPE_CHECK_BOX) {
  96. ds->global_menu_set_item_checkable(global_menu_name, index, true);
  97. } else if (item.checkable_type == Item::CHECKABLE_TYPE_RADIO_BUTTON) {
  98. ds->global_menu_set_item_radio_checkable(global_menu_name, index, true);
  99. }
  100. ds->global_menu_set_item_checked(global_menu_name, index, item.checked);
  101. ds->global_menu_set_item_disabled(global_menu_name, index, item.disabled);
  102. ds->global_menu_set_item_max_states(global_menu_name, index, item.max_states);
  103. ds->global_menu_set_item_icon(global_menu_name, index, item.icon);
  104. ds->global_menu_set_item_state(global_menu_name, index, item.state);
  105. ds->global_menu_set_item_indentation_level(global_menu_name, index, item.indent);
  106. ds->global_menu_set_item_tooltip(global_menu_name, index, item.tooltip);
  107. if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) {
  108. Array events = item.shortcut->get_events();
  109. for (int j = 0; j < events.size(); j++) {
  110. Ref<InputEventKey> ie = events[j];
  111. if (ie.is_valid() && _set_item_accelerator(index, ie)) {
  112. break;
  113. }
  114. }
  115. } else if (item.accel != Key::NONE) {
  116. ds->global_menu_set_item_accelerator(global_menu_name, index, item.accel);
  117. }
  118. }
  119. }
  120. return global_menu_name;
  121. }
  122. void PopupMenu::unbind_global_menu() {
  123. if (global_menu_name.is_empty()) {
  124. return;
  125. }
  126. if (global_menu_name == system_menu_name && system_menus[system_menu_name] == this) {
  127. system_menus.erase(system_menu_name);
  128. }
  129. for (int i = 0; i < items.size(); i++) {
  130. Item &item = items.write[i];
  131. if (!item.submenu.is_empty()) {
  132. PopupMenu *pm = Object::cast_to<PopupMenu>(get_node_or_null(item.submenu));
  133. if (pm) {
  134. pm->unbind_global_menu();
  135. }
  136. item.submenu_bound = false;
  137. }
  138. }
  139. DisplayServer::get_singleton()->global_menu_clear(global_menu_name);
  140. global_menu_name = String();
  141. }
  142. bool PopupMenu::is_system_menu() const {
  143. return (global_menu_name == system_menu_name) && (system_menu_name.length() > 0);
  144. }
  145. void PopupMenu::set_system_menu_root(const String &p_special) {
  146. if (is_inside_tree() && system_menu_name.length() > 0) {
  147. unbind_global_menu();
  148. }
  149. system_menu_name = p_special;
  150. if (is_inside_tree() && system_menu_name.length() > 0) {
  151. bind_global_menu();
  152. }
  153. }
  154. String PopupMenu::get_system_menu_root() const {
  155. return system_menu_name;
  156. }
  157. String PopupMenu::_get_accel_text(const Item &p_item) const {
  158. if (p_item.shortcut.is_valid()) {
  159. return p_item.shortcut->get_as_text();
  160. } else if (p_item.accel != Key::NONE) {
  161. return keycode_get_string(p_item.accel);
  162. }
  163. return String();
  164. }
  165. Size2 PopupMenu::_get_item_icon_size(int p_idx) const {
  166. const PopupMenu::Item &item = items[p_idx];
  167. Size2 icon_size = item.get_icon_size();
  168. int max_width = 0;
  169. if (theme_cache.icon_max_width > 0) {
  170. max_width = theme_cache.icon_max_width;
  171. }
  172. if (item.icon_max_width > 0 && (max_width == 0 || item.icon_max_width < max_width)) {
  173. max_width = item.icon_max_width;
  174. }
  175. if (max_width > 0 && icon_size.width > max_width) {
  176. icon_size.height = icon_size.height * max_width / icon_size.width;
  177. icon_size.width = max_width;
  178. }
  179. return icon_size;
  180. }
  181. Size2 PopupMenu::_get_contents_minimum_size() const {
  182. Size2 minsize = theme_cache.panel_style->get_minimum_size(); // Accounts for margin in the margin container
  183. minsize.x += scroll_container->get_v_scroll_bar()->get_size().width * 2; // Adds a buffer so that the scrollbar does not render over the top of content
  184. float max_w = 0.0;
  185. float icon_w = 0.0;
  186. int check_w = MAX(theme_cache.checked->get_width(), theme_cache.radio_checked->get_width()) + theme_cache.h_separation;
  187. int accel_max_w = 0;
  188. bool has_check = false;
  189. for (int i = 0; i < items.size(); i++) {
  190. Size2 item_size;
  191. const_cast<PopupMenu *>(this)->_shape_item(i);
  192. Size2 icon_size = _get_item_icon_size(i);
  193. item_size.height = _get_item_height(i);
  194. icon_w = MAX(icon_size.width, icon_w);
  195. item_size.width += items[i].indent * theme_cache.indent;
  196. if (items[i].checkable_type && !items[i].separator) {
  197. has_check = true;
  198. }
  199. item_size.width += items[i].text_buf->get_size().x;
  200. item_size.height += theme_cache.v_separation;
  201. if (items[i].accel != Key::NONE || (items[i].shortcut.is_valid() && items[i].shortcut->has_valid_event())) {
  202. int accel_w = theme_cache.h_separation * 2;
  203. accel_w += items[i].accel_text_buf->get_size().x;
  204. accel_max_w = MAX(accel_w, accel_max_w);
  205. }
  206. if (!items[i].submenu.is_empty()) {
  207. item_size.width += theme_cache.submenu->get_width();
  208. }
  209. max_w = MAX(max_w, item_size.width);
  210. minsize.height += item_size.height;
  211. }
  212. int item_side_padding = theme_cache.item_start_padding + theme_cache.item_end_padding;
  213. minsize.width += max_w + icon_w + accel_max_w + item_side_padding;
  214. if (has_check) {
  215. minsize.width += check_w;
  216. }
  217. if (is_inside_tree()) {
  218. int height_limit = get_usable_parent_rect().size.height;
  219. if (minsize.height > height_limit) {
  220. minsize.height = height_limit;
  221. }
  222. }
  223. return minsize;
  224. }
  225. int PopupMenu::_get_item_height(int p_idx) const {
  226. ERR_FAIL_INDEX_V(p_idx, items.size(), 0);
  227. Size2 icon_size = _get_item_icon_size(p_idx);
  228. int icon_height = icon_size.height;
  229. if (items[p_idx].checkable_type && !items[p_idx].separator) {
  230. icon_height = MAX(icon_height, MAX(theme_cache.checked->get_height(), theme_cache.radio_checked->get_height()));
  231. }
  232. int text_height = items[p_idx].text_buf->get_size().height;
  233. if (text_height == 0 && !items[p_idx].separator) {
  234. text_height = theme_cache.font->get_height(theme_cache.font_size);
  235. }
  236. int separator_height = 0;
  237. if (items[p_idx].separator) {
  238. separator_height = MAX(theme_cache.separator_style->get_minimum_size().height, MAX(theme_cache.labeled_separator_left->get_minimum_size().height, theme_cache.labeled_separator_right->get_minimum_size().height));
  239. }
  240. return MAX(separator_height, MAX(text_height, icon_height));
  241. }
  242. int PopupMenu::_get_items_total_height() const {
  243. // Get total height of all items by taking max of icon height and font height
  244. int items_total_height = 0;
  245. for (int i = 0; i < items.size(); i++) {
  246. items_total_height += _get_item_height(i) + theme_cache.v_separation;
  247. }
  248. // Subtract a separator which is not needed for the last item.
  249. return items_total_height - theme_cache.v_separation;
  250. }
  251. int PopupMenu::_get_mouse_over(const Point2 &p_over) const {
  252. if (p_over.x < 0 || p_over.x >= get_size().width) {
  253. return -1;
  254. }
  255. // Accounts for margin in the margin container
  256. Point2 ofs = theme_cache.panel_style->get_offset() + Point2(0, theme_cache.v_separation / 2);
  257. if (ofs.y > p_over.y) {
  258. return -1;
  259. }
  260. for (int i = 0; i < items.size(); i++) {
  261. ofs.y += i > 0 ? theme_cache.v_separation : (float)theme_cache.v_separation / 2;
  262. ofs.y += _get_item_height(i);
  263. if (p_over.y - control->get_position().y < ofs.y) {
  264. return i;
  265. }
  266. }
  267. return -1;
  268. }
  269. void PopupMenu::_activate_submenu(int p_over, bool p_by_keyboard) {
  270. Node *n = get_node_or_null(items[p_over].submenu);
  271. ERR_FAIL_NULL_MSG(n, "Item subnode does not exist: '" + items[p_over].submenu + "'.");
  272. Popup *submenu_popup = Object::cast_to<Popup>(n);
  273. ERR_FAIL_NULL_MSG(submenu_popup, "Item subnode is not a Popup: '" + items[p_over].submenu + "'.");
  274. if (submenu_popup->is_visible()) {
  275. return; // Already visible.
  276. }
  277. Point2 this_pos = get_position();
  278. Rect2 this_rect(this_pos, get_size());
  279. float scroll_offset = control->get_position().y;
  280. submenu_popup->reset_size(); // Shrink the popup size to its contents.
  281. Size2 submenu_size = submenu_popup->get_size();
  282. Point2 submenu_pos;
  283. if (control->is_layout_rtl()) {
  284. submenu_pos = this_pos + Point2(-submenu_size.width, items[p_over]._ofs_cache + scroll_offset - theme_cache.v_separation / 2);
  285. } else {
  286. submenu_pos = this_pos + Point2(this_rect.size.width, items[p_over]._ofs_cache + scroll_offset - theme_cache.v_separation / 2);
  287. }
  288. // Fix pos if going outside parent rect.
  289. if (submenu_pos.x < get_parent_rect().position.x) {
  290. submenu_pos.x = this_pos.x + submenu_size.width;
  291. }
  292. if (submenu_pos.x + submenu_size.width > get_parent_rect().position.x + get_parent_rect().size.width) {
  293. submenu_pos.x = this_pos.x - submenu_size.width;
  294. }
  295. submenu_popup->set_position(submenu_pos);
  296. PopupMenu *submenu_pum = Object::cast_to<PopupMenu>(submenu_popup);
  297. if (!submenu_pum) {
  298. submenu_popup->popup();
  299. return;
  300. }
  301. submenu_pum->activated_by_keyboard = p_by_keyboard;
  302. // If not triggered by the mouse, start the popup with its first enabled item focused.
  303. if (p_by_keyboard) {
  304. for (int i = 0; i < submenu_pum->get_item_count(); i++) {
  305. if (!submenu_pum->is_item_disabled(i)) {
  306. submenu_pum->set_focused_item(i);
  307. break;
  308. }
  309. }
  310. }
  311. submenu_pum->popup();
  312. // Set autohide areas.
  313. Rect2 safe_area = this_rect;
  314. safe_area.position.y += items[p_over]._ofs_cache + scroll_offset + theme_cache.panel_style->get_offset().height - theme_cache.v_separation / 2;
  315. safe_area.size.y = items[p_over]._height_cache + theme_cache.v_separation;
  316. Viewport *vp = submenu_popup->get_embedder();
  317. if (vp) {
  318. vp->subwindow_set_popup_safe_rect(submenu_popup, safe_area);
  319. } else {
  320. DisplayServer::get_singleton()->window_set_popup_safe_rect(submenu_popup->get_window_id(), safe_area);
  321. }
  322. // Make the position of the parent popup relative to submenu popup.
  323. this_rect.position = this_rect.position - submenu_pum->get_position();
  324. // Autohide area above the submenu item.
  325. submenu_pum->clear_autohide_areas();
  326. 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 + theme_cache.panel_style->get_offset().height - theme_cache.v_separation / 2));
  327. // If there is an area below the submenu item, add an autohide area there.
  328. if (items[p_over]._ofs_cache + items[p_over]._height_cache + scroll_offset <= control->get_size().height) {
  329. int from = items[p_over]._ofs_cache + items[p_over]._height_cache + scroll_offset + theme_cache.v_separation / 2 + theme_cache.panel_style->get_offset().height;
  330. submenu_pum->add_autohide_area(Rect2(this_rect.position.x, this_rect.position.y + from, this_rect.size.x, this_rect.size.y - from));
  331. }
  332. }
  333. void PopupMenu::_parent_focused() {
  334. if (is_embedded()) {
  335. Point2 mouse_pos_adjusted;
  336. Window *window_parent = Object::cast_to<Window>(get_parent()->get_viewport());
  337. while (window_parent) {
  338. if (!window_parent->is_embedded()) {
  339. mouse_pos_adjusted += window_parent->get_position();
  340. break;
  341. }
  342. window_parent = Object::cast_to<Window>(window_parent->get_parent()->get_viewport());
  343. }
  344. Rect2 safe_area = get_embedder()->subwindow_get_popup_safe_rect(this);
  345. Point2 pos = DisplayServer::get_singleton()->mouse_get_position() - mouse_pos_adjusted;
  346. if (safe_area == Rect2i() || !safe_area.has_point(pos)) {
  347. Popup::_parent_focused();
  348. } else {
  349. grab_focus();
  350. }
  351. }
  352. }
  353. void PopupMenu::_submenu_timeout() {
  354. if (mouse_over == submenu_over) {
  355. _activate_submenu(mouse_over);
  356. }
  357. submenu_over = -1;
  358. }
  359. void PopupMenu::_input_from_window(const Ref<InputEvent> &p_event) {
  360. if (p_event.is_valid()) {
  361. _input_from_window_internal(p_event);
  362. } else {
  363. WARN_PRINT_ONCE("PopupMenu has received an invalid InputEvent. Consider filtering invalid events out.");
  364. }
  365. Popup::_input_from_window(p_event);
  366. }
  367. void PopupMenu::_input_from_window_internal(const Ref<InputEvent> &p_event) {
  368. Ref<InputEventMouseButton> b = p_event;
  369. Ref<InputEventMouseMotion> m = p_event;
  370. if (!items.is_empty()) {
  371. Input *input = Input::get_singleton();
  372. Ref<InputEventJoypadMotion> joypadmotion_event = p_event;
  373. Ref<InputEventJoypadButton> joypadbutton_event = p_event;
  374. bool is_joypad_event = (joypadmotion_event.is_valid() || joypadbutton_event.is_valid());
  375. if (p_event->is_action("ui_down", true) && p_event->is_pressed()) {
  376. if (is_joypad_event) {
  377. if (!input->is_action_just_pressed("ui_down", true)) {
  378. return;
  379. }
  380. set_process_internal(true);
  381. }
  382. int search_from = mouse_over + 1;
  383. if (search_from >= items.size()) {
  384. search_from = 0;
  385. }
  386. bool match_found = false;
  387. for (int i = search_from; i < items.size(); i++) {
  388. if (!items[i].separator && !items[i].disabled) {
  389. mouse_over = i;
  390. emit_signal(SNAME("id_focused"), i);
  391. scroll_to_item(i);
  392. control->queue_redraw();
  393. set_input_as_handled();
  394. match_found = true;
  395. break;
  396. }
  397. }
  398. if (!match_found) {
  399. // If the last item is not selectable, try re-searching from the start.
  400. for (int i = 0; i < search_from; i++) {
  401. if (!items[i].separator && !items[i].disabled) {
  402. mouse_over = i;
  403. emit_signal(SNAME("id_focused"), i);
  404. scroll_to_item(i);
  405. control->queue_redraw();
  406. set_input_as_handled();
  407. break;
  408. }
  409. }
  410. }
  411. } else if (p_event->is_action("ui_up", true) && p_event->is_pressed()) {
  412. if (is_joypad_event) {
  413. if (!input->is_action_just_pressed("ui_up", true)) {
  414. return;
  415. }
  416. set_process_internal(true);
  417. }
  418. int search_from = mouse_over - 1;
  419. if (search_from < 0) {
  420. search_from = items.size() - 1;
  421. }
  422. bool match_found = false;
  423. for (int i = search_from; i >= 0; i--) {
  424. if (!items[i].separator && !items[i].disabled) {
  425. mouse_over = i;
  426. emit_signal(SNAME("id_focused"), i);
  427. scroll_to_item(i);
  428. control->queue_redraw();
  429. set_input_as_handled();
  430. match_found = true;
  431. break;
  432. }
  433. }
  434. if (!match_found) {
  435. // If the first item is not selectable, try re-searching from the end.
  436. for (int i = items.size() - 1; i >= search_from; i--) {
  437. if (!items[i].separator && !items[i].disabled) {
  438. mouse_over = i;
  439. emit_signal(SNAME("id_focused"), i);
  440. scroll_to_item(i);
  441. control->queue_redraw();
  442. set_input_as_handled();
  443. break;
  444. }
  445. }
  446. }
  447. } else if (p_event->is_action("ui_left", true) && p_event->is_pressed()) {
  448. Node *n = get_parent();
  449. if (n) {
  450. if (Object::cast_to<PopupMenu>(n)) {
  451. hide();
  452. set_input_as_handled();
  453. } else if (Object::cast_to<MenuBar>(n)) {
  454. Object::cast_to<MenuBar>(n)->gui_input(p_event);
  455. set_input_as_handled();
  456. return;
  457. }
  458. }
  459. } else if (p_event->is_action("ui_right", true) && p_event->is_pressed()) {
  460. if (mouse_over >= 0 && mouse_over < items.size() && !items[mouse_over].separator && !items[mouse_over].submenu.is_empty() && submenu_over != mouse_over) {
  461. _activate_submenu(mouse_over, true);
  462. set_input_as_handled();
  463. } else {
  464. Node *n = get_parent();
  465. if (n && Object::cast_to<MenuBar>(n)) {
  466. Object::cast_to<MenuBar>(n)->gui_input(p_event);
  467. set_input_as_handled();
  468. return;
  469. }
  470. }
  471. } else if (p_event->is_action("ui_accept", true) && p_event->is_pressed()) {
  472. if (mouse_over >= 0 && mouse_over < items.size() && !items[mouse_over].separator) {
  473. if (!items[mouse_over].submenu.is_empty() && submenu_over != mouse_over) {
  474. _activate_submenu(mouse_over, true);
  475. } else {
  476. activate_item(mouse_over);
  477. }
  478. set_input_as_handled();
  479. }
  480. }
  481. }
  482. // Make an area which does not include v scrollbar, so that items are not activated when dragging scrollbar.
  483. Rect2 item_clickable_area = scroll_container->get_rect();
  484. if (scroll_container->get_v_scroll_bar()->is_visible_in_tree()) {
  485. if (is_layout_rtl()) {
  486. item_clickable_area.position.x += scroll_container->get_v_scroll_bar()->get_size().width;
  487. } else {
  488. item_clickable_area.size.width -= scroll_container->get_v_scroll_bar()->get_size().width;
  489. }
  490. }
  491. if (m.is_valid() && drag_to_press) {
  492. BitField<MouseButtonMask> initial_button_mask = m->get_button_mask();
  493. if (!initial_button_mask.has_flag(mouse_button_to_mask(MouseButton::LEFT)) && !initial_button_mask.has_flag(mouse_button_to_mask(MouseButton::RIGHT))) {
  494. mouse_is_pressed = false;
  495. }
  496. if (!item_clickable_area.has_point(m->get_position()) && !mouse_is_pressed) {
  497. drag_to_press = false;
  498. }
  499. }
  500. if ((b.is_valid() && b->is_pressed()) || (!mouse_is_pressed && drag_to_press)) {
  501. if (b.is_valid()) {
  502. MouseButton button_idx = b->get_button_index();
  503. if (button_idx != MouseButton::LEFT && button_idx != MouseButton::RIGHT) {
  504. return;
  505. }
  506. } else {
  507. uint64_t now = OS::get_singleton()->get_ticks_msec();
  508. uint64_t diff = now - popup_time_msec;
  509. if (diff < 250) {
  510. drag_to_press = false;
  511. return;
  512. }
  513. }
  514. drag_to_press = false;
  515. int over = -1;
  516. if (m.is_valid()) {
  517. over = _get_mouse_over(m->get_position());
  518. } else if (b.is_valid()) {
  519. over = _get_mouse_over(b->get_position());
  520. }
  521. if (over < 0) {
  522. hide();
  523. return;
  524. }
  525. if (items[over].separator || items[over].disabled) {
  526. return;
  527. }
  528. if (!items[over].submenu.is_empty()) {
  529. _activate_submenu(over);
  530. return;
  531. }
  532. activate_item(over);
  533. }
  534. if (m.is_valid()) {
  535. if (m->get_velocity().is_zero_approx()) {
  536. return;
  537. }
  538. activated_by_keyboard = false;
  539. for (const Rect2 &E : autohide_areas) {
  540. if (!Rect2(Point2(), get_size()).has_point(m->get_position()) && E.has_point(m->get_position())) {
  541. _close_pressed();
  542. return;
  543. }
  544. }
  545. if (!item_clickable_area.has_point(m->get_position())) {
  546. return;
  547. }
  548. int over = _get_mouse_over(m->get_position());
  549. int id = (over < 0 || items[over].separator || items[over].disabled) ? -1 : (items[over].id >= 0 ? items[over].id : over);
  550. if (id < 0) {
  551. mouse_over = -1;
  552. control->queue_redraw();
  553. return;
  554. }
  555. if (!items[over].submenu.is_empty() && submenu_over != over) {
  556. submenu_over = over;
  557. submenu_timer->start();
  558. }
  559. if (over != mouse_over) {
  560. mouse_over = over;
  561. control->queue_redraw();
  562. }
  563. }
  564. Ref<InputEventKey> k = p_event;
  565. if (allow_search && k.is_valid() && k->get_unicode() && k->is_pressed()) {
  566. uint64_t now = OS::get_singleton()->get_ticks_msec();
  567. uint64_t diff = now - search_time_msec;
  568. uint64_t max_interval = uint64_t(GLOBAL_GET("gui/timers/incremental_search_max_interval_msec"));
  569. search_time_msec = now;
  570. if (diff > max_interval) {
  571. search_string = "";
  572. }
  573. if (String::chr(k->get_unicode()) != search_string) {
  574. search_string += String::chr(k->get_unicode());
  575. }
  576. for (int i = mouse_over + 1; i <= items.size(); i++) {
  577. if (i == items.size()) {
  578. if (mouse_over <= 0) {
  579. break;
  580. } else {
  581. i = 0;
  582. }
  583. }
  584. if (i == mouse_over) {
  585. break;
  586. }
  587. if (items[i].text.findn(search_string) == 0) {
  588. mouse_over = i;
  589. emit_signal(SNAME("id_focused"), i);
  590. scroll_to_item(i);
  591. control->queue_redraw();
  592. set_input_as_handled();
  593. break;
  594. }
  595. }
  596. }
  597. }
  598. void PopupMenu::_draw_items() {
  599. control->set_custom_minimum_size(Size2(0, _get_items_total_height()));
  600. RID ci = control->get_canvas_item();
  601. Size2 margin_size;
  602. margin_size.width = margin_container->get_margin_size(SIDE_LEFT) + margin_container->get_margin_size(SIDE_RIGHT);
  603. margin_size.height = margin_container->get_margin_size(SIDE_TOP) + margin_container->get_margin_size(SIDE_BOTTOM);
  604. // Space between the item content and the sides of popup menu.
  605. bool rtl = control->is_layout_rtl();
  606. // In Item::checkable_type enum order (less the non-checkable member), with disabled repeated at the end.
  607. Ref<Texture2D> check[] = { theme_cache.checked, theme_cache.radio_checked, theme_cache.checked_disabled, theme_cache.radio_checked_disabled };
  608. Ref<Texture2D> uncheck[] = { theme_cache.unchecked, theme_cache.radio_unchecked, theme_cache.unchecked_disabled, theme_cache.radio_unchecked_disabled };
  609. Ref<Texture2D> submenu;
  610. if (rtl) {
  611. submenu = theme_cache.submenu_mirrored;
  612. } else {
  613. submenu = theme_cache.submenu;
  614. }
  615. float scroll_width = scroll_container->get_v_scroll_bar()->is_visible_in_tree() ? scroll_container->get_v_scroll_bar()->get_size().width : 0;
  616. float display_width = control->get_size().width - scroll_width;
  617. // Find the widest icon and whether any items have a checkbox, and store the offsets for each.
  618. float icon_ofs = 0.0;
  619. bool has_check = false;
  620. for (int i = 0; i < items.size(); i++) {
  621. if (items[i].separator) {
  622. continue;
  623. }
  624. Size2 icon_size = _get_item_icon_size(i);
  625. icon_ofs = MAX(icon_size.width, icon_ofs);
  626. if (items[i].checkable_type) {
  627. has_check = true;
  628. }
  629. }
  630. if (icon_ofs > 0.0) {
  631. icon_ofs += theme_cache.h_separation;
  632. }
  633. float check_ofs = 0.0;
  634. if (has_check) {
  635. for (int i = 0; i < 4; i++) {
  636. check_ofs = MAX(check_ofs, check[i]->get_width());
  637. check_ofs = MAX(check_ofs, uncheck[i]->get_width());
  638. }
  639. check_ofs += theme_cache.h_separation;
  640. }
  641. Point2 ofs;
  642. // Loop through all items and draw each.
  643. for (int i = 0; i < items.size(); i++) {
  644. // For the first item only add half a separation. For all other items, add a whole separation to the offset.
  645. ofs.y += i > 0 ? theme_cache.v_separation : (float)theme_cache.v_separation / 2;
  646. _shape_item(i);
  647. Point2 item_ofs = ofs;
  648. Size2 icon_size = _get_item_icon_size(i);
  649. float h = _get_item_height(i);
  650. if (i == mouse_over) {
  651. if (rtl) {
  652. theme_cache.hover_style->draw(ci, Rect2(item_ofs + Point2(scroll_width, -theme_cache.v_separation / 2), Size2(display_width, h + theme_cache.v_separation)));
  653. } else {
  654. theme_cache.hover_style->draw(ci, Rect2(item_ofs + Point2(0, -theme_cache.v_separation / 2), Size2(display_width, h + theme_cache.v_separation)));
  655. }
  656. }
  657. String text = items[i].xl_text;
  658. // Separator
  659. item_ofs.x += items[i].indent * theme_cache.indent;
  660. if (items[i].separator) {
  661. if (!text.is_empty() || items[i].icon.is_valid()) {
  662. int content_size = items[i].text_buf->get_size().width + theme_cache.h_separation * 2;
  663. if (items[i].icon.is_valid()) {
  664. content_size += icon_size.width + theme_cache.h_separation;
  665. }
  666. int content_center = display_width / 2;
  667. int content_left = content_center - content_size / 2;
  668. int content_right = content_center + content_size / 2;
  669. if (content_left > item_ofs.x) {
  670. int sep_h = theme_cache.labeled_separator_left->get_minimum_size().height;
  671. int sep_ofs = Math::floor((h - sep_h) / 2.0);
  672. theme_cache.labeled_separator_left->draw(ci, Rect2(item_ofs + Point2(0, sep_ofs), Size2(MAX(0, content_left - item_ofs.x), sep_h)));
  673. }
  674. if (content_right < display_width) {
  675. int sep_h = theme_cache.labeled_separator_right->get_minimum_size().height;
  676. int sep_ofs = Math::floor((h - sep_h) / 2.0);
  677. theme_cache.labeled_separator_right->draw(ci, Rect2(Point2(content_right, item_ofs.y + sep_ofs), Size2(MAX(0, display_width - content_right), sep_h)));
  678. }
  679. } else {
  680. int sep_h = theme_cache.separator_style->get_minimum_size().height;
  681. int sep_ofs = Math::floor((h - sep_h) / 2.0);
  682. theme_cache.separator_style->draw(ci, Rect2(item_ofs + Point2(0, sep_ofs), Size2(display_width, sep_h)));
  683. }
  684. }
  685. Color icon_color(1, 1, 1, items[i].disabled && !items[i].separator ? 0.5 : 1);
  686. icon_color *= items[i].icon_modulate;
  687. // For non-separator items, add some padding for the content.
  688. if (!items[i].separator) {
  689. item_ofs.x += theme_cache.item_start_padding;
  690. }
  691. // Checkboxes
  692. if (items[i].checkable_type && !items[i].separator) {
  693. int disabled = int(items[i].disabled) * 2;
  694. Texture2D *icon = (items[i].checked ? check[items[i].checkable_type - 1 + disabled] : uncheck[items[i].checkable_type - 1 + disabled]).ptr();
  695. if (rtl) {
  696. 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);
  697. } else {
  698. icon->draw(ci, item_ofs + Point2(0, Math::floor((h - icon->get_height()) / 2.0)), icon_color);
  699. }
  700. }
  701. int separator_ofs = (display_width - items[i].text_buf->get_size().width) / 2;
  702. // Icon
  703. if (items[i].icon.is_valid()) {
  704. const Point2 icon_offset = Point2(0, Math::floor((h - icon_size.height) / 2.0));
  705. Point2 icon_pos;
  706. if (items[i].separator) {
  707. separator_ofs -= (icon_size.width + theme_cache.h_separation) / 2;
  708. if (rtl) {
  709. icon_pos = Size2(control->get_size().width - item_ofs.x - separator_ofs - icon_size.width, item_ofs.y);
  710. } else {
  711. icon_pos = item_ofs + Size2(separator_ofs, 0);
  712. separator_ofs += icon_size.width + theme_cache.h_separation;
  713. }
  714. } else {
  715. if (rtl) {
  716. icon_pos = Size2(control->get_size().width - item_ofs.x - check_ofs - icon_size.width, item_ofs.y);
  717. } else {
  718. icon_pos = item_ofs + Size2(check_ofs, 0);
  719. }
  720. }
  721. items[i].icon->draw_rect(ci, Rect2(icon_pos + icon_offset, icon_size), false, icon_color);
  722. }
  723. // Submenu arrow on right hand side.
  724. if (!items[i].submenu.is_empty()) {
  725. if (rtl) {
  726. submenu->draw(ci, Point2(scroll_width + theme_cache.panel_style->get_margin(SIDE_LEFT) + theme_cache.item_end_padding, item_ofs.y + Math::floor(h - submenu->get_height()) / 2), icon_color);
  727. } else {
  728. submenu->draw(ci, Point2(display_width - theme_cache.panel_style->get_margin(SIDE_RIGHT) - submenu->get_width() - theme_cache.item_end_padding, item_ofs.y + Math::floor(h - submenu->get_height()) / 2), icon_color);
  729. }
  730. }
  731. // Text
  732. if (items[i].separator) {
  733. if (!text.is_empty()) {
  734. Vector2 text_pos = Point2(separator_ofs, item_ofs.y + Math::floor((h - items[i].text_buf->get_size().y) / 2.0));
  735. if (theme_cache.font_separator_outline_size > 0 && theme_cache.font_separator_outline_color.a > 0) {
  736. items[i].text_buf->draw_outline(ci, text_pos, theme_cache.font_separator_outline_size, theme_cache.font_separator_outline_color);
  737. }
  738. items[i].text_buf->draw(ci, text_pos, theme_cache.font_separator_color);
  739. }
  740. } else {
  741. item_ofs.x += icon_ofs + check_ofs;
  742. if (rtl) {
  743. 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));
  744. if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) {
  745. items[i].text_buf->draw_outline(ci, text_pos, theme_cache.font_outline_size, theme_cache.font_outline_color);
  746. }
  747. items[i].text_buf->draw(ci, text_pos, items[i].disabled ? theme_cache.font_disabled_color : (i == mouse_over ? theme_cache.font_hover_color : theme_cache.font_color));
  748. } else {
  749. Vector2 text_pos = item_ofs + Point2(0, Math::floor((h - items[i].text_buf->get_size().y) / 2.0));
  750. if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) {
  751. items[i].text_buf->draw_outline(ci, text_pos, theme_cache.font_outline_size, theme_cache.font_outline_color);
  752. }
  753. items[i].text_buf->draw(ci, text_pos, items[i].disabled ? theme_cache.font_disabled_color : (i == mouse_over ? theme_cache.font_hover_color : theme_cache.font_color));
  754. }
  755. }
  756. // Accelerator / Shortcut
  757. if (items[i].accel != Key::NONE || (items[i].shortcut.is_valid() && items[i].shortcut->has_valid_event())) {
  758. if (rtl) {
  759. item_ofs.x = scroll_width + theme_cache.panel_style->get_margin(SIDE_LEFT) + theme_cache.item_end_padding;
  760. } else {
  761. item_ofs.x = display_width - theme_cache.panel_style->get_margin(SIDE_RIGHT) - items[i].accel_text_buf->get_size().x - theme_cache.item_end_padding;
  762. }
  763. Vector2 text_pos = item_ofs + Point2(0, Math::floor((h - items[i].text_buf->get_size().y) / 2.0));
  764. if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) {
  765. items[i].accel_text_buf->draw_outline(ci, text_pos, theme_cache.font_outline_size, theme_cache.font_outline_color);
  766. }
  767. items[i].accel_text_buf->draw(ci, text_pos, i == mouse_over ? theme_cache.font_hover_color : theme_cache.font_accelerator_color);
  768. }
  769. // Cache the item vertical offset from the first item and the height.
  770. items.write[i]._ofs_cache = ofs.y;
  771. items.write[i]._height_cache = h;
  772. ofs.y += h;
  773. }
  774. }
  775. void PopupMenu::_draw_background() {
  776. RID ci2 = margin_container->get_canvas_item();
  777. theme_cache.panel_style->draw(ci2, Rect2(Point2(), margin_container->get_size()));
  778. }
  779. void PopupMenu::_minimum_lifetime_timeout() {
  780. close_allowed = true;
  781. // If the mouse still isn't in this popup after timer expires, close.
  782. if (!activated_by_keyboard && !get_visible_rect().has_point(get_mouse_position())) {
  783. _close_pressed();
  784. }
  785. }
  786. void PopupMenu::_close_pressed() {
  787. // Only apply minimum lifetime to submenus.
  788. PopupMenu *parent_pum = Object::cast_to<PopupMenu>(get_parent());
  789. if (!parent_pum) {
  790. Popup::_close_pressed();
  791. return;
  792. }
  793. // If the timer has expired, close. If timer is still running, do nothing.
  794. if (close_allowed) {
  795. close_allowed = false;
  796. Popup::_close_pressed();
  797. } else if (minimum_lifetime_timer->is_stopped()) {
  798. minimum_lifetime_timer->start();
  799. }
  800. }
  801. void PopupMenu::_shape_item(int p_idx) {
  802. if (items.write[p_idx].dirty) {
  803. items.write[p_idx].text_buf->clear();
  804. Ref<Font> font = items[p_idx].separator ? theme_cache.font_separator : theme_cache.font;
  805. int font_size = items[p_idx].separator ? theme_cache.font_separator_size : theme_cache.font_size;
  806. if (items[p_idx].text_direction == Control::TEXT_DIRECTION_INHERITED) {
  807. items.write[p_idx].text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  808. } else {
  809. items.write[p_idx].text_buf->set_direction((TextServer::Direction)items[p_idx].text_direction);
  810. }
  811. items.write[p_idx].text_buf->add_string(items.write[p_idx].xl_text, font, font_size, items[p_idx].language);
  812. items.write[p_idx].accel_text_buf->clear();
  813. items.write[p_idx].accel_text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  814. items.write[p_idx].accel_text_buf->add_string(_get_accel_text(items.write[p_idx]), font, font_size);
  815. items.write[p_idx].dirty = false;
  816. }
  817. }
  818. void PopupMenu::_menu_changed() {
  819. emit_signal(SNAME("menu_changed"));
  820. }
  821. void PopupMenu::add_child_notify(Node *p_child) {
  822. Window::add_child_notify(p_child);
  823. if (Object::cast_to<PopupMenu>(p_child) && !global_menu_name.is_empty()) {
  824. String node_name = p_child->get_name();
  825. PopupMenu *pm = Object::cast_to<PopupMenu>(get_node_or_null(node_name));
  826. for (int i = 0; i < items.size(); i++) {
  827. if (items[i].submenu == node_name) {
  828. String submenu_name = pm->bind_global_menu();
  829. DisplayServer::get_singleton()->global_menu_set_item_submenu(global_menu_name, i, submenu_name);
  830. items.write[i].submenu_bound = true;
  831. }
  832. }
  833. }
  834. _menu_changed();
  835. }
  836. void PopupMenu::remove_child_notify(Node *p_child) {
  837. Window::remove_child_notify(p_child);
  838. PopupMenu *pm = Object::cast_to<PopupMenu>(p_child);
  839. if (!pm) {
  840. return;
  841. }
  842. if (Object::cast_to<PopupMenu>(p_child) && !global_menu_name.is_empty()) {
  843. String node_name = p_child->get_name();
  844. for (int i = 0; i < items.size(); i++) {
  845. if (items[i].submenu == node_name) {
  846. DisplayServer::get_singleton()->global_menu_set_item_submenu(global_menu_name, i, String());
  847. items.write[i].submenu_bound = false;
  848. }
  849. }
  850. pm->unbind_global_menu();
  851. }
  852. _menu_changed();
  853. }
  854. void PopupMenu::_notification(int p_what) {
  855. switch (p_what) {
  856. case NOTIFICATION_ENTER_TREE: {
  857. PopupMenu *pm = Object::cast_to<PopupMenu>(get_parent());
  858. if (pm) {
  859. // Inherit submenu's popup delay time from parent menu.
  860. float pm_delay = pm->get_submenu_popup_delay();
  861. set_submenu_popup_delay(pm_delay);
  862. }
  863. if (!is_embedded()) {
  864. set_flag(FLAG_NO_FOCUS, true);
  865. }
  866. if (system_menu_name.length() > 0) {
  867. bind_global_menu();
  868. }
  869. } break;
  870. case NOTIFICATION_EXIT_TREE: {
  871. if (system_menu_name.length() > 0) {
  872. unbind_global_menu();
  873. }
  874. } break;
  875. case NOTIFICATION_THEME_CHANGED:
  876. case Control::NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  877. case NOTIFICATION_TRANSLATION_CHANGED: {
  878. DisplayServer *ds = DisplayServer::get_singleton();
  879. bool is_global = !global_menu_name.is_empty();
  880. for (int i = 0; i < items.size(); i++) {
  881. Item &item = items.write[i];
  882. item.xl_text = atr(item.text);
  883. item.dirty = true;
  884. if (is_global) {
  885. ds->global_menu_set_item_text(global_menu_name, i, item.xl_text);
  886. }
  887. _shape_item(i);
  888. }
  889. child_controls_changed();
  890. _menu_changed();
  891. control->queue_redraw();
  892. } break;
  893. case NOTIFICATION_WM_MOUSE_ENTER: {
  894. grab_focus();
  895. } break;
  896. case NOTIFICATION_WM_MOUSE_EXIT: {
  897. if (mouse_over >= 0 && (items[mouse_over].submenu.is_empty() || submenu_over != -1)) {
  898. mouse_over = -1;
  899. control->queue_redraw();
  900. }
  901. } break;
  902. case NOTIFICATION_INTERNAL_PROCESS: {
  903. Input *input = Input::get_singleton();
  904. if (input->is_action_just_released("ui_up") || input->is_action_just_released("ui_down")) {
  905. gamepad_event_delay_ms = DEFAULT_GAMEPAD_EVENT_DELAY_MS;
  906. set_process_internal(false);
  907. return;
  908. }
  909. gamepad_event_delay_ms -= get_process_delta_time();
  910. if (gamepad_event_delay_ms <= 0) {
  911. if (input->is_action_pressed("ui_down")) {
  912. gamepad_event_delay_ms = GAMEPAD_EVENT_REPEAT_RATE_MS + gamepad_event_delay_ms;
  913. int search_from = mouse_over + 1;
  914. if (search_from >= items.size()) {
  915. search_from = 0;
  916. }
  917. bool match_found = false;
  918. for (int i = search_from; i < items.size(); i++) {
  919. if (!items[i].separator && !items[i].disabled) {
  920. mouse_over = i;
  921. emit_signal(SNAME("id_focused"), i);
  922. scroll_to_item(i);
  923. control->queue_redraw();
  924. match_found = true;
  925. break;
  926. }
  927. }
  928. if (!match_found) {
  929. // If the last item is not selectable, try re-searching from the start.
  930. for (int i = 0; i < search_from; i++) {
  931. if (!items[i].separator && !items[i].disabled) {
  932. mouse_over = i;
  933. emit_signal(SNAME("id_focused"), i);
  934. scroll_to_item(i);
  935. control->queue_redraw();
  936. break;
  937. }
  938. }
  939. }
  940. }
  941. if (input->is_action_pressed("ui_up")) {
  942. gamepad_event_delay_ms = GAMEPAD_EVENT_REPEAT_RATE_MS + gamepad_event_delay_ms;
  943. int search_from = mouse_over - 1;
  944. if (search_from < 0) {
  945. search_from = items.size() - 1;
  946. }
  947. bool match_found = false;
  948. for (int i = search_from; i >= 0; i--) {
  949. if (!items[i].separator && !items[i].disabled) {
  950. mouse_over = i;
  951. emit_signal(SNAME("id_focused"), i);
  952. scroll_to_item(i);
  953. control->queue_redraw();
  954. match_found = true;
  955. break;
  956. }
  957. }
  958. if (!match_found) {
  959. // If the first item is not selectable, try re-searching from the end.
  960. for (int i = items.size() - 1; i >= search_from; i--) {
  961. if (!items[i].separator && !items[i].disabled) {
  962. mouse_over = i;
  963. emit_signal(SNAME("id_focused"), i);
  964. scroll_to_item(i);
  965. control->queue_redraw();
  966. break;
  967. }
  968. }
  969. }
  970. }
  971. }
  972. // Only used when using operating system windows.
  973. if (!activated_by_keyboard && !is_embedded() && autohide_areas.size()) {
  974. Point2 mouse_pos = DisplayServer::get_singleton()->mouse_get_position();
  975. mouse_pos -= get_position();
  976. for (const Rect2 &E : autohide_areas) {
  977. if (!Rect2(Point2(), get_size()).has_point(mouse_pos) && E.has_point(mouse_pos)) {
  978. _close_pressed();
  979. return;
  980. }
  981. }
  982. }
  983. } break;
  984. case NOTIFICATION_VISIBILITY_CHANGED: {
  985. if (!is_visible()) {
  986. if (mouse_over >= 0) {
  987. mouse_over = -1;
  988. control->queue_redraw();
  989. }
  990. for (int i = 0; i < items.size(); i++) {
  991. if (items[i].submenu.is_empty()) {
  992. continue;
  993. }
  994. Node *n = get_node(items[i].submenu);
  995. if (!n) {
  996. continue;
  997. }
  998. PopupMenu *pm = Object::cast_to<PopupMenu>(n);
  999. if (!pm || !pm->is_visible()) {
  1000. continue;
  1001. }
  1002. pm->hide();
  1003. }
  1004. set_process_internal(false);
  1005. } else {
  1006. if (!is_embedded()) {
  1007. set_process_internal(true);
  1008. }
  1009. // Set margin on the margin container
  1010. margin_container->begin_bulk_theme_override();
  1011. margin_container->add_theme_constant_override("margin_left", theme_cache.panel_style->get_margin(Side::SIDE_LEFT));
  1012. margin_container->add_theme_constant_override("margin_top", theme_cache.panel_style->get_margin(Side::SIDE_TOP));
  1013. margin_container->add_theme_constant_override("margin_right", theme_cache.panel_style->get_margin(Side::SIDE_RIGHT));
  1014. margin_container->add_theme_constant_override("margin_bottom", theme_cache.panel_style->get_margin(Side::SIDE_BOTTOM));
  1015. margin_container->end_bulk_theme_override();
  1016. }
  1017. } break;
  1018. }
  1019. }
  1020. /* Methods to add items with or without icon, checkbox, shortcut.
  1021. * Be sure to keep them in sync when adding new properties in the Item struct.
  1022. */
  1023. #define ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel) \
  1024. item.text = p_label; \
  1025. item.xl_text = atr(p_label); \
  1026. item.id = p_id == -1 ? items.size() : p_id; \
  1027. item.accel = p_accel;
  1028. void PopupMenu::add_item(const String &p_label, int p_id, Key p_accel) {
  1029. Item item;
  1030. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  1031. items.push_back(item);
  1032. if (!global_menu_name.is_empty()) {
  1033. DisplayServer *ds = DisplayServer::get_singleton();
  1034. int index = ds->global_menu_add_item(global_menu_name, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1);
  1035. if (item.accel != Key::NONE) {
  1036. ds->global_menu_set_item_accelerator(global_menu_name, index, item.accel);
  1037. }
  1038. }
  1039. _shape_item(items.size() - 1);
  1040. control->queue_redraw();
  1041. child_controls_changed();
  1042. notify_property_list_changed();
  1043. _menu_changed();
  1044. }
  1045. void PopupMenu::add_icon_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id, Key p_accel) {
  1046. Item item;
  1047. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  1048. item.icon = p_icon;
  1049. items.push_back(item);
  1050. if (!global_menu_name.is_empty()) {
  1051. DisplayServer *ds = DisplayServer::get_singleton();
  1052. int index = ds->global_menu_add_item(global_menu_name, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1);
  1053. if (item.accel != Key::NONE) {
  1054. ds->global_menu_set_item_accelerator(global_menu_name, index, item.accel);
  1055. }
  1056. ds->global_menu_set_item_icon(global_menu_name, index, item.icon);
  1057. }
  1058. _shape_item(items.size() - 1);
  1059. control->queue_redraw();
  1060. child_controls_changed();
  1061. notify_property_list_changed();
  1062. _menu_changed();
  1063. }
  1064. void PopupMenu::add_check_item(const String &p_label, int p_id, Key p_accel) {
  1065. Item item;
  1066. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  1067. item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX;
  1068. items.push_back(item);
  1069. if (!global_menu_name.is_empty()) {
  1070. DisplayServer *ds = DisplayServer::get_singleton();
  1071. int index = ds->global_menu_add_item(global_menu_name, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1);
  1072. if (item.accel != Key::NONE) {
  1073. ds->global_menu_set_item_accelerator(global_menu_name, index, item.accel);
  1074. }
  1075. ds->global_menu_set_item_checkable(global_menu_name, index, true);
  1076. }
  1077. _shape_item(items.size() - 1);
  1078. control->queue_redraw();
  1079. child_controls_changed();
  1080. notify_property_list_changed();
  1081. _menu_changed();
  1082. }
  1083. void PopupMenu::add_icon_check_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id, Key p_accel) {
  1084. Item item;
  1085. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  1086. item.icon = p_icon;
  1087. item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX;
  1088. items.push_back(item);
  1089. if (!global_menu_name.is_empty()) {
  1090. DisplayServer *ds = DisplayServer::get_singleton();
  1091. int index = ds->global_menu_add_item(global_menu_name, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1);
  1092. if (item.accel != Key::NONE) {
  1093. ds->global_menu_set_item_accelerator(global_menu_name, index, item.accel);
  1094. }
  1095. ds->global_menu_set_item_icon(global_menu_name, index, item.icon);
  1096. ds->global_menu_set_item_checkable(global_menu_name, index, true);
  1097. }
  1098. _shape_item(items.size() - 1);
  1099. control->queue_redraw();
  1100. child_controls_changed();
  1101. notify_property_list_changed();
  1102. _menu_changed();
  1103. }
  1104. void PopupMenu::add_radio_check_item(const String &p_label, int p_id, Key p_accel) {
  1105. Item item;
  1106. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  1107. item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON;
  1108. items.push_back(item);
  1109. if (!global_menu_name.is_empty()) {
  1110. DisplayServer *ds = DisplayServer::get_singleton();
  1111. int index = ds->global_menu_add_item(global_menu_name, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1);
  1112. if (item.accel != Key::NONE) {
  1113. ds->global_menu_set_item_accelerator(global_menu_name, index, item.accel);
  1114. }
  1115. ds->global_menu_set_item_radio_checkable(global_menu_name, index, true);
  1116. }
  1117. _shape_item(items.size() - 1);
  1118. control->queue_redraw();
  1119. child_controls_changed();
  1120. notify_property_list_changed();
  1121. _menu_changed();
  1122. }
  1123. void PopupMenu::add_icon_radio_check_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id, Key p_accel) {
  1124. Item item;
  1125. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  1126. item.icon = p_icon;
  1127. item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON;
  1128. items.push_back(item);
  1129. if (!global_menu_name.is_empty()) {
  1130. DisplayServer *ds = DisplayServer::get_singleton();
  1131. int index = ds->global_menu_add_item(global_menu_name, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1);
  1132. if (item.accel != Key::NONE) {
  1133. ds->global_menu_set_item_accelerator(global_menu_name, index, item.accel);
  1134. }
  1135. ds->global_menu_set_item_icon(global_menu_name, index, item.icon);
  1136. ds->global_menu_set_item_radio_checkable(global_menu_name, index, true);
  1137. }
  1138. _shape_item(items.size() - 1);
  1139. control->queue_redraw();
  1140. child_controls_changed();
  1141. notify_property_list_changed();
  1142. _menu_changed();
  1143. }
  1144. void PopupMenu::add_multistate_item(const String &p_label, int p_max_states, int p_default_state, int p_id, Key p_accel) {
  1145. Item item;
  1146. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  1147. item.max_states = p_max_states;
  1148. item.state = p_default_state;
  1149. items.push_back(item);
  1150. if (!global_menu_name.is_empty()) {
  1151. DisplayServer *ds = DisplayServer::get_singleton();
  1152. int index = ds->global_menu_add_item(global_menu_name, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1);
  1153. if (item.accel != Key::NONE) {
  1154. ds->global_menu_set_item_accelerator(global_menu_name, index, item.accel);
  1155. }
  1156. ds->global_menu_set_item_max_states(global_menu_name, index, item.max_states);
  1157. ds->global_menu_set_item_state(global_menu_name, index, item.state);
  1158. }
  1159. _shape_item(items.size() - 1);
  1160. control->queue_redraw();
  1161. child_controls_changed();
  1162. _menu_changed();
  1163. notify_property_list_changed();
  1164. }
  1165. #define ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, p_allow_echo) \
  1166. ERR_FAIL_COND_MSG(p_shortcut.is_null(), "Cannot add item with invalid Shortcut."); \
  1167. _ref_shortcut(p_shortcut); \
  1168. item.text = p_shortcut->get_name(); \
  1169. item.xl_text = atr(item.text); \
  1170. item.id = p_id == -1 ? items.size() : p_id; \
  1171. item.shortcut = p_shortcut; \
  1172. item.shortcut_is_global = p_global; \
  1173. item.allow_echo = p_allow_echo;
  1174. void PopupMenu::add_shortcut(const Ref<Shortcut> &p_shortcut, int p_id, bool p_global, bool p_allow_echo) {
  1175. Item item;
  1176. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, p_allow_echo);
  1177. items.push_back(item);
  1178. if (!global_menu_name.is_empty()) {
  1179. DisplayServer *ds = DisplayServer::get_singleton();
  1180. int index = ds->global_menu_add_item(global_menu_name, item.xl_text, callable_mp(this, &PopupMenu::activate_item), p_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), items.size() - 1);
  1181. if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) {
  1182. Array events = item.shortcut->get_events();
  1183. for (int j = 0; j < events.size(); j++) {
  1184. Ref<InputEventKey> ie = events[j];
  1185. if (ie.is_valid() && _set_item_accelerator(index, ie)) {
  1186. break;
  1187. }
  1188. }
  1189. }
  1190. }
  1191. _shape_item(items.size() - 1);
  1192. control->queue_redraw();
  1193. child_controls_changed();
  1194. notify_property_list_changed();
  1195. _menu_changed();
  1196. }
  1197. void PopupMenu::add_icon_shortcut(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id, bool p_global, bool p_allow_echo) {
  1198. Item item;
  1199. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, p_allow_echo);
  1200. item.icon = p_icon;
  1201. items.push_back(item);
  1202. if (!global_menu_name.is_empty()) {
  1203. DisplayServer *ds = DisplayServer::get_singleton();
  1204. int index = ds->global_menu_add_item(global_menu_name, item.xl_text, callable_mp(this, &PopupMenu::activate_item), p_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), items.size() - 1);
  1205. if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) {
  1206. Array events = item.shortcut->get_events();
  1207. for (int j = 0; j < events.size(); j++) {
  1208. Ref<InputEventKey> ie = events[j];
  1209. if (ie.is_valid() && _set_item_accelerator(index, ie)) {
  1210. break;
  1211. }
  1212. }
  1213. }
  1214. ds->global_menu_set_item_icon(global_menu_name, index, item.icon);
  1215. }
  1216. _shape_item(items.size() - 1);
  1217. control->queue_redraw();
  1218. child_controls_changed();
  1219. notify_property_list_changed();
  1220. _menu_changed();
  1221. }
  1222. void PopupMenu::add_check_shortcut(const Ref<Shortcut> &p_shortcut, int p_id, bool p_global) {
  1223. Item item;
  1224. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, false); // Echo for check shortcuts doesn't make sense.
  1225. item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX;
  1226. items.push_back(item);
  1227. if (!global_menu_name.is_empty()) {
  1228. DisplayServer *ds = DisplayServer::get_singleton();
  1229. int index = ds->global_menu_add_item(global_menu_name, item.xl_text, callable_mp(this, &PopupMenu::activate_item), p_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), items.size() - 1);
  1230. if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) {
  1231. Array events = item.shortcut->get_events();
  1232. for (int j = 0; j < events.size(); j++) {
  1233. Ref<InputEventKey> ie = events[j];
  1234. if (ie.is_valid() && _set_item_accelerator(index, ie)) {
  1235. break;
  1236. }
  1237. }
  1238. }
  1239. ds->global_menu_set_item_checkable(global_menu_name, index, true);
  1240. }
  1241. _shape_item(items.size() - 1);
  1242. control->queue_redraw();
  1243. child_controls_changed();
  1244. notify_property_list_changed();
  1245. _menu_changed();
  1246. }
  1247. void PopupMenu::add_icon_check_shortcut(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id, bool p_global) {
  1248. Item item;
  1249. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, false);
  1250. item.icon = p_icon;
  1251. item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX;
  1252. items.push_back(item);
  1253. if (!global_menu_name.is_empty()) {
  1254. DisplayServer *ds = DisplayServer::get_singleton();
  1255. int index = ds->global_menu_add_item(global_menu_name, item.xl_text, callable_mp(this, &PopupMenu::activate_item), p_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), items.size() - 1);
  1256. if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) {
  1257. Array events = item.shortcut->get_events();
  1258. for (int j = 0; j < events.size(); j++) {
  1259. Ref<InputEventKey> ie = events[j];
  1260. if (ie.is_valid() && _set_item_accelerator(index, ie)) {
  1261. break;
  1262. }
  1263. }
  1264. }
  1265. ds->global_menu_set_item_icon(global_menu_name, index, item.icon);
  1266. ds->global_menu_set_item_checkable(global_menu_name, index, true);
  1267. }
  1268. _shape_item(items.size() - 1);
  1269. control->queue_redraw();
  1270. child_controls_changed();
  1271. notify_property_list_changed();
  1272. _menu_changed();
  1273. }
  1274. void PopupMenu::add_radio_check_shortcut(const Ref<Shortcut> &p_shortcut, int p_id, bool p_global) {
  1275. Item item;
  1276. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, false);
  1277. item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON;
  1278. items.push_back(item);
  1279. if (!global_menu_name.is_empty()) {
  1280. DisplayServer *ds = DisplayServer::get_singleton();
  1281. int index = ds->global_menu_add_item(global_menu_name, item.xl_text, callable_mp(this, &PopupMenu::activate_item), p_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), items.size() - 1);
  1282. if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) {
  1283. Array events = item.shortcut->get_events();
  1284. for (int j = 0; j < events.size(); j++) {
  1285. Ref<InputEventKey> ie = events[j];
  1286. if (ie.is_valid() && _set_item_accelerator(index, ie)) {
  1287. break;
  1288. }
  1289. }
  1290. }
  1291. ds->global_menu_set_item_radio_checkable(global_menu_name, index, true);
  1292. }
  1293. _shape_item(items.size() - 1);
  1294. control->queue_redraw();
  1295. child_controls_changed();
  1296. notify_property_list_changed();
  1297. _menu_changed();
  1298. }
  1299. void PopupMenu::add_icon_radio_check_shortcut(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id, bool p_global) {
  1300. Item item;
  1301. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, false);
  1302. item.icon = p_icon;
  1303. item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON;
  1304. items.push_back(item);
  1305. if (!global_menu_name.is_empty()) {
  1306. DisplayServer *ds = DisplayServer::get_singleton();
  1307. int index = ds->global_menu_add_item(global_menu_name, item.xl_text, callable_mp(this, &PopupMenu::activate_item), p_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), items.size() - 1);
  1308. if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) {
  1309. Array events = item.shortcut->get_events();
  1310. for (int j = 0; j < events.size(); j++) {
  1311. Ref<InputEventKey> ie = events[j];
  1312. if (ie.is_valid() && _set_item_accelerator(index, ie)) {
  1313. break;
  1314. }
  1315. }
  1316. }
  1317. ds->global_menu_set_item_icon(global_menu_name, index, item.icon);
  1318. ds->global_menu_set_item_radio_checkable(global_menu_name, index, true);
  1319. }
  1320. _shape_item(items.size() - 1);
  1321. control->queue_redraw();
  1322. child_controls_changed();
  1323. notify_property_list_changed();
  1324. _menu_changed();
  1325. }
  1326. void PopupMenu::add_submenu_item(const String &p_label, const String &p_submenu, int p_id) {
  1327. String submenu_name_safe = p_submenu.replace("@", "_"); // Allow special characters for auto-generated names.
  1328. if (submenu_name_safe.validate_node_name() != submenu_name_safe) {
  1329. ERR_FAIL_MSG(vformat("Invalid node name '%s' for a submenu, the following characters are not allowed:\n%s", p_submenu, String::get_invalid_node_name_characters(true)));
  1330. }
  1331. Item item;
  1332. item.text = p_label;
  1333. item.xl_text = atr(p_label);
  1334. item.id = p_id == -1 ? items.size() : p_id;
  1335. item.submenu = p_submenu;
  1336. items.push_back(item);
  1337. if (!global_menu_name.is_empty()) {
  1338. DisplayServer *ds = DisplayServer::get_singleton();
  1339. int index = ds->global_menu_add_item(global_menu_name, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1);
  1340. PopupMenu *pm = Object::cast_to<PopupMenu>(get_node_or_null(item.submenu)); // Find first menu with this name.
  1341. if (pm) {
  1342. String submenu_name = pm->bind_global_menu();
  1343. ds->global_menu_set_item_submenu(global_menu_name, index, submenu_name);
  1344. items.write[index].submenu_bound = true;
  1345. }
  1346. }
  1347. _shape_item(items.size() - 1);
  1348. control->queue_redraw();
  1349. child_controls_changed();
  1350. notify_property_list_changed();
  1351. _menu_changed();
  1352. }
  1353. #undef ITEM_SETUP_WITH_ACCEL
  1354. #undef ITEM_SETUP_WITH_SHORTCUT
  1355. /* Methods to modify existing items. */
  1356. void PopupMenu::set_item_text(int p_idx, const String &p_text) {
  1357. if (p_idx < 0) {
  1358. p_idx += get_item_count();
  1359. }
  1360. ERR_FAIL_INDEX(p_idx, items.size());
  1361. if (items[p_idx].text == p_text) {
  1362. return;
  1363. }
  1364. items.write[p_idx].text = p_text;
  1365. items.write[p_idx].xl_text = atr(p_text);
  1366. items.write[p_idx].dirty = true;
  1367. if (!global_menu_name.is_empty()) {
  1368. DisplayServer::get_singleton()->global_menu_set_item_text(global_menu_name, p_idx, items[p_idx].xl_text);
  1369. }
  1370. _shape_item(p_idx);
  1371. control->queue_redraw();
  1372. child_controls_changed();
  1373. _menu_changed();
  1374. }
  1375. void PopupMenu::set_item_text_direction(int p_idx, Control::TextDirection p_text_direction) {
  1376. if (p_idx < 0) {
  1377. p_idx += get_item_count();
  1378. }
  1379. ERR_FAIL_INDEX(p_idx, items.size());
  1380. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  1381. if (items[p_idx].text_direction != p_text_direction) {
  1382. items.write[p_idx].text_direction = p_text_direction;
  1383. items.write[p_idx].dirty = true;
  1384. control->queue_redraw();
  1385. }
  1386. }
  1387. void PopupMenu::set_item_language(int p_idx, const String &p_language) {
  1388. if (p_idx < 0) {
  1389. p_idx += get_item_count();
  1390. }
  1391. ERR_FAIL_INDEX(p_idx, items.size());
  1392. if (items[p_idx].language != p_language) {
  1393. items.write[p_idx].language = p_language;
  1394. items.write[p_idx].dirty = true;
  1395. control->queue_redraw();
  1396. }
  1397. }
  1398. void PopupMenu::set_item_icon(int p_idx, const Ref<Texture2D> &p_icon) {
  1399. if (p_idx < 0) {
  1400. p_idx += get_item_count();
  1401. }
  1402. ERR_FAIL_INDEX(p_idx, items.size());
  1403. if (items[p_idx].icon == p_icon) {
  1404. return;
  1405. }
  1406. items.write[p_idx].icon = p_icon;
  1407. if (!global_menu_name.is_empty()) {
  1408. DisplayServer::get_singleton()->global_menu_set_item_icon(global_menu_name, p_idx, items[p_idx].icon);
  1409. }
  1410. control->queue_redraw();
  1411. child_controls_changed();
  1412. _menu_changed();
  1413. }
  1414. void PopupMenu::set_item_icon_max_width(int p_idx, int p_width) {
  1415. if (p_idx < 0) {
  1416. p_idx += get_item_count();
  1417. }
  1418. ERR_FAIL_INDEX(p_idx, items.size());
  1419. if (items[p_idx].icon_max_width == p_width) {
  1420. return;
  1421. }
  1422. items.write[p_idx].icon_max_width = p_width;
  1423. control->queue_redraw();
  1424. child_controls_changed();
  1425. _menu_changed();
  1426. }
  1427. void PopupMenu::set_item_icon_modulate(int p_idx, const Color &p_modulate) {
  1428. if (p_idx < 0) {
  1429. p_idx += get_item_count();
  1430. }
  1431. ERR_FAIL_INDEX(p_idx, items.size());
  1432. if (items[p_idx].icon_modulate == p_modulate) {
  1433. return;
  1434. }
  1435. items.write[p_idx].icon_modulate = p_modulate;
  1436. control->queue_redraw();
  1437. }
  1438. void PopupMenu::set_item_checked(int p_idx, bool p_checked) {
  1439. if (p_idx < 0) {
  1440. p_idx += get_item_count();
  1441. }
  1442. ERR_FAIL_INDEX(p_idx, items.size());
  1443. if (items[p_idx].checked == p_checked) {
  1444. return;
  1445. }
  1446. items.write[p_idx].checked = p_checked;
  1447. if (!global_menu_name.is_empty()) {
  1448. DisplayServer::get_singleton()->global_menu_set_item_checked(global_menu_name, p_idx, p_checked);
  1449. }
  1450. control->queue_redraw();
  1451. child_controls_changed();
  1452. _menu_changed();
  1453. }
  1454. void PopupMenu::set_item_id(int p_idx, int p_id) {
  1455. if (p_idx < 0) {
  1456. p_idx += get_item_count();
  1457. }
  1458. ERR_FAIL_INDEX(p_idx, items.size());
  1459. if (items[p_idx].id == p_id) {
  1460. return;
  1461. }
  1462. items.write[p_idx].id = p_id;
  1463. if (!global_menu_name.is_empty()) {
  1464. DisplayServer::get_singleton()->global_menu_set_item_tag(global_menu_name, p_idx, p_id);
  1465. }
  1466. control->queue_redraw();
  1467. child_controls_changed();
  1468. _menu_changed();
  1469. }
  1470. void PopupMenu::set_item_accelerator(int p_idx, Key p_accel) {
  1471. if (p_idx < 0) {
  1472. p_idx += get_item_count();
  1473. }
  1474. ERR_FAIL_INDEX(p_idx, items.size());
  1475. if (items[p_idx].accel == p_accel) {
  1476. return;
  1477. }
  1478. items.write[p_idx].accel = p_accel;
  1479. items.write[p_idx].dirty = true;
  1480. if (!global_menu_name.is_empty()) {
  1481. DisplayServer::get_singleton()->global_menu_set_item_accelerator(global_menu_name, p_idx, p_accel);
  1482. }
  1483. control->queue_redraw();
  1484. child_controls_changed();
  1485. _menu_changed();
  1486. }
  1487. void PopupMenu::set_item_metadata(int p_idx, const Variant &p_meta) {
  1488. if (p_idx < 0) {
  1489. p_idx += get_item_count();
  1490. }
  1491. ERR_FAIL_INDEX(p_idx, items.size());
  1492. if (items[p_idx].metadata == p_meta) {
  1493. return;
  1494. }
  1495. items.write[p_idx].metadata = p_meta;
  1496. child_controls_changed();
  1497. _menu_changed();
  1498. }
  1499. void PopupMenu::set_item_disabled(int p_idx, bool p_disabled) {
  1500. if (p_idx < 0) {
  1501. p_idx += get_item_count();
  1502. }
  1503. ERR_FAIL_INDEX(p_idx, items.size());
  1504. if (items[p_idx].disabled == p_disabled) {
  1505. return;
  1506. }
  1507. items.write[p_idx].disabled = p_disabled;
  1508. if (!global_menu_name.is_empty()) {
  1509. DisplayServer::get_singleton()->global_menu_set_item_disabled(global_menu_name, p_idx, p_disabled);
  1510. }
  1511. control->queue_redraw();
  1512. child_controls_changed();
  1513. _menu_changed();
  1514. }
  1515. void PopupMenu::set_item_submenu(int p_idx, const String &p_submenu) {
  1516. if (p_idx < 0) {
  1517. p_idx += get_item_count();
  1518. }
  1519. ERR_FAIL_INDEX(p_idx, items.size());
  1520. if (items[p_idx].submenu == p_submenu) {
  1521. return;
  1522. }
  1523. if (!global_menu_name.is_empty()) {
  1524. if (items[p_idx].submenu_bound) {
  1525. PopupMenu *pm = Object::cast_to<PopupMenu>(get_node_or_null(items[p_idx].submenu));
  1526. if (pm) {
  1527. DisplayServer::get_singleton()->global_menu_set_item_submenu(global_menu_name, p_idx, String());
  1528. pm->unbind_global_menu();
  1529. }
  1530. items.write[p_idx].submenu_bound = false;
  1531. }
  1532. }
  1533. items.write[p_idx].submenu = p_submenu;
  1534. if (!global_menu_name.is_empty()) {
  1535. if (!items[p_idx].submenu.is_empty()) {
  1536. PopupMenu *pm = Object::cast_to<PopupMenu>(get_node_or_null(items[p_idx].submenu));
  1537. if (pm) {
  1538. String submenu_name = pm->bind_global_menu();
  1539. DisplayServer::get_singleton()->global_menu_set_item_submenu(global_menu_name, p_idx, submenu_name);
  1540. items.write[p_idx].submenu_bound = true;
  1541. }
  1542. }
  1543. }
  1544. control->queue_redraw();
  1545. child_controls_changed();
  1546. _menu_changed();
  1547. }
  1548. void PopupMenu::toggle_item_checked(int p_idx) {
  1549. ERR_FAIL_INDEX(p_idx, items.size());
  1550. items.write[p_idx].checked = !items[p_idx].checked;
  1551. if (!global_menu_name.is_empty()) {
  1552. DisplayServer::get_singleton()->global_menu_set_item_checked(global_menu_name, p_idx, items[p_idx].checked);
  1553. }
  1554. control->queue_redraw();
  1555. child_controls_changed();
  1556. _menu_changed();
  1557. }
  1558. String PopupMenu::get_item_text(int p_idx) const {
  1559. ERR_FAIL_INDEX_V(p_idx, items.size(), "");
  1560. return items[p_idx].text;
  1561. }
  1562. String PopupMenu::get_item_xl_text(int p_idx) const {
  1563. ERR_FAIL_INDEX_V(p_idx, items.size(), "");
  1564. return items[p_idx].xl_text;
  1565. }
  1566. Control::TextDirection PopupMenu::get_item_text_direction(int p_idx) const {
  1567. ERR_FAIL_INDEX_V(p_idx, items.size(), Control::TEXT_DIRECTION_INHERITED);
  1568. return items[p_idx].text_direction;
  1569. }
  1570. String PopupMenu::get_item_language(int p_idx) const {
  1571. ERR_FAIL_INDEX_V(p_idx, items.size(), "");
  1572. return items[p_idx].language;
  1573. }
  1574. int PopupMenu::get_item_idx_from_text(const String &text) const {
  1575. for (int idx = 0; idx < items.size(); idx++) {
  1576. if (items[idx].text == text) {
  1577. return idx;
  1578. }
  1579. }
  1580. return -1;
  1581. }
  1582. Ref<Texture2D> PopupMenu::get_item_icon(int p_idx) const {
  1583. ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<Texture2D>());
  1584. return items[p_idx].icon;
  1585. }
  1586. int PopupMenu::get_item_icon_max_width(int p_idx) const {
  1587. ERR_FAIL_INDEX_V(p_idx, items.size(), 0);
  1588. return items[p_idx].icon_max_width;
  1589. }
  1590. Color PopupMenu::get_item_icon_modulate(int p_idx) const {
  1591. ERR_FAIL_INDEX_V(p_idx, items.size(), Color());
  1592. return items[p_idx].icon_modulate;
  1593. }
  1594. Key PopupMenu::get_item_accelerator(int p_idx) const {
  1595. ERR_FAIL_INDEX_V(p_idx, items.size(), Key::NONE);
  1596. return items[p_idx].accel;
  1597. }
  1598. Variant PopupMenu::get_item_metadata(int p_idx) const {
  1599. ERR_FAIL_INDEX_V(p_idx, items.size(), Variant());
  1600. return items[p_idx].metadata;
  1601. }
  1602. bool PopupMenu::is_item_disabled(int p_idx) const {
  1603. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  1604. return items[p_idx].disabled;
  1605. }
  1606. bool PopupMenu::is_item_checked(int p_idx) const {
  1607. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  1608. return items[p_idx].checked;
  1609. }
  1610. int PopupMenu::get_item_id(int p_idx) const {
  1611. ERR_FAIL_INDEX_V(p_idx, items.size(), 0);
  1612. return items[p_idx].id;
  1613. }
  1614. int PopupMenu::get_item_index(int p_id) const {
  1615. for (int i = 0; i < items.size(); i++) {
  1616. if (items[i].id == p_id) {
  1617. return i;
  1618. }
  1619. }
  1620. return -1;
  1621. }
  1622. String PopupMenu::get_item_submenu(int p_idx) const {
  1623. ERR_FAIL_INDEX_V(p_idx, items.size(), "");
  1624. return items[p_idx].submenu;
  1625. }
  1626. String PopupMenu::get_item_tooltip(int p_idx) const {
  1627. ERR_FAIL_INDEX_V(p_idx, items.size(), "");
  1628. return items[p_idx].tooltip;
  1629. }
  1630. Ref<Shortcut> PopupMenu::get_item_shortcut(int p_idx) const {
  1631. ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<Shortcut>());
  1632. return items[p_idx].shortcut;
  1633. }
  1634. int PopupMenu::get_item_indent(int p_idx) const {
  1635. ERR_FAIL_INDEX_V(p_idx, items.size(), 0);
  1636. return items[p_idx].indent;
  1637. }
  1638. int PopupMenu::get_item_max_states(int p_idx) const {
  1639. ERR_FAIL_INDEX_V(p_idx, items.size(), -1);
  1640. return items[p_idx].max_states;
  1641. }
  1642. int PopupMenu::get_item_state(int p_idx) const {
  1643. ERR_FAIL_INDEX_V(p_idx, items.size(), -1);
  1644. return items[p_idx].state;
  1645. }
  1646. void PopupMenu::set_item_as_separator(int p_idx, bool p_separator) {
  1647. if (p_idx < 0) {
  1648. p_idx += get_item_count();
  1649. }
  1650. ERR_FAIL_INDEX(p_idx, items.size());
  1651. if (items[p_idx].separator == p_separator) {
  1652. return;
  1653. }
  1654. items.write[p_idx].separator = p_separator;
  1655. control->queue_redraw();
  1656. }
  1657. bool PopupMenu::is_item_separator(int p_idx) const {
  1658. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  1659. return items[p_idx].separator;
  1660. }
  1661. void PopupMenu::set_item_as_checkable(int p_idx, bool p_checkable) {
  1662. if (p_idx < 0) {
  1663. p_idx += get_item_count();
  1664. }
  1665. ERR_FAIL_INDEX(p_idx, items.size());
  1666. int type = (int)(p_checkable ? Item::CHECKABLE_TYPE_CHECK_BOX : Item::CHECKABLE_TYPE_NONE);
  1667. if (type == items[p_idx].checkable_type) {
  1668. return;
  1669. }
  1670. items.write[p_idx].checkable_type = p_checkable ? Item::CHECKABLE_TYPE_CHECK_BOX : Item::CHECKABLE_TYPE_NONE;
  1671. if (!global_menu_name.is_empty()) {
  1672. DisplayServer::get_singleton()->global_menu_set_item_checkable(global_menu_name, p_idx, p_checkable);
  1673. }
  1674. control->queue_redraw();
  1675. _menu_changed();
  1676. }
  1677. void PopupMenu::set_item_as_radio_checkable(int p_idx, bool p_radio_checkable) {
  1678. if (p_idx < 0) {
  1679. p_idx += get_item_count();
  1680. }
  1681. ERR_FAIL_INDEX(p_idx, items.size());
  1682. int type = (int)(p_radio_checkable ? Item::CHECKABLE_TYPE_RADIO_BUTTON : Item::CHECKABLE_TYPE_NONE);
  1683. if (type == items[p_idx].checkable_type) {
  1684. return;
  1685. }
  1686. items.write[p_idx].checkable_type = p_radio_checkable ? Item::CHECKABLE_TYPE_RADIO_BUTTON : Item::CHECKABLE_TYPE_NONE;
  1687. if (!global_menu_name.is_empty()) {
  1688. DisplayServer::get_singleton()->global_menu_set_item_radio_checkable(global_menu_name, p_idx, p_radio_checkable);
  1689. }
  1690. control->queue_redraw();
  1691. _menu_changed();
  1692. }
  1693. void PopupMenu::set_item_tooltip(int p_idx, const String &p_tooltip) {
  1694. if (p_idx < 0) {
  1695. p_idx += get_item_count();
  1696. }
  1697. ERR_FAIL_INDEX(p_idx, items.size());
  1698. if (items[p_idx].tooltip == p_tooltip) {
  1699. return;
  1700. }
  1701. items.write[p_idx].tooltip = p_tooltip;
  1702. if (!global_menu_name.is_empty()) {
  1703. DisplayServer::get_singleton()->global_menu_set_item_tooltip(global_menu_name, p_idx, p_tooltip);
  1704. }
  1705. control->queue_redraw();
  1706. _menu_changed();
  1707. }
  1708. void PopupMenu::set_item_shortcut(int p_idx, const Ref<Shortcut> &p_shortcut, bool p_global) {
  1709. if (p_idx < 0) {
  1710. p_idx += get_item_count();
  1711. }
  1712. ERR_FAIL_INDEX(p_idx, items.size());
  1713. if (items[p_idx].shortcut == p_shortcut && items[p_idx].shortcut_is_global == p_global && items[p_idx].shortcut.is_valid() == p_shortcut.is_valid()) {
  1714. return;
  1715. }
  1716. if (items[p_idx].shortcut.is_valid()) {
  1717. _unref_shortcut(items[p_idx].shortcut);
  1718. }
  1719. items.write[p_idx].shortcut = p_shortcut;
  1720. items.write[p_idx].shortcut_is_global = p_global;
  1721. items.write[p_idx].dirty = true;
  1722. if (items[p_idx].shortcut.is_valid()) {
  1723. _ref_shortcut(items[p_idx].shortcut);
  1724. }
  1725. if (!global_menu_name.is_empty()) {
  1726. DisplayServer *ds = DisplayServer::get_singleton();
  1727. ds->global_menu_set_item_accelerator(global_menu_name, p_idx, Key::NONE);
  1728. if (!items[p_idx].shortcut_is_disabled && items[p_idx].shortcut.is_valid() && items[p_idx].shortcut->has_valid_event()) {
  1729. Array events = items[p_idx].shortcut->get_events();
  1730. for (int j = 0; j < events.size(); j++) {
  1731. Ref<InputEventKey> ie = events[j];
  1732. if (ie.is_valid() && _set_item_accelerator(p_idx, ie)) {
  1733. break;
  1734. }
  1735. }
  1736. if (p_global) {
  1737. ds->global_menu_set_item_key_callback(global_menu_name, p_idx, callable_mp(this, &PopupMenu::activate_item));
  1738. } else {
  1739. ds->global_menu_set_item_key_callback(global_menu_name, p_idx, Callable());
  1740. }
  1741. }
  1742. }
  1743. control->queue_redraw();
  1744. _menu_changed();
  1745. }
  1746. void PopupMenu::set_item_indent(int p_idx, int p_indent) {
  1747. if (p_idx < 0) {
  1748. p_idx += get_item_count();
  1749. }
  1750. ERR_FAIL_INDEX(p_idx, items.size());
  1751. if (items.write[p_idx].indent == p_indent) {
  1752. return;
  1753. }
  1754. items.write[p_idx].indent = p_indent;
  1755. if (!global_menu_name.is_empty()) {
  1756. DisplayServer::get_singleton()->global_menu_set_item_indentation_level(global_menu_name, p_idx, p_indent);
  1757. }
  1758. control->queue_redraw();
  1759. child_controls_changed();
  1760. _menu_changed();
  1761. }
  1762. void PopupMenu::set_item_multistate(int p_idx, int p_state) {
  1763. if (p_idx < 0) {
  1764. p_idx += get_item_count();
  1765. }
  1766. ERR_FAIL_INDEX(p_idx, items.size());
  1767. if (items[p_idx].state == p_state) {
  1768. return;
  1769. }
  1770. items.write[p_idx].state = p_state;
  1771. if (!global_menu_name.is_empty()) {
  1772. DisplayServer::get_singleton()->global_menu_set_item_state(global_menu_name, p_idx, p_state);
  1773. }
  1774. control->queue_redraw();
  1775. _menu_changed();
  1776. }
  1777. void PopupMenu::set_item_shortcut_disabled(int p_idx, bool p_disabled) {
  1778. if (p_idx < 0) {
  1779. p_idx += get_item_count();
  1780. }
  1781. ERR_FAIL_INDEX(p_idx, items.size());
  1782. if (items[p_idx].shortcut_is_disabled == p_disabled) {
  1783. return;
  1784. }
  1785. items.write[p_idx].shortcut_is_disabled = p_disabled;
  1786. if (!global_menu_name.is_empty()) {
  1787. DisplayServer *ds = DisplayServer::get_singleton();
  1788. ds->global_menu_set_item_accelerator(global_menu_name, p_idx, Key::NONE);
  1789. if (!items[p_idx].shortcut_is_disabled && items[p_idx].shortcut.is_valid() && items[p_idx].shortcut->has_valid_event()) {
  1790. Array events = items[p_idx].shortcut->get_events();
  1791. for (int j = 0; j < events.size(); j++) {
  1792. Ref<InputEventKey> ie = events[j];
  1793. if (ie.is_valid() && _set_item_accelerator(p_idx, ie)) {
  1794. break;
  1795. }
  1796. }
  1797. }
  1798. }
  1799. control->queue_redraw();
  1800. _menu_changed();
  1801. }
  1802. void PopupMenu::toggle_item_multistate(int p_idx) {
  1803. ERR_FAIL_INDEX(p_idx, items.size());
  1804. if (0 >= items[p_idx].max_states) {
  1805. return;
  1806. }
  1807. ++items.write[p_idx].state;
  1808. if (items.write[p_idx].max_states <= items[p_idx].state) {
  1809. items.write[p_idx].state = 0;
  1810. }
  1811. if (!global_menu_name.is_empty()) {
  1812. DisplayServer::get_singleton()->global_menu_set_item_state(global_menu_name, p_idx, items[p_idx].state);
  1813. }
  1814. control->queue_redraw();
  1815. _menu_changed();
  1816. }
  1817. bool PopupMenu::is_item_checkable(int p_idx) const {
  1818. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  1819. return items[p_idx].checkable_type;
  1820. }
  1821. bool PopupMenu::is_item_radio_checkable(int p_idx) const {
  1822. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  1823. return items[p_idx].checkable_type == Item::CHECKABLE_TYPE_RADIO_BUTTON;
  1824. }
  1825. bool PopupMenu::is_item_shortcut_global(int p_idx) const {
  1826. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  1827. return items[p_idx].shortcut_is_global;
  1828. }
  1829. bool PopupMenu::is_item_shortcut_disabled(int p_idx) const {
  1830. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  1831. return items[p_idx].shortcut_is_disabled;
  1832. }
  1833. void PopupMenu::set_focused_item(int p_idx) {
  1834. if (p_idx != -1) {
  1835. ERR_FAIL_INDEX(p_idx, items.size());
  1836. }
  1837. if (mouse_over == p_idx) {
  1838. return;
  1839. }
  1840. mouse_over = p_idx;
  1841. if (mouse_over != -1) {
  1842. scroll_to_item(mouse_over);
  1843. }
  1844. control->queue_redraw();
  1845. }
  1846. int PopupMenu::get_focused_item() const {
  1847. return mouse_over;
  1848. }
  1849. void PopupMenu::set_item_count(int p_count) {
  1850. ERR_FAIL_COND(p_count < 0);
  1851. int prev_size = items.size();
  1852. if (prev_size == p_count) {
  1853. return;
  1854. }
  1855. DisplayServer *ds = DisplayServer::get_singleton();
  1856. bool is_global = !global_menu_name.is_empty();
  1857. if (is_global && prev_size > p_count) {
  1858. for (int i = prev_size - 1; i >= p_count; i--) {
  1859. ds->global_menu_remove_item(global_menu_name, i);
  1860. }
  1861. }
  1862. items.resize(p_count);
  1863. if (prev_size < p_count) {
  1864. for (int i = prev_size; i < p_count; i++) {
  1865. items.write[i].id = i;
  1866. if (is_global) {
  1867. ds->global_menu_add_item(global_menu_name, String(), callable_mp(this, &PopupMenu::activate_item), Callable(), i);
  1868. }
  1869. }
  1870. }
  1871. control->queue_redraw();
  1872. child_controls_changed();
  1873. notify_property_list_changed();
  1874. _menu_changed();
  1875. }
  1876. int PopupMenu::get_item_count() const {
  1877. return items.size();
  1878. }
  1879. void PopupMenu::scroll_to_item(int p_idx) {
  1880. ERR_FAIL_INDEX(p_idx, items.size());
  1881. // Calculate the position of the item relative to the visible area.
  1882. int item_y = items[p_idx]._ofs_cache;
  1883. int visible_height = scroll_container->get_size().height;
  1884. int relative_y = item_y - scroll_container->get_v_scroll();
  1885. // If item is not fully visible, adjust scroll.
  1886. if (relative_y < 0) {
  1887. scroll_container->set_v_scroll(item_y);
  1888. } else if (relative_y + items[p_idx]._height_cache > visible_height) {
  1889. scroll_container->set_v_scroll(item_y + items[p_idx]._height_cache - visible_height);
  1890. }
  1891. }
  1892. bool PopupMenu::activate_item_by_event(const Ref<InputEvent> &p_event, bool p_for_global_only) {
  1893. ERR_FAIL_COND_V(p_event.is_null(), false);
  1894. Key code = Key::NONE;
  1895. Ref<InputEventKey> k = p_event;
  1896. if (k.is_valid()) {
  1897. code = k->get_keycode();
  1898. if (code == Key::NONE) {
  1899. code = (Key)k->get_unicode();
  1900. }
  1901. if (k->is_ctrl_pressed()) {
  1902. code |= KeyModifierMask::CTRL;
  1903. }
  1904. if (k->is_alt_pressed()) {
  1905. code |= KeyModifierMask::ALT;
  1906. }
  1907. if (k->is_meta_pressed()) {
  1908. code |= KeyModifierMask::META;
  1909. }
  1910. if (k->is_shift_pressed()) {
  1911. code |= KeyModifierMask::SHIFT;
  1912. }
  1913. }
  1914. for (int i = 0; i < items.size(); i++) {
  1915. if (is_item_disabled(i) || items[i].shortcut_is_disabled || (!items[i].allow_echo && p_event->is_echo())) {
  1916. continue;
  1917. }
  1918. if (items[i].shortcut.is_valid() && items[i].shortcut->matches_event(p_event) && (items[i].shortcut_is_global || !p_for_global_only)) {
  1919. activate_item(i);
  1920. return true;
  1921. }
  1922. if (code != Key::NONE && items[i].accel == code) {
  1923. activate_item(i);
  1924. return true;
  1925. }
  1926. if (!items[i].submenu.is_empty()) {
  1927. Node *n = get_node(items[i].submenu);
  1928. if (!n) {
  1929. continue;
  1930. }
  1931. PopupMenu *pm = Object::cast_to<PopupMenu>(n);
  1932. if (!pm) {
  1933. continue;
  1934. }
  1935. if (pm->activate_item_by_event(p_event, p_for_global_only)) {
  1936. return true;
  1937. }
  1938. }
  1939. }
  1940. return false;
  1941. }
  1942. void PopupMenu::_about_to_popup() {
  1943. ERR_MAIN_THREAD_GUARD;
  1944. emit_signal(SNAME("about_to_popup"));
  1945. }
  1946. void PopupMenu::_about_to_close() {
  1947. ERR_MAIN_THREAD_GUARD;
  1948. emit_signal(SNAME("popup_hide"));
  1949. }
  1950. void PopupMenu::activate_item(int p_idx) {
  1951. ERR_FAIL_INDEX(p_idx, items.size());
  1952. ERR_FAIL_COND(items[p_idx].separator);
  1953. int id = items[p_idx].id >= 0 ? items[p_idx].id : p_idx;
  1954. //hide all parent PopupMenus
  1955. Node *next = get_parent();
  1956. PopupMenu *pop = Object::cast_to<PopupMenu>(next);
  1957. while (pop) {
  1958. // We close all parents that are chained together,
  1959. // with hide_on_item_selection enabled
  1960. if (items[p_idx].checkable_type) {
  1961. if (!hide_on_checkable_item_selection || !pop->is_hide_on_checkable_item_selection()) {
  1962. break;
  1963. }
  1964. } else if (0 < items[p_idx].max_states) {
  1965. if (!hide_on_multistate_item_selection || !pop->is_hide_on_multistate_item_selection()) {
  1966. break;
  1967. }
  1968. } else if (!hide_on_item_selection || !pop->is_hide_on_item_selection()) {
  1969. break;
  1970. }
  1971. pop->hide();
  1972. next = next->get_parent();
  1973. pop = Object::cast_to<PopupMenu>(next);
  1974. }
  1975. // Hides popup by default; unless otherwise specified
  1976. // by using set_hide_on_item_selection and set_hide_on_checkable_item_selection
  1977. bool need_hide = true;
  1978. if (items[p_idx].checkable_type) {
  1979. if (!hide_on_checkable_item_selection) {
  1980. need_hide = false;
  1981. }
  1982. } else if (0 < items[p_idx].max_states) {
  1983. if (!hide_on_multistate_item_selection) {
  1984. need_hide = false;
  1985. }
  1986. } else if (!hide_on_item_selection) {
  1987. need_hide = false;
  1988. }
  1989. if (need_hide) {
  1990. hide();
  1991. }
  1992. emit_signal(SNAME("id_pressed"), id);
  1993. emit_signal(SNAME("index_pressed"), p_idx);
  1994. }
  1995. void PopupMenu::remove_item(int p_idx) {
  1996. ERR_FAIL_INDEX(p_idx, items.size());
  1997. if (items[p_idx].shortcut.is_valid()) {
  1998. _unref_shortcut(items[p_idx].shortcut);
  1999. }
  2000. items.remove_at(p_idx);
  2001. if (!global_menu_name.is_empty()) {
  2002. DisplayServer::get_singleton()->global_menu_remove_item(global_menu_name, p_idx);
  2003. }
  2004. control->queue_redraw();
  2005. child_controls_changed();
  2006. _menu_changed();
  2007. }
  2008. void PopupMenu::add_separator(const String &p_text, int p_id) {
  2009. Item sep;
  2010. sep.separator = true;
  2011. sep.id = p_id;
  2012. if (!p_text.is_empty()) {
  2013. sep.text = p_text;
  2014. sep.xl_text = atr(p_text);
  2015. }
  2016. items.push_back(sep);
  2017. if (!global_menu_name.is_empty()) {
  2018. DisplayServer::get_singleton()->global_menu_add_separator(global_menu_name);
  2019. }
  2020. control->queue_redraw();
  2021. _menu_changed();
  2022. }
  2023. void PopupMenu::clear(bool p_free_submenus) {
  2024. for (const Item &I : items) {
  2025. if (I.shortcut.is_valid()) {
  2026. _unref_shortcut(I.shortcut);
  2027. }
  2028. if (p_free_submenus && !I.submenu.is_empty()) {
  2029. Node *submenu = get_node_or_null(I.submenu);
  2030. if (submenu) {
  2031. remove_child(submenu);
  2032. submenu->queue_free();
  2033. }
  2034. }
  2035. }
  2036. if (!global_menu_name.is_empty()) {
  2037. for (int i = 0; i < items.size(); i++) {
  2038. Item &item = items.write[i];
  2039. if (!item.submenu.is_empty()) {
  2040. PopupMenu *pm = Object::cast_to<PopupMenu>(get_node_or_null(item.submenu));
  2041. if (pm) {
  2042. pm->unbind_global_menu();
  2043. }
  2044. item.submenu_bound = false;
  2045. }
  2046. }
  2047. DisplayServer::get_singleton()->global_menu_clear(global_menu_name);
  2048. }
  2049. items.clear();
  2050. mouse_over = -1;
  2051. control->queue_redraw();
  2052. child_controls_changed();
  2053. notify_property_list_changed();
  2054. _menu_changed();
  2055. }
  2056. void PopupMenu::_ref_shortcut(Ref<Shortcut> p_sc) {
  2057. if (!shortcut_refcount.has(p_sc)) {
  2058. shortcut_refcount[p_sc] = 1;
  2059. p_sc->connect_changed(callable_mp(this, &PopupMenu::_shortcut_changed));
  2060. } else {
  2061. shortcut_refcount[p_sc] += 1;
  2062. }
  2063. }
  2064. void PopupMenu::_unref_shortcut(Ref<Shortcut> p_sc) {
  2065. ERR_FAIL_COND(!shortcut_refcount.has(p_sc));
  2066. shortcut_refcount[p_sc]--;
  2067. if (shortcut_refcount[p_sc] == 0) {
  2068. p_sc->disconnect_changed(callable_mp(this, &PopupMenu::_shortcut_changed));
  2069. shortcut_refcount.erase(p_sc);
  2070. }
  2071. }
  2072. void PopupMenu::_shortcut_changed() {
  2073. for (int i = 0; i < items.size(); i++) {
  2074. items.write[i].dirty = true;
  2075. }
  2076. control->queue_redraw();
  2077. }
  2078. // Hide on item selection determines whether or not the popup will close after item selection
  2079. void PopupMenu::set_hide_on_item_selection(bool p_enabled) {
  2080. hide_on_item_selection = p_enabled;
  2081. }
  2082. bool PopupMenu::is_hide_on_item_selection() const {
  2083. return hide_on_item_selection;
  2084. }
  2085. void PopupMenu::set_hide_on_checkable_item_selection(bool p_enabled) {
  2086. hide_on_checkable_item_selection = p_enabled;
  2087. }
  2088. bool PopupMenu::is_hide_on_checkable_item_selection() const {
  2089. return hide_on_checkable_item_selection;
  2090. }
  2091. void PopupMenu::set_hide_on_multistate_item_selection(bool p_enabled) {
  2092. hide_on_multistate_item_selection = p_enabled;
  2093. }
  2094. bool PopupMenu::is_hide_on_multistate_item_selection() const {
  2095. return hide_on_multistate_item_selection;
  2096. }
  2097. void PopupMenu::set_submenu_popup_delay(float p_time) {
  2098. if (p_time <= 0) {
  2099. p_time = 0.01;
  2100. }
  2101. submenu_timer->set_wait_time(p_time);
  2102. }
  2103. float PopupMenu::get_submenu_popup_delay() const {
  2104. return submenu_timer->get_wait_time();
  2105. }
  2106. void PopupMenu::set_allow_search(bool p_allow) {
  2107. allow_search = p_allow;
  2108. }
  2109. bool PopupMenu::get_allow_search() const {
  2110. return allow_search;
  2111. }
  2112. String PopupMenu::get_tooltip(const Point2 &p_pos) const {
  2113. int over = _get_mouse_over(p_pos);
  2114. if (over < 0 || over >= items.size()) {
  2115. return "";
  2116. }
  2117. return items[over].tooltip;
  2118. }
  2119. void PopupMenu::add_autohide_area(const Rect2 &p_area) {
  2120. autohide_areas.push_back(p_area);
  2121. }
  2122. void PopupMenu::clear_autohide_areas() {
  2123. autohide_areas.clear();
  2124. }
  2125. void PopupMenu::take_mouse_focus() {
  2126. ERR_FAIL_COND(!is_inside_tree());
  2127. if (get_parent()) {
  2128. get_parent()->get_viewport()->pass_mouse_focus_to(this, control);
  2129. }
  2130. }
  2131. bool PopupMenu::_set(const StringName &p_name, const Variant &p_value) {
  2132. Vector<String> components = String(p_name).split("/", true, 2);
  2133. if (components.size() >= 2 && components[0].begins_with("item_") && components[0].trim_prefix("item_").is_valid_int()) {
  2134. int item_index = components[0].trim_prefix("item_").to_int();
  2135. const String &property = components[1];
  2136. if (property == "text") {
  2137. set_item_text(item_index, p_value);
  2138. return true;
  2139. } else if (property == "icon") {
  2140. set_item_icon(item_index, p_value);
  2141. return true;
  2142. } else if (property == "checkable") {
  2143. bool radio_checkable = (int)p_value == Item::CHECKABLE_TYPE_RADIO_BUTTON;
  2144. if (radio_checkable) {
  2145. set_item_as_radio_checkable(item_index, true);
  2146. } else {
  2147. bool checkable = p_value;
  2148. set_item_as_checkable(item_index, checkable);
  2149. }
  2150. return true;
  2151. } else if (property == "checked") {
  2152. set_item_checked(item_index, p_value);
  2153. return true;
  2154. } else if (property == "id") {
  2155. set_item_id(item_index, p_value);
  2156. return true;
  2157. } else if (property == "disabled") {
  2158. set_item_disabled(item_index, p_value);
  2159. return true;
  2160. } else if (property == "separator") {
  2161. set_item_as_separator(item_index, p_value);
  2162. return true;
  2163. }
  2164. }
  2165. #ifndef DISABLE_DEPRECATED
  2166. // Compatibility.
  2167. if (p_name == "items") {
  2168. Array arr = p_value;
  2169. ERR_FAIL_COND_V(arr.size() % 10, false);
  2170. clear();
  2171. for (int i = 0; i < arr.size(); i += 10) {
  2172. String text = arr[i + 0];
  2173. Ref<Texture2D> icon = arr[i + 1];
  2174. // For compatibility, use false/true for no/checkbox and integers for other values
  2175. bool checkable = arr[i + 2];
  2176. bool radio_checkable = (int)arr[i + 2] == Item::CHECKABLE_TYPE_RADIO_BUTTON;
  2177. bool checked = arr[i + 3];
  2178. bool disabled = arr[i + 4];
  2179. int id = arr[i + 5];
  2180. int accel = arr[i + 6];
  2181. Variant meta = arr[i + 7];
  2182. String subm = arr[i + 8];
  2183. bool sep = arr[i + 9];
  2184. int idx = get_item_count();
  2185. add_item(text, id);
  2186. set_item_icon(idx, icon);
  2187. if (checkable) {
  2188. if (radio_checkable) {
  2189. set_item_as_radio_checkable(idx, true);
  2190. } else {
  2191. set_item_as_checkable(idx, true);
  2192. }
  2193. }
  2194. set_item_checked(idx, checked);
  2195. set_item_disabled(idx, disabled);
  2196. set_item_id(idx, id);
  2197. set_item_metadata(idx, meta);
  2198. set_item_as_separator(idx, sep);
  2199. set_item_accelerator(idx, (Key)accel);
  2200. set_item_submenu(idx, subm);
  2201. }
  2202. }
  2203. #endif
  2204. return false;
  2205. }
  2206. bool PopupMenu::_get(const StringName &p_name, Variant &r_ret) const {
  2207. Vector<String> components = String(p_name).split("/", true, 2);
  2208. if (components.size() >= 2 && components[0].begins_with("item_") && components[0].trim_prefix("item_").is_valid_int()) {
  2209. int item_index = components[0].trim_prefix("item_").to_int();
  2210. const String &property = components[1];
  2211. if (property == "text") {
  2212. r_ret = get_item_text(item_index);
  2213. return true;
  2214. } else if (property == "icon") {
  2215. r_ret = get_item_icon(item_index);
  2216. return true;
  2217. } else if (property == "checkable") {
  2218. if (item_index >= 0 && item_index < items.size()) {
  2219. r_ret = items[item_index].checkable_type;
  2220. return true;
  2221. } else {
  2222. r_ret = Item::CHECKABLE_TYPE_NONE;
  2223. ERR_FAIL_V(true);
  2224. }
  2225. } else if (property == "checked") {
  2226. r_ret = is_item_checked(item_index);
  2227. return true;
  2228. } else if (property == "id") {
  2229. r_ret = get_item_id(item_index);
  2230. return true;
  2231. } else if (property == "disabled") {
  2232. r_ret = is_item_disabled(item_index);
  2233. return true;
  2234. } else if (property == "separator") {
  2235. r_ret = is_item_separator(item_index);
  2236. return true;
  2237. }
  2238. }
  2239. return false;
  2240. }
  2241. void PopupMenu::_get_property_list(List<PropertyInfo> *p_list) const {
  2242. for (int i = 0; i < items.size(); i++) {
  2243. p_list->push_back(PropertyInfo(Variant::STRING, vformat("item_%d/text", i)));
  2244. PropertyInfo pi = PropertyInfo(Variant::OBJECT, vformat("item_%d/icon", i), PROPERTY_HINT_RESOURCE_TYPE, "Texture2D");
  2245. pi.usage &= ~(get_item_icon(i).is_null() ? PROPERTY_USAGE_STORAGE : 0);
  2246. p_list->push_back(pi);
  2247. pi = PropertyInfo(Variant::INT, vformat("item_%d/checkable", i), PROPERTY_HINT_ENUM, "No,As checkbox,As radio button");
  2248. pi.usage &= ~(!is_item_checkable(i) ? PROPERTY_USAGE_STORAGE : 0);
  2249. p_list->push_back(pi);
  2250. pi = PropertyInfo(Variant::BOOL, vformat("item_%d/checked", i));
  2251. pi.usage &= ~(!is_item_checked(i) ? PROPERTY_USAGE_STORAGE : 0);
  2252. p_list->push_back(pi);
  2253. pi = PropertyInfo(Variant::INT, vformat("item_%d/id", i), PROPERTY_HINT_RANGE, "0,10,1,or_greater");
  2254. p_list->push_back(pi);
  2255. pi = PropertyInfo(Variant::BOOL, vformat("item_%d/disabled", i));
  2256. pi.usage &= ~(!is_item_disabled(i) ? PROPERTY_USAGE_STORAGE : 0);
  2257. p_list->push_back(pi);
  2258. pi = PropertyInfo(Variant::BOOL, vformat("item_%d/separator", i));
  2259. pi.usage &= ~(!is_item_separator(i) ? PROPERTY_USAGE_STORAGE : 0);
  2260. p_list->push_back(pi);
  2261. }
  2262. }
  2263. void PopupMenu::_bind_methods() {
  2264. ClassDB::bind_method(D_METHOD("activate_item_by_event", "event", "for_global_only"), &PopupMenu::activate_item_by_event, DEFVAL(false));
  2265. ClassDB::bind_method(D_METHOD("add_item", "label", "id", "accel"), &PopupMenu::add_item, DEFVAL(-1), DEFVAL(0));
  2266. ClassDB::bind_method(D_METHOD("add_icon_item", "texture", "label", "id", "accel"), &PopupMenu::add_icon_item, DEFVAL(-1), DEFVAL(0));
  2267. ClassDB::bind_method(D_METHOD("add_check_item", "label", "id", "accel"), &PopupMenu::add_check_item, DEFVAL(-1), DEFVAL(0));
  2268. ClassDB::bind_method(D_METHOD("add_icon_check_item", "texture", "label", "id", "accel"), &PopupMenu::add_icon_check_item, DEFVAL(-1), DEFVAL(0));
  2269. ClassDB::bind_method(D_METHOD("add_radio_check_item", "label", "id", "accel"), &PopupMenu::add_radio_check_item, DEFVAL(-1), DEFVAL(0));
  2270. ClassDB::bind_method(D_METHOD("add_icon_radio_check_item", "texture", "label", "id", "accel"), &PopupMenu::add_icon_radio_check_item, DEFVAL(-1), DEFVAL(0));
  2271. 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));
  2272. ClassDB::bind_method(D_METHOD("add_shortcut", "shortcut", "id", "global", "allow_echo"), &PopupMenu::add_shortcut, DEFVAL(-1), DEFVAL(false), DEFVAL(false));
  2273. ClassDB::bind_method(D_METHOD("add_icon_shortcut", "texture", "shortcut", "id", "global", "allow_echo"), &PopupMenu::add_icon_shortcut, DEFVAL(-1), DEFVAL(false), DEFVAL(false));
  2274. ClassDB::bind_method(D_METHOD("add_check_shortcut", "shortcut", "id", "global"), &PopupMenu::add_check_shortcut, DEFVAL(-1), DEFVAL(false));
  2275. ClassDB::bind_method(D_METHOD("add_icon_check_shortcut", "texture", "shortcut", "id", "global"), &PopupMenu::add_icon_check_shortcut, DEFVAL(-1), DEFVAL(false));
  2276. ClassDB::bind_method(D_METHOD("add_radio_check_shortcut", "shortcut", "id", "global"), &PopupMenu::add_radio_check_shortcut, DEFVAL(-1), DEFVAL(false));
  2277. ClassDB::bind_method(D_METHOD("add_icon_radio_check_shortcut", "texture", "shortcut", "id", "global"), &PopupMenu::add_icon_radio_check_shortcut, DEFVAL(-1), DEFVAL(false));
  2278. ClassDB::bind_method(D_METHOD("add_submenu_item", "label", "submenu", "id"), &PopupMenu::add_submenu_item, DEFVAL(-1));
  2279. ClassDB::bind_method(D_METHOD("set_item_text", "index", "text"), &PopupMenu::set_item_text);
  2280. ClassDB::bind_method(D_METHOD("set_item_text_direction", "index", "direction"), &PopupMenu::set_item_text_direction);
  2281. ClassDB::bind_method(D_METHOD("set_item_language", "index", "language"), &PopupMenu::set_item_language);
  2282. ClassDB::bind_method(D_METHOD("set_item_icon", "index", "icon"), &PopupMenu::set_item_icon);
  2283. ClassDB::bind_method(D_METHOD("set_item_icon_max_width", "index", "width"), &PopupMenu::set_item_icon_max_width);
  2284. ClassDB::bind_method(D_METHOD("set_item_icon_modulate", "index", "modulate"), &PopupMenu::set_item_icon_modulate);
  2285. ClassDB::bind_method(D_METHOD("set_item_checked", "index", "checked"), &PopupMenu::set_item_checked);
  2286. ClassDB::bind_method(D_METHOD("set_item_id", "index", "id"), &PopupMenu::set_item_id);
  2287. ClassDB::bind_method(D_METHOD("set_item_accelerator", "index", "accel"), &PopupMenu::set_item_accelerator);
  2288. ClassDB::bind_method(D_METHOD("set_item_metadata", "index", "metadata"), &PopupMenu::set_item_metadata);
  2289. ClassDB::bind_method(D_METHOD("set_item_disabled", "index", "disabled"), &PopupMenu::set_item_disabled);
  2290. ClassDB::bind_method(D_METHOD("set_item_submenu", "index", "submenu"), &PopupMenu::set_item_submenu);
  2291. ClassDB::bind_method(D_METHOD("set_item_as_separator", "index", "enable"), &PopupMenu::set_item_as_separator);
  2292. ClassDB::bind_method(D_METHOD("set_item_as_checkable", "index", "enable"), &PopupMenu::set_item_as_checkable);
  2293. ClassDB::bind_method(D_METHOD("set_item_as_radio_checkable", "index", "enable"), &PopupMenu::set_item_as_radio_checkable);
  2294. ClassDB::bind_method(D_METHOD("set_item_tooltip", "index", "tooltip"), &PopupMenu::set_item_tooltip);
  2295. ClassDB::bind_method(D_METHOD("set_item_shortcut", "index", "shortcut", "global"), &PopupMenu::set_item_shortcut, DEFVAL(false));
  2296. ClassDB::bind_method(D_METHOD("set_item_indent", "index", "indent"), &PopupMenu::set_item_indent);
  2297. ClassDB::bind_method(D_METHOD("set_item_multistate", "index", "state"), &PopupMenu::set_item_multistate);
  2298. ClassDB::bind_method(D_METHOD("set_item_shortcut_disabled", "index", "disabled"), &PopupMenu::set_item_shortcut_disabled);
  2299. ClassDB::bind_method(D_METHOD("toggle_item_checked", "index"), &PopupMenu::toggle_item_checked);
  2300. ClassDB::bind_method(D_METHOD("toggle_item_multistate", "index"), &PopupMenu::toggle_item_multistate);
  2301. ClassDB::bind_method(D_METHOD("get_item_text", "index"), &PopupMenu::get_item_text);
  2302. ClassDB::bind_method(D_METHOD("get_item_text_direction", "index"), &PopupMenu::get_item_text_direction);
  2303. ClassDB::bind_method(D_METHOD("get_item_language", "index"), &PopupMenu::get_item_language);
  2304. ClassDB::bind_method(D_METHOD("get_item_icon", "index"), &PopupMenu::get_item_icon);
  2305. ClassDB::bind_method(D_METHOD("get_item_icon_max_width", "index"), &PopupMenu::get_item_icon_max_width);
  2306. ClassDB::bind_method(D_METHOD("get_item_icon_modulate", "index"), &PopupMenu::get_item_icon_modulate);
  2307. ClassDB::bind_method(D_METHOD("is_item_checked", "index"), &PopupMenu::is_item_checked);
  2308. ClassDB::bind_method(D_METHOD("get_item_id", "index"), &PopupMenu::get_item_id);
  2309. ClassDB::bind_method(D_METHOD("get_item_index", "id"), &PopupMenu::get_item_index);
  2310. ClassDB::bind_method(D_METHOD("get_item_accelerator", "index"), &PopupMenu::get_item_accelerator);
  2311. ClassDB::bind_method(D_METHOD("get_item_metadata", "index"), &PopupMenu::get_item_metadata);
  2312. ClassDB::bind_method(D_METHOD("is_item_disabled", "index"), &PopupMenu::is_item_disabled);
  2313. ClassDB::bind_method(D_METHOD("get_item_submenu", "index"), &PopupMenu::get_item_submenu);
  2314. ClassDB::bind_method(D_METHOD("is_item_separator", "index"), &PopupMenu::is_item_separator);
  2315. ClassDB::bind_method(D_METHOD("is_item_checkable", "index"), &PopupMenu::is_item_checkable);
  2316. ClassDB::bind_method(D_METHOD("is_item_radio_checkable", "index"), &PopupMenu::is_item_radio_checkable);
  2317. ClassDB::bind_method(D_METHOD("is_item_shortcut_disabled", "index"), &PopupMenu::is_item_shortcut_disabled);
  2318. ClassDB::bind_method(D_METHOD("get_item_tooltip", "index"), &PopupMenu::get_item_tooltip);
  2319. ClassDB::bind_method(D_METHOD("get_item_shortcut", "index"), &PopupMenu::get_item_shortcut);
  2320. ClassDB::bind_method(D_METHOD("get_item_indent", "index"), &PopupMenu::get_item_indent);
  2321. ClassDB::bind_method(D_METHOD("set_focused_item", "index"), &PopupMenu::set_focused_item);
  2322. ClassDB::bind_method(D_METHOD("get_focused_item"), &PopupMenu::get_focused_item);
  2323. ClassDB::bind_method(D_METHOD("set_item_count", "count"), &PopupMenu::set_item_count);
  2324. ClassDB::bind_method(D_METHOD("get_item_count"), &PopupMenu::get_item_count);
  2325. ClassDB::bind_method(D_METHOD("scroll_to_item", "index"), &PopupMenu::scroll_to_item);
  2326. ClassDB::bind_method(D_METHOD("remove_item", "index"), &PopupMenu::remove_item);
  2327. ClassDB::bind_method(D_METHOD("add_separator", "label", "id"), &PopupMenu::add_separator, DEFVAL(String()), DEFVAL(-1));
  2328. ClassDB::bind_method(D_METHOD("clear", "free_submenus"), &PopupMenu::clear, DEFVAL(false));
  2329. ClassDB::bind_method(D_METHOD("set_hide_on_item_selection", "enable"), &PopupMenu::set_hide_on_item_selection);
  2330. ClassDB::bind_method(D_METHOD("is_hide_on_item_selection"), &PopupMenu::is_hide_on_item_selection);
  2331. ClassDB::bind_method(D_METHOD("set_hide_on_checkable_item_selection", "enable"), &PopupMenu::set_hide_on_checkable_item_selection);
  2332. ClassDB::bind_method(D_METHOD("is_hide_on_checkable_item_selection"), &PopupMenu::is_hide_on_checkable_item_selection);
  2333. ClassDB::bind_method(D_METHOD("set_hide_on_state_item_selection", "enable"), &PopupMenu::set_hide_on_multistate_item_selection);
  2334. ClassDB::bind_method(D_METHOD("is_hide_on_state_item_selection"), &PopupMenu::is_hide_on_multistate_item_selection);
  2335. ClassDB::bind_method(D_METHOD("set_submenu_popup_delay", "seconds"), &PopupMenu::set_submenu_popup_delay);
  2336. ClassDB::bind_method(D_METHOD("get_submenu_popup_delay"), &PopupMenu::get_submenu_popup_delay);
  2337. ClassDB::bind_method(D_METHOD("set_allow_search", "allow"), &PopupMenu::set_allow_search);
  2338. ClassDB::bind_method(D_METHOD("get_allow_search"), &PopupMenu::get_allow_search);
  2339. ClassDB::bind_method(D_METHOD("is_system_menu"), &PopupMenu::is_system_menu);
  2340. ClassDB::bind_method(D_METHOD("set_system_menu_root", "special"), &PopupMenu::set_system_menu_root);
  2341. ClassDB::bind_method(D_METHOD("get_system_menu_root"), &PopupMenu::get_system_menu_root);
  2342. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_on_item_selection"), "set_hide_on_item_selection", "is_hide_on_item_selection");
  2343. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_on_checkable_item_selection"), "set_hide_on_checkable_item_selection", "is_hide_on_checkable_item_selection");
  2344. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_on_state_item_selection"), "set_hide_on_state_item_selection", "is_hide_on_state_item_selection");
  2345. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "submenu_popup_delay", PROPERTY_HINT_NONE, "suffix:s"), "set_submenu_popup_delay", "get_submenu_popup_delay");
  2346. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_search"), "set_allow_search", "get_allow_search");
  2347. ADD_PROPERTY(PropertyInfo(Variant::STRING, "system_menu_root", PROPERTY_HINT_ENUM, "Dock (macOS):_dock,Apple Menu(macOS):_apple,Window Menu(macOS):_window,Help Menu(macOS):_help"), "set_system_menu_root", "get_system_menu_root");
  2348. ADD_ARRAY_COUNT("Items", "item_count", "set_item_count", "get_item_count", "item_");
  2349. ADD_SIGNAL(MethodInfo("id_pressed", PropertyInfo(Variant::INT, "id")));
  2350. ADD_SIGNAL(MethodInfo("id_focused", PropertyInfo(Variant::INT, "id")));
  2351. ADD_SIGNAL(MethodInfo("index_pressed", PropertyInfo(Variant::INT, "index")));
  2352. ADD_SIGNAL(MethodInfo("menu_changed"));
  2353. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, PopupMenu, panel_style, "panel");
  2354. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, PopupMenu, hover_style, "hover");
  2355. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, PopupMenu, separator_style, "separator");
  2356. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, PopupMenu, labeled_separator_left);
  2357. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, PopupMenu, labeled_separator_right);
  2358. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, PopupMenu, v_separation);
  2359. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, PopupMenu, h_separation);
  2360. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, PopupMenu, indent);
  2361. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, PopupMenu, item_start_padding);
  2362. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, PopupMenu, item_end_padding);
  2363. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, PopupMenu, icon_max_width);
  2364. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, checked);
  2365. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, checked_disabled);
  2366. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, unchecked);
  2367. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, unchecked_disabled);
  2368. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, radio_checked);
  2369. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, radio_checked_disabled);
  2370. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, radio_unchecked);
  2371. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, radio_unchecked_disabled);
  2372. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, submenu);
  2373. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, submenu_mirrored);
  2374. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, PopupMenu, font);
  2375. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, PopupMenu, font_size);
  2376. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, PopupMenu, font_separator);
  2377. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, PopupMenu, font_separator_size);
  2378. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_color);
  2379. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_hover_color);
  2380. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_disabled_color);
  2381. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_accelerator_color);
  2382. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, PopupMenu, font_outline_size, "outline_size");
  2383. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_outline_color);
  2384. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_separator_color);
  2385. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, PopupMenu, font_separator_outline_size, "separator_outline_size");
  2386. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_separator_outline_color);
  2387. }
  2388. void PopupMenu::popup(const Rect2i &p_bounds) {
  2389. moved = Vector2();
  2390. popup_time_msec = OS::get_singleton()->get_ticks_msec();
  2391. Popup::popup(p_bounds);
  2392. drag_to_press = true;
  2393. mouse_is_pressed = true;
  2394. }
  2395. PopupMenu::PopupMenu() {
  2396. // Margin Container
  2397. margin_container = memnew(MarginContainer);
  2398. margin_container->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  2399. add_child(margin_container, false, INTERNAL_MODE_FRONT);
  2400. margin_container->connect("draw", callable_mp(this, &PopupMenu::_draw_background));
  2401. // Scroll Container
  2402. scroll_container = memnew(ScrollContainer);
  2403. scroll_container->set_clip_contents(true);
  2404. margin_container->add_child(scroll_container);
  2405. // The control which will display the items
  2406. control = memnew(Control);
  2407. control->set_clip_contents(false);
  2408. control->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  2409. control->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  2410. control->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2411. scroll_container->add_child(control, false, INTERNAL_MODE_FRONT);
  2412. control->connect("draw", callable_mp(this, &PopupMenu::_draw_items));
  2413. submenu_timer = memnew(Timer);
  2414. submenu_timer->set_wait_time(0.3);
  2415. submenu_timer->set_one_shot(true);
  2416. submenu_timer->connect("timeout", callable_mp(this, &PopupMenu::_submenu_timeout));
  2417. add_child(submenu_timer, false, INTERNAL_MODE_FRONT);
  2418. minimum_lifetime_timer = memnew(Timer);
  2419. minimum_lifetime_timer->set_wait_time(0.3);
  2420. minimum_lifetime_timer->set_one_shot(true);
  2421. minimum_lifetime_timer->connect("timeout", callable_mp(this, &PopupMenu::_minimum_lifetime_timeout));
  2422. add_child(minimum_lifetime_timer, false, INTERNAL_MODE_FRONT);
  2423. }
  2424. PopupMenu::~PopupMenu() {
  2425. }