From 5d0c999103c813c0e0254d1a8cd326d4c2de31ff Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 20 Oct 2021 17:57:42 +0200 Subject: [PATCH] Emit changed events properly in sd shapes. --- .../mat_maker_gd/nodes/bases/polygon_base.gd | 55 ++++++++----------- .../mat_maker_gd/nodes/sdf2d/sd_shape_arc.gd | 9 ++- .../mat_maker_gd/nodes/sdf2d/sd_shape_box.gd | 6 +- .../nodes/sdf2d/sd_shape_circle.gd | 4 +- .../nodes/sdf2d/sd_shape_polygon.gd | 3 + .../nodes/sdf2d/sd_shape_rhombus.gd | 6 +- .../widgets/polygon_edit/polygon_view.gd | 1 + 7 files changed, 43 insertions(+), 41 deletions(-) diff --git a/game/addons/mat_maker_gd/nodes/bases/polygon_base.gd b/game/addons/mat_maker_gd/nodes/bases/polygon_base.gd index 98991645..190241a5 100644 --- a/game/addons/mat_maker_gd/nodes/bases/polygon_base.gd +++ b/game/addons/mat_maker_gd/nodes/bases/polygon_base.gd @@ -3,36 +3,35 @@ extends MMNode var points : PoolVector2Array = [Vector2(0.2, 0.2), Vector2(0.7, 0.4), Vector2(0.4, 0.7)] -func to_string() -> String: - var rv = PoolStringArray() - for p in points: - rv.append("("+str(p.x)+","+str(p.y)+")") - return rv.join(",") - func clear() -> void: points.resize(0) - set_dirty(true) + _polygon_changed() func add_point(x : float, y : float, closed : bool = true) -> void: var p : Vector2 = Vector2(x, y) var points_count = points.size() + if points_count < 3: points.append(p) + _polygon_changed() return + var min_length : float = (p-Geometry.get_closest_point_to_segment_2d(p, points[0], points[points_count-1])).length() var insert_point = 0 + for i in points_count-1: var length = (p - Geometry.get_closest_point_to_segment_2d(p, points[i], points[i+1])).length() if length < min_length: min_length = length insert_point = i+1 + if !closed and insert_point == 0 and (points[0]-p).length() > (points[points_count-1]-p).length(): insert_point = points_count points.insert(insert_point, p) - set_dirty(true) + _polygon_changed() func remove_point(index : int) -> bool: var s = points.size() @@ -40,7 +39,8 @@ func remove_point(index : int) -> bool: return false else: points.remove(index) - set_dirty(true) + _polygon_changed() + return true func get_point_count() -> int: @@ -51,32 +51,21 @@ func get_point(i : int) -> Vector2: func set_point(i : int, v : Vector2) -> void: points[i] = v - set_dirty(true) + + _polygon_changed() func set_points(v : PoolVector2Array) -> void: points = v - set_dirty(true) + + _polygon_changed() -func get_shader() -> String: - var elements : PoolStringArray = PoolStringArray() +func _polygon_changed() -> void: + emit_changed() + + +func to_string() -> String: + var rv = PoolStringArray() for p in points: - elements.append("vec2(%.9f, %.9f)" % [p.x, p.y]) - return "{"+elements.join(", ")+"}" - -func serialize() -> Dictionary: - var rv = [] - for p in points: - rv.append({ x=p.x, y=p.y }) - return { type="Polygon", points=rv } - -func deserialize(v) -> void: - clear() - if typeof(v) == TYPE_DICTIONARY and v.has("type") and v.type == "Polygon": - for p in v.points: - points.push_back(Vector2(p.x, p.y)) - elif typeof(v) == TYPE_OBJECT and v.get_script() == get_script(): - clear() - for p in v.points: - points.push_back(Vector2(p.x, p.y)) - else: - print("Cannot deserialize polygon") + rv.append("("+str(p.x)+","+str(p.y)+")") + + return rv.join(",") diff --git a/game/addons/mat_maker_gd/nodes/sdf2d/sd_shape_arc.gd b/game/addons/mat_maker_gd/nodes/sdf2d/sd_shape_arc.gd index f31b6a21..b5436041 100644 --- a/game/addons/mat_maker_gd/nodes/sdf2d/sd_shape_arc.gd +++ b/game/addons/mat_maker_gd/nodes/sdf2d/sd_shape_arc.gd @@ -36,7 +36,8 @@ func get_angle() -> Vector2: func set_angle(val : Vector2) -> void: angle = val - + + emit_changed() output.emit_changed() #radius @@ -45,7 +46,8 @@ func get_radius() -> float: func set_radius(val : float) -> void: radius = val - + + emit_changed() output.emit_changed() #width @@ -54,5 +56,6 @@ func get_width() -> float: func set_width(val : float) -> void: width = val - + + emit_changed() output.emit_changed() diff --git a/game/addons/mat_maker_gd/nodes/sdf2d/sd_shape_box.gd b/game/addons/mat_maker_gd/nodes/sdf2d/sd_shape_box.gd index bb954528..35c866ee 100644 --- a/game/addons/mat_maker_gd/nodes/sdf2d/sd_shape_box.gd +++ b/game/addons/mat_maker_gd/nodes/sdf2d/sd_shape_box.gd @@ -34,7 +34,8 @@ func get_center() -> Vector2: func set_center(val : Vector2) -> void: center = val - + + emit_changed() output.emit_changed() #size @@ -43,5 +44,6 @@ func get_size() -> Vector2: func set_size(val : Vector2) -> void: size = val - + + emit_changed() output.emit_changed() diff --git a/game/addons/mat_maker_gd/nodes/sdf2d/sd_shape_circle.gd b/game/addons/mat_maker_gd/nodes/sdf2d/sd_shape_circle.gd index 63a45413..bc8488a4 100644 --- a/game/addons/mat_maker_gd/nodes/sdf2d/sd_shape_circle.gd +++ b/game/addons/mat_maker_gd/nodes/sdf2d/sd_shape_circle.gd @@ -34,7 +34,8 @@ func get_center() -> Vector2: func set_center(val : Vector2) -> void: center = val - + + emit_changed() output.emit_changed() #radius @@ -44,4 +45,5 @@ func get_radius() -> float: func set_radius(val : float) -> void: radius = val + emit_changed() output.emit_changed() diff --git a/game/addons/mat_maker_gd/nodes/sdf2d/sd_shape_polygon.gd b/game/addons/mat_maker_gd/nodes/sdf2d/sd_shape_polygon.gd index e02444b4..daecf362 100644 --- a/game/addons/mat_maker_gd/nodes/sdf2d/sd_shape_polygon.gd +++ b/game/addons/mat_maker_gd/nodes/sdf2d/sd_shape_polygon.gd @@ -24,3 +24,6 @@ func _register_methods(mm_graph_node) -> void: func get_property_value(uv : Vector2) -> float: return SDF2D.sdPolygon(uv, points) +func _polygon_changed() -> void: + emit_changed() + output.emit_changed() diff --git a/game/addons/mat_maker_gd/nodes/sdf2d/sd_shape_rhombus.gd b/game/addons/mat_maker_gd/nodes/sdf2d/sd_shape_rhombus.gd index 7964092e..07b5ed48 100644 --- a/game/addons/mat_maker_gd/nodes/sdf2d/sd_shape_rhombus.gd +++ b/game/addons/mat_maker_gd/nodes/sdf2d/sd_shape_rhombus.gd @@ -34,7 +34,8 @@ func get_center() -> Vector2: func set_center(val : Vector2) -> void: center = val - + + emit_changed() output.emit_changed() #size @@ -43,5 +44,6 @@ func get_size() -> Vector2: func set_size(val : Vector2) -> void: size = val - + + emit_changed() output.emit_changed() diff --git a/game/addons/mat_maker_gd/widgets/polygon_edit/polygon_view.gd b/game/addons/mat_maker_gd/widgets/polygon_edit/polygon_view.gd index 64d2a0e4..cf64af90 100644 --- a/game/addons/mat_maker_gd/widgets/polygon_edit/polygon_view.gd +++ b/game/addons/mat_maker_gd/widgets/polygon_edit/polygon_view.gd @@ -45,6 +45,7 @@ func _draw(): draw_rect(Rect2(draw_offset, draw_size), axes_color, false) var tp : Vector2 = transform_point(polygon.points[polygon.points.size()-1 if closed else 0]) + for p in polygon.points: var tnp = transform_point(p) draw_line(tp, tnp, curve_color)