Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/core/StateProxy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <godot_cpp/classes/object.hpp>
#include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
#include "StateProxy.hpp"

using namespace godot;

void StateProxy::_bind_methods() {
ClassDB::bind_method(
D_METHOD("register_property", "node", "property_name", "exposed_name"), &StateProxy::register_property);
ClassDB::bind_method(D_METHOD("get", "property", "default_value"), &StateProxy::get);
ClassDB::bind_method(D_METHOD("get_all"), &StateProxy::get_all);
}

void StateProxy::register_property(Object *node, const StringName &property_name, const StringName &exposed_name) {
StringName key = exposed_name.is_empty() ? property_name : exposed_name;
if (!_objects.has(key)) {
_objects[key] = node;
_property_names[key] = property_name;
}
}

Variant StateProxy::get(const StringName &property, const Variant &default_value) const {
if (_objects.has(property)) {
Object *obj = _objects[property];
if (obj) {
Variant ret = obj->get(_property_names[property]);
if (ret.get_type() != Variant::Type::NIL) {
return ret;
}
}
}
return default_value;
}

bool StateProxy::_get(const StringName &property, Variant &r_value) const {
if (_objects.has(property)) {
Object *obj = _objects[property];
if (obj) {
r_value = obj->get(_property_names[property]);
return true;
}
}
return false;
}

Variant StateProxy::_get(const StringName &property) const {
Variant value;
if (_get(property, value)) {
return value;
}
return Variant(); // Return nil if property doesn't exist
}

Dictionary StateProxy::get_all() const {
Dictionary out;
for (const KeyValue<StringName, Object *> &entry: _objects) {
Object *obj = entry.value;
StringName prop_name = _property_names[entry.key];
out[entry.key] = obj->get(prop_name);
}
return out;
}
34 changes: 34 additions & 0 deletions src/core/StateProxy.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// state_proxy.hpp
#ifndef STATE_PROXY_HPP
#define STATE_PROXY_HPP

#include <godot_cpp/classes/object.hpp>
#include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/templates/hash_map.hpp>
#include <godot_cpp/variant/variant.hpp>

namespace godot {

class StateProxy : public Object {
GDCLASS(StateProxy, Object);

private:
HashMap<StringName, Object *> _objects;
HashMap<StringName, StringName> _property_names;

protected:
static void _bind_methods();

public:
StateProxy() = default;
~StateProxy() = default;

void register_property(Object *node, const StringName &property_name, const StringName &exposed_name = "");
Variant get(const StringName &property, const Variant &default_value = Variant()) const;
bool _get(const StringName &property, Variant &r_value) const; // Correct signature
Variant _get(const StringName &property) const; // For Godot property binding
Dictionary get_all() const;
};
} // namespace godot

#endif // STATE_PROXY_HPP
2 changes: 2 additions & 0 deletions src/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "brakes/TrainBrake.hpp"
#include "core/GenericTrainPart.hpp"
#include "core/StateProxy.hpp"
#include "core/TrainController.hpp"
#include "core/TrainPart.hpp"
#include "core/TrainSystem.hpp"
Expand Down Expand Up @@ -38,6 +39,7 @@ void initialize_libmaszyna_module(ModuleInitializationLevel p_level) {
GDREGISTER_CLASS(TrainController);
GDREGISTER_CLASS(TrainSecuritySystem);
GDREGISTER_CLASS(TrainSystem);
GDREGISTER_CLASS(StateProxy);

train_system_singleton = memnew(TrainSystem);
Engine::get_singleton()->register_singleton("TrainSystem", train_system_singleton);
Expand Down