popup_menu.cpp 71 KB

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