popup_menu.cpp 34 KB

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