Implement InputProfile.

This commit is contained in:
Relintai 2020-07-31 01:05:52 +02:00
parent 1aa751f959
commit 0bce71e5b7
5 changed files with 138 additions and 3 deletions

View File

@ -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();
}

View File

@ -21,3 +21,98 @@ SOFTWARE.
*/
#include "input_profile.h"
#include "core/project_settings.h"
#include "core/ustring.h"
#include "../class_profile.h"
Ref<ClassProfile> InputProfile::get_owner() {
return Ref<ClassProfile>(_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<PropertyInfo> pinfo;
ProjectSettings::get_singleton()->get_property_list(&pinfo);
for (List<PropertyInfo>::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);
}

View File

@ -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<ClassProfile> 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

View File

@ -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<PlayerProfile> 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);
}

View File

@ -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();