split_container.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. void SplitContainerDragger::gui_input(const Ref<InputEvent> &p_event) {
  34. ERR_FAIL_COND(p_event.is_null());
  35. SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent());
  36. if (sc->collapsed || !sc->_getch(0) || !sc->_getch(1) || sc->dragger_visibility != SplitContainer::DRAGGER_VISIBLE) {
  37. return;
  38. }
  39. Ref<InputEventMouseButton> mb = p_event;
  40. if (mb.is_valid()) {
  41. if (mb->get_button_index() == MouseButton::LEFT) {
  42. if (mb->is_pressed()) {
  43. sc->_compute_middle_sep(true);
  44. dragging = true;
  45. drag_ofs = sc->split_offset;
  46. if (sc->vertical) {
  47. drag_from = get_transform().xform(mb->get_position()).y;
  48. } else {
  49. drag_from = get_transform().xform(mb->get_position()).x;
  50. }
  51. } else {
  52. dragging = false;
  53. queue_redraw();
  54. }
  55. }
  56. }
  57. Ref<InputEventMouseMotion> mm = p_event;
  58. if (mm.is_valid()) {
  59. if (!dragging) {
  60. return;
  61. }
  62. Vector2i in_parent_pos = get_transform().xform(mm->get_position());
  63. if (!sc->vertical && is_layout_rtl()) {
  64. sc->split_offset = drag_ofs - (in_parent_pos.x - drag_from);
  65. } else {
  66. sc->split_offset = drag_ofs + ((sc->vertical ? in_parent_pos.y : in_parent_pos.x) - drag_from);
  67. }
  68. sc->_compute_middle_sep(true);
  69. sc->queue_sort();
  70. sc->emit_signal(SNAME("dragged"), sc->get_split_offset());
  71. }
  72. }
  73. Control::CursorShape SplitContainerDragger::get_cursor_shape(const Point2 &p_pos) const {
  74. SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent());
  75. if (!sc->collapsed && sc->dragger_visibility == SplitContainer::DRAGGER_VISIBLE) {
  76. return (sc->vertical ? CURSOR_VSPLIT : CURSOR_HSPLIT);
  77. }
  78. return Control::get_cursor_shape(p_pos);
  79. }
  80. void SplitContainerDragger::_notification(int p_what) {
  81. switch (p_what) {
  82. case NOTIFICATION_MOUSE_ENTER: {
  83. mouse_inside = true;
  84. SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent());
  85. if (sc->get_theme_constant(SNAME("autohide"))) {
  86. queue_redraw();
  87. }
  88. } break;
  89. case NOTIFICATION_MOUSE_EXIT: {
  90. mouse_inside = false;
  91. SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent());
  92. if (sc->get_theme_constant(SNAME("autohide"))) {
  93. queue_redraw();
  94. }
  95. } break;
  96. case NOTIFICATION_DRAW: {
  97. SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent());
  98. if (!dragging && !mouse_inside && sc->get_theme_constant(SNAME("autohide"))) {
  99. return;
  100. }
  101. Ref<Texture2D> tex = sc->get_theme_icon(SNAME("grabber"));
  102. draw_texture(tex, (get_size() - tex->get_size()) / 2);
  103. } break;
  104. }
  105. }
  106. Control *SplitContainer::_getch(int p_idx) const {
  107. int idx = 0;
  108. for (int i = 0; i < get_child_count(false); i++) {
  109. Control *c = Object::cast_to<Control>(get_child(i, false));
  110. if (!c || !c->is_visible()) {
  111. continue;
  112. }
  113. if (c->is_set_as_top_level()) {
  114. continue;
  115. }
  116. if (idx == p_idx) {
  117. return c;
  118. }
  119. idx++;
  120. }
  121. return nullptr;
  122. }
  123. Ref<Texture2D> SplitContainer::_get_grabber_icon() const {
  124. if (is_fixed) {
  125. return theme_cache.grabber_icon;
  126. } else {
  127. if (vertical) {
  128. return theme_cache.grabber_icon_v;
  129. } else {
  130. return theme_cache.grabber_icon_h;
  131. }
  132. }
  133. }
  134. void SplitContainer::_compute_middle_sep(bool p_clamp) {
  135. Control *first = _getch(0);
  136. Control *second = _getch(1);
  137. // Determine expanded children.
  138. bool first_expanded = (vertical ? first->get_v_size_flags() : first->get_h_size_flags()) & SIZE_EXPAND;
  139. bool second_expanded = (vertical ? second->get_v_size_flags() : second->get_h_size_flags()) & SIZE_EXPAND;
  140. // Compute the minimum size.
  141. int axis = vertical ? 1 : 0;
  142. int size = get_size()[axis];
  143. int ms_first = first->get_combined_minimum_size()[axis];
  144. int ms_second = second->get_combined_minimum_size()[axis];
  145. // Determine the separation between items.
  146. Ref<Texture2D> g = _get_grabber_icon();
  147. int sep = (dragger_visibility != DRAGGER_HIDDEN_COLLAPSED) ? MAX(theme_cache.separation, vertical ? g->get_height() : g->get_width()) : 0;
  148. // Compute the wished separation_point.
  149. int wished_middle_sep = 0;
  150. int split_offset_with_collapse = 0;
  151. if (!collapsed) {
  152. split_offset_with_collapse = split_offset;
  153. }
  154. if (first_expanded && second_expanded) {
  155. float ratio = first->get_stretch_ratio() / (first->get_stretch_ratio() + second->get_stretch_ratio());
  156. wished_middle_sep = size * ratio - sep / 2 + split_offset_with_collapse;
  157. } else if (first_expanded) {
  158. wished_middle_sep = size - sep + split_offset_with_collapse;
  159. } else {
  160. wished_middle_sep = split_offset_with_collapse;
  161. }
  162. // Clamp the middle sep to acceptatble values.
  163. middle_sep = CLAMP(wished_middle_sep, ms_first, size - sep - ms_second);
  164. // Clamp the split_offset if requested.
  165. if (p_clamp) {
  166. split_offset -= wished_middle_sep - middle_sep;
  167. }
  168. }
  169. void SplitContainer::_resort() {
  170. Control *first = _getch(0);
  171. Control *second = _getch(1);
  172. // If we have only one element.
  173. if (!first || !second) {
  174. if (first) {
  175. fit_child_in_rect(first, Rect2(Point2(), get_size()));
  176. } else if (second) {
  177. fit_child_in_rect(second, Rect2(Point2(), get_size()));
  178. }
  179. dragging_area_control->hide();
  180. return;
  181. }
  182. // If we have more that one.
  183. _compute_middle_sep(false);
  184. // Determine the separation between items.
  185. Ref<Texture2D> g = _get_grabber_icon();
  186. int sep = (dragger_visibility != DRAGGER_HIDDEN_COLLAPSED) ? MAX(theme_cache.separation, vertical ? g->get_height() : g->get_width()) : 0;
  187. // Move the children, including the dragger.
  188. if (vertical) {
  189. fit_child_in_rect(first, Rect2(Point2(0, 0), Size2(get_size().width, middle_sep)));
  190. int sofs = middle_sep + sep;
  191. fit_child_in_rect(second, Rect2(Point2(0, sofs), Size2(get_size().width, get_size().height - sofs)));
  192. } else {
  193. if (is_layout_rtl()) {
  194. middle_sep = get_size().width - middle_sep - sep;
  195. fit_child_in_rect(second, Rect2(Point2(0, 0), Size2(middle_sep, get_size().height)));
  196. int sofs = middle_sep + sep;
  197. fit_child_in_rect(first, Rect2(Point2(sofs, 0), Size2(get_size().width - sofs, get_size().height)));
  198. } else {
  199. fit_child_in_rect(first, Rect2(Point2(0, 0), Size2(middle_sep, get_size().height)));
  200. int sofs = middle_sep + sep;
  201. fit_child_in_rect(second, Rect2(Point2(sofs, 0), Size2(get_size().width - sofs, get_size().height)));
  202. }
  203. }
  204. // Handle the dragger visibility and position.
  205. if (dragger_visibility == DRAGGER_VISIBLE && !collapsed) {
  206. dragging_area_control->show();
  207. int dragger_ctrl_size = MAX(sep, theme_cache.minimum_grab_thickness);
  208. if (vertical) {
  209. dragging_area_control->set_rect(Rect2(Point2(0, middle_sep - (dragger_ctrl_size - sep) / 2), Size2(get_size().width, dragger_ctrl_size)));
  210. } else {
  211. dragging_area_control->set_rect(Rect2(Point2(middle_sep - (dragger_ctrl_size - sep) / 2, 0), Size2(dragger_ctrl_size, get_size().height)));
  212. }
  213. dragging_area_control->queue_redraw();
  214. } else {
  215. dragging_area_control->hide();
  216. }
  217. }
  218. Size2 SplitContainer::get_minimum_size() const {
  219. Size2i minimum;
  220. Ref<Texture2D> g = _get_grabber_icon();
  221. int sep = (dragger_visibility != DRAGGER_HIDDEN_COLLAPSED) ? MAX(theme_cache.separation, vertical ? g->get_height() : g->get_width()) : 0;
  222. for (int i = 0; i < 2; i++) {
  223. if (!_getch(i)) {
  224. break;
  225. }
  226. if (i == 1) {
  227. if (vertical) {
  228. minimum.height += sep;
  229. } else {
  230. minimum.width += sep;
  231. }
  232. }
  233. Size2 ms = _getch(i)->get_combined_minimum_size();
  234. if (vertical) {
  235. minimum.height += ms.height;
  236. minimum.width = MAX(minimum.width, ms.width);
  237. } else {
  238. minimum.width += ms.width;
  239. minimum.height = MAX(minimum.height, ms.height);
  240. }
  241. }
  242. return minimum;
  243. }
  244. void SplitContainer::_validate_property(PropertyInfo &p_property) const {
  245. if (is_fixed && p_property.name == "vertical") {
  246. p_property.usage = PROPERTY_USAGE_NONE;
  247. }
  248. }
  249. void SplitContainer::_update_theme_item_cache() {
  250. Container::_update_theme_item_cache();
  251. theme_cache.separation = get_theme_constant(SNAME("separation"));
  252. theme_cache.minimum_grab_thickness = get_theme_constant(SNAME("minimum_grab_thickness"));
  253. theme_cache.autohide = get_theme_constant(SNAME("autohide"));
  254. theme_cache.grabber_icon = get_theme_icon(SNAME("grabber"));
  255. theme_cache.grabber_icon_h = get_theme_icon(SNAME("h_grabber"));
  256. theme_cache.grabber_icon_v = get_theme_icon(SNAME("v_grabber"));
  257. }
  258. void SplitContainer::_notification(int p_what) {
  259. switch (p_what) {
  260. case NOTIFICATION_TRANSLATION_CHANGED:
  261. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  262. queue_sort();
  263. } break;
  264. case NOTIFICATION_SORT_CHILDREN: {
  265. _resort();
  266. } break;
  267. case NOTIFICATION_THEME_CHANGED: {
  268. update_minimum_size();
  269. } break;
  270. }
  271. }
  272. void SplitContainer::set_split_offset(int p_offset) {
  273. if (split_offset == p_offset) {
  274. return;
  275. }
  276. split_offset = p_offset;
  277. queue_sort();
  278. }
  279. int SplitContainer::get_split_offset() const {
  280. return split_offset;
  281. }
  282. void SplitContainer::clamp_split_offset() {
  283. if (!_getch(0) || !_getch(1)) {
  284. return;
  285. }
  286. _compute_middle_sep(true);
  287. queue_sort();
  288. }
  289. void SplitContainer::set_collapsed(bool p_collapsed) {
  290. if (collapsed == p_collapsed) {
  291. return;
  292. }
  293. collapsed = p_collapsed;
  294. queue_sort();
  295. }
  296. void SplitContainer::set_dragger_visibility(DraggerVisibility p_visibility) {
  297. if (dragger_visibility == p_visibility) {
  298. return;
  299. }
  300. dragger_visibility = p_visibility;
  301. queue_sort();
  302. }
  303. SplitContainer::DraggerVisibility SplitContainer::get_dragger_visibility() const {
  304. return dragger_visibility;
  305. }
  306. bool SplitContainer::is_collapsed() const {
  307. return collapsed;
  308. }
  309. void SplitContainer::set_vertical(bool p_vertical) {
  310. ERR_FAIL_COND_MSG(is_fixed, "Can't change orientation of " + get_class() + ".");
  311. vertical = p_vertical;
  312. update_minimum_size();
  313. _resort();
  314. }
  315. bool SplitContainer::is_vertical() const {
  316. return vertical;
  317. }
  318. Vector<int> SplitContainer::get_allowed_size_flags_horizontal() const {
  319. Vector<int> flags;
  320. flags.append(SIZE_FILL);
  321. if (!vertical) {
  322. flags.append(SIZE_EXPAND);
  323. }
  324. flags.append(SIZE_SHRINK_BEGIN);
  325. flags.append(SIZE_SHRINK_CENTER);
  326. flags.append(SIZE_SHRINK_END);
  327. return flags;
  328. }
  329. Vector<int> SplitContainer::get_allowed_size_flags_vertical() const {
  330. Vector<int> flags;
  331. flags.append(SIZE_FILL);
  332. if (vertical) {
  333. flags.append(SIZE_EXPAND);
  334. }
  335. flags.append(SIZE_SHRINK_BEGIN);
  336. flags.append(SIZE_SHRINK_CENTER);
  337. flags.append(SIZE_SHRINK_END);
  338. return flags;
  339. }
  340. void SplitContainer::_bind_methods() {
  341. ClassDB::bind_method(D_METHOD("set_split_offset", "offset"), &SplitContainer::set_split_offset);
  342. ClassDB::bind_method(D_METHOD("get_split_offset"), &SplitContainer::get_split_offset);
  343. ClassDB::bind_method(D_METHOD("clamp_split_offset"), &SplitContainer::clamp_split_offset);
  344. ClassDB::bind_method(D_METHOD("set_collapsed", "collapsed"), &SplitContainer::set_collapsed);
  345. ClassDB::bind_method(D_METHOD("is_collapsed"), &SplitContainer::is_collapsed);
  346. ClassDB::bind_method(D_METHOD("set_dragger_visibility", "mode"), &SplitContainer::set_dragger_visibility);
  347. ClassDB::bind_method(D_METHOD("get_dragger_visibility"), &SplitContainer::get_dragger_visibility);
  348. ClassDB::bind_method(D_METHOD("set_vertical", "vertical"), &SplitContainer::set_vertical);
  349. ClassDB::bind_method(D_METHOD("is_vertical"), &SplitContainer::is_vertical);
  350. ADD_SIGNAL(MethodInfo("dragged", PropertyInfo(Variant::INT, "offset")));
  351. ADD_PROPERTY(PropertyInfo(Variant::INT, "split_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_split_offset", "get_split_offset");
  352. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collapsed"), "set_collapsed", "is_collapsed");
  353. ADD_PROPERTY(PropertyInfo(Variant::INT, "dragger_visibility", PROPERTY_HINT_ENUM, "Visible,Hidden,Hidden and Collapsed"), "set_dragger_visibility", "get_dragger_visibility");
  354. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "vertical"), "set_vertical", "is_vertical");
  355. BIND_ENUM_CONSTANT(DRAGGER_VISIBLE);
  356. BIND_ENUM_CONSTANT(DRAGGER_HIDDEN);
  357. BIND_ENUM_CONSTANT(DRAGGER_HIDDEN_COLLAPSED);
  358. }
  359. SplitContainer::SplitContainer(bool p_vertical) {
  360. vertical = p_vertical;
  361. dragging_area_control = memnew(SplitContainerDragger);
  362. add_child(dragging_area_control, false, Node::INTERNAL_MODE_BACK);
  363. }