Emit changed events properly in sd shapes.

This commit is contained in:
Relintai 2021-10-20 17:57:42 +02:00
parent 1e57dba26e
commit 5d0c999103
7 changed files with 43 additions and 41 deletions

View File

@ -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(",")

View File

@ -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()

View File

@ -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()

View File

@ -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()

View File

@ -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()

View File

@ -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()

View File

@ -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)