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);
31void imguiDestroy();
32
33void imguiProcessEvent(os::event& e);
34
35void imguiBeginFrame(float dt);
37
38namespace ImGui
39{
40#define IMGUI_FLAGS_NONE UINT8_C(1 << 0)
41#define IMGUI_FLAGS_ALPHA_BLEND UINT8_C(1 << 1)
42#define IMGUI_FLAGS_FLIP_UV UINT8_C(1 << 2)
43#define IMGUI_FLAGS_CUBEMAP UINT8_C(1 << 3)
63
64uint64_t GetDrawCalls();
65
66void PushFont(Font::Enum _font);
67ImFont* GetFont(Font::Enum _font);
68void PushWindowFontSize(int size);
70void PushWindowFontScale(float scale);
72
74
76{
77 struct
78 {
81 uint8_t flags;
82 uint8_t mip;
83 uint8_t index;
84 } s;
85 ImTextureID id;
86};
87
89 uint8_t _index,
90 gfx::program_handle _phandle,
91 uint8_t _mip = 0,
92 uint8_t _flags = IMGUI_FLAGS_ALPHA_BLEND)
93{
94 ImTexture tex;
95 tex.s.handle = _handle;
96 tex.s.phandle = _phandle;
97 tex.s.flags = _flags;
98 tex.s.mip = _mip;
99 tex.s.index = _index;
100 return tex;
101}
103inline ImTextureID ToId(gfx::texture_handle _handle, uint8_t _mip = 0, uint8_t _flags = IMGUI_FLAGS_ALPHA_BLEND)
104{
105 ImTexture tex = ToTex(_handle, 0, {gfx::invalid_handle}, _mip, _flags);
106 return tex.id;
107}
108
109inline ImTexture FromId(ImTextureID _id)
110{
111 ImTexture tex;
112 tex.id = _id;
113 return tex;
114}
115
116inline ImTextureID ToId(const gfx::texture& _handle, uint8_t _mip = 0, uint8_t _flags = IMGUI_FLAGS_ALPHA_BLEND)
117{
118 if(_handle.is_valid() && _handle.is_render_target() && gfx::is_origin_bottom_left())
119 {
120 _flags |= IMGUI_FLAGS_FLIP_UV;
121 }
122
123 if(_handle.info.cubeMap)
124 {
125 _flags |= IMGUI_FLAGS_CUBEMAP;
126 }
127
128 return ToId(_handle.native_handle(), _mip, _flags);
129}
130
131inline ImTextureID ToId(const gfx::texture::ptr& _handle, uint8_t _mip = 0, uint8_t _flags = IMGUI_FLAGS_ALPHA_BLEND)
132{
133 if(!_handle)
134 {
135 return {};
136 }
137
138 KeepAliveOneFrame(_handle);
139 return ToId(*_handle, _mip, _flags);
140}
141
142inline ImTextureID ToId(const asset_handle<gfx::texture>& _handle,
143 uint8_t _mip = 0,
144 uint8_t _flags = IMGUI_FLAGS_ALPHA_BLEND)
145{
146 return ToId(_handle.get(), _mip, _flags);
147}
148
149inline ImVec2 GetSize(const gfx::texture& tex, const ImVec2& fallback = {})
150{
151 return ImVec2{float(tex.info.width * (tex.info.cubeMap ? 4 : 1)), float(tex.info.height * (tex.info.cubeMap ? 3 : 1))};
152}
153
154inline ImVec2 GetSize(const gfx::texture::ptr& tex, const ImVec2& fallback = {})
155{
156 if(tex)
157 {
158 return GetSize(*tex, fallback);
159 }
160
161 return fallback;
162}
163
164inline ImVec2 GetSize(const asset_handle<gfx::texture>& _handle, const ImVec2& fallback = {})
165{
166 if(_handle.is_ready())
167 {
168 return GetSize(_handle.get(), fallback);
169 }
170
171 return fallback;
172}
173
174// Helper function for passing gfx::texture_handle to ImGui::Image.
175inline void Image(gfx::texture_handle _handle,
176 uint8_t _mip,
177 uint8_t _flags,
178 const ImVec2& _size,
179 const ImVec2& _uv0 = ImVec2(0.0f, 0.0f),
180 const ImVec2& _uv1 = ImVec2(1.0f, 1.0f))
181{
182 Image(ToId(_handle, _mip, _flags), _size, _uv0, _uv1);
183}
184
185// Helper function for passing gfx::texture_handle to ImGui::Image.
186inline void Image(gfx::texture_handle _handle,
187 const ImVec2& _size,
188 const ImVec2& _uv0 = ImVec2(0.0f, 0.0f),
189 const ImVec2& _uv1 = ImVec2(1.0f, 1.0f))
190{
191 Image(_handle, 0, IMGUI_FLAGS_ALPHA_BLEND, _size, _uv0, _uv1);
192}
193
194// Helper function for passing gfx::texture_handle to ImGui::ImageButton.
196 uint8_t _mip,
197 uint8_t _flags,
198 const ImVec2& _size,
199 const ImVec2& _uv0 = ImVec2(0.0f, 0.0f),
200 const ImVec2& _uv1 = ImVec2(1.0f, 1.0f),
201 const ImVec4& _bgCol = ImVec4(0.0f, 0.0f, 0.0f, 0.0f),
202 const ImVec4& _tintCol = ImVec4(1.0f, 1.0f, 1.0f, 1.0f))
203{
204 return ImageButton("image", ToId(_handle, _mip, _flags), _size, _uv0, _uv1, _bgCol, _tintCol);
205}
206
207// Helper function for passing gfx::texture_handle to ImGui::ImageButton.
209 const ImVec2& _size,
210 const ImVec2& _uv0 = ImVec2(0.0f, 0.0f),
211 const ImVec2& _uv1 = ImVec2(1.0f, 1.0f),
212 const ImVec4& _bgCol = ImVec4(0.0f, 0.0f, 0.0f, 0.0f),
213 const ImVec4& _tintCol = ImVec4(1.0f, 1.0f, 1.0f, 1.0f))
214{
215 return ImageButton(_handle, 0, IMGUI_FLAGS_ALPHA_BLEND, _size, _uv0, _uv1, _bgCol, _tintCol);
216}
217
219inline void NextLine()
220{
221 SetCursorPosY(GetCursorPosY() + GetTextLineHeightWithSpacing());
222}
223
225inline bool MouseOverArea()
226{
227 return false || ImGui::IsAnyItemActive() || ImGui::IsAnyItemHovered() ||
228 ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow) || ImGuizmo::IsOver();
229}
230
231bool IsReadonly();
233void PushReadonly(bool _enabled);
234
236void PopReadonly();
238void PushEnabled(bool _enabled);
239
241void PopEnabled();
242
243} // namespace ImGui
244
245#endif // IMGUI_H_HEADER_GUARD
auto is_valid() const -> bool
Definition handle_impl.h:48
auto native_handle() const -> T
Definition handle_impl.h:53
#define IMGUI_FLAGS_CUBEMAP
Definition imgui.h:43
#define IMGUI_FLAGS_ALPHA_BLEND
Definition imgui.h:41
void imguiCreate(unravel::render_window *window, float _fontSize=18.0f, bx::AllocatorI *_allocator=NULL)
Definition imgui.cpp:612
void imguiProcessEvent(os::event &e)
Definition imgui.cpp:627
void imguiBeginFrame(float dt)
Definition imgui.cpp:632
#define IMGUI_FLAGS_FLIP_UV
Definition imgui.h:42
void imguiEndFrame(gfx::view_id id)
Definition imgui.cpp:638
void imguiCreateCubemapProgram()
Definition imgui.cpp:617
void imguiDestroy()
Definition imgui.cpp:622
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:695
void NextLine()
Definition imgui.h:219
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:175
void PushFont(Font::Enum _font)
Definition imgui.cpp:646
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:88
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:195
void PushEnabled(bool _enabled)
Definition imgui.cpp:657
ImTextureID ToId(gfx::texture_handle _handle, uint8_t _mip=0, uint8_t _flags=IMGUI_FLAGS_ALPHA_BLEND)
Definition imgui.h:103
void PopWindowFontScale()
Definition imgui.cpp:734
ImTexture FromId(ImTextureID _id)
Definition imgui.h:109
bool MouseOverArea()
Definition imgui.h:225
void PopEnabled()
Definition imgui.cpp:664
bool IsReadonly()
Definition imgui.cpp:672
uint64_t GetDrawCalls()
Definition imgui.cpp:744
ImFont * GetFont(Font::Enum _font)
Definition imgui.cpp:652
void KeepAliveOneFrame(const gfx::texture::ptr &tex)
Definition imgui.cpp:749
void PushWindowFontScale(float scale)
Definition imgui.cpp:721
ImVec2 GetSize(const gfx::texture &tex, const ImVec2 &fallback={})
Definition imgui.h:149
void PopWindowFontSize()
Definition imgui.cpp:711
void PopReadonly()
Definition imgui.cpp:687
void PushReadonly(bool _enabled)
Definition imgui.cpp:678
Definition imgui.h:25
bgfx::TextureHandle texture_handle
Definition graphics.h:51
static const uint16_t invalid_handle
Definition graphics.h:18
bgfx::ViewId view_id
Definition graphics.h:20
bgfx::ProgramHandle program_handle
Definition graphics.h:49
auto is_origin_bottom_left() -> bool
std::vector< float > scale
@ BigIcons
Definition imgui.h:59
@ ExtraBold
Definition imgui.h:55
@ SemiBold
Definition imgui.h:53
@ ExtraLight
Definition imgui.h:49
@ Regular
Definition imgui.h:51
Thread-safe handle to an asset.
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:344
texture_info info
Texture detail info.
Definition texture.h:125
Struct representing a render window.
uint8_t mip
Definition imgui.h:82
uint8_t flags
Definition imgui.h:81
ImTextureID id
Definition imgui.h:85
struct ImGui::ImTexture::@58 s
gfx::program_handle phandle
Definition imgui.h:80
gfx::texture_handle handle
Definition imgui.h:79
uint8_t index
Definition imgui.h:83