aboutsummaryrefslogtreecommitdiff
path: root/src/addons/rmsmartshape/shapes/point_array.gd
blob: 34d9c1cbc4f3c2a9f6c80e47136cfeafab70db9b (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
tool
extends Resource
class_name SS2D_Point_Array

enum CONSTRAINT { NONE = 0, AXIS_X = 1, AXIS_Y = 2, CONTROL_POINTS = 4, PROPERTIES = 8, ALL = 15 }

# Maps a key to each point
export var _points: Dictionary = {} setget set_points
# Contains all keys; the order of the keys determines the order of the points
export var _point_order: Array = [] setget set_point_order
# Key is tuple of point_keys; Value is the CONSTRAINT enum
export var _constraints = {} setget set_constraints
# Next key value to generate
export var _next_key = 0 setget set_next_key

var _constraints_enabled: bool = true

signal constraint_removed(key1, key2)

###################
# HANDLING POINTS #
###################


func _init():
	# Required by Godot to correctly make unique instances of this resource
	_points = {}
	_point_order = []
	_constraints = {}
	_next_key = 0


func set_points(ps: Dictionary):
	# Called by Godot when loading from a saved scene
	for k in ps:
		var p = ps[k]
		p.connect("changed", self, "_on_point_changed", [p])
	_points = ps
	property_list_changed_notify()


func set_point_order(po: Array):
	_point_order = po
	property_list_changed_notify()


func set_constraints(cs: Dictionary):
	_constraints = cs
	property_list_changed_notify()


func set_next_key(i: int):
	_next_key = i
	property_list_changed_notify()


func __generate_key(next: int) -> int:
	if not is_key_valid(next):
		return __generate_key(max(next + 1, 0))
	return next


func _generate_key() -> int:
	var next = __generate_key(_next_key)
	_next_key = next + 1
	return next


func get_next_key() -> int:
	"""
	Will return the next key that will be used when adding a point
	"""
	return __generate_key(_next_key)


func is_key_valid(k: int) -> bool:
	if k < 0:
		return false
	if _points.has(k):
		return false
	return true


func add_point(point: Vector2, idx: int = -1, use_key: int = -1) -> int:
	var next_key = use_key
	if next_key == -1 or not is_key_valid(next_key):
		next_key = _generate_key()
	var new_point = SS2D_Point.new(point)
	new_point.connect("changed", self, "_on_point_changed", [new_point])
	_points[next_key] = new_point
	_point_order.push_back(next_key)
	if idx != -1:
		set_point_index(next_key, idx)
	return next_key


func is_index_in_range(idx: int) -> bool:
	return idx > 0 and idx < _point_order.size()


func get_point_key_at_index(idx: int) -> int:
	return _point_order[idx]


func get_point_at_index(idx: int) -> int:
	return _points[_point_order[idx]].duplicate(true)


func get_point(key: int) -> int:
	return _points[key].duplicate(true)


func set_point(key: int, value: SS2D_Point):
	if has_point(key):
		_points[key] = value.duplicate(true)


func get_point_count() -> int:
	return _point_order.size()


func get_point_index(key: int) -> int:
	if has_point(key):
		var idx = 0
		for k in _point_order:
			if key == k:
				break
			idx += 1
		return idx
	return -1


func invert_point_order():
	_point_order.invert()


func set_point_index(key: int, idx: int):
	if not has_point(key):
		return
	var old_idx = get_point_index(key)
	if idx < 0 or idx >= _points.size():
		idx = _points.size() - 1
	if idx == old_idx:
		return
	_point_order.remove(old_idx)
	_point_order.insert(idx, key)


func has_point(key: int) -> bool:
	return _points.has(key)


func get_all_point_keys() -> Array:
	"""
	_point_order should contain every single point ONLY ONCE
	"""
	return _point_order.duplicate(true)


func remove_point(key: int) -> bool:
	if has_point(key):
		remove_constraints(key)
		var p = _points[key]
		if p.is_connected("changed", self, "_on_point_changed"):
			p.disconnect("changed", self, "_on_point_changed")
		_point_order.remove(get_point_index(key))
		_points.erase(key)
		return true
	return false


func clear():
	_points.clear()
	_point_order.clear()
	_constraints.clear()
	_next_key = 0
	emit_signal("changed")


func set_point_in(key: int, value: Vector2):
	if has_point(key):
		_points[key].point_in = value


func get_point_in(key: int) -> Vector2:
	if has_point(key):
		return _points[key].point_in
	return Vector2(0, 0)


func set_point_out(key: int, value: Vector2):
	if has_point(key):
		_points[key].point_out = value


func get_point_out(key: int) -> Vector2:
	if has_point(key):
		return _points[key].point_out
	return Vector2(0, 0)


func set_point_position(key: int, value: Vector2):
	if has_point(key):
		_points[key].position = value


func get_point_position(key: int) -> Vector2:
	if has_point(key):
		return _points[key].position
	return Vector2(0, 0)


func set_point_properties(key: int, value: SS2D_VertexProperties):
	if has_point(key):
		_points[key].properties = value


func get_point_properties(key: int) -> SS2D_VertexProperties:
	if has_point(key):
		return _points[key].properties.duplicate(true)
	var new_props = SS2D_VertexProperties.new()
	return new_props


func get_key_from_point(p: SS2D_Point) -> int:
	for k in _points:
		if p == _points[k]:
			return k
	return -1


func _on_point_changed(p: SS2D_Point):
	var key = get_key_from_point(p)
	if _updating_constraints:
		_keys_to_update_constraints.push_back(key)
	else:
		update_constraints(key)


###############
# CONSTRAINTS #
###############

var _updating_constraints = false
var _keys_to_update_constraints = []


func disable_constraints():
	_constraints_enabled = false


func enable_constraints():
	_constraints_enabled = true


func _update_constraints(src: int):
	if not _constraints_enabled:
		return
	var constraints = get_point_constraints(src)
	for tuple in constraints:
		var constraint = constraints[tuple]
		if constraint == CONSTRAINT.NONE:
			continue
		var dst = get_other_value_from_tuple(tuple, src)
		if constraint & CONSTRAINT.AXIS_X:
			set_point_position(dst, Vector2(get_point_position(src).x, get_point_position(dst).y))
		if constraint & CONSTRAINT.AXIS_Y:
			set_point_position(dst, Vector2(get_point_position(dst).x, get_point_position(src).y))
		if constraint & CONSTRAINT.CONTROL_POINTS:
			set_point_in(dst, get_point_in(src))
			set_point_out(dst, get_point_out(src))
		if constraint & CONSTRAINT.PROPERTIES:
			set_point_properties(dst, get_point_properties(src))


func update_constraints(src: int):
	"""
	Will mutate points based on constraints
	values from Passed key will be used to update constrained points
	"""
	if not has_point(src) or _updating_constraints:
		return
	_updating_constraints = true
	# Initial pass of updating constraints
	_update_constraints(src)

	# Subsequent required passes of updating constraints
	while not _keys_to_update_constraints.empty():
		var key_set = _keys_to_update_constraints.duplicate(true)
		_keys_to_update_constraints.clear()
		for k in key_set:
			_update_constraints(k)

	_updating_constraints = false
	emit_signal("changed")


func get_point_constraints(key1: int) -> Dictionary:
	"""
	Will Return all constraints for a given key
	"""
	var constraints = {}
	for tuple in _constraints:
		if tuple.has(key1):
			constraints[tuple] = _constraints[tuple]
	return constraints


func get_point_constraint(key1: int, key2: int) -> int:
	"""
	Will Return the constraint for a pair of keys
	"""
	var t = create_tuple(key1, key2)
	var keys = _constraints.keys()
	var t_index = find_tuple_in_array_of_tuples(keys, t)
	if t_index == -1:
		return CONSTRAINT.NONE
	var t_key = keys[t_index]
	return _constraints[t_key]


func set_constraint(key1: int, key2: int, constraint: int):
	var t = create_tuple(key1, key2)
	var existing_tuples = _constraints.keys()
	var existing_t_index = find_tuple_in_array_of_tuples(existing_tuples, t)
	if existing_t_index != -1:
		t = existing_tuples[existing_t_index]
	_constraints[t] = constraint
	if _constraints[t] == CONSTRAINT.NONE:
		_constraints.erase(t)
		emit_signal("constraint_removed", key1, key2)
	else:
		update_constraints(key1)


func remove_constraints(key1: int):
	var constraints = get_point_constraints(key1)
	for tuple in constraints:
		var constraint = constraints[tuple]
		var key2 = get_other_value_from_tuple(tuple, key1)
		set_constraint(key1, key2, CONSTRAINT.NONE)


func remove_constraint(key1: int, key2: int):
	set_constraint(key1, key2, CONSTRAINT.NONE)


func get_all_constraints_of_type(type: int) -> int:
	var constraints = []
	for t in _constraints:
		var c = _constraints[t]
		if c == type:
			constraints.push_back(t)
	return constraints


########
# MISC #
########
func debug_print():
	for k in get_all_point_keys():
		var pos = get_point_position(k)
		var _in = get_point_in(k)
		var out = get_point_out(k)
		print("%s = P:%s | I:%s | O:%s" % [k, pos, _in, out])


func duplicate(sub_resource: bool = false):
	var _new = __new()
	_new._next_key = _next_key
	if sub_resource:
		var new_point_dict = {}
		for k in _points:
			new_point_dict[k] = _points[k].duplicate(true)
		_new._points = new_point_dict
		_new._point_order = _point_order.duplicate(true)

		_new._constraints = {}
		for tuple in _constraints:
			_new._constraints[tuple] = _constraints[tuple]
	else:
		_new._points = _points
		_new._point_order = _point_order
		_new._constraints = _constraints
	return _new


# Workaround (class cannot reference itself)
func __new():
	return get_script().new()


#########
# TUPLE #
#########

static func create_tuple(a: int, b: int) -> Array:
	return [a, b]

static func get_other_value_from_tuple(t: Array, value: int) -> int:
	if t[0] == value:
		return t[1]
	elif t[1] == value:
		return t[0]
	return -1

static func tuples_are_equal(t1: Array, t2: Array) -> bool:
	return (t1[0] == t2[0] and t1[1] == t2[1]) or (t1[0] == t2[1] and t1[1] == t2[0])

static func find_tuple_in_array_of_tuples(tuple_array: Array, t: Array) -> int:
	for i in range(tuple_array.size()):
		var other = tuple_array[i]
		if tuples_are_equal(t, other):
			return i
	return -1