Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
DynamicDataPin.cpp
Go to the documentation of this file.
1/**
2 * @file DynamicDataPin.cpp
3 * @brief Implementation of DynamicDataPin — construction, helpers, serialization, UUID gen.
4 * @author Olympe Engine
5 * @date 2026-03-16
6 *
7 * C++14 compliant — no std::optional, structured bindings, std::filesystem.
8 */
9
10#include "DynamicDataPin.h"
11
12#include <cstdint>
13#include <ctime>
14#include <sstream>
15#include <iomanip>
16#include <string>
17
18// ---------------------------------------------------------------------------
19// Pseudo-random seed helpers (C++14 compatible, no <random> state objects
20// stored in static locals to keep thread-safety simple for a single-threaded
21// editor context).
22// ---------------------------------------------------------------------------
23#include <cstdlib>
24
25namespace Olympe {
26
27// ---------------------------------------------------------------------------
28// Default constructor
29// ---------------------------------------------------------------------------
30
32 : id(GenerateUniqueID())
33 , conditionIndex(0)
34 , position(OperandPosition::Left)
35 , label("")
36 , nodePinID("")
37 , dataValue(0.0f)
38 , sequenceNumber(0)
39{
40}
41
42// ---------------------------------------------------------------------------
43// Convenience constructor
44// ---------------------------------------------------------------------------
45
47 const std::string& condPreview)
48 : id(GenerateUniqueID())
49 , conditionIndex(condIdx)
50 , position(pos)
51 , nodePinID("")
52 , dataValue(0.0f)
53 , sequenceNumber(0)
54{
55 // Build the label using the same format that GetDisplayLabel() produces.
56 std::string side = (pos == OperandPosition::Left) ? "L" : "R";
57 std::ostringstream oss;
58 oss << "In #" << (condIdx + 1) << side << ": " << condPreview;
59 label = oss.str();
60}
61
62// ---------------------------------------------------------------------------
63// Helpers
64// ---------------------------------------------------------------------------
65
67{
68 return label;
69}
70
72{
73 std::ostringstream oss;
74 oss << "Pin-in #" << sequenceNumber;
75 return oss.str();
76}
77
78/*static*/
80{
81 // Simple UUID v4-style generator using stdlib rand().
82 // In a production context a proper UUID library would be preferred, but
83 // this suffices for C++14 in a single-threaded editor tool.
84 static bool seeded = false;
85 if (!seeded)
86 {
87 std::srand(static_cast<unsigned int>(std::time(nullptr)));
88 seeded = true;
89 }
90
91 // Generate 16 random bytes and format as xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
92 unsigned int b[16];
93 for (int i = 0; i < 16; ++i)
94 b[i] = static_cast<unsigned int>(std::rand()) & 0xFF;
95
96 // Set version (4) and variant bits
97 b[6] = (b[6] & 0x0F) | 0x40;
98 b[8] = (b[8] & 0x3F) | 0x80;
99
100 std::ostringstream oss;
101 oss << std::hex << std::setfill('0');
102
103 for (int i = 0; i < 16; ++i)
104 {
105 if (i == 4 || i == 6 || i == 8 || i == 10)
106 oss << '-';
107 oss << std::setw(2) << b[i];
108 }
109
110 return "pin_" + oss.str();
111}
112
113// ---------------------------------------------------------------------------
114// Serialization
115// ---------------------------------------------------------------------------
116
118{
119 nlohmann::json j = nlohmann::json::object();
120 j["id"] = id;
121 j["conditionIndex"] = conditionIndex;
122 j["position"] = (position == OperandPosition::Left) ? "Left" : "Right";
123 j["label"] = label;
124 j["nodePinID"] = nodePinID;
125 j["dataValue"] = dataValue;
126 return j;
127}
128
129/*static*/
131{
133
134 if (!data.is_object())
135 return pin;
136
137 if (data.contains("id") && data["id"].is_string())
138 pin.id = data["id"].get<std::string>();
139
140 if (data.contains("conditionIndex") && data["conditionIndex"].is_number_integer())
141 pin.conditionIndex = data["conditionIndex"].get<int>();
142
143 if (data.contains("position") && data["position"].is_string())
144 {
145 std::string pos = data["position"].get<std::string>();
146 pin.position = (pos == "Right") ? OperandPosition::Right : OperandPosition::Left;
147 }
148
149 if (data.contains("label") && data["label"].is_string())
150 pin.label = data["label"].get<std::string>();
151
152 if (data.contains("nodePinID") && data["nodePinID"].is_string())
153 pin.nodePinID = data["nodePinID"].get<std::string>();
154
155 if (data.contains("dataValue") && data["dataValue"].is_number())
156 pin.dataValue = data["dataValue"].get<float>();
157
158 return pin;
159}
160
161} // namespace Olympe
Defines DynamicDataPin — a runtime data-input pin attached to a node.
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
< Provides AssetID and INVALID_ASSET_ID
OperandPosition
Identifies which operand side a DynamicDataPin serves.
@ Right
Pin provides data for the right operand of the condition.
@ Left
Pin provides data for the left operand of the condition.
nlohmann::json json
A data-input pin created dynamically for a Pin-mode operand.
static DynamicDataPin FromJson(const nlohmann::json &data)
Deserializes a DynamicDataPin from a JSON object.
int sequenceNumber
1-based sequence index across all pins on the node (for "Pin-in #N" label)
int conditionIndex
Index in node.conditions (0-based)
nlohmann::json ToJson() const
Serializes this pin to a JSON object.
OperandPosition position
Left or Right operand side.
std::string GetDisplayLabel() const
Returns the full display label shown on the node.
static std::string GenerateUniqueID()
Generates a globally unique identifier string (UUID v4-style).
float dataValue
Runtime float value received from connected node.
std::string GetShortLabel() const
Returns the short label for use as a pin connector slot.
std::string id
Global unique UUID (e.g. "pin_inst_abc123")
std::string nodePinID
ImGui/ImNodes pin ID (assigned at render time)
std::string label
Display label (set at construction)
DynamicDataPin()
Default constructor — zero-initialised.