2
0

foldable_container.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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. Rect2 title_rect = Rect2(0, (title_position == POSITION_TOP) ? 0 : get_size().height - title_minimum_size.height, get_size().width, title_minimum_size.height);
  188. if (title_rect.has_point(m->get_position())) {
  189. if (!is_hovering) {
  190. is_hovering = true;
  191. queue_redraw();
  192. }
  193. } else if (is_hovering) {
  194. is_hovering = false;
  195. queue_redraw();
  196. }
  197. return;
  198. }
  199. if (p_event->is_action_pressed(SNAME("ui_accept"), false, true)) {
  200. set_folded(!folded);
  201. emit_signal(SNAME("folding_changed"), folded);
  202. accept_event();
  203. return;
  204. }
  205. Ref<InputEventMouseButton> b = p_event;
  206. if (b.is_valid()) {
  207. Rect2 title_rect = Rect2(0, (title_position == POSITION_TOP) ? 0 : get_size().height - title_minimum_size.height, get_size().width, title_minimum_size.height);
  208. if (b->get_button_index() == MouseButton::LEFT && b->is_pressed() && title_rect.has_point(b->get_position())) {
  209. set_folded(!folded);
  210. emit_signal(SNAME("folding_changed"), folded);
  211. accept_event();
  212. }
  213. }
  214. }
  215. String FoldableContainer::get_tooltip(const Point2 &p_pos) const {
  216. 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)) {
  217. return Control::get_tooltip(p_pos);
  218. }
  219. return String();
  220. }
  221. void FoldableContainer::_notification(int p_what) {
  222. switch (p_what) {
  223. case NOTIFICATION_DRAW: {
  224. RID ci = get_canvas_item();
  225. Size2 size = get_size();
  226. int h_separation = _get_h_separation();
  227. Ref<StyleBox> title_style = _get_title_style();
  228. Ref<Texture2D> icon = _get_title_icon();
  229. real_t title_controls_width = _get_title_controls_width();
  230. if (title_controls_width > 0) {
  231. title_controls_width += h_separation;
  232. }
  233. Rect2 title_rect(
  234. Point2(0, (title_position == POSITION_TOP) ? 0 : size.height - title_minimum_size.height),
  235. Size2(size.width, title_minimum_size.height));
  236. _draw_flippable_stylebox(title_style, title_rect);
  237. Size2 title_ms = title_style->get_minimum_size();
  238. int title_text_width = size.width - title_ms.width;
  239. int title_style_ofs = (title_position == POSITION_TOP) ? title_style->get_margin(SIDE_TOP) : title_style->get_margin(SIDE_BOTTOM);
  240. Point2 title_text_pos(title_style->get_margin(SIDE_LEFT), title_style_ofs);
  241. title_text_pos.y += MAX((title_minimum_size.height - title_ms.height - text_buf->get_size().height) * 0.5, 0);
  242. title_text_width -= icon->get_width() + h_separation + title_controls_width;
  243. Point2 icon_pos(0, MAX((title_minimum_size.height - title_ms.height - icon->get_height()) * 0.5, 0) + title_style_ofs);
  244. bool rtl = is_layout_rtl();
  245. if (rtl) {
  246. icon_pos.x = size.width - title_style->get_margin(SIDE_RIGHT) - icon->get_width();
  247. title_text_pos.x += title_controls_width;
  248. } else {
  249. icon_pos.x = title_style->get_margin(SIDE_LEFT);
  250. title_text_pos.x += icon->get_width() + h_separation;
  251. }
  252. icon->draw(ci, title_rect.position + icon_pos);
  253. Color font_color = folded ? theme_cache.title_collapsed_font_color : theme_cache.title_font_color;
  254. if (is_hovering) {
  255. font_color = theme_cache.title_hovered_font_color;
  256. }
  257. text_buf->set_width(title_text_width);
  258. if (title_text_width > 0) {
  259. if (theme_cache.title_font_outline_size > 0 && theme_cache.title_font_outline_color.a > 0) {
  260. text_buf->draw_outline(ci, title_rect.position + title_text_pos, theme_cache.title_font_outline_size, theme_cache.title_font_outline_color);
  261. }
  262. text_buf->draw(ci, title_rect.position + title_text_pos, font_color);
  263. }
  264. if (!folded) {
  265. Rect2 panel_rect(
  266. Point2(0, (title_position == POSITION_TOP) ? title_minimum_size.height : 0),
  267. Size2(size.width, size.height - title_minimum_size.height));
  268. _draw_flippable_stylebox(theme_cache.panel_style, panel_rect);
  269. }
  270. if (has_focus()) {
  271. Rect2 focus_rect = folded ? title_rect : Rect2(Point2(), size);
  272. _draw_flippable_stylebox(theme_cache.focus_style, focus_rect);
  273. }
  274. } break;
  275. case NOTIFICATION_SORT_CHILDREN: {
  276. bool rtl = is_layout_rtl();
  277. const Vector2 size = get_size();
  278. const Ref<StyleBox> title_style = _get_title_style();
  279. uint32_t title_count = title_controls.size();
  280. if (title_count > 0) {
  281. int h_separation = MAX(theme_cache.h_separation, 0);
  282. real_t offset = 0.0;
  283. if (rtl) {
  284. offset = title_style->get_margin(SIDE_LEFT);
  285. } else {
  286. offset = _get_title_controls_width();
  287. offset = size.x - title_style->get_margin(SIDE_RIGHT) - offset;
  288. }
  289. real_t v_center = title_minimum_size.y * 0.5;
  290. if (title_position == POSITION_BOTTOM) {
  291. v_center = size.y - v_center + (title_style->get_margin(SIDE_BOTTOM) - title_style->get_margin(SIDE_TOP)) * 0.5;
  292. } else {
  293. v_center += (title_style->get_margin(SIDE_TOP) - title_style->get_margin(SIDE_BOTTOM)) * 0.5;
  294. }
  295. for (uint32_t i = 0; i < title_count; i++) {
  296. Control *control = title_controls[rtl ? title_count - i - 1 : i];
  297. if (!control->is_visible()) {
  298. continue;
  299. }
  300. Rect2 rect(Vector2(), control->get_combined_minimum_size());
  301. rect.position.x = offset;
  302. rect.position.y = v_center - rect.size.y * 0.5;
  303. fit_child_in_rect(control, rect);
  304. offset += rect.size.x + h_separation;
  305. }
  306. }
  307. Rect2 inner_rect;
  308. inner_rect.position.x = rtl ? theme_cache.panel_style->get_margin(SIDE_RIGHT) : theme_cache.panel_style->get_margin(SIDE_LEFT);
  309. inner_rect.size.x = size.x - theme_cache.panel_style->get_margin(SIDE_LEFT) - theme_cache.panel_style->get_margin(SIDE_RIGHT);
  310. inner_rect.position.y = theme_cache.panel_style->get_margin(SIDE_TOP);
  311. 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;
  312. if (title_position == POSITION_TOP) {
  313. inner_rect.position.y += title_minimum_size.y;
  314. }
  315. for (int i = 0; i < get_child_count(false); i++) {
  316. Control *c = as_sortable_control(get_child(i, false), SortableVisibilityMode::IGNORE);
  317. if (!c) {
  318. continue;
  319. }
  320. c->set_visible(!folded);
  321. if (!folded) {
  322. fit_child_in_rect(c, inner_rect);
  323. }
  324. }
  325. } break;
  326. case NOTIFICATION_MOUSE_EXIT: {
  327. if (is_hovering) {
  328. is_hovering = false;
  329. queue_redraw();
  330. }
  331. } break;
  332. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  333. case NOTIFICATION_TRANSLATION_CHANGED:
  334. case NOTIFICATION_THEME_CHANGED: {
  335. _shape();
  336. update_minimum_size();
  337. queue_redraw();
  338. } break;
  339. }
  340. }
  341. real_t FoldableContainer::_get_title_controls_width() const {
  342. real_t width = 0.0;
  343. int visible_controls = 0;
  344. for (const Control *control : title_controls) {
  345. if (control->is_visible()) {
  346. width += control->get_combined_minimum_size().x;
  347. visible_controls++;
  348. }
  349. }
  350. if (visible_controls > 1) {
  351. width += _get_h_separation() * (visible_controls - 1);
  352. }
  353. return width;
  354. }
  355. Ref<StyleBox> FoldableContainer::_get_title_style() const {
  356. if (is_hovering) {
  357. return folded ? theme_cache.title_collapsed_hover_style : theme_cache.title_hover_style;
  358. }
  359. return folded ? theme_cache.title_collapsed_style : theme_cache.title_style;
  360. }
  361. Ref<Texture2D> FoldableContainer::_get_title_icon() const {
  362. if (!folded) {
  363. return (title_position == POSITION_TOP) ? theme_cache.expanded_arrow : theme_cache.expanded_arrow_mirrored;
  364. } else if (is_layout_rtl()) {
  365. return theme_cache.folded_arrow_mirrored;
  366. }
  367. return theme_cache.folded_arrow;
  368. }
  369. void FoldableContainer::_update_title_min_size() const {
  370. Ref<StyleBox> title_style = folded ? theme_cache.title_collapsed_style : theme_cache.title_style;
  371. Ref<Texture2D> icon = _get_title_icon();
  372. Size2 title_ms = title_style->get_minimum_size();
  373. int h_separation = _get_h_separation();
  374. title_minimum_size = title_ms;
  375. title_minimum_size.width += icon->get_width();
  376. if (!title.is_empty()) {
  377. title_minimum_size.width += h_separation;
  378. Size2 text_size = text_buf->get_size();
  379. title_minimum_size.height += MAX(text_size.height, icon->get_height());
  380. if (overrun_behavior == TextServer::OverrunBehavior::OVERRUN_NO_TRIMMING) {
  381. title_minimum_size.width += text_size.width;
  382. }
  383. } else {
  384. title_minimum_size.height += icon->get_height();
  385. }
  386. if (!title_controls.is_empty()) {
  387. real_t controls_height = 0;
  388. int visible_controls = 0;
  389. for (const Control *control : title_controls) {
  390. if (!control->is_visible()) {
  391. continue;
  392. }
  393. Vector2 size = control->get_combined_minimum_size();
  394. title_minimum_size.width += size.width;
  395. controls_height = MAX(controls_height, size.height);
  396. visible_controls++;
  397. }
  398. if (visible_controls > 0) {
  399. title_minimum_size.width += h_separation * visible_controls;
  400. }
  401. title_minimum_size.height = MAX(title_minimum_size.height, title_ms.height + controls_height);
  402. }
  403. }
  404. void FoldableContainer::_shape() {
  405. Ref<Font> font = theme_cache.title_font;
  406. int font_size = theme_cache.title_font_size;
  407. if (font.is_null() || font_size == 0) {
  408. return;
  409. }
  410. text_buf->clear();
  411. text_buf->set_width(-1);
  412. if (title_text_direction == TEXT_DIRECTION_INHERITED) {
  413. text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  414. } else {
  415. text_buf->set_direction((TextServer::Direction)title_text_direction);
  416. }
  417. text_buf->set_horizontal_alignment(_get_actual_alignment());
  418. text_buf->set_text_overrun_behavior(overrun_behavior);
  419. text_buf->add_string(atr(title), font, font_size, language);
  420. }
  421. HorizontalAlignment FoldableContainer::_get_actual_alignment() const {
  422. if (is_layout_rtl()) {
  423. if (title_alignment == HORIZONTAL_ALIGNMENT_RIGHT) {
  424. return HORIZONTAL_ALIGNMENT_LEFT;
  425. } else if (title_alignment == HORIZONTAL_ALIGNMENT_LEFT) {
  426. return HORIZONTAL_ALIGNMENT_RIGHT;
  427. }
  428. }
  429. return title_alignment;
  430. }
  431. void FoldableContainer::_update_group() {
  432. foldable_group->updating_group = true;
  433. for (FoldableContainer *container : foldable_group->containers) {
  434. if (container != this) {
  435. container->set_folded(true);
  436. }
  437. }
  438. foldable_group->updating_group = false;
  439. }
  440. void FoldableContainer::_draw_flippable_stylebox(const Ref<StyleBox> p_stylebox, const Rect2 &p_rect) {
  441. if (title_position == POSITION_BOTTOM) {
  442. Rect2 rect(-p_rect.position, p_rect.size);
  443. draw_set_transform(Point2(0.0, p_stylebox->get_draw_rect(rect).size.height), 0.0, Size2(1.0, -1.0));
  444. p_stylebox->draw(get_canvas_item(), rect);
  445. draw_set_transform_matrix(Transform2D());
  446. } else {
  447. p_stylebox->draw(get_canvas_item(), p_rect);
  448. }
  449. }
  450. void FoldableContainer::_bind_methods() {
  451. ClassDB::bind_method(D_METHOD("fold"), &FoldableContainer::fold);
  452. ClassDB::bind_method(D_METHOD("expand"), &FoldableContainer::expand);
  453. ClassDB::bind_method(D_METHOD("set_folded", "folded"), &FoldableContainer::set_folded);
  454. ClassDB::bind_method(D_METHOD("is_folded"), &FoldableContainer::is_folded);
  455. ClassDB::bind_method(D_METHOD("set_foldable_group", "button_group"), &FoldableContainer::set_foldable_group);
  456. ClassDB::bind_method(D_METHOD("get_foldable_group"), &FoldableContainer::get_foldable_group);
  457. ClassDB::bind_method(D_METHOD("set_title", "text"), &FoldableContainer::set_title);
  458. ClassDB::bind_method(D_METHOD("get_title"), &FoldableContainer::get_title);
  459. ClassDB::bind_method(D_METHOD("set_title_alignment", "alignment"), &FoldableContainer::set_title_alignment);
  460. ClassDB::bind_method(D_METHOD("get_title_alignment"), &FoldableContainer::get_title_alignment);
  461. ClassDB::bind_method(D_METHOD("set_language", "language"), &FoldableContainer::set_language);
  462. ClassDB::bind_method(D_METHOD("get_language"), &FoldableContainer::get_language);
  463. ClassDB::bind_method(D_METHOD("set_title_text_direction", "text_direction"), &FoldableContainer::set_title_text_direction);
  464. ClassDB::bind_method(D_METHOD("get_title_text_direction"), &FoldableContainer::get_title_text_direction);
  465. ClassDB::bind_method(D_METHOD("set_title_text_overrun_behavior", "overrun_behavior"), &FoldableContainer::set_title_text_overrun_behavior);
  466. ClassDB::bind_method(D_METHOD("get_title_text_overrun_behavior"), &FoldableContainer::get_title_text_overrun_behavior);
  467. ClassDB::bind_method(D_METHOD("set_title_position", "title_position"), &FoldableContainer::set_title_position);
  468. ClassDB::bind_method(D_METHOD("get_title_position"), &FoldableContainer::get_title_position);
  469. ClassDB::bind_method(D_METHOD("add_title_bar_control", "control"), &FoldableContainer::add_title_bar_control);
  470. ClassDB::bind_method(D_METHOD("remove_title_bar_control", "control"), &FoldableContainer::remove_title_bar_control);
  471. ADD_SIGNAL(MethodInfo("folding_changed", PropertyInfo(Variant::BOOL, "is_folded")));
  472. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "folded"), "set_folded", "is_folded");
  473. ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
  474. ADD_PROPERTY(PropertyInfo(Variant::INT, "title_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_title_alignment", "get_title_alignment");
  475. ADD_PROPERTY(PropertyInfo(Variant::INT, "title_position", PROPERTY_HINT_ENUM, "Top,Bottom"), "set_title_position", "get_title_position");
  476. 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");
  477. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "foldable_group", PROPERTY_HINT_RESOURCE_TYPE, "FoldableGroup"), "set_foldable_group", "get_foldable_group");
  478. ADD_GROUP("BiDi", "");
  479. 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");
  480. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID), "set_language", "get_language");
  481. BIND_ENUM_CONSTANT(POSITION_TOP);
  482. BIND_ENUM_CONSTANT(POSITION_BOTTOM);
  483. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, title_style, "title_panel");
  484. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, title_hover_style, "title_hover_panel");
  485. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, title_collapsed_style, "title_collapsed_panel");
  486. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, title_collapsed_hover_style, "title_collapsed_hover_panel");
  487. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, focus_style, "focus");
  488. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, panel_style, "panel");
  489. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_FONT, FoldableContainer, title_font, "font");
  490. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_FONT_SIZE, FoldableContainer, title_font_size, "font_size");
  491. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, FoldableContainer, title_font_outline_size, "outline_size");
  492. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, FoldableContainer, title_font_color, "font_color");
  493. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, FoldableContainer, title_hovered_font_color, "hover_font_color");
  494. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, FoldableContainer, title_collapsed_font_color, "collapsed_font_color");
  495. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, FoldableContainer, title_font_outline_color, "font_outline_color");
  496. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FoldableContainer, expanded_arrow);
  497. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FoldableContainer, expanded_arrow_mirrored);
  498. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FoldableContainer, folded_arrow);
  499. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FoldableContainer, folded_arrow_mirrored);
  500. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, FoldableContainer, h_separation);
  501. }
  502. FoldableContainer::FoldableContainer(const String &p_text) {
  503. text_buf.instantiate();
  504. set_title(p_text);
  505. set_focus_mode(FOCUS_ALL);
  506. set_mouse_filter(MOUSE_FILTER_STOP);
  507. }
  508. FoldableContainer::~FoldableContainer() {
  509. if (foldable_group.is_valid()) {
  510. foldable_group->containers.erase(this);
  511. }
  512. }
  513. FoldableContainer *FoldableGroup::get_expanded_container() const {
  514. for (FoldableContainer *container : containers) {
  515. if (!container->is_folded()) {
  516. return container;
  517. }
  518. }
  519. return nullptr;
  520. }
  521. void FoldableGroup::set_allow_folding_all(bool p_enabled) {
  522. allow_folding_all = p_enabled;
  523. if (!allow_folding_all && !get_expanded_container() && containers.size() > 0) {
  524. updating_group = true;
  525. (*containers.begin())->set_folded(false);
  526. updating_group = false;
  527. }
  528. }
  529. bool FoldableGroup::is_allow_folding_all() const {
  530. return allow_folding_all;
  531. }
  532. void FoldableGroup::get_containers(List<FoldableContainer *> *r_containers) const {
  533. for (FoldableContainer *container : containers) {
  534. r_containers->push_back(container);
  535. }
  536. }
  537. TypedArray<FoldableContainer> FoldableGroup::_get_containers() const {
  538. TypedArray<FoldableContainer> foldable_containers;
  539. for (const FoldableContainer *container : containers) {
  540. foldable_containers.push_back(container);
  541. }
  542. return foldable_containers;
  543. }
  544. void FoldableGroup::_bind_methods() {
  545. ClassDB::bind_method(D_METHOD("get_expanded_container"), &FoldableGroup::get_expanded_container);
  546. ClassDB::bind_method(D_METHOD("get_containers"), &FoldableGroup::_get_containers);
  547. ClassDB::bind_method(D_METHOD("set_allow_folding_all", "enabled"), &FoldableGroup::set_allow_folding_all);
  548. ClassDB::bind_method(D_METHOD("is_allow_folding_all"), &FoldableGroup::is_allow_folding_all);
  549. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_folding_all"), "set_allow_folding_all", "is_allow_folding_all");
  550. ADD_SIGNAL(MethodInfo("expanded", PropertyInfo(Variant::OBJECT, "container", PROPERTY_HINT_RESOURCE_TYPE, "FoldableContainer")));
  551. }
  552. FoldableGroup::FoldableGroup() {
  553. set_local_to_scene(true);
  554. }