2023-12-20 14:11:57 -05:00
|
|
|
#include "backend.h"
|
2025-05-03 12:12:36 +02:00
|
|
|
#include "core/backend.h"
|
|
|
|
|
#include "core/style.h"
|
|
|
|
|
#include <GLFW/glfw3.h>
|
2025-05-03 13:16:19 +02:00
|
|
|
#include <mutex>
|
2025-05-03 12:12:36 +02:00
|
|
|
#include <vector>
|
2023-12-20 14:11:57 -05:00
|
|
|
|
2025-05-03 12:12:36 +02:00
|
|
|
extern GLFWwindow *window;
|
2023-12-31 11:11:36 -05:00
|
|
|
extern bool fallback_gl;
|
2023-12-20 14:11:57 -05:00
|
|
|
|
2025-05-03 13:16:19 +02:00
|
|
|
std::mutex glfw_frame_mtx;
|
|
|
|
|
|
2024-01-21 17:30:00 -05:00
|
|
|
float funcDeviceScale()
|
|
|
|
|
{
|
|
|
|
|
float display_scale;
|
|
|
|
|
#if GLFW_VERSION_MAJOR > 3 || (GLFW_VERSION_MAJOR == 3 && GLFW_VERSION_MINOR >= 3)
|
|
|
|
|
glfwGetWindowContentScale(window, &display_scale, nullptr);
|
|
|
|
|
display_scale /= style::macos_framebuffer_scale();
|
|
|
|
|
#else
|
|
|
|
|
display_scale = 1.0f;
|
|
|
|
|
#endif
|
|
|
|
|
return display_scale;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void funcRebuildFonts()
|
|
|
|
|
{
|
|
|
|
|
#ifndef IMGUI_IMPL_OPENGL_ES2
|
|
|
|
|
if (fallback_gl)
|
|
|
|
|
{
|
2026-01-13 20:29:08 +01:00
|
|
|
// ImGui_ImplOpenGL2_DestroyFontsTexture();
|
|
|
|
|
// ImGui_ImplOpenGL2_CreateFontsTexture();
|
2024-01-21 17:30:00 -05:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif
|
|
|
|
|
{
|
2026-01-13 20:29:08 +01:00
|
|
|
// ImGui_ImplOpenGL3_DestroyFontsTexture();
|
|
|
|
|
// ImGui_ImplOpenGL3_CreateFontsTexture();
|
2024-01-21 17:30:00 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-20 14:11:57 -05:00
|
|
|
void funcSetMousePos(int x, int y)
|
|
|
|
|
{
|
2026-01-28 20:29:19 +01:00
|
|
|
glfwSetCursorPos(window, x + backend::mouse_set_offset_x, y + backend::mouse_set_offset_y);
|
2025-01-03 12:05:46 -05:00
|
|
|
ImGui_ImplGlfw_CursorPosCallback(window, x, y);
|
2023-12-20 14:11:57 -05:00
|
|
|
}
|
|
|
|
|
|
2023-12-31 11:11:36 -05:00
|
|
|
std::pair<int, int> funcBeginFrame()
|
|
|
|
|
{
|
2025-05-03 13:16:19 +02:00
|
|
|
glfw_frame_mtx.lock();
|
2023-12-31 11:11:36 -05:00
|
|
|
// Start the Dear ImGui frame
|
|
|
|
|
#ifndef IMGUI_IMPL_OPENGL_ES2
|
|
|
|
|
if (fallback_gl)
|
|
|
|
|
ImGui_ImplOpenGL2_NewFrame();
|
|
|
|
|
else
|
|
|
|
|
#endif
|
|
|
|
|
ImGui_ImplOpenGL3_NewFrame();
|
|
|
|
|
ImGui_ImplGlfw_NewFrame();
|
|
|
|
|
ImGui::NewFrame();
|
|
|
|
|
|
|
|
|
|
int wwidth, wheight;
|
|
|
|
|
glfwGetWindowSize(window, &wwidth, &wheight);
|
2025-05-03 13:16:19 +02:00
|
|
|
glfw_frame_mtx.unlock();
|
2023-12-31 11:11:36 -05:00
|
|
|
return {wwidth, wheight};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void funcEndFrame()
|
|
|
|
|
{
|
2025-05-03 13:16:19 +02:00
|
|
|
glfw_frame_mtx.lock();
|
2023-12-31 11:11:36 -05:00
|
|
|
ImGui::Render();
|
|
|
|
|
int display_w, display_h;
|
|
|
|
|
glfwGetFramebufferSize(window, &display_w, &display_h);
|
|
|
|
|
glViewport(0, 0, display_w, display_h);
|
2024-01-21 14:57:41 -05:00
|
|
|
glClearColor(style::theme.frame_bg.Value.x, style::theme.frame_bg.Value.y, style::theme.frame_bg.Value.z, 1.0f);
|
2023-12-31 11:11:36 -05:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
|
|
|
|
|
#ifndef IMGUI_IMPL_OPENGL_ES2
|
|
|
|
|
if (fallback_gl)
|
|
|
|
|
ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
|
|
|
|
|
else
|
|
|
|
|
#endif
|
|
|
|
|
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
|
|
|
|
|
|
|
|
|
glfwSwapBuffers(window);
|
|
|
|
|
glfwPollEvents();
|
2025-05-03 13:16:19 +02:00
|
|
|
glfw_frame_mtx.unlock();
|
2023-12-31 11:11:36 -05:00
|
|
|
}
|
|
|
|
|
|
2023-12-31 11:46:40 -05:00
|
|
|
void funcSetIcon(uint8_t *image, int w, int h)
|
2023-12-31 11:11:36 -05:00
|
|
|
{
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
GLFWimage img;
|
|
|
|
|
img.pixels = image;
|
2023-12-31 11:46:40 -05:00
|
|
|
img.width = w;
|
|
|
|
|
img.height = h;
|
2023-12-31 11:11:36 -05:00
|
|
|
glfwSetWindowIcon(window, 1, &img);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-03 12:12:36 +02:00
|
|
|
//
|
|
|
|
|
|
2026-01-19 20:35:21 +01:00
|
|
|
#include "imgui/portable-file-dialogs.h"
|
2025-05-03 12:12:36 +02:00
|
|
|
#include "nfd/include/nfd.hpp"
|
|
|
|
|
#include "nfd/include/nfd_glfw3.h"
|
|
|
|
|
|
|
|
|
|
std::string selectFolderDialog(std::string default_path)
|
|
|
|
|
{
|
2026-01-19 20:35:21 +01:00
|
|
|
#ifdef __APPLE__
|
|
|
|
|
auto result = pfd::select_folder("Select Folder", default_path);
|
|
|
|
|
|
|
|
|
|
while (!result.ready(1000))
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
|
|
|
|
|
|
|
|
|
if (result.result().size() > 0)
|
|
|
|
|
return result.result();
|
|
|
|
|
return "";
|
|
|
|
|
#else
|
2025-05-04 19:52:09 +02:00
|
|
|
// Init file dialogs
|
|
|
|
|
NFD::Guard nfdGuard;
|
|
|
|
|
|
2025-05-03 13:16:19 +02:00
|
|
|
glfw_frame_mtx.lock();
|
|
|
|
|
|
2025-05-03 12:12:36 +02:00
|
|
|
NFD::UniquePath outPath;
|
|
|
|
|
|
|
|
|
|
// show the dialog
|
|
|
|
|
nfdwindowhandle_t h;
|
|
|
|
|
NFD_GetNativeWindowFromGLFWWindow(window, &h);
|
2025-05-03 13:16:19 +02:00
|
|
|
|
|
|
|
|
glfw_frame_mtx.unlock();
|
|
|
|
|
|
2025-05-03 16:20:28 +02:00
|
|
|
nfdresult_t result = NFD::PickFolder(outPath, default_path == "" ? nullptr : default_path.c_str(), h);
|
|
|
|
|
|
2025-05-03 12:12:36 +02:00
|
|
|
if (result == NFD_OKAY)
|
|
|
|
|
return outPath.get();
|
|
|
|
|
else if (result == NFD_CANCEL)
|
|
|
|
|
return ""; //"User pressed cancel.";
|
|
|
|
|
else
|
|
|
|
|
return ""; // "Error: " + std::string(NFD::GetError());
|
2026-01-19 20:35:21 +01:00
|
|
|
#endif
|
2025-05-03 12:12:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string selectFileDialog(std::vector<std::pair<std::string, std::string>> filters, std::string default_path)
|
|
|
|
|
{
|
2026-01-19 20:35:21 +01:00
|
|
|
#ifdef __APPLE__
|
|
|
|
|
std::vector<std::string> f;
|
|
|
|
|
for (auto &ff : filters)
|
|
|
|
|
{
|
|
|
|
|
f.push_back(ff.first);
|
|
|
|
|
f.push_back(ff.second);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto result = pfd::open_file("Select File", default_path, f);
|
|
|
|
|
|
|
|
|
|
while (!result.ready(1000))
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
|
|
|
|
|
|
|
|
|
if (result.result().size() > 0 && result.result()[0].size() > 0)
|
|
|
|
|
return result.result()[0];
|
|
|
|
|
return "";
|
|
|
|
|
#else
|
2025-05-04 19:52:09 +02:00
|
|
|
// Init file dialogs
|
|
|
|
|
NFD::Guard nfdGuard;
|
|
|
|
|
|
2025-05-03 13:16:19 +02:00
|
|
|
glfw_frame_mtx.lock();
|
|
|
|
|
|
2025-05-03 12:12:36 +02:00
|
|
|
NFD::UniquePath outPath;
|
|
|
|
|
// show the dialog
|
|
|
|
|
nfdwindowhandle_t h;
|
|
|
|
|
NFD_GetNativeWindowFromGLFWWindow(window, &h);
|
|
|
|
|
|
2025-05-03 16:20:28 +02:00
|
|
|
glfw_frame_mtx.unlock();
|
|
|
|
|
|
2025-05-03 12:12:36 +02:00
|
|
|
std::vector<nfdfilteritem_t> filt;
|
|
|
|
|
for (auto &f : filters)
|
|
|
|
|
filt.push_back({f.first.c_str(), f.second.c_str()});
|
|
|
|
|
|
|
|
|
|
nfdresult_t result = NFD::OpenDialog(outPath, filt.data(), filt.size(), default_path == "" ? nullptr : default_path.c_str(), h);
|
2025-05-03 13:16:19 +02:00
|
|
|
|
2025-05-03 12:12:36 +02:00
|
|
|
if (result == NFD_OKAY)
|
|
|
|
|
return outPath.get();
|
|
|
|
|
else if (result == NFD_CANCEL)
|
|
|
|
|
return ""; //"User pressed cancel.";
|
|
|
|
|
else
|
|
|
|
|
return ""; // "Error: " + std::string(NFD::GetError());
|
2026-01-19 20:35:21 +01:00
|
|
|
#endif
|
2025-05-03 12:12:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string saveFileDialog(std::vector<std::pair<std::string, std::string>> filters, std::string default_path, std::string default_name)
|
|
|
|
|
{
|
2026-01-19 20:35:21 +01:00
|
|
|
#ifdef __APPLE__
|
|
|
|
|
std::vector<std::string> f;
|
|
|
|
|
for (auto &ff : filters)
|
|
|
|
|
{
|
|
|
|
|
f.push_back(ff.first);
|
|
|
|
|
f.push_back(ff.second);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto result = pfd::save_file("Save File", default_path, f);
|
|
|
|
|
|
|
|
|
|
while (!result.ready(1000))
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
|
|
|
|
|
|
|
|
|
if (result.result().size() > 0)
|
|
|
|
|
return result.result();
|
|
|
|
|
return "";
|
|
|
|
|
#else
|
2025-05-04 19:52:09 +02:00
|
|
|
// Init file dialogs
|
|
|
|
|
NFD::Guard nfdGuard;
|
|
|
|
|
|
2025-05-03 13:16:19 +02:00
|
|
|
glfw_frame_mtx.lock();
|
|
|
|
|
|
2025-05-03 12:12:36 +02:00
|
|
|
NFD::UniquePath outPath;
|
|
|
|
|
// show the dialog
|
|
|
|
|
nfdwindowhandle_t h;
|
|
|
|
|
NFD_GetNativeWindowFromGLFWWindow(window, &h);
|
|
|
|
|
|
2025-05-03 16:20:28 +02:00
|
|
|
glfw_frame_mtx.unlock();
|
|
|
|
|
|
2025-05-03 12:12:36 +02:00
|
|
|
std::vector<nfdfilteritem_t> filt;
|
|
|
|
|
for (auto &f : filters)
|
|
|
|
|
filt.push_back({f.first.c_str(), f.second.c_str()});
|
|
|
|
|
|
|
|
|
|
nfdresult_t result = NFD::SaveDialog(outPath, filt.data(), filt.size(), default_path.c_str(), default_name.c_str(), h);
|
2025-05-03 13:16:19 +02:00
|
|
|
|
2025-05-03 12:12:36 +02:00
|
|
|
if (result == NFD_OKAY)
|
|
|
|
|
return outPath.get();
|
|
|
|
|
else if (result == NFD_CANCEL)
|
|
|
|
|
return ""; //"User pressed cancel.";
|
|
|
|
|
else
|
|
|
|
|
return ""; // "Error: " + std::string(NFD::GetError());
|
2026-01-19 20:35:21 +01:00
|
|
|
#endif
|
2025-05-03 12:12:36 +02:00
|
|
|
}
|
|
|
|
|
|
2023-12-20 14:11:57 -05:00
|
|
|
void bindBackendFunctions()
|
|
|
|
|
{
|
2024-01-21 17:30:00 -05:00
|
|
|
backend::device_scale = funcDeviceScale();
|
|
|
|
|
|
|
|
|
|
backend::rebuildFonts = funcRebuildFonts;
|
2023-12-31 11:46:40 -05:00
|
|
|
backend::setMousePos = funcSetMousePos;
|
|
|
|
|
backend::beginFrame = funcBeginFrame;
|
|
|
|
|
backend::endFrame = funcEndFrame;
|
2023-12-31 11:11:36 -05:00
|
|
|
backend::setIcon = funcSetIcon;
|
2025-05-03 12:12:36 +02:00
|
|
|
|
|
|
|
|
backend::selectFolderDialog = selectFolderDialog;
|
|
|
|
|
backend::selectFileDialog = selectFileDialog;
|
|
|
|
|
backend::saveFileDialog = saveFileDialog;
|
2023-12-31 11:46:40 -05:00
|
|
|
}
|