Add initial implementation of BBVariable and BlackboardSource classes

This commit is contained in:
Serhii Snitsaruk 2024-01-23 10:33:57 +01:00
parent 33b455f8d9
commit 6ef0dd942e
4 changed files with 325 additions and 0 deletions

146
blackboard/bb_variable.cpp Normal file
View File

@ -0,0 +1,146 @@
/**
* bb_variable.cpp
* =============================================================================
* Copyright 2021-2024 Serhii Snitsaruk
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
* =============================================================================
*/
#include "bb_variable.h"
void BBVariable::unref() {
if (data && data->refcount.unref()) {
memdelete(data);
}
data = nullptr;
}
// void BBVariable::init_ref() {
// if (data) {
// unref();
// }
// data = memnew(Data);
// data->refcount.init();
// }
void BBVariable::set_value(const Variant &p_value) {
data->value = p_value;
}
Variant BBVariable::get_value() const {
return data->value;
}
void BBVariable::set_type(Variant::Type p_type) {
data->type = p_type;
}
Variant::Type BBVariable::get_type() const {
return data->type;
}
void BBVariable::set_hint(PropertyHint p_hint) {
data->hint = p_hint;
}
PropertyHint BBVariable::get_hint() const {
return data->hint;
}
void BBVariable::set_hint_string(const String &p_hint_string) {
data->hint_string = p_hint_string;
}
String BBVariable::get_hint_string() const {
return data->hint_string;
}
BBVariable BBVariable::duplicate() const {
BBVariable var;
var.data->hint = data->hint;
var.data->hint_string = data->hint_string;
var.data->type = data->type;
var.data->value = data->value;
return var;
}
bool BBVariable::is_same_prop_info(const BBVariable &p_other) const {
if (data->type != p_other.data->type) {
return false;
}
if (data->hint != p_other.data->hint) {
return false;
}
if (data->hint_string != p_other.data->hint_string) {
return false;
}
return true;
}
void BBVariable::copy_prop_info(const BBVariable &p_other) {
data->type = p_other.data->type;
data->hint = p_other.data->hint;
data->hint_string = p_other.data->hint_string;
}
bool BBVariable::operator==(const BBVariable &p_var) const {
if (data == p_var.data) {
return true;
}
if (!data || !p_var.data) {
return false;
}
if (data->type != p_var.data->type) {
return false;
}
if (data->hint != p_var.data->hint) {
return false;
}
if (data->value != p_var.data->value) {
return false;
}
if (data->hint_string != p_var.data->hint_string) {
return false;
}
return true;
}
bool BBVariable::operator!=(const BBVariable &p_var) const {
return !(*this == p_var);
}
void BBVariable::operator=(const BBVariable &p_var) {
if (this == &p_var) {
return;
}
unref();
if (p_var.data && p_var.data->refcount.ref()) {
data = p_var.data;
}
}
BBVariable::BBVariable(const BBVariable &p_var) {
if (p_var.data && p_var.data->refcount.ref()) {
data = p_var.data;
}
}
BBVariable::BBVariable() {
data = memnew(Data);
data->refcount.init();
}
BBVariable::~BBVariable() {
unref();
}

67
blackboard/bb_variable.h Normal file
View File

@ -0,0 +1,67 @@
/**
* bb_variable.h
* =============================================================================
* Copyright 2021-2024 Serhii Snitsaruk
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
* =============================================================================
*/
#ifndef BB_VARIABLE_H
#define BB_VARIABLE_H
#include "core/object/object.h"
#include "core/templates/safe_refcount.h"
class BBVariable {
private:
struct Data {
SafeRefCount refcount;
Variant value;
Variant::Type type = Variant::NIL;
PropertyHint hint = PropertyHint::PROPERTY_HINT_NONE;
String hint_string;
// bool bound = false;
// uint64_t bound_object = 0;
// StringName bound_property;
};
Data *data = nullptr;
void unref();
// void init_ref();
public:
void set_value(const Variant &p_value);
Variant get_value() const;
void set_type(Variant::Type p_type);
Variant::Type get_type() const;
void set_hint(PropertyHint p_hint);
PropertyHint get_hint() const;
void set_hint_string(const String &p_hint_string);
String get_hint_string() const;
BBVariable duplicate() const;
bool is_same_prop_info(const BBVariable &p_other) const;
void copy_prop_info(const BBVariable &p_other);
// bool is_bound() { return bound; }
// void bind(Node *p_root, NodePath p_path);
// void unbind();
bool operator==(const BBVariable &p_var) const;
bool operator!=(const BBVariable &p_var) const;
void operator=(const BBVariable &p_var);
BBVariable(const BBVariable &p_var);
BBVariable();
~BBVariable();
};
#endif // BB_VARIABLE_H

View File

@ -0,0 +1,70 @@
/**
* blackboard_source.cpp
* =============================================================================
* Copyright 2021-2024 Serhii Snitsaruk
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
* =============================================================================
*/
#include "blackboard_source.h"
void BlackboardSource::set_value(const String &p_name, const Variant &p_value) {
ERR_FAIL_COND(!vars.has(p_name));
vars.get(p_name).set_value(p_value);
}
Variant BlackboardSource::get_value(const String &p_name) const {
ERR_FAIL_COND_V(!vars.has(p_name), Variant());
return vars.get(p_name).get_value();
}
void BlackboardSource::add_var(const String &p_name, const BBVariable &p_var) {
ERR_FAIL_COND(vars.has(p_name));
ERR_FAIL_COND(base.is_valid());
vars.insert(p_name, p_var);
}
void BlackboardSource::remove_var(const String &p_name) {
ERR_FAIL_COND(!vars.has(p_name));
ERR_FAIL_COND(base.is_valid());
vars.erase(p_name);
}
BBVariable BlackboardSource::get_var(const String &p_name) {
ERR_FAIL_COND_V(!vars.has(p_name), BBVariable());
return vars.get(p_name);
}
PackedStringArray BlackboardSource::list_vars() const {
PackedStringArray ret;
for (const KeyValue<String, BBVariable> &kv : vars) {
ret.append(kv.key);
}
return ret;
}
void BlackboardSource::sync_base() {
for (const KeyValue<String, BBVariable> &kv : base->vars) {
if (!vars.has(kv.key)) {
vars.insert(kv.key, kv.value.duplicate());
continue;
}
BBVariable var = vars.get(kv.key);
if (!var.is_same_prop_info(kv.value)) {
var.copy_prop_info(kv.value);
}
if (var.get_value().get_type() != kv.value.get_type()) {
var.set_value(kv.value.get_value());
}
}
}
Ref<Blackboard> BlackboardSource::instantiate() {
Ref<Blackboard> bb = memnew(Blackboard);
// TODO: fill bb
return bb;
}

View File

@ -0,0 +1,42 @@
/**
* blackboard_source.h
* =============================================================================
* Copyright 2021-2024 Serhii Snitsaruk
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
* =============================================================================
*/
#ifndef BLACKBOARD_SOURCE_H
#define BLACKBOARD_SOURCE_H
#include "core/io/resource.h"
#include "bb_variable.h"
#include "blackboard.h"
class BlackboardSource : public Resource {
GDCLASS(BlackboardSource, Resource);
private:
HashMap<String, BBVariable> vars;
Ref<BlackboardSource> base;
// HashMap<String, BBVariable> overrides;
public:
void set_value(const String &p_name, const Variant &p_value);
Variant get_value(const String &p_name) const;
void add_var(const String &p_name, const BBVariable &p_var);
void remove_var(const String &p_name);
BBVariable get_var(const String &p_name);
PackedStringArray list_vars() const;
void sync_base();
Ref<Blackboard> instantiate();
BlackboardSource() = default;
};
#endif // BLACKBOARD_SOURCE_H