infinipaint/src/MainProgram.cpp

470 lines
15 KiB
C++
Raw Normal View History

2025-07-06 20:43:53 +03:00
#include "MainProgram.hpp"
2026-04-05 20:26:03 +03:00
#include "CustomEvents.hpp"
2026-04-17 19:32:49 +03:00
#include "Screens/FileSelectScreen.hpp"
#include "VersionConstants.hpp"
2025-11-20 12:38:28 +03:00
#include <SDL3/SDL_render.h>
#include <SDL3/SDL_video.h>
2025-07-06 20:43:53 +03:00
#include <include/core/SkAlphaType.h>
#include <include/core/SkColor.h>
#include <include/core/SkPaint.h>
#include <filesystem>
#include <iostream>
#include <include/core/SkFont.h>
#include <include/core/SkData.h>
#include <include/core/SkSurfaceProps.h>
#include "InputManager.hpp"
#include <cereal/types/vector.hpp>
#include <cereal/types/unordered_map.hpp>
#include <Eigen/Core>
#include <fstream>
#include <cereal/archives/json.hpp>
#include <Helpers/Serializers.hpp>
#include <nlohmann/json.hpp>
2026-04-12 12:36:00 +03:00
#include "Screens/DrawingProgramScreen.hpp"
2026-04-17 19:32:49 +03:00
#include "Screens/FileSelectScreen.hpp"
2025-07-06 20:43:53 +03:00
#include <include/core/SkSurface.h>
#ifdef USE_SKIA_BACKEND_GRAPHITE
#include <include/gpu/graphite/Surface.h>
#elif USE_SKIA_BACKEND_GANESH
#include <include/gpu/ganesh/GrDirectContext.h>
#include <include/gpu/ganesh/SkSurfaceGanesh.h>
#endif
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
#include <Helpers/Logger.hpp>
MainProgram::MainProgram():
2026-03-01 17:28:43 +03:00
input(*this),
2025-11-16 10:42:01 +03:00
fonts(std::make_shared<FontData>()),
2026-03-31 13:33:15 +03:00
g(*this)
2025-07-06 20:43:53 +03:00
{
screen = std::make_unique<FileSelectScreen>(*this);
2025-07-06 20:43:53 +03:00
Logger::get().add_log("WORLDFATAL", [&](const std::string& text) {
*logFile << "[WORLDFATAL] " << text << std::endl;
std::cout << "[WORLDFATAL] " << text << std::endl;
2026-04-12 12:36:00 +03:00
logMessages.emplace_front(UserLogMessage{text, UserLogMessage::COLOR_ERROR});
2025-07-06 20:43:53 +03:00
if(logMessages.size() == LOG_SIZE)
logMessages.pop_back();
2026-04-05 22:01:30 +03:00
g.gui.set_to_layout();
2025-07-06 20:43:53 +03:00
});
Logger::get().add_log("USERINFO", [&](const std::string& text) {
*logFile << "[USERINFO] " << text << std::endl;
std::cout << "[USERINFO] " << text << std::endl;
2026-04-12 12:36:00 +03:00
logMessages.emplace_front(UserLogMessage{text, UserLogMessage::COLOR_NORMAL});
2025-07-06 20:43:53 +03:00
if(logMessages.size() == LOG_SIZE)
logMessages.pop_back();
2026-04-05 22:01:30 +03:00
g.gui.set_to_layout();
2025-07-06 20:43:53 +03:00
});
Logger::get().add_log("CHAT", [&](const std::string& text) {
*logFile << "[CHAT] " << text << std::endl;
std::cout << "[CHAT] " << text << std::endl;
});
}
2025-07-06 20:43:53 +03:00
void MainProgram::update() {
deltaTime.update_time_since();
deltaTime.update_time_point();
2026-04-09 10:20:40 +03:00
input.update();
g.update();
2026-04-12 12:36:00 +03:00
screen->update();
for(auto& w : worlds)
w->update();
NetLibrary::update();
2026-04-05 22:01:30 +03:00
post_callback();
2025-07-06 20:43:53 +03:00
}
2026-01-18 13:39:49 +03:00
bool MainProgram::app_close_requested() {
2026-04-12 12:36:00 +03:00
return screen->app_close_requested();
2026-01-18 13:39:49 +03:00
}
2025-07-08 12:18:55 +03:00
void MainProgram::update_display_names() {
for(auto& w : worlds) {
2026-01-01 22:46:02 +03:00
if(!w->netObjMan.is_connected())
w->ownClientData->set_display_name(conf.displayName);
2025-07-08 12:18:55 +03:00
}
}
void MainProgram::init_net_library() {
2026-04-12 12:36:00 +03:00
NetLibrary::init(conf.configPath / "p2p.json");
}
2025-07-06 20:43:53 +03:00
void MainProgram::save_config() {
2026-04-12 12:36:00 +03:00
std::ofstream f(conf.configPath / "config.json");
2025-07-06 20:43:53 +03:00
if(f.is_open()) {
using json = nlohmann::json;
json j;
j["version"] = VersionConstants::CURRENT_VERSION_STRING;
j["settings"] = conf.get_config_json(input);
2025-07-06 20:43:53 +03:00
j["window"]["pos"] = window.writtenPos;
j["window"]["size"] = window.writtenSize;
j["window"]["maximized"] = window.maximized;
j["window"]["fullscreen"] = window.fullscreen;
2026-04-12 12:36:00 +03:00
j["fileselectorpath"] = conf.currentSearchPath;
j["toolConfig"] = toolConfig;
2025-07-06 20:43:53 +03:00
f << std::setw(4) << j;
f.close();
}
2026-04-12 12:36:00 +03:00
conf.save_palettes();
2025-07-06 20:43:53 +03:00
}
void MainProgram::load_config() {
2026-04-12 12:36:00 +03:00
std::ifstream f(conf.configPath / "config.json");
conf.currentSearchPath = homePath;
2025-07-06 20:43:53 +03:00
if(f.is_open()) {
using json = nlohmann::json;
2025-07-06 20:43:53 +03:00
try {
json j;
f >> j;
VersionNumber version(0, 0, 1);
try {
std::string versionStr;
j.at("version").get_to(versionStr);
auto optVersion = version_str_to_version_numbers(versionStr);
if(optVersion.has_value())
version = optVersion.value();
}
catch(...) {}
2025-07-06 20:43:53 +03:00
try {
conf.set_config_json(input, j["settings"], version);
2025-07-06 20:43:53 +03:00
}
catch(...) {}
#ifndef __EMSCRIPTEN__
try {
j.at("window").at("size").get_to(window.size);
window.writtenSize = window.size;
j.at("window").at("pos").get_to(window.pos);
window.writtenPos = window.pos;
j.at("window").at("maximized").get_to(window.maximized);
j.at("window").at("fullscreen").get_to(window.fullscreen);
}
catch(...) {}
try {
2026-04-12 12:36:00 +03:00
j.at("fileselectorpath").get_to(conf.currentSearchPath);
2025-07-06 20:43:53 +03:00
}
catch(...) {
2026-04-12 12:36:00 +03:00
conf.currentSearchPath = homePath;
2025-07-06 20:43:53 +03:00
}
#endif
try {
j.at("toolConfig").get_to(toolConfig);
}
catch(...) {}
2025-07-06 20:43:53 +03:00
f.close();
} catch(...) {}
}
#ifdef __EMSCRIPTEN__
else
2026-04-12 17:31:32 +03:00
conf.viewWebVersionWelcome = true;
2025-07-06 20:43:53 +03:00
#endif
2026-04-12 12:36:00 +03:00
conf.load_palettes();
g.load_theme(conf.configPath, conf.themeCurrentlyLoaded);
conf.load_licenses();
2026-04-12 12:36:00 +03:00
NetLibrary::copy_default_p2p_config_to_path(conf.configPath / "p2p.json");
update_display_names();
set_vsync_value(conf.vsyncValue);
refresh_draw_surfaces();
2025-07-06 20:43:53 +03:00
}
2025-11-20 12:38:28 +03:00
void MainProgram::set_vsync_value(int vsyncValue) {
if(!SDL_GL_SetSwapInterval(vsyncValue)) {
Logger::get().log("INFO", "Vsync value " + std::to_string(vsyncValue) + " not available. Setting to 1");
if(vsyncValue == -1) {
Logger::get().log("USERINFO", "Adaptive VSync not available. Setting Vsync to On");
vsyncValue = 1;
}
2025-11-20 12:38:28 +03:00
SDL_GL_SetSwapInterval(1);
}
conf.vsyncValue = vsyncValue;
2025-11-20 12:38:28 +03:00
}
void MainProgram::update_scale_and_density() {
#ifdef __EMSCRIPTEN__
window.scale = 1.0f;
#else
window.scale = conf.applyDisplayScale ? SDL_GetWindowDisplayScale(window.sdlWindow) : 1.0f;
#endif
window.density = SDL_GetWindowPixelDensity(window.sdlWindow);
}
float MainProgram::get_scale_and_density_factor_gui() {
return window.scale * window.density;
}
void MainProgram::refresh_draw_surfaces() {
if(window.canCreateSurfaces) {
window.defaultMSAASampleCount = 0;
window.defaultMSAASurfaceProps = SkSurfaceProps(conf.antialiasing == GlobalConfig::AntiAliasing::DYNAMIC_MSAA ? SkSurfaceProps::kDynamicMSAA_Flag : SkSurfaceProps::kDefault_Flag, kUnknown_SkPixelGeometry);
if(conf.antialiasing == GlobalConfig::AntiAliasing::DYNAMIC_MSAA)
window.intermediateSurfaceMSAA = create_native_surface(window.size, true);
else
window.intermediateSurfaceMSAA = nullptr;
2026-04-08 17:34:50 +03:00
g.delete_cache_surface();
DrawingProgramCache::delete_all_draw_cache();
}
}
2026-04-17 19:32:49 +03:00
void MainProgram::draw_world(SkCanvas* canvas, std::shared_ptr<World> worldToDraw, const DrawData& drawData) {
canvas->clear(drawData.transparentBackground ? SkColor4f{0.0f, 0.0f, 0.0f, 0.0f} : worldToDraw->canvasTheme.get_back_color());
DrawData drawDataCopy = drawData;
drawDataCopy.skiaAA = conf.antialiasing == GlobalConfig::AntiAliasing::SKIA;
worldToDraw->draw(canvas, drawDataCopy);
2026-04-17 19:32:49 +03:00
}
void MainProgram::draw(SkCanvas* canvas) {
screen->draw(canvas);
g.draw(canvas, conf.antialiasing == GlobalConfig::AntiAliasing::SKIA);
2025-07-06 20:43:53 +03:00
}
sk_sp<SkSurface> MainProgram::create_native_surface(Vector2i resolution, bool isMSAA) {
if(window.canCreateSurfaces) {
SkImageInfo imgInfo = SkImageInfo::MakeN32Premul(resolution.x(), resolution.y());
SkSurfaceProps defaultProps;
#ifdef USE_SKIA_BACKEND_GRAPHITE
auto surfaceToRet = SkSurfaces::RenderTarget(window.recorder(), imgInfo, skgpu::Mipmapped::kNo, isMSAA ? &window.defaultMSAASurfaceProps : &defaultProps);
#elif USE_SKIA_BACKEND_GANESH
auto surfaceToRet = SkSurfaces::RenderTarget(window.ctx.get(), skgpu::Budgeted::kNo, imgInfo, isMSAA ? window.defaultMSAASampleCount : 0, isMSAA ? &window.defaultMSAASurfaceProps : &defaultProps);
#endif
if(!surfaceToRet)
throw std::runtime_error("[MainProgram::create_native_surface] Could not make native surface");
return surfaceToRet;
}
return nullptr;
}
void MainProgram::create_new_tab(const CustomEvents::OpenInfiniPaintFileEvent& openFile) {
2026-04-05 20:26:03 +03:00
std::shared_ptr<World> newWorld;
try {
newWorld = std::make_shared<World>(*this, openFile);
}
catch(const std::runtime_error& e) {
Logger::get().log("WORLDFATAL", "Failed to open canvas: " + (openFile.filePathSource.has_value() ? openFile.filePathSource.value().string() : "NO PATH") + " with error: " + e.what());
return;
}
worlds.emplace_back(newWorld);
switch_to_tab(worlds.size() - 1);
g.gui.set_to_layout();
}
void MainProgram::set_tab_to_close(World* world) {
tabsToClose.emplace(world);
}
void MainProgram::switch_to_tab(size_t wIndex) {
world = worlds[wIndex];
worldIndex = wIndex;
}
void MainProgram::post_callback() {
close_set_to_close_tabs();
g.post_callback();
}
void MainProgram::input_add_file_to_canvas_callback(const CustomEvents::AddFileToCanvasEvent& addFile) {
2026-04-12 12:36:00 +03:00
screen->input_add_file_to_canvas_callback(addFile);
post_callback();
}
void MainProgram::input_open_infinipaint_file_callback(const CustomEvents::OpenInfiniPaintFileEvent& openFile) {
2026-04-12 12:36:00 +03:00
screen->input_open_infinipaint_file_callback(openFile);
2026-04-05 20:26:03 +03:00
post_callback();
}
void MainProgram::input_paste_callback(const CustomEvents::PasteEvent& paste) {
g.input_paste_callback(paste);
2026-04-12 12:36:00 +03:00
screen->input_paste_callback(paste);
2026-04-05 20:26:03 +03:00
post_callback();
}
2026-04-02 19:18:39 +03:00
bool MainProgram::input_keybind_callback(const Vector2ui32& newKey) {
if(keybindWaiting.has_value()) {
unsigned v = keybindWaiting.value();
input.keyAssignments.erase(newKey);
auto f = std::find_if(input.keyAssignments.begin(), input.keyAssignments.end(), [&](auto& p) {
return p.second == v;
});
if(f != input.keyAssignments.end())
input.keyAssignments.erase(f);
input.keyAssignments.emplace(newKey, v);
keybindWaiting = std::nullopt;
g.gui.set_to_layout();
2026-04-05 22:01:30 +03:00
post_callback();
2026-04-02 19:18:39 +03:00
return true;
}
return false;
}
void MainProgram::input_drop_file_callback(const InputManager::DropCallbackArgs& drop) {
2026-04-12 12:36:00 +03:00
screen->input_drop_file_callback(drop);
post_callback();
}
void MainProgram::input_drop_text_callback(const InputManager::DropCallbackArgs& drop) {
2026-04-12 12:36:00 +03:00
screen->input_drop_text_callback(drop);
post_callback();
}
void MainProgram::input_key_callback(const InputManager::KeyCallbackArgs& key) {
2026-04-12 12:36:00 +03:00
if(key.key == InputManager::KEY_FULLSCREEN) {
if(key.down && !key.repeat)
toggle_full_screen();
}
g.input_key_callback(key);
2026-04-12 12:36:00 +03:00
screen->input_key_callback(key);
2026-04-05 20:26:03 +03:00
post_callback();
}
void MainProgram::input_text_key_callback(const InputManager::KeyCallbackArgs& key) {
g.input_text_key_callback(key);
2026-04-12 12:36:00 +03:00
screen->input_text_key_callback(key);
2026-04-05 20:26:03 +03:00
post_callback();
2026-04-02 10:15:22 +03:00
}
void MainProgram::input_text_callback(const InputManager::TextCallbackArgs& text) {
g.input_text_callback(text);
2026-04-12 12:36:00 +03:00
screen->input_text_callback(text);
2026-04-05 20:26:03 +03:00
post_callback();
2026-04-02 10:15:22 +03:00
}
void MainProgram::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) {
g.input_mouse_button_callback(button);
2026-04-12 12:36:00 +03:00
screen->input_mouse_button_callback(button);
2026-04-05 20:26:03 +03:00
post_callback();
}
void MainProgram::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) {
g.input_mouse_motion_callback(motion);
2026-04-12 12:36:00 +03:00
screen->input_mouse_motion_callback(motion);
2026-04-05 20:26:03 +03:00
post_callback();
}
2026-03-02 23:09:48 +03:00
void MainProgram::input_mouse_wheel_callback(const InputManager::MouseWheelCallbackArgs& wheel) {
g.input_mouse_wheel_callback(wheel);
2026-04-12 12:36:00 +03:00
screen->input_mouse_wheel_callback(wheel);
2026-04-05 20:26:03 +03:00
post_callback();
2026-03-02 23:09:48 +03:00
}
2026-03-03 14:59:18 +03:00
void MainProgram::input_pen_button_callback(const InputManager::PenButtonCallbackArgs& button) {
2026-04-12 12:36:00 +03:00
screen->input_pen_button_callback(button);
post_callback();
2026-03-03 14:59:18 +03:00
}
void MainProgram::input_pen_touch_callback(const InputManager::PenTouchCallbackArgs& touch) {
2026-04-12 12:36:00 +03:00
screen->input_pen_touch_callback(touch);
post_callback();
2026-03-03 14:59:18 +03:00
}
void MainProgram::input_pen_motion_callback(const InputManager::PenMotionCallbackArgs& motion) {
2026-04-12 12:36:00 +03:00
screen->input_pen_motion_callback(motion);
post_callback();
2026-03-03 16:01:21 +03:00
}
void MainProgram::input_pen_axis_callback(const InputManager::PenAxisCallbackArgs& axis) {
2026-04-12 12:36:00 +03:00
screen->input_pen_axis_callback(axis);
post_callback();
2026-03-03 14:59:18 +03:00
}
void MainProgram::input_multi_finger_touch_callback(const InputManager::MultiFingerTouchCallbackArgs& touch) {
2026-04-12 12:36:00 +03:00
screen->input_multi_finger_touch_callback(touch);
post_callback();
}
void MainProgram::input_multi_finger_motion_callback(const InputManager::MultiFingerMotionCallbackArgs& motion) {
2026-04-12 12:36:00 +03:00
screen->input_multi_finger_motion_callback(motion);
post_callback();
}
void MainProgram::input_finger_touch_callback(const InputManager::FingerTouchCallbackArgs& touch) {
2026-04-12 12:36:00 +03:00
screen->input_finger_touch_callback(touch);
post_callback();
}
void MainProgram::input_finger_motion_callback(const InputManager::FingerMotionCallbackArgs& motion) {
2026-04-12 12:36:00 +03:00
screen->input_finger_motion_callback(motion);
post_callback();
}
void MainProgram::input_window_resize_callback(const InputManager::WindowResizeCallbackArgs& w) {
for(auto& wor : worlds)
wor->drawData.cam.set_viewing_area(window.size.cast<float>());
g.window_update();
2026-04-12 12:36:00 +03:00
screen->input_window_resize_callback(w);
post_callback();
}
void MainProgram::input_window_scale_callback(const InputManager::WindowScaleCallbackArgs& w) {
g.window_update();
2026-04-12 12:36:00 +03:00
screen->input_window_scale_callback(w);
post_callback();
}
2026-04-07 20:33:48 +03:00
void MainProgram::toggle_full_screen() {
window.fullscreen = !window.fullscreen;
SDL_SetWindowFullscreen(window.sdlWindow, window.fullscreen);
}
2026-04-05 20:26:03 +03:00
void MainProgram::close_set_to_close_tabs() {
if(!tabsToClose.empty()) {
std::erase_if(worlds, [&](auto& w) {
if(tabsToClose.contains(w.get())) {
if(w == world)
world = nullptr;
return true;
}
return false;
});
if(worlds.empty())
create_new_tab({
.isClient = false
});
else if(world)
worldIndex = std::find(worlds.begin(), worlds.end(), world) - worlds.begin();
else
switch_to_tab(0);
tabsToClose.clear();
g.gui.set_to_layout();
}
}
2025-07-06 20:43:53 +03:00
bool MainProgram::network_being_used() {
for(auto& w : worlds) {
2026-01-01 22:46:02 +03:00
if(w->netObjMan.is_connected())
2025-07-06 20:43:53 +03:00
return true;
}
return false;
}
bool MainProgram::net_server_hosted() {
for(auto& w : worlds) {
2026-01-01 22:46:02 +03:00
if(w->netServer)
2025-07-06 20:43:53 +03:00
return true;
}
return false;
}
void MainProgram::early_destroy() {
for(auto& w : worlds)
w->early_destroy();
}
MainProgram::~MainProgram() {
NetLibrary::destroy();
}