Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
VisualScriptRenderer.cpp
Go to the documentation of this file.
1/**
2 * @file VisualScriptRenderer.cpp
3 * @brief IGraphRenderer adapter that wraps VisualScriptEditorPanel.
4 * @author Olympe Engine
5 * @date 2026-03-11
6 *
7 * @details C++14 compliant.
8 */
9
11
12#include "../TaskSystem/TaskGraphLoader.h"
13#include "BTtoVSMigrator.h"
14#include "../third_party/nlohmann/json.hpp"
15#include "../system/system_utils.h"
16#include "../DataManager.h"
17
18#include <fstream>
19#include <iostream>
20#include <vector>
21#include <string>
22
23namespace Olympe {
24
29
34
39
40bool VisualScriptRenderer::Load(const std::string& path)
41{
42 if (path.empty())
43 return false;
44
45 // Phase 38: Resolve relative paths using DataManager
46 std::string resolvedPath = ResolvePath(path);
47
49 {
50 std::ifstream ifs(resolvedPath.c_str());
51 if (!ifs.good())
52 {
53 SYSTEM_LOG << "[VisualScriptRenderer] Cannot open file: " << resolvedPath << "\n";
54 return false;
55 }
56 try
57 {
58 ifs >> fileJson;
59 }
60 catch (...)
61 {
62 SYSTEM_LOG << "[VisualScriptRenderer] JSON parse error: " << resolvedPath << "\n";
63 return false;
64 }
65 }
66
67 if (!fileJson.is_object())
68 return false;
69
70 int schemaVersion = 0;
71 std::string graphType;
72 std::string blueprintType;
73
74 if (fileJson.contains("schema_version") && fileJson["schema_version"].is_number())
75 schemaVersion = fileJson["schema_version"].get<int>();
76 if (fileJson.contains("graphType") && fileJson["graphType"].is_string())
77 graphType = fileJson["graphType"].get<std::string>();
78 if (fileJson.contains("blueprintType") && fileJson["blueprintType"].is_string())
79 blueprintType = fileJson["blueprintType"].get<std::string>();
80
81 // VS v4 graph
82 if (schemaVersion == 4 && graphType == "VisualScript")
83 {
84 std::vector<std::string> errors;
86 if (!tmpl)
87 {
88 SYSTEM_LOG << "[VisualScriptRenderer] Failed to parse VS v4 graph: " << resolvedPath << "\n";
89 return false;
90 }
92 delete tmpl;
93 SYSTEM_LOG << "[VisualScriptRenderer] Loaded VS v4 graph: " << resolvedPath << "\n";
94 return true;
95 }
96
97 // Legacy BT v2 — auto-migrate to VS v4
98 if (blueprintType == "BehaviorTree")
99 {
100 std::vector<std::string> errors;
103 SYSTEM_LOG << "[VisualScriptRenderer] Auto-migrated BT v2 -> VS v4: " << resolvedPath << "\n";
104 return true;
105 }
106
107 SYSTEM_LOG << "[VisualScriptRenderer] Unknown graph format in: " << resolvedPath << "\n";
108 return false;
109}
110
111bool VisualScriptRenderer::Save(const std::string& path)
112{
113 if (!path.empty())
114 return m_panel.SaveAs(path);
115 return m_panel.Save();
116}
117
119{
120 return m_panel.IsDirty();
121}
122
124{
125 return "VisualScript";
126}
127
129{
130 return m_panel.GetCurrentPath();
131}
132
133// Phase 38: Path resolution using DataManager enhanced resolver
134std::string VisualScriptRenderer::ResolvePath(const std::string& path) const
135{
136 // Use DataManager's robust path resolution
137 std::string resolved = DataManager::Get().ResolveFilePath(path);
138
139 if (resolved.empty())
140 {
141 SYSTEM_LOG << "[VisualScriptRenderer] Warning: Could not resolve path: " << path << "\n";
142 return path; // Return original, will fail gracefully in Load()
143 }
144
145 return resolved;
146}
147
148// Phase 35.0: Canvas state management
150{
151 // Phase 35.0: VisualScript uses imnodes native pan/zoom which is context-global
152 // For now, viewport state is preserved via ImNodes::EditorContext
153 // This stub ensures compatibility with the IGraphRenderer interface
154}
155
157{
158 // Phase 35.0: VisualScript pan/zoom restoration (handled by ImNodes context)
159 // This stub ensures compatibility with the IGraphRenderer interface
160}
161
163{
164 // Return empty for now - can be extended to persist canvas state in JSON files
165 return "";
166}
167
169{
170 // Parse and restore from JSON - can be extended for persistence
171 (void)json;
172}
173
174} // namespace Olympe
Converts legacy BT v2 JSON graphs to ATS VS v4 JSON format (Phase 6).
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
IGraphRenderer adapter that wraps VisualScriptEditorPanel.
static DataManager & Get()
Definition DataManager.h:91
std::string ResolveFilePath(const std::string &relativePath) const
static TaskGraphTemplate Convert(const nlohmann::json &btV2Json, std::vector< std::string > &outErrors)
Converts a BT v2 JSON document to a TaskGraphTemplate.
static TaskGraphTemplate * LoadFromJson(const json &data, std::vector< std::string > &outErrors)
Loads a TaskGraphTemplate from an already-parsed JSON object.
Immutable, shareable task graph asset.
bool Save()
Saves the current canvas state to JSON v4 at the loaded path.
void Shutdown()
Shutdown the editor panel and release all resources.
void Initialize()
Initialize the editor panel with ImNodes context and UI helpers.
void RenderContent()
Renders panel content without window wrapper - for fixed layout.
const std::string & GetCurrentPath() const
Returns the currently loaded file path (empty if unsaved).
bool IsDirty() const
Returns true when there are unsaved modifications.
bool SaveAs(const std::string &path)
Saves the current canvas state to a new JSON v4 file.
void LoadTemplate(const TaskGraphTemplate *tmpl, const std::string &path)
Loads a VS graph template into the editor canvas.
void Render() override
Renders the graph canvas into the current ImGui child window.
std::string GetCanvasStateJSON() const override
Get canvas state as JSON string for persistence.
bool Load(const std::string &path) override
Loads a graph from a file on disk.
std::string GetCurrentPath() const override
Returns the last path successfully loaded/saved, or empty string.
void RestoreCanvasState() override
Restore previously saved canvas viewport state Called when tab is reactivated.
bool Save(const std::string &path) override
Saves the current graph state to disk.
void SaveCanvasState() override
Save the current canvas viewport state (pan, zoom, etc.) Called when tab is deactivated.
std::string ResolvePath(const std::string &path) const
bool IsDirty() const override
Returns true when the graph has unsaved changes.
std::string GetGraphType() const override
Returns the graph type string, e.g.
void SetCanvasStateJSON(const std::string &json) override
Restore canvas state from JSON string.
VisualScriptEditorPanel m_panel
< Provides AssetID and INVALID_ASSET_ID
nlohmann::json json
nlohmann::json json
#define SYSTEM_LOG