Skip to content

Commit 6ae1b67

Browse files
committed
wip: dockspace api
1 parent 206bd45 commit 6ae1b67

File tree

9 files changed

+213
-49
lines changed

9 files changed

+213
-49
lines changed

include/exgine/datastore.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include <sol/sol.hpp>
4+
5+
#include <string>
6+
#include <unordered_map>
7+
8+
namespace Exgine {
9+
struct DataStore {
10+
std::unordered_map<std::string, sol::object> entries;
11+
12+
void DynamicSet(std::string key, sol::stack_object value);
13+
sol::object DynamicGet(std::string key);
14+
};
15+
} // namespace Exgine

include/exgine/gameobject.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include "exgine/datastore.h"
34
#define SOL_ALL_SAFETIES_ON 1
45
#define GLFW_INCLUDE_NONE
56

@@ -21,6 +22,8 @@ class GameObject {
2122
bool hasTexture = false;
2223
GLuint textureHandle = 0;
2324

25+
DataStore datastore;
26+
2427
glm::vec3 colour = glm::vec3(1.0f);
2528

2629
const glm::vec3 &GetPosition() const { return transform.position; }

include/exgine/lualoader.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#define SOL_ALL_SAFETIES_ON 1
44
#define GLFW_INCLUDE_NONE
55

6+
#include "exgine/datastore.h"
67
#include "exgine/engine.h"
78

89
#include <sol/sol.hpp>
@@ -69,6 +70,8 @@ class LuaLoader {
6970
std::vector<LoadedScript> scripts;
7071
std::vector<std::string> loadedDirectories;
7172

73+
DataStore datastore;
74+
7275
Exgine::Engine *engine;
7376
};
7477
} // namespace Exgine

plugins/editor/dockspace.lua

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
function draw()
2+
local dockspace_flags = ImGui.DockNodeFlags_PassthruCentralNode
3+
local window_flags = ImGuiWindowFlags.MenuBar | ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoCollapse |
4+
ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoBringToFrontOnFocus |
5+
ImGuiWindowFlags.NoNavFocus
6+
7+
local viewport = ImGui.GetMainViewport()
8+
if viewport == nil then
9+
return
10+
end
11+
12+
print("(" .. tostring(viewport.Pos.x) .. ", " .. tostring(viewport.Pos.y) .. ")")
13+
print("(" .. tostring(viewport.Size.x) .. ", " .. tostring(viewport.Size.y) .. ")")
14+
15+
ImGui.SetNextWindowPos(viewport.Pos.x, viewport.Pos.y, ImGuiCond.Always)
16+
ImGui.SetNextWindowSize(viewport.Size.x, viewport.Size.y, ImGuiCond.Always)
17+
ImGui.SetNextWindowViewport(viewport.ID)
18+
19+
ImGui.PushStyleVar(ImGuiStyleVar.WindowRounding, 0.0)
20+
ImGui.PushStyleVar(ImGuiStyleVar.WindowBorderSize, 0.0)
21+
ImGui.PushStyleVar(ImGuiStyleVar.WindowPadding, 0.0, 0.0)
22+
23+
ImGui.Begin("Viewport", true, window_flags)
24+
ImGui.PopStyleVar(3)
25+
26+
local dockspace_id = math.floor(ImGui.GetID("MyDockSpace"))
27+
28+
ImGui.DockSpace(dockspace_id, math.floor(viewport.Size.x), math.floor(viewport.Size.y),
29+
ImGuiDockNodeFlags.DockSpace | ImGuiDockNodeFlags.PassthruCentralNode)
30+
31+
if globals.firstTime then
32+
globals.firstTime = false
33+
warn("Running first time")
34+
35+
ImGui.DockBuilderDockWindow("Viewport", dockspace_id)
36+
37+
local dock_left, dock_right = ImGui.DockBuilderSplitNode(dockspace_id, ImGuiDir.Left, 0.5)
38+
local dock_left_bottom, dock_right_bottom = ImGui.DockBuilderSplitNode(dock_left, ImGuiDir.Down, 0.5)
39+
40+
ImGui.DockBuilderDockWindow("Hierarchy", dock_right)
41+
ImGui.DockBuilderDockWindow("Game tools", dock_right_bottom)
42+
ImGui.DockBuilderFinish(dockspace_id)
43+
end
44+
45+
ImGui.End()
46+
end
47+

plugins/editor/posthook.lua

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,9 @@ end
66

77
function draw()
88
ImGui.Begin("Viewport")
9+
globals.viewportSize = ImGui.ContentRegion()
910

10-
local windowSize = ImGui.ContentRegion()
11-
12-
engine.renderer.width = math.floor(windowSize.x)
13-
engine.renderer.height = math.floor(windowSize.y)
14-
engine.framebuffer:Resize(engine.renderer.width, engine.renderer.height)
15-
16-
ImGui.Image(engine.framebuffer.texture, ImGui.ImVec2(windowSize.x, windowSize.y))
11+
ImGui.Image(engine.framebuffer.texture, ImGui.ImVec2(globals.viewportSize.x, globals.viewportSize.y))
1712

1813
ImGui.End()
1914
end

plugins/editor/prehook.lua

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,23 @@ function ready()
55
end
66

77
function preDraw()
8+
if not globals.viewportSize then
9+
return
10+
end
11+
12+
local width, height = math.floor(globals.viewportSize.x), math.floor(globals.viewportSize.y)
13+
14+
-- Resize the renderer
15+
engine.renderer.width = width
16+
engine.renderer.height = height
17+
18+
-- Resize the framebuffer
19+
engine.framebuffer:Resize(width, height)
20+
821
-- Bind the framebuffer
922
engine.framebuffer:Bind()
1023

1124
-- Clear
12-
utils.clearColor(0.0, 0.0, 0.0, 1.0)
13-
utils.clear()
25+
utils.ClearColor(0.0, 0.0, 0.0, 1.0)
26+
utils.Clear()
1427
end

src/datastore.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "exgine/datastore.h"
2+
3+
using namespace Exgine;
4+
5+
void DataStore::DynamicSet(std::string key, sol::stack_object value) {
6+
auto it = entries.find(key);
7+
if (it == entries.cend()) {
8+
entries.insert(it, {std::move(key), std::move(value)});
9+
} else {
10+
std::pair<const std::string, sol::object> &kvp = *it;
11+
sol::object &entry = kvp.second;
12+
entry = sol::object(std::move(value));
13+
}
14+
}
15+
16+
sol::object DataStore::DynamicGet(std::string key) {
17+
auto it = entries.find(key);
18+
if (it == entries.cend()) {
19+
return sol::lua_nil;
20+
}
21+
22+
return it->second;
23+
}

src/lualoader.cpp

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,60 @@ LuaLoader::LuaLoader(Engine *engine) : engine(engine) {
2929
sol::lib::math);
3030

3131
sol_ImGui::Init(this->lua);
32-
3332
auto igt = this->lua["ImGui"];
3433

34+
lua.new_enum("ImGuiDockNodeFlags", ENUM_HELPER(ImGuiDockNodeFlags, None),
35+
ENUM_HELPER(ImGuiDockNodeFlags, KeepAliveOnly),
36+
ENUM_HELPER(ImGuiDockNodeFlags, NoDockingOverCentralNode),
37+
ENUM_HELPER(ImGuiDockNodeFlags, PassthruCentralNode),
38+
ENUM_HELPER(ImGuiDockNodeFlags, NoDockingSplit),
39+
ENUM_HELPER(ImGuiDockNodeFlags, NoResize),
40+
ENUM_HELPER(ImGuiDockNodeFlags, AutoHideTabBar),
41+
ENUM_HELPER(ImGuiDockNodeFlags, NoUndocking),
42+
ENUM_HELPER(ImGuiDockNodeFlags, CentralNode),
43+
ENUM_HELPER(ImGuiDockNodeFlags, NoSplit),
44+
ENUM_HELPER(ImGuiDockNodeFlags, NoTabBar),
45+
ENUM_HELPER(ImGuiDockNodeFlags, NoCloseButton),
46+
ENUM_HELPER(ImGuiDockNodeFlags, NoDocking),
47+
ENUM_HELPER(ImGuiDockNodeFlags, NoDockingOverMe),
48+
ENUM_HELPER(ImGuiDockNodeFlags, NoDockingOverOther),
49+
ENUM_HELPER(ImGuiDockNodeFlags, PassthruCentralNode),
50+
ENUM_HELPER(ImGuiDockNodeFlags, DockSpace));
51+
52+
/*ImGui::DockBuilderSplitNode(ImGuiID node_id, ImGuiDir split_dir, float
53+
* size_ratio_for_node_at_dir, ImGuiID *out_id_at_dir, ImGuiID
54+
* *out_id_at_opposite_dir)*/
55+
auto dockBuilderSplitNode = [](ImGuiID node_id, ImGuiDir split_dir,
56+
float size_ratio_for_node_at_dir) {
57+
ImGuiID out_id_at_dir{}, out_id_at_opposite_dir{};
58+
ImGui::DockBuilderSplitNode(node_id, split_dir, size_ratio_for_node_at_dir,
59+
&out_id_at_dir, &out_id_at_opposite_dir);
60+
return std::make_tuple(out_id_at_dir, out_id_at_opposite_dir);
61+
};
62+
igt["DockBuilderSplitNode"] = dockBuilderSplitNode;
63+
64+
igt["PushStyleVar"] = sol::overload(
65+
[](int index, float valX) { sol_ImGui::PushStyleVar(index, valX); },
66+
[](int index, float valX, float valY) {
67+
sol_ImGui::PushStyleVar(index, valX, valY);
68+
});
69+
70+
igt["PopStyleVar"] =
71+
sol::overload([](int count) { sol_ImGui::PopStyleVar(count); },
72+
[]() { sol_ImGui::PopStyleVar(); });
73+
3574
igt["ShowDemoWindow"] = &ImGui::ShowDemoWindow;
3675

3776
igt["GetMainViewport"] = &ImGui::GetMainViewport;
77+
78+
lua.new_usertype<ImGuiViewport>(
79+
"ImGuiViewport", sol::no_constructor, "Pos", &ImGuiViewport::Pos, "Size",
80+
&ImGuiViewport::Size, "ID", &ImGuiViewport::ID);
81+
3882
igt["SetNextWindowViewport"] = &ImGui::SetNextWindowViewport;
3983
igt["DockBuilderRemoveNode"] = &ImGui::DockBuilderRemoveNode;
4084
igt["DockBuilderAddNode"] = &ImGui::DockBuilderAddNode;
41-
igt["DockBuilderSplitNode"] = &ImGui::DockBuilderSplitNode;
85+
/*igt["DockBuilderSplitNode"] = &ImGui::DockBuilderSplitNode;*/
4286
igt["DockBuilderDockWindow"] = &ImGui::DockBuilderDockWindow;
4387
igt["DockBuilderFinish"] = &ImGui::DockBuilderFinish;
4488

@@ -73,9 +117,21 @@ LuaLoader::LuaLoader(Engine *engine) : engine(engine) {
73117
};
74118

75119
igt["Image"] = [](int user_texture_id, const ImVec2 &size) {
76-
ImGui::Image((ImTextureID)user_texture_id, size);
120+
ImGui::Image((ImTextureID)user_texture_id, size, ImVec2(0, 1),
121+
ImVec2(1, 0));
77122
};
78123

124+
lua.new_usertype<DataStore>(
125+
"DataStore", sol::no_constructor, sol::meta_function::index,
126+
[](DataStore &ds, const std::string &key) { return ds.DynamicGet(key); },
127+
sol::meta_function::new_index,
128+
[](DataStore &ds, const std::string &key, sol::stack_object value) {
129+
ds.DynamicSet(key, std::move(value));
130+
},
131+
sol::meta_function::length,
132+
[](DataStore &ds) { return ds.entries.size(); });
133+
lua.set("globals", this->datastore);
134+
79135
// Disable GC (PERF: Fix this later)
80136
this->lua.change_gc_mode_generational(0, 0);
81137

@@ -182,7 +238,16 @@ LuaLoader::LuaLoader(Engine *engine) : engine(engine) {
182238
"Translate", &GameObject::Translate, "Scale", &GameObject::Scale,
183239
"Rotate", &GameObject::Rotate, "GetPosition", &GameObject::GetPosition,
184240
"GetScale", &GameObject::GetScale, "GetRotation",
185-
&GameObject::GetRotation, "GetColour", &GameObject::GetColour);
241+
&GameObject::GetRotation, "GetColour", &GameObject::GetColour, "data",
242+
&GameObject::datastore, sol::meta_function::index,
243+
[](GameObject &gobj, const std::string &key) {
244+
return gobj.datastore.DynamicGet(key);
245+
},
246+
247+
sol::meta_function::new_index,
248+
[](GameObject &gobj, const std::string &key, sol::stack_object value) {
249+
gobj.datastore.DynamicSet(key, std::move(value));
250+
});
186251

187252
lua.new_usertype<Scene::GameObjectPair>("GameObjectPair", sol::no_constructor,
188253
"name", &Scene::GameObjectPair::name,
@@ -439,12 +504,12 @@ LuaLoader::LuaLoader(Engine *engine) : engine(engine) {
439504
auto utilsTable = this->lua.create_named_table("utils");
440505

441506
utilsTable.set_function(
442-
"clearColor",
507+
"ClearColor",
443508
[&](float r, float g, float b, float a) { glClearColor(r, g, b, a); });
444509

445-
utilsTable.set_function("clear", [&]() { glClear(GL_COLOR_BUFFER_BIT); });
510+
utilsTable.set_function("Clear", [&]() { glClear(GL_COLOR_BUFFER_BIT); });
446511

447-
utilsTable.set_function("getWindowSize", [&]() {
512+
utilsTable.set_function("GetWindowSize", [&]() {
448513
int w, h;
449514
glfwGetWindowSize(glfwGetCurrentContext(), &w, &h);
450515
return glm::vec2(w, h);

src/starter.cpp

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -138,39 +138,39 @@ int main(int argc, char *argv[]) {
138138

139139
ImGui::SetNextWindowPos(ImVec2(32, 32));
140140

141-
ImGui::Begin("Stats", nullptr,
142-
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove |
143-
ImGuiWindowFlags_NoCollapse);
144-
145-
if (ImGui::BeginTable("##statstab", 2)) {
146-
ImGui::TableNextRow();
147-
ImGui::TableNextColumn();
148-
ImGui::Text("FPS");
149-
ImGui::TableNextColumn();
150-
ImGui::Text("%f", ImGui::GetIO().Framerate);
151-
152-
ImGui::TableNextRow();
153-
ImGui::TableNextColumn();
154-
ImGui::Text("FPS AVG");
155-
ImGui::TableNextColumn();
156-
ImGui::Text("%f", 1.0f / dt_avg);
157-
158-
ImGui::TableNextRow();
159-
ImGui::TableNextColumn();
160-
ImGui::Text("DT (µs)");
161-
ImGui::TableNextColumn();
162-
ImGui::Text("%f", ImGui::GetIO().DeltaTime * 1000000);
163-
164-
ImGui::TableNextRow();
165-
ImGui::TableNextColumn();
166-
ImGui::Text("DT AVG (µs)");
167-
ImGui::TableNextColumn();
168-
ImGui::Text("%f", dt_avg * 1000000);
169-
170-
ImGui::EndTable();
171-
}
172-
173-
ImGui::End();
141+
/*ImGui::Begin("Stats", nullptr,*/
142+
/* ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove |*/
143+
/* ImGuiWindowFlags_NoCollapse);*/
144+
/**/
145+
/*if (ImGui::BeginTable("##statstab", 2)) {*/
146+
/* ImGui::TableNextRow();*/
147+
/* ImGui::TableNextColumn();*/
148+
/* ImGui::Text("FPS");*/
149+
/* ImGui::TableNextColumn();*/
150+
/* ImGui::Text("%f", ImGui::GetIO().Framerate);*/
151+
/**/
152+
/* ImGui::TableNextRow();*/
153+
/* ImGui::TableNextColumn();*/
154+
/* ImGui::Text("FPS AVG");*/
155+
/* ImGui::TableNextColumn();*/
156+
/* ImGui::Text("%f", 1.0f / dt_avg);*/
157+
/**/
158+
/* ImGui::TableNextRow();*/
159+
/* ImGui::TableNextColumn();*/
160+
/* ImGui::Text("DT (µs)");*/
161+
/* ImGui::TableNextColumn();*/
162+
/* ImGui::Text("%f", ImGui::GetIO().DeltaTime * 1000000);*/
163+
/**/
164+
/* ImGui::TableNextRow();*/
165+
/* ImGui::TableNextColumn();*/
166+
/* ImGui::Text("DT AVG (µs)");*/
167+
/* ImGui::TableNextColumn();*/
168+
/* ImGui::Text("%f", dt_avg * 1000000);*/
169+
/**/
170+
/* ImGui::EndTable();*/
171+
/*}*/
172+
/**/
173+
/*ImGui::End();*/
174174

175175
ImGui::Render();
176176
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

0 commit comments

Comments
 (0)