Skip to content

Commit

Permalink
first version of scene_unique_id for nodes and resources
Browse files Browse the repository at this point in the history
  • Loading branch information
jxarco committed Jan 28, 2025
1 parent 283ce28 commit 87ba389
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/framework/nodes/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -22,6 +21,8 @@ Node::Node()
{
node_type = "Node";

scene_unique_id = generate_unique_id();

name = "Node_" + std::to_string(last_node_id++);
}

Expand Down
20 changes: 11 additions & 9 deletions src/framework/nodes/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class Node {
std::string name;
std::string node_type;

std::string scene_unique_id;

Node* parent = nullptr;
std::vector<Node*> children;

Expand All @@ -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<Node*>& get_children() { return children; }
virtual Node* get_node(std::vector<std::string>& 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<std::string, AnimatableProperty>& get_animatable_properties() const;

template <typename T = Node*>
T get_parent() const { if (!parent) return nullptr; return dynamic_cast<T>(parent); };

virtual Node* get_node(std::vector<std::string>& 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<std::string, AnimatableProperty>& 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);
Expand Down
6 changes: 5 additions & 1 deletion src/framework/resources/resource.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#include "resource.h"

#include "framework/utils/utils.h"

#include "spdlog/spdlog.h"

#include <cassert>

Resource::Resource()
{
// TODO: set proper unique id for project save/load
id = reinterpret_cast<intptr_t>(this);
// id = reinterpret_cast<intptr_t>(this);

scene_unique_id = generate_unique_id();
}

void Resource::ref()
Expand Down
6 changes: 4 additions & 2 deletions src/framework/resources/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
18 changes: 18 additions & 0 deletions src/framework/utils/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "utils.h"

#include <fstream>
#include <random>
#include <algorithm>

#include "spdlog/spdlog.h"

Expand Down Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions src/framework/utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 87ba389

Please sign in to comment.