popup_menu.cpp 47 KB

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