Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
TemplateBrowserPanel.cpp
Go to the documentation of this file.
1/*
2 * Olympe Blueprint Editor - Template Browser Panel Implementation
3 */
4
6#include "BlueprintEditor.h"
7#include "TemplateManager.h"
8#include "../third_party/imgui/imgui.h"
9#include <iostream>
10#include <cstring>
11#include "../third_party/nlohmann/json.hpp"
12
13// Si vous utilisez using namespace
15
16namespace Olympe
17{
19 : m_ShowPanel(true)
20 , m_ShowSaveAsTemplateModal(false)
21 , m_SelectedCategoryIndex(0)
22 {
23 m_SearchBuffer[0] = '\0';
24 m_TemplateNameBuffer[0] = '\0';
27 }
28
32
34 {
35 // Initialize panel state
36 m_ShowPanel = true;
37 }
38
40 {
41 // Cleanup
42 }
43
53
55 {
56 ImGui::Begin("Template Browser", &m_ShowPanel);
57
59 if (!templateManager.IsInitialized())
60 {
61 ImGui::Text("Template Manager not initialized");
62 ImGui::End();
63 return;
64 }
65
66 // Toolbar buttons
67 if (ImGui::Button("Save Current as Template"))
68 {
70 }
71
72 ImGui::SameLine();
73 if (ImGui::Button("Refresh"))
74 {
76 }
77
78 ImGui::Separator();
79
80 // Search bar
81 ImGui::InputText("Search", m_SearchBuffer, sizeof(m_SearchBuffer));
82
83 // Category filter
84 std::vector<std::string> categories = templateManager.GetAllCategories();
85
86 // Build category list with "All" at the beginning
87 std::vector<const char*> categoryNames;
88 categoryNames.push_back("All");
89 for (const auto& cat : categories)
90 {
91 categoryNames.push_back(cat.c_str());
92 }
93
94 if (ImGui::Combo("Category", &m_SelectedCategoryIndex, categoryNames.data(),
95 static_cast<int>(categoryNames.size())))
96 {
97 // Category changed
98 }
99
100 ImGui::Separator();
101
102 // Template list
103 auto& templates = templateManager.GetAllTemplates();
104
105 if (templates.empty())
106 {
107 ImGui::TextColored(ImVec4(0.7f, 0.7f, 0.7f, 1.0f),
108 "No templates available. Create one using 'Save Current as Template'");
109 }
110 else
111 {
112 // Filter templates
113 std::string searchStr = m_SearchBuffer;
115 ? categories[m_SelectedCategoryIndex - 1] : "";
116
117 for (const auto& tpl : templates)
118 {
119 // Apply search filter
120 if (!searchStr.empty() && tpl.name.find(searchStr) == std::string::npos)
121 {
122 continue;
123 }
124
125 // Apply category filter
126 if (!selectedCategory.empty() && tpl.category != selectedCategory)
127 {
128 continue;
129 }
130
131 ImGui::PushID(tpl.id.c_str());
132
133 // Selectable template item
134 bool isSelected = (m_SelectedTemplateId == tpl.id);
135 if (ImGui::Selectable(tpl.name.c_str(), isSelected))
136 {
138 }
139
140 // Tooltip with details
141 if (ImGui::IsItemHovered())
142 {
143 ImGui::BeginTooltip();
144 ImGui::Text("Name: %s", tpl.name.c_str());
145 ImGui::Text("Description: %s", tpl.description.c_str());
146 ImGui::Text("Category: %s", tpl.category.c_str());
147 ImGui::Text("Author: %s", tpl.author.c_str());
148 ImGui::Text("Version: %s", tpl.version.c_str());
149 ImGui::EndTooltip();
150 }
151
152 // Context menu
153 if (ImGui::BeginPopupContextItem())
154 {
155 if (ImGui::MenuItem("Apply to Current Blueprint"))
156 {
157 if (BlueprintEditor::Get().ApplyTemplate(tpl.id))
158 {
159 std::cout << "Applied template: " << tpl.name << std::endl;
160 }
161 else
162 {
163 std::cerr << "Failed to apply template: " << tpl.name << std::endl;
164 }
165 }
166
167 ImGui::Separator();
168
169 if (ImGui::MenuItem("Delete"))
170 {
171 if (BlueprintEditor::Get().DeleteTemplate(tpl.id))
172 {
173 std::cout << "Deleted template: " << tpl.name << std::endl;
174 if (m_SelectedTemplateId == tpl.id)
175 {
176 m_SelectedTemplateId.clear();
177 }
178 }
179 else
180 {
181 std::cerr << "Failed to delete template: " << tpl.name << std::endl;
182 }
183 }
184
185 ImGui::EndPopup();
186 }
187
188 ImGui::PopID();
189 }
190 }
191
192 ImGui::Separator();
193
194 // Action buttons
195 ImGui::BeginDisabled(m_SelectedTemplateId.empty());
196 if (ImGui::Button("Apply Selected Template", ImVec2(200, 0)))
197 {
198 if (!m_SelectedTemplateId.empty())
199 {
200 if (BlueprintEditor::Get().ApplyTemplate(m_SelectedTemplateId))
201 {
202 std::cout << "Applied template: " << m_SelectedTemplateId << std::endl;
203 }
204 else
205 {
206 std::cerr << "Failed to apply template" << std::endl;
207 }
208 }
209 }
210 ImGui::EndDisabled();
211
212 ImGui::End();
213 }
214
216 {
218 {
219 return;
220 }
221
222 ImGui::OpenPopup("Save as Template");
223
224 ImVec2 center = ImGui::GetMainViewport()->GetCenter();
225 ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
226
227 if (ImGui::BeginPopupModal("Save as Template", &m_ShowSaveAsTemplateModal,
229 {
230 ImGui::Text("Create a new template from the current blueprint");
231 ImGui::Separator();
232
233 ImGui::InputText("Template Name", m_TemplateNameBuffer, sizeof(m_TemplateNameBuffer));
234 ImGui::InputTextMultiline("Description", m_TemplateDescriptionBuffer,
235 sizeof(m_TemplateDescriptionBuffer), ImVec2(400, 100));
236 ImGui::InputText("Category", m_TemplateCategoryBuffer, sizeof(m_TemplateCategoryBuffer));
237
238 ImGui::Separator();
239
240 if (ImGui::Button("Save", ImVec2(120, 0)))
241 {
242 if (std::strlen(m_TemplateNameBuffer) > 0)
243 {
244 if (BlueprintEditor::Get().SaveCurrentAsTemplate(
248 {
249 std::cout << "Template saved successfully: " << m_TemplateNameBuffer << std::endl;
250
251 // Clear buffers
252 m_TemplateNameBuffer[0] = '\0';
255
257 }
258 else
259 {
260
261 }
262 {
263 std::cerr << "Failed to save template: "
264 << BlueprintEditor::Get().GetLastError() << std::endl;
265 }
266 }
267 }
268
269 ImGui::SameLine();
270 if (ImGui::Button("Cancel", ImVec2(120, 0)))
271 {
273 }
274
275 ImGui::EndPopup();
276 }
277 }
278}
nlohmann::json json
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
std::string GetLastError() const
static BlueprintEditor & Get()
static TemplateManager & Get()
nlohmann::json json