-
-
Notifications
You must be signed in to change notification settings - Fork 782
Expand file tree
/
Copy pathmvViewport_linux.cpp
More file actions
303 lines (253 loc) · 9.02 KB
/
mvViewport_linux.cpp
File metadata and controls
303 lines (253 loc) · 9.02 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "mvViewport.h"
#include "mvFontManager.h"
#include "mvLinuxSpecifics.h"
#include "implot.h"
#include "imgui.h"
#include "imnodes.h"
#include "imgui_internal.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include <stdio.h>
#include <stb_image.h>
#include "mvToolManager.h"
static void
glfw_error_callback(int error, const char* description)
{
fprintf(stderr, "Glfw Error %d: %s\n", error, description);
}
static void
window_close_callback(GLFWwindow* window)
{
if (GContext->viewport->disableClose) {
mvSubmitCallback([=]() {
mvRunCallback(GContext->callbackRegistry->onCloseCallback, 0, nullptr, GContext->callbackRegistry->onCloseCallbackUserData);
});
}
else {
GContext->started = false;
}
}
static void
window_size_callback(GLFWwindow* window, int width, int height)
{
GContext->viewport->actualHeight = height;
GContext->viewport->clientHeight = height;
GContext->viewport->actualWidth = width;
GContext->viewport->clientWidth = width;
GContext->viewport->resized = true;
}
static void
mvPrerender()
{
mvViewport* viewport = GContext->viewport;
auto viewportData = (mvViewportData*)viewport->platformSpecifics;
viewport->running = !glfwWindowShouldClose(viewportData->handle);
if (viewport->posDirty)
{
glfwSetWindowPos(viewportData->handle, viewport->xpos, viewport->ypos);
viewport->posDirty = false;
}
if (viewport->sizeDirty)
{
glfwSetWindowSizeLimits(viewportData->handle, (int)viewport->minwidth, (int)viewport->minheight, (int)viewport->maxwidth, (int)viewport->maxheight);
glfwSetWindowSize(viewportData->handle, viewport->actualWidth, viewport->actualHeight);
viewport->sizeDirty = false;
}
if (viewport->modesDirty)
{
glfwSetWindowAttrib(viewportData->handle, GLFW_RESIZABLE, viewport->resizable ? GLFW_TRUE : GLFW_FALSE);
glfwSetWindowAttrib(viewportData->handle, GLFW_DECORATED, viewport->decorated ? GLFW_TRUE : GLFW_FALSE);
glfwSetWindowAttrib(viewportData->handle, GLFW_FLOATING, viewport->alwaysOnTop ? GLFW_TRUE : GLFW_FALSE);
viewport->modesDirty = false;
}
if (viewport->titleDirty)
{
glfwSetWindowTitle(viewportData->handle, viewport->title.c_str());
viewport->titleDirty = false;
}
if (glfwGetWindowAttrib(viewportData->handle, GLFW_ICONIFIED))
{
glfwWaitEvents();
return;
}
// Poll and handle events (inputs, window resize, etc.)
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
if (GContext->IO.waitForInput)
glfwWaitEvents();
else
glfwPollEvents();
if (mvToolManager::GetFontManager().isInvalid())
{
mvToolManager::GetFontManager().rebuildAtlas();
ImGui_ImplOpenGL3_DestroyDeviceObjects();
mvToolManager::GetFontManager().updateAtlas();
}
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
}
mvViewport*
mvCreateViewport(unsigned width, unsigned height)
{
mvViewport* viewport = new mvViewport();
viewport->width = width;
viewport->height = height;
viewport->platformSpecifics = new mvViewportData();
return viewport;
}
void
mvCleanupViewport(mvViewport& viewport)
{
auto viewportData = (mvViewportData*)viewport.platformSpecifics;
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
glfwDestroyWindow(viewportData->handle);
glfwTerminate();
GContext->started = false;
delete viewportData;
viewportData = nullptr;
}
void
mvShowViewport(mvViewport& viewport, bool minimized, bool maximized)
{
auto viewportData = (mvViewportData*)viewport.platformSpecifics;
// Setup window
glfwSetErrorCallback(glfw_error_callback);
glfwInit();
// specifies whether the window framebuffer will be transparent
// if enabled and supported by the system, the window framebuffer
// alpha channel will be used to combine the framebuffer with
// the background (this does not affect window decorations).
glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE);
if (!viewport.resizable)
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
if (viewport.alwaysOnTop)
glfwWindowHint(GLFW_FLOATING, GLFW_TRUE);
if (maximized)
glfwWindowHint(GLFW_MAXIMIZED, GLFW_TRUE);
else if (minimized)
glfwWindowHint(GLFW_AUTO_ICONIFY, GLFW_TRUE);
if (!viewport.decorated)
glfwWindowHint(GLFW_DECORATED, GLFW_FALSE);
// Create window with graphics context
// GL 3.0 + GLSL 130
const char* glsl_version = "#version 130";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
viewportData->handle = glfwCreateWindow(viewport.actualWidth, viewport.actualHeight, viewport.title.c_str(), nullptr, nullptr);
glfwSetWindowPos(viewportData->handle, viewport.xpos, viewport.ypos);
glfwSetWindowSizeLimits(viewportData->handle, (int)viewport.minwidth, (int)viewport.minheight, (int)viewport.maxwidth, (int)viewport.maxheight);
viewport.clientHeight = viewport.actualHeight;
viewport.clientWidth = viewport.actualWidth;
std::vector<GLFWimage> images;
if (!viewport.small_icon.empty())
{
int image_width, image_height;
unsigned char* image_data = stbi_load(viewport.small_icon.c_str(), &image_width, &image_height, nullptr, 4);
if (image_data)
{
images.push_back({ image_width, image_height, image_data });
}
}
if (!viewport.large_icon.empty())
{
int image_width, image_height;
unsigned char* image_data = stbi_load(viewport.large_icon.c_str(), &image_width, &image_height, nullptr, 4);
if (image_data)
{
images.push_back({ image_width, image_height, image_data });
}
}
if (!images.empty())
glfwSetWindowIcon(viewportData->handle, images.size(), images.data());
glfwMakeContextCurrent(viewportData->handle);
gl3wInit();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigWindowsMoveFromTitleBarOnly = true;
if (GContext->IO.loadIniFile)
{
ImGui::LoadIniSettingsFromDisk(GContext->IO.iniFile.c_str());
io.IniFilename = nullptr;
if (GContext->IO.autoSaveIniFile)
io.IniFilename = GContext->IO.iniFile.c_str();
}
else
{
if (GContext->IO.iniFile.empty())
io.IniFilename = nullptr;
else
io.IniFilename = GContext->IO.iniFile.c_str();
}
if (GContext->IO.docking)
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
// Setup style
ImGui::StyleColorsDark();
SetDefaultTheme();
// Setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForOpenGL(viewportData->handle, true);
// Setup callbacks
glfwSetWindowSizeCallback(viewportData->handle, window_size_callback);
glfwSetWindowCloseCallback(viewportData->handle, window_close_callback);
}
void
mvMaximizeViewport(mvViewport& viewport)
{
auto viewportData = (mvViewportData*)viewport.platformSpecifics;
glfwMaximizeWindow(viewportData->handle);
}
void
mvMinimizeViewport(mvViewport& viewport)
{
auto viewportData = (mvViewportData*)viewport.platformSpecifics;
glfwIconifyWindow(viewportData->handle);
}
void
mvRestoreViewport(mvViewport& viewport)
{
auto viewportData = (mvViewportData*)viewport.platformSpecifics;
glfwRestoreWindow(viewportData->handle);
}
void
mvRenderFrame()
{
mvPrerender();
if (GImGui->CurrentWindow == nullptr)
return;
Render();
present(GContext->graphics, GContext->viewport->clearColor, GContext->viewport->vsync);
}
void
mvToggleFullScreen(mvViewport& viewport)
{
static size_t storedWidth = 0;
static size_t storedHeight = 0;
static int storedXPos = 0;
static int storedYPos = 0;
auto viewportData = (mvViewportData*)viewport.platformSpecifics;
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
int framerate = -1;
if (viewport.vsync)
{
framerate = mode->refreshRate;
}
if (viewport.fullScreen)
{
glfwSetWindowMonitor(viewportData->handle, nullptr, storedXPos, storedYPos, storedWidth, storedHeight, framerate);
viewport.fullScreen = false;
}
else
{
storedWidth = (size_t)viewport.actualWidth;
storedHeight = (size_t)viewport.actualHeight;
storedXPos = viewport.xpos;
storedYPos = viewport.ypos;
glfwSetWindowMonitor(viewportData->handle, monitor, 0, 0, mode->width, mode->height, framerate);
viewport.fullScreen = true;
}
}