Files
waylight/src/ImGui.hpp

67 lines
1.5 KiB
C++
Raw Normal View History

#pragma once
#include <bitset>
#include <memory>
#include <raylib.h>
#include "TextRenderer.hpp"
constexpr float DEFAULT_FONT_SIZE { 16 };
struct TextInputOptions {
bool multiline { false };
float font_size { DEFAULT_FONT_SIZE };
};
struct ImGui {
ImGui(std::shared_ptr<TextRenderer> text_renderer);
ImGui(ImGui const &) = delete;
auto operator=(ImGui const &) -> ImGui & = delete;
ImGui(ImGui &&) = default;
auto operator=(ImGui &&) -> ImGui & = default;
void begin(u32 const rune, bool ctrl, bool shift);
void end();
// Bit 0 -> Submitted
// Bit 1 -> String changed
auto text_input(std::size_t id, std::pmr::string &str, Rectangle rec,
TextInputOptions options = {}) -> std::bitset<2>;
[[nodiscard]] inline auto id(std::string_view const str) -> std::size_t
{
std::hash<std::string_view> hasher;
return hasher(str);
}
private:
struct TextInputState {
int current_rune_idx { 0 };
Vector2 scroll_offset; // y not used if multiline == false
Vector2 cursor_position; // y not used if multiline == false
};
std::unordered_map<std::size_t, TextInputState> m_ti_states;
std::size_t m_focused_id {};
u32 m_rune {}; // 1234 <-> hjkl arrow keys
bool m_ctrl {};
bool m_shift {};
std::shared_ptr<TextRenderer> m_text_renderer {};
};
struct ImGuiGuard {
ImGuiGuard(std::shared_ptr<ImGui> imgui, u32 const rune, bool const ctrl,
bool const shift)
: m_imgui(imgui)
{
m_imgui->begin(rune, ctrl, shift);
}
~ImGuiGuard() { m_imgui->end(); }
private:
std::shared_ptr<ImGui> m_imgui { nullptr };
};