Unravel Engine C++ Reference
Loading...
Searching...
No Matches
imgui.h
Go to the documentation of this file.
1/*
2 * Copyright 2011-2023 Branimir Karadzic. All rights reserved.
3 * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
4 */
5
6#ifndef IMGUI_H_HEADER_GUARD
7#define IMGUI_H_HEADER_GUARD
8
10
12
13#include "graphics/graphics.h"
14#include <imgui_includes.h>
15
17#include <graphics/texture.h>
18
19inline uint32_t imguiRGBA(uint8_t _r, uint8_t _g, uint8_t _b, uint8_t _a = 255)
20{
21 return 0 | (uint32_t(_r) << 0) | (uint32_t(_g) << 8) | (uint32_t(_b) << 16) | (uint32_t(_a) << 24);
22}
23
24namespace bx
25{
26struct AllocatorI;
27}
28
29void imguiCreate(unravel::render_window* window, float _fontSize = 18.0f, bx::AllocatorI* _allocator = NULL);
30void imguiDestroy();
31
32void imguiProcessEvent(os::event& e);
33
34void imguiBeginFrame(float dt);
36
37namespace ImGui
38{
39#define IMGUI_FLAGS_NONE UINT8_C(1 << 0)
40#define IMGUI_FLAGS_ALPHA_BLEND UINT8_C(1 << 1)
41#define IMGUI_FLAGS_FLIP_UV UINT8_C(1 << 2)
42#define IMGUI_FLAGS_CUBEMAP UINT8_C(1 << 3)
62
63uint64_t GetDrawCalls();
64
65void PushFont(Font::Enum _font);
66ImFont* GetFont(Font::Enum _font);
67void PushWindowFontSize(int size);
69void PushWindowFontScale(float scale);
71
73
75{
76 struct
77 {
80 uint8_t flags;
81 uint8_t mip;
82 uint8_t index;
83 } s;
84 ImTextureID id;
85};
86
88 uint8_t _index,
89 gfx::program_handle _phandle,
90 uint8_t _mip = 0,
91 uint8_t _flags = IMGUI_FLAGS_ALPHA_BLEND)
92{
93 ImTexture tex;
94 tex.s.handle = _handle;
95 tex.s.phandle = _phandle;
96 tex.s.flags = _flags;
97 tex.s.mip = _mip;
98 tex.s.index = _index;
99 return tex;
100}
102inline ImTextureID ToId(gfx::texture_handle _handle, uint8_t _mip = 0, uint8_t _flags = IMGUI_FLAGS_ALPHA_BLEND)
103{
104 ImTexture tex = ToTex(_handle, 0, {gfx::invalid_handle}, _mip, _flags);
105 return tex.id;
106}
107
108inline ImTexture FromId(ImTextureID _id)
109{
110 ImTexture tex;
111 tex.id = _id;
112 return tex;
113}
114
115inline ImTextureID ToId(const gfx::texture& _handle, uint8_t _mip = 0, uint8_t _flags = IMGUI_FLAGS_ALPHA_BLEND)
116{
117 if(_handle.is_valid() && _handle.is_render_target() && gfx::is_origin_bottom_left())
118 {
119 _flags |= IMGUI_FLAGS_FLIP_UV;
120 }
121
122 if(_handle.info.cubeMap)
123 {
124 _flags |= IMGUI_FLAGS_CUBEMAP;
125 }
126
127 return ToId(_handle.native_handle(), _mip, _flags);
128}
129
130inline ImTextureID ToId(const gfx::texture::ptr& _handle, uint8_t _mip = 0, uint8_t _flags = IMGUI_FLAGS_ALPHA_BLEND)
131{
132 if(!_handle)
133 {
134 return {};
135 }
136
137 KeepAliveOneFrame(_handle);
138 return ToId(*_handle, _mip, _flags);
139}
140
141inline ImTextureID ToId(const asset_handle<gfx::texture>& _handle,
142 uint8_t _mip = 0,
143 uint8_t _flags = IMGUI_FLAGS_ALPHA_BLEND)
144{
145 return ToId(_handle.get(), _mip, _flags);
146}
147
148inline ImVec2 GetSize(const gfx::texture& tex, const ImVec2& fallback = {})
149{
150 return ImVec2{float(tex.info.width * (tex.info.cubeMap ? 4 : 1)), float(tex.info.height * (tex.info.cubeMap ? 3 : 1))};
151}
152
153inline ImVec2 GetSize(const gfx::texture::ptr& tex, const ImVec2& fallback = {})
154{
155 if(tex)
156 {
157 return GetSize(*tex, fallback);
158 }
159
160 return fallback;
161}
162
163inline ImVec2 GetSize(const asset_handle<gfx::texture>& _handle, const ImVec2& fallback = {})
164{
165 if(_handle.is_ready())
166 {
167 return GetSize(_handle.get(), fallback);
168 }
169
170 return fallback;
171}
172
173// Helper function for passing gfx::texture_handle to ImGui::Image.
174inline void Image(gfx::texture_handle _handle,
175 uint8_t _mip,
176 uint8_t _flags,
177 const ImVec2& _size,
178 const ImVec2& _uv0 = ImVec2(0.0f, 0.0f),
179 const ImVec2& _uv1 = ImVec2(1.0f, 1.0f))
180{
181 Image(ToId(_handle, _mip, _flags), _size, _uv0, _uv1);
182}
183
184// Helper function for passing gfx::texture_handle to ImGui::Image.
185inline void Image(gfx::texture_handle _handle,
186 const ImVec2& _size,
187 const ImVec2& _uv0 = ImVec2(0.0f, 0.0f),
188 const ImVec2& _uv1 = ImVec2(1.0f, 1.0f))
189{
190 Image(_handle, 0, IMGUI_FLAGS_ALPHA_BLEND, _size, _uv0, _uv1);
191}
192
193// Helper function for passing gfx::texture_handle to ImGui::ImageButton.
195 uint8_t _mip,
196 uint8_t _flags,
197 const ImVec2& _size,
198 const ImVec2& _uv0 = ImVec2(0.0f, 0.0f),
199 const ImVec2& _uv1 = ImVec2(1.0f, 1.0f),
200 const ImVec4& _bgCol = ImVec4(0.0f, 0.0f, 0.0f, 0.0f),
201 const ImVec4& _tintCol = ImVec4(1.0f, 1.0f, 1.0f, 1.0f))
202{
203 return ImageButton("image", ToId(_handle, _mip, _flags), _size, _uv0, _uv1, _bgCol, _tintCol);
204}
205
206// Helper function for passing gfx::texture_handle to ImGui::ImageButton.
208 const ImVec2& _size,
209 const ImVec2& _uv0 = ImVec2(0.0f, 0.0f),
210 const ImVec2& _uv1 = ImVec2(1.0f, 1.0f),
211 const ImVec4& _bgCol = ImVec4(0.0f, 0.0f, 0.0f, 0.0f),
212 const ImVec4& _tintCol = ImVec4(1.0f, 1.0f, 1.0f, 1.0f))
213{
214 return ImageButton(_handle, 0, IMGUI_FLAGS_ALPHA_BLEND, _size, _uv0, _uv1, _bgCol, _tintCol);
215}
216
218inline void NextLine()
219{
220 SetCursorPosY(GetCursorPosY() + GetTextLineHeightWithSpacing());
221}
222
224inline bool MouseOverArea()
225{
226 return false || ImGui::IsAnyItemActive() || ImGui::IsAnyItemHovered() ||
227 ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow) || ImGuizmo::IsOver();
228}
229
230bool IsReadonly();
232void PushReadonly(bool _enabled);
233
235void PopReadonly();
237void PushEnabled(bool _enabled);
238
240void PopEnabled();
241
242} // namespace ImGui
243
244#endif // IMGUI_H_HEADER_GUARD
auto is_valid() const -> bool
Definition handle_impl.h:35
auto native_handle() const -> T
Definition handle_impl.h:40
float scale
Definition hub.cpp:25
#define IMGUI_FLAGS_CUBEMAP
Definition imgui.h:42
#define IMGUI_FLAGS_ALPHA_BLEND
Definition imgui.h:40
void imguiCreate(unravel::render_window *window, float _fontSize=18.0f, bx::AllocatorI *_allocator=NULL)
Definition imgui.cpp:588
void imguiProcessEvent(os::event &e)
Definition imgui.cpp:598
void imguiBeginFrame(float dt)
Definition imgui.cpp:603
#define IMGUI_FLAGS_FLIP_UV
Definition imgui.h:41
void imguiEndFrame(gfx::view_id id)
Definition imgui.cpp:609
void imguiDestroy()
Definition imgui.cpp:593
uint32_t imguiRGBA(uint8_t _r, uint8_t _g, uint8_t _b, uint8_t _a=255)
Definition imgui.h:19
void PushWindowFontSize(int size)
Definition imgui.cpp:666
void NextLine()
Definition imgui.h:218
void Image(gfx::texture_handle _handle, uint8_t _mip, uint8_t _flags, const ImVec2 &_size, const ImVec2 &_uv0=ImVec2(0.0f, 0.0f), const ImVec2 &_uv1=ImVec2(1.0f, 1.0f))
Definition imgui.h:174
void PushFont(Font::Enum _font)
Definition imgui.cpp:617
ImTexture ToTex(gfx::texture_handle _handle, uint8_t _index, gfx::program_handle _phandle, uint8_t _mip=0, uint8_t _flags=IMGUI_FLAGS_ALPHA_BLEND)
Definition imgui.h:87
bool ImageButton(gfx::texture_handle _handle, uint8_t _mip, uint8_t _flags, const ImVec2 &_size, const ImVec2 &_uv0=ImVec2(0.0f, 0.0f), const ImVec2 &_uv1=ImVec2(1.0f, 1.0f), const ImVec4 &_bgCol=ImVec4(0.0f, 0.0f, 0.0f, 0.0f), const ImVec4 &_tintCol=ImVec4(1.0f, 1.0f, 1.0f, 1.0f))
Definition imgui.h:194
void PushEnabled(bool _enabled)
Definition imgui.cpp:628
ImTextureID ToId(gfx::texture_handle _handle, uint8_t _mip=0, uint8_t _flags=IMGUI_FLAGS_ALPHA_BLEND)
Definition imgui.h:102
void PopWindowFontScale()
Definition imgui.cpp:705
ImTexture FromId(ImTextureID _id)
Definition imgui.h:108
bool MouseOverArea()
Definition imgui.h:224
void PopEnabled()
Definition imgui.cpp:635
bool IsReadonly()
Definition imgui.cpp:643
uint64_t GetDrawCalls()
Definition imgui.cpp:715
ImFont * GetFont(Font::Enum _font)
Definition imgui.cpp:623
void KeepAliveOneFrame(const gfx::texture::ptr &tex)
Definition imgui.cpp:720
void PushWindowFontScale(float scale)
Definition imgui.cpp:692
ImVec2 GetSize(const gfx::texture &tex, const ImVec2 &fallback={})
Definition imgui.h:148
void PopWindowFontSize()
Definition imgui.cpp:682
void PopReadonly()
Definition imgui.cpp:658
void PushReadonly(bool _enabled)
Definition imgui.cpp:649
Definition imgui.h:25
bgfx::TextureHandle texture_handle
Definition graphics.h:49
bgfx::ViewId view_id
Definition graphics.h:20
bgfx::ProgramHandle program_handle
Definition graphics.h:47
auto is_origin_bottom_left() -> bool
@ BigIcons
Definition imgui.h:58
@ ExtraBold
Definition imgui.h:54
@ SemiBold
Definition imgui.h:52
@ ExtraLight
Definition imgui.h:48
@ Regular
Definition imgui.h:50
Represents a handle to an asset, providing access and management functions.
auto get(bool wait=true) const -> std::shared_ptr< T >
Gets the shared pointer to the asset.
auto is_ready() const -> bool
Checks if the task is ready.
auto is_render_target() const -> bool
Definition texture.cpp:73
texture_info info
Texture detail info.
Definition texture.h:95
Struct representing a render window.
uint8_t mip
Definition imgui.h:81
uint8_t flags
Definition imgui.h:80
ImTextureID id
Definition imgui.h:84
struct ImGui::ImTexture::@57 s
gfx::program_handle phandle
Definition imgui.h:79
gfx::texture_handle handle
Definition imgui.h:78
uint8_t index
Definition imgui.h:82