diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 946bad9b..6732f870 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -29,10 +29,10 @@ - + @@ -44,5 +44,6 @@ + \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0388c455..4e83f654 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,8 +1,6 @@ target_sources(BrickSimLib PRIVATE binary_file.cpp binary_file.h - config.cpp - config.h controller.cpp controller.h custom_imconfig.h diff --git a/src/config.cpp b/src/config.cpp deleted file mode 100644 index 597a90ca..00000000 --- a/src/config.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include "config.h" -#include "db.h" -#include - -namespace bricksim::config { - - template<> - [[nodiscard]] std::string get(const Key& key) { - return stringValues.get(key.name, key.defaultValue); - } - - template<> - [[nodiscard]] int get(const Key& key) { - return intValues.get(key.name, key.defaultValue); - } - - template<> - [[nodiscard]] double get(const Key& key) { - return doubleValues.get(key.name, key.defaultValue); - } - - template<> - [[nodiscard]] color::RGB get(const Key& key) { - return color::RGB(stringValues.get(key.name, key.defaultValue.asHtmlCode())); - } - - template<> - [[nodiscard]] float get(const Key& key) { - return static_cast(doubleValues.get(key.name, key.defaultValue)); - } - template<> - [[nodiscard]] bool get(const Key& key) { - return intValues.get(key.name, key.defaultValue); - } - - template<> - void set(const Key& key, const color::RGB& value) { - stringValues.set(key.name, value.asHtmlCode()); - } - - void resetAllToDefault() { - stringValues.clear(); - intValues.clear(); - doubleValues.clear(); - db::config::deleteAll(); - } -} diff --git a/src/config.h b/src/config.h deleted file mode 100644 index c2784173..00000000 --- a/src/config.h +++ /dev/null @@ -1,195 +0,0 @@ -#pragma once - -#include "db.h" -#include "helpers/color.h" -#include "helpers/util.h" -#include "snapping/snap_const.h" -#include -#include -#include -#include -#include - -namespace bricksim::config { - namespace { - template - class ValuesCache { - //using const char* as key is ok here because there's only one instance of each key - uomap_t values; - - std::function writeFunction; - std::function(const char*)> readFunction; - - std::mutex mutex; - - public: - T get(const char* key, T defaultValue); - - void set(const char* key, T value); - - ValuesCache(std::function writeFunction, - std::function(const char*)> readFunction); - - void clear(); - }; - - ValuesCache stringValues(db::config::setString, db::config::getString); - ValuesCache intValues(db::config::setInt, db::config::getInt); - ValuesCache doubleValues(db::config::setDouble, db::config::getDouble); - - template - T ValuesCache::get(const char* key, T defaultValue) { - const auto it = values.find(key); - if (it == values.end()) { - std::scoped_lock lg(mutex); - std::optional opt = readFunction(key); - if (opt.has_value()) { - return values[key] = opt.value(); - } else { - writeFunction(key, defaultValue); - return values[key] = defaultValue; - } - } - return it->second; - } - - template - void ValuesCache::set(const char* key, T value) { - std::scoped_lock lg(mutex); - writeFunction(key, value); - values[key] = value; - } - - template - ValuesCache::ValuesCache( - const std::function writeFunction, - const std::function(const char*)> readFunction) : - writeFunction(std::move(writeFunction)), - readFunction(std::move(readFunction)), - mutex() {} - - template - void ValuesCache::clear() { - values.clear(); - } - } - - template - requires( - std::is_same_v - || std::is_convertible_v || std::is_convertible_v || std::is_convertible_v) - class Key { - public: - const char* const name; - const T defaultValue; - - Key(const char* const name, const T defaultValue) : - name(name), defaultValue(defaultValue) {} - - bool operator==(const Key& rhs) const { - return std::strcmp(name, rhs.name) == 0; - } - }; - - template - [[nodiscard]] T get(const Key& key) = delete; - - template<> - [[nodiscard]] std::string get(const Key& key); - - template<> - [[nodiscard]] int get(const Key& key); - - template<> - [[nodiscard]] double get(const Key& key); - - template<> - [[nodiscard]] color::RGB get(const Key& key); - - template<> - [[nodiscard]] float get(const Key& key); - - template<> - [[nodiscard]] bool get(const Key& key); - - template - requires std::is_convertible_v - void set(const Key& key, const V& value); - - template - void set(const Key& key, const V& value) { - stringValues.set(key.name, value); - } - - template - void set(const Key& key, const V& value) { - intValues.set(key.name, value); - } - - template - void set(const Key& key, const V& value) { - doubleValues.set(key.name, value); - } - - template - void set(const Key& key, const V& value) { - doubleValues.set(key.name, value); - } - - template - void set(const Key& key, const V& value) { - intValues.set(key.name, value); - } - - template<> - void set(const Key& key, const color::RGB& value); - - void resetAllToDefault(); - - /*ldraw.libraryLocation*/const Key LDRAW_PARTS_LIBRARY("ldrawPartsLibrary", "~/ldraw"); - /*ldraw.shadowLibraryLocation*/const Key SHADOW_LIBRARY_PATH("shadowLibraryPath", "~/LDCadShadowLibrary"); - /*PS.windowWidth*/const Key SCREEN_WIDTH("screenWidth", 1280); - /*PS.windowHeight*/const Key SCREEN_HEIGHT("screenHeight", 720); - /**/const Key INSTANCED_MIN_COMPLEXITY("instancedMinComplexity", 6000); - /*graphics.msaaSamples*/const Key MSAA_SAMPLES("msaaSamples", 16); - /*gui.scale*/const Key GUI_SCALE("guiScale", 1.5); - /*gui.style*/const Key GUI_STYLE("guiStyle", "BrickSim");//or ImGuiLight, ImGuiClassic or ImGuiDark - /*graphics.background*/const Key BACKGROUND_COLOR("backgroundColor", color::RGB(0x36, 0x36, 0x36)); - /*graphics.showNormals*/const Key SHOW_NORMALS("showNormals", false); - /*graphics.displaySelectionBuffer*/const Key DISPLAY_SELECTION_BUFFER("displaySelectionBuffer", false); - /*graphics.jpgScreenshotQuality*/const Key JPG_SCREENSHOT_QUALITY("jpgScreenshotQuality", 90); - /**/const Key COLOR_MULTI_PART_DOCUMENT("colorMultiPartDocument", color::RGB(0x0, 0xff, 0xff)); - /**/const Key COLOR_MPD_SUBFILE("colorMpdSubfile", color::RGB(0x0, 0xff, 0x0)); - /*elementTree.nodeColors.modelInstance*/const Key COLOR_MPD_SUBFILE_INSTANCE("colorMpdSubfileInstance", color::RGB(0xff, 0xff, 0x0)); - /*elementTree.nodeColors.part*/const Key COLOR_OFFICAL_PART("colorOfficalPart", color::RGB(0xff, 0xff, 0xff)); - /**/const Key COLOR_UNOFFICAL_PART("colorUnofficalPart", color::RGB(0xff, 0xff, 0xff)); - /*partPalette.thumbnailSize*/const Key THUMBNAIL_SIZE("thumbnailSize", 256); - /**/const Key THUMBNAIL_CACHE_SIZE_BYTES("thumbnailCacheSizeBytes", 1073741824); - /*graphics.drawMinimalEnclosingBallLines*/const Key DRAW_MINIMAL_ENCLOSING_BALL_LINES("drawMinimalEnclosingBallLines", false); - /*bricklinkIntegration.currencyCode*/const Key BRICKLINK_CURRENCY_CODE("bricklinkCurrencyCode", "CHF"); - /*gui.enableImGuiViewports*/const Key ENABLE_VIEWPORTS("enableViewports", false); - /*gui.font*/const Key FONT("font", "Roboto"); - /*log.notImportantLogMessageKeepCount*/const Key NOT_IMPORTANT_LOG_MESSAGE_KEEP_COUNT("notImportantLogMessageKeepCount", 10); - /*system.enableGlDebugOutput*/const Key ENABLE_GL_DEBUG_OUTPUT("enableGlDebugOutput", false); - /*view3d.sensitivity.rotate*/const Key MOUSE_3DVIEW_ROTATE_SENSITIVITY("mouse3dViewRotateSensitivity", 1); - /*view3d.sensitivity.pan*/const Key MOUSE_3DVIEW_PAN_SENSITIVITY("mouse3dViewPanSensitivity", 1); - /*view3d.sensitivity.zoom*/const Key MOUSE_3DVIEW_ZOOM_SENSITIVITY("mouse3dViewZoomSensitivity", 1); - /*graphics.vsync*/const Key ENABLE_VSYNC("enableVsync", true); - /**/const Key TRANSFORM_GIZMO_SIZE("transformGizmoSize", 1.0); - /*graphics.faceCulling*/const Key FACE_CULLING_ENABLED("faceCullingEnabled", true); - /*system.enableThreading*/const Key THREADING_ENABLED("threadingEnabled", true); - /*elementProperties.angleMode*/const Key USE_EULER_ANGLES("useEulerAngles", true); - /*graphics.deleteVertexDataAfterUploading*/const Key DELETE_VERTEX_DATA_AFTER_UPLOADING("deleteVertexDataAfterUploading", true); - /*ldraw.enableTexmapSupport*/const Key ENABLE_TEXMAP_SUPPORT("enableTexmapSupport", true); - /*graphics.displayConnectorDataIn3DView*/const Key DISPLAY_CONNECTOR_DATA_IN_3D_VIEW("displayConnectorDataIn3DView", false); - /*editor.newFileLocation*/const Key NEW_FILE_LOCATION("newFileLocation", "~"); - /*PS.snapping.linearStepXZ*/const Key LINEAR_SNAP_STEP_XZ("linearSnapStepXZ", 20); - /*PS.snapping.linearStepY*/const Key LINEAR_SNAP_STEP_Y("linearSnapStepY", 20); - /*snapping.linearPresets*/const Key LINEAR_SNAP_PRESETS("linearSnapPresets", snap::DEFAULT_LINEAR_SNAP_DISTANCE_PRESETS); - /*PS.snapping.enabled*/const Key SNAP_ENABLED("snapEnabled", true); - /*snapping.rotationalPresets*/const Key ROTATIONAL_SNAP_PRESETS("rotationalSnapPresets", snap::DEFAULT_ROTATIONAL_SNAP_PRESETS); - /*PS.snapping.rotationalStep*/const Key ROTATIONAL_SNAP_STEP("rotationalSnapStep", 90.f); - /*keyboardShortcuts.defaultCount*/const Key DEFAULT_KEYBOARD_SHORTCUT_COUNT("defaultKeyboardShortcutCount", 0); - /*system.renderingTmpDirectory*/const Key RENDERING_TMP_DIRECTORY("renderingTmpDirectory", "{tmp}/BrickSimRenderingTmp"); - /*system.clearRenderingTmpDirectoryOnExit*/const Key CLEAR_RENDERING_TMP_DIRECTORY_ON_EXIT("clearRenderingTmpDirectoryOnExit", true); -} diff --git a/src/db.cpp b/src/db.cpp index a7aa124e..5882f7ea 100644 --- a/src/db.cpp +++ b/src/db.cpp @@ -6,29 +6,9 @@ namespace bricksim::db { namespace { - std::optional configDb; std::optional cacheDb; - const int NEWEST_CONFIG_DB_VERSION = 2; - const int NEWEST_CACHE_DB_VERSION = 2; - - void upgradeConfigDbToVersion(int newVersion) { - spdlog::info("Upgrading config.db3 to version {}", newVersion); - switch (newVersion) { - case 2: { - configDb->exec("CREATE TABLE key_shortcuts (" - " action INTEGER PRIMARY KEY," - " key INTEGER," - " modifier INTEGER," - " event INTEGER);"); - } - default: - break; - } - SQLite::Statement stmt(configDb.value(), "UPDATE meta SET version=?"); - stmt.bind(1, newVersion); - stmt.exec(); - } + constexpr int NEWEST_CACHE_DB_VERSION = 2; void upgradeCacheDbToVersion(int newVersion) { spdlog::info("Upgrading cache.db3 to version {}", newVersion); @@ -60,45 +40,9 @@ namespace bricksim::db { void initialize() { plScope("db::initialize"); - bool configDbNew = !std::filesystem::is_regular_file("config.db3"); - bool cacheDbNew = !std::filesystem::is_regular_file("cache.db3"); - configDb = SQLite::Database("config.db3", SQLite::OPEN_CREATE | SQLite::OPEN_READWRITE); + const bool cacheDbNew = !std::filesystem::is_regular_file("cache.db3"); cacheDb = SQLite::Database("cache.db3", SQLite::OPEN_CREATE | SQLite::OPEN_READWRITE); - if (configDbNew) { - // !! DO NOT EDIT THIS SCRIPT ANYMORE -> add it in upgradeConfigDbToVersion and increment NEWEST_CONFIG_DB_VERSION by 1 !! - configDb.value().exec("CREATE TABLE strings (" - " key TEXT PRIMARY KEY," - " value TEXT NOT NULL);" - //"CREATE UNIQUE INDEX idx_strings ON strings (key);" - - "CREATE TABLE ints (" - " key TEXT PRIMARY KEY," - " value INTEGER NOT NULL);" - //"CREATE UNIQUE INDEX idx_ints ON ints (key);" - - "CREATE TABLE doubles (" - " key TEXT PRIMARY KEY," - " value REAL NOT NULL);" - //"CREATE UNIQUE INDEX idx_doubles ON doubles (key);" - - "CREATE TABLE meta (version INTEGER);" - "INSERT INTO meta (version) VALUES (1);"); - } - while (true) { - SQLite::Statement stmt(configDb.value(), "SELECT version FROM meta"); - if (stmt.executeStep()) { - int currentDbVersion = stmt.getColumn(0); - if (currentDbVersion < NEWEST_CONFIG_DB_VERSION) { - upgradeConfigDbToVersion(currentDbVersion + 1); - } else { - break; - } - } else { - throw std::invalid_argument("config.meta table should have exactly one record!"); - } - } - if (cacheDbNew) { // !! DO NOT EDIT THIS SCRIPT ANYMORE -> add it in upgradeCacheDbToVersion and increment NEWEST_CACHE_DB_VERSION by 1 !! cacheDb.value().exec("CREATE TABLE files (" @@ -134,7 +78,7 @@ namespace bricksim::db { while (true) { SQLite::Statement stmt(cacheDb.value(), "SELECT version FROM meta"); if (stmt.executeStep()) { - int currentDbVersion = stmt.getColumn(0); + const int currentDbVersion = stmt.getColumn(0); if (currentDbVersion < NEWEST_CACHE_DB_VERSION) { upgradeCacheDbToVersion(currentDbVersion + 1); } else { @@ -235,61 +179,6 @@ namespace bricksim::db { } } - namespace config { - std::optional getString(const char* key) { - SQLite::Statement query(configDb.value(), "SELECT value FROM strings WHERE key=?"); - query.bind(1, key); - if (query.executeStep()) { - return std::make_optional(query.getColumn(0).getString()); - } - return std::nullopt; - } - - std::optional getInt(const char* key) { - SQLite::Statement query(configDb.value(), "SELECT value FROM ints WHERE key=?"); - query.bind(1, key); - if (query.executeStep()) { - return query.getColumn(0).getInt(); - } - return {}; - } - - std::optional getDouble(const char* key) { - SQLite::Statement query(configDb.value(), "SELECT value FROM doubles WHERE key=?"); - query.bind(1, key); - if (query.executeStep()) { - return query.getColumn(0).getDouble(); - } - return {}; - } - - void setString(const char* key, const std::string& value) { - SQLite::Statement query(configDb.value(), "REPLACE INTO strings (key, value) VALUES (?, ?)"); - query.bind(1, key); - query.bind(2, value); - query.exec(); - } - - void setInt(const char* key, int value) { - SQLite::Statement query(configDb.value(), "REPLACE INTO ints (key, value) VALUES (?, ?)"); - query.bind(1, key); - query.bind(2, value); - query.exec(); - } - - void setDouble(const char* key, double value) { - SQLite::Statement query(configDb.value(), "REPLACE INTO doubles (key, value) VALUES (?, ?)"); - query.bind(1, key); - query.bind(2, value); - query.exec(); - } - - void deleteAll() { - SQLite::Statement query(configDb.value(), "DELETE FROM strings; DELETE FROM ints; DELETE FROM doubles;"); - query.exec(); - } - } - namespace fileList { int getSize() { @@ -387,33 +276,4 @@ namespace bricksim::db { stmt.exec(); } } - - namespace key_shortcuts { - - std::vector loadShortcuts() { - SQLite::Statement stmt(configDb.value(), "SELECT action, key, modifier, event FROM key_shortcuts;"); - auto result = std::vector(); - while (stmt.executeStep()) { - result.emplace_back( - stmt.getColumn(0).getInt(), - stmt.getColumn(1).getInt(), - stmt.getColumn(2).getInt(), - stmt.getColumn(3).getInt()); - } - return result; - } - - void saveShortcut(record_t record) { - SQLite::Statement stmt(configDb.value(), "INSERT INTO key_shortcuts (action, key, modifier, event) VALUES (?, ?, ?, ?);"); - stmt.bind(1, std::get<0>(record)); - stmt.bind(2, std::get<1>(record)); - stmt.bind(3, std::get<2>(record)); - stmt.bind(4, std::get<3>(record)); - stmt.exec(); - } - - void deleteAll() { - configDb->exec("DELETE FROM key_shortcuts"); - } - } } diff --git a/src/db.h b/src/db.h index 101edd26..f877e18b 100644 --- a/src/db.h +++ b/src/db.h @@ -61,23 +61,4 @@ namespace bricksim::db { template<> void set(const char* key, std::string value); } - - namespace config { - std::optional getString(const char* key); - std::optional getInt(const char* key); - std::optional getDouble(const char* key); - - void setString(const char* key, const std::string& value); - void setInt(const char* key, int value); - void setDouble(const char* key, double value); - - void deleteAll(); - } - - namespace key_shortcuts { - using record_t = std::tuple; - std::vector loadShortcuts(); - void saveShortcut(record_t record); - void deleteAll(); - } } diff --git a/src/keyboard_shortcut_manager.cpp b/src/keyboard_shortcut_manager.cpp index d5b62897..5468475a 100644 --- a/src/keyboard_shortcut_manager.cpp +++ b/src/keyboard_shortcut_manager.cpp @@ -10,7 +10,6 @@ #include namespace bricksim::keyboard_shortcut_manager { - namespace modifier { constexpr auto NONE = static_cast(0); constexpr auto CTRL = static_cast(GLFW_MOD_CONTROL); @@ -98,7 +97,7 @@ namespace bricksim::keyboard_shortcut_manager { GLFW_KEY_PERIOD, GLFW_KEY_SLASH, }; - static_assert(SPECIAL_CHARS.size()==SPECIAL_CHAR_KEYS.size()); + static_assert(SPECIAL_CHARS.size() == SPECIAL_CHAR_KEYS.size()); /// IMPORTANT: always append new shortcuts at the end, otherwise existing configs aren't updated correctly const std::array DEFAULT_SHORTCUTS = { @@ -188,7 +187,7 @@ namespace bricksim::keyboard_shortcut_manager { saveNewDefaultsToDB(); } - void shortcutPressed(key_t key, uint64_t keyAction, modifier_t modifiers, bool isCapturedByGui) { + void shortcutPressed(int key, int keyAction, modifier_t modifiers, const bool isCapturedByGui) { for (const auto& modifierKey: modifier::ALL_KEYS) { if (modifierKey == key) { return; diff --git a/src/keyboard_shortcut_manager.h b/src/keyboard_shortcut_manager.h index 21dba46e..b68b4bd6 100644 --- a/src/keyboard_shortcut_manager.h +++ b/src/keyboard_shortcut_manager.h @@ -1,13 +1,9 @@ #pragma once -#include "gui/windows/windows.h" -#include "magic_enum.hpp" #include "config/data.h" -#include #include #include -#include namespace bricksim::user_actions { enum Action : uint32_t; @@ -16,7 +12,7 @@ namespace bricksim::user_actions { namespace bricksim::keyboard_shortcut_manager { using modifier_t = decltype(config::KeyboardShortcut::modifiers); void initialize(); - void shortcutPressed(key_t key, uint64_t keyAction, modifier_t modifiers, bool isCapturedByGui); + void shortcutPressed(int key, int keyAction, modifier_t modifiers, bool isCapturedByGui); //std::vector& getAllShortcuts(); //void replaceAllShortcuts(const std::vector& newShortcuts); void resetToDefault();