Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
ComponentNodeData.cpp
Go to the documentation of this file.
1/*
2 * Olympe Entity Prefab Editor - Component Node Data
3 * Visual representation of a component in the graph
4 */
5
6#include "ComponentNodeData.h"
7
8namespace Olympe
9{
11 : nodeId(InvalidNodeId), selected(false), hovered(false), enabled(true)
12 {
13 position = Vector(0.0f, 0.0f, 0.0f);
14 size = Vector(150.0f, 80.0f, 0.0f);
15 }
16
17 ComponentNode::ComponentNode(const std::string& type)
18 : nodeId(InvalidNodeId), componentType(type), selected(false), hovered(false), enabled(true)
19 {
20 position = Vector(0.0f, 0.0f, 0.0f);
21 size = Vector(150.0f, 80.0f, 0.0f);
22 }
23
25 {
26 return nodeId == other.nodeId && componentType == other.componentType;
27 }
28
30 {
31 return !(*this == other);
32 }
33
35 {
36 std::string result = componentType;
37 if (!componentName.empty())
38 {
39 result += " (" + componentName + ")";
40 }
41 if (!enabled)
42 {
43 result += " [Disabled]";
44 }
45 return result;
46 }
47
49 {
50 json j;
51 j["nodeId"] = static_cast<int>(nodeId);
52 j["componentType"] = componentType;
53 j["componentName"] = componentName;
54 j["position"]["x"] = position.x;
55 j["position"]["y"] = position.y;
56 j["size"]["x"] = size.x;
57 j["size"]["y"] = size.y;
58 j["selected"] = selected;
59 j["enabled"] = enabled;
60
61 j["properties"] = json::object();
62 for (auto it = properties.begin(); it != properties.end(); ++it)
63 {
64 j["properties"][it->first] = it->second;
65 }
66
67 j["ports"] = json::array();
68 for (const auto& port : ports)
69 {
70 j["ports"].push_back(port.ToJson());
71 }
72
73 return j;
74 }
75
77 {
79
80 if (data.contains("nodeId"))
81 {
82 node.nodeId = data["nodeId"].get<NodeId>();
83 }
84
85 node.componentType = data.value("componentType", "");
86 node.componentName = data.value("componentName", "");
87 node.selected = data.value("selected", false);
88 node.enabled = data.value("enabled", true);
89
90 if (data.contains("position"))
91 {
92 node.position.x = data["position"].value("x", 0.0f);
93 node.position.y = data["position"].value("y", 0.0f);
94 }
95
96 if (data.contains("size"))
97 {
98 node.size.x = data["size"].value("x", 150.0f);
99 node.size.y = data["size"].value("y", 80.0f);
100 }
101
102 if (data.contains("properties"))
103 {
104 const nlohmann::json& propsJson = data["properties"];
105 for (auto it = propsJson.begin(); it != propsJson.end(); ++it)
106 {
107 node.properties[it.key()] = it.value().dump();
108 }
109 }
110
111 if (data.contains("ports"))
112 {
113 node.ports.clear();
114 for (const auto& portJson : data["ports"])
115 {
117 node.ports.push_back(port);
118 }
119 }
120 else
121 {
122 node.InitializePorts(1, 1);
123 }
124
125 return node;
126 }
127
128 void ComponentNode::SetProperty(const std::string& key, const std::string& value)
129 {
130 properties[key] = value;
131 }
132
133 std::string ComponentNode::GetProperty(const std::string& key) const
134 {
135 auto it = properties.find(key);
136 if (it != properties.end())
137 {
138 return it->second;
139 }
140 return "";
141 }
142
143 bool ComponentNode::HasProperty(const std::string& key) const
144 {
145 return properties.find(key) != properties.end();
146 }
147
149 {
150 if (!enabled)
151 {
152 return style.disabledColor;
153 }
154 if (selected)
155 {
156 return style.selectedColor;
157 }
158 if (hovered)
159 {
160 return style.hoverColor;
161 }
162 return style.normalColor;
163 }
164
166 {
167 ports.clear();
168
169 for (uint32_t i = 0; i < numInputPorts; ++i)
170 {
171 NodePort port(nodeId, i, false);
172 ports.push_back(port);
173 }
174
175 for (uint32_t i = 0; i < numOutputPorts; ++i)
176 {
178 ports.push_back(port);
179 }
180 }
181
182 const std::vector<NodePort>& ComponentNode::GetPorts() const
183 {
184 return ports;
185 }
186
187 std::vector<NodePort>& ComponentNode::GetPorts()
188 {
189 return ports;
190 }
191
193 {
194 for (auto& port : ports)
195 {
196 if (port.portId == portId)
197 {
198 return &port;
199 }
200 }
201 return nullptr;
202 }
203
205 {
206 for (const auto& port : ports)
207 {
208 if (port.portId == portId)
209 {
210 return &port;
211 }
212 }
213 return nullptr;
214 }
215}
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
float y
Definition vector.h:25
float x
Definition vector.h:25
< Provides AssetID and INVALID_ASSET_ID
nlohmann::json json
@ Vector
3-component vector (Vector from vector.h)
uint32_t NodeId
uint32_t PortId
const NodeId InvalidNodeId
nlohmann::json json
bool HasProperty(const std::string &key) const
std::string GetProperty(const std::string &key) const
std::map< std::string, std::string > properties
std::vector< NodePort > ports
const std::vector< NodePort > & GetPorts() const
bool operator!=(const ComponentNode &other) const
NodePort * FindPort(PortId portId)
static ComponentNode FromJson(const json &data)
void SetProperty(const std::string &key, const std::string &value)
void InitializePorts(uint32_t numInputPorts=1, uint32_t numOutputPorts=1)
bool operator==(const ComponentNode &other) const
ComponentNodeStyle style
std::string ToDisplayString() const
static NodePort FromJson(const json &data, NodeId nodeId)