IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
devui_imgui.hxx
Go to the documentation of this file.
1
3
4#pragma once
5#include <ice/heap_string.hxx>
6#include <ice/assert_core.hxx>
7#include <ice/colors.hxx>
8#include <ice/math.hxx>
9
10#ifdef IM_ASSERT
11#undef IM_ASSERT
12#endif
13
14#define IM_ASSERT(cond) ICE_ASSERT_CORE(cond)
15#include <imgui/imgui.h>
16#include <fmt/core.h>
17#undef assert
18
19namespace ImGui
20{
21
22 // Helpers
23 namespace Compat
24 {
25
26 inline constexpr auto Vec2(ice::vec2f val) noexcept -> ImVec2 { return { val.v[0][0], val.v[0][1] }; };
27 inline constexpr auto Vec4(ice::vec4f val) noexcept -> ImVec4 { return { val.v[0][0], val.v[0][1], val.v[0][2], val.v[0][3] }; };
28
29 inline constexpr auto Color(ice::Color color) noexcept -> ImColor
30 {
31 ice::color::SRGB const srgb = color.gammut_corrected(ice::ColorSpace::SRGB, 0.0f).to_lrgb().to_srgb();
32 return ImGui::ColorConvertFloat4ToU32({ srgb.red, srgb.green, srgb.blue, srgb.alpha });
33 }
34
35 } // Details
36
37 inline bool Begin(ice::String name, bool* inout_open = nullptr, ImGuiWindowFlags flags = 0) noexcept
38 {
39 return ImGui::Begin(name.begin(), inout_open, flags);
40 }
41
42 inline bool BeginListBox(ice::String label, ice::vec2f size = {}) noexcept
43 {
44 return ImGui::BeginListBox(label.begin(), Compat::Vec2(size));
45 }
46
47 inline void TextUnformatted(ice::String text) noexcept
48 {
49 ImGui::TextUnformatted(text.begin(), text.end());
50 }
51
52 inline bool Selectable(
53 ice::String label,
54 bool selected = false,
55 ImGuiSelectableFlags flags = 0,
56 ice::vec2f size = {}
57 ) noexcept
58 {
59 return ImGui::Selectable(label.begin(), selected, flags, Compat::Vec2(size));
60 }
61
62 // Extensions
63
64 namespace Detail
65 {
66
67 auto TempBuffer() noexcept -> char*;
68 auto TempBufferSize() noexcept -> size_t;
69 void TextEx(char const* begin, char const* end) noexcept;
70
71 } // namespace Detail
72
73 inline auto ToColor(ice::Color color) noexcept -> ImU32
74 {
75 return Compat::Color(color);
76 }
77
78 constexpr auto ToVec2(ice::vec4f pos) noexcept -> ImVec2
79 {
80 return { pos.x, pos.y };
81 }
82
83 template<typename... Args>
84 inline void TextT(fmt::format_string<Args...> format, Args&&... args) noexcept
85 {
86 fmt::format_to_n_result<char*> result = fmt::format_to_n(
89 format,
90 ice::forward<Args>(args)...
91 );
93 }
94
95 template<typename... Args>
96 inline void TextColoredT(ice::Color col, fmt::format_string<Args...> format, Args&&... args) noexcept
97 {
98 ImGui::PushStyleColor(ImGuiCol_Text, ImGui::ToColor(col));
99 TextT(format, ice::forward<Args>(args)...);
100 ImGui::PopStyleColor();
101 }
102
103 template<typename... Args>
104 inline void TextRightT(fmt::format_string<Args...> format, Args&&... args) noexcept
105 {
106 fmt::format_to_n_result<char*> result = fmt::format_to_n(
109 format,
110 ice::forward<Args>(args)...
111 );
112
113 ImVec2 const avail = ImGui::GetContentRegionAvail();
114 ImVec2 const text_size = ImGui::CalcTextSize(Detail::TempBuffer(), result.out);
115 float const cursor_pos = ImGui::GetCursorPosX();
116 float const new_cursor_pos = (cursor_pos + avail.x) - (text_size.x);
117
118 ImGui::SetCursorPosX(new_cursor_pos);
119 Detail::TextEx(Detail::TempBuffer(), result.out);
120 }
121
122 template<typename... Args>
123 inline void TextRightColoredT(ice::Color col, fmt::format_string<Args...> format, Args&&... args) noexcept
124 {
125 ImGui::PushStyleColor(ImGuiCol_Text, ImGui::ToColor(col));
126 TextRightT(format, ice::forward<Args>(args)...);
127 ImGui::PopStyleColor();
128 }
129
131 ice::String label,
132 ice::HeapString<>& out_string,
133 ImGuiInputTextFlags flags = ImGuiInputTextFlags_None
134 ) noexcept;
135
137 std::string_view label,
138 int& inout_status,
139 ImVec2 const& size_arg = {0,0},
140 ImGuiButtonFlags flags = 0
141 ) noexcept;
142
143 void EndLargeButton() noexcept;
144
145 inline void SetCursorScreenPos2(ice::vec2f pos) noexcept
146 {
147 ImGui::SetCursorScreenPos(Compat::Vec2(pos));
148 }
149
150} // namespace ImGui
Definition devui_imgui.hxx:24
constexpr auto Vec4(ice::vec4f val) noexcept -> ImVec4
Definition devui_imgui.hxx:27
constexpr auto Vec2(ice::vec2f val) noexcept -> ImVec2
Definition devui_imgui.hxx:26
constexpr auto Color(ice::Color color) noexcept -> ImColor
Definition devui_imgui.hxx:29
Definition devui_imgui.hxx:65
void TextEx(char const *begin, char const *end) noexcept
auto TempBuffer() noexcept -> char *
auto TempBufferSize() noexcept -> size_t
Definition devui_imgui.hxx:20
auto ToColor(ice::Color color) noexcept -> ImU32
Definition devui_imgui.hxx:73
bool InputText(ice::String label, ice::HeapString<> &out_string, ImGuiInputTextFlags flags=ImGuiInputTextFlags_None) noexcept
void EndLargeButton() noexcept
bool Selectable(ice::String label, bool selected=false, ImGuiSelectableFlags flags=0, ice::vec2f size={}) noexcept
Definition devui_imgui.hxx:52
void TextT(fmt::format_string< Args... > format, Args &&... args) noexcept
Definition devui_imgui.hxx:84
constexpr auto ToVec2(ice::vec4f pos) noexcept -> ImVec2
Definition devui_imgui.hxx:78
void TextRightColoredT(ice::Color col, fmt::format_string< Args... > format, Args &&... args) noexcept
Definition devui_imgui.hxx:123
bool BeginLargeButton(std::string_view label, int &inout_status, ImVec2 const &size_arg={0, 0}, ImGuiButtonFlags flags=0) noexcept
void TextRightT(fmt::format_string< Args... > format, Args &&... args) noexcept
Definition devui_imgui.hxx:104
void TextColoredT(ice::Color col, fmt::format_string< Args... > format, Args &&... args) noexcept
Definition devui_imgui.hxx:96
void TextUnformatted(ice::String text) noexcept
Definition devui_imgui.hxx:47
bool Begin(ice::String name, bool *inout_open=nullptr, ImGuiWindowFlags flags=0) noexcept
Definition devui_imgui.hxx:37
bool BeginListBox(ice::String label, ice::vec2f size={}) noexcept
Definition devui_imgui.hxx:42
void SetCursorScreenPos2(ice::vec2f pos) noexcept
Definition devui_imgui.hxx:145
ice::detail::ColorData< ColorFormat::StandardRGB > SRGB
Definition colors.hxx:18
vec< 2, f32 > vec2f
Definition vector.hxx:178
vec< 4, f32 > vec4f
Definition vector.hxx:188
SPDX-License-Identifier: MIT.
Definition array.hxx:12
ice::BasicString< char > String
Definition string.hxx:82
@ SRGB
Definition color_enums.hxx:12
ice::color::OkLCH Color
Definition colors.hxx:34
Definition heap_string.hxx:15
ice::f32 red
Definition color_data.hxx:42
ice::f32 green
Definition color_data.hxx:43
ice::f32 alpha
Definition color_data.hxx:45
ice::f32 blue
Definition color_data.hxx:44
constexpr auto begin(this Self const &self) noexcept -> typename Self::ConstIterator
Definition readonly_operations.hxx:273