add imgui menu bar draft

This commit is contained in:
Green Sky 2021-01-11 16:02:05 +01:00
parent fd821e1f02
commit db07c78815
3 changed files with 151 additions and 0 deletions

View File

@ -7,6 +7,10 @@ project(imgui_lib CXX)
add_library(imgui_service
./src/mm/services/imgui_s.hpp
./src/mm/services/imgui_s.cpp
# sneak
./src/mm/services/imgui_menu_bar.hpp
./src/mm/services/imgui_menu_bar.cpp
)
target_include_directories(imgui_service PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/src")

View 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

View File

@ -0,0 +1,49 @@
#pragma once
#include <mm/services/sdl_service.hpp>
#include <map>
#include <variant>
#include <functional>
namespace MM::Services {
class ImGuiMenuBar : public Service {
public:
const char* name(void) override { return "ImGuiMenuBar"; }
bool enable(Engine& engine) override;
void disable(Engine& engine) override;
std::vector<UpdateStrategies::UpdateCreationInfo> registerUpdates(void) override;
public:
bool show_menu_bar = true;
SDL_Keycode toggle_key = SDLK_F2;
using menu_item_callback_t = std::function<void(MM::Engine&)>;
using section_t = std::map<std::string, menu_item_callback_t>;
// menu tree
// section -> section_t -> menu_item_t
// section is stable id key
// menus contain stable id keys
std::map<std::string, section_t> menu_tree;
std::vector<std::string> section_order {
"Engine",
};
private:
MM::Services::SDLService::EventHandlerHandle _event_handle = nullptr;
private:
void renderImGui(Engine& engine);
void renderSection(Engine& engine, const std::string& section_key, section_t& section);
};
} // namespace MM::Services