popup_menu.cpp 48 KB

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