From 0bce71e5b7133922d469a576c78507b9ee64a8bb Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 31 Jul 2020 01:05:52 +0200 Subject: [PATCH] Implement InputProfile. --- profiles/class_profile.cpp | 9 +++ profiles/input/input_profile.cpp | 95 ++++++++++++++++++++++++++++++++ profiles/input/input_profile.h | 26 ++++++++- singletons/profile_manager.cpp | 9 +++ singletons/profile_manager.h | 2 + 5 files changed, 138 insertions(+), 3 deletions(-) diff --git a/profiles/class_profile.cpp b/profiles/class_profile.cpp index c37f6b1..1862725 100644 --- a/profiles/class_profile.cpp +++ b/profiles/class_profile.cpp @@ -116,7 +116,9 @@ Dictionary ClassProfile::to_dict() const { dict["level"] = _level; dict["xp"] = _xp; dict["actionbar_locked"] = _actionbar_locked; + dict["actionbar_profile"] = _action_bar_profile->to_dict(); + dict["input_profile"] = _input_profile->to_dict(); dict["custom_data"] = _custom_data; @@ -132,6 +134,7 @@ void ClassProfile::from_dict(const Dictionary &dict) { _actionbar_locked = dict.get("actionbar_locked", false); _action_bar_profile->from_dict(dict.get("actionbar_profile", Dictionary())); + _input_profile->from_dict(dict.get("input_profile", Dictionary())); _custom_data = dict.get("custom_data", Dictionary()); @@ -141,7 +144,9 @@ void ClassProfile::from_dict(const Dictionary &dict) { ClassProfile::ClassProfile() { _action_bar_profile.instance(); _action_bar_profile->set_owner(this); + _input_profile.instance(); + _input_profile->set_owner(this); _level = 1; _xp = 0; @@ -151,7 +156,9 @@ ClassProfile::ClassProfile() { ClassProfile::ClassProfile(const StringName &class_path) { _action_bar_profile.instance(); _action_bar_profile->set_owner(this); + _input_profile.instance(); + _input_profile->set_owner(this); _class_path = class_path; _level = 1; @@ -164,6 +171,7 @@ ClassProfile::ClassProfile(const String &class_name, const StringName &class_pat _action_bar_profile->set_owner(this); _input_profile.instance(); + _input_profile->set_owner(this); _character_class_name = class_name; _class_path = class_path; @@ -181,6 +189,7 @@ ClassProfile::~ClassProfile() { void ClassProfile::load_defaults() { _action_bar_profile->load_defaults(); + _input_profile->load_defaults(); emit_change(); } diff --git a/profiles/input/input_profile.cpp b/profiles/input/input_profile.cpp index 4d3dec3..6e6475b 100644 --- a/profiles/input/input_profile.cpp +++ b/profiles/input/input_profile.cpp @@ -21,3 +21,98 @@ SOFTWARE. */ #include "input_profile.h" + +#include "core/project_settings.h" +#include "core/ustring.h" + +#include "../class_profile.h" + +Ref InputProfile::get_owner() { + return Ref(_owner); +} +void InputProfile::set_owner(ClassProfile *owner) { + _owner = owner; +} + +Array InputProfile::get_data() { + return _data; +} +void InputProfile::set_data(const Array &data) { + _data = data; +} + +void InputProfile::load_to_projectsettings() { + for (int i = 0; i < _data.size(); ++i) { + Array arr = _data[i]; + + ProjectSettings::get_singleton()->set(arr[0], arr[1]); + } +} + +void InputProfile::save_from_projectsettings() { + _data.clear(); + + List pinfo; + ProjectSettings::get_singleton()->get_property_list(&pinfo); + + for (List::Element *E = pinfo.front(); E; E = E->next()) { + const PropertyInfo &pi = E->get(); + + if (!pi.name.begins_with("input/")) + continue; + + String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length()); + + Dictionary action = ProjectSettings::get_singleton()->get(pi.name); + + Array arr; + arr.push_back(name); + arr.push_back(action); + + _data.push_back(arr); + } +} + +void InputProfile::load_defaults() { + _data.clear(); +} + +void InputProfile::emit_change() { + emit_signal("changed"); + + if (_owner != NULL) + _owner->emit_change(); +} + +Dictionary InputProfile::to_dict() const { + Dictionary dict; + + dict["data"] = _data; + + return dict; +} +void InputProfile::from_dict(const Dictionary &dict) { + ERR_FAIL_COND(dict.empty()); + + _data = dict.get("data", Array()); + + emit_change(); +} + +InputProfile::InputProfile() { +} + +void InputProfile::_bind_methods() { + ADD_SIGNAL(MethodInfo("changed")); + + ClassDB::bind_method(D_METHOD("get_owner"), &InputProfile::get_owner); + + ClassDB::bind_method(D_METHOD("load_defaults"), &InputProfile::load_defaults); + + ClassDB::bind_method(D_METHOD("get_data"), &InputProfile::get_data); + ClassDB::bind_method(D_METHOD("set_data", "data"), &InputProfile::set_data); + ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "data"), "set_data", "get_data"); + + ClassDB::bind_method(D_METHOD("load_to_projectsettings"), &InputProfile::load_to_projectsettings); + ClassDB::bind_method(D_METHOD("save_from_projectsettings"), &InputProfile::save_from_projectsettings); +} \ No newline at end of file diff --git a/profiles/input/input_profile.h b/profiles/input/input_profile.h index 213d3b6..167d21d 100644 --- a/profiles/input/input_profile.h +++ b/profiles/input/input_profile.h @@ -26,17 +26,37 @@ SOFTWARE. #include "core/reference.h" #include "core/ustring.h" +class ClassProfile; + class InputProfile : public Reference { GDCLASS(InputProfile, Reference); public: - InputProfile() {} + Ref get_owner(); + void set_owner(ClassProfile *owner); + + Array get_data(); + void set_data(const Array &data); + + void load_to_projectsettings(); + void save_from_projectsettings(); + + void load_defaults(); + + void emit_change(); + + Dictionary to_dict() const; + void from_dict(const Dictionary &dict); + + InputProfile(); protected: - static void _bind_methods() {} + static void _bind_methods(); private: - int placeholder; + Array _data; + + ClassProfile *_owner; }; #endif diff --git a/singletons/profile_manager.cpp b/singletons/profile_manager.cpp index 775f372..d432ed0 100644 --- a/singletons/profile_manager.cpp +++ b/singletons/profile_manager.cpp @@ -185,6 +185,12 @@ void ProfileManager::from_dict(const Dictionary &dict) { } } +void ProfileManager::on_keybinds_changed(const StringName &class_path) { + getc_player_profile()->get_class_profile(class_path)->get_input_profile()->save_from_projectsettings(); + + emit_signal("keybinds_changed"); +} + ProfileManager::ProfileManager() { _instance = this; @@ -214,6 +220,7 @@ void ProfileManager::_on_player_profile_changed(Ref profile) { } void ProfileManager::_bind_methods() { + ADD_SIGNAL(MethodInfo("keybinds_changed")); ADD_SIGNAL(MethodInfo("changed")); BIND_VMETHOD(MethodInfo("_save")); @@ -252,4 +259,6 @@ void ProfileManager::_bind_methods() { ClassDB::bind_method(D_METHOD("to_dict"), &ProfileManager::to_dict); ClassDB::bind_method(D_METHOD("_on_player_profile_changed", "profile"), &ProfileManager::_on_player_profile_changed); + + ClassDB::bind_method(D_METHOD("on_keybinds_changed", "class_path"), &ProfileManager::on_keybinds_changed); } diff --git a/singletons/profile_manager.h b/singletons/profile_manager.h index 9c2bbf5..8421543 100644 --- a/singletons/profile_manager.h +++ b/singletons/profile_manager.h @@ -66,6 +66,8 @@ public: Dictionary to_dict() const; void from_dict(const Dictionary &dict); + void on_keybinds_changed(const StringName &class_path); + ProfileManager(); ~ProfileManager();