Last active
November 11, 2024 19:50
-
-
Save WolfgangSenff/a0d9da477ef67e9cbf6532fafe9c5745 to your computer and use it in GitHub Desktop.
MVVM in Godot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class_name ArrayResource | |
extends Resource | |
var _values = []: | |
set(value): | |
_values = value | |
emit_changed() | |
func _init(initial_values: Array = []) -> void: | |
if not initial_values.is_empty(): | |
_values = initial_values | |
func add_value(value) -> void: | |
_values.push_back(value) | |
emit_changed() | |
func insert(pos, value) -> void: | |
_values.insert(position, value) | |
emit_changed() | |
func remove_value(value) -> void: | |
_values.erase(value) | |
emit_changed() | |
func remove_at(pos) -> void: | |
_values.remove_at(pos) | |
emit_changed() | |
func append_values(values) -> void: | |
_values.append_array(values) | |
emit_changed() | |
func push_back(value) -> void: | |
_values.push_back(value) | |
emit_changed() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class_name BoundContainer | |
extends BoxContainer # Can update this to whatever you want; this allows both vertical and horizontal containment | |
@export_file("*.tscn") var ItemTemplate | |
var items: ArrayResource: | |
set(value): | |
if null != value and null != ItemTemplate: | |
items = value | |
items.changed.connect(_on_items_changed, CONNECT_REFERENCE_COUNTED) | |
_load_items() | |
func _load_items() -> void: | |
if nulll == ItemTemplate: | |
return | |
for item in items._values: | |
var item_view = load(ItemTemplate).instantiate() | |
add_child(item_view) | |
item_view.set_deferred("data", item) | |
# The ugliest way to handle this, but probably the most efficient coding-wise | |
func _on_items_changed() -> void: | |
for child in get_children(): | |
child.free() | |
_load_items() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class_name BoundLabel | |
extends Label | |
func bind_property(property: PropertyResource) -> void: | |
property.changed.connect(func(): text = str(property.Value)) | |
text = str(property.Value) | |
# I will create more Bound-* controls in the future and release this as a full library. For the time being, I'm teasing out the full design of my game that uses this still, and prefer to only build controls and libraries out of necessity, rather than a misguided belief that anyone else gives a damn about my libraries. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class_name PropertyResource | |
extends Resource | |
var Value: Variant: | |
set(value): | |
Value = value | |
emit_changed() | |
func _init(value) -> void: | |
Value = value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment