split_container.cpp 14 KB

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