diff --git a/src/framework/nodes/node.cpp b/src/framework/nodes/node.cpp index cb5d645..99ae7f4 100644 --- a/src/framework/nodes/node.cpp +++ b/src/framework/nodes/node.cpp @@ -2,7 +2,6 @@ #include "framework/input.h" #include "framework/utils/utils.h" -#include "framework/nodes/node_factory.h" #include "node_factory.h" #include "node_binary_format.h" @@ -22,6 +21,8 @@ Node::Node() { node_type = "Node"; + scene_unique_id = generate_unique_id(); + name = "Node_" + std::to_string(last_node_id++); } diff --git a/src/framework/nodes/node.h b/src/framework/nodes/node.h index 5c5ebea..8d0dc6f 100644 --- a/src/framework/nodes/node.h +++ b/src/framework/nodes/node.h @@ -67,6 +67,8 @@ class Node { std::string name; std::string node_type; + std::string scene_unique_id; + Node* parent = nullptr; std::vector children; @@ -88,25 +90,25 @@ class Node { virtual void serialize(std::ofstream& binary_scene_file); virtual void parse(std::ifstream& binary_scene_file); + void set_node_type(const std::string& new_type) { node_type = new_type; } + void set_name(const std::string& new_name) { name = new_name; } + virtual void set_aabb(const AABB& new_aabb) { aabb = new_aabb; } + std::string get_name() const { return name; } std::string get_node_type() const { return node_type; } + std::string get_scene_unique_id() const { return scene_unique_id; } virtual std::vector& get_children() { return children; } + virtual Node* get_node(std::vector& path_tokens); AABB get_aabb() const; + Node* get_node(const std::string& path); + Node::AnimatableProperty get_animatable_property(const std::string& name); + const std::unordered_map& get_animatable_properties() const; template T get_parent() const { if (!parent) return nullptr; return dynamic_cast(parent); }; - virtual Node* get_node(std::vector& path_tokens); - Node* get_node(const std::string& path); std::string find_path(const std::string& node_name, const std::string& current_path = ""); - Node::AnimatableProperty get_animatable_property(const std::string& name); - const std::unordered_map& get_animatable_properties() const; - - void set_node_type(const std::string& new_type) { node_type = new_type; } - void set_name(const std::string& new_name) { name = new_name; } - virtual void set_aabb(const AABB& new_aabb) { aabb = new_aabb; } - virtual void disable_2d(); virtual void clone(Node* new_node, bool copy = true); diff --git a/src/framework/resources/resource.cpp b/src/framework/resources/resource.cpp index 614906e..a3badda 100644 --- a/src/framework/resources/resource.cpp +++ b/src/framework/resources/resource.cpp @@ -1,5 +1,7 @@ #include "resource.h" +#include "framework/utils/utils.h" + #include "spdlog/spdlog.h" #include @@ -7,7 +9,9 @@ Resource::Resource() { // TODO: set proper unique id for project save/load - id = reinterpret_cast(this); + // id = reinterpret_cast(this); + + scene_unique_id = generate_unique_id(); } void Resource::ref() diff --git a/src/framework/resources/resource.h b/src/framework/resources/resource.h index d64ac95..6919696 100644 --- a/src/framework/resources/resource.h +++ b/src/framework/resources/resource.h @@ -22,15 +22,17 @@ class Resource { bool operator==(const Resource& other) const { - return id == other.id; + return scene_unique_id == other.scene_unique_id; } void* get_property(const std::string& name); + std::string get_scene_unique_id() const { return scene_unique_id; } + private: uint32_t ref_count = 0; - intptr_t id = 0; + std::string scene_unique_id = ""; protected: diff --git a/src/framework/utils/utils.cpp b/src/framework/utils/utils.cpp index 1d2a131..2de4efb 100644 --- a/src/framework/utils/utils.cpp +++ b/src/framework/utils/utils.cpp @@ -1,6 +1,8 @@ #include "utils.h" #include +#include +#include #include "spdlog/spdlog.h" @@ -97,3 +99,19 @@ std::string dirname_of_file(const std::string& fname) ? "" : fname.substr(0, pos); } + +std::string generate_unique_id(uint32_t num_characters) +{ + static std::string characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + static std::random_device random_device; + static std::mt19937 generator(random_device()); + + std::uniform_int_distribution<> distribution(0, characters.size() - 1); + std::string unique_id = ""; + + for (uint32_t i = 0; i < num_characters; i++) { + unique_id += characters[distribution(generator)]; + } + + return unique_id; +} diff --git a/src/framework/utils/utils.h b/src/framework/utils/utils.h index 1a381fa..371be61 100644 --- a/src/framework/utils/utils.h +++ b/src/framework/utils/utils.h @@ -35,3 +35,5 @@ bool read_file(const std::string & filename, std::string & content); // https://stackoverflow.com/a/8518855 std::string dirname_of_file(const std::string& fname); + +std::string generate_unique_id(uint32_t num_characters = 6u);