Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/MyPlugin/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ static Plugin info {
on_load, on_unload, on_update, on_draw_ui
};

extern "C" __declspec(dllexport) Plugin* get_plugin() { return &info; }
PLUGIN_EXPORT Plugin* get_plugin() { return &info; }
Empty file modified scripts/build_engine.sh
100644 → 100755
Empty file.
Empty file modified scripts/build_plugin.sh
100644 → 100755
Empty file.
2 changes: 2 additions & 0 deletions src/editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ ImGuizmo::OPERATION gizmo_mode = ImGuizmo::TRANSLATE;
int renaming_index = -1;
char rename_buf[128] = "";
bool scene_asset_dragging = false;

std::string dragged_scene_asset_name;
bool file_dragging = false;
int dragged_file_index = -1;
int dragged_target_folder_index = -1;

std::unordered_map<std::string, Texture> tex_cache;
bool show_about_window = false;
bool has_clipboard = false;
Expand Down
3 changes: 3 additions & 0 deletions src/editor/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
#include <unordered_map>

namespace editor_internal {

extern ImGuizmo::OPERATION gizmo_mode;
extern int renaming_index;
extern char rename_buf[128];
extern bool scene_asset_dragging;

extern std::string dragged_scene_asset_name;
extern bool file_dragging;
extern int dragged_file_index;
extern int dragged_target_folder_index;

extern std::unordered_map<std::string, Texture> tex_cache;
extern bool show_about_window;
extern bool has_clipboard;
Expand Down
15 changes: 1 addition & 14 deletions src/editor/editor_assets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,6 @@
#include <sstream>
#include <string>

#ifdef _WIN32
#define NOMINMAX 1
#define WIN32_LEAN_AND_MEAN
#define CloseWindow WinAPICloseWindow
#define ShowCursor WinAPIShowCursor
#define Rectangle WinAPIRectangle
#include <windows.h>
#include <shellapi.h>
#undef CloseWindow
#undef ShowCursor
#undef Rectangle
#endif

#define lang LanguageManager::get()

namespace fs = std::filesystem;
Expand Down Expand Up @@ -572,7 +559,7 @@ void draw_assets_ui(Editor& editor) {

else {
#ifdef _WIN32
ShellExecuteA(nullptr, "open", full_path.string().c_str(), nullptr, nullptr, SW_SHOWNORMAL);
ShellExecuteA(nullptr, "open", full_path.string().c_str(), nullptr, nullptr, 1);
#endif
}
}
Expand Down
17 changes: 13 additions & 4 deletions src/editor/editor_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ void delete_entity(Editor& editor, Entity* entity, Shader shader) {
editor.scene.selected = -1;
}

void draw_ui(Editor& editor, Shader shader, FlyCamera camera) {
void draw_ui(Editor& editor, Shader shader, FlyCamera camera, PluginContext* plugin_ctx) {
using namespace editor_internal;

ImGuizmo::BeginFrame();
Expand All @@ -1152,13 +1152,17 @@ void draw_ui(Editor& editor, Shader shader, FlyCamera camera) {

if (ImGui::BeginMainMenuBar()) {
if (ImGui::BeginMenu(lang.word("file"))) {
editor.plugin_manager->draw_ui_region(UI_MENU_FILE, *plugin_ctx);

if (ImGui::MenuItem(lang.word("save"), "Ctrl+S")) project_save(editor.project_path, editor.scene);
ImGui::Separator();
if (ImGui::MenuItem(lang.word("exit"))) CloseWindow();
ImGui::EndMenu();
}

if (ImGui::BeginMenu(lang.word("edit"))) {
editor.plugin_manager->draw_ui_region(UI_MENU_EDIT, *plugin_ctx);

if (ImGui::MenuItem(lang.word("undo"), "Ctrl+Z")) editor.undo();
if (ImGui::MenuItem(lang.word("redo"), "Ctrl+Y")) editor.redo();

Expand Down Expand Up @@ -1219,6 +1223,8 @@ void draw_ui(Editor& editor, Shader shader, FlyCamera camera) {
}

if (ImGui::BeginMenu(lang.word("help"))) {
editor.plugin_manager->draw_ui_region(UI_MENU_HELP, *plugin_ctx);

if (ImGui::MenuItem(lang.word("about"))) show_about_window = true;
ImGui::EndMenu();
}
Expand All @@ -1230,6 +1236,7 @@ void draw_ui(Editor& editor, Shader shader, FlyCamera camera) {

if (show_hierarchy) {
ImGui::Begin(lang.word("hierarchy"), &show_hierarchy);
editor.plugin_manager->draw_ui_region(UI_HIERARCHY, *plugin_ctx);

auto draw_entity_item = [&](int entity_index) {
Entity& entity = editor.scene.entities[entity_index];
Expand Down Expand Up @@ -1434,6 +1441,7 @@ void draw_ui(Editor& editor, Shader shader, FlyCamera camera) {

if (show_inspector) {
ImGui::Begin(lang.word("inspector"), &show_inspector);
editor.plugin_manager->draw_ui_region(UI_INSPECTOR, *plugin_ctx);

ImGui::Text(lang.word("mode"));
ImGui::SameLine();
Expand Down Expand Up @@ -1484,6 +1492,7 @@ void draw_ui(Editor& editor, Shader shader, FlyCamera camera) {
if (show_scene) {
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
if (ImGui::Begin(lang.word("scene"), &show_scene)) {
editor.plugin_manager->draw_ui_region(UI_INSPECTOR, *plugin_ctx);
g_scene_window_pos = ImGui::GetCursorScreenPos();
g_scene_window_size = ImGui::GetContentRegionAvail();

Expand Down Expand Up @@ -1585,6 +1594,6 @@ void draw_ui(Editor& editor, Shader shader, FlyCamera camera) {
}
}

void Editor::draw_ui(Shader shader, FlyCamera camera) {
::draw_ui(*this, shader, camera);
}
void Editor::draw_ui(Shader shader, FlyCamera camera, PluginContext* ctx) {
::draw_ui(*this, shader, camera, ctx);
}
2 changes: 1 addition & 1 deletion src/editor/editor_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void paste_entity(Editor& editor);
void dublicate_entity(Editor& editor, Entity* entity);
void delete_entity(Editor& editor, Entity* entity, Shader shader);

void draw_ui(Editor& editor, Shader shader, FlyCamera camera);
void draw_ui(Editor& editor, Shader shader, FlyCamera camera, PluginContext* ctx);

void draw_gizmo(Editor& editor, FlyCamera camera);
void handle_scene_asset_drop(Editor& editor, Camera3D camera);
Expand Down
5 changes: 4 additions & 1 deletion src/headers/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "models.h"
#include <stack>
#include <filesystem>
#include <plugin_manager.h>

struct SceneState {
std::vector<Entity> entities;
Expand All @@ -19,9 +20,11 @@ struct Editor {
std::stack<SceneState> undo_stack;
std::stack<SceneState> redo_stack;

PluginManager* plugin_manager = nullptr;

std::filesystem::path current_asset_path;

void draw_ui(Shader shader, FlyCamera camera);
void draw_ui(Shader shader, FlyCamera camera, PluginContext* ctx);
void draw_assets_ui();
void handle_input();
void draw_entity_with_texture(Entity& e);
Expand Down
115 changes: 77 additions & 38 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ Shader shadowmap_shader = {0};
RenderTexture2D shadow_map = {0};
RenderTexture2D scene_rt = {0};

PluginManager* g_plugin_manager = nullptr;
PluginContext* ctx = nullptr;

extern bool g_is_scene_hovered;
extern bool g_is_scene_active;
static Shader shadowcaster_shader = {0};
Expand Down Expand Up @@ -76,40 +79,64 @@ static void reload_editor_fonts(const std::string& language_code) {
io.Fonts->Build();
}

static void init_plugin_context(PluginContext* ctx) {
ctx->ui_begin = [](const char* t) { return ImGui::Begin(t); };
ctx->ui_end = []() { ImGui::End(); };
ctx->ui_begin_menu = [](const char* l) { return ImGui::BeginMenu(l); };
ctx->ui_end_menu = []() { ImGui::EndMenu(); };
ctx->ui_menu_item = [](const char* l) { return ImGui::MenuItem(l); };
ctx->ui_text = [](const char* t) { ImGui::Text("%s", t); };
ctx->ui_button = [](const char* l) { return ImGui::Button(l); };
ctx->ui_checkbox = [](const char* l, bool* v) { return ImGui::Checkbox(l, v); };
ctx->ui_slider_float = [](const char* l, float* v, float mn, float mx) { return ImGui::SliderFloat(l, v, mn, mx); };
ctx->ui_input_float = [](const char* l, float* v) { return ImGui::InputFloat(l, v); };
ctx->ui_color_edit3 = [](const char* l, float c[3]) { return ImGui::ColorEdit3(l, c); };
ctx->ui_separator = []() { ImGui::Separator(); };
ctx->ui_same_line = []() { ImGui::SameLine(); };

ctx->register_ui_callback = [](UIRegion region, PluginUICallback callback) {
if (g_plugin_manager) g_plugin_manager->register_ui_callback(region, callback);
};
}

static void update_plugins(PluginManager& plugin_manager, Editor& editor) {
static Editor* s_editor = nullptr;
s_editor = &editor;

PluginContext ctx;
ctx.delta_time = GetDeltaTime();
ctx.entity_count = (int)s_editor->scene.entities.size();
ctx.selected = &s_editor->scene.selected;

ctx.ui_begin = [](const char* t) { return ImGui::Begin(t); };
ctx.ui_end = []() { ImGui::End(); };
ctx.ui_text = [](const char* t) { ImGui::Text("%s", t); };
ctx.ui_button = [](const char* l) { return ImGui::Button(l); };
ctx.ui_checkbox = [](const char* l, bool* v) { return ImGui::Checkbox(l, v); };
ctx.ui_slider_float = [](const char* l, float* v, float mn, float mx) { return ImGui::SliderFloat(l, v, mn, mx); };
ctx.ui_input_float = [](const char* l, float* v) { return ImGui::InputFloat(l, v); };
ctx.ui_color_edit3 = [](const char* l, float c[3]) { return ImGui::ColorEdit3(l, c); };
ctx.ui_separator = []() { ImGui::Separator(); };
ctx.ui_same_line = []() { ImGui::SameLine(); };

ctx.entity_get_name = [](int i) -> const char* { return s_editor->scene.entities[i].name.c_str(); };
ctx.entity_get_position = [](int i, float* x, float* y, float* z) { if (auto* t = s_editor->scene.entities[i].get_transform_component()) { *x = t->position.x; *y = t->position.y; *z = t->position.z; } };
ctx.entity_get_rotation = [](int i, float* x, float* y, float* z) { if (auto* t = s_editor->scene.entities[i].get_transform_component()) { *x = t->rotation.x; *y = t->rotation.y; *z = t->rotation.z; } };
ctx.entity_get_scale = [](int i, float* x, float* y, float* z) { if (auto* t = s_editor->scene.entities[i].get_transform_component()) { *x = t->scale.x; *y = t->scale.y; *z = t->scale.z; } };
ctx.entity_get_color = [](int i, unsigned char* r, unsigned char* g, unsigned char* b, unsigned char* a) { if (auto* m = s_editor->scene.entities[i].get_material_component()) { *r = m->color.r; *g = m->color.g; *b = m->color.b; *a = m->color.a; } };

ctx.entity_set_position = [](int i, float x, float y, float z) { if (auto* t = s_editor->scene.entities[i].get_transform_component()) t->position = {x, y, z}; };
ctx.entity_set_rotation = [](int i, float x, float y, float z) { if (auto* t = s_editor->scene.entities[i].get_transform_component()) t->rotation = {x, y, z}; };
ctx.entity_set_scale = [](int i, float x, float y, float z) { if (auto* t = s_editor->scene.entities[i].get_transform_component()) t->scale = {x, y, z}; };
ctx.entity_set_color = [](int i, unsigned char r, unsigned char g, unsigned char b, unsigned char a) { if (auto* m = s_editor->scene.entities[i].get_material_component()) m->color = {r, g, b, a}; };
ctx.entity_set_name = [](int i, const char* name) { s_editor->scene.entities[i].name = name; };

ctx.scene_save = []() { project_save(s_editor->project_path, s_editor->scene); };
ctx.scene_spawn = [](const char* asset_name) -> int {
ctx->scene = &s_editor->scene;

ctx->delta_time = GetFrameTime();
ctx->entity_count = (int)s_editor->scene.entities.size();
ctx->selected = &s_editor->scene.selected;

ctx->ui_begin = [](const char* t) { return ImGui::Begin(t); };
ctx->ui_end = []() { ImGui::End(); };
ctx->ui_begin_menu = [](const char* label) { return ImGui::BeginMenu(label); };
ctx->ui_end_menu = []() { ImGui::EndMenu(); };
ctx->ui_menu_item = [](const char* label) { return ImGui::MenuItem(label); };
ctx->ui_text = [](const char* t) { ImGui::Text("%s", t); };
ctx->ui_button = [](const char* l) { return ImGui::Button(l); };
ctx->ui_checkbox = [](const char* l, bool* v) { return ImGui::Checkbox(l, v); };
ctx->ui_slider_float = [](const char* l, float* v, float mn, float mx) { return ImGui::SliderFloat(l, v, mn, mx); };
ctx->ui_input_float = [](const char* l, float* v) { return ImGui::InputFloat(l, v); };
ctx->ui_color_edit3 = [](const char* l, float c[3]) { return ImGui::ColorEdit3(l, c); };
ctx->ui_separator = []() { ImGui::Separator(); };
ctx->ui_same_line = []() { ImGui::SameLine(); };

ctx->entity_get_name = [](int i) -> const char* { return s_editor->scene.entities[i].name.c_str(); };
ctx->entity_get_position = [](int i, float* x, float* y, float* z) { if (auto* t = s_editor->scene.entities[i].get_transform_component()) { *x = t->position.x; *y = t->position.y; *z = t->position.z; } };
ctx->entity_get_rotation = [](int i, float* x, float* y, float* z) { if (auto* t = s_editor->scene.entities[i].get_transform_component()) { *x = t->rotation.x; *y = t->rotation.y; *z = t->rotation.z; } };
ctx->entity_get_scale = [](int i, float* x, float* y, float* z) { if (auto* t = s_editor->scene.entities[i].get_transform_component()) { *x = t->scale.x; *y = t->scale.y; *z = t->scale.z; } };
ctx->entity_get_color = [](int i, unsigned char* r, unsigned char* g, unsigned char* b, unsigned char* a) { if (auto* m = s_editor->scene.entities[i].get_material_component()) { *r = m->color.r; *g = m->color.g; *b = m->color.b; *a = m->color.a; } };

ctx->entity_set_position = [](int i, float x, float y, float z) { if (auto* t = s_editor->scene.entities[i].get_transform_component()) t->position = {x, y, z}; };
ctx->entity_set_rotation = [](int i, float x, float y, float z) { if (auto* t = s_editor->scene.entities[i].get_transform_component()) t->rotation = {x, y, z}; };
ctx->entity_set_scale = [](int i, float x, float y, float z) { if (auto* t = s_editor->scene.entities[i].get_transform_component()) t->scale = {x, y, z}; };
ctx->entity_set_color = [](int i, unsigned char r, unsigned char g, unsigned char b, unsigned char a) { if (auto* m = s_editor->scene.entities[i].get_material_component()) m->color = {r, g, b, a}; };
ctx->entity_set_name = [](int i, const char* name) { s_editor->scene.entities[i].name = name; };

ctx->scene_save = []() { project_save(s_editor->project_path, s_editor->scene); };
ctx->scene_spawn = [](const char* asset_name) -> int {
for (auto& a : assets) {
if (a.name == asset_name) {
Entity e = make_entity_from_asset(s_editor->scene, a);
Expand All @@ -121,15 +148,20 @@ static void update_plugins(PluginManager& plugin_manager, Editor& editor) {
}
return -1;
};
ctx.scene_delete = [](int i) {
ctx->scene_delete = [](int i) {
if (i < 0 || i >= (int)s_editor->scene.entities.size()) return;
s_editor->scene.entities.erase(s_editor->scene.entities.begin() + i);
if (s_editor->scene.selected >= (int)s_editor->scene.entities.size())
s_editor->scene.selected = -1;
};
ctx->register_ui_callback = [](UIRegion region, PluginUICallback callback) {
if (g_plugin_manager) {
g_plugin_manager->register_ui_callback(region, callback);
}
};

plugin_manager.update_all(ctx);
plugin_manager.draw_ui_all(ctx);
plugin_manager.update_all(*ctx);
plugin_manager.draw_ui_all(*ctx);
}

static Matrix compose_entity_transform(const Entity& entity) {
Expand Down Expand Up @@ -436,6 +468,11 @@ int main(int argc, char* argv[]) {
Mat4 light_view = {0};
Mat4 light_proj = {0};

g_plugin_manager = new PluginManager();
ctx = new PluginContext{};

init_plugin_context(ctx);

load_models();
load_textures(project_path);
refresh_assets(project_path);
Expand All @@ -446,8 +483,10 @@ int main(int argc, char* argv[]) {
else
project_new(project_path, editor.scene);

PluginManager plugin_manager;
plugin_manager.load_all("plugins");

g_plugin_manager->load_all("plugins", ctx);
editor.plugin_manager = g_plugin_manager;

std::string active_font_language = LanguageManager::get().current;

while (!WindowShouldClose()) {
Expand Down Expand Up @@ -584,9 +623,9 @@ int main(int argc, char* argv[]) {

editor.handle_input();

editor.draw_ui(shadowmap_shader, camera);
editor.draw_ui(shadowmap_shader, camera, ctx);

update_plugins(plugin_manager, editor);
update_plugins(*g_plugin_manager, editor);

EndImGui();
EndDrawing();
Expand All @@ -598,7 +637,7 @@ int main(int argc, char* argv[]) {
UnloadShader(shadowcaster_shader);
UnloadShader(shadowmap_shader);
unload_shadowmap_render_texture(shadow_map);
plugin_manager.unload_all();
g_plugin_manager->unload_all();
ShutdownImGui();
CloseWindow();
return 0;
Expand Down
Loading
Loading