Added change notifications to ProfileManager, and it's data classes. Also added option "automatic save" for the ProfileManager (still needs some work).

This commit is contained in:
Relintai 2020-03-18 03:04:50 +01:00
parent 9cc99bbc27
commit affacaea58
11 changed files with 289 additions and 67 deletions

View File

@ -620,19 +620,20 @@ void Entity::setup_actionbars() {
ProfileManager *pm = ProfileManager::get_instance();
if (pm != NULL) {
Ref<ClassProfile> cp = pm->get_class_profile(gets_entity_data()->get_id());
//if (pm != NULL) {
// Ref<ClassProfile> cp = pm->get_class_profile(gets_entity_data()->get_id());
if (cp.is_valid()) {
set_actionbar_locked(cp->get_actionbar_locked());
// if (cp.is_valid()) {
// set_actionbar_locked(cp->get_actionbar_locked());
// _action_bar_profile = cp->get_action_bar_profile();
get_action_bar_profile()->clear_action_bars();
//get_action_bar_profile()->clear_action_bars();
Ref<ActionBarProfile> abp = cp->get_action_bar_profile();
//Ref<ActionBarProfile> abp = cp->get_action_bar_profile();
get_action_bar_profile()->from_actionbar_profile(abp);
}
}
//get_action_bar_profile()->from_actionbar_profile(abp);
//}
//}
if (!gets_bag().is_valid()) {
@ -1053,7 +1054,7 @@ Dictionary Entity::_to_dict() {
//// Actionbars ////
dict["actionbar_locked"] = _actionbar_locked;
dict["actionbar_profile"] = _action_bar_profile->to_dict();
//dict["actionbar_profile"] = _action_bar_profile->to_dict();
return dict;
}
@ -1315,7 +1316,7 @@ void Entity::_from_dict(const Dictionary &dict) {
//// Actionbars ////
_actionbar_locked = dict.get("actionbar_locked", false);
_action_bar_profile->from_dict(dict.get("actionbar_profile", Dictionary()));
//_action_bar_profile->from_dict(dict.get("actionbar_profile", Dictionary()));
int edi = dict.get("entity_data_id", 0);
@ -5361,7 +5362,16 @@ void Entity::set_actionbar_locked(bool value) {
}
Ref<ActionBarProfile> Entity::get_action_bar_profile() {
return _action_bar_profile;
Ref<ClassProfile> cp = ProfileManager::get_instance()->get_class_profile(gets_entity_data()->get_id());
if (cp.is_valid()) {
set_actionbar_locked(cp->get_actionbar_locked());
return cp->get_action_bar_profile();
}
return Ref<ActionBarProfile>();
//return _action_bar_profile;
}
void Entity::loaded() {
@ -5762,7 +5772,7 @@ Entity::Entity() {
_s_free_spell_points = 0;
_c_free_spell_points = 0;
_action_bar_profile.instance();
//_action_bar_profile.instance();
_actionbar_locked = false;
for (int i = 0; i < Stat::STAT_ID_TOTAL_STATS; ++i) {

View File

@ -22,8 +22,17 @@ SOFTWARE.
#include "action_bar_button_entry.h"
#include "action_bar_entry.h"
const String ActionBarButtonEntry::BINDING_STRING_ACTIONBAR_BUTTON_ENTRY_TYPE = "None, Spell, Item";
Ref<ActionBarEntry> ActionBarButtonEntry::get_owner() {
return Ref<ActionBarEntry>(_owner);
}
void ActionBarButtonEntry::set_owner(ActionBarEntry *owner) {
_owner = owner;
}
int ActionBarButtonEntry::get_action_bar_id() {
return _action_bar_id;
}
@ -31,7 +40,7 @@ int ActionBarButtonEntry::get_action_bar_id() {
void ActionBarButtonEntry::set_action_bar_id(int value) {
_action_bar_id = value;
changed();
emit_change();
}
int ActionBarButtonEntry::get_slot_id() {
@ -41,7 +50,7 @@ int ActionBarButtonEntry::get_slot_id() {
void ActionBarButtonEntry::set_slot_id(int value) {
_slot_id = value;
changed();
emit_change();
}
ActionBarButtonEntry::ActionBarButtonEntryType ActionBarButtonEntry::get_type() {
@ -51,7 +60,7 @@ ActionBarButtonEntry::ActionBarButtonEntryType ActionBarButtonEntry::get_type()
void ActionBarButtonEntry::set_type(ActionBarButtonEntry::ActionBarButtonEntryType value) {
_type = value;
changed();
emit_change();
}
int ActionBarButtonEntry::get_item_id() {
@ -61,11 +70,14 @@ int ActionBarButtonEntry::get_item_id() {
void ActionBarButtonEntry::set_item_id(int value) {
_item_id = value;
changed();
emit_change();
}
void ActionBarButtonEntry::changed() {
emit_signal("changed");
void ActionBarButtonEntry::emit_change() {
emit_signal("changed", Ref<ActionBarButtonEntry>(this));
if (_owner != NULL)
_owner->emit_change();
}
Dictionary ActionBarButtonEntry::to_dict() const {
@ -85,9 +97,13 @@ void ActionBarButtonEntry::from_dict(const Dictionary &dict) {
_slot_id = dict.get("slot_id", 0);
_type = VariantCaster<ActionBarButtonEntryType>().cast(dict.get("type", ACTION_BAR_BUTTON_ENTRY_TYPE_NONE));
_item_id = dict.get("item_id", 0);
emit_change();
}
ActionBarButtonEntry::ActionBarButtonEntry() {
_owner = NULL;
_action_bar_id = 0;
_slot_id = 0;
_type = ACTION_BAR_BUTTON_ENTRY_TYPE_NONE;
@ -95,6 +111,8 @@ ActionBarButtonEntry::ActionBarButtonEntry() {
}
ActionBarButtonEntry::ActionBarButtonEntry(int actionBarId, int slotId) {
_owner = NULL;
_action_bar_id = actionBarId;
_slot_id = slotId;
@ -103,14 +121,21 @@ ActionBarButtonEntry::ActionBarButtonEntry(int actionBarId, int slotId) {
}
ActionBarButtonEntry::ActionBarButtonEntry(int actionBarId, int slotId, ActionBarButtonEntryType type, int itemId) {
_owner = NULL;
_action_bar_id = actionBarId;
_slot_id = slotId;
_type = type;
_item_id = itemId;
}
ActionBarButtonEntry::~ActionBarButtonEntry() {
}
void ActionBarButtonEntry::_bind_methods() {
ADD_SIGNAL(MethodInfo("changed"));
ADD_SIGNAL(MethodInfo("changed", PropertyInfo(Variant::OBJECT, "action_bar_button_entry", PROPERTY_HINT_RESOURCE_TYPE, "ActionBarButtonEntry")));
ClassDB::bind_method(D_METHOD("get_owner"), &ActionBarButtonEntry::get_owner);
ClassDB::bind_method(D_METHOD("get_action_bar_id"), &ActionBarButtonEntry::get_action_bar_id);
ClassDB::bind_method(D_METHOD("set_action_bar_id", "value"), &ActionBarButtonEntry::set_action_bar_id);
@ -131,6 +156,8 @@ void ActionBarButtonEntry::_bind_methods() {
ClassDB::bind_method(D_METHOD("from_dict", "dict"), &ActionBarButtonEntry::from_dict);
ClassDB::bind_method(D_METHOD("to_dict"), &ActionBarButtonEntry::to_dict);
ClassDB::bind_method(D_METHOD("emit_change"), &ActionBarButtonEntry::emit_change);
BIND_ENUM_CONSTANT(ACTION_BAR_BUTTON_ENTRY_TYPE_NONE);
BIND_ENUM_CONSTANT(ACTION_BAR_BUTTON_ENTRY_TYPE_SPELL);
BIND_ENUM_CONSTANT(ACTION_BAR_BUTTON_ENTRY_TYPE_ITEM);

View File

@ -26,6 +26,8 @@ SOFTWARE.
#include "core/dictionary.h"
#include "core/reference.h"
class ActionBarEntry;
class ActionBarButtonEntry : public Reference {
GDCLASS(ActionBarButtonEntry, Reference);
@ -38,6 +40,9 @@ public:
ACTION_BAR_BUTTON_ENTRY_TYPE_ITEM
};
Ref<ActionBarEntry> get_owner();
void set_owner(ActionBarEntry *owner);
int get_action_bar_id();
void set_action_bar_id(int value);
@ -50,7 +55,7 @@ public:
int get_item_id();
void set_item_id(int value);
void changed();
void emit_change();
Dictionary to_dict() const;
void from_dict(const Dictionary &dict);
@ -58,6 +63,7 @@ public:
ActionBarButtonEntry();
ActionBarButtonEntry(int actionBarId, int slotId);
ActionBarButtonEntry(int actionBarId, int slotId, ActionBarButtonEntryType type, int itemId);
~ActionBarButtonEntry();
protected:
static void _bind_methods();
@ -67,6 +73,8 @@ private:
int _slot_id;
ActionBarButtonEntryType _type;
int _item_id;
ActionBarEntry *_owner;
};
VARIANT_ENUM_CAST(ActionBarButtonEntry::ActionBarButtonEntryType);

View File

@ -22,6 +22,15 @@ SOFTWARE.
#include "action_bar_entry.h"
#include "action_bar_profile.h"
Ref<ActionBarProfile> ActionBarEntry::get_owner() {
return Ref<ActionBarProfile>(_owner);
}
void ActionBarEntry::set_owner(ActionBarProfile *owner) {
_owner = owner;
}
float ActionBarEntry::get_size() {
return _size;
}
@ -29,7 +38,7 @@ float ActionBarEntry::get_size() {
void ActionBarEntry::set_size(float value) {
_size = value;
emit_signal("changed");
emit_change();
}
int ActionBarEntry::get_action_bar_id() {
@ -39,7 +48,7 @@ int ActionBarEntry::get_action_bar_id() {
void ActionBarEntry::set_action_bar_id(int value) {
_action_bar_id = value;
emit_signal("changed");
emit_change();
}
int ActionBarEntry::get_slot_num() {
@ -49,13 +58,20 @@ int ActionBarEntry::get_slot_num() {
void ActionBarEntry::set_slot_num(int value) {
_slot_num = value;
emit_signal("changed");
emit_change();
}
int ActionBarEntry::get_action_bar_entry_count() {
return _button_entries.size();
}
void ActionBarEntry::emit_change() {
emit_signal("changed");
if (_owner != NULL)
_owner->emit_change();
}
Ref<ActionBarButtonEntry> ActionBarEntry::get_button_for_slotid(int slotId) {
for (int i = 0; i < _button_entries.size(); ++i) {
if (_button_entries.get(i)->get_slot_id() == slotId) {
@ -64,7 +80,11 @@ Ref<ActionBarButtonEntry> ActionBarEntry::get_button_for_slotid(int slotId) {
}
Ref<ActionBarButtonEntry> abe = Ref<ActionBarButtonEntry>(memnew(ActionBarButtonEntry(_action_bar_id, slotId, ActionBarButtonEntry::ACTION_BAR_BUTTON_ENTRY_TYPE_NONE, 0)));
abe->set_owner(this);
_button_entries.push_back(abe);
emit_change();
return Ref<ActionBarButtonEntry>(abe);
}
@ -95,6 +115,10 @@ Dictionary ActionBarEntry::to_dict() const {
void ActionBarEntry::from_dict(const Dictionary &dict) {
ERR_FAIL_COND(dict.empty());
for (int i = 0; i < _button_entries.size(); ++i) {
_button_entries.get(i)->set_owner(NULL);
}
_button_entries.clear();
_action_bar_id = dict.get("action_bar_id", 0);
@ -107,12 +131,17 @@ void ActionBarEntry::from_dict(const Dictionary &dict) {
e.instance();
e->from_dict(arr.get(i));
e->set_owner(this);
_button_entries.push_back(e);
}
emit_change();
}
ActionBarEntry::ActionBarEntry() {
_owner = NULL;
_size = 45;
_slot_num = 12;
}
@ -124,6 +153,8 @@ ActionBarEntry::~ActionBarEntry() {
void ActionBarEntry::_bind_methods() {
ADD_SIGNAL(MethodInfo("changed"));
ClassDB::bind_method(D_METHOD("get_owner"), &ActionBarEntry::get_owner);
ClassDB::bind_method(D_METHOD("get_size"), &ActionBarEntry::get_size);
ClassDB::bind_method(D_METHOD("set_size", "value"), &ActionBarEntry::set_size);
ADD_PROPERTY(PropertyInfo(Variant::REAL, "size"), "set_size", "get_size");
@ -143,4 +174,6 @@ void ActionBarEntry::_bind_methods() {
ClassDB::bind_method(D_METHOD("from_dict", "dict"), &ActionBarEntry::from_dict);
ClassDB::bind_method(D_METHOD("to_dict"), &ActionBarEntry::to_dict);
ClassDB::bind_method(D_METHOD("emit_change"), &ActionBarEntry::emit_change);
}

View File

@ -30,10 +30,15 @@ SOFTWARE.
#include "action_bar_button_entry.h"
class ActionBarProfile;
class ActionBarEntry : public Reference {
GDCLASS(ActionBarEntry, Reference);
public:
Ref<ActionBarProfile> get_owner();
void set_owner(ActionBarProfile *owner);
float get_size();
void set_size(float value);
@ -45,6 +50,8 @@ public:
int get_action_bar_entry_count();
void emit_change();
Ref<ActionBarButtonEntry> get_button_for_slotid(int slotId);
Ref<ActionBarButtonEntry> get_button(int index);
@ -63,6 +70,8 @@ private:
Vector<Ref<ActionBarButtonEntry> > _button_entries;
float _size;
ActionBarProfile *_owner;
};
#endif

View File

@ -22,12 +22,23 @@ SOFTWARE.
#include "action_bar_profile.h"
#include "../class_profile.h"
Ref<ClassProfile> ActionBarProfile::get_owner() {
return Ref<ClassProfile>(_owner);
}
void ActionBarProfile::set_owner(ClassProfile *owner) {
_owner = owner;
}
String ActionBarProfile::get_action_bar_profile_name() {
return _name;
}
void ActionBarProfile::set_action_bar_profile_name(String value) {
_name = value;
emit_change();
}
Vector<Ref<ActionBarEntry> > &ActionBarProfile::get_action_bars() {
@ -40,22 +51,28 @@ void ActionBarProfile::load_defaults() {
Ref<ActionBarEntry> actionBarEntry = Ref<ActionBarEntry>(memnew(ActionBarEntry()));
actionBarEntry->set_action_bar_id(1);
actionBarEntry->set_slot_num(12);
actionBarEntry->set_owner(this);
_action_bars.push_back(Ref<ActionBarEntry>(actionBarEntry));
actionBarEntry = Ref<ActionBarEntry>(memnew(ActionBarEntry()));
actionBarEntry->set_action_bar_id(2);
actionBarEntry->set_slot_num(12);
actionBarEntry->set_owner(this);
_action_bars.push_back(Ref<ActionBarEntry>(actionBarEntry));
actionBarEntry = Ref<ActionBarEntry>(memnew(ActionBarEntry()));
actionBarEntry->set_action_bar_id(3);
actionBarEntry->set_slot_num(12);
actionBarEntry->set_owner(this);
_action_bars.push_back(Ref<ActionBarEntry>(actionBarEntry));
actionBarEntry = Ref<ActionBarEntry>(memnew(ActionBarEntry()));
actionBarEntry->set_action_bar_id(4);
actionBarEntry->set_slot_num(12);
actionBarEntry->set_owner(this);
_action_bars.push_back(Ref<ActionBarEntry>(actionBarEntry));
emit_change();
}
int ActionBarProfile::get_action_bar_count() {
@ -63,7 +80,12 @@ int ActionBarProfile::get_action_bar_count() {
}
void ActionBarProfile::add_action_bar(Ref<ActionBarEntry> actionbar) {
ERR_FAIL_COND(!actionbar.is_valid());
actionbar->set_owner(this);
_action_bars.push_back(Ref<ActionBarEntry>(actionbar));
emit_change();
}
Ref<ActionBarEntry> ActionBarProfile::get_action_bar(int index) {
@ -73,11 +95,21 @@ Ref<ActionBarEntry> ActionBarProfile::get_action_bar(int index) {
}
void ActionBarProfile::remove_action_bar(int index) {
_action_bars.get(index)->set_owner(NULL);
_action_bars.remove(index);
emit_change();
}
void ActionBarProfile::clear_action_bars() {
for (int i = 0; i < _action_bars.size(); ++i) {
_action_bars.get(i)->set_owner(NULL);
}
_action_bars.clear();
emit_change();
}
Dictionary ActionBarProfile::to_dict() const {
@ -101,6 +133,10 @@ void ActionBarProfile::from_dict(const Dictionary &dict) {
if (dict.empty())
return;
for (int i = 0; i < _action_bars.size(); ++i) {
_action_bars.get(i)->set_owner(NULL);
}
_action_bars.clear();
_name = dict.get("name", "");
@ -112,29 +148,53 @@ void ActionBarProfile::from_dict(const Dictionary &dict) {
e.instance();
e->from_dict(arr.get(i));
e->set_owner(this);
_action_bars.push_back(e);
}
emit_change();
}
void ActionBarProfile::from_actionbar_profile(Ref<ActionBarProfile> other) {
for (int i = 0; i < _action_bars.size(); ++i) {
_action_bars.get(i)->set_owner(NULL);
}
_action_bars.clear();
_name = other->get_action_bar_profile_name();
for (int i = 0; i < other->get_action_bar_count(); ++i) {
_action_bars.push_back(other->get_action_bar(i));
Ref<ActionBarEntry> e = other->get_action_bar(i);
e->set_owner(this);
_action_bars.push_back(e);
}
emit_change();
}
ActionBarProfile::ActionBarProfile() {
_owner = NULL;
}
ActionBarProfile::~ActionBarProfile() {
_action_bars.clear();
}
void ActionBarProfile::emit_change() {
emit_signal("changed");
if (_owner != NULL)
_owner->emit_change();
}
void ActionBarProfile::_bind_methods() {
ADD_SIGNAL(MethodInfo("changed"));
ClassDB::bind_method(D_METHOD("get_owner"), &ActionBarProfile::get_owner);
ClassDB::bind_method(D_METHOD("get_action_bar_profile_name"), &ActionBarProfile::get_action_bar_profile_name);
ClassDB::bind_method(D_METHOD("set_action_bar_profile_name", "value"), &ActionBarProfile::set_action_bar_profile_name);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "action_bar_profile_name"), "set_action_bar_profile_name", "get_action_bar_profile_name");
@ -151,4 +211,6 @@ void ActionBarProfile::_bind_methods() {
ClassDB::bind_method(D_METHOD("to_dict"), &ActionBarProfile::to_dict);
ClassDB::bind_method(D_METHOD("from_actionbar_profile", "other"), &ActionBarProfile::from_actionbar_profile);
ClassDB::bind_method(D_METHOD("emit_change"), &ActionBarProfile::emit_change);
}

View File

@ -31,10 +31,15 @@ SOFTWARE.
#include "action_bar_entry.h"
class ClassProfile;
class ActionBarProfile : public Reference {
GDCLASS(ActionBarProfile, Reference);
public:
Ref<ClassProfile> get_owner();
void set_owner(ClassProfile *owner);
String get_action_bar_profile_name();
void set_action_bar_profile_name(String value);
Vector<Ref<ActionBarEntry> > &get_action_bars();
@ -51,6 +56,8 @@ public:
void from_actionbar_profile(Ref<ActionBarProfile> other);
void emit_change();
ActionBarProfile();
~ActionBarProfile();
@ -60,6 +67,8 @@ protected:
private:
String _name;
Vector<Ref<ActionBarEntry> > _action_bars;
ClassProfile *_owner;
};
#endif

View File

@ -44,6 +44,8 @@ int ClassProfile::get_level() {
void ClassProfile::set_level(int value) {
_level = value;
emit_change();
}
int ClassProfile::get_xp() {
@ -52,6 +54,8 @@ int ClassProfile::get_xp() {
void ClassProfile::set_xp(int value) {
_xp = value;
emit_change();
}
bool ClassProfile::get_actionbar_locked() {
@ -59,6 +63,8 @@ bool ClassProfile::get_actionbar_locked() {
}
void ClassProfile::set_actionbar_locked(bool value) {
_actionbar_locked = value;
emit_change();
}
Ref<InputProfile> ClassProfile::get_input_profile() {
@ -68,11 +74,17 @@ Ref<ActionBarProfile> ClassProfile::get_action_bar_profile() {
return _action_bar_profile;
}
void ClassProfile::emit_change() {
emit_signal("changed", Ref<ClassProfile>(this));
}
Dictionary ClassProfile::get_custom_data() {
return _custom_data;
}
void ClassProfile::set_custom_data(const Dictionary &dict) {
_custom_data = dict;
emit_change();
}
Dictionary ClassProfile::to_dict() const {
@ -101,11 +113,14 @@ void ClassProfile::from_dict(const Dictionary &dict) {
_action_bar_profile->from_dict(dict.get("actionbar_profile", Dictionary()));
_custom_data = dict.get("custom_data", Dictionary());
emit_change();
}
ClassProfile::ClassProfile() {
_action_bar_profile = Ref<ActionBarProfile>(memnew(ActionBarProfile()));
_input_profile = Ref<InputProfile>(memnew(InputProfile()));
_action_bar_profile.instance();
_action_bar_profile->set_owner(this);
_input_profile.instance();
_class_id = 0;
_level = 0;
@ -114,36 +129,45 @@ ClassProfile::ClassProfile() {
}
ClassProfile::ClassProfile(int class_id) {
_action_bar_profile = Ref<ActionBarProfile>(memnew(ActionBarProfile()));
_input_profile = Ref<InputProfile>(memnew(InputProfile()));
_action_bar_profile.instance();
_action_bar_profile->set_owner(this);
_input_profile.instance();
_class_id = class_id;
_level = 50;
_xp = 0;
_actionbar_locked = false;
load_defaults();
}
ClassProfile::ClassProfile(String class_name, int class_id, int level, int xp, bool locked, bool pload_defaults) {
_action_bar_profile = Ref<ActionBarProfile>(memnew(ActionBarProfile()));
_input_profile = Ref<InputProfile>(memnew(InputProfile()));
ClassProfile::ClassProfile(String class_name, int class_id, int level, int xp, bool locked) {
_action_bar_profile.instance();
_action_bar_profile->set_owner(this);
_input_profile.instance();
_character_class_name = class_name;
_class_id = class_id;
_level = level;
_xp = xp;
_actionbar_locked = true;
}
if (pload_defaults) {
load_defaults();
}
ClassProfile::~ClassProfile() {
_input_profile.unref();
_action_bar_profile.unref();
_custom_data.clear();
}
void ClassProfile::load_defaults() {
_action_bar_profile->load_defaults();
emit_change();
}
void ClassProfile::_bind_methods() {
ADD_SIGNAL(MethodInfo("changed"));
ClassDB::bind_method(D_METHOD("get_class_id"), &ClassProfile::get_class_id);
ClassDB::bind_method(D_METHOD("set_class_id", "value"), &ClassProfile::set_class_id);
ADD_PROPERTY(PropertyInfo(Variant::INT, "class_id"), "set_class_id", "get_class_id");

View File

@ -47,6 +47,8 @@ public:
Ref<InputProfile> get_input_profile();
Ref<ActionBarProfile> get_action_bar_profile();
void emit_change();
Dictionary get_custom_data();
void set_custom_data(const Dictionary &dict);
@ -55,7 +57,9 @@ public:
ClassProfile();
ClassProfile(int class_id);
ClassProfile(String class_name, int class_id, int level, int xp, bool locked, bool load_defaults);
ClassProfile(String class_name, int class_id, int level, int xp, bool locked);
~ClassProfile();
void load_defaults();
protected:

View File

@ -38,41 +38,64 @@ String ProfileManager::get_save_file() const {
}
void ProfileManager::set_save_file(const String &file) {
_save_file = file;
if (_automatic_save)
save();
}
int ProfileManager::get_last_used_class() {
int ProfileManager::get_last_used_class() const {
return _last_used_class;
}
void ProfileManager::set_last_used_class(int value) {
void ProfileManager::set_last_used_class(const int value) {
_last_used_class = value;
if (_automatic_save)
save();
}
int ProfileManager::get_class_profile_count() {
int ProfileManager::get_class_profile_count() const {
return _class_profiles.size();
}
Ref<ClassProfile> ProfileManager::get_class_profile_index(int index) {
Ref<ClassProfile> ProfileManager::get_class_profile_index(const int index) {
return _class_profiles.get(index);
}
void ProfileManager::add_class_profile(Ref<ClassProfile> profile) {
profile->connect("changed", this, "_on_class_profile_changed");
_class_profiles.push_back(profile);
if (_automatic_save)
save();
}
void ProfileManager::clear_class_profiles() {
for (int i = 0; i < _class_profiles.size(); ++i) {
_class_profiles.get(i)->disconnect("changed", this, "_on_class_profile_changed");
}
_class_profiles.clear();
if (_automatic_save)
save();
}
void ProfileManager::remove_class_profile(int index) {
void ProfileManager::remove_class_profile(const int index) {
_class_profiles.get(index)->disconnect("changed", this, "_on_class_profile_changed");
_class_profiles.remove(index);
if (_automatic_save)
save();
}
Vector<Ref<ClassProfile> > &ProfileManager::get_class_profiles() {
return _class_profiles;
}
Ref<ClassProfile> ProfileManager::get_class_profile(int class_id) {
Ref<ClassProfile> ProfileManager::get_class_profile(const int class_id) {
for (int i = 0; i < _class_profiles.size(); ++i) {
if (_class_profiles.get(i)->get_class_id() == class_id) {
return Ref<ClassProfile>(_class_profiles.get(i));
@ -82,12 +105,13 @@ Ref<ClassProfile> ProfileManager::get_class_profile(int class_id) {
Ref<ClassProfile> class_profile = Ref<ClassProfile>(memnew(ClassProfile(class_id)));
class_profile->load_defaults();
class_profile->connect("changed", this, "_on_class_profile_changed");
_class_profiles.push_back(Ref<ClassProfile>(class_profile));
emit_signal("changed");
return Ref<ClassProfile>(class_profile);
return class_profile;
}
void ProfileManager::save() {
@ -96,6 +120,8 @@ void ProfileManager::save() {
void ProfileManager::load() {
call("_load");
emit_signal("changed");
}
void ProfileManager::_save() {
@ -140,29 +166,24 @@ void ProfileManager::_load() {
}
}
void ProfileManager::save_profile(String name) {
void ProfileManager::save_profile(const String &name) {
}
void ProfileManager::load_profile(String name) {
void ProfileManager::load_profile(const String &name) {
load_defaults();
}
int _last_used_class;
String _profile_name;
Vector<Ref<ClassProfile> > _class_profiles;
void ProfileManager::load_defaults() {
_class_profiles.clear();
clear_class_profiles();
_class_profiles.push_back(memnew(ClassProfile("Naturalist", 1, 1, 0, false, true)));
_class_profiles.push_back(memnew(ClassProfile("Berserker", 3, 1, 0, false, true)));
_class_profiles.push_back(memnew(ClassProfile("IceArcher", 4, 1, 0, false, true)));
_class_profiles.push_back(memnew(ClassProfile("Chronomancer", 6, 1, 0, false, true)));
_class_profiles.push_back(memnew(ClassProfile("Naturalist", 1, 1, 0, false)));
_class_profiles.push_back(memnew(ClassProfile("Berserker", 3, 1, 0, false)));
_class_profiles.push_back(memnew(ClassProfile("IceArcher", 4, 1, 0, false)));
_class_profiles.push_back(memnew(ClassProfile("Chronomancer", 6, 1, 0, false)));
for (int i = 0; i < _class_profiles.size(); ++i) {
_class_profiles.get(i)->load_defaults();
_class_profiles.get(i)->connect("changed", this, "_on_class_profile_changed");
}
emit_signal("changed");
@ -189,6 +210,8 @@ Dictionary ProfileManager::to_dict() const {
void ProfileManager::from_dict(const Dictionary &dict) {
ERR_FAIL_COND(dict.empty());
clear_class_profiles();
_last_used_class = dict.get("last_used_class", "");
_profile_name = dict.get("profile_name", 0);
@ -199,6 +222,7 @@ void ProfileManager::from_dict(const Dictionary &dict) {
c.instance();
c->from_dict(arr.get(i));
c->connect("changed", this, "_on_class_profile_changed");
_class_profiles.push_back(c);
}
@ -211,11 +235,11 @@ ProfileManager::ProfileManager() {
_profile_name = ProfileManager::DEFAULT_PROFILE_FILE_NAME;
_automatic_load = GLOBAL_DEF("ess/profiles/automatic_load", false);
_automatic_save = GLOBAL_DEF("ess/profiles/automatic_save", false);
_save_file = GLOBAL_DEF("ess/profiles/save_file", "user://profile.save");
if (_automatic_load) {
if (_automatic_load)
call_deferred("load");
}
}
ProfileManager::~ProfileManager() {
@ -224,6 +248,11 @@ ProfileManager::~ProfileManager() {
_class_profiles.clear();
}
void ProfileManager::_on_class_profile_changed(Ref<ClassProfile> profile) {
if (_automatic_save)
save();
}
void ProfileManager::_bind_methods() {
ADD_SIGNAL(MethodInfo("changed"));
@ -260,4 +289,6 @@ void ProfileManager::_bind_methods() {
ClassDB::bind_method(D_METHOD("from_dict", "dict"), &ProfileManager::from_dict);
ClassDB::bind_method(D_METHOD("to_dict"), &ProfileManager::to_dict);
ClassDB::bind_method(D_METHOD("_on_class_profile_changed", "profile"), &ProfileManager::_on_class_profile_changed);
}

View File

@ -40,20 +40,23 @@ public:
bool get_automatic_load() { return _automatic_load; }
void set_automatic_load(bool load) { _automatic_load = load; }
bool get_automatic_save() { return _automatic_save; }
void set_automatic_save(bool load) { _automatic_save = load; }
String get_save_file() const;
void set_save_file(const String &file);
int get_last_used_class();
void set_last_used_class(int value);
int get_last_used_class() const;
void set_last_used_class(const int value);
int get_class_profile_count();
Ref<ClassProfile> get_class_profile_index(int index);
int get_class_profile_count() const;
Ref<ClassProfile> get_class_profile_index(const int index);
void add_class_profile(Ref<ClassProfile> profile);
void clear_class_profiles();
void remove_class_profile(int index);
void remove_class_profile(const int index);
Vector<Ref<ClassProfile> > &get_class_profiles();
Ref<ClassProfile> get_class_profile(int class_id);
Ref<ClassProfile> get_class_profile(const int class_id);
void save();
void load();
@ -61,8 +64,8 @@ public:
void _save();
void _load();
void save_profile(String name);
void load_profile(String name);
void save_profile(const String &name);
void load_profile(const String &name);
void load_defaults();
@ -73,12 +76,14 @@ public:
~ProfileManager();
protected:
void _on_class_profile_changed(Ref<ClassProfile> profile);
static void _bind_methods();
private:
static ProfileManager *_instance;
bool _automatic_load;
bool _automatic_save;
int _last_used_class;