forked from godot-extended-libraries/godot-next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_array.gd
88 lines (67 loc) · 2.06 KB
/
resource_array.gd
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
# ResourceArray
# author: xdgamestudios
# license: MIT
# description: A ResourceCollection implementation that manages an Array of Resources.
# One can add multiple instances of any given Resource type.
# deps:
# - ResourceCollection
# - PropertyInfo
tool
extends ResourceCollection
class_name ResourceArray
##### CLASSES #####
##### SIGNALS #####
##### CONSTANTS #####
const COLLECTION_NAME = "[ Array ]"
##### PROPERTIES #####
var _data := []
##### NOTIFICATIONS #####
func _init() -> void:
resource_name = COLLECTION_NAME
func _get(p_property: String):
if p_property.begins_with(DATA_PREFIX):
var index := int(p_property.trim_prefix(DATA_PREFIX + "item_"))
return _data[index] if index < _data.size() else null
return null
func _set(p_property, p_value):
if p_property.begins_with(DATA_PREFIX):
var index := int(p_property.trim_prefix(DATA_PREFIX + "item_"))
if not p_value:
_data.remove(index)
property_list_changed_notify()
else:
var res = _instantiate_script(p_value) if p_value is Script else p_value
_class_type.res = res
if res and _class_type.is_type(_type):
_data[index] = res
property_list_changed_notify()
return true
return false
##### OVERRIDES #####
func _add_element(script) -> void:
_data.append(script.new())
func _refresh_data() -> void:
if _type == null:
clear()
return
var data_cache := _data.duplicate()
for a_resource in data_cache:
if not ClassType.new(a_resource).is_type(_type):
_data.erase(a_resource)
func _export_data_group() -> Array:
var list := ._export_data_group()
list.append(PropertyInfo.new_storage_only("_data").to_dict())
if _data.empty():
list.append(PropertyInfo.new_nil(DATA_PREFIX + EMPTY_ENTRY).to_dict())
for an_index in _data.size():
list.append(PropertyInfo.new_resource("%sitem_%s" % [DATA_PREFIX, an_index], "", PROPERTY_USAGE_EDITOR).to_dict())
return list
##### VIRTUALS #####
##### PUBLIC METHODS #####
func clear() -> void:
_data.clear()
##### PRIVATE METHODS #####
##### CONNECTIONS #####
##### SETTERS AND GETTERS #####
func get_data() -> Array:
return _data