mirror of
https://github.com/MadeOfJelly/MushMachine.git
synced 2025-12-07 00:26:34 +01:00
add imgui menu bar draft
This commit is contained in:
98
framework/imgui/src/mm/services/imgui_menu_bar.cpp
Normal file
98
framework/imgui/src/mm/services/imgui_menu_bar.cpp
Normal file
@@ -0,0 +1,98 @@
|
||||
#include "./imgui_menu_bar.hpp"
|
||||
|
||||
#include <mm/engine.hpp>
|
||||
#include <entt/core/hashed_string.hpp>
|
||||
|
||||
#include <imgui/imgui.h>
|
||||
|
||||
#include <unordered_set>
|
||||
|
||||
#include <mm/logger.hpp>
|
||||
#define LOG_CRIT(...) __LOG_CRIT( "ImGuiMenuBar", __VA_ARGS__)
|
||||
#define LOG_ERROR(...) __LOG_ERROR("ImGuiMenuBar", __VA_ARGS__)
|
||||
#define LOG_WARN(...) __LOG_WARN( "ImGuiMenuBar", __VA_ARGS__)
|
||||
#define LOG_INFO(...) __LOG_INFO( "ImGuiMenuBar", __VA_ARGS__)
|
||||
#define LOG_DEBUG(...) __LOG_DEBUG("ImGuiMenuBar", __VA_ARGS__)
|
||||
#define LOG_TRACE(...) __LOG_TRACE("ImGuiMenuBar", __VA_ARGS__)
|
||||
|
||||
namespace MM::Services {
|
||||
|
||||
bool ImGuiMenuBar::enable(Engine& engine) {
|
||||
MM::Logger::initSectionLogger("ImGuiMenuBar");
|
||||
|
||||
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 == toggle_key) {
|
||||
show_menu_bar = !show_menu_bar;
|
||||
}
|
||||
return false; // invis
|
||||
});
|
||||
} else {
|
||||
LOG_WARN("no SDLService, skipping toggle hotkey");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ImGuiMenuBar::disable(Engine& engine) {
|
||||
if (_event_handle) {
|
||||
auto* sdl_ss = engine.tryService<MM::Services::SDLService>();
|
||||
sdl_ss->removeEventHandler(_event_handle);
|
||||
_event_handle = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<UpdateStrategies::UpdateCreationInfo> ImGuiMenuBar::registerUpdates(void) {
|
||||
using namespace entt::literals;
|
||||
return {
|
||||
{
|
||||
"ImGuiMenuBar::render"_hs,
|
||||
"ImGuiMenuBar::render",
|
||||
[this](Engine& e){ renderImGui(e); }
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
void ImGuiMenuBar::renderImGui(Engine& engine) {
|
||||
if (show_menu_bar && ImGui::BeginMainMenuBar()) {
|
||||
|
||||
// TODO: better solution?
|
||||
std::unordered_set<std::string> done;
|
||||
|
||||
// we dont check for errors in section_order
|
||||
for (const auto& section_key : section_order) {
|
||||
if (menu_tree.count(section_key)) {
|
||||
renderSection(engine, section_key, menu_tree[section_key]);
|
||||
|
||||
done.emplace(section_key);
|
||||
}
|
||||
}
|
||||
|
||||
for (auto& [section_key, section_data] : menu_tree) {
|
||||
if (done.count(section_key)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
renderSection(engine, section_key, section_data);
|
||||
|
||||
done.emplace(section_key); // we dont need this
|
||||
}
|
||||
|
||||
|
||||
ImGui::Text("%.1fFPS", ImGui::GetIO().Framerate);
|
||||
ImGui::EndMainMenuBar();
|
||||
}
|
||||
}
|
||||
|
||||
void ImGuiMenuBar::renderSection(Engine& engine, const std::string& section_key, section_t& section) {
|
||||
if (ImGui::BeginMenu(section_key.c_str())) {
|
||||
for (auto& item : section) {
|
||||
item.second(engine);
|
||||
}
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace MM::Services
|
||||
|
||||
Reference in New Issue
Block a user