foldable_container.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /**************************************************************************/
  2. /* foldable_container.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "foldable_container.h"
  31. #include "scene/resources/text_line.h"
  32. #include "scene/theme/theme_db.h"
  33. Size2 FoldableContainer::get_minimum_size() const {
  34. _update_title_min_size();
  35. if (folded) {
  36. return title_minimum_size;
  37. }
  38. Size2 ms;
  39. for (int i = 0; i < get_child_count(); i++) {
  40. Control *c = as_sortable_control(get_child(i));
  41. if (!c) {
  42. continue;
  43. }
  44. ms = ms.max(c->get_combined_minimum_size());
  45. }
  46. ms += theme_cache.panel_style->get_minimum_size();
  47. return Size2(MAX(ms.width, title_minimum_size.width), ms.height + title_minimum_size.height);
  48. }
  49. void FoldableContainer::fold() {
  50. set_folded(true);
  51. emit_signal(SNAME("folding_changed"), folded);
  52. }
  53. void FoldableContainer::expand() {
  54. set_folded(false);
  55. emit_signal(SNAME("folding_changed"), folded);
  56. }
  57. void FoldableContainer::set_folded(bool p_folded) {
  58. if (folded != p_folded) {
  59. if (!changing_group && foldable_group.is_valid()) {
  60. if (!p_folded) {
  61. _update_group();
  62. foldable_group->emit_signal(SNAME("expanded"), this);
  63. } else if (!foldable_group->updating_group && foldable_group->get_expanded_container() == this && !foldable_group->is_allow_folding_all()) {
  64. return;
  65. }
  66. }
  67. folded = p_folded;
  68. update_minimum_size();
  69. queue_sort();
  70. queue_redraw();
  71. }
  72. }
  73. bool FoldableContainer::is_folded() const {
  74. return folded;
  75. }
  76. void FoldableContainer::set_foldable_group(const Ref<FoldableGroup> &p_group) {
  77. if (foldable_group.is_valid()) {
  78. foldable_group->containers.erase(this);
  79. }
  80. foldable_group = p_group;
  81. if (foldable_group.is_valid()) {
  82. changing_group = true;
  83. if (folded && !foldable_group->get_expanded_container() && !foldable_group->is_allow_folding_all()) {
  84. set_folded(false);
  85. } else if (!folded && foldable_group->get_expanded_container()) {
  86. set_folded(true);
  87. }
  88. foldable_group->containers.insert(this);
  89. changing_group = false;
  90. }
  91. queue_redraw();
  92. }
  93. Ref<FoldableGroup> FoldableContainer::get_foldable_group() const {
  94. return foldable_group;
  95. }
  96. void FoldableContainer::set_title(const String &p_text) {
  97. if (title == p_text) {
  98. return;
  99. }
  100. title = p_text;
  101. _shape();
  102. update_minimum_size();
  103. queue_redraw();
  104. }
  105. String FoldableContainer::get_title() const {
  106. return title;
  107. }
  108. void FoldableContainer::set_title_alignment(HorizontalAlignment p_alignment) {
  109. ERR_FAIL_INDEX((int)p_alignment, 3);
  110. title_alignment = p_alignment;
  111. if (_get_actual_alignment() != text_buf->get_horizontal_alignment()) {
  112. _shape();
  113. queue_redraw();
  114. }
  115. }
  116. HorizontalAlignment FoldableContainer::get_title_alignment() const {
  117. return title_alignment;
  118. }
  119. void FoldableContainer::set_language(const String &p_language) {
  120. if (language == p_language) {
  121. return;
  122. }
  123. language = p_language;
  124. _shape();
  125. update_minimum_size();
  126. queue_redraw();
  127. }
  128. String FoldableContainer::get_language() const {
  129. return language;
  130. }
  131. void FoldableContainer::set_title_text_direction(TextDirection p_text_direction) {
  132. ERR_FAIL_INDEX(int(p_text_direction), 4);
  133. if (title_text_direction == p_text_direction) {
  134. return;
  135. }
  136. title_text_direction = p_text_direction;
  137. _shape();
  138. queue_redraw();
  139. }
  140. Control::TextDirection FoldableContainer::get_title_text_direction() const {
  141. return title_text_direction;
  142. }
  143. void FoldableContainer::set_title_text_overrun_behavior(TextServer::OverrunBehavior p_overrun_behavior) {
  144. if (overrun_behavior == p_overrun_behavior) {
  145. return;
  146. }
  147. overrun_behavior = p_overrun_behavior;
  148. _shape();
  149. update_minimum_size();
  150. queue_redraw();
  151. }
  152. TextServer::OverrunBehavior FoldableContainer::get_title_text_overrun_behavior() const {
  153. return overrun_behavior;
  154. }
  155. void FoldableContainer::set_title_position(TitlePosition p_title_position) {
  156. ERR_FAIL_INDEX(p_title_position, POSITION_MAX);
  157. if (title_position == p_title_position) {
  158. return;
  159. }
  160. title_position = p_title_position;
  161. queue_redraw();
  162. queue_sort();
  163. }
  164. FoldableContainer::TitlePosition FoldableContainer::get_title_position() const {
  165. return title_position;
  166. }
  167. void FoldableContainer::add_title_bar_control(Control *p_control) {
  168. ERR_FAIL_NULL(p_control);
  169. if (p_control->get_parent()) {
  170. p_control->get_parent()->remove_child(p_control);
  171. ERR_FAIL_COND_MSG(p_control->get_parent() != nullptr, "Failed to remove control from parent.");
  172. }
  173. add_child(p_control, false, INTERNAL_MODE_FRONT);
  174. title_controls.push_back(p_control);
  175. }
  176. void FoldableContainer::remove_title_bar_control(Control *p_control) {
  177. ERR_FAIL_NULL(p_control);
  178. int64_t index = title_controls.find(p_control);
  179. ERR_FAIL_COND_MSG(index == -1, "Can't remove control from title bar.");
  180. title_controls.remove_at(index);
  181. remove_child(p_control);
  182. }
  183. void FoldableContainer::gui_input(const Ref<InputEvent> &p_event) {
  184. ERR_FAIL_COND(p_event.is_null());
  185. Ref<InputEventMouseMotion> m = p_event;
  186. if (m.is_valid()) {
  187. if (_get_title_rect().has_point(m->get_position())) {
  188. if (!is_hovering) {
  189. is_hovering = true;
  190. queue_redraw();
  191. }
  192. } else if (is_hovering) {
  193. is_hovering = false;
  194. queue_redraw();
  195. }
  196. return;
  197. }
  198. if (p_event->is_action_pressed(SNAME("ui_accept"), false, true)) {
  199. set_folded(!folded);
  200. emit_signal(SNAME("folding_changed"), folded);
  201. accept_event();
  202. return;
  203. }
  204. Ref<InputEventMouseButton> b = p_event;
  205. if (b.is_valid()) {
  206. if (b->get_button_index() == MouseButton::LEFT && b->is_pressed() && _get_title_rect().has_point(b->get_position())) {
  207. set_folded(!folded);
  208. emit_signal(SNAME("folding_changed"), folded);
  209. accept_event();
  210. }
  211. }
  212. }
  213. String FoldableContainer::get_tooltip(const Point2 &p_pos) const {
  214. if (Rect2(0, (title_position == POSITION_TOP) ? 0 : get_size().height - title_minimum_size.height, get_size().width, title_minimum_size.height).has_point(p_pos)) {
  215. return Control::get_tooltip(p_pos);
  216. }
  217. return String();
  218. }
  219. bool FoldableContainer::has_point(const Point2 &p_point) const {
  220. if (folded) {
  221. return _get_title_rect().has_point(p_point);
  222. }
  223. return Control::has_point(p_point);
  224. }
  225. void FoldableContainer::_notification(int p_what) {
  226. switch (p_what) {
  227. case NOTIFICATION_DRAW: {
  228. RID ci = get_canvas_item();
  229. Size2 size = get_size();
  230. int h_separation = _get_h_separation();
  231. Ref<StyleBox> title_style = _get_title_style();
  232. Ref<Texture2D> icon = _get_title_icon();
  233. real_t title_controls_width = _get_title_controls_width();
  234. if (title_controls_width > 0) {
  235. title_controls_width += h_separation;
  236. }
  237. const Rect2 title_rect = _get_title_rect();
  238. _draw_flippable_stylebox(title_style, title_rect);
  239. Size2 title_ms = title_style->get_minimum_size();
  240. int title_text_width = size.width - title_ms.width;
  241. int title_style_ofs = (title_position == POSITION_TOP) ? title_style->get_margin(SIDE_TOP) : title_style->get_margin(SIDE_BOTTOM);
  242. Point2 title_text_pos(title_style->get_margin(SIDE_LEFT), title_style_ofs);
  243. title_text_pos.y += MAX((title_minimum_size.height - title_ms.height - text_buf->get_size().height) * 0.5, 0);
  244. title_text_width -= icon->get_width() + h_separation + title_controls_width;
  245. Point2 icon_pos(0, MAX((title_minimum_size.height - title_ms.height - icon->get_height()) * 0.5, 0) + title_style_ofs);
  246. bool rtl = is_layout_rtl();
  247. if (rtl) {
  248. icon_pos.x = size.width - title_style->get_margin(SIDE_RIGHT) - icon->get_width();
  249. title_text_pos.x += title_controls_width;
  250. } else {
  251. icon_pos.x = title_style->get_margin(SIDE_LEFT);
  252. title_text_pos.x += icon->get_width() + h_separation;
  253. }
  254. icon->draw(ci, title_rect.position + icon_pos);
  255. Color font_color = folded ? theme_cache.title_collapsed_font_color : theme_cache.title_font_color;
  256. if (is_hovering) {
  257. font_color = theme_cache.title_hovered_font_color;
  258. }
  259. text_buf->set_width(title_text_width);
  260. if (title_text_width > 0) {
  261. if (theme_cache.title_font_outline_size > 0 && theme_cache.title_font_outline_color.a > 0) {
  262. text_buf->draw_outline(ci, title_rect.position + title_text_pos, theme_cache.title_font_outline_size, theme_cache.title_font_outline_color);
  263. }
  264. text_buf->draw(ci, title_rect.position + title_text_pos, font_color);
  265. }
  266. if (!folded) {
  267. Rect2 panel_rect(
  268. Point2(0, (title_position == POSITION_TOP) ? title_minimum_size.height : 0),
  269. Size2(size.width, size.height - title_minimum_size.height));
  270. _draw_flippable_stylebox(theme_cache.panel_style, panel_rect);
  271. }
  272. if (has_focus(true)) {
  273. Rect2 focus_rect = folded ? title_rect : Rect2(Point2(), size);
  274. _draw_flippable_stylebox(theme_cache.focus_style, focus_rect);
  275. }
  276. } break;
  277. case NOTIFICATION_SORT_CHILDREN: {
  278. bool rtl = is_layout_rtl();
  279. const Vector2 size = get_size();
  280. const Ref<StyleBox> title_style = _get_title_style();
  281. uint32_t title_count = title_controls.size();
  282. if (title_count > 0) {
  283. int h_separation = MAX(theme_cache.h_separation, 0);
  284. real_t offset = 0.0;
  285. if (rtl) {
  286. offset = title_style->get_margin(SIDE_LEFT);
  287. } else {
  288. offset = _get_title_controls_width();
  289. offset = size.x - title_style->get_margin(SIDE_RIGHT) - offset;
  290. }
  291. real_t v_center = title_minimum_size.y * 0.5;
  292. if (title_position == POSITION_BOTTOM) {
  293. v_center = size.y - v_center + (title_style->get_margin(SIDE_BOTTOM) - title_style->get_margin(SIDE_TOP)) * 0.5;
  294. } else {
  295. v_center += (title_style->get_margin(SIDE_TOP) - title_style->get_margin(SIDE_BOTTOM)) * 0.5;
  296. }
  297. for (uint32_t i = 0; i < title_count; i++) {
  298. Control *control = title_controls[rtl ? title_count - i - 1 : i];
  299. if (!control->is_visible()) {
  300. continue;
  301. }
  302. Rect2 rect(Vector2(), control->get_combined_minimum_size());
  303. rect.position.x = offset;
  304. rect.position.y = v_center - rect.size.y * 0.5;
  305. fit_child_in_rect(control, rect);
  306. offset += rect.size.x + h_separation;
  307. }
  308. }
  309. Rect2 inner_rect;
  310. inner_rect.position.x = rtl ? theme_cache.panel_style->get_margin(SIDE_RIGHT) : theme_cache.panel_style->get_margin(SIDE_LEFT);
  311. inner_rect.size.x = size.x - theme_cache.panel_style->get_margin(SIDE_LEFT) - theme_cache.panel_style->get_margin(SIDE_RIGHT);
  312. inner_rect.position.y = theme_cache.panel_style->get_margin(SIDE_TOP);
  313. inner_rect.size.y = size.y - theme_cache.panel_style->get_margin(SIDE_TOP) - theme_cache.panel_style->get_margin(SIDE_BOTTOM) - title_minimum_size.y;
  314. if (title_position == POSITION_TOP) {
  315. inner_rect.position.y += title_minimum_size.y;
  316. }
  317. for (int i = 0; i < get_child_count(false); i++) {
  318. Control *c = as_sortable_control(get_child(i, false), SortableVisibilityMode::IGNORE);
  319. if (!c) {
  320. continue;
  321. }
  322. c->set_visible(!folded);
  323. if (!folded) {
  324. fit_child_in_rect(c, inner_rect);
  325. }
  326. }
  327. } break;
  328. case NOTIFICATION_MOUSE_EXIT: {
  329. if (is_hovering) {
  330. is_hovering = false;
  331. queue_redraw();
  332. }
  333. } break;
  334. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  335. case NOTIFICATION_TRANSLATION_CHANGED:
  336. case NOTIFICATION_THEME_CHANGED: {
  337. _shape();
  338. update_minimum_size();
  339. queue_redraw();
  340. } break;
  341. }
  342. }
  343. real_t FoldableContainer::_get_title_controls_width() const {
  344. real_t width = 0.0;
  345. int visible_controls = 0;
  346. for (const Control *control : title_controls) {
  347. if (control->is_visible()) {
  348. width += control->get_combined_minimum_size().x;
  349. visible_controls++;
  350. }
  351. }
  352. if (visible_controls > 1) {
  353. width += _get_h_separation() * (visible_controls - 1);
  354. }
  355. return width;
  356. }
  357. Ref<StyleBox> FoldableContainer::_get_title_style() const {
  358. if (is_hovering) {
  359. return folded ? theme_cache.title_collapsed_hover_style : theme_cache.title_hover_style;
  360. }
  361. return folded ? theme_cache.title_collapsed_style : theme_cache.title_style;
  362. }
  363. Ref<Texture2D> FoldableContainer::_get_title_icon() const {
  364. if (!folded) {
  365. return (title_position == POSITION_TOP) ? theme_cache.expanded_arrow : theme_cache.expanded_arrow_mirrored;
  366. } else if (is_layout_rtl()) {
  367. return theme_cache.folded_arrow_mirrored;
  368. }
  369. return theme_cache.folded_arrow;
  370. }
  371. Rect2 FoldableContainer::_get_title_rect() const {
  372. return Rect2(0, (title_position == POSITION_TOP) ? 0 : (get_size().height - title_minimum_size.height), get_size().width, title_minimum_size.height);
  373. }
  374. void FoldableContainer::_update_title_min_size() const {
  375. Ref<StyleBox> title_style = folded ? theme_cache.title_collapsed_style : theme_cache.title_style;
  376. Ref<Texture2D> icon = _get_title_icon();
  377. Size2 title_ms = title_style->get_minimum_size();
  378. int h_separation = _get_h_separation();
  379. title_minimum_size = title_ms;
  380. title_minimum_size.width += icon->get_width();
  381. if (!title.is_empty()) {
  382. title_minimum_size.width += h_separation;
  383. Size2 text_size = text_buf->get_size();
  384. title_minimum_size.height += MAX(text_size.height, icon->get_height());
  385. if (overrun_behavior == TextServer::OverrunBehavior::OVERRUN_NO_TRIMMING) {
  386. title_minimum_size.width += text_size.width;
  387. }
  388. } else {
  389. title_minimum_size.height += icon->get_height();
  390. }
  391. if (!title_controls.is_empty()) {
  392. real_t controls_height = 0;
  393. int visible_controls = 0;
  394. for (const Control *control : title_controls) {
  395. if (!control->is_visible()) {
  396. continue;
  397. }
  398. Vector2 size = control->get_combined_minimum_size();
  399. title_minimum_size.width += size.width;
  400. controls_height = MAX(controls_height, size.height);
  401. visible_controls++;
  402. }
  403. if (visible_controls > 0) {
  404. title_minimum_size.width += h_separation * visible_controls;
  405. }
  406. title_minimum_size.height = MAX(title_minimum_size.height, title_ms.height + controls_height);
  407. }
  408. }
  409. void FoldableContainer::_shape() {
  410. Ref<Font> font = theme_cache.title_font;
  411. int font_size = theme_cache.title_font_size;
  412. if (font.is_null() || font_size == 0) {
  413. return;
  414. }
  415. text_buf->clear();
  416. text_buf->set_width(-1);
  417. if (title_text_direction == TEXT_DIRECTION_INHERITED) {
  418. text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  419. } else {
  420. text_buf->set_direction((TextServer::Direction)title_text_direction);
  421. }
  422. text_buf->set_horizontal_alignment(_get_actual_alignment());
  423. text_buf->set_text_overrun_behavior(overrun_behavior);
  424. const String &lang = language.is_empty() ? _get_locale() : language;
  425. text_buf->add_string(atr(title), font, font_size, lang);
  426. }
  427. HorizontalAlignment FoldableContainer::_get_actual_alignment() const {
  428. if (is_layout_rtl()) {
  429. if (title_alignment == HORIZONTAL_ALIGNMENT_RIGHT) {
  430. return HORIZONTAL_ALIGNMENT_LEFT;
  431. } else if (title_alignment == HORIZONTAL_ALIGNMENT_LEFT) {
  432. return HORIZONTAL_ALIGNMENT_RIGHT;
  433. }
  434. }
  435. return title_alignment;
  436. }
  437. void FoldableContainer::_update_group() {
  438. foldable_group->updating_group = true;
  439. for (FoldableContainer *container : foldable_group->containers) {
  440. if (container != this) {
  441. container->set_folded(true);
  442. }
  443. }
  444. foldable_group->updating_group = false;
  445. }
  446. void FoldableContainer::_draw_flippable_stylebox(const Ref<StyleBox> p_stylebox, const Rect2 &p_rect) {
  447. if (title_position == POSITION_BOTTOM) {
  448. Rect2 rect(-p_rect.position, p_rect.size);
  449. draw_set_transform(Point2(0.0, p_stylebox->get_draw_rect(rect).size.height), 0.0, Size2(1.0, -1.0));
  450. p_stylebox->draw(get_canvas_item(), rect);
  451. draw_set_transform_matrix(Transform2D());
  452. } else {
  453. p_stylebox->draw(get_canvas_item(), p_rect);
  454. }
  455. }
  456. void FoldableContainer::_bind_methods() {
  457. ClassDB::bind_method(D_METHOD("fold"), &FoldableContainer::fold);
  458. ClassDB::bind_method(D_METHOD("expand"), &FoldableContainer::expand);
  459. ClassDB::bind_method(D_METHOD("set_folded", "folded"), &FoldableContainer::set_folded);
  460. ClassDB::bind_method(D_METHOD("is_folded"), &FoldableContainer::is_folded);
  461. ClassDB::bind_method(D_METHOD("set_foldable_group", "button_group"), &FoldableContainer::set_foldable_group);
  462. ClassDB::bind_method(D_METHOD("get_foldable_group"), &FoldableContainer::get_foldable_group);
  463. ClassDB::bind_method(D_METHOD("set_title", "text"), &FoldableContainer::set_title);
  464. ClassDB::bind_method(D_METHOD("get_title"), &FoldableContainer::get_title);
  465. ClassDB::bind_method(D_METHOD("set_title_alignment", "alignment"), &FoldableContainer::set_title_alignment);
  466. ClassDB::bind_method(D_METHOD("get_title_alignment"), &FoldableContainer::get_title_alignment);
  467. ClassDB::bind_method(D_METHOD("set_language", "language"), &FoldableContainer::set_language);
  468. ClassDB::bind_method(D_METHOD("get_language"), &FoldableContainer::get_language);
  469. ClassDB::bind_method(D_METHOD("set_title_text_direction", "text_direction"), &FoldableContainer::set_title_text_direction);
  470. ClassDB::bind_method(D_METHOD("get_title_text_direction"), &FoldableContainer::get_title_text_direction);
  471. ClassDB::bind_method(D_METHOD("set_title_text_overrun_behavior", "overrun_behavior"), &FoldableContainer::set_title_text_overrun_behavior);
  472. ClassDB::bind_method(D_METHOD("get_title_text_overrun_behavior"), &FoldableContainer::get_title_text_overrun_behavior);
  473. ClassDB::bind_method(D_METHOD("set_title_position", "title_position"), &FoldableContainer::set_title_position);
  474. ClassDB::bind_method(D_METHOD("get_title_position"), &FoldableContainer::get_title_position);
  475. ClassDB::bind_method(D_METHOD("add_title_bar_control", "control"), &FoldableContainer::add_title_bar_control);
  476. ClassDB::bind_method(D_METHOD("remove_title_bar_control", "control"), &FoldableContainer::remove_title_bar_control);
  477. ADD_SIGNAL(MethodInfo("folding_changed", PropertyInfo(Variant::BOOL, "is_folded")));
  478. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "folded"), "set_folded", "is_folded");
  479. ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
  480. ADD_PROPERTY(PropertyInfo(Variant::INT, "title_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_title_alignment", "get_title_alignment");
  481. ADD_PROPERTY(PropertyInfo(Variant::INT, "title_position", PROPERTY_HINT_ENUM, "Top,Bottom"), "set_title_position", "get_title_position");
  482. ADD_PROPERTY(PropertyInfo(Variant::INT, "title_text_overrun_behavior", PROPERTY_HINT_ENUM, "Trim Nothing,Trim Characters,Trim Words,Ellipsis,Word Ellipsis"), "set_title_text_overrun_behavior", "get_title_text_overrun_behavior");
  483. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "foldable_group", PROPERTY_HINT_RESOURCE_TYPE, "FoldableGroup"), "set_foldable_group", "get_foldable_group");
  484. ADD_GROUP("BiDi", "");
  485. ADD_PROPERTY(PropertyInfo(Variant::INT, "title_text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_title_text_direction", "get_title_text_direction");
  486. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID), "set_language", "get_language");
  487. BIND_ENUM_CONSTANT(POSITION_TOP);
  488. BIND_ENUM_CONSTANT(POSITION_BOTTOM);
  489. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, title_style, "title_panel");
  490. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, title_hover_style, "title_hover_panel");
  491. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, title_collapsed_style, "title_collapsed_panel");
  492. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, title_collapsed_hover_style, "title_collapsed_hover_panel");
  493. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, focus_style, "focus");
  494. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, panel_style, "panel");
  495. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_FONT, FoldableContainer, title_font, "font");
  496. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_FONT_SIZE, FoldableContainer, title_font_size, "font_size");
  497. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, FoldableContainer, title_font_outline_size, "outline_size");
  498. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, FoldableContainer, title_font_color, "font_color");
  499. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, FoldableContainer, title_hovered_font_color, "hover_font_color");
  500. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, FoldableContainer, title_collapsed_font_color, "collapsed_font_color");
  501. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, FoldableContainer, title_font_outline_color, "font_outline_color");
  502. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FoldableContainer, expanded_arrow);
  503. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FoldableContainer, expanded_arrow_mirrored);
  504. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FoldableContainer, folded_arrow);
  505. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FoldableContainer, folded_arrow_mirrored);
  506. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, FoldableContainer, h_separation);
  507. }
  508. FoldableContainer::FoldableContainer(const String &p_text) {
  509. text_buf.instantiate();
  510. set_title(p_text);
  511. set_focus_mode(FOCUS_ALL);
  512. set_mouse_filter(MOUSE_FILTER_STOP);
  513. }
  514. FoldableContainer::~FoldableContainer() {
  515. if (foldable_group.is_valid()) {
  516. foldable_group->containers.erase(this);
  517. }
  518. }
  519. FoldableContainer *FoldableGroup::get_expanded_container() const {
  520. for (FoldableContainer *container : containers) {
  521. if (!container->is_folded()) {
  522. return container;
  523. }
  524. }
  525. return nullptr;
  526. }
  527. void FoldableGroup::set_allow_folding_all(bool p_enabled) {
  528. allow_folding_all = p_enabled;
  529. if (!allow_folding_all && !get_expanded_container() && containers.size() > 0) {
  530. updating_group = true;
  531. (*containers.begin())->set_folded(false);
  532. updating_group = false;
  533. }
  534. }
  535. bool FoldableGroup::is_allow_folding_all() const {
  536. return allow_folding_all;
  537. }
  538. void FoldableGroup::get_containers(List<FoldableContainer *> *r_containers) const {
  539. for (FoldableContainer *container : containers) {
  540. r_containers->push_back(container);
  541. }
  542. }
  543. TypedArray<FoldableContainer> FoldableGroup::_get_containers() const {
  544. TypedArray<FoldableContainer> foldable_containers;
  545. for (const FoldableContainer *container : containers) {
  546. foldable_containers.push_back(container);
  547. }
  548. return foldable_containers;
  549. }
  550. void FoldableGroup::_bind_methods() {
  551. ClassDB::bind_method(D_METHOD("get_expanded_container"), &FoldableGroup::get_expanded_container);
  552. ClassDB::bind_method(D_METHOD("get_containers"), &FoldableGroup::_get_containers);
  553. ClassDB::bind_method(D_METHOD("set_allow_folding_all", "enabled"), &FoldableGroup::set_allow_folding_all);
  554. ClassDB::bind_method(D_METHOD("is_allow_folding_all"), &FoldableGroup::is_allow_folding_all);
  555. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_folding_all"), "set_allow_folding_all", "is_allow_folding_all");
  556. ADD_SIGNAL(MethodInfo("expanded", PropertyInfo(Variant::OBJECT, "container", PROPERTY_HINT_RESOURCE_TYPE, "FoldableContainer")));
  557. }
  558. FoldableGroup::FoldableGroup() {
  559. set_local_to_scene(true);
  560. }