popup_menu.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  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 text = _get_accel_text(i);
  400. item_ofs.x = size.width - style->get_margin(MARGIN_RIGHT) - font->get_string_size(text).width;
  401. font->draw(ci, item_ofs + Point2(0, Math::floor((h - font_h) / 2.0)), text, 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. }