diff --git a/scripts/properties/entity.gd b/scripts/properties/entity.gd new file mode 100644 index 0000000..131d0c8 --- /dev/null +++ b/scripts/properties/entity.gd @@ -0,0 +1,81 @@ +extends Reference + +class_name GDB_Entity + +var _properties := [].duplicate() +var _property_ids := [].duplicate() + +################ +# public stuff # +################ +func get_properties() -> Array: + return _properties + +func get_property_ids() -> Array: + return _property_ids + +func get_property(index : int) -> Object: + if index < 0 || index >= _properties.size(): + return null + return _properties[index] + +func get_property_id(index : int) -> String: + if index < 0 || index >= _property_ids.size(): + return "" + return _property_ids[index] + +func get_property_by_id(id : String) -> Object: + return get_property(find_property_by_id(id)) + +func add_property(id : String, property : Object, index := -1) -> void: + if !GDB_Property.is_valid_property(property): + printerr("GDB_Entity: attempted to add invalid property.") + return + + if find_property(property) >= 0: + return + + if find_property_by_id(id) >= 0: + printerr("GDB_Entity: duplicate property id") + return + + if index < 0: + index = _properties.size() + index = min(index, _properties.size()) + _properties.insert(index, property) + _property_ids.insert(index, id) + emit_signal("property_added", index) + +func remove_property(property : Object) -> bool: + var index := find_property(property) + if index < 0: + return false + + _properties.remove(index) + _property_ids.remove(index) + emit_signal("property_removed", index, property) + return true + +func remove_property_at(index : int) -> bool: + if index < 0 || index >= _properties.size(): + return false + return remove_property(_properties[index]) + +func find_property(property : Object) -> int: + return _properties.find(property) + +func find_property_by_id(id : String) -> int: + return _property_ids.find(id) + +################ +# static stuff # +################ +static func is_valid_entity(object : Object) -> bool: + return object != null \ + && object.has_method("get_properties") + +########### +# signals # +########### +signal property_added(index) +signal property_removed(index, property) diff --git a/scripts/properties/property.gd b/scripts/properties/property.gd new file mode 100644 index 0000000..9b7f515 --- /dev/null +++ b/scripts/properties/property.gd @@ -0,0 +1,92 @@ +extends Reference + +class_name GDB_Property + +# special types +const TYPE_HEADER = -1 + +var _name := "" +var _icon : Texture = null +var _value = null +var _type := TYPE_INT + +################ +# overridables # +################ +func get_name() -> String: + return _name + +func set_name(name : String) -> void: + if _name != name: + _name = name + emit_signal("name_changed") + +func get_icon() -> Texture: + return _icon + +func set_icon(icon : Texture) -> void: + if _icon != icon: + _icon = icon + emit_signal("icon_changed") + +func get_value(): + return _value + +func set_value(value) -> void: + if value != _value: + _value = value + emit_signal("value_changed") + +func get_type() -> int: + return _type + +func set_type(type : int) -> void: + if type != _type: + _type = type + emit_signal("type_changed") + +################ +# static stuff # +################ +static func is_valid_property(object : Object) -> bool: + return object != null \ + && object.has_method("get_name") \ + && object.has_method("get_value") \ + && object.has_method("get_type") + +static func get_prop_icon(property : Object) -> Texture: + var icon : Texture = null + if property.has_method("get_icon"): + icon = property.get_icon() as Texture + else: + icon = property.get("icon") as Texture + return icon + +static func is_prop_value_valid(property : Object, value) -> bool: + if property.has_method("is_value_valud"): + return property.is_value_valid(value) + + match property.get_type(): + TYPE_HEADER: + return false + TYPE_INT: + return value is int + TYPE_REAL: + return value is float + TYPE_STRING: + return value is String + _: # TODO: more default checks + return true + +static func get_prop_value_as_string(property : Object) -> String: + if property.has_method("get_value_as_string"): + return property.get_value_as_string() + return str(property.get_value()) + +########### +# signals # +########### +signal name_changed() +signal icon_changed() +signal value_changed() +signal type_changed()