forked from Kam1k4dze/SubChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui_main.cpp
326 lines (279 loc) · 11.6 KB
/
gui_main.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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#ifndef IMGUI_DEFINE_MATH_OPERATORS
#define IMGUI_DEFINE_MATH_OPERATORS
#endif // IMGUI_DEFINE_MATH_OPERATORS
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include <GL/glew.h>
#include <cstdio>
#define GL_SILENCE_DEPRECATION
#include <GLFW/glfw3.h>
#include "image_view.h"
#include "misc/cpp/imgui_stdlib.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include "ytt_generator.h"
#include "fonts/lucon.hpp"
#include <nfd.h>
constexpr float FONT_LOAD_SIZE = 96;
constexpr float FONT_SIZE = 20;
constexpr float FONT_SCALE_CONSTANT = FONT_SIZE / FONT_LOAD_SIZE;
bool first_time_layout = true;
static ImGuiID dockspace_id = 0;
// Simple helper function to load an image into a OpenGL texture with common settings
bool LoadTextureFromMemory(const void *data, size_t data_size, GLuint *out_texture, int *out_width, int *out_height) {
// Load from file
int image_width = 0;
int image_height = 0;
unsigned char *image_data = stbi_load_from_memory(static_cast<const unsigned char *>(data),
static_cast<int>(data_size), &image_width,
&image_height, nullptr, 4);
if (image_data == nullptr)
return false;
// Create a OpenGL texture identifier
GLuint image_texture;
glGenTextures(1, &image_texture);
glBindTexture(GL_TEXTURE_2D, image_texture);
// Setup filtering parameters for display
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Upload pixels into texture
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image_width, image_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data);
stbi_image_free(image_data);
*out_texture = image_texture;
*out_width = image_width;
*out_height = image_height;
return true;
}
// Open and read a file, then forward to LoadTextureFromMemory()
bool LoadTextureFromFile(const char *file_name, GLuint *out_texture, int *out_width, int *out_height) {
FILE *f = fopen(file_name, "rb");
if (f == nullptr)
return false;
fseek(f, 0, SEEK_END);
auto file_size = static_cast<size_t>(ftell(f));
if (file_size == -1)
return false;
fseek(f, 0, SEEK_SET);
void *file_data = IM_ALLOC(file_size);
fread(file_data, 1, file_size, f);
fclose(f);
bool ret = LoadTextureFromMemory(file_data, file_size, out_texture, out_width, out_height);
IM_FREE(file_data);
return ret;
}
static void glfw_error_callback(int error, const char *description) {
fprintf(stderr, "GLFW Error %d: %s\n", error, description);
}
static float GetDPIScale(GLFWwindow *window) {
float xscale, yscale;
glfwGetWindowContentScale(window, &xscale, &yscale);
return xscale; // Assuming uniform scaling for simplicity
}
void LoadFonts() {
ImGuiIO &io = ImGui::GetIO();
io.Fonts->Clear();
ImFontConfig font_cfg;
font_cfg.FontDataOwnedByAtlas = false;
auto font = io.Fonts->AddFontFromMemoryCompressedTTF(LuCon_compressed_data, LuCon_compressed_size, FONT_LOAD_SIZE,
&font_cfg,
io.Fonts->GetGlyphRangesCyrillic());
font->Scale = FONT_SCALE_CONSTANT;
io.Fonts->Build();
}
void ApplyDPI(float dpi_scale) {
static const ImGuiStyle default_style = ImGui::GetStyle();
ImGui::GetStyle() = default_style;
ImGuiStyle &style = ImGui::GetStyle();
style.ScaleAllSizes(dpi_scale);
}
void ShowDockSpace() {
ImGuiViewport *vp = ImGui::GetMainViewport();
ImGui::SetNextWindowPos(vp->Pos);
ImGui::SetNextWindowSize(vp->Size);
ImGui::SetNextWindowViewport(vp->ID);
ImGuiWindowFlags wflags =
ImGuiWindowFlags_NoTitleBar
| ImGuiWindowFlags_NoCollapse
| ImGuiWindowFlags_NoResize
| ImGuiWindowFlags_NoMove
| ImGuiWindowFlags_NoBringToFrontOnFocus
| ImGuiWindowFlags_NoNavFocus
| ImGuiWindowFlags_NoBackground;
ImGui::Begin("##MainDockSpace", nullptr, wflags);
dockspace_id = ImGui::GetID("MyDockSpace");
ImGui::DockSpace(dockspace_id, ImVec2(0, 0), ImGuiDockNodeFlags_PassthruCentralNode);
if (first_time_layout) {
first_time_layout = false;
ImGui::DockBuilderRemoveNode(dockspace_id);
ImGui::DockBuilderAddNode(dockspace_id, ImGuiDockNodeFlags_None);
ImGui::DockBuilderSetNodeSize(dockspace_id, vp->Size);
ImGuiID left_id, right_id;
ImGui::DockBuilderSplitNode(dockspace_id, ImGuiDir_Left, 0.7f, &left_id, &right_id);
ImGui::DockBuilderDockWindow("Preview", left_id);
ImGui::DockBuilderDockWindow("Settings", right_id);
ImGui::DockBuilderFinish(dockspace_id);
}
ImGui::End();
}
void ShowColorEdit(const char *label, Color &color) {
float col[4] = {color.r / 255.f,
color.g / 255.f,
color.b / 255.f,
color.a / 255.f
};
// ImGui color edit widget
if (ImGui::ColorEdit4(label, col)) {
color.r = col[0] * 255;
color.g = col[1] * 255;
color.b = col[2] * 255;
color.a = col[3] * 255;
}
}
int main(int, char **) {
glfwSetErrorCallback(glfw_error_callback);
if (!glfwInit())
return 1;
const char *glsl_version = "#version 130";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
GLFWwindow *window = glfwCreateWindow(1280, 720, "SubChat Config Generator", nullptr, nullptr);
if (window == nullptr)
return 1;
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
if (NFD_Init() != NFD_OKAY) {
printf("Error: %s\n", NFD_GetError());
return 1;
}
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO &io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init(glsl_version);
float dpi_scale = GetDPIScale(window);
LoadFonts();
ApplyDPI(dpi_scale);
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
int preview_width = 0;
int preview_height = 0;
GLuint preview_texture = 0;
PreloadPreviewFont();
InteractiveTextOverlay text_overlay{};
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
float new_dpi_scale = GetDPIScale(window);
if (fabs(dpi_scale - new_dpi_scale) > 0.1f * std::max(fabs(dpi_scale), fabs(new_dpi_scale))) {
dpi_scale = new_dpi_scale;
ApplyDPI(dpi_scale);
}
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ShowDockSpace();
ShowInteractiveImage(preview_texture, preview_width, preview_height, &text_overlay);
auto &p = text_overlay.params;
ImGui::Begin("Settings");
if (ImGui::Button("Load Image for Preview")) {
nfdu8char_t *outPath = nullptr;
nfdu8filteritem_t filters[1] = {{"Image Files", "png,jpg,jpeg"}};
nfdopendialogu8args_t args = {0};
args.filterList = filters;
args.filterCount = 1;
// Set the parent window handle using the GLFW binding
//NFD_GetNativeWindowFromGLFWWindow(window, &args.parentWindow);
nfdresult_t result = NFD_OpenDialogU8_With(&outPath, &args);
if (result == NFD_OKAY) {
if (preview_texture != 0) {
glDeleteTextures(1, &preview_texture);
preview_texture = 0;
}
bool ret = LoadTextureFromFile(outPath, &preview_texture, &preview_width, &preview_height);
if (!ret)
printf("Failed to load image: %s\n", outPath);
NFD_FreePathU8(outPath);
} else if (result == NFD_CANCEL) {
} else {
printf("Error: %s\n", NFD_GetError());
}
}
if (preview_texture == 0) ImGui::BeginDisabled();
ImGui::SliderInt("X", &p.horizontalMargin, 0, 100);
ImGui::SliderInt("Y", &p.verticalMargin, 0, 100);
ImGui::SliderInt("Font size", &p.fontSizePercent, 0, 300);
ImGui::SliderInt("Vertical\nspacing", &p.verticalSpacing, 0, 25);
// TODO add more options to GUI
// ImGui::Checkbox("Bold", &p.textBold);
// ImGui::SameLine();
// ImGui::Checkbox("Italic", &p.textItalic);
// ImGui::SameLine();
// ImGui::Checkbox("Underline", &p.textUnderline);
ShowColorEdit("Text Color", p.textForegroundColor);
text_overlay.revalidatePreview += ImGui::SliderInt("Characters\nper line", &p.maxCharsPerLine, 5, 50);
text_overlay.revalidatePreview += ImGui::SliderInt("Line\nCount", &p.totalDisplayLines, 1, 50);
text_overlay.revalidatePreview += ImGui::InputText("Username\nSeparator", &p.usernameSeparator);
if (ImGui::Button("Load Config")) {
nfdu8char_t *outPath = nullptr;
nfdu8filteritem_t filters[1] = {{"Chat configs", "ini"}};
nfdopendialogu8args_t args = {0};
args.filterList = filters;
args.filterCount = 1;
// TODO Set the parent window handle using the GLFW binding
//NFD_GetNativeWindowFromGLFWWindow(window, &args.parentWindow);
nfdresult_t result = NFD_OpenDialogU8_With(&outPath, &args);
if (result == NFD_OKAY) {
p.loadFromFile(outPath);
text_overlay.revalidatePreview = true;
NFD_FreePathU8(outPath);
} else if (result == NFD_CANCEL) {
printf("User pressed cancel on load config.\n");
} else {
printf("Error (load config): %s\n", NFD_GetError());
}
}
ImGui::SameLine();
if (!text_overlay.isInsidePicture)
ImGui::BeginDisabled();
if (ImGui::Button("Save Config")) {
nfdu8char_t *outPath = nullptr;
nfdu8filteritem_t filters[1] = {{"Chat configs", "ini"}};
nfdsavedialogu8args_t args = {0};
args.filterList = filters;
args.filterCount = 1;
args.defaultName = "config.ini";
// TODO NFD_GetNativeWindowFromGLFWWindow(window, &args.parentWindow);
nfdresult_t result = NFD_SaveDialogU8_With(&outPath, &args);
if (result == NFD_OKAY) {
p.saveToFile(outPath);
NFD_FreePathU8(outPath);
} else if (result == NFD_CANCEL) {
printf("User pressed cancel on save configs.\n");
} else {
printf("Error (save configs): %s\n", NFD_GetError());
}
}
if (!text_overlay.isInsidePicture) ImGui::EndDisabled();
if (preview_texture == 0) ImGui::EndDisabled();
ImGui::End();
ImGui::Render();
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w,
clear_color.w);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
}
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
NFD_Quit();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}