popup_menu.cpp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577
  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/os/input.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/os/os.h"
  34. #include "core/print_string.h"
  35. #include "core/translation.h"
  36. String PopupMenu::_get_accel_text(int p_item) const {
  37. ERR_FAIL_INDEX_V(p_item, items.size(), String());
  38. if (items[p_item].shortcut.is_valid()) {
  39. return items[p_item].shortcut->get_as_text();
  40. } else if (items[p_item].accel) {
  41. return keycode_get_string(items[p_item].accel);
  42. }
  43. return String();
  44. }
  45. Size2 PopupMenu::get_minimum_size() const {
  46. int vseparation = get_constant("vseparation");
  47. int hseparation = get_constant("hseparation");
  48. Size2 minsize = get_stylebox("panel")->get_minimum_size(); // Accounts for margin in the margin container
  49. minsize.x += scroll_container->get_v_scrollbar()->get_size().width * 2; // Adds a buffer so that the scrollbar does not render over the top of content
  50. Ref<Font> font = get_font("font");
  51. Ref<Font> font_separator = get_font("font_separator");
  52. float max_w = 0;
  53. float icon_w = 0;
  54. int check_w = MAX(get_icon("checked")->get_width(), get_icon("radio_checked")->get_width()) + hseparation;
  55. int accel_max_w = 0;
  56. bool has_check = false;
  57. for (int i = 0; i < items.size(); i++) {
  58. String text = items[i].xl_text;
  59. int font_h = items[i].separator && text != String() ? font_separator->get_height() : font->get_height();
  60. Size2 size;
  61. Size2 icon_size = items[i].get_icon_size();
  62. size.height = MAX(icon_size.height, font_h);
  63. icon_w = MAX(icon_size.width, icon_w);
  64. size.width += items[i].h_ofs;
  65. if (items[i].checkable_type) {
  66. has_check = true;
  67. }
  68. size.width += items[i].separator ? font_separator->get_string_size(text).width : font->get_string_size(text).width;
  69. size.height += vseparation;
  70. if (items[i].accel || (items[i].shortcut.is_valid() && items[i].shortcut->is_valid())) {
  71. int accel_w = hseparation * 2;
  72. accel_w += font->get_string_size(_get_accel_text(i)).width;
  73. accel_max_w = MAX(accel_w, accel_max_w);
  74. }
  75. if (items[i].submenu != "") {
  76. size.width += get_icon("submenu")->get_width();
  77. }
  78. max_w = MAX(max_w, size.width);
  79. minsize.height += size.height;
  80. }
  81. minsize.width += max_w + icon_w + accel_max_w;
  82. if (has_check) {
  83. minsize.width += check_w;
  84. }
  85. minsize.height = MIN(minsize.height, OS::get_singleton()->get_window_size().height);
  86. if (max_height > 0 && minsize.height > max_height) {
  87. minsize.height = max_height;
  88. }
  89. return minsize;
  90. }
  91. int PopupMenu::_get_items_total_height() const {
  92. int font_height = get_font("font")->get_height();
  93. int vsep = get_constant("vseparation");
  94. // Get total height of all items by taking max of icon height and font height
  95. int items_total_height = 0;
  96. for (int i = 0; i < items.size(); i++) {
  97. items_total_height += MAX(items[i].get_icon_size().height, font_height) + vsep;
  98. }
  99. // Subtract a separator which is not needed for the last item.
  100. return items_total_height - vsep;
  101. }
  102. void PopupMenu::_scroll_to_item(int p_item) {
  103. ERR_FAIL_INDEX(p_item, items.size());
  104. // Scroll item into view (upwards)
  105. if (items[p_item]._ofs_cache < -control->get_position().y) {
  106. int amnt_over = items[p_item]._ofs_cache + control->get_position().y;
  107. scroll_container->set_v_scroll(scroll_container->get_v_scroll() + amnt_over);
  108. }
  109. // Scroll item into view (downwards)
  110. if (items[p_item]._ofs_cache + items[p_item]._height_cache > -control->get_position().y + scroll_container->get_size().height) {
  111. int amnt_over = items[p_item]._ofs_cache + items[p_item]._height_cache + control->get_position().y - scroll_container->get_size().height;
  112. scroll_container->set_v_scroll(scroll_container->get_v_scroll() + amnt_over);
  113. }
  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. Ref<StyleBox> style = get_stylebox("panel"); // Accounts for margin in the margin container
  120. int vseparation = get_constant("vseparation");
  121. float font_h = get_font("font")->get_height();
  122. Point2 ofs = style->get_offset() + Point2(0, vseparation / 2);
  123. for (int i = 0; i < items.size(); i++) {
  124. if (i > 0) {
  125. ofs.y += vseparation;
  126. }
  127. ofs.y += MAX(items[i].get_icon_size().height, font_h);
  128. if (p_over.y - control->get_position().y < ofs.y) {
  129. return i;
  130. }
  131. }
  132. return -1;
  133. }
  134. void PopupMenu::_activate_submenu(int over, bool p_by_keyboard) {
  135. Node *n = get_node(items[over].submenu);
  136. ERR_FAIL_COND_MSG(!n, "Item subnode does not exist: " + items[over].submenu + ".");
  137. Popup *submenu_popup = Object::cast_to<Popup>(n);
  138. ERR_FAIL_COND_MSG(!submenu_popup, "Item subnode is not a Popup: " + items[over].submenu + ".");
  139. if (submenu_popup->is_visible_in_tree()) {
  140. return; // Already visible.
  141. }
  142. Ref<StyleBox> style = get_stylebox("panel");
  143. int vsep = get_constant("vseparation");
  144. Point2 this_pos = get_position();
  145. Rect2 this_rect(this_pos, get_size());
  146. float scroll_offset = control->get_position().y;
  147. Point2 submenu_pos = this_pos + Point2(this_rect.size.width, items[over]._ofs_cache + scroll_offset);
  148. Size2 submenu_size = submenu_popup->get_size();
  149. // Fix pos if going outside parent rect
  150. if (submenu_pos.x + submenu_size.width > get_viewport_rect().size.width) {
  151. submenu_pos.x = this_pos.x - submenu_size.width;
  152. }
  153. submenu_popup->set_position(submenu_pos);
  154. submenu_popup->set_as_minsize(); // Shrink the popup size to it's contents.
  155. submenu_popup->popup();
  156. // Set autohide areas
  157. PopupMenu *submenu_pum = Object::cast_to<PopupMenu>(submenu_popup);
  158. if (submenu_pum) {
  159. submenu_pum->activated_by_keyboard = p_by_keyboard;
  160. // If not triggered by the mouse, start the popup with its first enabled item focused.
  161. if (p_by_keyboard) {
  162. for (int i = 0; i < submenu_pum->get_item_count(); i++) {
  163. if (!submenu_pum->is_item_disabled(i)) {
  164. submenu_pum->set_current_index(i);
  165. break;
  166. }
  167. }
  168. }
  169. // Make the position of the parent popup relative to submenu popup
  170. this_rect.position = this_rect.position - submenu_pum->get_position();
  171. // Autohide area above the submenu item
  172. submenu_pum->clear_autohide_areas();
  173. submenu_pum->add_autohide_area(Rect2(this_rect.position.x, this_rect.position.y, this_rect.size.x, items[over]._ofs_cache + scroll_offset + style->get_offset().height - vsep / 2));
  174. // If there is an area below the submenu item, add an autohide area there.
  175. if (items[over]._ofs_cache + items[over]._height_cache + scroll_offset <= control->get_size().height) {
  176. int from = items[over]._ofs_cache + items[over]._height_cache + scroll_offset + vsep / 2 + style->get_offset().height;
  177. submenu_pum->add_autohide_area(Rect2(this_rect.position.x, this_rect.position.y + from, this_rect.size.x, this_rect.size.y - from));
  178. }
  179. }
  180. }
  181. void PopupMenu::_submenu_timeout() {
  182. ERR_FAIL_COND(submenu_over == -1);
  183. if (mouse_over == submenu_over) {
  184. _activate_submenu(mouse_over);
  185. }
  186. submenu_over = -1;
  187. }
  188. void PopupMenu::_gui_input(const Ref<InputEvent> &p_event) {
  189. ERR_FAIL_COND(p_event.is_null());
  190. if (!items.empty()) {
  191. if (p_event->is_action("ui_down") && p_event->is_pressed() && mouse_over != items.size() - 1) {
  192. int search_from = mouse_over + 1;
  193. if (search_from >= items.size()) {
  194. search_from = 0;
  195. }
  196. bool match_found = false;
  197. for (int i = search_from; i < items.size(); i++) {
  198. if (!items[i].separator && !items[i].disabled) {
  199. mouse_over = i;
  200. emit_signal("id_focused", i);
  201. _scroll_to_item(i);
  202. control->update();
  203. control->accept_event();
  204. match_found = true;
  205. break;
  206. }
  207. }
  208. if (!match_found) {
  209. // If the last item is not selectable, try re-searching from the start.
  210. for (int i = 0; i < search_from; i++) {
  211. if (!items[i].separator && !items[i].disabled) {
  212. mouse_over = i;
  213. emit_signal("id_focused", i);
  214. _scroll_to_item(i);
  215. control->update();
  216. control->accept_event();
  217. break;
  218. }
  219. }
  220. }
  221. } else if (p_event->is_action("ui_up") && p_event->is_pressed() && mouse_over != 0) {
  222. int search_from = mouse_over - 1;
  223. if (search_from < 0) {
  224. search_from = items.size() - 1;
  225. }
  226. bool match_found = false;
  227. for (int i = search_from; i >= 0; i--) {
  228. if (!items[i].separator && !items[i].disabled) {
  229. mouse_over = i;
  230. emit_signal("id_focused", i);
  231. _scroll_to_item(i);
  232. control->update();
  233. control->accept_event();
  234. match_found = true;
  235. break;
  236. }
  237. }
  238. if (!match_found) {
  239. // If the first item is not selectable, try re-searching from the end.
  240. for (int i = items.size() - 1; i >= search_from; i--) {
  241. if (!items[i].separator && !items[i].disabled) {
  242. mouse_over = i;
  243. emit_signal("id_focused", i);
  244. _scroll_to_item(i);
  245. control->update();
  246. control->accept_event();
  247. break;
  248. }
  249. }
  250. }
  251. } else if (p_event->is_action("ui_left") && p_event->is_pressed()) {
  252. Node *n = get_parent();
  253. if (n && Object::cast_to<PopupMenu>(n)) {
  254. hide();
  255. control->accept_event();
  256. }
  257. } else if (p_event->is_action("ui_right") && p_event->is_pressed()) {
  258. if (mouse_over >= 0 && mouse_over < items.size() && !items[mouse_over].separator && items[mouse_over].submenu != "" && submenu_over != mouse_over) {
  259. _activate_submenu(mouse_over, true);
  260. control->accept_event();
  261. }
  262. } else if (p_event->is_action("ui_accept") && p_event->is_pressed()) {
  263. if (mouse_over >= 0 && mouse_over < items.size() && !items[mouse_over].separator) {
  264. if (items[mouse_over].submenu != "" && submenu_over != mouse_over) {
  265. _activate_submenu(mouse_over, true);
  266. } else {
  267. activate_item(mouse_over);
  268. }
  269. control->accept_event();
  270. }
  271. }
  272. }
  273. // Make an area which does not include v scrollbar, so that items are not activated when dragging scrollbar.
  274. Transform2D xform = get_global_transform_with_canvas();
  275. Point2 item_origin = xform.get_origin();
  276. float scroll_width = scroll_container->get_v_scrollbar()->is_visible_in_tree() ? scroll_container->get_v_scrollbar()->get_size().width : 0;
  277. Size2 item_size = (control->get_global_rect().get_size() - Vector2(scroll_width, 0)) * xform.get_scale();
  278. Rect2 item_clickable_area = Rect2(item_origin, item_size);
  279. Ref<InputEventMouseButton> b = p_event;
  280. if (b.is_valid()) {
  281. if (!item_clickable_area.has_point(b->get_global_position())) {
  282. return;
  283. }
  284. int button_idx = b->get_button_index();
  285. if (!b->is_pressed()) {
  286. // Allow activating item by releasing the LMB or any that was down when the popup appeared.
  287. // However, if button was not held when opening menu, do not allow release to activate item.
  288. if (button_idx == BUTTON_LEFT || (initial_button_mask & (1 << (button_idx - 1)))) {
  289. bool was_during_grabbed_click = during_grabbed_click;
  290. during_grabbed_click = false;
  291. initial_button_mask = 0;
  292. int over = _get_mouse_over(b->get_position());
  293. if (invalidated_click) {
  294. invalidated_click = false;
  295. return;
  296. }
  297. if (over < 0) {
  298. if (!was_during_grabbed_click) {
  299. hide();
  300. }
  301. return;
  302. }
  303. if (items[over].separator || items[over].disabled) {
  304. return;
  305. }
  306. if (items[over].submenu != "") {
  307. _activate_submenu(over);
  308. return;
  309. }
  310. activate_item(over);
  311. }
  312. }
  313. }
  314. Ref<InputEventMouseMotion> m = p_event;
  315. if (m.is_valid()) {
  316. if (invalidated_click) {
  317. moved += m->get_relative();
  318. if (moved.length() > 4) {
  319. invalidated_click = false;
  320. }
  321. }
  322. for (List<Rect2>::Element *E = autohide_areas.front(); E; E = E->next()) {
  323. if (!Rect2(Point2(), get_size()).has_point(m->get_position()) && E->get().has_point(m->get_position())) {
  324. call_deferred("hide");
  325. return;
  326. }
  327. }
  328. if (!item_clickable_area.has_point(m->get_global_position())) {
  329. return;
  330. }
  331. int over = _get_mouse_over(m->get_position());
  332. int id = (over < 0 || items[over].separator || items[over].disabled) ? -1 : (items[over].id >= 0 ? items[over].id : over);
  333. if (id < 0) {
  334. mouse_over = -1;
  335. control->update();
  336. return;
  337. }
  338. if (items[over].submenu != "" && submenu_over != over) {
  339. submenu_over = over;
  340. submenu_timer->start();
  341. }
  342. if (over != mouse_over) {
  343. mouse_over = over;
  344. control->update();
  345. }
  346. }
  347. Ref<InputEventKey> k = p_event;
  348. if (allow_search && k.is_valid() && k->get_unicode() && k->is_pressed()) {
  349. uint64_t now = OS::get_singleton()->get_ticks_msec();
  350. uint64_t diff = now - search_time_msec;
  351. uint64_t max_interval = uint64_t(GLOBAL_DEF("gui/timers/incremental_search_max_interval_msec", 2000));
  352. search_time_msec = now;
  353. if (diff > max_interval) {
  354. search_string = "";
  355. }
  356. if (String::chr(k->get_unicode()) != search_string) {
  357. search_string += String::chr(k->get_unicode());
  358. }
  359. for (int i = mouse_over + 1; i <= items.size(); i++) {
  360. if (i == items.size()) {
  361. if (mouse_over <= 0) {
  362. break;
  363. } else {
  364. i = 0;
  365. }
  366. }
  367. if (i == mouse_over) {
  368. break;
  369. }
  370. if (items[i].text.findn(search_string) == 0) {
  371. mouse_over = i;
  372. emit_signal("id_focused", i);
  373. _scroll_to_item(i);
  374. control->update();
  375. control->accept_event();
  376. break;
  377. }
  378. }
  379. }
  380. }
  381. void PopupMenu::_draw_items() {
  382. control->set_custom_minimum_size(Size2(0, _get_items_total_height()));
  383. RID ci = control->get_canvas_item();
  384. Size2 margin_size;
  385. margin_size.width = margin_container->get_constant("margin_right") + margin_container->get_constant("margin_left");
  386. margin_size.height = margin_container->get_constant("margin_top") + margin_container->get_constant("margin_bottom");
  387. Ref<StyleBox> style = get_stylebox("panel");
  388. Ref<StyleBox> hover = get_stylebox("hover");
  389. Ref<Font> font = get_font("font");
  390. select_font(font);
  391. // In Item::checkable_type enum order (less the non-checkable member)
  392. Ref<Texture> check[] = { get_icon("checked"), get_icon("radio_checked") };
  393. Ref<Texture> uncheck[] = { get_icon("unchecked"), get_icon("radio_unchecked") };
  394. Ref<Texture> submenu = get_icon("submenu");
  395. Ref<StyleBox> separator = get_stylebox("separator");
  396. Ref<StyleBox> labeled_separator_left = get_stylebox("labeled_separator_left");
  397. Ref<StyleBox> labeled_separator_right = get_stylebox("labeled_separator_right");
  398. int vseparation = get_constant("vseparation");
  399. int hseparation = get_constant("hseparation");
  400. Color font_color = get_color("font_color");
  401. Color font_color_disabled = get_color("font_color_disabled");
  402. Color font_color_accel = get_color("font_color_accel");
  403. Color font_color_hover = get_color("font_color_hover");
  404. float font_h = font->get_height();
  405. float scroll_width = scroll_container->get_v_scrollbar()->is_visible_in_tree() ? scroll_container->get_v_scrollbar()->get_size().width : 0;
  406. float display_width = control->get_size().width - scroll_width;
  407. // Find the widest icon and whether any items have a checkbox, and store the offsets for each.
  408. float icon_ofs = 0.0;
  409. bool has_check = false;
  410. for (int i = 0; i < items.size(); i++) {
  411. icon_ofs = MAX(items[i].get_icon_size().width, icon_ofs);
  412. if (items[i].checkable_type) {
  413. has_check = true;
  414. }
  415. }
  416. if (icon_ofs > 0.0) {
  417. icon_ofs += hseparation;
  418. }
  419. float check_ofs = 0.0;
  420. if (has_check) {
  421. check_ofs = MAX(get_icon("checked")->get_width(), get_icon("radio_checked")->get_width()) + hseparation;
  422. }
  423. Point2 ofs = Point2();
  424. // Loop through all items and draw each.
  425. for (int i = 0; i < items.size(); i++) {
  426. // If not the first item, add the separation space between items.
  427. if (i > 0) {
  428. ofs.y += vseparation;
  429. }
  430. Point2 item_ofs = ofs;
  431. Size2 icon_size = items[i].get_icon_size();
  432. float h = MAX(icon_size.height, font_h);
  433. if (i == mouse_over) {
  434. hover->draw(ci, Rect2(item_ofs + Point2(-hseparation, -vseparation / 2), Size2(display_width + hseparation * 2, h + vseparation)));
  435. }
  436. String text = items[i].xl_text;
  437. // Separator
  438. item_ofs.x += items[i].h_ofs;
  439. if (items[i].separator) {
  440. int sep_h = separator->get_center_size().height + separator->get_minimum_size().height;
  441. if (text != String()) {
  442. int text_size = font->get_string_size(text).width;
  443. int text_center = display_width / 2;
  444. int text_left = text_center - text_size / 2;
  445. int text_right = text_center + text_size / 2;
  446. if (text_left > item_ofs.x) {
  447. labeled_separator_left->draw(ci, Rect2(item_ofs + Point2(0, Math::floor((h - sep_h) / 2.0)), Size2(MAX(0, text_left - item_ofs.x), sep_h)));
  448. }
  449. if (text_right < display_width) {
  450. labeled_separator_right->draw(ci, Rect2(Point2(text_right, item_ofs.y + Math::floor((h - sep_h) / 2.0)), Size2(MAX(0, display_width - text_right), sep_h)));
  451. }
  452. } else {
  453. separator->draw(ci, Rect2(item_ofs + Point2(0, Math::floor((h - sep_h) / 2.0)), Size2(display_width, sep_h)));
  454. }
  455. }
  456. Color icon_color(1, 1, 1, items[i].disabled ? 0.5 : 1);
  457. // Checkboxes
  458. if (items[i].checkable_type) {
  459. Texture *icon = (items[i].checked ? check[items[i].checkable_type - 1] : uncheck[items[i].checkable_type - 1]).ptr();
  460. icon->draw(ci, item_ofs + Point2(0, Math::floor((h - icon->get_height()) / 2.0)), icon_color);
  461. }
  462. // Icon
  463. if (!items[i].icon.is_null()) {
  464. items[i].icon->draw(ci, item_ofs + Size2(check_ofs, 0) + Point2(0, Math::floor((h - icon_size.height) / 2.0)), icon_color);
  465. }
  466. // Submenu arrow on right hand side
  467. if (items[i].submenu != "") {
  468. submenu->draw(ci, Point2(display_width - submenu->get_width(), item_ofs.y + Math::floor(h - submenu->get_height()) / 2), icon_color);
  469. }
  470. // Text
  471. item_ofs.y += font->get_ascent();
  472. if (items[i].separator) {
  473. if (text != String()) {
  474. int center = (display_width - font->get_string_size(text).width) / 2;
  475. font->draw(ci, Point2(center, item_ofs.y + Math::floor((h - font_h) / 2.0)), text, font_color_disabled);
  476. }
  477. } else {
  478. item_ofs.x += icon_ofs + check_ofs;
  479. font->draw(ci, item_ofs + Point2(0, Math::floor((h - font_h) / 2.0)), text, items[i].disabled ? font_color_disabled : (i == mouse_over ? font_color_hover : font_color));
  480. }
  481. // Accelerator / Shortcut
  482. if (items[i].accel || (items[i].shortcut.is_valid() && items[i].shortcut->is_valid())) {
  483. String sc_text = _get_accel_text(i);
  484. item_ofs.x = display_width - font->get_string_size(sc_text).width;
  485. font->draw(ci, item_ofs + Point2(0, Math::floor((h - font_h) / 2.0)), sc_text, i == mouse_over ? font_color_hover : font_color_accel);
  486. }
  487. // Cache the item vertical offset from the first item and the height
  488. items.write[i]._ofs_cache = ofs.y;
  489. items.write[i]._height_cache = h;
  490. ofs.y += h;
  491. }
  492. }
  493. void PopupMenu::_draw_background() {
  494. Ref<StyleBox> style = get_stylebox("panel");
  495. RID ci2 = margin_container->get_canvas_item();
  496. style->draw(ci2, Rect2(Point2(), margin_container->get_size()));
  497. }
  498. bool PopupMenu::has_point(const Point2 &p_point) const {
  499. if (parent_rect.has_point(p_point)) {
  500. return true;
  501. }
  502. for (const List<Rect2>::Element *E = autohide_areas.front(); E; E = E->next()) {
  503. if (E->get().has_point(p_point)) {
  504. return true;
  505. }
  506. }
  507. return Control::has_point(p_point);
  508. }
  509. void PopupMenu::_notification(int p_what) {
  510. switch (p_what) {
  511. case NOTIFICATION_ENTER_TREE: {
  512. PopupMenu *pm = Object::cast_to<PopupMenu>(get_parent());
  513. if (pm) {
  514. // Inherit submenu's popup delay time from parent menu
  515. float pm_delay = pm->get_submenu_popup_delay();
  516. set_submenu_popup_delay(pm_delay);
  517. }
  518. } break;
  519. case NOTIFICATION_TRANSLATION_CHANGED: {
  520. for (int i = 0; i < items.size(); i++) {
  521. items.write[i].xl_text = tr(items[i].text);
  522. }
  523. minimum_size_changed();
  524. control->update();
  525. } break;
  526. case MainLoop::NOTIFICATION_WM_FOCUS_OUT: {
  527. if (hide_on_window_lose_focus) {
  528. hide();
  529. }
  530. } break;
  531. case NOTIFICATION_MOUSE_ENTER: {
  532. grab_focus();
  533. } break;
  534. case NOTIFICATION_MOUSE_EXIT: {
  535. if (mouse_over >= 0 && (items[mouse_over].submenu == "" || submenu_over != -1)) {
  536. mouse_over = -1;
  537. control->update();
  538. }
  539. } break;
  540. case NOTIFICATION_POST_POPUP: {
  541. initial_button_mask = Input::get_singleton()->get_mouse_button_mask();
  542. during_grabbed_click = (bool)initial_button_mask;
  543. } break;
  544. case NOTIFICATION_VISIBILITY_CHANGED: {
  545. // Set margin on the margin container
  546. Ref<StyleBox> panel_style = get_stylebox("panel");
  547. margin_container->add_constant_override("margin_top", panel_style->get_margin(Margin::MARGIN_TOP));
  548. margin_container->add_constant_override("margin_bottom", panel_style->get_margin(Margin::MARGIN_BOTTOM));
  549. margin_container->add_constant_override("margin_left", panel_style->get_margin(Margin::MARGIN_LEFT));
  550. margin_container->add_constant_override("margin_right", panel_style->get_margin(Margin::MARGIN_RIGHT));
  551. } break;
  552. case NOTIFICATION_POPUP_HIDE: {
  553. if (mouse_over >= 0) {
  554. mouse_over = -1;
  555. control->update();
  556. }
  557. for (int i = 0; i < items.size(); i++) {
  558. if (items[i].submenu == "") {
  559. continue;
  560. }
  561. Node *n = get_node(items[i].submenu);
  562. if (!n) {
  563. continue;
  564. }
  565. PopupMenu *pm = Object::cast_to<PopupMenu>(n);
  566. if (!pm || !pm->is_visible()) {
  567. continue;
  568. }
  569. pm->hide();
  570. }
  571. } break;
  572. }
  573. }
  574. /* Methods to add items with or without icon, checkbox, shortcut.
  575. * Be sure to keep them in sync when adding new properties in the Item struct.
  576. */
  577. #define ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel) \
  578. item.text = p_label; \
  579. item.xl_text = tr(p_label); \
  580. item.id = p_id == -1 ? items.size() : p_id; \
  581. item.accel = p_accel;
  582. void PopupMenu::add_item(const String &p_label, int p_id, uint32_t p_accel) {
  583. Item item;
  584. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  585. items.push_back(item);
  586. control->update();
  587. minimum_size_changed();
  588. }
  589. void PopupMenu::add_icon_item(const Ref<Texture> &p_icon, const String &p_label, int p_id, uint32_t p_accel) {
  590. Item item;
  591. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  592. item.icon = p_icon;
  593. items.push_back(item);
  594. control->update();
  595. minimum_size_changed();
  596. }
  597. void PopupMenu::add_check_item(const String &p_label, int p_id, uint32_t p_accel) {
  598. Item item;
  599. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  600. item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX;
  601. items.push_back(item);
  602. control->update();
  603. minimum_size_changed();
  604. }
  605. void PopupMenu::add_icon_check_item(const Ref<Texture> &p_icon, const String &p_label, int p_id, uint32_t p_accel) {
  606. Item item;
  607. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  608. item.icon = p_icon;
  609. item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX;
  610. items.push_back(item);
  611. control->update();
  612. minimum_size_changed();
  613. }
  614. void PopupMenu::add_radio_check_item(const String &p_label, int p_id, uint32_t p_accel) {
  615. Item item;
  616. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  617. item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON;
  618. items.push_back(item);
  619. control->update();
  620. minimum_size_changed();
  621. }
  622. void PopupMenu::add_icon_radio_check_item(const Ref<Texture> &p_icon, const String &p_label, int p_id, uint32_t p_accel) {
  623. Item item;
  624. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  625. item.icon = p_icon;
  626. item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON;
  627. items.push_back(item);
  628. control->update();
  629. minimum_size_changed();
  630. }
  631. void PopupMenu::add_multistate_item(const String &p_label, int p_max_states, int p_default_state, int p_id, uint32_t p_accel) {
  632. Item item;
  633. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  634. item.max_states = p_max_states;
  635. item.state = p_default_state;
  636. items.push_back(item);
  637. control->update();
  638. minimum_size_changed();
  639. }
  640. #define ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global) \
  641. ERR_FAIL_COND_MSG(p_shortcut.is_null(), "Cannot add item with invalid ShortCut."); \
  642. _ref_shortcut(p_shortcut); \
  643. item.text = p_shortcut->get_name(); \
  644. item.xl_text = tr(item.text); \
  645. item.id = p_id == -1 ? items.size() : p_id; \
  646. item.shortcut = p_shortcut; \
  647. item.shortcut_is_global = p_global;
  648. void PopupMenu::add_shortcut(const Ref<ShortCut> &p_shortcut, int p_id, bool p_global) {
  649. Item item;
  650. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global);
  651. items.push_back(item);
  652. control->update();
  653. minimum_size_changed();
  654. }
  655. void PopupMenu::add_icon_shortcut(const Ref<Texture> &p_icon, const Ref<ShortCut> &p_shortcut, int p_id, bool p_global) {
  656. Item item;
  657. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global);
  658. item.icon = p_icon;
  659. items.push_back(item);
  660. control->update();
  661. minimum_size_changed();
  662. }
  663. void PopupMenu::add_check_shortcut(const Ref<ShortCut> &p_shortcut, int p_id, bool p_global) {
  664. Item item;
  665. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global);
  666. item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX;
  667. items.push_back(item);
  668. control->update();
  669. minimum_size_changed();
  670. }
  671. void PopupMenu::add_icon_check_shortcut(const Ref<Texture> &p_icon, const Ref<ShortCut> &p_shortcut, int p_id, bool p_global) {
  672. Item item;
  673. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global);
  674. item.icon = p_icon;
  675. item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX;
  676. items.push_back(item);
  677. control->update();
  678. minimum_size_changed();
  679. }
  680. void PopupMenu::add_radio_check_shortcut(const Ref<ShortCut> &p_shortcut, int p_id, bool p_global) {
  681. Item item;
  682. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global);
  683. item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON;
  684. items.push_back(item);
  685. control->update();
  686. minimum_size_changed();
  687. }
  688. void PopupMenu::add_icon_radio_check_shortcut(const Ref<Texture> &p_icon, const Ref<ShortCut> &p_shortcut, int p_id, bool p_global) {
  689. Item item;
  690. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global);
  691. item.icon = p_icon;
  692. item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON;
  693. items.push_back(item);
  694. control->update();
  695. minimum_size_changed();
  696. }
  697. void PopupMenu::add_submenu_item(const String &p_label, const String &p_submenu, int p_id) {
  698. Item item;
  699. item.text = p_label;
  700. item.xl_text = tr(p_label);
  701. item.id = p_id == -1 ? items.size() : p_id;
  702. item.submenu = p_submenu;
  703. items.push_back(item);
  704. control->update();
  705. minimum_size_changed();
  706. }
  707. #undef ITEM_SETUP_WITH_ACCEL
  708. #undef ITEM_SETUP_WITH_SHORTCUT
  709. /* Methods to modify existing items. */
  710. void PopupMenu::set_item_text(int p_idx, const String &p_text) {
  711. ERR_FAIL_INDEX(p_idx, items.size());
  712. items.write[p_idx].text = p_text;
  713. items.write[p_idx].xl_text = tr(p_text);
  714. control->update();
  715. minimum_size_changed();
  716. }
  717. void PopupMenu::set_item_icon(int p_idx, const Ref<Texture> &p_icon) {
  718. ERR_FAIL_INDEX(p_idx, items.size());
  719. items.write[p_idx].icon = p_icon;
  720. control->update();
  721. minimum_size_changed();
  722. }
  723. void PopupMenu::set_item_checked(int p_idx, bool p_checked) {
  724. ERR_FAIL_INDEX(p_idx, items.size());
  725. items.write[p_idx].checked = p_checked;
  726. control->update();
  727. minimum_size_changed();
  728. }
  729. void PopupMenu::set_item_id(int p_idx, int p_id) {
  730. ERR_FAIL_INDEX(p_idx, items.size());
  731. items.write[p_idx].id = p_id;
  732. control->update();
  733. minimum_size_changed();
  734. }
  735. void PopupMenu::set_item_accelerator(int p_idx, uint32_t p_accel) {
  736. ERR_FAIL_INDEX(p_idx, items.size());
  737. items.write[p_idx].accel = p_accel;
  738. control->update();
  739. minimum_size_changed();
  740. }
  741. void PopupMenu::set_item_metadata(int p_idx, const Variant &p_meta) {
  742. ERR_FAIL_INDEX(p_idx, items.size());
  743. items.write[p_idx].metadata = p_meta;
  744. control->update();
  745. minimum_size_changed();
  746. }
  747. void PopupMenu::set_item_disabled(int p_idx, bool p_disabled) {
  748. ERR_FAIL_INDEX(p_idx, items.size());
  749. items.write[p_idx].disabled = p_disabled;
  750. control->update();
  751. minimum_size_changed();
  752. }
  753. void PopupMenu::set_item_submenu(int p_idx, const String &p_submenu) {
  754. ERR_FAIL_INDEX(p_idx, items.size());
  755. items.write[p_idx].submenu = p_submenu;
  756. control->update();
  757. minimum_size_changed();
  758. }
  759. void PopupMenu::toggle_item_checked(int p_idx) {
  760. ERR_FAIL_INDEX(p_idx, items.size());
  761. items.write[p_idx].checked = !items[p_idx].checked;
  762. control->update();
  763. minimum_size_changed();
  764. }
  765. String PopupMenu::get_item_text(int p_idx) const {
  766. ERR_FAIL_INDEX_V(p_idx, items.size(), "");
  767. return items[p_idx].text;
  768. }
  769. int PopupMenu::get_item_idx_from_text(const String &text) const {
  770. for (int idx = 0; idx < items.size(); idx++) {
  771. if (items[idx].text == text) {
  772. return idx;
  773. }
  774. }
  775. return -1;
  776. }
  777. Ref<Texture> PopupMenu::get_item_icon(int p_idx) const {
  778. ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<Texture>());
  779. return items[p_idx].icon;
  780. }
  781. uint32_t PopupMenu::get_item_accelerator(int p_idx) const {
  782. ERR_FAIL_INDEX_V(p_idx, items.size(), 0);
  783. return items[p_idx].accel;
  784. }
  785. Variant PopupMenu::get_item_metadata(int p_idx) const {
  786. ERR_FAIL_INDEX_V(p_idx, items.size(), Variant());
  787. return items[p_idx].metadata;
  788. }
  789. bool PopupMenu::is_item_disabled(int p_idx) const {
  790. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  791. return items[p_idx].disabled;
  792. }
  793. bool PopupMenu::is_item_checked(int p_idx) const {
  794. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  795. return items[p_idx].checked;
  796. }
  797. int PopupMenu::get_item_id(int p_idx) const {
  798. ERR_FAIL_INDEX_V(p_idx, items.size(), 0);
  799. return items[p_idx].id;
  800. }
  801. int PopupMenu::get_item_index(int p_id) const {
  802. for (int i = 0; i < items.size(); i++) {
  803. if (items[i].id == p_id) {
  804. return i;
  805. }
  806. }
  807. return -1;
  808. }
  809. String PopupMenu::get_item_submenu(int p_idx) const {
  810. ERR_FAIL_INDEX_V(p_idx, items.size(), "");
  811. return items[p_idx].submenu;
  812. }
  813. String PopupMenu::get_item_tooltip(int p_idx) const {
  814. ERR_FAIL_INDEX_V(p_idx, items.size(), "");
  815. return items[p_idx].tooltip;
  816. }
  817. Ref<ShortCut> PopupMenu::get_item_shortcut(int p_idx) const {
  818. ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<ShortCut>());
  819. return items[p_idx].shortcut;
  820. }
  821. int PopupMenu::get_item_state(int p_idx) const {
  822. ERR_FAIL_INDEX_V(p_idx, items.size(), -1);
  823. return items[p_idx].state;
  824. }
  825. void PopupMenu::set_item_as_separator(int p_idx, bool p_separator) {
  826. ERR_FAIL_INDEX(p_idx, items.size());
  827. items.write[p_idx].separator = p_separator;
  828. control->update();
  829. }
  830. bool PopupMenu::is_item_separator(int p_idx) const {
  831. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  832. return items[p_idx].separator;
  833. }
  834. void PopupMenu::set_item_as_checkable(int p_idx, bool p_checkable) {
  835. ERR_FAIL_INDEX(p_idx, items.size());
  836. items.write[p_idx].checkable_type = p_checkable ? Item::CHECKABLE_TYPE_CHECK_BOX : Item::CHECKABLE_TYPE_NONE;
  837. control->update();
  838. }
  839. void PopupMenu::set_item_as_radio_checkable(int p_idx, bool p_radio_checkable) {
  840. ERR_FAIL_INDEX(p_idx, items.size());
  841. items.write[p_idx].checkable_type = p_radio_checkable ? Item::CHECKABLE_TYPE_RADIO_BUTTON : Item::CHECKABLE_TYPE_NONE;
  842. control->update();
  843. }
  844. void PopupMenu::set_item_tooltip(int p_idx, const String &p_tooltip) {
  845. ERR_FAIL_INDEX(p_idx, items.size());
  846. items.write[p_idx].tooltip = p_tooltip;
  847. control->update();
  848. }
  849. void PopupMenu::set_item_shortcut(int p_idx, const Ref<ShortCut> &p_shortcut, bool p_global) {
  850. ERR_FAIL_INDEX(p_idx, items.size());
  851. if (items[p_idx].shortcut.is_valid()) {
  852. _unref_shortcut(items[p_idx].shortcut);
  853. }
  854. items.write[p_idx].shortcut = p_shortcut;
  855. items.write[p_idx].shortcut_is_global = p_global;
  856. if (items[p_idx].shortcut.is_valid()) {
  857. _ref_shortcut(items[p_idx].shortcut);
  858. }
  859. control->update();
  860. }
  861. void PopupMenu::set_item_h_offset(int p_idx, int p_offset) {
  862. ERR_FAIL_INDEX(p_idx, items.size());
  863. items.write[p_idx].h_ofs = p_offset;
  864. control->update();
  865. minimum_size_changed();
  866. }
  867. void PopupMenu::set_item_multistate(int p_idx, int p_state) {
  868. ERR_FAIL_INDEX(p_idx, items.size());
  869. items.write[p_idx].state = p_state;
  870. control->update();
  871. }
  872. void PopupMenu::set_item_shortcut_disabled(int p_idx, bool p_disabled) {
  873. ERR_FAIL_INDEX(p_idx, items.size());
  874. items.write[p_idx].shortcut_is_disabled = p_disabled;
  875. control->update();
  876. }
  877. void PopupMenu::toggle_item_multistate(int p_idx) {
  878. ERR_FAIL_INDEX(p_idx, items.size());
  879. if (0 >= items[p_idx].max_states) {
  880. return;
  881. }
  882. ++items.write[p_idx].state;
  883. if (items.write[p_idx].max_states <= items[p_idx].state) {
  884. items.write[p_idx].state = 0;
  885. }
  886. control->update();
  887. }
  888. bool PopupMenu::is_item_checkable(int p_idx) const {
  889. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  890. return items[p_idx].checkable_type;
  891. }
  892. bool PopupMenu::is_item_radio_checkable(int p_idx) const {
  893. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  894. return items[p_idx].checkable_type == Item::CHECKABLE_TYPE_RADIO_BUTTON;
  895. }
  896. bool PopupMenu::is_item_shortcut_disabled(int p_idx) const {
  897. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  898. return items[p_idx].shortcut_is_disabled;
  899. }
  900. void PopupMenu::set_current_index(int p_idx) {
  901. if (p_idx != -1) {
  902. ERR_FAIL_INDEX(p_idx, items.size());
  903. }
  904. if (mouse_over == p_idx) {
  905. return;
  906. }
  907. mouse_over = p_idx;
  908. control->update();
  909. }
  910. int PopupMenu::get_current_index() const {
  911. return mouse_over;
  912. }
  913. int PopupMenu::get_item_count() const {
  914. return items.size();
  915. }
  916. bool PopupMenu::activate_item_by_event(const Ref<InputEvent> &p_event, bool p_for_global_only) {
  917. uint32_t code = 0;
  918. Ref<InputEventKey> k = p_event;
  919. if (k.is_valid()) {
  920. code = k->get_scancode();
  921. if (code == 0) {
  922. code = k->get_unicode();
  923. }
  924. if (k->get_control()) {
  925. code |= KEY_MASK_CTRL;
  926. }
  927. if (k->get_alt()) {
  928. code |= KEY_MASK_ALT;
  929. }
  930. if (k->get_metakey()) {
  931. code |= KEY_MASK_META;
  932. }
  933. if (k->get_shift()) {
  934. code |= KEY_MASK_SHIFT;
  935. }
  936. }
  937. for (int i = 0; i < items.size(); i++) {
  938. if (is_item_disabled(i) || items[i].shortcut_is_disabled) {
  939. continue;
  940. }
  941. if (items[i].shortcut.is_valid() && items[i].shortcut->is_shortcut(p_event) && (items[i].shortcut_is_global || !p_for_global_only)) {
  942. activate_item(i);
  943. return true;
  944. }
  945. if (code != 0 && items[i].accel == code) {
  946. activate_item(i);
  947. return true;
  948. }
  949. if (items[i].submenu != "") {
  950. Node *n = get_node(items[i].submenu);
  951. if (!n) {
  952. continue;
  953. }
  954. PopupMenu *pm = Object::cast_to<PopupMenu>(n);
  955. if (!pm) {
  956. continue;
  957. }
  958. if (pm->activate_item_by_event(p_event, p_for_global_only)) {
  959. return true;
  960. }
  961. }
  962. }
  963. return false;
  964. }
  965. void PopupMenu::activate_item(int p_item) {
  966. ERR_FAIL_INDEX(p_item, items.size());
  967. ERR_FAIL_COND(items[p_item].separator);
  968. int id = items[p_item].id >= 0 ? items[p_item].id : p_item;
  969. //hide all parent PopupMenus
  970. Node *next = get_parent();
  971. PopupMenu *pop = Object::cast_to<PopupMenu>(next);
  972. while (pop) {
  973. // We close all parents that are chained together,
  974. // with hide_on_item_selection enabled
  975. if (items[p_item].checkable_type) {
  976. if (!hide_on_checkable_item_selection || !pop->is_hide_on_checkable_item_selection()) {
  977. break;
  978. }
  979. } else if (0 < items[p_item].max_states) {
  980. if (!hide_on_multistate_item_selection || !pop->is_hide_on_multistate_item_selection()) {
  981. break;
  982. }
  983. } else if (!hide_on_item_selection || !pop->is_hide_on_item_selection()) {
  984. break;
  985. }
  986. pop->hide();
  987. next = next->get_parent();
  988. pop = Object::cast_to<PopupMenu>(next);
  989. }
  990. // Hides popup by default; unless otherwise specified
  991. // by using set_hide_on_item_selection and set_hide_on_checkable_item_selection
  992. bool need_hide = true;
  993. if (items[p_item].checkable_type) {
  994. if (!hide_on_checkable_item_selection) {
  995. need_hide = false;
  996. }
  997. } else if (0 < items[p_item].max_states) {
  998. if (!hide_on_multistate_item_selection) {
  999. need_hide = false;
  1000. }
  1001. } else if (!hide_on_item_selection) {
  1002. need_hide = false;
  1003. }
  1004. emit_signal("id_pressed", id);
  1005. emit_signal("index_pressed", p_item);
  1006. if (need_hide) {
  1007. hide();
  1008. }
  1009. }
  1010. void PopupMenu::remove_item(int p_idx) {
  1011. ERR_FAIL_INDEX(p_idx, items.size());
  1012. if (items[p_idx].shortcut.is_valid()) {
  1013. _unref_shortcut(items[p_idx].shortcut);
  1014. }
  1015. items.remove(p_idx);
  1016. control->update();
  1017. minimum_size_changed();
  1018. }
  1019. void PopupMenu::add_separator(const String &p_text, int p_id) {
  1020. Item sep;
  1021. sep.separator = true;
  1022. sep.id = p_id;
  1023. if (p_text != String()) {
  1024. sep.text = p_text;
  1025. sep.xl_text = tr(p_text);
  1026. }
  1027. items.push_back(sep);
  1028. control->update();
  1029. }
  1030. void PopupMenu::clear() {
  1031. for (int i = 0; i < items.size(); i++) {
  1032. if (items[i].shortcut.is_valid()) {
  1033. _unref_shortcut(items[i].shortcut);
  1034. }
  1035. }
  1036. items.clear();
  1037. mouse_over = -1;
  1038. control->update();
  1039. minimum_size_changed();
  1040. }
  1041. Array PopupMenu::_get_items() const {
  1042. Array items;
  1043. for (int i = 0; i < get_item_count(); i++) {
  1044. items.push_back(get_item_text(i));
  1045. items.push_back(get_item_icon(i));
  1046. // For compatibility, use false/true for no/checkbox and integers for other values
  1047. int ct = this->items[i].checkable_type;
  1048. items.push_back(Variant(ct <= Item::CHECKABLE_TYPE_CHECK_BOX ? is_item_checkable(i) : ct));
  1049. items.push_back(is_item_checked(i));
  1050. items.push_back(is_item_disabled(i));
  1051. items.push_back(get_item_id(i));
  1052. items.push_back(get_item_accelerator(i));
  1053. items.push_back(get_item_metadata(i));
  1054. items.push_back(get_item_submenu(i));
  1055. items.push_back(is_item_separator(i));
  1056. }
  1057. return items;
  1058. }
  1059. void PopupMenu::_ref_shortcut(Ref<ShortCut> p_sc) {
  1060. if (!shortcut_refcount.has(p_sc)) {
  1061. shortcut_refcount[p_sc] = 1;
  1062. p_sc->connect("changed", this, "update");
  1063. } else {
  1064. shortcut_refcount[p_sc] += 1;
  1065. }
  1066. }
  1067. void PopupMenu::_unref_shortcut(Ref<ShortCut> p_sc) {
  1068. ERR_FAIL_COND(!shortcut_refcount.has(p_sc));
  1069. shortcut_refcount[p_sc]--;
  1070. if (shortcut_refcount[p_sc] == 0) {
  1071. p_sc->disconnect("changed", this, "update");
  1072. shortcut_refcount.erase(p_sc);
  1073. }
  1074. }
  1075. void PopupMenu::_set_items(const Array &p_items) {
  1076. ERR_FAIL_COND(p_items.size() % 10);
  1077. clear();
  1078. for (int i = 0; i < p_items.size(); i += 10) {
  1079. String text = p_items[i + 0];
  1080. Ref<Texture> icon = p_items[i + 1];
  1081. // For compatibility, use false/true for no/checkbox and integers for other values
  1082. bool checkable = p_items[i + 2];
  1083. bool radio_checkable = (int)p_items[i + 2] == Item::CHECKABLE_TYPE_RADIO_BUTTON;
  1084. bool checked = p_items[i + 3];
  1085. bool disabled = p_items[i + 4];
  1086. int id = p_items[i + 5];
  1087. int accel = p_items[i + 6];
  1088. Variant meta = p_items[i + 7];
  1089. String subm = p_items[i + 8];
  1090. bool sep = p_items[i + 9];
  1091. int idx = get_item_count();
  1092. add_item(text, id);
  1093. set_item_icon(idx, icon);
  1094. if (checkable) {
  1095. if (radio_checkable) {
  1096. set_item_as_radio_checkable(idx, true);
  1097. } else {
  1098. set_item_as_checkable(idx, true);
  1099. }
  1100. }
  1101. set_item_checked(idx, checked);
  1102. set_item_disabled(idx, disabled);
  1103. set_item_id(idx, id);
  1104. set_item_metadata(idx, meta);
  1105. set_item_as_separator(idx, sep);
  1106. set_item_accelerator(idx, accel);
  1107. set_item_submenu(idx, subm);
  1108. }
  1109. }
  1110. // Hide on item selection determines whether or not the popup will close after item selection
  1111. void PopupMenu::set_hide_on_item_selection(bool p_enabled) {
  1112. hide_on_item_selection = p_enabled;
  1113. }
  1114. bool PopupMenu::is_hide_on_item_selection() const {
  1115. return hide_on_item_selection;
  1116. }
  1117. void PopupMenu::set_hide_on_checkable_item_selection(bool p_enabled) {
  1118. hide_on_checkable_item_selection = p_enabled;
  1119. }
  1120. bool PopupMenu::is_hide_on_checkable_item_selection() const {
  1121. return hide_on_checkable_item_selection;
  1122. }
  1123. void PopupMenu::set_hide_on_multistate_item_selection(bool p_enabled) {
  1124. hide_on_multistate_item_selection = p_enabled;
  1125. }
  1126. bool PopupMenu::is_hide_on_multistate_item_selection() const {
  1127. return hide_on_multistate_item_selection;
  1128. }
  1129. void PopupMenu::set_submenu_popup_delay(float p_time) {
  1130. if (p_time <= 0) {
  1131. p_time = 0.01;
  1132. }
  1133. submenu_timer->set_wait_time(p_time);
  1134. }
  1135. float PopupMenu::get_submenu_popup_delay() const {
  1136. return submenu_timer->get_wait_time();
  1137. }
  1138. void PopupMenu::set_allow_search(bool p_allow) {
  1139. allow_search = p_allow;
  1140. }
  1141. bool PopupMenu::get_allow_search() const {
  1142. return allow_search;
  1143. }
  1144. void PopupMenu::set_max_height(real_t p_max_height) {
  1145. max_height = p_max_height;
  1146. minimum_size_changed();
  1147. }
  1148. real_t PopupMenu::get_max_height() const {
  1149. return max_height;
  1150. }
  1151. void PopupMenu::set_hide_on_window_lose_focus(bool p_enabled) {
  1152. hide_on_window_lose_focus = p_enabled;
  1153. }
  1154. bool PopupMenu::is_hide_on_window_lose_focus() const {
  1155. return hide_on_window_lose_focus;
  1156. }
  1157. String PopupMenu::get_tooltip(const Point2 &p_pos) const {
  1158. int over = _get_mouse_over(p_pos);
  1159. if (over < 0 || over >= items.size()) {
  1160. return "";
  1161. }
  1162. return items[over].tooltip;
  1163. }
  1164. void PopupMenu::set_parent_rect(const Rect2 &p_rect) {
  1165. parent_rect = p_rect;
  1166. }
  1167. void PopupMenu::get_translatable_strings(List<String> *p_strings) const {
  1168. for (int i = 0; i < items.size(); i++) {
  1169. if (items[i].xl_text != "") {
  1170. p_strings->push_back(items[i].xl_text);
  1171. }
  1172. }
  1173. }
  1174. void PopupMenu::add_autohide_area(const Rect2 &p_area) {
  1175. autohide_areas.push_back(p_area);
  1176. }
  1177. void PopupMenu::clear_autohide_areas() {
  1178. autohide_areas.clear();
  1179. }
  1180. void PopupMenu::_bind_methods() {
  1181. ClassDB::bind_method(D_METHOD("_gui_input"), &PopupMenu::_gui_input);
  1182. ClassDB::bind_method(D_METHOD("add_item", "label", "id", "accel"), &PopupMenu::add_item, DEFVAL(-1), DEFVAL(0));
  1183. ClassDB::bind_method(D_METHOD("add_icon_item", "texture", "label", "id", "accel"), &PopupMenu::add_icon_item, DEFVAL(-1), DEFVAL(0));
  1184. ClassDB::bind_method(D_METHOD("add_check_item", "label", "id", "accel"), &PopupMenu::add_check_item, DEFVAL(-1), DEFVAL(0));
  1185. ClassDB::bind_method(D_METHOD("add_icon_check_item", "texture", "label", "id", "accel"), &PopupMenu::add_icon_check_item, DEFVAL(-1), DEFVAL(0));
  1186. ClassDB::bind_method(D_METHOD("add_radio_check_item", "label", "id", "accel"), &PopupMenu::add_radio_check_item, DEFVAL(-1), DEFVAL(0));
  1187. ClassDB::bind_method(D_METHOD("add_icon_radio_check_item", "texture", "label", "id", "accel"), &PopupMenu::add_icon_radio_check_item, DEFVAL(-1), DEFVAL(0));
  1188. 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));
  1189. ClassDB::bind_method(D_METHOD("add_shortcut", "shortcut", "id", "global"), &PopupMenu::add_shortcut, DEFVAL(-1), DEFVAL(false));
  1190. ClassDB::bind_method(D_METHOD("add_icon_shortcut", "texture", "shortcut", "id", "global"), &PopupMenu::add_icon_shortcut, DEFVAL(-1), DEFVAL(false));
  1191. ClassDB::bind_method(D_METHOD("add_check_shortcut", "shortcut", "id", "global"), &PopupMenu::add_check_shortcut, DEFVAL(-1), DEFVAL(false));
  1192. ClassDB::bind_method(D_METHOD("add_icon_check_shortcut", "texture", "shortcut", "id", "global"), &PopupMenu::add_icon_check_shortcut, DEFVAL(-1), DEFVAL(false));
  1193. ClassDB::bind_method(D_METHOD("add_radio_check_shortcut", "shortcut", "id", "global"), &PopupMenu::add_radio_check_shortcut, DEFVAL(-1), DEFVAL(false));
  1194. ClassDB::bind_method(D_METHOD("add_icon_radio_check_shortcut", "texture", "shortcut", "id", "global"), &PopupMenu::add_icon_radio_check_shortcut, DEFVAL(-1), DEFVAL(false));
  1195. ClassDB::bind_method(D_METHOD("add_submenu_item", "label", "submenu", "id"), &PopupMenu::add_submenu_item, DEFVAL(-1));
  1196. ClassDB::bind_method(D_METHOD("set_item_text", "idx", "text"), &PopupMenu::set_item_text);
  1197. ClassDB::bind_method(D_METHOD("set_item_icon", "idx", "icon"), &PopupMenu::set_item_icon);
  1198. ClassDB::bind_method(D_METHOD("set_item_checked", "idx", "checked"), &PopupMenu::set_item_checked);
  1199. ClassDB::bind_method(D_METHOD("set_item_id", "idx", "id"), &PopupMenu::set_item_id);
  1200. ClassDB::bind_method(D_METHOD("set_item_accelerator", "idx", "accel"), &PopupMenu::set_item_accelerator);
  1201. ClassDB::bind_method(D_METHOD("set_item_metadata", "idx", "metadata"), &PopupMenu::set_item_metadata);
  1202. ClassDB::bind_method(D_METHOD("set_item_disabled", "idx", "disabled"), &PopupMenu::set_item_disabled);
  1203. ClassDB::bind_method(D_METHOD("set_item_submenu", "idx", "submenu"), &PopupMenu::set_item_submenu);
  1204. ClassDB::bind_method(D_METHOD("set_item_as_separator", "idx", "enable"), &PopupMenu::set_item_as_separator);
  1205. ClassDB::bind_method(D_METHOD("set_item_as_checkable", "idx", "enable"), &PopupMenu::set_item_as_checkable);
  1206. ClassDB::bind_method(D_METHOD("set_item_as_radio_checkable", "idx", "enable"), &PopupMenu::set_item_as_radio_checkable);
  1207. ClassDB::bind_method(D_METHOD("set_item_tooltip", "idx", "tooltip"), &PopupMenu::set_item_tooltip);
  1208. ClassDB::bind_method(D_METHOD("set_item_shortcut", "idx", "shortcut", "global"), &PopupMenu::set_item_shortcut, DEFVAL(false));
  1209. ClassDB::bind_method(D_METHOD("set_item_multistate", "idx", "state"), &PopupMenu::set_item_multistate);
  1210. ClassDB::bind_method(D_METHOD("set_item_shortcut_disabled", "idx", "disabled"), &PopupMenu::set_item_shortcut_disabled);
  1211. ClassDB::bind_method(D_METHOD("toggle_item_checked", "idx"), &PopupMenu::toggle_item_checked);
  1212. ClassDB::bind_method(D_METHOD("toggle_item_multistate", "idx"), &PopupMenu::toggle_item_multistate);
  1213. ClassDB::bind_method(D_METHOD("get_item_text", "idx"), &PopupMenu::get_item_text);
  1214. ClassDB::bind_method(D_METHOD("get_item_icon", "idx"), &PopupMenu::get_item_icon);
  1215. ClassDB::bind_method(D_METHOD("is_item_checked", "idx"), &PopupMenu::is_item_checked);
  1216. ClassDB::bind_method(D_METHOD("get_item_id", "idx"), &PopupMenu::get_item_id);
  1217. ClassDB::bind_method(D_METHOD("get_item_index", "id"), &PopupMenu::get_item_index);
  1218. ClassDB::bind_method(D_METHOD("get_item_accelerator", "idx"), &PopupMenu::get_item_accelerator);
  1219. ClassDB::bind_method(D_METHOD("get_item_metadata", "idx"), &PopupMenu::get_item_metadata);
  1220. ClassDB::bind_method(D_METHOD("is_item_disabled", "idx"), &PopupMenu::is_item_disabled);
  1221. ClassDB::bind_method(D_METHOD("get_item_submenu", "idx"), &PopupMenu::get_item_submenu);
  1222. ClassDB::bind_method(D_METHOD("is_item_separator", "idx"), &PopupMenu::is_item_separator);
  1223. ClassDB::bind_method(D_METHOD("is_item_checkable", "idx"), &PopupMenu::is_item_checkable);
  1224. ClassDB::bind_method(D_METHOD("is_item_radio_checkable", "idx"), &PopupMenu::is_item_radio_checkable);
  1225. ClassDB::bind_method(D_METHOD("is_item_shortcut_disabled", "idx"), &PopupMenu::is_item_shortcut_disabled);
  1226. ClassDB::bind_method(D_METHOD("get_item_tooltip", "idx"), &PopupMenu::get_item_tooltip);
  1227. ClassDB::bind_method(D_METHOD("get_item_shortcut", "idx"), &PopupMenu::get_item_shortcut);
  1228. ClassDB::bind_method(D_METHOD("set_current_index", "index"), &PopupMenu::set_current_index);
  1229. ClassDB::bind_method(D_METHOD("get_current_index"), &PopupMenu::get_current_index);
  1230. ClassDB::bind_method(D_METHOD("get_item_count"), &PopupMenu::get_item_count);
  1231. ClassDB::bind_method(D_METHOD("remove_item", "idx"), &PopupMenu::remove_item);
  1232. ClassDB::bind_method(D_METHOD("add_separator", "label", "id"), &PopupMenu::add_separator, DEFVAL(String()), DEFVAL(-1));
  1233. ClassDB::bind_method(D_METHOD("clear"), &PopupMenu::clear);
  1234. ClassDB::bind_method(D_METHOD("_set_items"), &PopupMenu::_set_items);
  1235. ClassDB::bind_method(D_METHOD("_get_items"), &PopupMenu::_get_items);
  1236. ClassDB::bind_method(D_METHOD("set_hide_on_item_selection", "enable"), &PopupMenu::set_hide_on_item_selection);
  1237. ClassDB::bind_method(D_METHOD("is_hide_on_item_selection"), &PopupMenu::is_hide_on_item_selection);
  1238. ClassDB::bind_method(D_METHOD("set_hide_on_checkable_item_selection", "enable"), &PopupMenu::set_hide_on_checkable_item_selection);
  1239. ClassDB::bind_method(D_METHOD("is_hide_on_checkable_item_selection"), &PopupMenu::is_hide_on_checkable_item_selection);
  1240. ClassDB::bind_method(D_METHOD("set_hide_on_state_item_selection", "enable"), &PopupMenu::set_hide_on_multistate_item_selection);
  1241. ClassDB::bind_method(D_METHOD("is_hide_on_state_item_selection"), &PopupMenu::is_hide_on_multistate_item_selection);
  1242. ClassDB::bind_method(D_METHOD("set_submenu_popup_delay", "seconds"), &PopupMenu::set_submenu_popup_delay);
  1243. ClassDB::bind_method(D_METHOD("get_submenu_popup_delay"), &PopupMenu::get_submenu_popup_delay);
  1244. ClassDB::bind_method(D_METHOD("set_hide_on_window_lose_focus", "enable"), &PopupMenu::set_hide_on_window_lose_focus);
  1245. ClassDB::bind_method(D_METHOD("is_hide_on_window_lose_focus"), &PopupMenu::is_hide_on_window_lose_focus);
  1246. ClassDB::bind_method(D_METHOD("set_allow_search", "allow"), &PopupMenu::set_allow_search);
  1247. ClassDB::bind_method(D_METHOD("get_allow_search"), &PopupMenu::get_allow_search);
  1248. ClassDB::bind_method(D_METHOD("_submenu_timeout"), &PopupMenu::_submenu_timeout);
  1249. ClassDB::bind_method(D_METHOD("_draw_items"), &PopupMenu::_draw_items);
  1250. ClassDB::bind_method(D_METHOD("_draw_background"), &PopupMenu::_draw_background);
  1251. ClassDB::bind_method(D_METHOD("set_max_height", "max_height"), &PopupMenu::set_max_height);
  1252. ClassDB::bind_method(D_METHOD("get_max_height"), &PopupMenu::get_max_height);
  1253. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "items", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_items", "_get_items");
  1254. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_on_item_selection"), "set_hide_on_item_selection", "is_hide_on_item_selection");
  1255. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_on_checkable_item_selection"), "set_hide_on_checkable_item_selection", "is_hide_on_checkable_item_selection");
  1256. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_on_state_item_selection"), "set_hide_on_state_item_selection", "is_hide_on_state_item_selection");
  1257. ADD_PROPERTY(PropertyInfo(Variant::REAL, "submenu_popup_delay"), "set_submenu_popup_delay", "get_submenu_popup_delay");
  1258. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_search"), "set_allow_search", "get_allow_search");
  1259. ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_height"), "set_max_height", "get_max_height");
  1260. ADD_SIGNAL(MethodInfo("id_pressed", PropertyInfo(Variant::INT, "id")));
  1261. ADD_SIGNAL(MethodInfo("id_focused", PropertyInfo(Variant::INT, "id")));
  1262. ADD_SIGNAL(MethodInfo("index_pressed", PropertyInfo(Variant::INT, "index")));
  1263. }
  1264. void PopupMenu::popup(const Rect2 &p_bounds) {
  1265. grab_click_focus();
  1266. moved = Vector2();
  1267. invalidated_click = true;
  1268. Popup::popup(p_bounds);
  1269. }
  1270. PopupMenu::PopupMenu() {
  1271. activated_by_keyboard = false;
  1272. // Margin Container
  1273. margin_container = memnew(MarginContainer);
  1274. margin_container->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1275. margin_container->set_mouse_filter(MouseFilter::MOUSE_FILTER_IGNORE);
  1276. add_child(margin_container);
  1277. margin_container->connect("draw", this, "_draw_background");
  1278. // Scroll Container
  1279. scroll_container = memnew(ScrollContainer);
  1280. scroll_container->set_clip_contents(true);
  1281. scroll_container->set_mouse_filter(MouseFilter::MOUSE_FILTER_PASS);
  1282. margin_container->add_child(scroll_container);
  1283. // The control which will display the items
  1284. control = memnew(Control);
  1285. control->set_clip_contents(false);
  1286. control->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1287. control->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1288. control->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1289. control->set_mouse_filter(MouseFilter::MOUSE_FILTER_IGNORE);
  1290. scroll_container->add_child(control);
  1291. control->connect("draw", this, "_draw_items");
  1292. mouse_over = -1;
  1293. submenu_over = -1;
  1294. initial_button_mask = 0;
  1295. during_grabbed_click = false;
  1296. allow_search = false;
  1297. search_time_msec = 0;
  1298. search_string = "";
  1299. max_height = 0;
  1300. set_focus_mode(FOCUS_ALL);
  1301. set_as_toplevel(true);
  1302. set_hide_on_item_selection(true);
  1303. set_hide_on_checkable_item_selection(true);
  1304. set_hide_on_multistate_item_selection(false);
  1305. set_hide_on_window_lose_focus(true);
  1306. submenu_timer = memnew(Timer);
  1307. submenu_timer->set_wait_time(0.3);
  1308. submenu_timer->set_one_shot(true);
  1309. submenu_timer->connect("timeout", this, "_submenu_timeout");
  1310. add_child(submenu_timer);
  1311. }
  1312. PopupMenu::~PopupMenu() {
  1313. }