graph_frame.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /**************************************************************************/
  2. /* graph_frame.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 "graph_frame.h"
  31. #include "core/string/translation.h"
  32. #include "scene/gui/box_container.h"
  33. #include "scene/gui/label.h"
  34. #include "scene/resources/style_box_flat.h"
  35. #include "scene/resources/style_box_texture.h"
  36. #include "scene/theme/theme_db.h"
  37. void GraphFrame::gui_input(const Ref<InputEvent> &p_ev) {
  38. ERR_FAIL_COND(p_ev.is_null());
  39. Ref<InputEventMouseButton> mb = p_ev;
  40. if (mb.is_valid()) {
  41. ERR_FAIL_NULL_MSG(get_parent_control(), "GraphFrame must be the child of a GraphEdit node.");
  42. if (mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  43. Vector2 mpos = mb->get_position();
  44. Ref<Texture2D> resizer = theme_cache.resizer;
  45. if (resizable && mpos.x > get_size().x - resizer->get_width() && mpos.y > get_size().y - resizer->get_height()) {
  46. resizing = true;
  47. resizing_from = mpos;
  48. resizing_from_size = get_size();
  49. accept_event();
  50. return;
  51. }
  52. emit_signal(SNAME("raise_request"));
  53. }
  54. if (!mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  55. if (resizing) {
  56. resizing = false;
  57. emit_signal(SNAME("resize_end"), get_size());
  58. return;
  59. }
  60. }
  61. }
  62. Ref<InputEventMouseMotion> mm = p_ev;
  63. // Only resize if the frame is not auto-resizing based on linked nodes.
  64. if (resizing && !autoshrink_enabled && mm.is_valid()) {
  65. Vector2 mpos = mm->get_position();
  66. Vector2 diff = mpos - resizing_from;
  67. emit_signal(SNAME("resize_request"), resizing_from_size + diff);
  68. }
  69. }
  70. Control::CursorShape GraphFrame::get_cursor_shape(const Point2 &p_pos) const {
  71. if (resizable && !autoshrink_enabled) {
  72. if (resizing || (p_pos.x > get_size().x - theme_cache.resizer->get_width() && p_pos.y > get_size().y - theme_cache.resizer->get_height())) {
  73. return CURSOR_FDIAGSIZE;
  74. }
  75. }
  76. return Control::get_cursor_shape(p_pos);
  77. }
  78. void GraphFrame::_notification(int p_what) {
  79. switch (p_what) {
  80. case NOTIFICATION_DRAW: {
  81. // Used for layout calculations.
  82. Ref<StyleBox> sb_panel = theme_cache.panel;
  83. Ref<StyleBox> sb_titlebar = theme_cache.titlebar;
  84. // Used for drawing.
  85. Ref<StyleBox> sb_to_draw_panel = selected ? theme_cache.panel_selected : sb_panel;
  86. Ref<StyleBox> sb_to_draw_titlebar = selected ? theme_cache.titlebar_selected : sb_titlebar;
  87. Ref<StyleBoxFlat> sb_panel_flat = sb_to_draw_panel;
  88. Ref<StyleBoxTexture> sb_panel_texture = sb_to_draw_panel;
  89. Rect2 titlebar_rect(Point2(), titlebar_hbox->get_size() + sb_titlebar->get_minimum_size());
  90. Size2 body_size = get_size();
  91. body_size.y -= titlebar_rect.size.height;
  92. Rect2 body_rect(Point2(0, titlebar_rect.size.height), body_size);
  93. // Draw body stylebox.
  94. if (tint_color_enabled) {
  95. if (sb_panel_flat.is_valid()) {
  96. Color original_border_color = sb_panel_flat->get_border_color();
  97. sb_panel_flat = sb_panel_flat->duplicate();
  98. sb_panel_flat->set_bg_color(tint_color);
  99. sb_panel_flat->set_border_color(selected ? original_border_color : tint_color.lightened(0.3));
  100. draw_style_box(sb_panel_flat, body_rect);
  101. } else if (sb_panel_texture.is_valid()) {
  102. sb_panel_texture = sb_panel_texture->duplicate();
  103. sb_panel_texture->set_modulate(tint_color);
  104. draw_style_box(sb_panel_texture, body_rect);
  105. }
  106. } else {
  107. draw_style_box(sb_panel_flat, body_rect);
  108. }
  109. // Draw title bar stylebox above.
  110. draw_style_box(sb_to_draw_titlebar, titlebar_rect);
  111. // Only draw the resize handle if the frame is not auto-resizing.
  112. if (resizable && !autoshrink_enabled) {
  113. Ref<Texture2D> resizer = theme_cache.resizer;
  114. Color resizer_color = theme_cache.resizer_color;
  115. if (resizable) {
  116. draw_texture(resizer, get_size() - resizer->get_size(), resizer_color);
  117. }
  118. }
  119. } break;
  120. }
  121. }
  122. void GraphFrame::_resort() {
  123. Ref<StyleBox> sb_panel = theme_cache.panel;
  124. Ref<StyleBox> sb_titlebar = theme_cache.titlebar;
  125. // Resort titlebar first.
  126. Size2 titlebar_size = Size2(get_size().width, titlebar_hbox->get_size().height);
  127. titlebar_size -= sb_titlebar->get_minimum_size();
  128. Rect2 titlebar_rect = Rect2(sb_titlebar->get_offset(), titlebar_size);
  129. fit_child_in_rect(titlebar_hbox, titlebar_rect);
  130. // After resort the children of the titlebar container may have changed their height (e.g. Label autowrap).
  131. Size2i titlebar_min_size = titlebar_hbox->get_combined_minimum_size();
  132. Size2 size = get_size() - sb_panel->get_minimum_size() - Size2(0, titlebar_min_size.height + sb_titlebar->get_minimum_size().height);
  133. Point2 offset = Point2(sb_panel->get_margin(SIDE_LEFT), sb_panel->get_margin(SIDE_TOP) + titlebar_min_size.height + sb_titlebar->get_minimum_size().height);
  134. for (int i = 0; i < get_child_count(false); i++) {
  135. Control *child = as_sortable_control(get_child(i, false));
  136. if (!child) {
  137. continue;
  138. }
  139. fit_child_in_rect(child, Rect2(offset, size));
  140. }
  141. }
  142. void GraphFrame::_bind_methods() {
  143. ClassDB::bind_method(D_METHOD("set_title", "title"), &GraphFrame::set_title);
  144. ClassDB::bind_method(D_METHOD("get_title"), &GraphFrame::get_title);
  145. ClassDB::bind_method(D_METHOD("get_titlebar_hbox"), &GraphFrame::get_titlebar_hbox);
  146. ClassDB::bind_method(D_METHOD("set_autoshrink_enabled", "shrink"), &GraphFrame::set_autoshrink_enabled);
  147. ClassDB::bind_method(D_METHOD("is_autoshrink_enabled"), &GraphFrame::is_autoshrink_enabled);
  148. ClassDB::bind_method(D_METHOD("set_autoshrink_margin", "autoshrink_margin"), &GraphFrame::set_autoshrink_margin);
  149. ClassDB::bind_method(D_METHOD("get_autoshrink_margin"), &GraphFrame::get_autoshrink_margin);
  150. ClassDB::bind_method(D_METHOD("set_drag_margin", "drag_margin"), &GraphFrame::set_drag_margin);
  151. ClassDB::bind_method(D_METHOD("get_drag_margin"), &GraphFrame::get_drag_margin);
  152. ClassDB::bind_method(D_METHOD("set_tint_color_enabled", "enable"), &GraphFrame::set_tint_color_enabled);
  153. ClassDB::bind_method(D_METHOD("is_tint_color_enabled"), &GraphFrame::is_tint_color_enabled);
  154. ClassDB::bind_method(D_METHOD("set_tint_color", "color"), &GraphFrame::set_tint_color);
  155. ClassDB::bind_method(D_METHOD("get_tint_color"), &GraphFrame::get_tint_color);
  156. ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
  157. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoshrink_enabled"), "set_autoshrink_enabled", "is_autoshrink_enabled");
  158. ADD_PROPERTY(PropertyInfo(Variant::INT, "autoshrink_margin", PROPERTY_HINT_RANGE, "0,128,1"), "set_autoshrink_margin", "get_autoshrink_margin");
  159. ADD_PROPERTY(PropertyInfo(Variant::INT, "drag_margin", PROPERTY_HINT_RANGE, "0,128,1"), "set_drag_margin", "get_drag_margin");
  160. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tint_color_enabled"), "set_tint_color_enabled", "is_tint_color_enabled");
  161. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "tint_color"), "set_tint_color", "get_tint_color");
  162. ADD_SIGNAL(MethodInfo(SNAME("autoshrink_changed")));
  163. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphFrame, panel);
  164. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphFrame, panel_selected);
  165. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphFrame, titlebar);
  166. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphFrame, titlebar_selected);
  167. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphFrame, resizer);
  168. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphFrame, resizer_color);
  169. }
  170. void GraphFrame::_validate_property(PropertyInfo &p_property) const {
  171. if (p_property.name == "resizable") {
  172. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  173. }
  174. }
  175. void GraphFrame::set_title(const String &p_title) {
  176. if (title == p_title) {
  177. return;
  178. }
  179. title = p_title;
  180. if (title_label) {
  181. title_label->set_text(title);
  182. }
  183. update_minimum_size();
  184. }
  185. String GraphFrame::get_title() const {
  186. return title;
  187. }
  188. void GraphFrame::set_autoshrink_enabled(bool p_shrink) {
  189. if (autoshrink_enabled == p_shrink) {
  190. return;
  191. }
  192. autoshrink_enabled = p_shrink;
  193. emit_signal("autoshrink_changed", get_size());
  194. queue_redraw();
  195. }
  196. bool GraphFrame::is_autoshrink_enabled() const {
  197. return autoshrink_enabled;
  198. }
  199. void GraphFrame::set_autoshrink_margin(const int &p_margin) {
  200. if (autoshrink_margin == p_margin) {
  201. return;
  202. }
  203. autoshrink_margin = p_margin;
  204. emit_signal("autoshrink_changed", get_size());
  205. }
  206. int GraphFrame::get_autoshrink_margin() const {
  207. return autoshrink_margin;
  208. }
  209. HBoxContainer *GraphFrame::get_titlebar_hbox() {
  210. return titlebar_hbox;
  211. }
  212. Size2 GraphFrame::get_titlebar_size() const {
  213. return titlebar_hbox->get_size() + theme_cache.titlebar->get_minimum_size();
  214. }
  215. void GraphFrame::set_drag_margin(int p_margin) {
  216. drag_margin = p_margin;
  217. }
  218. int GraphFrame::get_drag_margin() const {
  219. return drag_margin;
  220. }
  221. void GraphFrame::set_tint_color_enabled(bool p_enable) {
  222. tint_color_enabled = p_enable;
  223. queue_redraw();
  224. }
  225. bool GraphFrame::is_tint_color_enabled() const {
  226. return tint_color_enabled;
  227. }
  228. void GraphFrame::set_tint_color(const Color &p_color) {
  229. tint_color = p_color;
  230. queue_redraw();
  231. }
  232. Color GraphFrame::get_tint_color() const {
  233. return tint_color;
  234. }
  235. bool GraphFrame::has_point(const Point2 &p_point) const {
  236. Ref<StyleBox> sb_panel = theme_cache.panel;
  237. Ref<StyleBox> sb_titlebar = theme_cache.titlebar;
  238. Ref<Texture2D> resizer = theme_cache.resizer;
  239. if (Rect2(get_size() - resizer->get_size(), resizer->get_size()).has_point(p_point)) {
  240. return true;
  241. }
  242. // For grabbing on the titlebar.
  243. int titlebar_height = titlebar_hbox->get_size().height + sb_titlebar->get_minimum_size().height;
  244. if (Rect2(0, 0, get_size().width, titlebar_height).has_point(p_point)) {
  245. return true;
  246. }
  247. // Allow grabbing on all sides of the frame.
  248. Rect2 frame_rect = Rect2(0, 0, get_size().width, get_size().height);
  249. Rect2 no_drag_rect = frame_rect.grow(-drag_margin);
  250. if (frame_rect.has_point(p_point) && !no_drag_rect.has_point(p_point)) {
  251. return true;
  252. }
  253. return false;
  254. }
  255. Size2 GraphFrame::get_minimum_size() const {
  256. Ref<StyleBox> sb_panel = theme_cache.panel;
  257. Ref<StyleBox> sb_titlebar = theme_cache.titlebar;
  258. Size2 minsize = titlebar_hbox->get_minimum_size() + sb_titlebar->get_minimum_size();
  259. for (int i = 0; i < get_child_count(false); i++) {
  260. Control *child = as_sortable_control(get_child(i, false));
  261. if (!child) {
  262. continue;
  263. }
  264. Size2i size = child->get_combined_minimum_size();
  265. size.width += sb_panel->get_minimum_size().width;
  266. minsize.x = MAX(minsize.x, size.x);
  267. minsize.y += MAX(minsize.y, size.y);
  268. }
  269. minsize.height += sb_panel->get_minimum_size().height;
  270. return minsize;
  271. }
  272. GraphFrame::GraphFrame() {
  273. titlebar_hbox = memnew(HBoxContainer);
  274. titlebar_hbox->set_h_size_flags(SIZE_EXPAND_FILL);
  275. add_child(titlebar_hbox, false, INTERNAL_MODE_FRONT);
  276. title_label = memnew(Label);
  277. title_label->set_theme_type_variation("GraphFrameTitleLabel");
  278. title_label->set_h_size_flags(SIZE_EXPAND_FILL);
  279. title_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  280. titlebar_hbox->add_child(title_label);
  281. set_mouse_filter(MOUSE_FILTER_STOP);
  282. }