popup_menu.cpp 32 KB

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