2
0

popup_menu.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  1. /*************************************************************************/
  2. /* popup_menu.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/os/input.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/print_string.h"
  34. #include "core/translation.h"
  35. String PopupMenu::_get_accel_text(int p_item) const {
  36. ERR_FAIL_INDEX_V(p_item, items.size(), String());
  37. if (items[p_item].shortcut.is_valid())
  38. return items[p_item].shortcut->get_as_text();
  39. else if (items[p_item].accel)
  40. return keycode_get_string(items[p_item].accel);
  41. return String();
  42. }
  43. Size2 PopupMenu::get_minimum_size() const {
  44. int vseparation = get_constant("vseparation");
  45. int hseparation = get_constant("hseparation");
  46. Size2 minsize = get_stylebox("panel")->get_minimum_size();
  47. Ref<Font> font = get_font("font");
  48. float max_w = 0;
  49. int font_h = font->get_height();
  50. int check_w = MAX(get_icon("checked")->get_width(), get_icon("radio_checked")->get_width());
  51. int accel_max_w = 0;
  52. for (int i = 0; i < items.size(); i++) {
  53. Size2 size;
  54. if (!items[i].icon.is_null()) {
  55. Size2 icon_size = items[i].icon->get_size();
  56. size.height = MAX(icon_size.height, font_h);
  57. size.width += icon_size.width;
  58. size.width += hseparation;
  59. } else {
  60. size.height = font_h;
  61. }
  62. size.width += items[i].h_ofs;
  63. if (items[i].checkable_type) {
  64. size.width += check_w + hseparation;
  65. }
  66. String text = items[i].shortcut.is_valid() ? String(tr(items[i].shortcut->get_name())) : items[i].xl_text;
  67. size.width += font->get_string_size(text).width;
  68. if (i > 0)
  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. minsize.height += size.height;
  79. max_w = MAX(max_w, size.width);
  80. }
  81. minsize.width += max_w + accel_max_w;
  82. return minsize;
  83. }
  84. int PopupMenu::_get_mouse_over(const Point2 &p_over) const {
  85. if (p_over.x < 0 || p_over.x >= get_size().width)
  86. return -1;
  87. Ref<StyleBox> style = get_stylebox("panel");
  88. Point2 ofs = style->get_offset();
  89. if (ofs.y > p_over.y)
  90. return -1;
  91. Ref<Font> font = get_font("font");
  92. int vseparation = get_constant("vseparation");
  93. float font_h = font->get_height();
  94. for (int i = 0; i < items.size(); i++) {
  95. ofs.y += vseparation;
  96. float h;
  97. if (!items[i].icon.is_null()) {
  98. Size2 icon_size = items[i].icon->get_size();
  99. h = MAX(icon_size.height, font_h);
  100. } else {
  101. h = font_h;
  102. }
  103. ofs.y += h;
  104. if (p_over.y < ofs.y) {
  105. return i;
  106. }
  107. }
  108. return -1;
  109. }
  110. void PopupMenu::_activate_submenu(int over) {
  111. Node *n = get_node(items[over].submenu);
  112. ERR_EXPLAIN("item subnode does not exist: " + items[over].submenu);
  113. ERR_FAIL_COND(!n);
  114. Popup *pm = Object::cast_to<Popup>(n);
  115. ERR_EXPLAIN("item subnode is not a Popup: " + items[over].submenu);
  116. ERR_FAIL_COND(!pm);
  117. if (pm->is_visible_in_tree())
  118. return; //already visible!
  119. Point2 p = get_global_position();
  120. Rect2 pr(p, get_size());
  121. Ref<StyleBox> style = get_stylebox("panel");
  122. Point2 pos = p + Point2(get_size().width, items[over]._ofs_cache - style->get_offset().y);
  123. Size2 size = pm->get_size();
  124. // fix pos
  125. if (pos.x + size.width > get_viewport_rect().size.width)
  126. pos.x = p.x - size.width;
  127. pm->set_position(pos);
  128. pm->popup();
  129. PopupMenu *pum = Object::cast_to<PopupMenu>(pm);
  130. if (pum) {
  131. pr.position -= pum->get_global_position();
  132. pum->clear_autohide_areas();
  133. pum->add_autohide_area(Rect2(pr.position.x, pr.position.y, pr.size.x, items[over]._ofs_cache));
  134. if (over < items.size() - 1) {
  135. int from = items[over + 1]._ofs_cache;
  136. pum->add_autohide_area(Rect2(pr.position.x, pr.position.y + from, pr.size.x, pr.size.y - from));
  137. }
  138. }
  139. }
  140. void PopupMenu::_submenu_timeout() {
  141. if (mouse_over == submenu_over)
  142. _activate_submenu(mouse_over);
  143. submenu_over = -1;
  144. }
  145. void PopupMenu::_scroll(float p_factor, const Point2 &p_over) {
  146. const float global_y = get_global_position().y;
  147. int vseparation = get_constant("vseparation");
  148. Ref<Font> font = get_font("font");
  149. float dy = (vseparation + font->get_height()) * 3 * p_factor;
  150. if (dy > 0 && global_y < 0)
  151. dy = MIN(dy, -global_y - 1);
  152. else if (dy < 0 && global_y + get_size().y > get_viewport_rect().size.y)
  153. dy = -MIN(-dy, global_y + get_size().y - get_viewport_rect().size.y - 1);
  154. set_position(get_position() + Vector2(0, dy));
  155. Ref<InputEventMouseMotion> ie;
  156. ie.instance();
  157. ie->set_position(p_over - Vector2(0, dy));
  158. _gui_input(ie);
  159. }
  160. void PopupMenu::_gui_input(const Ref<InputEvent> &p_event) {
  161. if (p_event->is_action("ui_down") && p_event->is_pressed()) {
  162. int search_from = mouse_over + 1;
  163. if (search_from >= items.size())
  164. search_from = 0;
  165. for (int i = search_from; i < items.size(); i++) {
  166. if (i < 0 || i >= items.size())
  167. continue;
  168. if (!items[i].separator && !items[i].disabled) {
  169. mouse_over = i;
  170. emit_signal("id_focused", i);
  171. update();
  172. accept_event();
  173. break;
  174. }
  175. }
  176. } else if (p_event->is_action("ui_up") && p_event->is_pressed()) {
  177. int search_from = mouse_over - 1;
  178. if (search_from < 0)
  179. search_from = items.size() - 1;
  180. for (int i = search_from; i >= 0; i--) {
  181. if (i < 0 || i >= items.size())
  182. continue;
  183. if (!items[i].separator && !items[i].disabled) {
  184. mouse_over = i;
  185. emit_signal("id_focused", i);
  186. update();
  187. accept_event();
  188. break;
  189. }
  190. }
  191. } else if (p_event->is_action("ui_left") && p_event->is_pressed()) {
  192. Node *n = get_parent();
  193. if (n && Object::cast_to<PopupMenu>(n)) {
  194. hide();
  195. accept_event();
  196. }
  197. } else if (p_event->is_action("ui_right") && p_event->is_pressed()) {
  198. if (mouse_over >= 0 && mouse_over < items.size() && !items[mouse_over].separator && items[mouse_over].submenu != "" && submenu_over != mouse_over) {
  199. _activate_submenu(mouse_over);
  200. accept_event();
  201. }
  202. } else if (p_event->is_action("ui_accept") && p_event->is_pressed()) {
  203. if (mouse_over >= 0 && mouse_over < items.size() && !items[mouse_over].separator) {
  204. if (items[mouse_over].submenu != "" && submenu_over != mouse_over) {
  205. _activate_submenu(mouse_over);
  206. } else {
  207. activate_item(mouse_over);
  208. }
  209. accept_event();
  210. }
  211. }
  212. Ref<InputEventMouseButton> b = p_event;
  213. if (b.is_valid()) {
  214. if (b->is_pressed())
  215. return;
  216. int button_idx = b->get_button_index();
  217. switch (button_idx) {
  218. case BUTTON_WHEEL_DOWN: {
  219. if (get_global_position().y + get_size().y > get_viewport_rect().size.y) {
  220. _scroll(-b->get_factor(), b->get_position());
  221. }
  222. } break;
  223. case BUTTON_WHEEL_UP: {
  224. if (get_global_position().y < 0) {
  225. _scroll(b->get_factor(), b->get_position());
  226. }
  227. } break;
  228. default: {
  229. // Allow activating item by releasing the LMB or any that was down when the popup appeared
  230. if (button_idx == BUTTON_LEFT || (initial_button_mask & (1 << (button_idx - 1)))) {
  231. bool was_during_grabbed_click = during_grabbed_click;
  232. during_grabbed_click = false;
  233. int over = _get_mouse_over(b->get_position());
  234. if (invalidated_click) {
  235. invalidated_click = false;
  236. break;
  237. }
  238. if (over < 0) {
  239. if (!was_during_grabbed_click) {
  240. hide();
  241. }
  242. break; //non-activable
  243. }
  244. if (items[over].separator || items[over].disabled)
  245. break;
  246. if (items[over].submenu != "") {
  247. _activate_submenu(over);
  248. return;
  249. }
  250. activate_item(over);
  251. }
  252. }
  253. }
  254. //update();
  255. }
  256. Ref<InputEventMouseMotion> m = p_event;
  257. if (m.is_valid()) {
  258. if (invalidated_click) {
  259. moved += m->get_relative();
  260. if (moved.length() > 4)
  261. invalidated_click = false;
  262. }
  263. for (List<Rect2>::Element *E = autohide_areas.front(); E; E = E->next()) {
  264. if (!Rect2(Point2(), get_size()).has_point(m->get_position()) && E->get().has_point(m->get_position())) {
  265. call_deferred("hide");
  266. return;
  267. }
  268. }
  269. int over = _get_mouse_over(m->get_position());
  270. int id = (over < 0 || items[over].separator || items[over].disabled) ? -1 : (items[over].ID >= 0 ? items[over].ID : over);
  271. if (id < 0) {
  272. mouse_over = -1;
  273. update();
  274. return;
  275. }
  276. if (items[over].submenu != "" && submenu_over != over) {
  277. submenu_over = over;
  278. submenu_timer->start();
  279. }
  280. if (over != mouse_over) {
  281. mouse_over = over;
  282. update();
  283. }
  284. }
  285. Ref<InputEventPanGesture> pan_gesture = p_event;
  286. if (pan_gesture.is_valid()) {
  287. if (get_global_position().y + get_size().y > get_viewport_rect().size.y || get_global_position().y < 0) {
  288. _scroll(-pan_gesture->get_delta().y, pan_gesture->get_position());
  289. }
  290. }
  291. }
  292. bool PopupMenu::has_point(const Point2 &p_point) const {
  293. if (parent_rect.has_point(p_point))
  294. return true;
  295. for (const List<Rect2>::Element *E = autohide_areas.front(); E; E = E->next()) {
  296. if (E->get().has_point(p_point))
  297. return true;
  298. }
  299. return Control::has_point(p_point);
  300. }
  301. void PopupMenu::_notification(int p_what) {
  302. switch (p_what) {
  303. case NOTIFICATION_ENTER_TREE: {
  304. PopupMenu *pm = Object::cast_to<PopupMenu>(get_parent());
  305. if (pm) {
  306. // Inherit submenu's popup delay time from parent menu
  307. float pm_delay = pm->get_submenu_popup_delay();
  308. set_submenu_popup_delay(pm_delay);
  309. }
  310. } break;
  311. case NOTIFICATION_TRANSLATION_CHANGED: {
  312. for (int i = 0; i < items.size(); i++) {
  313. items.write[i].xl_text = tr(items[i].text);
  314. }
  315. minimum_size_changed();
  316. update();
  317. } break;
  318. case NOTIFICATION_DRAW: {
  319. RID ci = get_canvas_item();
  320. Size2 size = get_size();
  321. Ref<StyleBox> style = get_stylebox("panel");
  322. Ref<StyleBox> hover = get_stylebox("hover");
  323. Ref<Font> font = get_font("font");
  324. // In Item::checkable_type enum order (less the non-checkable member)
  325. Ref<Texture> check[] = { get_icon("checked"), get_icon("radio_checked") };
  326. Ref<Texture> uncheck[] = { get_icon("unchecked"), get_icon("radio_unchecked") };
  327. Ref<Texture> submenu = get_icon("submenu");
  328. Ref<StyleBox> separator = get_stylebox("separator");
  329. Ref<StyleBox> labeled_separator_left = get_stylebox("labeled_separator_left");
  330. Ref<StyleBox> labeled_separator_right = get_stylebox("labeled_separator_right");
  331. style->draw(ci, Rect2(Point2(), get_size()));
  332. Point2 ofs = style->get_offset();
  333. int vseparation = get_constant("vseparation");
  334. int hseparation = get_constant("hseparation");
  335. Color font_color = get_color("font_color");
  336. Color font_color_disabled = get_color("font_color_disabled");
  337. Color font_color_accel = get_color("font_color_accel");
  338. Color font_color_hover = get_color("font_color_hover");
  339. float font_h = font->get_height();
  340. for (int i = 0; i < items.size(); i++) {
  341. if (i > 0)
  342. ofs.y += vseparation;
  343. Point2 item_ofs = ofs;
  344. float h;
  345. Size2 icon_size;
  346. Color icon_color(1, 1, 1, items[i].disabled ? 0.5 : 1);
  347. if (!items[i].icon.is_null()) {
  348. icon_size = items[i].icon->get_size();
  349. h = MAX(icon_size.height, font_h);
  350. } else {
  351. h = font_h;
  352. }
  353. if (i == mouse_over) {
  354. hover->draw(ci, Rect2(item_ofs + Point2(-hseparation, -vseparation / 2), Size2(get_size().width - style->get_minimum_size().width + hseparation * 2, h + vseparation)));
  355. }
  356. String text = items[i].shortcut.is_valid() ? String(tr(items[i].shortcut->get_name())) : items[i].xl_text;
  357. item_ofs.x += items[i].h_ofs;
  358. if (items[i].separator) {
  359. int sep_h = separator->get_center_size().height + separator->get_minimum_size().height;
  360. if (text != String()) {
  361. int ss = font->get_string_size(text).width;
  362. int center = (get_size().width) / 2;
  363. int l = center - ss / 2;
  364. int r = center + ss / 2;
  365. if (l > item_ofs.x) {
  366. labeled_separator_left->draw(ci, Rect2(item_ofs + Point2(0, Math::floor((h - sep_h) / 2.0)), Size2(MAX(0, l - item_ofs.x), sep_h)));
  367. }
  368. if (r < get_size().width - style->get_margin(MARGIN_RIGHT)) {
  369. labeled_separator_right->draw(ci, Rect2(Point2(r, item_ofs.y + Math::floor((h - sep_h) / 2.0)), Size2(MAX(0, get_size().width - style->get_margin(MARGIN_RIGHT) - r), sep_h)));
  370. }
  371. } else {
  372. separator->draw(ci, Rect2(item_ofs + Point2(0, Math::floor((h - sep_h) / 2.0)), Size2(get_size().width - style->get_minimum_size().width, sep_h)));
  373. }
  374. }
  375. if (items[i].checkable_type) {
  376. Texture *icon = (items[i].checked ? check[items[i].checkable_type - 1] : uncheck[items[i].checkable_type - 1]).ptr();
  377. icon->draw(ci, item_ofs + Point2(0, Math::floor((h - icon->get_height()) / 2.0)), icon_color);
  378. item_ofs.x += icon->get_width() + hseparation;
  379. }
  380. if (!items[i].icon.is_null()) {
  381. items[i].icon->draw(ci, item_ofs + Point2(0, Math::floor((h - icon_size.height) / 2.0)), icon_color);
  382. item_ofs.x += items[i].icon->get_width();
  383. item_ofs.x += hseparation;
  384. }
  385. if (items[i].submenu != "") {
  386. submenu->draw(ci, Point2(size.width - style->get_margin(MARGIN_RIGHT) - submenu->get_width(), item_ofs.y + Math::floor(h - submenu->get_height()) / 2), icon_color);
  387. }
  388. item_ofs.y += font->get_ascent();
  389. if (items[i].separator) {
  390. if (text != String()) {
  391. int center = (get_size().width - font->get_string_size(text).width) / 2;
  392. font->draw(ci, Point2(center, item_ofs.y + Math::floor((h - font_h) / 2.0)), text, font_color_disabled);
  393. }
  394. } else {
  395. 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));
  396. }
  397. if (items[i].accel || (items[i].shortcut.is_valid() && items[i].shortcut->is_valid())) {
  398. //accelerator
  399. String text2 = _get_accel_text(i);
  400. item_ofs.x = size.width - style->get_margin(MARGIN_RIGHT) - font->get_string_size(text2).width;
  401. font->draw(ci, item_ofs + Point2(0, Math::floor((h - font_h) / 2.0)), text2, i == mouse_over ? font_color_hover : font_color_accel);
  402. }
  403. items.write[i]._ofs_cache = ofs.y;
  404. ofs.y += h;
  405. }
  406. } break;
  407. case MainLoop::NOTIFICATION_WM_FOCUS_OUT: {
  408. if (hide_on_window_lose_focus)
  409. hide();
  410. } break;
  411. case NOTIFICATION_MOUSE_ENTER: {
  412. grab_focus();
  413. } break;
  414. case NOTIFICATION_MOUSE_EXIT: {
  415. if (mouse_over >= 0 && (items[mouse_over].submenu == "" || submenu_over != -1)) {
  416. mouse_over = -1;
  417. update();
  418. }
  419. } break;
  420. case NOTIFICATION_POST_POPUP: {
  421. initial_button_mask = Input::get_singleton()->get_mouse_button_mask();
  422. during_grabbed_click = (bool)initial_button_mask;
  423. } break;
  424. case NOTIFICATION_POPUP_HIDE: {
  425. if (mouse_over >= 0) {
  426. mouse_over = -1;
  427. update();
  428. }
  429. for (int i = 0; i < items.size(); i++) {
  430. if (items[i].submenu == "")
  431. continue;
  432. Node *n = get_node(items[i].submenu);
  433. if (!n)
  434. continue;
  435. PopupMenu *pm = Object::cast_to<PopupMenu>(n);
  436. if (!pm || !pm->is_visible())
  437. continue;
  438. pm->hide();
  439. }
  440. } break;
  441. }
  442. }
  443. void PopupMenu::add_icon_item(const Ref<Texture> &p_icon, const String &p_label, int p_ID, uint32_t p_accel) {
  444. Item item;
  445. item.icon = p_icon;
  446. item.text = p_label;
  447. item.xl_text = tr(p_label);
  448. item.accel = p_accel;
  449. item.ID = p_ID;
  450. items.push_back(item);
  451. update();
  452. minimum_size_changed();
  453. }
  454. void PopupMenu::add_item(const String &p_label, int p_ID, uint32_t p_accel) {
  455. Item item;
  456. item.text = p_label;
  457. item.xl_text = tr(p_label);
  458. item.accel = p_accel;
  459. item.ID = p_ID;
  460. items.push_back(item);
  461. update();
  462. minimum_size_changed();
  463. }
  464. void PopupMenu::add_submenu_item(const String &p_label, const String &p_submenu, int p_ID) {
  465. Item item;
  466. item.text = p_label;
  467. item.xl_text = tr(p_label);
  468. item.ID = p_ID;
  469. item.submenu = p_submenu;
  470. items.push_back(item);
  471. update();
  472. minimum_size_changed();
  473. }
  474. void PopupMenu::add_icon_check_item(const Ref<Texture> &p_icon, const String &p_label, int p_ID, uint32_t p_accel) {
  475. Item item;
  476. item.icon = p_icon;
  477. item.text = p_label;
  478. item.xl_text = tr(p_label);
  479. item.accel = p_accel;
  480. item.ID = p_ID;
  481. item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX;
  482. items.push_back(item);
  483. update();
  484. minimum_size_changed();
  485. }
  486. void PopupMenu::add_check_item(const String &p_label, int p_ID, uint32_t p_accel) {
  487. Item item;
  488. item.text = p_label;
  489. item.xl_text = tr(p_label);
  490. item.accel = p_accel;
  491. item.ID = p_ID;
  492. item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX;
  493. items.push_back(item);
  494. update();
  495. minimum_size_changed();
  496. }
  497. void PopupMenu::add_radio_check_item(const String &p_label, int p_ID, uint32_t p_accel) {
  498. add_check_item(p_label, p_ID, p_accel);
  499. items.write[items.size() - 1].checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON;
  500. update();
  501. minimum_size_changed();
  502. }
  503. void PopupMenu::add_icon_radio_check_item(const Ref<Texture> &p_icon, const String &p_label, int p_ID, uint32_t p_accel) {
  504. add_icon_check_item(p_icon, p_label, p_ID, p_accel);
  505. items.write[items.size() - 1].checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON;
  506. update();
  507. minimum_size_changed();
  508. }
  509. void PopupMenu::add_icon_shortcut(const Ref<Texture> &p_icon, const Ref<ShortCut> &p_shortcut, int p_ID, bool p_global) {
  510. ERR_FAIL_COND(p_shortcut.is_null());
  511. _ref_shortcut(p_shortcut);
  512. Item item;
  513. item.ID = p_ID;
  514. item.icon = p_icon;
  515. item.shortcut = p_shortcut;
  516. item.shortcut_is_global = p_global;
  517. items.push_back(item);
  518. update();
  519. minimum_size_changed();
  520. }
  521. void PopupMenu::add_shortcut(const Ref<ShortCut> &p_shortcut, int p_ID, bool p_global) {
  522. ERR_FAIL_COND(p_shortcut.is_null());
  523. _ref_shortcut(p_shortcut);
  524. Item item;
  525. item.ID = p_ID;
  526. item.shortcut = p_shortcut;
  527. item.shortcut_is_global = p_global;
  528. items.push_back(item);
  529. update();
  530. minimum_size_changed();
  531. }
  532. void PopupMenu::add_icon_check_shortcut(const Ref<Texture> &p_icon, const Ref<ShortCut> &p_shortcut, int p_ID, bool p_global) {
  533. ERR_FAIL_COND(p_shortcut.is_null());
  534. _ref_shortcut(p_shortcut);
  535. Item item;
  536. item.ID = p_ID;
  537. item.shortcut = p_shortcut;
  538. item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX;
  539. item.icon = p_icon;
  540. item.shortcut_is_global = p_global;
  541. items.push_back(item);
  542. update();
  543. minimum_size_changed();
  544. }
  545. void PopupMenu::add_check_shortcut(const Ref<ShortCut> &p_shortcut, int p_ID, bool p_global) {
  546. ERR_FAIL_COND(p_shortcut.is_null());
  547. _ref_shortcut(p_shortcut);
  548. Item item;
  549. item.ID = p_ID;
  550. item.shortcut = p_shortcut;
  551. item.shortcut_is_global = p_global;
  552. item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX;
  553. items.push_back(item);
  554. update();
  555. minimum_size_changed();
  556. }
  557. void PopupMenu::add_radio_check_shortcut(const Ref<ShortCut> &p_shortcut, int p_ID, bool p_global) {
  558. add_check_shortcut(p_shortcut, p_ID, p_global);
  559. items.write[items.size() - 1].checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON;
  560. update();
  561. minimum_size_changed();
  562. }
  563. void PopupMenu::add_multistate_item(const String &p_label, int p_max_states, int p_default_state, int p_ID, uint32_t p_accel) {
  564. Item item;
  565. item.text = p_label;
  566. item.xl_text = tr(p_label);
  567. item.accel = p_accel;
  568. item.ID = p_ID;
  569. item.max_states = p_max_states;
  570. item.state = p_default_state;
  571. items.push_back(item);
  572. update();
  573. minimum_size_changed();
  574. }
  575. void PopupMenu::set_item_text(int p_idx, const String &p_text) {
  576. ERR_FAIL_INDEX(p_idx, items.size());
  577. items.write[p_idx].text = p_text;
  578. items.write[p_idx].xl_text = tr(p_text);
  579. update();
  580. minimum_size_changed();
  581. }
  582. void PopupMenu::set_item_icon(int p_idx, const Ref<Texture> &p_icon) {
  583. ERR_FAIL_INDEX(p_idx, items.size());
  584. items.write[p_idx].icon = p_icon;
  585. update();
  586. minimum_size_changed();
  587. }
  588. void PopupMenu::set_item_checked(int p_idx, bool p_checked) {
  589. ERR_FAIL_INDEX(p_idx, items.size());
  590. items.write[p_idx].checked = p_checked;
  591. update();
  592. minimum_size_changed();
  593. }
  594. void PopupMenu::set_item_id(int p_idx, int p_ID) {
  595. ERR_FAIL_INDEX(p_idx, items.size());
  596. items.write[p_idx].ID = p_ID;
  597. update();
  598. minimum_size_changed();
  599. }
  600. void PopupMenu::set_item_accelerator(int p_idx, uint32_t p_accel) {
  601. ERR_FAIL_INDEX(p_idx, items.size());
  602. items.write[p_idx].accel = p_accel;
  603. update();
  604. minimum_size_changed();
  605. }
  606. void PopupMenu::set_item_metadata(int p_idx, const Variant &p_meta) {
  607. ERR_FAIL_INDEX(p_idx, items.size());
  608. items.write[p_idx].metadata = p_meta;
  609. update();
  610. minimum_size_changed();
  611. }
  612. void PopupMenu::set_item_disabled(int p_idx, bool p_disabled) {
  613. ERR_FAIL_INDEX(p_idx, items.size());
  614. items.write[p_idx].disabled = p_disabled;
  615. update();
  616. minimum_size_changed();
  617. }
  618. void PopupMenu::set_item_submenu(int p_idx, const String &p_submenu) {
  619. ERR_FAIL_INDEX(p_idx, items.size());
  620. items.write[p_idx].submenu = p_submenu;
  621. update();
  622. minimum_size_changed();
  623. }
  624. void PopupMenu::toggle_item_checked(int p_idx) {
  625. ERR_FAIL_INDEX(p_idx, items.size());
  626. items.write[p_idx].checked = !items[p_idx].checked;
  627. update();
  628. minimum_size_changed();
  629. }
  630. String PopupMenu::get_item_text(int p_idx) const {
  631. ERR_FAIL_INDEX_V(p_idx, items.size(), "");
  632. return items[p_idx].text;
  633. }
  634. int PopupMenu::get_item_idx_from_text(const String &text) const {
  635. for (int idx = 0; idx < items.size(); idx++) {
  636. if (items[idx].text == text)
  637. return idx;
  638. }
  639. return -1;
  640. }
  641. Ref<Texture> PopupMenu::get_item_icon(int p_idx) const {
  642. ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<Texture>());
  643. return items[p_idx].icon;
  644. }
  645. uint32_t PopupMenu::get_item_accelerator(int p_idx) const {
  646. ERR_FAIL_INDEX_V(p_idx, items.size(), 0);
  647. return items[p_idx].accel;
  648. }
  649. Variant PopupMenu::get_item_metadata(int p_idx) const {
  650. ERR_FAIL_INDEX_V(p_idx, items.size(), Variant());
  651. return items[p_idx].metadata;
  652. }
  653. bool PopupMenu::is_item_disabled(int p_idx) const {
  654. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  655. return items[p_idx].disabled;
  656. }
  657. bool PopupMenu::is_item_checked(int p_idx) const {
  658. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  659. return items[p_idx].checked;
  660. }
  661. int PopupMenu::get_item_id(int p_idx) const {
  662. ERR_FAIL_INDEX_V(p_idx, items.size(), 0);
  663. return items[p_idx].ID;
  664. }
  665. int PopupMenu::get_item_index(int p_ID) const {
  666. for (int i = 0; i < items.size(); i++) {
  667. if (items[i].ID == p_ID)
  668. return i;
  669. }
  670. return -1;
  671. }
  672. String PopupMenu::get_item_submenu(int p_idx) const {
  673. ERR_FAIL_INDEX_V(p_idx, items.size(), "");
  674. return items[p_idx].submenu;
  675. }
  676. String PopupMenu::get_item_tooltip(int p_idx) const {
  677. ERR_FAIL_INDEX_V(p_idx, items.size(), "");
  678. return items[p_idx].tooltip;
  679. }
  680. Ref<ShortCut> PopupMenu::get_item_shortcut(int p_idx) const {
  681. ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<ShortCut>());
  682. return items[p_idx].shortcut;
  683. }
  684. int PopupMenu::get_item_state(int p_idx) const {
  685. ERR_FAIL_INDEX_V(p_idx, items.size(), -1);
  686. return items[p_idx].state;
  687. }
  688. void PopupMenu::set_item_as_separator(int p_idx, bool p_separator) {
  689. ERR_FAIL_INDEX(p_idx, items.size());
  690. items.write[p_idx].separator = p_separator;
  691. update();
  692. }
  693. bool PopupMenu::is_item_separator(int p_idx) const {
  694. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  695. return items[p_idx].separator;
  696. }
  697. void PopupMenu::set_item_as_checkable(int p_idx, bool p_checkable) {
  698. ERR_FAIL_INDEX(p_idx, items.size());
  699. items.write[p_idx].checkable_type = p_checkable ? Item::CHECKABLE_TYPE_CHECK_BOX : Item::CHECKABLE_TYPE_NONE;
  700. update();
  701. }
  702. void PopupMenu::set_item_as_radio_checkable(int p_idx, bool p_radio_checkable) {
  703. ERR_FAIL_INDEX(p_idx, items.size());
  704. items.write[p_idx].checkable_type = p_radio_checkable ? Item::CHECKABLE_TYPE_RADIO_BUTTON : Item::CHECKABLE_TYPE_NONE;
  705. update();
  706. }
  707. void PopupMenu::set_item_tooltip(int p_idx, const String &p_tooltip) {
  708. ERR_FAIL_INDEX(p_idx, items.size());
  709. items.write[p_idx].tooltip = p_tooltip;
  710. update();
  711. }
  712. void PopupMenu::set_item_shortcut(int p_idx, const Ref<ShortCut> &p_shortcut, bool p_global) {
  713. ERR_FAIL_INDEX(p_idx, items.size());
  714. if (items[p_idx].shortcut.is_valid()) {
  715. _unref_shortcut(items[p_idx].shortcut);
  716. }
  717. items.write[p_idx].shortcut = p_shortcut;
  718. items.write[p_idx].shortcut_is_global = p_global;
  719. if (items[p_idx].shortcut.is_valid()) {
  720. _ref_shortcut(items[p_idx].shortcut);
  721. }
  722. update();
  723. }
  724. void PopupMenu::set_item_h_offset(int p_idx, int p_offset) {
  725. ERR_FAIL_INDEX(p_idx, items.size());
  726. items.write[p_idx].h_ofs = p_offset;
  727. update();
  728. minimum_size_changed();
  729. }
  730. void PopupMenu::set_item_multistate(int p_idx, int p_state) {
  731. ERR_FAIL_INDEX(p_idx, items.size());
  732. items.write[p_idx].state = p_state;
  733. update();
  734. }
  735. void PopupMenu::set_item_shortcut_disabled(int p_idx, bool p_disabled) {
  736. ERR_FAIL_INDEX(p_idx, items.size());
  737. items.write[p_idx].shortcut_is_disabled = p_disabled;
  738. update();
  739. }
  740. void PopupMenu::toggle_item_multistate(int p_idx) {
  741. ERR_FAIL_INDEX(p_idx, items.size());
  742. if (0 >= items[p_idx].max_states) {
  743. return;
  744. }
  745. ++items.write[p_idx].state;
  746. if (items.write[p_idx].max_states <= items[p_idx].state)
  747. items.write[p_idx].state = 0;
  748. update();
  749. }
  750. bool PopupMenu::is_item_checkable(int p_idx) const {
  751. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  752. return items[p_idx].checkable_type;
  753. }
  754. bool PopupMenu::is_item_radio_checkable(int p_idx) const {
  755. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  756. return items[p_idx].checkable_type == Item::CHECKABLE_TYPE_RADIO_BUTTON;
  757. }
  758. bool PopupMenu::is_item_shortcut_disabled(int p_idx) const {
  759. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  760. return items[p_idx].shortcut_is_disabled;
  761. }
  762. int PopupMenu::get_item_count() const {
  763. return items.size();
  764. }
  765. bool PopupMenu::activate_item_by_event(const Ref<InputEvent> &p_event, bool p_for_global_only) {
  766. uint32_t code = 0;
  767. Ref<InputEventKey> k = p_event;
  768. if (k.is_valid()) {
  769. code = k->get_scancode();
  770. if (code == 0)
  771. code = k->get_unicode();
  772. if (k->get_control())
  773. code |= KEY_MASK_CTRL;
  774. if (k->get_alt())
  775. code |= KEY_MASK_ALT;
  776. if (k->get_metakey())
  777. code |= KEY_MASK_META;
  778. if (k->get_shift())
  779. code |= KEY_MASK_SHIFT;
  780. }
  781. for (int i = 0; i < items.size(); i++) {
  782. if (is_item_disabled(i) || items[i].shortcut_is_disabled)
  783. continue;
  784. if (items[i].shortcut.is_valid() && items[i].shortcut->is_shortcut(p_event) && (items[i].shortcut_is_global || !p_for_global_only)) {
  785. activate_item(i);
  786. return true;
  787. }
  788. if (code != 0 && items[i].accel == code) {
  789. activate_item(i);
  790. return true;
  791. }
  792. if (items[i].submenu != "") {
  793. Node *n = get_node(items[i].submenu);
  794. if (!n)
  795. continue;
  796. PopupMenu *pm = Object::cast_to<PopupMenu>(n);
  797. if (!pm)
  798. continue;
  799. if (pm->activate_item_by_event(p_event, p_for_global_only)) {
  800. return true;
  801. }
  802. }
  803. }
  804. return false;
  805. }
  806. void PopupMenu::activate_item(int p_item) {
  807. ERR_FAIL_INDEX(p_item, items.size());
  808. ERR_FAIL_COND(items[p_item].separator);
  809. int id = items[p_item].ID >= 0 ? items[p_item].ID : p_item;
  810. //hide all parent PopupMenus
  811. Node *next = get_parent();
  812. PopupMenu *pop = Object::cast_to<PopupMenu>(next);
  813. while (pop) {
  814. // We close all parents that are chained together,
  815. // with hide_on_item_selection enabled
  816. if (items[p_item].checkable_type) {
  817. if (!hide_on_checkable_item_selection || !pop->is_hide_on_checkable_item_selection())
  818. break;
  819. } else if (0 < items[p_item].max_states) {
  820. if (!hide_on_multistate_item_selection || !pop->is_hide_on_multistate_item_selection())
  821. break;
  822. } else if (!hide_on_item_selection || !pop->is_hide_on_item_selection())
  823. break;
  824. pop->hide();
  825. next = next->get_parent();
  826. pop = Object::cast_to<PopupMenu>(next);
  827. }
  828. // Hides popup by default; unless otherwise specified
  829. // by using set_hide_on_item_selection and set_hide_on_checkable_item_selection
  830. bool need_hide = true;
  831. if (items[p_item].checkable_type) {
  832. if (!hide_on_checkable_item_selection)
  833. need_hide = false;
  834. } else if (0 < items[p_item].max_states) {
  835. if (!hide_on_multistate_item_selection)
  836. need_hide = false;
  837. } else if (!hide_on_item_selection)
  838. need_hide = false;
  839. emit_signal("id_pressed", id);
  840. emit_signal("index_pressed", p_item);
  841. if (need_hide) {
  842. hide();
  843. }
  844. }
  845. void PopupMenu::remove_item(int p_idx) {
  846. ERR_FAIL_INDEX(p_idx, items.size());
  847. if (items[p_idx].shortcut.is_valid()) {
  848. _unref_shortcut(items[p_idx].shortcut);
  849. }
  850. items.remove(p_idx);
  851. update();
  852. minimum_size_changed();
  853. }
  854. void PopupMenu::add_separator(const String &p_text) {
  855. Item sep;
  856. sep.separator = true;
  857. sep.ID = -1;
  858. if (p_text != String()) {
  859. sep.text = p_text;
  860. sep.xl_text = tr(p_text);
  861. }
  862. items.push_back(sep);
  863. update();
  864. }
  865. void PopupMenu::clear() {
  866. for (int i = 0; i < items.size(); i++) {
  867. if (items[i].shortcut.is_valid()) {
  868. _unref_shortcut(items[i].shortcut);
  869. }
  870. }
  871. items.clear();
  872. mouse_over = -1;
  873. update();
  874. minimum_size_changed();
  875. }
  876. Array PopupMenu::_get_items() const {
  877. Array items;
  878. for (int i = 0; i < get_item_count(); i++) {
  879. items.push_back(get_item_text(i));
  880. items.push_back(get_item_icon(i));
  881. // For compatibility, use false/true for no/checkbox and integers for other values
  882. int ct = this->items[i].checkable_type;
  883. items.push_back(Variant(ct <= Item::CHECKABLE_TYPE_CHECK_BOX ? is_item_checkable(i) : ct));
  884. items.push_back(is_item_checked(i));
  885. items.push_back(is_item_disabled(i));
  886. items.push_back(get_item_id(i));
  887. items.push_back(get_item_accelerator(i));
  888. items.push_back(get_item_metadata(i));
  889. items.push_back(get_item_submenu(i));
  890. items.push_back(is_item_separator(i));
  891. }
  892. return items;
  893. }
  894. void PopupMenu::_ref_shortcut(Ref<ShortCut> p_sc) {
  895. if (!shortcut_refcount.has(p_sc)) {
  896. shortcut_refcount[p_sc] = 1;
  897. p_sc->connect("changed", this, "update");
  898. } else {
  899. shortcut_refcount[p_sc] += 1;
  900. }
  901. }
  902. void PopupMenu::_unref_shortcut(Ref<ShortCut> p_sc) {
  903. ERR_FAIL_COND(!shortcut_refcount.has(p_sc));
  904. shortcut_refcount[p_sc]--;
  905. if (shortcut_refcount[p_sc] == 0) {
  906. p_sc->disconnect("changed", this, "update");
  907. shortcut_refcount.erase(p_sc);
  908. }
  909. }
  910. void PopupMenu::_set_items(const Array &p_items) {
  911. ERR_FAIL_COND(p_items.size() % 10);
  912. clear();
  913. for (int i = 0; i < p_items.size(); i += 10) {
  914. String text = p_items[i + 0];
  915. Ref<Texture> icon = p_items[i + 1];
  916. // For compatibility, use false/true for no/checkbox and integers for other values
  917. bool checkable = p_items[i + 2];
  918. bool radio_checkable = (int)p_items[i + 2] == Item::CHECKABLE_TYPE_RADIO_BUTTON;
  919. bool checked = p_items[i + 3];
  920. bool disabled = p_items[i + 4];
  921. int id = p_items[i + 5];
  922. int accel = p_items[i + 6];
  923. Variant meta = p_items[i + 7];
  924. String subm = p_items[i + 8];
  925. bool sep = p_items[i + 9];
  926. int idx = get_item_count();
  927. add_item(text, id);
  928. set_item_icon(idx, icon);
  929. if (checkable) {
  930. if (radio_checkable) {
  931. set_item_as_radio_checkable(idx, true);
  932. } else {
  933. set_item_as_checkable(idx, true);
  934. }
  935. }
  936. set_item_checked(idx, checked);
  937. set_item_disabled(idx, disabled);
  938. set_item_id(idx, id);
  939. set_item_metadata(idx, meta);
  940. set_item_as_separator(idx, sep);
  941. set_item_accelerator(idx, accel);
  942. set_item_submenu(idx, subm);
  943. }
  944. }
  945. // Hide on item selection determines whether or not the popup will close after item selection
  946. void PopupMenu::set_hide_on_item_selection(bool p_enabled) {
  947. hide_on_item_selection = p_enabled;
  948. }
  949. bool PopupMenu::is_hide_on_item_selection() const {
  950. return hide_on_item_selection;
  951. }
  952. void PopupMenu::set_hide_on_checkable_item_selection(bool p_enabled) {
  953. hide_on_checkable_item_selection = p_enabled;
  954. }
  955. bool PopupMenu::is_hide_on_checkable_item_selection() const {
  956. return hide_on_checkable_item_selection;
  957. }
  958. void PopupMenu::set_hide_on_multistate_item_selection(bool p_enabled) {
  959. hide_on_multistate_item_selection = p_enabled;
  960. }
  961. bool PopupMenu::is_hide_on_multistate_item_selection() const {
  962. return hide_on_multistate_item_selection;
  963. }
  964. void PopupMenu::set_submenu_popup_delay(float p_time) {
  965. if (p_time <= 0)
  966. p_time = 0.01;
  967. submenu_timer->set_wait_time(p_time);
  968. }
  969. float PopupMenu::get_submenu_popup_delay() const {
  970. return submenu_timer->get_wait_time();
  971. }
  972. void PopupMenu::set_hide_on_window_lose_focus(bool p_enabled) {
  973. hide_on_window_lose_focus = p_enabled;
  974. }
  975. bool PopupMenu::is_hide_on_window_lose_focus() const {
  976. return hide_on_window_lose_focus;
  977. }
  978. String PopupMenu::get_tooltip(const Point2 &p_pos) const {
  979. int over = _get_mouse_over(p_pos);
  980. if (over < 0 || over >= items.size())
  981. return "";
  982. return items[over].tooltip;
  983. }
  984. void PopupMenu::set_parent_rect(const Rect2 &p_rect) {
  985. parent_rect = p_rect;
  986. }
  987. void PopupMenu::get_translatable_strings(List<String> *p_strings) const {
  988. for (int i = 0; i < items.size(); i++) {
  989. if (items[i].xl_text != "")
  990. p_strings->push_back(items[i].xl_text);
  991. }
  992. }
  993. void PopupMenu::add_autohide_area(const Rect2 &p_area) {
  994. autohide_areas.push_back(p_area);
  995. }
  996. void PopupMenu::clear_autohide_areas() {
  997. autohide_areas.clear();
  998. }
  999. void PopupMenu::_bind_methods() {
  1000. ClassDB::bind_method(D_METHOD("_gui_input"), &PopupMenu::_gui_input);
  1001. ClassDB::bind_method(D_METHOD("add_icon_item", "texture", "label", "id", "accel"), &PopupMenu::add_icon_item, DEFVAL(-1), DEFVAL(0));
  1002. ClassDB::bind_method(D_METHOD("add_item", "label", "id", "accel"), &PopupMenu::add_item, DEFVAL(-1), DEFVAL(0));
  1003. ClassDB::bind_method(D_METHOD("add_icon_check_item", "texture", "label", "id", "accel"), &PopupMenu::add_icon_check_item, DEFVAL(-1), DEFVAL(0));
  1004. ClassDB::bind_method(D_METHOD("add_check_item", "label", "id", "accel"), &PopupMenu::add_check_item, DEFVAL(-1), DEFVAL(0));
  1005. ClassDB::bind_method(D_METHOD("add_radio_check_item", "label", "id", "accel"), &PopupMenu::add_radio_check_item, DEFVAL(-1), DEFVAL(0));
  1006. ClassDB::bind_method(D_METHOD("add_submenu_item", "label", "submenu", "id"), &PopupMenu::add_submenu_item, DEFVAL(-1));
  1007. ClassDB::bind_method(D_METHOD("add_icon_shortcut", "texture", "shortcut", "id", "global"), &PopupMenu::add_icon_shortcut, DEFVAL(-1), DEFVAL(false));
  1008. ClassDB::bind_method(D_METHOD("add_shortcut", "shortcut", "id", "global"), &PopupMenu::add_shortcut, DEFVAL(-1), DEFVAL(false));
  1009. ClassDB::bind_method(D_METHOD("add_icon_check_shortcut", "texture", "shortcut", "id", "global"), &PopupMenu::add_icon_check_shortcut, DEFVAL(-1), DEFVAL(false));
  1010. ClassDB::bind_method(D_METHOD("add_check_shortcut", "shortcut", "id", "global"), &PopupMenu::add_check_shortcut, DEFVAL(-1), DEFVAL(false));
  1011. ClassDB::bind_method(D_METHOD("add_radio_check_shortcut", "shortcut", "id", "global"), &PopupMenu::add_radio_check_shortcut, DEFVAL(-1), DEFVAL(false));
  1012. ClassDB::bind_method(D_METHOD("set_item_text", "idx", "text"), &PopupMenu::set_item_text);
  1013. ClassDB::bind_method(D_METHOD("set_item_icon", "idx", "icon"), &PopupMenu::set_item_icon);
  1014. ClassDB::bind_method(D_METHOD("set_item_checked", "idx", "checked"), &PopupMenu::set_item_checked);
  1015. ClassDB::bind_method(D_METHOD("set_item_id", "idx", "id"), &PopupMenu::set_item_id);
  1016. ClassDB::bind_method(D_METHOD("set_item_accelerator", "idx", "accel"), &PopupMenu::set_item_accelerator);
  1017. ClassDB::bind_method(D_METHOD("set_item_metadata", "idx", "metadata"), &PopupMenu::set_item_metadata);
  1018. ClassDB::bind_method(D_METHOD("set_item_disabled", "idx", "disabled"), &PopupMenu::set_item_disabled);
  1019. ClassDB::bind_method(D_METHOD("set_item_submenu", "idx", "submenu"), &PopupMenu::set_item_submenu);
  1020. ClassDB::bind_method(D_METHOD("set_item_as_separator", "idx", "enable"), &PopupMenu::set_item_as_separator);
  1021. ClassDB::bind_method(D_METHOD("set_item_as_checkable", "idx", "enable"), &PopupMenu::set_item_as_checkable);
  1022. ClassDB::bind_method(D_METHOD("set_item_as_radio_checkable", "idx", "enable"), &PopupMenu::set_item_as_radio_checkable);
  1023. ClassDB::bind_method(D_METHOD("set_item_tooltip", "idx", "tooltip"), &PopupMenu::set_item_tooltip);
  1024. ClassDB::bind_method(D_METHOD("set_item_shortcut", "idx", "shortcut", "global"), &PopupMenu::set_item_shortcut, DEFVAL(false));
  1025. ClassDB::bind_method(D_METHOD("set_item_multistate", "idx", "state"), &PopupMenu::set_item_multistate);
  1026. ClassDB::bind_method(D_METHOD("set_item_shortcut_disabled", "idx", "disabled"), &PopupMenu::set_item_shortcut_disabled);
  1027. ClassDB::bind_method(D_METHOD("toggle_item_checked", "idx"), &PopupMenu::toggle_item_checked);
  1028. ClassDB::bind_method(D_METHOD("toggle_item_multistate", "idx"), &PopupMenu::toggle_item_multistate);
  1029. ClassDB::bind_method(D_METHOD("get_item_text", "idx"), &PopupMenu::get_item_text);
  1030. ClassDB::bind_method(D_METHOD("get_item_icon", "idx"), &PopupMenu::get_item_icon);
  1031. ClassDB::bind_method(D_METHOD("is_item_checked", "idx"), &PopupMenu::is_item_checked);
  1032. ClassDB::bind_method(D_METHOD("get_item_id", "idx"), &PopupMenu::get_item_id);
  1033. ClassDB::bind_method(D_METHOD("get_item_index", "id"), &PopupMenu::get_item_index);
  1034. ClassDB::bind_method(D_METHOD("get_item_accelerator", "idx"), &PopupMenu::get_item_accelerator);
  1035. ClassDB::bind_method(D_METHOD("get_item_metadata", "idx"), &PopupMenu::get_item_metadata);
  1036. ClassDB::bind_method(D_METHOD("is_item_disabled", "idx"), &PopupMenu::is_item_disabled);
  1037. ClassDB::bind_method(D_METHOD("get_item_submenu", "idx"), &PopupMenu::get_item_submenu);
  1038. ClassDB::bind_method(D_METHOD("is_item_separator", "idx"), &PopupMenu::is_item_separator);
  1039. ClassDB::bind_method(D_METHOD("is_item_checkable", "idx"), &PopupMenu::is_item_checkable);
  1040. ClassDB::bind_method(D_METHOD("is_item_radio_checkable", "idx"), &PopupMenu::is_item_radio_checkable);
  1041. ClassDB::bind_method(D_METHOD("is_item_shortcut_disabled", "idx"), &PopupMenu::is_item_shortcut_disabled);
  1042. ClassDB::bind_method(D_METHOD("get_item_tooltip", "idx"), &PopupMenu::get_item_tooltip);
  1043. ClassDB::bind_method(D_METHOD("get_item_shortcut", "idx"), &PopupMenu::get_item_shortcut);
  1044. ClassDB::bind_method(D_METHOD("get_item_count"), &PopupMenu::get_item_count);
  1045. ClassDB::bind_method(D_METHOD("remove_item", "idx"), &PopupMenu::remove_item);
  1046. ClassDB::bind_method(D_METHOD("add_separator", "label"), &PopupMenu::add_separator, DEFVAL(String()));
  1047. ClassDB::bind_method(D_METHOD("clear"), &PopupMenu::clear);
  1048. ClassDB::bind_method(D_METHOD("_set_items"), &PopupMenu::_set_items);
  1049. ClassDB::bind_method(D_METHOD("_get_items"), &PopupMenu::_get_items);
  1050. ClassDB::bind_method(D_METHOD("set_hide_on_item_selection", "enable"), &PopupMenu::set_hide_on_item_selection);
  1051. ClassDB::bind_method(D_METHOD("is_hide_on_item_selection"), &PopupMenu::is_hide_on_item_selection);
  1052. ClassDB::bind_method(D_METHOD("set_hide_on_checkable_item_selection", "enable"), &PopupMenu::set_hide_on_checkable_item_selection);
  1053. ClassDB::bind_method(D_METHOD("is_hide_on_checkable_item_selection"), &PopupMenu::is_hide_on_checkable_item_selection);
  1054. ClassDB::bind_method(D_METHOD("set_hide_on_state_item_selection", "enable"), &PopupMenu::set_hide_on_multistate_item_selection);
  1055. ClassDB::bind_method(D_METHOD("is_hide_on_state_item_selection"), &PopupMenu::is_hide_on_multistate_item_selection);
  1056. ClassDB::bind_method(D_METHOD("set_submenu_popup_delay", "seconds"), &PopupMenu::set_submenu_popup_delay);
  1057. ClassDB::bind_method(D_METHOD("get_submenu_popup_delay"), &PopupMenu::get_submenu_popup_delay);
  1058. ClassDB::bind_method(D_METHOD("set_hide_on_window_lose_focus", "enable"), &PopupMenu::set_hide_on_window_lose_focus);
  1059. ClassDB::bind_method(D_METHOD("is_hide_on_window_lose_focus"), &PopupMenu::is_hide_on_window_lose_focus);
  1060. ClassDB::bind_method(D_METHOD("_submenu_timeout"), &PopupMenu::_submenu_timeout);
  1061. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "items", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_items", "_get_items");
  1062. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_on_item_selection"), "set_hide_on_item_selection", "is_hide_on_item_selection");
  1063. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_on_checkable_item_selection"), "set_hide_on_checkable_item_selection", "is_hide_on_checkable_item_selection");
  1064. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_on_state_item_selection"), "set_hide_on_state_item_selection", "is_hide_on_state_item_selection");
  1065. ADD_PROPERTY(PropertyInfo(Variant::REAL, "submenu_popup_delay"), "set_submenu_popup_delay", "get_submenu_popup_delay");
  1066. ADD_SIGNAL(MethodInfo("id_pressed", PropertyInfo(Variant::INT, "ID")));
  1067. ADD_SIGNAL(MethodInfo("id_focused", PropertyInfo(Variant::INT, "ID")));
  1068. ADD_SIGNAL(MethodInfo("index_pressed", PropertyInfo(Variant::INT, "index")));
  1069. }
  1070. void PopupMenu::popup(const Rect2 &p_bounds) {
  1071. grab_click_focus();
  1072. moved = Vector2();
  1073. invalidated_click = true;
  1074. Popup::popup(p_bounds);
  1075. }
  1076. PopupMenu::PopupMenu() {
  1077. mouse_over = -1;
  1078. submenu_over = -1;
  1079. initial_button_mask = 0;
  1080. during_grabbed_click = false;
  1081. set_focus_mode(FOCUS_ALL);
  1082. set_as_toplevel(true);
  1083. set_hide_on_item_selection(true);
  1084. set_hide_on_checkable_item_selection(true);
  1085. set_hide_on_multistate_item_selection(false);
  1086. set_hide_on_window_lose_focus(true);
  1087. submenu_timer = memnew(Timer);
  1088. submenu_timer->set_wait_time(0.3);
  1089. submenu_timer->set_one_shot(true);
  1090. submenu_timer->connect("timeout", this, "_submenu_timeout");
  1091. add_child(submenu_timer);
  1092. }
  1093. PopupMenu::~PopupMenu() {
  1094. }