Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
ActionParametersPanel.cpp
Go to the documentation of this file.
1/**
2 * @file ActionParametersPanel.cpp
3 * @brief Implementation of ActionParametersPanel for editing action node parameters.
4 */
5
7#include "../../third_party/imgui/imgui.h"
8#include <algorithm>
9#include <cstring>
10
11namespace Olympe {
12
17
19{
21}
22
23void ActionParametersPanel::SetNodeName(const std::string& name)
24{
25 m_nodeName = name;
26}
27
28void ActionParametersPanel::SetParameters(const std::unordered_map<std::string, std::string>& params)
29{
30 // Rebuild parameter list from provided map
31 m_parameters.clear();
32
33 for (const auto& kv : params)
34 {
36 param.name = kv.first;
37 param.value = kv.second;
38 param.defaultValue = kv.second; // Assume current value is default if not specified
39 param.isDirty = false;
40
41 // Guess type from value (simple heuristic)
42 if (kv.second == "true" || kv.second == "false")
43 {
44 param.type = "Bool";
45 }
46 else if (std::all_of(kv.second.begin(), kv.second.end(),
47 [](unsigned char c) { return std::isdigit(c) || c == '.'; }))
48 {
49 param.type = (kv.second.find('.') != std::string::npos) ? "Float" : "Int";
50 }
51 else
52 {
53 param.type = "String";
54 }
55
56 m_parameters.push_back(param);
57 }
58
59 m_dirty = false;
60}
61
62const std::vector<ActionParameter>& ActionParametersPanel::GetParameters() const
63{
64 return m_parameters;
65}
66
67std::string ActionParametersPanel::GetParameterValue(const std::string& paramName) const
68{
69 for (const auto& param : m_parameters)
70 {
71 if (param.name == paramName)
72 return param.value;
73 }
74 return "";
75}
76
78{
79 if (m_dirty)
80 return true;
81
82 for (const auto& param : m_parameters)
83 {
84 if (param.isDirty)
85 return true;
86 }
87 return false;
88}
89
91{
92 m_dirty = false;
93 for (auto& param : m_parameters)
94 {
95 param.isDirty = false;
96 }
97}
98
100{
101#ifndef OLYMPE_HEADLESS
102 if (m_taskID.empty())
103 {
104 ImGui::TextDisabled("(no action selected)");
105 return;
106 }
107
109 ImGui::Spacing();
110
111 if (!m_parameters.empty())
112 {
114 }
115 else
116 {
117 ImGui::TextDisabled("(no parameters for this action)");
118 }
119#endif
120}
121
123{
124#ifndef OLYMPE_HEADLESS
125 ImGui::PushStyleColor(ImGuiCol_Header, ImVec4(0.4f, 0.6f, 0.8f, 1.0f));
126 ImGui::Text("Action: %s", m_nodeName.c_str());
127 ImGui::PopStyleColor();
128 ImGui::TextDisabled("Task ID: %s", m_taskID.c_str());
129#endif
130}
131
133{
134#ifndef OLYMPE_HEADLESS
135 ImGui::Separator();
136 ImGui::Text("Parameters:");
137 ImGui::Spacing();
138
139 for (size_t i = 0; i < m_parameters.size(); ++i)
140 {
141 ImGui::PushID(static_cast<int>(i));
143 ImGui::PopID();
144 }
145#endif
146}
147
149{
150#ifndef OLYMPE_HEADLESS
151 ImGui::Indent();
152
153 // Parameter label with type hint
154 std::string label = param.name + " (" + param.type + ")";
155
156 if (param.type == "Bool")
157 {
158 bool value = (param.value == "true" || param.value == "1");
159 if (ImGui::Checkbox(label.c_str(), &value))
160 {
161 param.value = value ? "true" : "false";
162 param.isDirty = true;
163 m_dirty = true;
164 }
165 }
166 else if (param.type == "Int")
167 {
168 int value = 0;
169 try {
170 value = std::stoi(param.value);
171 } catch (...) {
172 // Keep default 0
173 }
174 ImGui::SetNextItemWidth(200.0f);
175 if (ImGui::DragInt(label.c_str(), &value))
176 {
177 param.value = std::to_string(value);
178 param.isDirty = true;
179 m_dirty = true;
180 }
181 }
182 else if (param.type == "Float")
183 {
184 float value = 0.0f;
185 try {
186 value = std::stof(param.value);
187 } catch (...) {
188 // Keep default 0.0f
189 }
190 ImGui::SetNextItemWidth(200.0f);
191 if (ImGui::DragFloat(label.c_str(), &value, 0.1f))
192 {
193 param.value = std::to_string(value);
194 param.isDirty = true;
195 m_dirty = true;
196 }
197 }
198 else if (param.type == "String")
199 {
200 // For path references or generic strings
201 static char buffer[512] = {0};
202 strncpy_s(buffer, param.value.c_str(), sizeof(buffer) - 1);
203 buffer[sizeof(buffer) - 1] = '\0';
204
205 ImGui::SetNextItemWidth(250.0f);
206 if (ImGui::InputText(label.c_str(), buffer, sizeof(buffer)))
207 {
208 param.value = buffer;
209 param.isDirty = true;
210 m_dirty = true;
211 }
212 }
213 else
214 {
215 // Unknown type: display as text
216 ImGui::TextDisabled("%s: %s", label.c_str(), param.value.c_str());
217 }
218
219 ImGui::Unindent();
220#endif
221}
222
223} // namespace Olympe
UI Properties panel for atomic task (action) node parameters.
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
std::vector< ActionParameter > m_parameters
void SetNodeName(const std::string &name)
Set the node name for display purposes.
void RenderParameter(ActionParameter &param)
void SetParameters(const std::unordered_map< std::string, std::string > &params)
Set the parameters from a map of name->value pairs.
std::string GetParameterValue(const std::string &paramName) const
Get a specific parameter value by name.
const std::vector< ActionParameter > & GetParameters() const
Get all parameters with their current values.
void SetActionTaskID(const std::string &taskID)
Set the action task ID (e.g., "log_message", "patrol_path")
bool IsDirty() const
Check if any parameter has been modified.
void ClearDirty()
Clear the dirty flag after changes have been applied.
< Provides AssetID and INVALID_ASSET_ID
Represents a single parameter of an action task.
std::string name
Parameter name (e.g., "message", "speed")