popup_menu.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  1. /*************************************************************************/
  2. /* popup_menu.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.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. minsize.height += size.height;
  90. max_w = MAX(max_w, size.width);
  91. }
  92. minsize.width += max_w + accel_max_w;
  93. return minsize;
  94. }
  95. int PopupMenu::_get_mouse_over(const Point2 &p_over) const {
  96. if (p_over.x < 0 || p_over.x >= get_size().width)
  97. return -1;
  98. Ref<StyleBox> style = get_stylebox("panel");
  99. Point2 ofs = style->get_offset();
  100. if (ofs.y > p_over.y)
  101. return -1;
  102. Ref<Font> font = get_font("font");
  103. int vseparation = get_constant("vseparation");
  104. //int hseparation = get_constant("hseparation");
  105. float font_h = font->get_height();
  106. for (int i = 0; i < items.size(); i++) {
  107. if (i > 0)
  108. ofs.y += vseparation;
  109. float h;
  110. if (!items[i].icon.is_null()) {
  111. Size2 icon_size = items[i].icon->get_size();
  112. h = MAX(icon_size.height, font_h);
  113. } else {
  114. h = font_h;
  115. }
  116. ofs.y += h;
  117. if (p_over.y < ofs.y) {
  118. return i;
  119. }
  120. }
  121. return -1;
  122. }
  123. void PopupMenu::_activate_submenu(int over) {
  124. Node *n = get_node(items[over].submenu);
  125. ERR_EXPLAIN("item subnode does not exist: " + items[over].submenu);
  126. ERR_FAIL_COND(!n);
  127. Popup *pm = n->cast_to<Popup>();
  128. ERR_EXPLAIN("item subnode is not a Popup: " + items[over].submenu);
  129. ERR_FAIL_COND(!pm);
  130. if (pm->is_visible_in_tree())
  131. return; //already visible!
  132. Point2 p = get_global_position();
  133. Rect2 pr(p, get_size());
  134. Ref<StyleBox> style = get_stylebox("panel");
  135. Point2 pos = p + Point2(get_size().width, items[over]._ofs_cache - style->get_offset().y);
  136. Size2 size = pm->get_size();
  137. // fix pos
  138. if (pos.x + size.width > get_viewport_rect().size.width)
  139. pos.x = p.x - size.width;
  140. pm->set_position(pos);
  141. pm->popup();
  142. PopupMenu *pum = pm->cast_to<PopupMenu>();
  143. if (pum) {
  144. pr.pos -= pum->get_global_position();
  145. pum->clear_autohide_areas();
  146. pum->add_autohide_area(Rect2(pr.pos.x, pr.pos.y, pr.size.x, items[over]._ofs_cache));
  147. if (over < items.size() - 1) {
  148. int from = items[over + 1]._ofs_cache;
  149. pum->add_autohide_area(Rect2(pr.pos.x, pr.pos.y + from, pr.size.x, pr.size.y - from));
  150. }
  151. }
  152. }
  153. void PopupMenu::_submenu_timeout() {
  154. if (mouse_over == submenu_over) {
  155. _activate_submenu(mouse_over);
  156. submenu_over = -1;
  157. }
  158. }
  159. void PopupMenu::_gui_input(const InputEvent &p_event) {
  160. switch (p_event.type) {
  161. case InputEvent::KEY: {
  162. if (!p_event.key.pressed)
  163. break;
  164. switch (p_event.key.scancode) {
  165. case KEY_DOWN: {
  166. for (int i = mouse_over + 1; i < items.size(); i++) {
  167. if (i < 0 || i >= items.size())
  168. continue;
  169. if (!items[i].separator && !items[i].disabled) {
  170. mouse_over = i;
  171. update();
  172. break;
  173. }
  174. }
  175. } break;
  176. case KEY_UP: {
  177. for (int i = mouse_over - 1; i >= 0; i--) {
  178. if (i < 0 || i >= items.size())
  179. continue;
  180. if (!items[i].separator && !items[i].disabled) {
  181. mouse_over = i;
  182. update();
  183. break;
  184. }
  185. }
  186. } break;
  187. case KEY_RETURN:
  188. case KEY_ENTER: {
  189. if (mouse_over >= 0 && mouse_over < items.size() && !items[mouse_over].separator) {
  190. activate_item(mouse_over);
  191. }
  192. } break;
  193. }
  194. } break;
  195. case InputEvent::MOUSE_BUTTON: {
  196. const InputEventMouseButton &b = p_event.mouse_button;
  197. if (b.pressed)
  198. break;
  199. switch (b.button_index) {
  200. case BUTTON_WHEEL_DOWN: {
  201. if (get_global_position().y + get_size().y > get_viewport_rect().size.y) {
  202. int vseparation = get_constant("vseparation");
  203. Ref<Font> font = get_font("font");
  204. Point2 pos = get_position();
  205. int s = (vseparation + font->get_height()) * 3;
  206. pos.y -= s;
  207. set_position(pos);
  208. //update hover
  209. InputEvent ie;
  210. ie.type = InputEvent::MOUSE_MOTION;
  211. ie.mouse_motion.x = b.x;
  212. ie.mouse_motion.y = b.y + s;
  213. _gui_input(ie);
  214. }
  215. } break;
  216. case BUTTON_WHEEL_UP: {
  217. if (get_global_position().y < 0) {
  218. int vseparation = get_constant("vseparation");
  219. Ref<Font> font = get_font("font");
  220. Point2 pos = get_position();
  221. int s = (vseparation + font->get_height()) * 3;
  222. pos.y += s;
  223. set_position(pos);
  224. //update hover
  225. InputEvent ie;
  226. ie.type = InputEvent::MOUSE_MOTION;
  227. ie.mouse_motion.x = b.x;
  228. ie.mouse_motion.y = b.y - s;
  229. _gui_input(ie);
  230. }
  231. } break;
  232. case BUTTON_LEFT: {
  233. int over = _get_mouse_over(Point2(b.x, b.y));
  234. if (invalidated_click) {
  235. invalidated_click = false;
  236. break;
  237. }
  238. if (over < 0) {
  239. hide();
  240. break; //non-activable
  241. }
  242. if (items[over].separator || items[over].disabled)
  243. break;
  244. if (items[over].submenu != "") {
  245. _activate_submenu(over);
  246. return;
  247. }
  248. activate_item(over);
  249. } break;
  250. }
  251. //update();
  252. } break;
  253. case InputEvent::MOUSE_MOTION: {
  254. if (invalidated_click) {
  255. moved += Vector2(p_event.mouse_motion.relative_x, p_event.mouse_motion.relative_y);
  256. if (moved.length() > 4)
  257. invalidated_click = false;
  258. }
  259. const InputEventMouseMotion &m = p_event.mouse_motion;
  260. for (List<Rect2>::Element *E = autohide_areas.front(); E; E = E->next()) {
  261. if (!Rect2(Point2(), get_size()).has_point(Point2(m.x, m.y)) && E->get().has_point(Point2(m.x, m.y))) {
  262. call_deferred("hide");
  263. return;
  264. }
  265. }
  266. int over = _get_mouse_over(Point2(m.x, m.y));
  267. int id = (over < 0 || items[over].separator || items[over].disabled) ? -1 : (items[over].ID >= 0 ? items[over].ID : over);
  268. if (id < 0) {
  269. mouse_over = -1;
  270. update();
  271. break;
  272. }
  273. if (items[over].submenu != "" && submenu_over != over) {
  274. submenu_over = over;
  275. submenu_timer->start();
  276. }
  277. if (over != mouse_over) {
  278. mouse_over = over;
  279. update();
  280. }
  281. } break;
  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 = XL_MESSAGE(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 = XL_MESSAGE(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 = XL_MESSAGE(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 = XL_MESSAGE(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 = XL_MESSAGE(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 = XL_MESSAGE(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 = XL_MESSAGE(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. Ref<Texture> PopupMenu::get_item_icon(int p_idx) const {
  526. ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<Texture>());
  527. return items[p_idx].icon;
  528. }
  529. uint32_t PopupMenu::get_item_accelerator(int p_idx) const {
  530. ERR_FAIL_INDEX_V(p_idx, items.size(), 0);
  531. return items[p_idx].accel;
  532. }
  533. Variant PopupMenu::get_item_metadata(int p_idx) const {
  534. ERR_FAIL_INDEX_V(p_idx, items.size(), Variant());
  535. return items[p_idx].metadata;
  536. }
  537. bool PopupMenu::is_item_disabled(int p_idx) const {
  538. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  539. return items[p_idx].disabled;
  540. }
  541. bool PopupMenu::is_item_checked(int p_idx) const {
  542. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  543. return items[p_idx].checked;
  544. }
  545. int PopupMenu::get_item_ID(int p_idx) const {
  546. ERR_FAIL_INDEX_V(p_idx, items.size(), 0);
  547. return items[p_idx].ID;
  548. }
  549. int PopupMenu::get_item_index(int p_ID) const {
  550. for (int i = 0; i < items.size(); i++) {
  551. if (items[i].ID == p_ID)
  552. return i;
  553. }
  554. return -1;
  555. }
  556. String PopupMenu::get_item_submenu(int p_idx) const {
  557. ERR_FAIL_INDEX_V(p_idx, items.size(), "");
  558. return items[p_idx].submenu;
  559. }
  560. String PopupMenu::get_item_tooltip(int p_idx) const {
  561. ERR_FAIL_INDEX_V(p_idx, items.size(), "");
  562. return items[p_idx].tooltip;
  563. }
  564. Ref<ShortCut> PopupMenu::get_item_shortcut(int p_idx) const {
  565. ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<ShortCut>());
  566. return items[p_idx].shortcut;
  567. }
  568. void PopupMenu::set_item_as_separator(int p_idx, bool p_separator) {
  569. ERR_FAIL_INDEX(p_idx, items.size());
  570. items[p_idx].separator = p_separator;
  571. update();
  572. }
  573. bool PopupMenu::is_item_separator(int p_idx) const {
  574. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  575. return items[p_idx].separator;
  576. }
  577. void PopupMenu::set_item_as_checkable(int p_idx, bool p_checkable) {
  578. ERR_FAIL_INDEX(p_idx, items.size());
  579. items[p_idx].checkable = p_checkable;
  580. update();
  581. }
  582. void PopupMenu::set_item_tooltip(int p_idx, const String &p_tooltip) {
  583. ERR_FAIL_INDEX(p_idx, items.size());
  584. items[p_idx].tooltip = p_tooltip;
  585. update();
  586. }
  587. void PopupMenu::set_item_shortcut(int p_idx, const Ref<ShortCut> &p_shortcut, bool p_global) {
  588. ERR_FAIL_INDEX(p_idx, items.size());
  589. if (items[p_idx].shortcut.is_valid()) {
  590. _unref_shortcut(items[p_idx].shortcut);
  591. }
  592. items[p_idx].shortcut = p_shortcut;
  593. items[p_idx].shortcut_is_global = p_global;
  594. if (items[p_idx].shortcut.is_valid()) {
  595. _ref_shortcut(items[p_idx].shortcut);
  596. }
  597. update();
  598. }
  599. void PopupMenu::set_item_h_offset(int p_idx, int p_offset) {
  600. ERR_FAIL_INDEX(p_idx, items.size());
  601. items[p_idx].h_ofs = p_offset;
  602. update();
  603. }
  604. bool PopupMenu::is_item_checkable(int p_idx) const {
  605. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  606. return items[p_idx].checkable;
  607. }
  608. int PopupMenu::get_item_count() const {
  609. return items.size();
  610. }
  611. bool PopupMenu::activate_item_by_event(const InputEvent &p_event, bool p_for_global_only) {
  612. uint32_t code = 0;
  613. if (p_event.type == InputEvent::KEY) {
  614. code = p_event.key.scancode;
  615. if (code == 0)
  616. code = p_event.key.unicode;
  617. if (p_event.key.mod.control)
  618. code |= KEY_MASK_CTRL;
  619. if (p_event.key.mod.alt)
  620. code |= KEY_MASK_ALT;
  621. if (p_event.key.mod.meta)
  622. code |= KEY_MASK_META;
  623. if (p_event.key.mod.shift)
  624. code |= KEY_MASK_SHIFT;
  625. }
  626. int il = items.size();
  627. for (int i = 0; i < il; i++) {
  628. if (is_item_disabled(i))
  629. continue;
  630. if (items[i].shortcut.is_valid() && items[i].shortcut->is_shortcut(p_event) && (items[i].shortcut_is_global || !p_for_global_only)) {
  631. activate_item(i);
  632. return true;
  633. }
  634. if (code != 0 && items[i].accel == code) {
  635. activate_item(i);
  636. return true;
  637. }
  638. if (items[i].submenu != "") {
  639. Node *n = get_node(items[i].submenu);
  640. if (!n)
  641. continue;
  642. PopupMenu *pm = n->cast_to<PopupMenu>();
  643. if (!pm)
  644. continue;
  645. if (pm->activate_item_by_event(p_event, p_for_global_only)) {
  646. return true;
  647. }
  648. }
  649. }
  650. return false;
  651. }
  652. void PopupMenu::activate_item(int p_item) {
  653. ERR_FAIL_INDEX(p_item, items.size());
  654. ERR_FAIL_COND(items[p_item].separator);
  655. int id = items[p_item].ID >= 0 ? items[p_item].ID : p_item;
  656. emit_signal("id_pressed", id);
  657. emit_signal("index_pressed", p_item);
  658. //hide all parent PopupMenue's
  659. Node *next = get_parent();
  660. PopupMenu *pop = next->cast_to<PopupMenu>();
  661. while (pop) {
  662. // We close all parents that are chained together,
  663. // with hide_on_item_selection enabled
  664. 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())) {
  665. pop->hide();
  666. next = next->get_parent();
  667. pop = next->cast_to<PopupMenu>();
  668. } else {
  669. // Break out of loop when the next parent has
  670. // hide_on_item_selection disabled
  671. break;
  672. }
  673. }
  674. // Hides popup by default; unless otherwise specified
  675. // by using set_hide_on_item_selection and set_hide_on_checkable_item_selection
  676. if ((items[p_item].checkable && hide_on_checkable_item_selection) || (!items[p_item].checkable && hide_on_item_selection)) {
  677. hide();
  678. }
  679. }
  680. void PopupMenu::remove_item(int p_idx) {
  681. ERR_FAIL_INDEX(p_idx, items.size());
  682. if (items[p_idx].shortcut.is_valid()) {
  683. _unref_shortcut(items[p_idx].shortcut);
  684. }
  685. items.remove(p_idx);
  686. update();
  687. }
  688. void PopupMenu::add_separator() {
  689. Item sep;
  690. sep.separator = true;
  691. sep.ID = -1;
  692. items.push_back(sep);
  693. update();
  694. }
  695. void PopupMenu::clear() {
  696. for (int i = 0; i < items.size(); i++) {
  697. if (items[i].shortcut.is_valid()) {
  698. _unref_shortcut(items[i].shortcut);
  699. }
  700. }
  701. items.clear();
  702. mouse_over = -1;
  703. update();
  704. }
  705. Array PopupMenu::_get_items() const {
  706. Array items;
  707. for (int i = 0; i < get_item_count(); i++) {
  708. items.push_back(get_item_text(i));
  709. items.push_back(get_item_icon(i));
  710. items.push_back(is_item_checkable(i));
  711. items.push_back(is_item_checked(i));
  712. items.push_back(is_item_disabled(i));
  713. items.push_back(get_item_ID(i));
  714. items.push_back(get_item_accelerator(i));
  715. items.push_back(get_item_metadata(i));
  716. items.push_back(get_item_submenu(i));
  717. items.push_back(is_item_separator(i));
  718. }
  719. return items;
  720. }
  721. void PopupMenu::_ref_shortcut(Ref<ShortCut> p_sc) {
  722. if (!shortcut_refcount.has(p_sc)) {
  723. shortcut_refcount[p_sc] = 1;
  724. p_sc->connect("changed", this, "update");
  725. } else {
  726. shortcut_refcount[p_sc] += 1;
  727. }
  728. }
  729. void PopupMenu::_unref_shortcut(Ref<ShortCut> p_sc) {
  730. ERR_FAIL_COND(!shortcut_refcount.has(p_sc));
  731. shortcut_refcount[p_sc]--;
  732. if (shortcut_refcount[p_sc] == 0) {
  733. p_sc->disconnect("changed", this, "update");
  734. shortcut_refcount.erase(p_sc);
  735. }
  736. }
  737. void PopupMenu::_set_items(const Array &p_items) {
  738. ERR_FAIL_COND(p_items.size() % 10);
  739. clear();
  740. for (int i = 0; i < p_items.size(); i += 10) {
  741. String text = p_items[i + 0];
  742. Ref<Texture> icon = p_items[i + 1];
  743. bool checkable = p_items[i + 2];
  744. bool checked = p_items[i + 3];
  745. bool disabled = p_items[i + 4];
  746. int id = p_items[i + 5];
  747. int accel = p_items[i + 6];
  748. Variant meta = p_items[i + 7];
  749. String subm = p_items[i + 8];
  750. bool sep = p_items[i + 9];
  751. int idx = get_item_count();
  752. add_item(text, id);
  753. set_item_icon(idx, icon);
  754. set_item_as_checkable(idx, checkable);
  755. set_item_checked(idx, checked);
  756. set_item_disabled(idx, disabled);
  757. set_item_ID(idx, id);
  758. set_item_metadata(idx, meta);
  759. set_item_as_separator(idx, sep);
  760. set_item_accelerator(idx, accel);
  761. set_item_submenu(idx, subm);
  762. }
  763. }
  764. // Hide on item selection determines whether or not the popup will close after item selection
  765. void PopupMenu::set_hide_on_item_selection(bool p_enabled) {
  766. hide_on_item_selection = p_enabled;
  767. }
  768. bool PopupMenu::is_hide_on_item_selection() {
  769. return hide_on_item_selection;
  770. }
  771. void PopupMenu::set_hide_on_checkable_item_selection(bool p_enabled) {
  772. hide_on_checkable_item_selection = p_enabled;
  773. }
  774. bool PopupMenu::is_hide_on_checkable_item_selection() {
  775. return hide_on_checkable_item_selection;
  776. }
  777. String PopupMenu::get_tooltip(const Point2 &p_pos) const {
  778. int over = _get_mouse_over(p_pos);
  779. if (over < 0 || over >= items.size())
  780. return "";
  781. return items[over].tooltip;
  782. }
  783. void PopupMenu::set_parent_rect(const Rect2 &p_rect) {
  784. parent_rect = p_rect;
  785. }
  786. void PopupMenu::get_translatable_strings(List<String> *p_strings) const {
  787. for (int i = 0; i < items.size(); i++) {
  788. if (items[i].xl_text != "")
  789. p_strings->push_back(items[i].xl_text);
  790. }
  791. }
  792. void PopupMenu::add_autohide_area(const Rect2 &p_area) {
  793. autohide_areas.push_back(p_area);
  794. }
  795. void PopupMenu::clear_autohide_areas() {
  796. autohide_areas.clear();
  797. }
  798. void PopupMenu::_bind_methods() {
  799. ClassDB::bind_method(D_METHOD("_gui_input"), &PopupMenu::_gui_input);
  800. ClassDB::bind_method(D_METHOD("add_icon_item", "texture", "label", "id", "accel"), &PopupMenu::add_icon_item, DEFVAL(-1), DEFVAL(0));
  801. ClassDB::bind_method(D_METHOD("add_item", "label", "id", "accel"), &PopupMenu::add_item, DEFVAL(-1), DEFVAL(0));
  802. ClassDB::bind_method(D_METHOD("add_icon_check_item", "texture", "label", "id", "accel"), &PopupMenu::add_icon_check_item, DEFVAL(-1), DEFVAL(0));
  803. ClassDB::bind_method(D_METHOD("add_check_item", "label", "id", "accel"), &PopupMenu::add_check_item, DEFVAL(-1), DEFVAL(0));
  804. ClassDB::bind_method(D_METHOD("add_submenu_item", "label", "submenu", "id"), &PopupMenu::add_submenu_item, DEFVAL(-1));
  805. ClassDB::bind_method(D_METHOD("add_icon_shortcut", "texture", "shortcut:ShortCut", "id", "global"), &PopupMenu::add_icon_shortcut, DEFVAL(-1), DEFVAL(false));
  806. ClassDB::bind_method(D_METHOD("add_shortcut", "shortcut:ShortCut", "id", "global"), &PopupMenu::add_shortcut, DEFVAL(-1), DEFVAL(false));
  807. ClassDB::bind_method(D_METHOD("add_icon_check_shortcut", "texture", "shortcut:ShortCut", "id", "global"), &PopupMenu::add_icon_check_shortcut, DEFVAL(-1), DEFVAL(false));
  808. ClassDB::bind_method(D_METHOD("add_check_shortcut", "shortcut:ShortCut", "id", "global"), &PopupMenu::add_check_shortcut, DEFVAL(-1), DEFVAL(false));
  809. ClassDB::bind_method(D_METHOD("set_item_text", "idx", "text"), &PopupMenu::set_item_text);
  810. ClassDB::bind_method(D_METHOD("set_item_icon", "idx", "icon"), &PopupMenu::set_item_icon);
  811. ClassDB::bind_method(D_METHOD("set_item_checked", "idx", "checked"), &PopupMenu::set_item_checked);
  812. ClassDB::bind_method(D_METHOD("set_item_ID", "idx", "id"), &PopupMenu::set_item_ID);
  813. ClassDB::bind_method(D_METHOD("set_item_accelerator", "idx", "accel"), &PopupMenu::set_item_accelerator);
  814. ClassDB::bind_method(D_METHOD("set_item_metadata", "idx", "metadata"), &PopupMenu::set_item_metadata);
  815. ClassDB::bind_method(D_METHOD("set_item_disabled", "idx", "disabled"), &PopupMenu::set_item_disabled);
  816. ClassDB::bind_method(D_METHOD("set_item_submenu", "idx", "submenu"), &PopupMenu::set_item_submenu);
  817. ClassDB::bind_method(D_METHOD("set_item_as_separator", "idx", "enable"), &PopupMenu::set_item_as_separator);
  818. ClassDB::bind_method(D_METHOD("set_item_as_checkable", "idx", "enable"), &PopupMenu::set_item_as_checkable);
  819. ClassDB::bind_method(D_METHOD("set_item_tooltip", "idx", "tooltip"), &PopupMenu::set_item_tooltip);
  820. ClassDB::bind_method(D_METHOD("set_item_shortcut", "idx", "shortcut:ShortCut", "global"), &PopupMenu::set_item_shortcut, DEFVAL(false));
  821. ClassDB::bind_method(D_METHOD("toggle_item_checked", "idx"), &PopupMenu::toggle_item_checked);
  822. ClassDB::bind_method(D_METHOD("get_item_text", "idx"), &PopupMenu::get_item_text);
  823. ClassDB::bind_method(D_METHOD("get_item_icon", "idx"), &PopupMenu::get_item_icon);
  824. ClassDB::bind_method(D_METHOD("is_item_checked", "idx"), &PopupMenu::is_item_checked);
  825. ClassDB::bind_method(D_METHOD("get_item_ID", "idx"), &PopupMenu::get_item_ID);
  826. ClassDB::bind_method(D_METHOD("get_item_index", "id"), &PopupMenu::get_item_index);
  827. ClassDB::bind_method(D_METHOD("get_item_accelerator", "idx"), &PopupMenu::get_item_accelerator);
  828. ClassDB::bind_method(D_METHOD("get_item_metadata", "idx"), &PopupMenu::get_item_metadata);
  829. ClassDB::bind_method(D_METHOD("is_item_disabled", "idx"), &PopupMenu::is_item_disabled);
  830. ClassDB::bind_method(D_METHOD("get_item_submenu", "idx"), &PopupMenu::get_item_submenu);
  831. ClassDB::bind_method(D_METHOD("is_item_separator", "idx"), &PopupMenu::is_item_separator);
  832. ClassDB::bind_method(D_METHOD("is_item_checkable", "idx"), &PopupMenu::is_item_checkable);
  833. ClassDB::bind_method(D_METHOD("get_item_tooltip", "idx"), &PopupMenu::get_item_tooltip);
  834. ClassDB::bind_method(D_METHOD("get_item_shortcut:ShortCut", "idx"), &PopupMenu::get_item_shortcut);
  835. ClassDB::bind_method(D_METHOD("get_item_count"), &PopupMenu::get_item_count);
  836. ClassDB::bind_method(D_METHOD("remove_item", "idx"), &PopupMenu::remove_item);
  837. ClassDB::bind_method(D_METHOD("add_separator"), &PopupMenu::add_separator);
  838. ClassDB::bind_method(D_METHOD("clear"), &PopupMenu::clear);
  839. ClassDB::bind_method(D_METHOD("_set_items"), &PopupMenu::_set_items);
  840. ClassDB::bind_method(D_METHOD("_get_items"), &PopupMenu::_get_items);
  841. ClassDB::bind_method(D_METHOD("set_hide_on_item_selection", "enable"), &PopupMenu::set_hide_on_item_selection);
  842. ClassDB::bind_method(D_METHOD("is_hide_on_item_selection"), &PopupMenu::is_hide_on_item_selection);
  843. ClassDB::bind_method(D_METHOD("set_hide_on_checkable_item_selection", "enable"), &PopupMenu::set_hide_on_checkable_item_selection);
  844. ClassDB::bind_method(D_METHOD("is_hide_on_checkable_item_selection"), &PopupMenu::is_hide_on_checkable_item_selection);
  845. ClassDB::bind_method(D_METHOD("_submenu_timeout"), &PopupMenu::_submenu_timeout);
  846. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "items", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_items", "_get_items");
  847. ADD_PROPERTYNO(PropertyInfo(Variant::BOOL, "hide_on_item_selection"), "set_hide_on_item_selection", "is_hide_on_item_selection");
  848. ADD_PROPERTYNO(PropertyInfo(Variant::BOOL, "hide_on_checkable_item_selection"), "set_hide_on_checkable_item_selection", "is_hide_on_checkable_item_selection");
  849. ADD_SIGNAL(MethodInfo("id_pressed", PropertyInfo(Variant::INT, "ID")));
  850. ADD_SIGNAL(MethodInfo("index_pressed", PropertyInfo(Variant::INT, "index")));
  851. }
  852. void PopupMenu::set_invalidate_click_until_motion() {
  853. moved = Vector2();
  854. invalidated_click = true;
  855. }
  856. PopupMenu::PopupMenu() {
  857. mouse_over = -1;
  858. set_focus_mode(FOCUS_ALL);
  859. set_as_toplevel(true);
  860. set_hide_on_item_selection(true);
  861. set_hide_on_checkable_item_selection(true);
  862. submenu_timer = memnew(Timer);
  863. submenu_timer->set_wait_time(0.3);
  864. submenu_timer->set_one_shot(true);
  865. submenu_timer->connect("timeout", this, "_submenu_timeout");
  866. add_child(submenu_timer);
  867. }
  868. PopupMenu::~PopupMenu() {
  869. }