tabs.cpp 29 KB

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