tabs.cpp 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. /*************************************************************************/
  2. /* tabs.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "tabs.h"
  31. #include "core/message_queue.h"
  32. #include "scene/gui/box_container.h"
  33. #include "scene/gui/label.h"
  34. #include "scene/gui/texture_rect.h"
  35. Size2 Tabs::get_minimum_size() const {
  36. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  37. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  38. Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
  39. Ref<Font> font = get_font("font");
  40. Size2 ms(0, MAX(MAX(tab_bg->get_minimum_size().height, tab_fg->get_minimum_size().height), tab_disabled->get_minimum_size().height) + font->get_height());
  41. for (int i = 0; i < tabs.size(); i++) {
  42. Ref<Texture> tex = tabs[i].icon;
  43. if (tex.is_valid()) {
  44. ms.height = MAX(ms.height, tex->get_size().height);
  45. if (tabs[i].text != "")
  46. ms.width += get_constant("hseparation");
  47. }
  48. ms.width += Math::ceil(font->get_string_size(tabs[i].xl_text).width);
  49. if (tabs[i].disabled)
  50. ms.width += tab_disabled->get_minimum_size().width;
  51. else if (current == i)
  52. ms.width += tab_fg->get_minimum_size().width;
  53. else
  54. ms.width += tab_bg->get_minimum_size().width;
  55. if (tabs[i].right_button.is_valid()) {
  56. Ref<Texture> rb = tabs[i].right_button;
  57. Size2 bms = rb->get_size();
  58. bms.width += get_constant("hseparation");
  59. ms.width += bms.width;
  60. ms.height = MAX(bms.height + tab_bg->get_minimum_size().height, ms.height);
  61. }
  62. if (cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && i == current)) {
  63. Ref<Texture> cb = get_icon("close");
  64. Size2 bms = cb->get_size();
  65. bms.width += get_constant("hseparation");
  66. ms.width += bms.width;
  67. ms.height = MAX(bms.height + tab_bg->get_minimum_size().height, ms.height);
  68. }
  69. }
  70. ms.width = 0; //TODO: should make this optional
  71. return ms;
  72. }
  73. void Tabs::_gui_input(const Ref<InputEvent> &p_event) {
  74. Ref<InputEventMouseMotion> mm = p_event;
  75. if (mm.is_valid()) {
  76. Point2 pos = mm->get_position();
  77. highlight_arrow = -1;
  78. if (buttons_visible) {
  79. Ref<Texture> incr = get_icon("increment");
  80. Ref<Texture> decr = get_icon("decrement");
  81. int limit = get_size().width - incr->get_width() - decr->get_width();
  82. if (pos.x > limit + decr->get_width()) {
  83. highlight_arrow = 1;
  84. } else if (pos.x > limit) {
  85. highlight_arrow = 0;
  86. }
  87. }
  88. _update_hover();
  89. update();
  90. return;
  91. }
  92. Ref<InputEventMouseButton> mb = p_event;
  93. if (mb.is_valid()) {
  94. if (mb->is_pressed() && mb->get_button_index() == BUTTON_WHEEL_UP && !mb->get_command()) {
  95. if (scrolling_enabled && buttons_visible) {
  96. if (offset > 0) {
  97. offset--;
  98. update();
  99. }
  100. }
  101. }
  102. if (mb->is_pressed() && mb->get_button_index() == BUTTON_WHEEL_DOWN && !mb->get_command()) {
  103. if (scrolling_enabled && buttons_visible) {
  104. if (missing_right) {
  105. offset++;
  106. update();
  107. }
  108. }
  109. }
  110. if (rb_pressing && !mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  111. if (rb_hover != -1) {
  112. //pressed
  113. emit_signal("right_button_pressed", rb_hover);
  114. }
  115. rb_pressing = false;
  116. update();
  117. }
  118. if (cb_pressing && !mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  119. if (cb_hover != -1) {
  120. //pressed
  121. emit_signal("tab_close", cb_hover);
  122. }
  123. cb_pressing = false;
  124. update();
  125. }
  126. if (mb->is_pressed() && (mb->get_button_index() == BUTTON_LEFT || (select_with_rmb && mb->get_button_index() == BUTTON_RIGHT))) {
  127. // clicks
  128. Point2 pos(mb->get_position().x, mb->get_position().y);
  129. if (buttons_visible) {
  130. Ref<Texture> incr = get_icon("increment");
  131. Ref<Texture> decr = get_icon("decrement");
  132. int limit = get_size().width - incr->get_width() - decr->get_width();
  133. if (pos.x > limit + decr->get_width()) {
  134. if (missing_right) {
  135. offset++;
  136. update();
  137. }
  138. return;
  139. } else if (pos.x > limit) {
  140. if (offset > 0) {
  141. offset--;
  142. update();
  143. }
  144. return;
  145. }
  146. }
  147. int found = -1;
  148. for (int i = 0; i < tabs.size(); i++) {
  149. if (i < offset)
  150. continue;
  151. if (tabs[i].rb_rect.has_point(pos)) {
  152. rb_pressing = true;
  153. update();
  154. return;
  155. }
  156. if (tabs[i].cb_rect.has_point(pos)) {
  157. cb_pressing = true;
  158. update();
  159. return;
  160. }
  161. if (pos.x >= tabs[i].ofs_cache && pos.x < tabs[i].ofs_cache + tabs[i].size_cache) {
  162. if (!tabs[i].disabled) {
  163. found = i;
  164. }
  165. break;
  166. }
  167. }
  168. if (found != -1) {
  169. set_current_tab(found);
  170. emit_signal("tab_clicked", found);
  171. }
  172. }
  173. }
  174. }
  175. void Tabs::_notification(int p_what) {
  176. switch (p_what) {
  177. case NOTIFICATION_TRANSLATION_CHANGED: {
  178. for (int i = 0; i < tabs.size(); ++i) {
  179. tabs.write[i].xl_text = tr(tabs[i].text);
  180. }
  181. minimum_size_changed();
  182. update();
  183. } break;
  184. case NOTIFICATION_RESIZED: {
  185. _update_cache();
  186. _ensure_no_over_offset();
  187. ensure_tab_visible(current);
  188. } break;
  189. case NOTIFICATION_DRAW: {
  190. _update_cache();
  191. RID ci = get_canvas_item();
  192. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  193. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  194. Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
  195. Ref<Font> font = get_font("font");
  196. Color color_fg = get_color("font_color_fg");
  197. Color color_bg = get_color("font_color_bg");
  198. Color color_disabled = get_color("font_color_disabled");
  199. Ref<Texture> close = get_icon("close");
  200. int h = get_size().height;
  201. int w = 0;
  202. int mw = 0;
  203. for (int i = 0; i < tabs.size(); i++) {
  204. tabs.write[i].ofs_cache = mw;
  205. mw += get_tab_width(i);
  206. }
  207. if (tab_align == ALIGN_CENTER) {
  208. w = (get_size().width - mw) / 2;
  209. } else if (tab_align == ALIGN_RIGHT) {
  210. w = get_size().width - mw;
  211. }
  212. if (w < 0) {
  213. w = 0;
  214. }
  215. Ref<Texture> incr = get_icon("increment");
  216. Ref<Texture> decr = get_icon("decrement");
  217. Ref<Texture> incr_hl = get_icon("increment_highlight");
  218. Ref<Texture> decr_hl = get_icon("decrement_highlight");
  219. int limit = get_size().width - incr->get_size().width - decr->get_size().width;
  220. missing_right = false;
  221. for (int i = 0; i < tabs.size(); i++) {
  222. if (i < offset)
  223. continue;
  224. tabs.write[i].ofs_cache = w;
  225. int lsize = tabs[i].size_cache;
  226. Ref<StyleBox> sb;
  227. Color col;
  228. if (tabs[i].disabled) {
  229. sb = tab_disabled;
  230. col = color_disabled;
  231. } else if (i == current) {
  232. sb = tab_fg;
  233. col = color_fg;
  234. } else {
  235. sb = tab_bg;
  236. col = color_bg;
  237. }
  238. if (w + lsize > limit) {
  239. max_drawn_tab = i - 1;
  240. missing_right = true;
  241. break;
  242. } else {
  243. max_drawn_tab = i;
  244. }
  245. Rect2 sb_rect = Rect2(w, 0, tabs[i].size_cache, h);
  246. sb->draw(ci, sb_rect);
  247. w += sb->get_margin(MARGIN_LEFT);
  248. Size2i sb_ms = sb->get_minimum_size();
  249. Ref<Texture> icon = tabs[i].icon;
  250. if (icon.is_valid()) {
  251. icon->draw(ci, Point2i(w, sb->get_margin(MARGIN_TOP) + ((sb_rect.size.y - sb_ms.y) - icon->get_height()) / 2));
  252. if (tabs[i].text != "")
  253. w += icon->get_width() + get_constant("hseparation");
  254. }
  255. font->draw(ci, Point2i(w, sb->get_margin(MARGIN_TOP) + ((sb_rect.size.y - sb_ms.y) - font->get_height()) / 2 + font->get_ascent()), tabs[i].xl_text, col, tabs[i].size_text);
  256. w += tabs[i].size_text;
  257. if (tabs[i].right_button.is_valid()) {
  258. Ref<StyleBox> style = get_stylebox("button");
  259. Ref<Texture> rb = tabs[i].right_button;
  260. w += get_constant("hseparation");
  261. Rect2 rb_rect;
  262. rb_rect.size = style->get_minimum_size() + rb->get_size();
  263. rb_rect.position.x = w;
  264. rb_rect.position.y = sb->get_margin(MARGIN_TOP) + ((sb_rect.size.y - sb_ms.y) - (rb_rect.size.y)) / 2;
  265. if (rb_hover == i) {
  266. if (rb_pressing)
  267. get_stylebox("button_pressed")->draw(ci, rb_rect);
  268. else
  269. style->draw(ci, rb_rect);
  270. }
  271. rb->draw(ci, Point2i(w + style->get_margin(MARGIN_LEFT), rb_rect.position.y + style->get_margin(MARGIN_TOP)));
  272. w += rb->get_width();
  273. tabs.write[i].rb_rect = rb_rect;
  274. }
  275. if (cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && i == current)) {
  276. Ref<StyleBox> style = get_stylebox("button");
  277. Ref<Texture> cb = close;
  278. w += get_constant("hseparation");
  279. Rect2 cb_rect;
  280. cb_rect.size = style->get_minimum_size() + cb->get_size();
  281. cb_rect.position.x = w;
  282. cb_rect.position.y = sb->get_margin(MARGIN_TOP) + ((sb_rect.size.y - sb_ms.y) - (cb_rect.size.y)) / 2;
  283. if (!tabs[i].disabled && cb_hover == i) {
  284. if (cb_pressing)
  285. get_stylebox("button_pressed")->draw(ci, cb_rect);
  286. else
  287. style->draw(ci, cb_rect);
  288. }
  289. cb->draw(ci, Point2i(w + style->get_margin(MARGIN_LEFT), cb_rect.position.y + style->get_margin(MARGIN_TOP)));
  290. w += cb->get_width();
  291. tabs.write[i].cb_rect = cb_rect;
  292. }
  293. w += sb->get_margin(MARGIN_RIGHT);
  294. }
  295. if (offset > 0 || missing_right) {
  296. int vofs = (get_size().height - incr->get_size().height) / 2;
  297. if (offset > 0)
  298. draw_texture(highlight_arrow == 0 ? decr_hl : decr, Point2(limit, vofs));
  299. else
  300. draw_texture(decr, Point2(limit, vofs), Color(1, 1, 1, 0.5));
  301. if (missing_right)
  302. draw_texture(highlight_arrow == 1 ? incr_hl : incr, Point2(limit + decr->get_size().width, vofs));
  303. else
  304. draw_texture(incr, Point2(limit + decr->get_size().width, vofs), Color(1, 1, 1, 0.5));
  305. buttons_visible = true;
  306. } else {
  307. buttons_visible = false;
  308. }
  309. } break;
  310. }
  311. }
  312. int Tabs::get_tab_count() const {
  313. return tabs.size();
  314. }
  315. void Tabs::set_current_tab(int p_current) {
  316. if (current == p_current) return;
  317. ERR_FAIL_INDEX(p_current, get_tab_count());
  318. previous = current;
  319. current = p_current;
  320. _change_notify("current_tab");
  321. _update_cache();
  322. update();
  323. emit_signal("tab_changed", p_current);
  324. }
  325. int Tabs::get_current_tab() const {
  326. return current;
  327. }
  328. int Tabs::get_previous_tab() const {
  329. return previous;
  330. }
  331. int Tabs::get_hovered_tab() const {
  332. return hover;
  333. }
  334. int Tabs::get_tab_offset() const {
  335. return offset;
  336. }
  337. bool Tabs::get_offset_buttons_visible() const {
  338. return buttons_visible;
  339. }
  340. void Tabs::set_tab_title(int p_tab, const String &p_title) {
  341. ERR_FAIL_INDEX(p_tab, tabs.size());
  342. tabs.write[p_tab].text = p_title;
  343. tabs.write[p_tab].xl_text = tr(p_title);
  344. update();
  345. minimum_size_changed();
  346. }
  347. String Tabs::get_tab_title(int p_tab) const {
  348. ERR_FAIL_INDEX_V(p_tab, tabs.size(), "");
  349. return tabs[p_tab].text;
  350. }
  351. void Tabs::set_tab_icon(int p_tab, const Ref<Texture> &p_icon) {
  352. ERR_FAIL_INDEX(p_tab, tabs.size());
  353. tabs.write[p_tab].icon = p_icon;
  354. update();
  355. minimum_size_changed();
  356. }
  357. Ref<Texture> Tabs::get_tab_icon(int p_tab) const {
  358. ERR_FAIL_INDEX_V(p_tab, tabs.size(), Ref<Texture>());
  359. return tabs[p_tab].icon;
  360. }
  361. void Tabs::set_tab_disabled(int p_tab, bool p_disabled) {
  362. ERR_FAIL_INDEX(p_tab, tabs.size());
  363. tabs.write[p_tab].disabled = p_disabled;
  364. update();
  365. }
  366. bool Tabs::get_tab_disabled(int p_tab) const {
  367. ERR_FAIL_INDEX_V(p_tab, tabs.size(), false);
  368. return tabs[p_tab].disabled;
  369. }
  370. void Tabs::set_tab_right_button(int p_tab, const Ref<Texture> &p_right_button) {
  371. ERR_FAIL_INDEX(p_tab, tabs.size());
  372. tabs.write[p_tab].right_button = p_right_button;
  373. _update_cache();
  374. update();
  375. minimum_size_changed();
  376. }
  377. Ref<Texture> Tabs::get_tab_right_button(int p_tab) const {
  378. ERR_FAIL_INDEX_V(p_tab, tabs.size(), Ref<Texture>());
  379. return tabs[p_tab].right_button;
  380. }
  381. void Tabs::_update_hover() {
  382. if (!is_inside_tree()) {
  383. return;
  384. }
  385. const Point2 &pos = get_local_mouse_position();
  386. // test hovering to display right or close button
  387. int hover_now = -1;
  388. int hover_buttons = -1;
  389. for (int i = 0; i < tabs.size(); i++) {
  390. if (i < offset)
  391. continue;
  392. Rect2 rect = get_tab_rect(i);
  393. if (rect.has_point(pos)) {
  394. hover_now = i;
  395. }
  396. if (tabs[i].rb_rect.has_point(pos)) {
  397. rb_hover = i;
  398. cb_hover = -1;
  399. hover_buttons = i;
  400. break;
  401. } else if (!tabs[i].disabled && tabs[i].cb_rect.has_point(pos)) {
  402. cb_hover = i;
  403. rb_hover = -1;
  404. hover_buttons = i;
  405. break;
  406. }
  407. }
  408. if (hover != hover_now) {
  409. hover = hover_now;
  410. emit_signal("tab_hover", hover);
  411. }
  412. if (hover_buttons == -1) { // no hover
  413. rb_hover = hover_buttons;
  414. cb_hover = hover_buttons;
  415. }
  416. }
  417. void Tabs::_update_cache() {
  418. Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
  419. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  420. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  421. Ref<Font> font = get_font("font");
  422. Ref<Texture> incr = get_icon("increment");
  423. Ref<Texture> decr = get_icon("decrement");
  424. int limit = get_size().width - incr->get_width() - decr->get_width();
  425. int w = 0;
  426. int mw = 0;
  427. int size_fixed = 0;
  428. int count_resize = 0;
  429. for (int i = 0; i < tabs.size(); i++) {
  430. tabs.write[i].ofs_cache = mw;
  431. tabs.write[i].size_cache = get_tab_width(i);
  432. tabs.write[i].size_text = Math::ceil(font->get_string_size(tabs[i].xl_text).width);
  433. mw += tabs[i].size_cache;
  434. if (tabs[i].size_cache <= min_width || i == current) {
  435. size_fixed += tabs[i].size_cache;
  436. } else {
  437. count_resize++;
  438. }
  439. }
  440. int m_width = min_width;
  441. if (count_resize > 0) {
  442. m_width = MAX((limit - size_fixed) / count_resize, min_width);
  443. }
  444. for (int i = 0; i < tabs.size(); i++) {
  445. if (i < offset)
  446. continue;
  447. Ref<StyleBox> sb;
  448. if (tabs[i].disabled) {
  449. sb = tab_disabled;
  450. } else if (i == current) {
  451. sb = tab_fg;
  452. } else {
  453. sb = tab_bg;
  454. }
  455. int lsize = tabs[i].size_cache;
  456. int slen = tabs[i].size_text;
  457. if (min_width > 0 && mw > limit && i != current) {
  458. if (lsize > m_width) {
  459. slen = m_width - (sb->get_margin(MARGIN_LEFT) + sb->get_margin(MARGIN_RIGHT));
  460. if (tabs[i].icon.is_valid()) {
  461. slen -= tabs[i].icon->get_width();
  462. slen -= get_constant("hseparation");
  463. }
  464. if (cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && i == current)) {
  465. Ref<Texture> cb = get_icon("close");
  466. slen -= cb->get_width();
  467. slen -= get_constant("hseparation");
  468. }
  469. slen = MAX(slen, 1);
  470. lsize = m_width;
  471. }
  472. }
  473. tabs.write[i].ofs_cache = w;
  474. tabs.write[i].size_cache = lsize;
  475. tabs.write[i].size_text = slen;
  476. w += lsize;
  477. }
  478. }
  479. void Tabs::_on_mouse_exited() {
  480. rb_hover = -1;
  481. cb_hover = -1;
  482. hover = -1;
  483. highlight_arrow = -1;
  484. update();
  485. }
  486. void Tabs::add_tab(const String &p_str, const Ref<Texture> &p_icon) {
  487. Tab t;
  488. t.text = p_str;
  489. t.xl_text = tr(p_str);
  490. t.icon = p_icon;
  491. t.disabled = false;
  492. t.ofs_cache = 0;
  493. t.size_cache = 0;
  494. tabs.push_back(t);
  495. _update_cache();
  496. call_deferred("_update_hover");
  497. update();
  498. minimum_size_changed();
  499. }
  500. void Tabs::clear_tabs() {
  501. tabs.clear();
  502. current = 0;
  503. previous = 0;
  504. call_deferred("_update_hover");
  505. update();
  506. }
  507. void Tabs::remove_tab(int p_idx) {
  508. ERR_FAIL_INDEX(p_idx, tabs.size());
  509. tabs.remove(p_idx);
  510. if (current >= p_idx)
  511. current--;
  512. _update_cache();
  513. call_deferred("_update_hover");
  514. update();
  515. minimum_size_changed();
  516. if (current < 0) {
  517. current = 0;
  518. previous = 0;
  519. }
  520. if (current >= tabs.size())
  521. current = tabs.size() - 1;
  522. _ensure_no_over_offset();
  523. }
  524. Variant Tabs::get_drag_data(const Point2 &p_point) {
  525. if (!drag_to_rearrange_enabled)
  526. return Variant();
  527. int tab_over = get_tab_idx_at_point(p_point);
  528. if (tab_over < 0)
  529. return Variant();
  530. HBoxContainer *drag_preview = memnew(HBoxContainer);
  531. if (!tabs[tab_over].icon.is_null()) {
  532. TextureRect *tf = memnew(TextureRect);
  533. tf->set_texture(tabs[tab_over].icon);
  534. drag_preview->add_child(tf);
  535. }
  536. Label *label = memnew(Label(tabs[tab_over].xl_text));
  537. drag_preview->add_child(label);
  538. if (!tabs[tab_over].right_button.is_null()) {
  539. TextureRect *tf = memnew(TextureRect);
  540. tf->set_texture(tabs[tab_over].right_button);
  541. drag_preview->add_child(tf);
  542. }
  543. set_drag_preview(drag_preview);
  544. Dictionary drag_data;
  545. drag_data["type"] = "tab_element";
  546. drag_data["tab_element"] = tab_over;
  547. drag_data["from_path"] = get_path();
  548. return drag_data;
  549. }
  550. bool Tabs::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  551. if (!drag_to_rearrange_enabled)
  552. return false;
  553. Dictionary d = p_data;
  554. if (!d.has("type"))
  555. return false;
  556. if (String(d["type"]) == "tab_element") {
  557. NodePath from_path = d["from_path"];
  558. NodePath to_path = get_path();
  559. if (from_path == to_path) {
  560. return true;
  561. } else if (get_tabs_rearrange_group() != -1) {
  562. // drag and drop between other Tabs
  563. Node *from_node = get_node(from_path);
  564. Tabs *from_tabs = Object::cast_to<Tabs>(from_node);
  565. if (from_tabs && from_tabs->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
  566. return true;
  567. }
  568. }
  569. }
  570. return false;
  571. }
  572. void Tabs::drop_data(const Point2 &p_point, const Variant &p_data) {
  573. if (!drag_to_rearrange_enabled)
  574. return;
  575. int hover_now = get_tab_idx_at_point(p_point);
  576. Dictionary d = p_data;
  577. if (!d.has("type"))
  578. return;
  579. if (String(d["type"]) == "tab_element") {
  580. int tab_from_id = d["tab_element"];
  581. NodePath from_path = d["from_path"];
  582. NodePath to_path = get_path();
  583. if (from_path == to_path) {
  584. if (hover_now < 0)
  585. hover_now = get_tab_count() - 1;
  586. move_tab(tab_from_id, hover_now);
  587. emit_signal("reposition_active_tab_request", hover_now);
  588. set_current_tab(hover_now);
  589. } else if (get_tabs_rearrange_group() != -1) {
  590. // drag and drop between Tabs
  591. Node *from_node = get_node(from_path);
  592. Tabs *from_tabs = Object::cast_to<Tabs>(from_node);
  593. if (from_tabs && from_tabs->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
  594. if (tab_from_id >= from_tabs->get_tab_count())
  595. return;
  596. Tab moving_tab = from_tabs->tabs[tab_from_id];
  597. if (hover_now < 0)
  598. hover_now = get_tab_count();
  599. tabs.insert(hover_now, moving_tab);
  600. from_tabs->remove_tab(tab_from_id);
  601. set_current_tab(hover_now);
  602. emit_signal("tab_changed", hover_now);
  603. _update_cache();
  604. }
  605. }
  606. }
  607. update();
  608. }
  609. int Tabs::get_tab_idx_at_point(const Point2 &p_point) const {
  610. int hover_now = -1;
  611. for (int i = 0; i < tabs.size(); i++) {
  612. if (i < offset)
  613. continue;
  614. Rect2 rect = get_tab_rect(i);
  615. if (rect.has_point(p_point)) {
  616. hover_now = i;
  617. }
  618. }
  619. return hover_now;
  620. }
  621. void Tabs::set_tab_align(TabAlign p_align) {
  622. ERR_FAIL_INDEX(p_align, ALIGN_MAX);
  623. tab_align = p_align;
  624. update();
  625. }
  626. Tabs::TabAlign Tabs::get_tab_align() const {
  627. return tab_align;
  628. }
  629. void Tabs::move_tab(int from, int to) {
  630. if (from == to)
  631. return;
  632. ERR_FAIL_INDEX(from, tabs.size());
  633. ERR_FAIL_INDEX(to, tabs.size());
  634. Tab tab_from = tabs[from];
  635. tabs.remove(from);
  636. tabs.insert(to, tab_from);
  637. _update_cache();
  638. update();
  639. }
  640. int Tabs::get_tab_width(int p_idx) const {
  641. ERR_FAIL_INDEX_V(p_idx, tabs.size(), 0);
  642. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  643. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  644. Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
  645. Ref<Font> font = get_font("font");
  646. int x = 0;
  647. Ref<Texture> tex = tabs[p_idx].icon;
  648. if (tex.is_valid()) {
  649. x += tex->get_width();
  650. if (tabs[p_idx].text != "")
  651. x += get_constant("hseparation");
  652. }
  653. x += Math::ceil(font->get_string_size(tabs[p_idx].xl_text).width);
  654. if (tabs[p_idx].disabled)
  655. x += tab_disabled->get_minimum_size().width;
  656. else if (current == p_idx)
  657. x += tab_fg->get_minimum_size().width;
  658. else
  659. x += tab_bg->get_minimum_size().width;
  660. if (tabs[p_idx].right_button.is_valid()) {
  661. Ref<Texture> rb = tabs[p_idx].right_button;
  662. x += rb->get_width();
  663. x += get_constant("hseparation");
  664. }
  665. if (cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && p_idx == current)) {
  666. Ref<Texture> cb = get_icon("close");
  667. x += cb->get_width();
  668. x += get_constant("hseparation");
  669. }
  670. return x;
  671. }
  672. void Tabs::_ensure_no_over_offset() {
  673. if (!is_inside_tree())
  674. return;
  675. Ref<Texture> incr = get_icon("increment");
  676. Ref<Texture> decr = get_icon("decrement");
  677. int limit = get_size().width - incr->get_width() - decr->get_width();
  678. while (offset > 0) {
  679. int total_w = 0;
  680. for (int i = 0; i < tabs.size(); i++) {
  681. if (i < offset - 1)
  682. continue;
  683. total_w += tabs[i].size_cache;
  684. }
  685. if (total_w < limit) {
  686. offset--;
  687. update();
  688. } else {
  689. break;
  690. }
  691. }
  692. }
  693. void Tabs::ensure_tab_visible(int p_idx) {
  694. if (!is_inside_tree())
  695. return;
  696. if (tabs.size() == 0) return;
  697. ERR_FAIL_INDEX(p_idx, tabs.size());
  698. if (p_idx == offset) {
  699. return;
  700. }
  701. if (p_idx < offset) {
  702. offset = p_idx;
  703. update();
  704. return;
  705. }
  706. int prev_offset = offset;
  707. Ref<Texture> incr = get_icon("increment");
  708. Ref<Texture> decr = get_icon("decrement");
  709. int limit = get_size().width - incr->get_width() - decr->get_width();
  710. for (int i = offset; i <= p_idx; i++) {
  711. if (tabs[i].ofs_cache + tabs[i].size_cache > limit) {
  712. offset++;
  713. }
  714. }
  715. if (prev_offset != offset) {
  716. update();
  717. }
  718. }
  719. Rect2 Tabs::get_tab_rect(int p_tab) const {
  720. ERR_FAIL_INDEX_V(p_tab, tabs.size(), Rect2());
  721. return Rect2(tabs[p_tab].ofs_cache, 0, tabs[p_tab].size_cache, get_size().height);
  722. }
  723. void Tabs::set_tab_close_display_policy(CloseButtonDisplayPolicy p_policy) {
  724. ERR_FAIL_INDEX(p_policy, CLOSE_BUTTON_MAX);
  725. cb_displaypolicy = p_policy;
  726. update();
  727. }
  728. Tabs::CloseButtonDisplayPolicy Tabs::get_tab_close_display_policy() const {
  729. return cb_displaypolicy;
  730. }
  731. void Tabs::set_min_width(int p_width) {
  732. min_width = p_width;
  733. }
  734. void Tabs::set_scrolling_enabled(bool p_enabled) {
  735. scrolling_enabled = p_enabled;
  736. }
  737. bool Tabs::get_scrolling_enabled() const {
  738. return scrolling_enabled;
  739. }
  740. void Tabs::set_drag_to_rearrange_enabled(bool p_enabled) {
  741. drag_to_rearrange_enabled = p_enabled;
  742. }
  743. bool Tabs::get_drag_to_rearrange_enabled() const {
  744. return drag_to_rearrange_enabled;
  745. }
  746. void Tabs::set_tabs_rearrange_group(int p_group_id) {
  747. tabs_rearrange_group = p_group_id;
  748. }
  749. int Tabs::get_tabs_rearrange_group() const {
  750. return tabs_rearrange_group;
  751. }
  752. void Tabs::set_select_with_rmb(bool p_enabled) {
  753. select_with_rmb = p_enabled;
  754. }
  755. bool Tabs::get_select_with_rmb() const {
  756. return select_with_rmb;
  757. }
  758. void Tabs::_bind_methods() {
  759. ClassDB::bind_method(D_METHOD("_gui_input"), &Tabs::_gui_input);
  760. ClassDB::bind_method(D_METHOD("_update_hover"), &Tabs::_update_hover);
  761. ClassDB::bind_method(D_METHOD("_on_mouse_exited"), &Tabs::_on_mouse_exited);
  762. ClassDB::bind_method(D_METHOD("get_tab_count"), &Tabs::get_tab_count);
  763. ClassDB::bind_method(D_METHOD("set_current_tab", "tab_idx"), &Tabs::set_current_tab);
  764. ClassDB::bind_method(D_METHOD("get_current_tab"), &Tabs::get_current_tab);
  765. ClassDB::bind_method(D_METHOD("get_previous_tab"), &Tabs::get_previous_tab);
  766. ClassDB::bind_method(D_METHOD("set_tab_title", "tab_idx", "title"), &Tabs::set_tab_title);
  767. ClassDB::bind_method(D_METHOD("get_tab_title", "tab_idx"), &Tabs::get_tab_title);
  768. ClassDB::bind_method(D_METHOD("set_tab_icon", "tab_idx", "icon"), &Tabs::set_tab_icon);
  769. ClassDB::bind_method(D_METHOD("get_tab_icon", "tab_idx"), &Tabs::get_tab_icon);
  770. ClassDB::bind_method(D_METHOD("set_tab_disabled", "tab_idx", "disabled"), &Tabs::set_tab_disabled);
  771. ClassDB::bind_method(D_METHOD("get_tab_disabled", "tab_idx"), &Tabs::get_tab_disabled);
  772. ClassDB::bind_method(D_METHOD("remove_tab", "tab_idx"), &Tabs::remove_tab);
  773. ClassDB::bind_method(D_METHOD("add_tab", "title", "icon"), &Tabs::add_tab, DEFVAL(""), DEFVAL(Ref<Texture>()));
  774. ClassDB::bind_method(D_METHOD("set_tab_align", "align"), &Tabs::set_tab_align);
  775. ClassDB::bind_method(D_METHOD("get_tab_align"), &Tabs::get_tab_align);
  776. ClassDB::bind_method(D_METHOD("get_tab_offset"), &Tabs::get_tab_offset);
  777. ClassDB::bind_method(D_METHOD("get_offset_buttons_visible"), &Tabs::get_offset_buttons_visible);
  778. ClassDB::bind_method(D_METHOD("ensure_tab_visible", "idx"), &Tabs::ensure_tab_visible);
  779. ClassDB::bind_method(D_METHOD("get_tab_rect", "tab_idx"), &Tabs::get_tab_rect);
  780. ClassDB::bind_method(D_METHOD("move_tab", "from", "to"), &Tabs::move_tab);
  781. ClassDB::bind_method(D_METHOD("set_tab_close_display_policy", "policy"), &Tabs::set_tab_close_display_policy);
  782. ClassDB::bind_method(D_METHOD("get_tab_close_display_policy"), &Tabs::get_tab_close_display_policy);
  783. ClassDB::bind_method(D_METHOD("set_scrolling_enabled", "enabled"), &Tabs::set_scrolling_enabled);
  784. ClassDB::bind_method(D_METHOD("get_scrolling_enabled"), &Tabs::get_scrolling_enabled);
  785. ClassDB::bind_method(D_METHOD("set_drag_to_rearrange_enabled", "enabled"), &Tabs::set_drag_to_rearrange_enabled);
  786. ClassDB::bind_method(D_METHOD("get_drag_to_rearrange_enabled"), &Tabs::get_drag_to_rearrange_enabled);
  787. ClassDB::bind_method(D_METHOD("set_tabs_rearrange_group", "group_id"), &Tabs::set_tabs_rearrange_group);
  788. ClassDB::bind_method(D_METHOD("get_tabs_rearrange_group"), &Tabs::get_tabs_rearrange_group);
  789. ClassDB::bind_method(D_METHOD("set_select_with_rmb", "enabled"), &Tabs::set_select_with_rmb);
  790. ClassDB::bind_method(D_METHOD("get_select_with_rmb"), &Tabs::get_select_with_rmb);
  791. ADD_SIGNAL(MethodInfo("tab_changed", PropertyInfo(Variant::INT, "tab")));
  792. ADD_SIGNAL(MethodInfo("right_button_pressed", PropertyInfo(Variant::INT, "tab")));
  793. ADD_SIGNAL(MethodInfo("tab_close", PropertyInfo(Variant::INT, "tab")));
  794. ADD_SIGNAL(MethodInfo("tab_hover", PropertyInfo(Variant::INT, "tab")));
  795. ADD_SIGNAL(MethodInfo("reposition_active_tab_request", PropertyInfo(Variant::INT, "idx_to")));
  796. ADD_SIGNAL(MethodInfo("tab_clicked", PropertyInfo(Variant::INT, "tab")));
  797. ADD_PROPERTY(PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE, "-1,4096,1", PROPERTY_USAGE_EDITOR), "set_current_tab", "get_current_tab");
  798. ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_align", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_tab_align", "get_tab_align");
  799. ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_close_display_policy", PROPERTY_HINT_ENUM, "Show Never,Show Active Only,Show Always"), "set_tab_close_display_policy", "get_tab_close_display_policy");
  800. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scrolling_enabled"), "set_scrolling_enabled", "get_scrolling_enabled");
  801. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_to_rearrange_enabled"), "set_drag_to_rearrange_enabled", "get_drag_to_rearrange_enabled");
  802. BIND_ENUM_CONSTANT(ALIGN_LEFT);
  803. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  804. BIND_ENUM_CONSTANT(ALIGN_RIGHT);
  805. BIND_ENUM_CONSTANT(ALIGN_MAX);
  806. BIND_ENUM_CONSTANT(CLOSE_BUTTON_SHOW_NEVER);
  807. BIND_ENUM_CONSTANT(CLOSE_BUTTON_SHOW_ACTIVE_ONLY);
  808. BIND_ENUM_CONSTANT(CLOSE_BUTTON_SHOW_ALWAYS);
  809. BIND_ENUM_CONSTANT(CLOSE_BUTTON_MAX);
  810. }
  811. Tabs::Tabs() {
  812. current = 0;
  813. previous = 0;
  814. tab_align = ALIGN_CENTER;
  815. rb_hover = -1;
  816. rb_pressing = false;
  817. highlight_arrow = -1;
  818. cb_hover = -1;
  819. cb_pressing = false;
  820. cb_displaypolicy = CLOSE_BUTTON_SHOW_NEVER;
  821. offset = 0;
  822. max_drawn_tab = 0;
  823. select_with_rmb = false;
  824. min_width = 0;
  825. scrolling_enabled = true;
  826. buttons_visible = false;
  827. hover = -1;
  828. drag_to_rearrange_enabled = false;
  829. tabs_rearrange_group = -1;
  830. connect("mouse_exited", this, "_on_mouse_exited");
  831. }