split_container.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*************************************************************************/
  2. /* split_container.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 "split_container.h"
  31. #include "label.h"
  32. #include "margin_container.h"
  33. Control *SplitContainer::_getch(int p_idx) const {
  34. int idx = 0;
  35. for (int i = 0; i < get_child_count(); i++) {
  36. Control *c = Object::cast_to<Control>(get_child(i));
  37. if (!c || !c->is_visible()) {
  38. continue;
  39. }
  40. if (c->is_set_as_top_level()) {
  41. continue;
  42. }
  43. if (idx == p_idx) {
  44. return c;
  45. }
  46. idx++;
  47. }
  48. return nullptr;
  49. }
  50. void SplitContainer::_resort() {
  51. int axis = vertical ? 1 : 0;
  52. Control *first = _getch(0);
  53. Control *second = _getch(1);
  54. // If we have only one element
  55. if (!first || !second) {
  56. if (first) {
  57. fit_child_in_rect(first, Rect2(Point2(), get_size()));
  58. } else if (second) {
  59. fit_child_in_rect(second, Rect2(Point2(), get_size()));
  60. }
  61. return;
  62. }
  63. // Determine expanded children
  64. bool first_expanded = (vertical ? first->get_v_size_flags() : first->get_h_size_flags()) & SIZE_EXPAND;
  65. bool second_expanded = (vertical ? second->get_v_size_flags() : second->get_h_size_flags()) & SIZE_EXPAND;
  66. // Determine the separation between items
  67. Ref<Texture2D> g = get_theme_icon(SNAME("grabber"));
  68. int sep = get_theme_constant(SNAME("separation"));
  69. sep = (dragger_visibility != DRAGGER_HIDDEN_COLLAPSED) ? MAX(sep, vertical ? g->get_height() : g->get_width()) : 0;
  70. // Compute the minimum size
  71. Size2 ms_first = first->get_combined_minimum_size();
  72. Size2 ms_second = second->get_combined_minimum_size();
  73. // Compute the separator position without the split offset
  74. float ratio = first->get_stretch_ratio() / (first->get_stretch_ratio() + second->get_stretch_ratio());
  75. int no_offset_middle_sep = 0;
  76. if (first_expanded && second_expanded) {
  77. no_offset_middle_sep = get_size()[axis] * ratio - sep / 2;
  78. } else if (first_expanded) {
  79. no_offset_middle_sep = get_size()[axis] - ms_second[axis] - sep;
  80. } else {
  81. no_offset_middle_sep = ms_first[axis];
  82. }
  83. // Compute the final middle separation
  84. middle_sep = no_offset_middle_sep;
  85. if (!collapsed) {
  86. int clamped_split_offset = CLAMP(split_offset, ms_first[axis] - no_offset_middle_sep, get_size()[axis] - ms_second[axis] - sep);
  87. middle_sep = MAX(middle_sep, clamped_split_offset);
  88. if (should_clamp_split_offset) {
  89. split_offset = clamped_split_offset;
  90. should_clamp_split_offset = false;
  91. }
  92. }
  93. if (vertical) {
  94. fit_child_in_rect(first, Rect2(Point2(0, 0), Size2(get_size().width, middle_sep)));
  95. int sofs = middle_sep + sep;
  96. fit_child_in_rect(second, Rect2(Point2(0, sofs), Size2(get_size().width, get_size().height - sofs)));
  97. } else {
  98. if (is_layout_rtl()) {
  99. middle_sep = get_size().width - middle_sep - sep;
  100. fit_child_in_rect(second, Rect2(Point2(0, 0), Size2(middle_sep, get_size().height)));
  101. int sofs = middle_sep + sep;
  102. fit_child_in_rect(first, Rect2(Point2(sofs, 0), Size2(get_size().width - sofs, get_size().height)));
  103. } else {
  104. fit_child_in_rect(first, Rect2(Point2(0, 0), Size2(middle_sep, get_size().height)));
  105. int sofs = middle_sep + sep;
  106. fit_child_in_rect(second, Rect2(Point2(sofs, 0), Size2(get_size().width - sofs, get_size().height)));
  107. }
  108. }
  109. update();
  110. }
  111. Size2 SplitContainer::get_minimum_size() const {
  112. /* Calculate MINIMUM SIZE */
  113. Size2i minimum;
  114. Ref<Texture2D> g = get_theme_icon(SNAME("grabber"));
  115. int sep = get_theme_constant(SNAME("separation"));
  116. sep = (dragger_visibility != DRAGGER_HIDDEN_COLLAPSED) ? MAX(sep, vertical ? g->get_height() : g->get_width()) : 0;
  117. for (int i = 0; i < 2; i++) {
  118. if (!_getch(i)) {
  119. break;
  120. }
  121. if (i == 1) {
  122. if (vertical) {
  123. minimum.height += sep;
  124. } else {
  125. minimum.width += sep;
  126. }
  127. }
  128. Size2 ms = _getch(i)->get_combined_minimum_size();
  129. if (vertical) {
  130. minimum.height += ms.height;
  131. minimum.width = MAX(minimum.width, ms.width);
  132. } else {
  133. minimum.width += ms.width;
  134. minimum.height = MAX(minimum.height, ms.height);
  135. }
  136. }
  137. return minimum;
  138. }
  139. void SplitContainer::_notification(int p_what) {
  140. switch (p_what) {
  141. case NOTIFICATION_TRANSLATION_CHANGED:
  142. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  143. queue_sort();
  144. } break;
  145. case NOTIFICATION_SORT_CHILDREN: {
  146. _resort();
  147. } break;
  148. case NOTIFICATION_MOUSE_EXIT: {
  149. mouse_inside = false;
  150. if (get_theme_constant(SNAME("autohide"))) {
  151. update();
  152. }
  153. } break;
  154. case NOTIFICATION_DRAW: {
  155. if (!_getch(0) || !_getch(1)) {
  156. return;
  157. }
  158. if (collapsed || (!dragging && !mouse_inside && get_theme_constant(SNAME("autohide")))) {
  159. return;
  160. }
  161. if (dragger_visibility != DRAGGER_VISIBLE) {
  162. return;
  163. }
  164. int sep = dragger_visibility != DRAGGER_HIDDEN_COLLAPSED ? get_theme_constant(SNAME("separation")) : 0;
  165. Ref<Texture2D> tex = get_theme_icon(SNAME("grabber"));
  166. Size2 size = get_size();
  167. if (vertical) {
  168. draw_texture(tex, Point2i((size.x - tex->get_width()) / 2, middle_sep + (sep - tex->get_height()) / 2));
  169. } else {
  170. draw_texture(tex, Point2i(middle_sep + (sep - tex->get_width()) / 2, (size.y - tex->get_height()) / 2));
  171. }
  172. } break;
  173. case NOTIFICATION_THEME_CHANGED: {
  174. update_minimum_size();
  175. } break;
  176. }
  177. }
  178. void SplitContainer::gui_input(const Ref<InputEvent> &p_event) {
  179. ERR_FAIL_COND(p_event.is_null());
  180. if (collapsed || !_getch(0) || !_getch(1) || dragger_visibility != DRAGGER_VISIBLE) {
  181. return;
  182. }
  183. Ref<InputEventMouseButton> mb = p_event;
  184. if (mb.is_valid()) {
  185. if (mb->get_button_index() == MouseButton::LEFT) {
  186. if (mb->is_pressed()) {
  187. int sep = get_theme_constant(SNAME("separation"));
  188. if (vertical) {
  189. if (mb->get_position().y > middle_sep && mb->get_position().y < middle_sep + sep) {
  190. dragging = true;
  191. drag_from = mb->get_position().y;
  192. drag_ofs = split_offset;
  193. }
  194. } else {
  195. if (mb->get_position().x > middle_sep && mb->get_position().x < middle_sep + sep) {
  196. dragging = true;
  197. drag_from = mb->get_position().x;
  198. drag_ofs = split_offset;
  199. }
  200. }
  201. } else {
  202. dragging = false;
  203. }
  204. }
  205. }
  206. Ref<InputEventMouseMotion> mm = p_event;
  207. if (mm.is_valid()) {
  208. bool mouse_inside_state = false;
  209. if (vertical) {
  210. mouse_inside_state = mm->get_position().y > middle_sep && mm->get_position().y < middle_sep + get_theme_constant(SNAME("separation"));
  211. } else {
  212. mouse_inside_state = mm->get_position().x > middle_sep && mm->get_position().x < middle_sep + get_theme_constant(SNAME("separation"));
  213. }
  214. if (mouse_inside != mouse_inside_state) {
  215. mouse_inside = mouse_inside_state;
  216. if (get_theme_constant(SNAME("autohide"))) {
  217. update();
  218. }
  219. }
  220. if (!dragging) {
  221. return;
  222. }
  223. if (!vertical && is_layout_rtl()) {
  224. split_offset = drag_ofs + (drag_from - (vertical ? mm->get_position().y : mm->get_position().x));
  225. } else {
  226. split_offset = drag_ofs + ((vertical ? mm->get_position().y : mm->get_position().x) - drag_from);
  227. }
  228. should_clamp_split_offset = true;
  229. queue_sort();
  230. emit_signal(SNAME("dragged"), get_split_offset());
  231. }
  232. }
  233. Control::CursorShape SplitContainer::get_cursor_shape(const Point2 &p_pos) const {
  234. if (dragging) {
  235. return (vertical ? CURSOR_VSPLIT : CURSOR_HSPLIT);
  236. }
  237. if (!collapsed && _getch(0) && _getch(1) && dragger_visibility == DRAGGER_VISIBLE) {
  238. int sep = get_theme_constant(SNAME("separation"));
  239. if (vertical) {
  240. if (p_pos.y > middle_sep && p_pos.y < middle_sep + sep) {
  241. return CURSOR_VSPLIT;
  242. }
  243. } else {
  244. if (p_pos.x > middle_sep && p_pos.x < middle_sep + sep) {
  245. return CURSOR_HSPLIT;
  246. }
  247. }
  248. }
  249. return Control::get_cursor_shape(p_pos);
  250. }
  251. void SplitContainer::set_split_offset(int p_offset) {
  252. if (split_offset == p_offset) {
  253. return;
  254. }
  255. split_offset = p_offset;
  256. queue_sort();
  257. }
  258. int SplitContainer::get_split_offset() const {
  259. return split_offset;
  260. }
  261. void SplitContainer::clamp_split_offset() {
  262. should_clamp_split_offset = true;
  263. queue_sort();
  264. }
  265. void SplitContainer::set_collapsed(bool p_collapsed) {
  266. if (collapsed == p_collapsed) {
  267. return;
  268. }
  269. collapsed = p_collapsed;
  270. queue_sort();
  271. }
  272. void SplitContainer::set_dragger_visibility(DraggerVisibility p_visibility) {
  273. dragger_visibility = p_visibility;
  274. queue_sort();
  275. update();
  276. }
  277. SplitContainer::DraggerVisibility SplitContainer::get_dragger_visibility() const {
  278. return dragger_visibility;
  279. }
  280. bool SplitContainer::is_collapsed() const {
  281. return collapsed;
  282. }
  283. Vector<int> SplitContainer::get_allowed_size_flags_horizontal() const {
  284. Vector<int> flags;
  285. flags.append(SIZE_FILL);
  286. if (!vertical) {
  287. flags.append(SIZE_EXPAND);
  288. }
  289. flags.append(SIZE_SHRINK_BEGIN);
  290. flags.append(SIZE_SHRINK_CENTER);
  291. flags.append(SIZE_SHRINK_END);
  292. return flags;
  293. }
  294. Vector<int> SplitContainer::get_allowed_size_flags_vertical() const {
  295. Vector<int> flags;
  296. flags.append(SIZE_FILL);
  297. if (vertical) {
  298. flags.append(SIZE_EXPAND);
  299. }
  300. flags.append(SIZE_SHRINK_BEGIN);
  301. flags.append(SIZE_SHRINK_CENTER);
  302. flags.append(SIZE_SHRINK_END);
  303. return flags;
  304. }
  305. void SplitContainer::_bind_methods() {
  306. ClassDB::bind_method(D_METHOD("set_split_offset", "offset"), &SplitContainer::set_split_offset);
  307. ClassDB::bind_method(D_METHOD("get_split_offset"), &SplitContainer::get_split_offset);
  308. ClassDB::bind_method(D_METHOD("clamp_split_offset"), &SplitContainer::clamp_split_offset);
  309. ClassDB::bind_method(D_METHOD("set_collapsed", "collapsed"), &SplitContainer::set_collapsed);
  310. ClassDB::bind_method(D_METHOD("is_collapsed"), &SplitContainer::is_collapsed);
  311. ClassDB::bind_method(D_METHOD("set_dragger_visibility", "mode"), &SplitContainer::set_dragger_visibility);
  312. ClassDB::bind_method(D_METHOD("get_dragger_visibility"), &SplitContainer::get_dragger_visibility);
  313. ADD_SIGNAL(MethodInfo("dragged", PropertyInfo(Variant::INT, "offset")));
  314. ADD_PROPERTY(PropertyInfo(Variant::INT, "split_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_split_offset", "get_split_offset");
  315. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collapsed"), "set_collapsed", "is_collapsed");
  316. ADD_PROPERTY(PropertyInfo(Variant::INT, "dragger_visibility", PROPERTY_HINT_ENUM, "Visible,Hidden,Hidden and Collapsed"), "set_dragger_visibility", "get_dragger_visibility");
  317. BIND_ENUM_CONSTANT(DRAGGER_VISIBLE);
  318. BIND_ENUM_CONSTANT(DRAGGER_HIDDEN);
  319. BIND_ENUM_CONSTANT(DRAGGER_HIDDEN_COLLAPSED);
  320. }
  321. SplitContainer::SplitContainer(bool p_vertical) {
  322. vertical = p_vertical;
  323. }