11 #if defined IGRAPHICS_IMGUI 14 #include "IGraphicsImGui.h" 17 #if defined IGRAPHICS_GL2 18 #include "imgui_impl_opengl2.h" 19 #elif defined IGRAPHICS_GL3 || defined IGRAPHICS_GLES2 || defined IGRAPHICS_GLES3 20 #include "imgui_impl_opengl3.h" 22 #if defined OS_MAC || defined OS_IOS 23 #import <Metal/Metal.h> 24 #import <QuartzCore/QuartzCore.h> 25 #include "imgui_impl_metal.h" 27 #error "ImGui is only supported on this platform using the OpenGL based backends" 31 using namespace iplug;
32 using namespace igraphics;
34 ImGuiRenderer::ImGuiRenderer(
IGraphics* pGraphics, std::function<
void(
IGraphics*)> drawFunc, std::function<
void()> setupFunc)
35 : mGraphics(pGraphics)
39 mCtx = ImGui::CreateContext();
40 ImGuiIO& io = ImGui::GetIO();
42 if(setupFunc ==
nullptr)
44 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
45 ImGui::StyleColorsDark();
51 io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors;
52 io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos;
53 io.BackendPlatformName =
"imgui_impl_igraphics";
55 io.KeyMap[ImGuiKey_Tab] = kVK_TAB;
56 io.KeyMap[ImGuiKey_LeftArrow] = kVK_LEFT;
57 io.KeyMap[ImGuiKey_RightArrow] = kVK_RIGHT;
58 io.KeyMap[ImGuiKey_UpArrow] = kVK_UP;
59 io.KeyMap[ImGuiKey_DownArrow] = kVK_DOWN;
60 io.KeyMap[ImGuiKey_PageUp] = kVK_PRIOR;
61 io.KeyMap[ImGuiKey_PageDown] = kVK_NEXT;
62 io.KeyMap[ImGuiKey_Home] = kVK_HOME;
63 io.KeyMap[ImGuiKey_End] = kVK_END;
64 io.KeyMap[ImGuiKey_Insert] = kVK_INSERT;
65 io.KeyMap[ImGuiKey_Delete] = kVK_DELETE;
66 io.KeyMap[ImGuiKey_Backspace] = kVK_BACK;
67 io.KeyMap[ImGuiKey_Space] = kVK_SPACE;
68 io.KeyMap[ImGuiKey_Enter] = kVK_RETURN;
69 io.KeyMap[ImGuiKey_Escape] = kVK_ESCAPE;
70 io.KeyMap[ImGuiKey_A] =
'A';
71 io.KeyMap[ImGuiKey_C] =
'C';
72 io.KeyMap[ImGuiKey_V] =
'V';
73 io.KeyMap[ImGuiKey_X] =
'X';
74 io.KeyMap[ImGuiKey_Y] =
'Y';
75 io.KeyMap[ImGuiKey_Z] =
'Z';
88 ImGuiRenderer::~ImGuiRenderer()
91 ImGui::DestroyContext(mCtx);
95 void ImGuiRenderer::DoFrame()
97 ImGui::SetCurrentContext(mCtx);
98 ImGuiIO &io = ImGui::GetIO();
99 io.DisplaySize.x = std::round(mGraphics->Width() * mGraphics->GetDrawScale());
100 io.DisplaySize.y = std::round(mGraphics->Height() * mGraphics->GetDrawScale());
101 float scale = (float) mGraphics->GetScreenScale();
102 io.DisplayFramebufferScale = ImVec2(scale, scale);
103 io.DeltaTime = 1.f / mGraphics->FPS();
105 if (!(io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange))
107 ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
108 if (imgui_cursor == ImGuiMouseCursor_None || io.MouseDrawCursor)
110 mGraphics->HideMouseCursor();
112 else if (io.WantCaptureMouse)
116 case ImGuiMouseCursor_Arrow: mGraphics->SetMouseCursor(ECursor::ARROW);
break;
117 case ImGuiMouseCursor_TextInput: mGraphics->SetMouseCursor(ECursor::IBEAM);
break;
118 case ImGuiMouseCursor_ResizeAll: mGraphics->SetMouseCursor(ECursor::SIZEALL);
break;
119 case ImGuiMouseCursor_ResizeNS: mGraphics->SetMouseCursor(ECursor::SIZENS);
break;
120 case ImGuiMouseCursor_ResizeEW: mGraphics->SetMouseCursor(ECursor::SIZEWE);
break;
121 case ImGuiMouseCursor_ResizeNESW: mGraphics->SetMouseCursor(ECursor::SIZENESW);
break;
122 case ImGuiMouseCursor_ResizeNWSE: mGraphics->SetMouseCursor(ECursor::SIZENWSE);
break;
123 case ImGuiMouseCursor_Hand: mGraphics->SetMouseCursor(ECursor::HAND);
break;
125 mGraphics->HideMouseCursor(
false);
129 if (io.WantSetMousePos)
131 mGraphics->MoveMouseCursor(io.MousePos.x, io.MousePos.y);
134 ImGui::SetCurrentContext(mCtx);
138 mDrawFunc(mGraphics);
143 bool ImGuiRenderer::OnMouseDown(
float x,
float y,
const IMouseMod &mod)
145 ImGui::SetCurrentContext(mCtx);
146 ImGuiIO &io = ImGui::GetIO();
147 io.MouseDown[0] = mod.L;
148 io.MouseDown[1] = mod.R;
149 return io.WantCaptureMouse;
152 bool ImGuiRenderer::OnMouseUp(
float x,
float y,
const IMouseMod &mod)
154 ImGui::SetCurrentContext(mCtx);
155 ImGuiIO &io = ImGui::GetIO();
156 io.MouseDown[0] =
false;
157 io.MouseDown[1] =
false;
158 return io.WantCaptureMouse;
161 bool ImGuiRenderer::OnMouseWheel(
float x,
float y,
const IMouseMod& mod,
float delta)
163 ImGui::SetCurrentContext(mCtx);
164 ImGuiIO &io = ImGui::GetIO();
165 io.MouseWheel += (delta * 0.1f);
166 return io.WantCaptureMouse;
169 void ImGuiRenderer::OnMouseMove(
float x,
float y,
const IMouseMod& mod)
171 ImGui::SetCurrentContext(mCtx);
172 ImGuiIO &io = ImGui::GetIO();
173 io.MousePos = ImVec2(x * mGraphics->GetDrawScale(), y * mGraphics->GetDrawScale());
176 bool ImGuiRenderer::OnKeyDown(
float x,
float y,
const IKeyPress& keyPress)
178 ImGui::SetCurrentContext(mCtx);
179 ImGuiIO &io = ImGui::GetIO();
181 if(keyPress.VK != kVK_BACK)
182 io.AddInputCharactersUTF8(keyPress.utf8);
184 io.KeysDown[keyPress.VK] =
true;
186 io.KeyCtrl = keyPress.C;
187 io.KeyShift = keyPress.S;
188 io.KeyAlt = keyPress.A;
190 return io.WantCaptureKeyboard;
193 bool ImGuiRenderer::OnKeyUp(
float x,
float y,
const IKeyPress& keyPress)
195 ImGui::SetCurrentContext(mCtx);
196 ImGuiIO &io = ImGui::GetIO();
197 io.KeysDown[keyPress.VK] =
false;
198 return io.WantCaptureKeyboard;
201 void ImGuiRenderer::Init()
203 #if defined IGRAPHICS_SKIA 204 #if defined IGRAPHICS_CPU // ImGui drawn via SKIA when !CPU 205 ImGui_ImplMetal_Init(MTLCreateSystemDefaultDevice());
207 ImGuiIO& io = ImGui::GetIO();
209 unsigned char* pixels;
210 io.Fonts->GetTexDataAsAlpha8(&pixels, &w, &h);
211 SkImageInfo info = SkImageInfo::MakeA8(w, h);
212 SkPixmap pmap(info, pixels, info.minRowBytes());
213 SkMatrix localMatrix = SkMatrix::Scale(1.0f / w, 1.0f / h);
214 auto fontImage = SkImage::MakeFromRaster(pmap,
nullptr,
nullptr);
215 auto fontShader = fontImage->makeShader(&localMatrix);
216 fFontPaint.setShader(fontShader);
217 fFontPaint.setColor(SK_ColorWHITE);
218 fFontPaint.setFilterQuality(kLow_SkFilterQuality);
219 io.Fonts->TexID = &fFontPaint;
222 #if defined IGRAPHICS_GL2 223 ImGui_ImplOpenGL2_Init();
224 #elif defined IGRAPHICS_GL3 225 ImGui_ImplOpenGL3_Init();
226 #elif defined IGRAPHICS_GLES2 || defined IGRAPHICS_GLES3 227 const char* glsl_version =
"#version 100";
229 ImGui_ImplOpenGL3_Init(glsl_version);
230 #elif defined IGRAPHICS_METAL 231 ImGui_ImplMetal_Init(MTLCreateSystemDefaultDevice());
236 void ImGuiRenderer::Destroy()
238 ImGui::SetCurrentContext(mCtx);
240 #if defined IGRAPHICS_GL2 241 ImGui_ImplOpenGL2_Shutdown();
242 #elif defined IGRAPHICS_GL3 || defined IGRAPHICS_GLES2 || defined IGRAPHICS_GLES3 243 ImGui_ImplOpenGL3_Shutdown();
244 #elif defined IGRAPHICS_METAL 245 ImGui_ImplMetal_Shutdown();
249 void ImGuiRenderer::NewFrame()
251 ImGui::SetCurrentContext(mCtx);
253 #if defined IGRAPHICS_SKIA 254 ImGuiIO& io = ImGui::GetIO();
256 unsigned char* pixels;
257 io.Fonts->GetTexDataAsAlpha8(&pixels, &w, &h);
258 SkImageInfo info = SkImageInfo::MakeA8(w, h);
259 SkPixmap pmap(info, pixels, info.minRowBytes());
260 SkMatrix localMatrix = SkMatrix::Scale(1.0f / w, 1.0f / h);
261 auto fontImage = SkImage::MakeFromRaster(pmap,
nullptr,
nullptr);
262 auto fontShader = fontImage->makeShader(&localMatrix);
263 fFontPaint.setShader(fontShader);
264 fFontPaint.setColor(SK_ColorWHITE);
265 fFontPaint.setFilterQuality(kLow_SkFilterQuality);
266 io.Fonts->TexID = &fFontPaint;
268 #elif defined IGRAPHICS_GL2 269 ImGui_ImplOpenGL2_NewFrame();
271 ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
272 #elif defined IGRAPHICS_GL3 || defined IGRAPHICS_GLES2 || defined IGRAPHICS_GLES3 273 ImGui_ImplOpenGL3_NewFrame();
276 #if defined IGRAPHICS_GL2 277 ImGui_ImplOpenGL2_NewFrame();
279 ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
280 #elif defined IGRAPHICS_GL3 || defined IGRAPHICS_GLES2 || defined IGRAPHICS_GLES3 281 ImGui_ImplOpenGL3_NewFrame();
283 ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
291 #include "imgui_widgets.cpp" 292 #include "imgui_draw.cpp" 293 #include "imgui_demo.cpp" 294 #include "imgui_tables.cpp" 296 #if defined IGRAPHICS_GL2 297 #include "imgui_impl_opengl2.cpp" 298 #elif defined IGRAPHICS_GL3 || defined IGRAPHICS_GLES2 || defined IGRAPHICS_GLES3 299 #include "imgui_impl_opengl3.cpp" 304 #endif //IGRAPHICS_IMGUI Used to manage mouse modifiers i.e.
Used for choosing a drawing backend.
The lowest level base class of an IGraphics context.
void Init(const IRECT &r, YGFlexDirection direction=YGFlexDirectionRow, YGJustify justify=YGJustifyFlexStart, YGWrap wrap=YGWrapNoWrap, float padding=0.f, float margin=0.f)
Initialize the IFlexBox flex container.
Used for key press info, such as ASCII representation, virtual key (mapped to win32 codes) and modifi...