|
| 1 | +# Copyright (c) 2026 Liam Sherwin. All rights reserved. |
| 2 | +# This file is part of the Spectrum Lighting Engine, licensed under the GPL v3.0 or later. |
| 3 | +# See the LICENSE file for details. |
| 4 | + |
| 5 | +class_name Data extends Object |
| 6 | +## Class to manage custom data types |
| 7 | + |
| 8 | + |
| 9 | +## Enum for Type |
| 10 | +enum Type { |
| 11 | + NULL, ## Represents no value (null / None) |
| 12 | + STRING, ## A standard text string |
| 13 | + BOOL, ## A true/false boolean value |
| 14 | + INT, ## A 64-bit integer number |
| 15 | + FLOAT, ## A floating-point number |
| 16 | + ARRAY, ## A dynamic array of values |
| 17 | + DICTIONARY, ## A key/value map (hash table) |
| 18 | + VECTOR2, ## A 2D vector (x, y) with floats |
| 19 | + VECTOR2I, ## A 2D vector (x, y) with integers |
| 20 | + RECT2, ## A 2D rectangle defined by position and size (floats) |
| 21 | + RECT2I, ## A 2D rectangle defined by position and size (integers) |
| 22 | + VECTOR3, ## A 3D vector (x, y, z) with floats |
| 23 | + VECTOR3I, ## A 3D vector (x, y, z) with integers |
| 24 | + VECTOR4, ## A 4D vector (x, y, z, w) with floats |
| 25 | + VECTOR4I, ## A 4D vector (x, y, z, w) with integers |
| 26 | + COLOR, ## A color with red, green, blue, and alpha channels |
| 27 | + OBJECT, ## A reference to any Godot Object (Node, Resource, etc.) |
| 28 | + CALLABLE, ## A callable function reference |
| 29 | + SIGNAL, ## A signal reference (connectable event) |
| 30 | + ENUM, ## An enumerator |
| 31 | + BITFLAGS, ## Bit Flags |
| 32 | + NAME, ## A symbolic name or identifier |
| 33 | + IP, ## An IP address |
| 34 | + INPUTEVENT, ## An InputEvent |
| 35 | + SETTINGSMANAGER, ## A SettingsManager |
| 36 | + PACKEDSCENE, ## A PackedScene object |
| 37 | + ACTION, ## An Action that can be triggred |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +## Map custom Type to Godot Variant.Type |
| 42 | +static var custom_type_map: Dictionary[Type, Variant.Type] = { |
| 43 | + Type.NULL: TYPE_NIL, |
| 44 | + Type.STRING: TYPE_STRING, |
| 45 | + Type.BOOL: TYPE_BOOL, |
| 46 | + Type.INT: TYPE_INT, |
| 47 | + Type.FLOAT: TYPE_FLOAT, |
| 48 | + Type.ARRAY: TYPE_ARRAY, |
| 49 | + Type.DICTIONARY: TYPE_DICTIONARY, |
| 50 | + Type.VECTOR2: TYPE_VECTOR2, |
| 51 | + Type.VECTOR2I: TYPE_VECTOR2, |
| 52 | + Type.RECT2: TYPE_RECT2, |
| 53 | + Type.RECT2I: TYPE_RECT2, |
| 54 | + Type.VECTOR3: TYPE_VECTOR3, |
| 55 | + Type.VECTOR3I: TYPE_VECTOR3, |
| 56 | + Type.VECTOR4: TYPE_VECTOR4, |
| 57 | + Type.VECTOR4I: TYPE_VECTOR4, |
| 58 | + Type.COLOR: TYPE_COLOR, |
| 59 | + Type.OBJECT: TYPE_OBJECT, |
| 60 | + Type.CALLABLE: TYPE_CALLABLE, |
| 61 | + Type.SIGNAL: TYPE_SIGNAL, |
| 62 | + Type.ENUM: TYPE_INT, |
| 63 | + Type.BITFLAGS: TYPE_INT, |
| 64 | + Type.NAME: TYPE_STRING, |
| 65 | + Type.IP: TYPE_STRING, |
| 66 | + Type.INPUTEVENT: TYPE_OBJECT, |
| 67 | + Type.SETTINGSMANAGER: TYPE_OBJECT, |
| 68 | + Type.PACKEDSCENE: TYPE_OBJECT, |
| 69 | + Type.ACTION: TYPE_NIL, |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | +## User config |
| 74 | +static var _config: Dictionary[String, Variant] |
| 75 | + |
| 76 | +## User config method to convert a custom type into a string |
| 77 | +static var _custom_type_to_string_method: Callable |
| 78 | + |
| 79 | +## User config method to get a name changed signal from an object |
| 80 | +static var _get_object_name_signal_method: Callable |
| 81 | + |
| 82 | + |
| 83 | +## static init |
| 84 | +static func _static_init() -> void: |
| 85 | + var script: Variant = load("res://DataConfig.gd") |
| 86 | + |
| 87 | + if script is GDScript and script.get("config") is Dictionary: |
| 88 | + _config = script.get("config") |
| 89 | + |
| 90 | + _custom_type_to_string_method = type_convert(_config.get("custom_type_to_string_method"), TYPE_CALLABLE) |
| 91 | + _get_object_name_signal_method = type_convert(_config.get("get_object_name_signal_method"), TYPE_CALLABLE) |
| 92 | + |
| 93 | + |
| 94 | +## Returns true if the 2 given types have a matching Variant.Type base |
| 95 | +static func do_types_match_base(p_type_one: Type, p_type_two: Type) -> bool: |
| 96 | + var type_one_base: Variant.Type = custom_type_map[p_type_one] |
| 97 | + var type_two_base: Variant.Type = custom_type_map[p_type_two] |
| 98 | + |
| 99 | + return type_one_base == type_two_base |
| 100 | + |
| 101 | + |
| 102 | +## Converts a custom data type to a string, with a human readable name |
| 103 | +static func custom_type_to_string(p_variant: String, p_orignal_type: Type) -> String: |
| 104 | + if _custom_type_to_string_method.is_valid(): |
| 105 | + var result: Variant = _custom_type_to_string_method.call(p_variant, p_orignal_type) |
| 106 | + |
| 107 | + if typeof(result) == TYPE_STRING: |
| 108 | + return result |
| 109 | + |
| 110 | + return type_convert(p_variant, TYPE_STRING) |
| 111 | + |
| 112 | + |
| 113 | +## Returns the signal emitted when the name of an object is changed |
| 114 | +static func get_object_name_changed_signal(p_module: SettingsModule) -> Signal: |
| 115 | + if _get_object_name_signal_method.is_valid(): |
| 116 | + var result: Variant = _get_object_name_signal_method.call(p_module) |
| 117 | + |
| 118 | + if typeof(result) == TYPE_SIGNAL: |
| 119 | + return result |
| 120 | + |
| 121 | + var object: Variant = p_module.get_getter().call() |
| 122 | + if typeof(object) != TYPE_OBJECT or not is_instance_valid(object): |
| 123 | + return Signal() |
| 124 | + |
| 125 | + if object is Node: |
| 126 | + return (object as Node).renamed |
| 127 | + |
| 128 | + return Signal() |
0 commit comments