Просмотр исходного кода

Add different grid patterns to GraphEdit

Hendrik Brucker 1 год назад
Родитель
Сommit
1a8e676131
3 измененных файлов с 105 добавлено и 34 удалено
  1. 11 2
      doc/classes/GraphEdit.xml
  2. 82 32
      scene/gui/graph_edit.cpp
  3. 12 0
      scene/gui/graph_edit.h

+ 11 - 2
doc/classes/GraphEdit.xml

@@ -237,6 +237,9 @@
 			The thickness of the lines between the nodes.
 		</member>
 		<member name="focus_mode" type="int" setter="set_focus_mode" getter="get_focus_mode" overrides="Control" enum="Control.FocusMode" default="2" />
+		<member name="grid_pattern" type="int" setter="set_grid_pattern" getter="get_grid_pattern" enum="GraphEdit.GridPattern" default="0">
+			The pattern used for drawing the grid.
+		</member>
 		<member name="minimap_enabled" type="bool" setter="set_minimap_enabled" getter="is_minimap_enabled" default="true">
 			If [code]true[/code], the minimap is visible.
 		</member>
@@ -405,16 +408,22 @@
 		<constant name="SCROLL_PANS" value="1" enum="PanningScheme">
 			[kbd]Mouse Wheel[/kbd] will move the view, [kbd]Ctrl + Mouse Wheel[/kbd] will zoom.
 		</constant>
+		<constant name="GRID_PATTERN_LINES" value="0" enum="GridPattern">
+			Draw the grid using solid lines.
+		</constant>
+		<constant name="GRID_PATTERN_DOTS" value="1" enum="GridPattern">
+			Draw the grid using dots.
+		</constant>
 	</constants>
 	<theme_items>
 		<theme_item name="activity" data_type="color" type="Color" default="Color(1, 1, 1, 1)">
 			Color of the connection's activity (see [method set_connection_activity]).
 		</theme_item>
 		<theme_item name="grid_major" data_type="color" type="Color" default="Color(1, 1, 1, 0.2)">
-			Color of major grid lines.
+			Color of major grid lines/dots.
 		</theme_item>
 		<theme_item name="grid_minor" data_type="color" type="Color" default="Color(1, 1, 1, 0.05)">
-			Color of minor grid lines.
+			Color of minor grid lines/dots.
 		</theme_item>
 		<theme_item name="selection_fill" data_type="color" type="Color" default="Color(1, 1, 1, 0.3)">
 			The fill color of the selection rectangle.

+ 82 - 32
scene/gui/graph_edit.cpp

@@ -542,40 +542,9 @@ void GraphEdit::_notification(int p_what) {
 
 			// Draw background grid.
 			if (show_grid) {
-				Vector2 offset = get_scroll_offset() / zoom;
-				Size2 size = get_size() / zoom;
-
-				Point2i from_pos = (offset / float(snapping_distance)).floor();
-				Point2i len = (size / float(snapping_distance)).floor() + Vector2(1, 1);
-
-				for (int i = from_pos.x; i < from_pos.x + len.x; i++) {
-					Color color;
-
-					if (ABS(i) % GRID_MINOR_STEPS_PER_MAJOR_LINE == 0) {
-						color = theme_cache.grid_major;
-					} else {
-						color = theme_cache.grid_minor;
-					}
-
-					float base_offset = i * snapping_distance * zoom - offset.x * zoom;
-					draw_line(Vector2(base_offset, 0), Vector2(base_offset, get_size().height), color);
-				}
-
-				for (int i = from_pos.y; i < from_pos.y + len.y; i++) {
-					Color color;
-
-					if (ABS(i) % GRID_MINOR_STEPS_PER_MAJOR_LINE == 0) {
-						color = theme_cache.grid_major;
-					} else {
-						color = theme_cache.grid_minor;
-					}
-
-					float base_offset = i * snapping_distance * zoom - offset.y * zoom;
-					draw_line(Vector2(0, base_offset), Vector2(get_size().width, base_offset), color);
-				}
+				_draw_grid();
 			}
 		} break;
-
 		case NOTIFICATION_RESIZED: {
 			_update_scroll();
 			top_layer->queue_redraw();
@@ -1128,6 +1097,67 @@ void GraphEdit::_minimap_draw() {
 	minimap->draw_texture(resizer, Point2(), resizer_color);
 }
 
+void GraphEdit::_draw_grid() {
+	Vector2 offset = get_scroll_offset() / zoom;
+	Size2 size = get_size() / zoom;
+
+	Point2i from_pos = (offset / float(snapping_distance)).floor();
+	Point2i len = (size / float(snapping_distance)).floor() + Vector2(1, 1);
+
+	switch (grid_pattern) {
+		case GRID_PATTERN_LINES: {
+			for (int i = from_pos.x; i < from_pos.x + len.x; i++) {
+				Color color;
+
+				if (ABS(i) % GRID_MINOR_STEPS_PER_MAJOR_LINE == 0) {
+					color = theme_cache.grid_major;
+				} else {
+					color = theme_cache.grid_minor;
+				}
+
+				float base_offset = i * snapping_distance * zoom - offset.x * zoom;
+				draw_line(Vector2(base_offset, 0), Vector2(base_offset, get_size().height), color);
+			}
+
+			for (int i = from_pos.y; i < from_pos.y + len.y; i++) {
+				Color color;
+
+				if (ABS(i) % GRID_MINOR_STEPS_PER_MAJOR_LINE == 0) {
+					color = theme_cache.grid_major;
+				} else {
+					color = theme_cache.grid_minor;
+				}
+
+				float base_offset = i * snapping_distance * zoom - offset.y * zoom;
+				draw_line(Vector2(0, base_offset), Vector2(get_size().width, base_offset), color);
+			}
+		} break;
+		case GRID_PATTERN_DOTS: {
+			Color transparent_grid_minor = theme_cache.grid_minor;
+			transparent_grid_minor.a *= CLAMP(2 * (zoom - 0.4), 0, 1);
+
+			for (int i = from_pos.x; i < from_pos.x + len.x; i++) {
+				for (int j = from_pos.y; j < from_pos.y + len.y; j++) {
+					Color color = transparent_grid_minor;
+
+					if (ABS(i) % GRID_MINOR_STEPS_PER_MAJOR_LINE == 0 && ABS(j) % GRID_MINOR_STEPS_PER_MAJOR_LINE == 0) {
+						color = theme_cache.grid_major;
+					}
+
+					if (color.a == 0) {
+						continue;
+					}
+
+					float base_offset_x = i * snapping_distance * zoom - offset.x * zoom;
+					float base_offset_y = j * snapping_distance * zoom - offset.y * zoom;
+
+					draw_rect(Rect2(base_offset_x - 1, base_offset_y - 1, 3, 3), color);
+				}
+			}
+		} break;
+	}
+}
+
 void GraphEdit::set_selected(Node *p_child) {
 	for (int i = get_child_count() - 1; i >= 0; i--) {
 		GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i));
@@ -1647,6 +1677,19 @@ bool GraphEdit::is_showing_grid() const {
 	return show_grid;
 }
 
+void GraphEdit::set_grid_pattern(GridPattern p_pattern) {
+	if (grid_pattern == p_pattern) {
+		return;
+	}
+
+	grid_pattern = p_pattern;
+	queue_redraw();
+}
+
+GraphEdit::GridPattern GraphEdit::get_grid_pattern() const {
+	return grid_pattern;
+}
+
 void GraphEdit::_snapping_toggled() {
 	snapping_enabled = toggle_snapping_button->is_pressed();
 }
@@ -1859,6 +1902,9 @@ void GraphEdit::_bind_methods() {
 	ClassDB::bind_method(D_METHOD("set_show_grid", "enable"), &GraphEdit::set_show_grid);
 	ClassDB::bind_method(D_METHOD("is_showing_grid"), &GraphEdit::is_showing_grid);
 
+	ClassDB::bind_method(D_METHOD("set_grid_pattern", "pattern"), &GraphEdit::set_grid_pattern);
+	ClassDB::bind_method(D_METHOD("get_grid_pattern"), &GraphEdit::get_grid_pattern);
+
 	ClassDB::bind_method(D_METHOD("set_snapping_enabled", "enable"), &GraphEdit::set_snapping_enabled);
 	ClassDB::bind_method(D_METHOD("is_snapping_enabled"), &GraphEdit::is_snapping_enabled);
 
@@ -1917,6 +1963,7 @@ void GraphEdit::_bind_methods() {
 
 	ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_scroll_offset", "get_scroll_offset");
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_grid"), "set_show_grid", "is_showing_grid");
+	ADD_PROPERTY(PropertyInfo(Variant::INT, "grid_pattern", PROPERTY_HINT_ENUM, "Lines,Dots"), "set_grid_pattern", "get_grid_pattern");
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "snapping_enabled"), "set_snapping_enabled", "is_snapping_enabled");
 	ADD_PROPERTY(PropertyInfo(Variant::INT, "snapping_distance", PROPERTY_HINT_NONE, "suffix:px"), "set_snapping_distance", "get_snapping_distance");
 	ADD_PROPERTY(PropertyInfo(Variant::INT, "panning_scheme", PROPERTY_HINT_ENUM, "Scroll Zooms,Scroll Pans"), "set_panning_scheme", "get_panning_scheme");
@@ -1970,6 +2017,9 @@ void GraphEdit::_bind_methods() {
 	BIND_ENUM_CONSTANT(SCROLL_ZOOMS);
 	BIND_ENUM_CONSTANT(SCROLL_PANS);
 
+	BIND_ENUM_CONSTANT(GRID_PATTERN_LINES);
+	BIND_ENUM_CONSTANT(GRID_PATTERN_DOTS);
+
 	BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphEdit, panel);
 	BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, grid_major);
 	BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, grid_minor);

+ 12 - 0
scene/gui/graph_edit.h

@@ -126,6 +126,11 @@ public:
 		SCROLL_PANS,
 	};
 
+	enum GridPattern {
+		GRID_PATTERN_LINES,
+		GRID_PATTERN_DOTS
+	};
+
 private:
 	struct ConnectionType {
 		union {
@@ -176,6 +181,7 @@ private:
 	bool snapping_enabled = true;
 	int snapping_distance = 20;
 	bool show_grid = true;
+	GridPattern grid_pattern = GRID_PATTERN_LINES;
 
 	bool connecting = false;
 	String connecting_from;
@@ -289,6 +295,8 @@ private:
 	void _connections_layer_draw();
 	void _minimap_draw();
 
+	void _draw_grid();
+
 	TypedArray<Dictionary> _get_connection_list() const;
 
 	friend class GraphEditFilter;
@@ -412,6 +420,9 @@ public:
 	void set_show_grid(bool p_enable);
 	bool is_showing_grid() const;
 
+	void set_grid_pattern(GridPattern p_pattern);
+	GridPattern get_grid_pattern() const;
+
 	void set_connection_lines_curvature(float p_curvature);
 	float get_connection_lines_curvature() const;
 
@@ -431,5 +442,6 @@ public:
 };
 
 VARIANT_ENUM_CAST(GraphEdit::PanningScheme);
+VARIANT_ENUM_CAST(GraphEdit::GridPattern);
 
 #endif // GRAPH_EDIT_H