mirror of
https://github.com/MadeOfJelly/MushMachine.git
synced 2025-06-20 19:56:37 +02:00
initial import, >900commits predate this
This commit is contained in:
141
framework/imgui/src/mm/services/imgui_s.cpp
Normal file
141
framework/imgui/src/mm/services/imgui_s.cpp
Normal file
@ -0,0 +1,141 @@
|
||||
#include "./imgui_s.hpp"
|
||||
|
||||
#include <imgui/imgui.h>
|
||||
#include <imgui_impl_sdl.h>
|
||||
|
||||
#ifdef MM_OPENGL_3
|
||||
#include <imgui_impl_opengl3.h>
|
||||
#endif
|
||||
|
||||
#include <IconsIonicons.h>
|
||||
|
||||
#if defined(__GNUC__) || defined(__clang__)
|
||||
#define USE_BASE85
|
||||
#endif
|
||||
|
||||
#ifdef USE_BASE85
|
||||
#include "../../../res/ionicons/ionicons.ttf.base85.h"
|
||||
#else
|
||||
#include "../../../res/ionicons/ionicons.ttf.h"
|
||||
#endif
|
||||
|
||||
#include <tracy/Tracy.hpp>
|
||||
|
||||
namespace MM::Services {
|
||||
|
||||
bool ImGuiService::enable(Engine& engine) {
|
||||
IMGUI_CHECKVERSION();
|
||||
|
||||
ImGui::CreateContext();
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavNoCaptureKeyboard;
|
||||
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // TODO: dont, if ingame ?
|
||||
|
||||
auto& sdl_ss = engine.getService<MM::Services::SDLService>();
|
||||
#ifdef MM_OPENGL_3
|
||||
ImGui_ImplSDL2_InitForOpenGL(sdl_ss.win, sdl_ss.gl_context);
|
||||
ImGui_ImplOpenGL3_Init();
|
||||
#endif
|
||||
|
||||
// style
|
||||
{
|
||||
ImGui::StyleColorsDark();
|
||||
|
||||
auto& style = ImGui::GetStyle();
|
||||
style.WindowRounding = 2.f;
|
||||
}
|
||||
|
||||
// font
|
||||
{
|
||||
io.Fonts->AddFontDefault();
|
||||
static const ImWchar icons_ranges[] = {ICON_MIN_II, ICON_MAX_II, 0};
|
||||
ImFontConfig icons_config;
|
||||
icons_config.MergeMode = true;
|
||||
icons_config.PixelSnapH = true;
|
||||
icons_config.FontDataOwnedByAtlas = false;
|
||||
icons_config.GlyphOffset = {0.f, 4.f};
|
||||
//io.Fonts->AddFontFromMemoryTTF(ionicons_ttf, ionicons_ttf_len, 16.f, &icons_config, icons_ranges);
|
||||
#ifdef USE_BASE85
|
||||
io.Fonts->AddFontFromMemoryCompressedBase85TTF(ionicons_ttf_compressed_data_base85, 16.f*1.3f, &icons_config, icons_ranges);
|
||||
#else
|
||||
io.Fonts->AddFontFromMemoryTTF(ionicons_ttf, ionicons_ttf_len, 16.f, &icons_config, icons_ranges);
|
||||
#endif
|
||||
}
|
||||
|
||||
// TODO: register event handle
|
||||
_event_handle = sdl_ss.addEventHandler([](const SDL_Event& e) {
|
||||
if (!ImGui_ImplSDL2_ProcessEvent(&e)) {
|
||||
return false; // if the event was not relevant to imgui anyway
|
||||
}
|
||||
|
||||
auto& io = ImGui::GetIO();
|
||||
// keyboard
|
||||
if ((
|
||||
e.type == SDL_KEYDOWN ||
|
||||
e.type == SDL_KEYUP ||
|
||||
e.type == SDL_TEXTEDITING ||
|
||||
e.type == SDL_TEXTINPUT
|
||||
)
|
||||
&& io.WantCaptureKeyboard) {
|
||||
return true;
|
||||
}
|
||||
// mouse
|
||||
if ((
|
||||
e.type == SDL_MOUSEMOTION ||
|
||||
e.type == SDL_MOUSEBUTTONUP ||
|
||||
e.type == SDL_MOUSEBUTTONDOWN ||
|
||||
e.type == SDL_MOUSEWHEEL // ????
|
||||
)
|
||||
&& io.WantCaptureMouse) {
|
||||
return true;
|
||||
}
|
||||
//// controller ???
|
||||
//if ((
|
||||
//e.type == SDL_CONTROLLERBUTTONUP ||
|
||||
//e.type == SDL_CONTROLLERBUTTONDOWN
|
||||
//)[>
|
||||
//&& io.WantCaptureKeyboard*/) {
|
||||
//return false;
|
||||
//}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
_new_frame_handle = engine.addUpdate([this](Engine& e) { this->imgui_new_frame(e); });
|
||||
assert(!_new_frame_handle.expired());
|
||||
auto tmp_lock = _new_frame_handle.lock();
|
||||
tmp_lock->priority = 90; // after sdl events (100)
|
||||
tmp_lock->name = "imgui new frame";
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ImGuiService::disable(Engine& engine) {
|
||||
auto& sdl_ss = engine.getService<MM::Services::SDLService>();
|
||||
sdl_ss.removeEventHandler(_event_handle);
|
||||
|
||||
engine.removeUpdate(_new_frame_handle);
|
||||
|
||||
ImGui::EndFrame(); // making sure, does not work????
|
||||
|
||||
#ifdef MM_OPENGL_3
|
||||
ImGui_ImplOpenGL3_Shutdown();
|
||||
#endif
|
||||
ImGui_ImplSDL2_Shutdown();
|
||||
ImGui::DestroyContext();
|
||||
}
|
||||
|
||||
void ImGuiService::imgui_new_frame(Engine& engine) {
|
||||
ZoneScopedN("MM::Services::ImGuiService::imgui_new_frame");
|
||||
|
||||
#ifdef MM_OPENGL_3
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
#endif
|
||||
|
||||
ImGui_ImplSDL2_NewFrame(engine.getService<MM::Services::SDLService>().win);
|
||||
ImGui::NewFrame();
|
||||
}
|
||||
|
||||
} // namespace MM::Services
|
||||
|
28
framework/imgui/src/mm/services/imgui_s.hpp
Normal file
28
framework/imgui/src/mm/services/imgui_s.hpp
Normal file
@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <mm/engine.hpp>
|
||||
#include <mm/services/sdl_service.hpp>
|
||||
|
||||
namespace MM::Services {
|
||||
|
||||
// responsable for event handling and setup
|
||||
// enable requires an open sdl window (and context)
|
||||
class ImGuiService : public Service {
|
||||
private:
|
||||
MM::Services::SDLService::EventHandlerHandle _event_handle = nullptr;
|
||||
|
||||
// new frame needs to start AFTER the events have been processed (and obv bf rendt)
|
||||
MM::Engine::FunctionDataHandle _new_frame_handle;
|
||||
|
||||
public:
|
||||
bool enable(Engine& engine) override;
|
||||
void disable(Engine& engine) override;
|
||||
|
||||
const char* name(void) override { return "ImGuiService"; }
|
||||
|
||||
private:
|
||||
void imgui_new_frame(Engine& engine);
|
||||
};
|
||||
|
||||
} // namespace MM::Services
|
||||
|
213
framework/imgui/src/mm/services/scene_tools.cpp
Normal file
213
framework/imgui/src/mm/services/scene_tools.cpp
Normal file
@ -0,0 +1,213 @@
|
||||
#include "./scene_tools.hpp"
|
||||
|
||||
#include <entt/config/version.h>
|
||||
|
||||
#include <mm/services/opengl_renderer.hpp>
|
||||
|
||||
#include <mm/resource_manager.hpp>
|
||||
|
||||
#include <mm/engine.hpp>
|
||||
|
||||
#include <imgui/imgui.h>
|
||||
|
||||
#include <mm/components/name.hpp>
|
||||
#include <mm/components/transform2d.hpp>
|
||||
#include <mm/components/velocity2d.hpp>
|
||||
|
||||
#include <mm/imgui/widgets/texture_resource_manager.hpp>
|
||||
#include <mm/imgui/widgets/entity.hpp>
|
||||
|
||||
#include <mm/imgui/widgets/components/name.hpp>
|
||||
#include <mm/imgui/widgets/components/transform2d.hpp>
|
||||
#include <mm/imgui/widgets/components/transform3d.hpp>
|
||||
#include <mm/imgui/widgets/components/velocity2d.hpp>
|
||||
#include <mm/imgui/widgets/components/view_dir2d.hpp>
|
||||
#include <mm/imgui/widgets/components/view_dir3d.hpp>
|
||||
#include <mm/imgui/widgets/components/color.hpp>
|
||||
#include <mm/imgui/widgets/components/texture.hpp>
|
||||
|
||||
#include <mm/imgui/widgets/camera.hpp>
|
||||
|
||||
#include <mm/imgui/widgets/filesystem.hpp>
|
||||
#include <mm/services/filesystem.hpp>
|
||||
|
||||
#include <mm/logger.hpp>
|
||||
#define LOGIGS(x) LOG("ImGuiSceneToolsService", x)
|
||||
|
||||
namespace MM::Services {
|
||||
|
||||
void ImGuiSceneToolsService::renderImGui(Engine& engine) {
|
||||
if (show_menu && ImGui::BeginMainMenuBar()) {
|
||||
if (ImGui::BeginMenu("Engine")) {
|
||||
if (ImGui::MenuItem("Stop Engine")) {
|
||||
engine.stop();
|
||||
}
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
|
||||
if (ImGui::BeginMenu("TextEditor")) {
|
||||
if (ImGui::MenuItem("New Editor")) {
|
||||
_text_editor_list.emplace_back(engine);
|
||||
}
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
|
||||
if (ImGui::BeginMenu("Windows")) {
|
||||
ImGui::MenuItem("MM Metrics", NULL, &_show_mm_metrics);
|
||||
ImGui::MenuItem("ImGui Metrics", NULL, &_show_imgui_metrics);
|
||||
ImGui::MenuItem("Texture Loader", NULL, &_show_texture_loader);
|
||||
ImGui::MenuItem("Texture List", NULL, &_show_texture_list);
|
||||
ImGui::MenuItem("Entity Editor", NULL, &_show_entity_editor);
|
||||
ImGui::MenuItem("Camera Tool", NULL, &_show_camera_tool);
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
|
||||
ImGui::Text("%.1fFPS", ImGui::GetIO().Framerate);
|
||||
ImGui::EndMainMenuBar();
|
||||
}
|
||||
|
||||
auto& scene = engine.tryService<MM::Services::SceneServiceInterface>()->getScene();
|
||||
|
||||
if (_show_mm_metrics) {
|
||||
if (ImGui::Begin("Metrics##MM", &_show_mm_metrics)) {
|
||||
if (ImGui::CollapsingHeader("ECS")) {
|
||||
ImGui::Text("EnTT v%d.%d.%d", ENTT_VERSION_MAJOR, ENTT_VERSION_MINOR, ENTT_VERSION_PATCH);
|
||||
ImGui::Text("capacity: %lu", scene.capacity());
|
||||
ImGui::Text("size: %lu", scene.size());
|
||||
ImGui::Text("alive: %lu", scene.alive());
|
||||
size_t orphans = 0;
|
||||
scene.orphans([&](auto) { orphans++; });
|
||||
ImGui::Text("orphans: %lu", orphans);
|
||||
}
|
||||
}
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
if (_show_imgui_metrics) {
|
||||
ImGui::ShowMetricsWindow(&_show_imgui_metrics);
|
||||
}
|
||||
|
||||
if (_show_texture_loader) {
|
||||
if (ImGui::Begin("Texture Loader", &_show_texture_loader)) {
|
||||
ImGuiWidgets::TextureResourceManagerLoader(engine);
|
||||
}
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
if (_show_texture_list) {
|
||||
if (ImGui::Begin("Texture List", &_show_texture_list)) {
|
||||
ImGuiWidgets::TextureResourceManagerList();
|
||||
}
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
if (_show_entity_editor) {
|
||||
ImGui::SetNextWindowSize(ImVec2(750, 500), ImGuiCond_FirstUseEver);
|
||||
if (ImGui::Begin("Entity Editor", &_show_entity_editor, ImGuiWindowFlags_MenuBar)) {
|
||||
ImGui::BeginMenuBar();
|
||||
ImGui::Checkbox("Show List", &_show_entity_list);
|
||||
ImGui::EndMenuBar();
|
||||
|
||||
if (_show_entity_list) {
|
||||
if (ImGui::BeginChild("list", {300, 0}, true)) {
|
||||
ImGuiWidgets::EntityTrashCan(scene); // TODO: remove?
|
||||
_entity_editor.renderEntityList(scene, _entity_comp_list);
|
||||
}
|
||||
ImGui::EndChild();
|
||||
ImGui::SameLine();
|
||||
}
|
||||
|
||||
if (ImGui::BeginChild("editor")) {
|
||||
_entity_editor.renderEditor(scene, _e);
|
||||
}
|
||||
ImGui::EndChild();
|
||||
}
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
if (_show_camera_tool) {
|
||||
if (ImGui::Begin("Camera3D Tool", &_show_camera_tool)) {
|
||||
ImGuiWidgets::Camera3D(scene);
|
||||
}
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
for (auto it = _text_editor_list.begin(); it != _text_editor_list.end();) {
|
||||
bool keep_open = true;
|
||||
it->renderImGui(&keep_open);
|
||||
|
||||
if (!keep_open) {
|
||||
it = _text_editor_list.erase(it);
|
||||
continue;
|
||||
}
|
||||
it++;
|
||||
}
|
||||
}
|
||||
|
||||
EntityEditor<Entity>& ImGuiSceneToolsService::getEntityEditor(void) {
|
||||
return _entity_editor;
|
||||
}
|
||||
|
||||
bool ImGuiSceneToolsService::enable(Engine& engine) {
|
||||
if (!engine.tryService<MM::Services::OpenGLRenderer>()) {
|
||||
LOGIGS("error: rendering is not in engine");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!engine.tryService<MM::Services::FilesystemService>()) {
|
||||
LOGIGS("error: filesystem not initialized");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!engine.tryService<MM::Services::SceneServiceInterface>()) {
|
||||
LOGIGS("error: no SceneServiceInterface");
|
||||
return false;
|
||||
}
|
||||
|
||||
// setup entity editor defaults
|
||||
{
|
||||
_entity_editor.show_window = false;
|
||||
|
||||
_entity_editor.registerComponent<MM::Components::Name>("Name");
|
||||
_entity_editor.registerComponent<MM::Components::Transform2D>("Transform2D");
|
||||
_entity_editor.registerComponent<MM::Components::Transform3D>("Transform3D");
|
||||
_entity_editor.registerComponent<MM::Components::Velocity2D>("Velocity2D");
|
||||
_entity_editor.registerComponent<MM::Components::ViewDir2D>("ViewDir2D");
|
||||
_entity_editor.registerComponent<MM::Components::ViewDir3D>("ViewDir3D");
|
||||
_entity_editor.registerComponent<MM::Components::Color>("Color");
|
||||
|
||||
_entity_editor.registerComponent<MM::Components::OpenGL::Texture>("Texture");
|
||||
}
|
||||
|
||||
_render_handle = engine.addUpdate([this](Engine& e){ this->renderImGui(e); });
|
||||
|
||||
auto* sdl_ss = engine.tryService<MM::Services::SDLService>();
|
||||
if (sdl_ss) {
|
||||
_event_handle = sdl_ss->addEventHandler([this](const SDL_Event& e) -> bool {
|
||||
if (e.type == SDL_KEYDOWN && !e.key.repeat && e.key.keysym.sym == this->toggle_key) {
|
||||
this->show_menu = !this->show_menu;
|
||||
}
|
||||
return false; // invis
|
||||
});
|
||||
} else {
|
||||
LOGIGS("Warn: no SDLService, skipping toggle hotkey");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ImGuiSceneToolsService::disable(Engine& engine) {
|
||||
if (!_render_handle.expired()) {
|
||||
engine.removeUpdate(_render_handle);
|
||||
_render_handle.reset();
|
||||
}
|
||||
|
||||
if (_event_handle) {
|
||||
auto* sdl_ss = engine.tryService<MM::Services::SDLService>();
|
||||
sdl_ss->removeEventHandler(_event_handle);
|
||||
_event_handle = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace MM::Services
|
||||
|
53
framework/imgui/src/mm/services/scene_tools.hpp
Normal file
53
framework/imgui/src/mm/services/scene_tools.hpp
Normal file
@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
|
||||
#include <mm/services/sdl_service.hpp>
|
||||
|
||||
#define MM_IEEE_ENTITY_WIDGET ::MM::ImGuiWidgets::Entity // evil
|
||||
#define MM_IEEE_ASSERT(x)
|
||||
#include <mm/imgui/widgets/entity.hpp>
|
||||
#include <mm/imgui/imgui_entt_entity_editor.hpp>
|
||||
|
||||
#include <mm/imgui/file_text_editor.hpp>
|
||||
|
||||
namespace MM::Services {
|
||||
|
||||
class ImGuiSceneToolsService : public Service {
|
||||
public:
|
||||
bool show_menu = true;
|
||||
|
||||
SDL_Keycode toggle_key = SDLK_F2;
|
||||
|
||||
private:
|
||||
bool _show_mm_metrics = false;
|
||||
bool _show_imgui_metrics = false;
|
||||
bool _show_texture_loader = false;
|
||||
bool _show_texture_list = false;
|
||||
bool _show_entity_editor = false;
|
||||
bool _show_entity_list = true;
|
||||
bool _show_camera_tool = false;
|
||||
|
||||
EntityEditor<Entity> _entity_editor;
|
||||
// for list
|
||||
std::set<EntityEditor<Entity>::ComponentTypeID> _entity_comp_list;
|
||||
|
||||
std::vector<MM::FileTextEditor> _text_editor_list;
|
||||
|
||||
Engine::FunctionDataHandle _render_handle;
|
||||
MM::Services::SDLService::EventHandlerHandle _event_handle = nullptr;
|
||||
|
||||
private:
|
||||
void renderImGui(Engine& engine);
|
||||
|
||||
public:
|
||||
EntityEditor<Entity>& getEntityEditor(void);
|
||||
Entity _e = entt::null; // entity currently in editor
|
||||
|
||||
public:
|
||||
bool enable(Engine& engine) override;
|
||||
void disable(Engine& engine) override;
|
||||
|
||||
const char* name(void) override { return "ImGuiSceneToolsService"; }
|
||||
};
|
||||
|
||||
} // namespace MM::Services
|
||||
|
Reference in New Issue
Block a user