2
0

graph_frame.cpp 13 KB

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