载入中...
搜索中...
未找到
EditorHostCapabilities.cpp
浏览该文件的文档.
1#include <string>
2#include "common/Capability.h"
3#include "common/EditorHost.h"
4#include "ui/EditorHost.h"
5#include "ui/EditorShell.h"
6#include "ui/UIHost.h"
7
8// EditorHostCapabilities.cpp
9namespace eve::ui {
10namespace {
11
12class EditorHostImpl final : public eve::IEditorHost {
13public:
14 std::string status() const override { return EditorHost::instance().status(); }
15 std::string openWindow(const std::string &title, int width, int height) override {
17 }
18 std::string closeWindow() override { return EditorHost::instance().closeWindow(); }
19 std::string windowState() const override { return EditorHost::instance().windowState(); }
20 bool isWindowOpen() const override { return EditorHost::instance().isWindowOpen(); }
21 std::string applyEditor(const std::string &json) override { return EditorHost::instance().applyEditor(json); }
22 std::string removeEditor(const std::string &id) override { return EditorHost::instance().removeEditor(id); }
23 std::string listEditors() const override { return EditorHost::instance().listEditors(); }
24 std::string editorState(const std::string &id) const override { return EditorHost::instance().editorState(id); }
25 std::string setEditorValue(const std::string &editorId, const std::string &widgetId,
26 const std::string &value) override {
27 return EditorHost::instance().setEditorValue(editorId, widgetId, value);
28 }
29 std::string consumeEvents(const std::string &editorId) override {
30 return EditorHost::instance().consumeEvents(editorId);
31 }
32 std::string widgetRect(const std::string &editorId, const std::string &widgetId) const override {
33 return EditorHost::instance().widgetRect(editorId, widgetId);
34 }
35 std::string registerVM(const std::string &name, const std::string &source) override {
37 }
38 std::string unregisterVM(const std::string &name) override { return EditorHost::instance().unregisterVM(name); }
39 std::string saveEditor(const std::string &id) override { return EditorHost::instance().saveEditor(id); }
40 std::string unloadEditor(const std::string &id) override { return EditorHost::instance().unloadEditor(id); }
41 std::string capture(const std::string &path) override { return EditorHost::instance().capture(path); }
42 std::string runScript(const std::string &source) override { return EditorHost::instance().runScript(source); }
43 std::string reloadResource(const std::string &path) override { return EditorHost::instance().reloadResource(path); }
44 std::string hotReloadStatus() const override { return EditorHost::instance().hotReloadStatus(); }
45 void setHotReloadWatchCount(int count) override { EditorHost::instance().setHotReloadWatchCount(count); }
46 void requestExit() override { EditorHost::instance().requestExit(); }
47 bool exitRequested() override { return EditorHost::instance().exitRequested(); }
48 void frame() override { EditorHost::instance().frame(); }
49 void stop() override { EditorHost::instance().stop(); }
50 void start(ssq::VM &vm, const std::string &gameRoot, bool allowWindow) override {
51 EditorHost::instance().start(vm, gameRoot, allowWindow);
52 }
53 void exposeScriptApi(ssq::VM &vm) override { EditorHost::instance().exposeScriptApi(vm); }
54};
55
56} // namespace
57
59 static EditorHostImpl impl;
60 eve::cap::provide<eve::IEditorHost>(&impl);
61}
62
63} // namespace eve::ui
64
65// EditorShell.cpp
66namespace eve::ui {
67namespace {
68
69constexpr const char *kShellHostName = "eve_editor";
70
71void setHostVisible(UIHostHandle handle, bool visible) {
73 if (host) host->get().setVisible(visible);
74}
75
76} // namespace
77
79 auto host = UIHost::resolve(host_);
80 if (host) host->get().setTree(window("", {}));
81}
82
83void EditorShell::open(UIHostHandle inspector, UIHostHandle database, UIHostHandle scene) {
84 inspector_ = inspector;
85 database_ = database;
86 scene_ = scene;
87 auto host = UIHost::resolve(host_);
88 if (!host) {
89 host_ = UIHost::createHost(kShellHostName);
90 host = UIHost::resolve(host_);
91 }
92 if (!host) return;
93 UIHost &shell = host->get();
94 auto meta = shell.meta();
95 meta->overlay = true; // no title bar
96 meta->hasPos = true;
97 meta->lockPos = true;
98 meta->posX = 0.f;
99 meta->posY = 0.f;
100 meta->hasSize = true;
101 meta->sizeX = 0.f; // width = full display (percentW below)
102 meta->sizeY = toolbarHeight_;
103 meta->lockSize = true;
104 meta->percentW = 1.f;
105 meta->visible = true;
106 meta->overlayBgAlpha = 1.f;
107 meta->overlayFlush = true;
108 shell.setTree(build());
109 relayout();
110 setHostVisible(inspector_, true);
111 setHostVisible(database_, true);
112 setHostVisible(scene_, true);
113}
114
116 setHostVisible(host_, false);
117 setHostVisible(inspector_, false);
118 setHostVisible(database_, false);
119 setHostVisible(scene_, false);
120}
121
123 auto host = UIHost::resolve(host_);
124 return host && host->get().meta()->visible;
125}
126
127bool EditorShell::selectPanel(const std::string &name) {
128 auto host = UIHost::resolve(host_);
129 if (!host || !host->get().meta()->visible) return false;
130 if (name == "inspector") {
131 setHostVisible(inspector_, true);
132 setHostVisible(database_, false);
133 setHostVisible(scene_, false);
134 return true;
135 }
136 if (name == "database") {
137 setHostVisible(database_, true);
138 setHostVisible(inspector_, false);
139 setHostVisible(scene_, false);
140 return true;
141 }
142 if (name == "scene") {
143 setHostVisible(scene_, true);
144 setHostVisible(inspector_, false);
145 setHostVisible(database_, false);
146 return true;
147 }
148 if (name.empty()) {
149 setHostVisible(inspector_, false);
150 setHostVisible(database_, false);
151 setHostVisible(scene_, false);
152 return true;
153 }
154 return false;
155}
156
157bool EditorShell::togglePanel(const std::string &name) {
158 auto host = UIHost::resolve(host_);
159 if (!host || !host->get().meta()->visible) return false;
161 if (name == "inspector") handle = inspector_;
162 if (name == "database") handle = database_;
163 if (name == "scene") handle = scene_;
164 auto panel = UIHost::resolve(handle);
165 if (!panel) return false;
166 UIHost &panelHost = panel->get();
167 panelHost.setVisible(!panelHost.meta()->visible);
168 return true;
169}
170
172 std::vector<WidgetDesc> commands;
173 commands.push_back(icon(Icon::Cubes, "brand_icon"));
174 commands.push_back(text("EVEngine Editor", "lbl_title"));
175 commands.push_back(badge("Workspace", "workspace_badge"));
176 commands.push_back(spacer("menu_spacer"));
177 commands.push_back(iconButton(Icon::Sliders, "", "menu_inspector", [this]() {
178 togglePanel("inspector");
179 }).withTooltip("Show or hide the Inspector"));
180 commands.push_back(iconButton(Icon::Database, "", "menu_database", [this]() {
181 togglePanel("database");
182 }).withTooltip("Show or hide the object database"));
183 commands.push_back(iconButton(Icon::Layers, "", "menu_scene", [this]() {
184 togglePanel("scene");
185 }).withTooltip("Show or hide the scene hierarchy"));
186 commands.push_back(iconButton(Icon::EyeSlash, "", "menu_close", [this]() {
187 selectPanel("");
188 }).withTooltip("Hide all editor panels"));
189 return window("Editor", {toolbar(std::move(commands), "editor_toolbar")}, "root");
190}
191
192void EditorShell::relayout() {
193 // Seed a responsive three-column workspace once. lockPos/lockSize are
194 // disabled so subsequent user adjustments are stored/restored by ImGui's
195 // standard ini workspace persistence.
196 if (auto inspector = UIHost::resolve(inspector_)) {
197 auto meta = inspector->get().meta();
198 meta->hasPos = true;
199 meta->posX = 0.f;
200 meta->posY = toolbarHeight_;
201 meta->anchorX = 0.f;
202 meta->anchorY = 0.f;
203 meta->lockPos = false;
204 meta->hasSize = true;
205 meta->sizeX = 0.f;
206 meta->sizeY = 0.f;
207 meta->percentW = 0.3f;
208 meta->percentH = 0.9f;
209 meta->lockSize = false;
210 }
211 if (auto database = UIHost::resolve(database_)) {
212 auto meta = database->get().meta();
213 meta->hasPos = true;
214 meta->posX = 0.f;
215 meta->posY = toolbarHeight_;
216 meta->anchorX = 0.3f;
217 meta->anchorY = 0.f;
218 meta->lockPos = false;
219 meta->hasSize = true;
220 meta->sizeX = 0.f;
221 meta->sizeY = 0.f;
222 meta->percentW = 0.35f;
223 meta->percentH = 0.9f;
224 meta->lockSize = false;
225 }
226 if (auto scene = UIHost::resolve(scene_)) {
227 auto meta = scene->get().meta();
228 meta->hasPos = true;
229 meta->posX = 0.f;
230 meta->posY = toolbarHeight_;
231 meta->anchorX = 0.65f;
232 meta->anchorY = 0.f;
233 meta->lockPos = false;
234 meta->hasSize = true;
235 meta->sizeX = 0.f;
236 meta->sizeY = 0.f;
237 meta->percentW = 0.35f;
238 meta->percentH = 0.9f;
239 meta->lockSize = false;
240 }
241}
242
243} // namespace eve::ui
Duration start
void * impl
std::string title
HSQUIRRELVM vm
Definition ECS.cpp:38
std::string name
Definition ECS.cpp:154
wgpu::PopErrorScopeStatus status
Icon icon
Definition Icons.cpp:11
std::uint32_t height
std::uint32_t width
ssq::Object frame
std::string path
Definition PlayHost.cpp:111
std::weak_ptr< PrimitiveScene > scene
PrimitiveHandle handle
Object::Ptr value
std::string text
std::string_view source
int count
bool visible
UIHostHandle host
std::vector< VegetationPresetCommand > commands
Declarative editor-host control (provided by the ui module).
Definition EditorHost.h:17
void stop()
Stops .
void setHotReloadWatchCount(int count)
Set active project watch count reported by hotReloadStatus().
std::string status() const
Status.
std::string consumeEvents(const std::string &editorId)
Read and clear pending interaction events for one editor ("" = all).
void requestExit()
Request exit.
Definition EditorHost.h:122
std::string closeWindow()
Closes window.
std::string listEditors() const
List editors.
std::string unregisterVM(const std::string &name)
Unregisters vm.
bool isWindowOpen() const
True when window open.
std::string setEditorValue(const std::string &editorId, const std::string &widgetId, const std::string &jsonValue)
Sets the editor value.
std::string capture(const std::string &path)
Capture.
std::string applyEditor(const std::string &json)
JSON object/string: {id,title,vm,x,y,width,height,theme,children[]}.
std::string unloadEditor(const std::string &id)
Remove an editor from the session (files stay on disk).
std::string windowState() const
Window state.
std::string editorState(const std::string &id) const
Full state JSON: editors + values + (non-destructive) events.
void frame()
Pump events + update/render hooks + present (no-op without window).
std::string removeEditor(const std::string &id)
Removes editor.
std::string hotReloadStatus() const
Watch count, reload counters and latest diagnostic as compact JSON.
static EditorHost & instance()
Instance.
std::string openWindow(const std::string &title, int width, int height)
Opens window.
std::string registerVM(const std::string &name, const std::string &source, const std::string &sourceName={})
Compile Squirrel source in the host VM, then register table name.
std::string widgetRect(const std::string &editorId, const std::string &widgetId) const
Last-frame widget rect JSON (for script drawing inside viewports).
std::string reloadResource(const std::string &path)
Reload mcp.nut, scripts under mcp, or editor VM and manifest files.
bool exitRequested() const
Exit requested.
Definition EditorHost.h:124
std::string saveEditor(const std::string &id)
Write editors/<id>.editor.json + editors/<id>.vm.nut under gameRoot.
void start(ssq::VM &vm, const std::string &gameRoot={}, bool allowWindow=true)
Attach to the host VM. gameRoot is the project dir for editors/.
void exposeScriptApi(ssq::VM &vm)
Expose eve.host table (registerVM/unregisterVM/widgetRect/...).
std::string runScript(const std::string &source, const std::string &sourceName={})
Compile and run Squirrel source in the host VM.
bool togglePanel(const std::string &name)
Toggles one panel without hiding the other docked panels.
bool selectPanel(const std::string &name)
Shows one panel and hides the others.
void close()
Hides the menu bar and every docked panel.
UIHostHandle host() const noexcept
Returns the menu-bar host handle, or an empty handle until open().
Definition EditorShell.h:42
bool isOpen() const
True while the shell is mounted and visible.
void open(UIHostHandle inspector, UIHostHandle database, UIHostHandle scene)
Mounts the menu bar and docks the given panel hosts.
WidgetDesc build()
Declarative tree of the menu bar.
ECS mount point for one UI panel/screen. Subclass to attach UI to game entities, e....
Definition UIHost.h:284
void setTree(WidgetDesc root)
Full replace.
Definition UIHost.cpp:100
void setVisible(bool v)
Host visibility / layer / modality.
Definition UIHost.h:498
static UIHostHandle createHost(const std::string &name="")
Creates an ECS-owned UI host.
Definition UIHost.cpp:49
static eve::OptionalRef< UIHost > resolve(UIHostHandle handle) noexcept
Resolves a UI host handle with ECS generation checking.
Definition UIHost.cpp:62
void registerEditorHostCapabilities()
Registers editor host capabilities.
WidgetDesc badge(std::string label, std::string id)
Compact status/category pill. Tint controls its background color.
Definition Widget.cpp:581
WidgetDesc window(std::string title, std::vector< WidgetDesc > children, std::string id)
Top-level window widget with a title bar.
Definition Widget.cpp:326
WidgetDesc spacer(std::string id, float grow)
Flexible empty space; default flexGrow=1 so it absorbs free space in a Flex parent.
Definition Widget.cpp:888
WidgetDesc toolbar(std::vector< WidgetDesc > children, std::string id)
Horizontal editor command strip; Spacer children absorb free width.
Definition Widget.cpp:655
WidgetDesc iconButton(Icon value, std::string label, std::string id, std::function< void()> onClick)
Button containing a semantic icon and optional visible label.
Definition Widget.cpp:357
ecs::EntityHandle UIHostHandle
Definition UIHost.h:20
Declarative widget description (build once / on dirty → flatten into UIHost::Tree).
Definition Widget.h:15