popup_menu.cpp 65 KB

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