aboutsummaryrefslogtreecommitdiff
path: root/src/addons/rmsmartshape/shapes/shape_meta.gd
blob: 25ad9a94aea0ebc993b8d6a2682ab0bfd18c17dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
tool
extends SS2D_Shape_Base
class_name SS2D_Shape_Meta, "../assets/meta_shape.png"

"""
This shape will set the point_array data of all children shapes
"""

export (bool) var press_to_update_cached_children = false setget _on_update_children
var _cached_shape_children: Array = []


#############
# OVERRIDES #
#############
func _init():
	._init()
	_is_instantiable = true


func _ready():
	for s in _get_shapes(self):
		_add_to_meta(s)
	call_deferred("_update_cached_children")
	._ready()


func _draw():
	pass


func remove_child(node: Node):
	_remove_from_meta(node)
	call_deferred("_update_cached_children")
	.remove_child(node)


func add_child(node: Node, legible_unique_name: bool = false):
	_add_to_meta(node)
	call_deferred("_update_cached_children")
	.add_child(node, legible_unique_name)


func add_child_below_node(node: Node, child_node: Node, legible_unique_name: bool = false):
	_add_to_meta(child_node)
	call_deferred("_update_cached_children")
	.add_child_below_node(node, child_node, legible_unique_name)


func _on_dirty_update():
	pass


func set_as_dirty():
	_update_shapes()

########
# META #
########
func _on_update_children(ignore: bool):
	#print("Updating Cached Children...")
	_update_cached_children()
	#print("...Updated")


func _update_cached_children():
	# TODO, need to be made aware when cached children's children change!
	_cached_shape_children = _get_shapes(self)
	if treat_as_closed():
		can_edit = false
		if editor_debug:
			print ("META Shape contains Closed shapes, edit the meta shape using the child closed shape; DO NOT EDIT META DIRECTLY")
	else:
		can_edit = true
		if editor_debug:
			print ("META Shape contains no Closed shapes, can edit META shape directly")


func _get_shapes(n: Node, a: Array = []) -> Array:
	for c in n.get_children():
		if c is SS2D_Shape_Base:
			a.push_back(c)
		_get_shapes(c, a)
	return a


func _add_to_meta(n: Node):
	if not n is SS2D_Shape_Base:
		return
	# Assign node to have the same point array data as this meta shape
	n.set_point_array(_points, false)
	n.connect("points_modified", self, "_update_shapes", [[n]])


func _update_shapes(except: Array = []):
	_update_curve(_points)
	for s in _cached_shape_children:
		if not except.has(s):
			s.set_as_dirty()
			s._update_curve(s.get_point_array())


func _remove_from_meta(n: Node):
	if not n is SS2D_Shape_Base:
		return
	# Make Point Data Unique
	n.set_point_array(n.get_point_array(), true)
	n.disconnect("points_modified", self, "_update_shapes")

func treat_as_closed()->bool:
	var has_closed = false
	for c in _cached_shape_children:
		if c is SS2D_Shape_Closed:
			has_closed = true
			break
	if has_closed:
		return true
	return false