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 ImTextureID ToId(const gfx::texture& _handle, uint8_t _mip = 0, uint8_t _flags = IMGUI_FLAGS_ALPHA_BLEND)
109{
110 if(_handle.is_valid() && _handle.is_render_target() && gfx::is_origin_bottom_left())
111 {
112 _flags |= IMGUI_FLAGS_FLIP_UV;
113 }
114
115 if(_handle.info.cubeMap)
116 {
117 _flags |= IMGUI_FLAGS_CUBEMAP;
118 }
119
120 return ToId(_handle.native_handle(), _mip, _flags);
121}
122
123inline ImTextureID ToId(const gfx::texture::ptr& _handle, uint8_t _mip = 0, uint8_t _flags = IMGUI_FLAGS_ALPHA_BLEND)
124{
125 if(!_handle)
126 {
127 return {};
128 }
129
130 KeepAliveOneFrame(_handle);
131 return ToId(*_handle, _mip, _flags);
132}
133
134inline ImTextureID ToId(const asset_handle<gfx::texture>& _handle,
135 uint8_t _mip = 0,
136 uint8_t _flags = IMGUI_FLAGS_ALPHA_BLEND)
137{
138 return ToId(_handle.get(), _mip, _flags);
139}
140
141inline ImVec2 GetSize(const gfx::texture& tex, const ImVec2& fallback = {})
142{
143 return ImVec2{float(tex.info.width * (tex.info.cubeMap ? 4 : 1)), float(tex.info.height * (tex.info.cubeMap ? 3 : 1))};
144}
145
146inline ImVec2 GetSize(const gfx::texture::ptr& tex, const ImVec2& fallback = {})
147{
148 if(tex)
149 {
150 return GetSize(*tex, fallback);
151 }
152
153 return fallback;
154}
155
156inline ImVec2 GetSize(const asset_handle<gfx::texture>& _handle, const ImVec2& fallback = {})
157{
158 if(_handle.is_ready())
159 {
160 return GetSize(_handle.get(), fallback);
161 }
162
163 return fallback;
164}
165
166// Helper function for passing gfx::texture_handle to ImGui::Image.
167inline void Image(gfx::texture_handle _handle,
168 uint8_t _mip,
169 uint8_t _flags,
170 const ImVec2& _size,
171 const ImVec2& _uv0 = ImVec2(0.0f, 0.0f),
172 const ImVec2& _uv1 = ImVec2(1.0f, 1.0f))
173{
174 Image(ToId(_handle, _mip, _flags), _size, _uv0, _uv1);
175}
176
177// Helper function for passing gfx::texture_handle to ImGui::Image.
178inline void Image(gfx::texture_handle _handle,
179 const ImVec2& _size,
180 const ImVec2& _uv0 = ImVec2(0.0f, 0.0f),
181 const ImVec2& _uv1 = ImVec2(1.0f, 1.0f))
182{
183 Image(_handle, 0, IMGUI_FLAGS_ALPHA_BLEND, _size, _uv0, _uv1);
184}
185
186// Helper function for passing gfx::texture_handle to ImGui::ImageButton.
188 uint8_t _mip,
189 uint8_t _flags,
190 const ImVec2& _size,
191 const ImVec2& _uv0 = ImVec2(0.0f, 0.0f),
192 const ImVec2& _uv1 = ImVec2(1.0f, 1.0f),
193 const ImVec4& _bgCol = ImVec4(0.0f, 0.0f, 0.0f, 0.0f),
194 const ImVec4& _tintCol = ImVec4(1.0f, 1.0f, 1.0f, 1.0f))
195{
196 return ImageButton("image", ToId(_handle, _mip, _flags), _size, _uv0, _uv1, _bgCol, _tintCol);
197}
198
199// Helper function for passing gfx::texture_handle to ImGui::ImageButton.
201 const ImVec2& _size,
202 const ImVec2& _uv0 = ImVec2(0.0f, 0.0f),
203 const ImVec2& _uv1 = ImVec2(1.0f, 1.0f),
204 const ImVec4& _bgCol = ImVec4(0.0f, 0.0f, 0.0f, 0.0f),
205 const ImVec4& _tintCol = ImVec4(1.0f, 1.0f, 1.0f, 1.0f))
206{
207 return ImageButton(_handle, 0, IMGUI_FLAGS_ALPHA_BLEND, _size, _uv0, _uv1, _bgCol, _tintCol);
208}
209
211inline void NextLine()
212{
213 SetCursorPosY(GetCursorPosY() + GetTextLineHeightWithSpacing());
214}
215
217inline bool MouseOverArea()
218{
219 return false || ImGui::IsAnyItemActive() || ImGui::IsAnyItemHovered() ||
220 ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow) || ImGuizmo::IsOver();
221}
222
223bool IsReadonly();
225void PushReadonly(bool _enabled);
226
228void PopReadonly();
230void PushEnabled(bool _enabled);
231
233void PopEnabled();
234
235} // namespace ImGui
236
237#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:211
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:167
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:187
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
bool MouseOverArea()
Definition imgui.h:217
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:141
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