-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathImGuiContext.cpp
216 lines (178 loc) · 7.92 KB
/
ImGuiContext.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "ImGuiContext.h"
#include "imgui_impl_dx11.h"
#include "imgui_impl_win32.h"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tchar.h>
#include <d3d.h>
#include <d3d11.h>
#include <d3dcompiler.h>
#include <dxgi.h>
#include <stdio.h>
#include "imgui.h"
#include "imgui_internal.h"
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "d3dcompiler.lib")
#pragma comment(lib, "dxgi.lib")
#pragma comment(lib, "imm32.lib")
extern HWND g_hWnd;
extern ID3D11DeviceContext* g_pd3dDeviceContext;
extern ID3D11Device* g_pd3dDevice;
extern IDXGIFactory1* g_pFactory;
static ID3D11RenderTargetView* g_mainRenderTargetView;
//extern LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
extern void ImGui_ImplWin32_InitPlatformInterface();
extern void ImGui_ImplDX11_InitPlatformInterface();
namespace ImGuiCLI
{
ImGuiContext::ImGuiContext(System::IntPtr hwnd, System::IntPtr devicePtr, System::IntPtr mainDeviceContext, System::IntPtr renderTarget)
{
g_hWnd = (HWND)hwnd.ToInt64();
g_pd3dDeviceContext = (ID3D11DeviceContext*)mainDeviceContext.ToPointer();
g_pd3dDevice = (ID3D11Device*)devicePtr.ToPointer();
ImGui::CreateContext();
ImGui_ImplDX11_Init(g_pd3dDevice, g_pd3dDeviceContext);
g_mainRenderTargetView = (ID3D11RenderTargetView*)renderTarget.ToPointer();
// Setup ImGui binding
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
io.ConfigFlags |= ImGuiConfigFlags_ViewportsNoTaskBarIcons;
io.ConfigFlags |= ImGuiConfigFlags_DpiEnableScaleFonts;
io.ConfigFlags |= ImGuiConfigFlags_DpiEnableScaleViewports;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
// Setup back-end capabilities flags
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional)
io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; // We can set io.MouseHoveredViewport correctly (optional, not easy)
ImGuiViewport* main_viewport = ImGui::GetMainViewport();
main_viewport->PlatformHandle = (void*)g_hWnd;
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
ImGui_ImplWin32_InitPlatformInterface();
//ImGui::GetIO().ImeWindowHandle = g_hWnd;
// Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array that we will update during the application lifetime.
io.KeyMap[ImGuiKey_Tab] = VK_TAB;
io.KeyMap[ImGuiKey_LeftArrow] = VK_LEFT;
io.KeyMap[ImGuiKey_RightArrow] = VK_RIGHT;
io.KeyMap[ImGuiKey_UpArrow] = VK_UP;
io.KeyMap[ImGuiKey_DownArrow] = VK_DOWN;
io.KeyMap[ImGuiKey_PageUp] = VK_PRIOR;
io.KeyMap[ImGuiKey_PageDown] = VK_NEXT;
io.KeyMap[ImGuiKey_Home] = VK_HOME;
io.KeyMap[ImGuiKey_End] = VK_END;
io.KeyMap[ImGuiKey_Insert] = VK_INSERT;
io.KeyMap[ImGuiKey_Delete] = VK_DELETE;
io.KeyMap[ImGuiKey_Backspace] = VK_BACK;
io.KeyMap[ImGuiKey_Space] = VK_SPACE;
io.KeyMap[ImGuiKey_Enter] = VK_RETURN;
io.KeyMap[ImGuiKey_Escape] = VK_ESCAPE;
io.KeyMap[ImGuiKey_A] = 'A';
io.KeyMap[ImGuiKey_C] = 'C';
io.KeyMap[ImGuiKey_V] = 'V';
io.KeyMap[ImGuiKey_X] = 'X';
io.KeyMap[ImGuiKey_Y] = 'Y';
io.KeyMap[ImGuiKey_Z] = 'Z';
io.Fonts->AddFontDefault();
// Setup back-end capabilities flags
io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional)
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
ImGui_ImplDX11_InitPlatformInterface();
// Setup style
ImGui::StyleColorsDark();
//ImGui::StyleColorsClassic();
ImGuiStyle& style = ImGui::GetStyle();
style.WindowRounding = 0.0f;
ImGui_ImplDX11_CreateDeviceObjects();
}
ImGuiContext::~ImGuiContext()
{
}
void ImGuiContext::NewFrame(int w, int h)
{
extern ID3D11SamplerState* g_pFontSampler;
if (!g_pFontSampler)
ImGui_ImplDX11_CreateDeviceObjects();
auto& io = ImGui::GetIO();
io.DisplaySize.x = w;
io.DisplaySize.y = h;
extern void ImGui_ImplDX11_NewFrame();
extern void ImGui_ImplWin32_NewFrame();
ImGui_ImplDX11_NewFrame();
ImGui_ImplWin32_NewFrame();
}
void ImGuiContext::Shutdown()
{
extern void ImGui_ImplDX11_ShutdownPlatformInterface();
extern void ImGui_ImplDX11_InvalidateDeviceObjects();
ImGui_ImplDX11_ShutdownPlatformInterface();
ImGui_ImplDX11_InvalidateDeviceObjects();
extern void ImGui_ImplWin32_ShutdownPlatformInterface();
ImGui_ImplWin32_ShutdownPlatformInterface();
ImGui::DestroyContext();
}
void ImGuiContext::RenderNoDraw()
{
auto& io = ImGui::GetIO();
ImGui::Render();
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
ImGui::UpdatePlatformWindows();
}
void ImGuiContext::RenderAndDraw(System::IntPtr renderTarget)
{
auto& io = ImGui::GetIO();
g_mainRenderTargetView = (ID3D11RenderTargetView*)renderTarget.ToPointer();
extern void ImGui_ImplDX11_RenderDrawData(ImDrawData*);
// Rendering
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
RecordState();
ImGui::Render();
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
}
RestoreState();
}
void ImGuiContext::Draw(System::IntPtr renderTarget)
{
g_mainRenderTargetView = (ID3D11RenderTargetView*)renderTarget.ToPointer();
extern void ImGui_ImplDX11_RenderDrawData(ImDrawData*);
RecordState();
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
ImGui::RenderPlatformWindowsDefault();
RestoreState();
}
void ImGuiContext::ResizeMain(int width, int height, System::IntPtr mainRenderTarget)
{
}
static ID3D11RenderTargetView* previousTargets[4];
static ID3D11DepthStencilView* previousDepth;
static unsigned previousNumViewports = 0;
static D3D11_VIEWPORT previousViewports[4];
//static ID3DD
void ImGuiContext::RecordState()
{
g_pd3dDeviceContext->OMGetRenderTargets(4, previousTargets, &previousDepth);
g_pd3dDeviceContext->RSGetViewports(&previousNumViewports, nullptr);
g_pd3dDeviceContext->RSGetViewports(&previousNumViewports, previousViewports);
}
void ImGuiContext::RestoreState()
{
// count number of non null views, passing a bad count will mess stuff up
int targetCt = 0;
for (int i = 0; i < 4; ++i)
if (previousTargets[i] != nullptr)
++targetCt;
g_pd3dDeviceContext->OMSetRenderTargets(targetCt, previousTargets, previousDepth);
for (int i = 0; i < 4; ++i)
{
if (previousTargets[i] != nullptr)
previousTargets[i]->Release();
previousTargets[i] = nullptr;
}
if (previousNumViewports)
g_pd3dDeviceContext->RSSetViewports(previousNumViewports, previousViewports);
}
}