61
src/ImGui.hpp
Normal file
61
src/ImGui.hpp
Normal file
@@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include <bitset>
|
||||
#include <memory>
|
||||
|
||||
#include <raylib.h>
|
||||
|
||||
#include "TextRenderer.hpp"
|
||||
|
||||
struct TextInputOptions {
|
||||
bool multiline { false };
|
||||
};
|
||||
|
||||
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(std::pmr::vector<u32> const input_runes, 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 };
|
||||
};
|
||||
|
||||
std::unordered_map<std::size_t, TextInputState> m_ti_states;
|
||||
std::size_t m_focused {};
|
||||
std::pmr::vector<u32> m_input_runes {}; // 0123 <-> hjkl arrow keys
|
||||
bool m_ctrl {};
|
||||
bool m_shift {};
|
||||
|
||||
std::shared_ptr<TextRenderer> m_text_renderer {};
|
||||
};
|
||||
|
||||
struct ImGuiGuard {
|
||||
ImGuiGuard(ImGui *imgui, std::pmr::vector<u32> const input_runes, bool ctrl,
|
||||
bool shift)
|
||||
: m_imgui(imgui)
|
||||
{
|
||||
m_imgui->begin(input_runes, ctrl, shift);
|
||||
}
|
||||
~ImGuiGuard() { m_imgui->end(); }
|
||||
|
||||
private:
|
||||
ImGui *m_imgui { nullptr };
|
||||
};
|
||||
Reference in New Issue
Block a user