From 167388aefb4a96a4d0077a26968abb4054baad67 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 28 Apr 2024 20:09:55 +0300 Subject: [PATCH] track-width fixes --- res/layouts/pages/controls.xml | 2 +- res/layouts/pages/settings.xml.lua | 1 - res/layouts/pages/settings_audio.xml.lua | 2 +- res/layouts/templates/track_setting.xml | 3 +- src/coders/toml.cpp | 1 + src/files/settings_io.cpp | 42 +------------------- src/files/settings_io.hpp | 11 +++--- src/graphics/ui/elements/TrackBar.hpp | 2 +- src/graphics/ui/gui_xml.cpp | 2 +- src/voxel_engine.cpp | 5 ++- src/window/Events.cpp | 49 +++++++++++++++++++++++- src/window/Events.h | 3 ++ 12 files changed, 66 insertions(+), 57 deletions(-) diff --git a/res/layouts/pages/controls.xml b/res/layouts/pages/controls.xml index 3082474c..c42aa267 100644 --- a/res/layouts/pages/controls.xml +++ b/res/layouts/pages/controls.xml @@ -1,7 +1,7 @@ diff --git a/res/layouts/pages/settings.xml.lua b/res/layouts/pages/settings.xml.lua index 70f8c7f5..65c107a5 100644 --- a/res/layouts/pages/settings.xml.lua +++ b/res/layouts/pages/settings.xml.lua @@ -10,7 +10,6 @@ function create_setting(id, name, step, postfix) min=info.min, max=info.max, step=step, - track_width=12, postfix=postfix })) end diff --git a/res/layouts/pages/settings_audio.xml.lua b/res/layouts/pages/settings_audio.xml.lua index e1033774..0779d906 100644 --- a/res/layouts/pages/settings_audio.xml.lua +++ b/res/layouts/pages/settings_audio.xml.lua @@ -11,7 +11,7 @@ function new_volume_control(setting, id, name) document.tracks_panel:add("") -- value track-bar document.tracks_panel:add(string.format( - "" , id, core.get_setting(setting), setting, id, name)) refresh_label(setting, id, name) diff --git a/res/layouts/templates/track_setting.xml b/res/layouts/templates/track_setting.xml index 52b79f05..9f2546bd 100644 --- a/res/layouts/templates/track_setting.xml +++ b/res/layouts/templates/track_setting.xml @@ -1,7 +1,6 @@ diff --git a/src/coders/toml.cpp b/src/coders/toml.cpp index c1fc197b..7be28a52 100644 --- a/src/coders/toml.cpp +++ b/src/coders/toml.cpp @@ -1,5 +1,6 @@ #include "toml.h" #include "commons.h" +#include "../data/setting.hpp" #include "../data/dynamic.h" #include "../util/stringutil.h" #include "../files/settings_io.hpp" diff --git a/src/files/settings_io.cpp b/src/files/settings_io.cpp index 94fdcd86..44932aff 100644 --- a/src/files/settings_io.cpp +++ b/src/files/settings_io.cpp @@ -5,6 +5,7 @@ #include "../coders/toml.h" #include "../coders/json.h" #include "../debug/Logger.hpp" +#include "../settings.h" #include @@ -166,44 +167,3 @@ void SettingsHandler::setValue(const std::string& name, const dynamic::Value& va std::vector
& SettingsHandler::getSections() { return sections; } - -std::string write_controls() { - dynamic::Map obj; - for (auto& entry : Events::bindings) { - const auto& binding = entry.second; - - auto& jentry = obj.putMap(entry.first); - switch (binding.type) { - case inputtype::keyboard: jentry.put("type", "keyboard"); break; - case inputtype::mouse: jentry.put("type", "mouse"); break; - default: throw std::runtime_error("unsupported control type"); - } - jentry.put("code", binding.code); - } - return json::stringify(&obj, true, " "); -} - -void load_controls(std::string filename, std::string source) { - auto obj = json::parse(filename, source); - for (auto& entry : Events::bindings) { - auto& binding = entry.second; - - auto jentry = obj->map(entry.first); - if (jentry == nullptr) - continue; - inputtype type; - std::string typestr; - jentry->str("type", typestr); - - if (typestr == "keyboard") { - type = inputtype::keyboard; - } else if (typestr == "mouse") { - type = inputtype::mouse; - } else { - logger.error() << "unknown input type '" << typestr << "'"; - continue; - } - binding.type = type; - jentry->num("code", binding.code); - } -} diff --git a/src/files/settings_io.hpp b/src/files/settings_io.hpp index 20c0d8af..9a04d065 100644 --- a/src/files/settings_io.hpp +++ b/src/files/settings_io.hpp @@ -1,12 +1,15 @@ #ifndef FILES_SETTINGS_IO_HPP_ #define FILES_SETTINGS_IO_HPP_ +#include "../data/dynamic.h" + #include #include #include #include -#include "../settings.h" -#include "../data/dynamic.h" + +class Setting; +struct EngineSettings; struct Section { std::string name; @@ -27,8 +30,4 @@ public: std::vector
& getSections(); }; -std::string write_controls(); - -void load_controls(std::string filename, std::string source); - #endif // FILES_SETTINGS_IO_HPP_ diff --git a/src/graphics/ui/elements/TrackBar.hpp b/src/graphics/ui/elements/TrackBar.hpp index e27ff0db..c8525867 100644 --- a/src/graphics/ui/elements/TrackBar.hpp +++ b/src/graphics/ui/elements/TrackBar.hpp @@ -19,7 +19,7 @@ namespace gui { double max, double value, double step=1.0, - int trackWidth=1); + int trackWidth=12); virtual void draw(const GfxContext* pctx, Assets* assets) override; virtual void setSupplier(doublesupplier supplier); diff --git a/src/graphics/ui/gui_xml.cpp b/src/graphics/ui/gui_xml.cpp index 01f1f5cb..6117be37 100644 --- a/src/graphics/ui/gui_xml.cpp +++ b/src/graphics/ui/gui_xml.cpp @@ -351,7 +351,7 @@ static std::shared_ptr readTrackBar(UiXmlReader& reader, xml::xmlelement float max = element->attr("max", "1.0").asFloat(); float def = element->attr("value", "0.0").asFloat(); float step = element->attr("step", "1.0").asFloat(); - int trackWidth = element->attr("track-width", "1.0").asInt(); + int trackWidth = element->attr("track-width", "12.0").asInt(); auto bar = std::make_shared(min, max, def, step, trackWidth); _readUINode(reader, element, *bar); if (element->has("consumer")) { diff --git a/src/voxel_engine.cpp b/src/voxel_engine.cpp index 726b2b86..4454a53b 100644 --- a/src/voxel_engine.cpp +++ b/src/voxel_engine.cpp @@ -13,6 +13,7 @@ #include "files/engine_paths.h" #include "util/platform.h" #include "util/command_line.hpp" +#include "window/Events.h" #include "debug/Logger.hpp" inline std::string SETTINGS_FILE = "settings.toml"; @@ -47,13 +48,13 @@ int main(int argc, char** argv) { if (fs::is_regular_file(controls_file)) { logger.info() << "loading controls"; std::string text = files::read_string(controls_file); - load_controls(controls_file.string(), text); + Events::loadBindings(controls_file.string(), text); } engine.mainloop(); logger.info() << "saving settings"; files::write_string(settings_file, toml::stringify(handler)); - files::write_string(controls_file, write_controls()); + files::write_string(controls_file, Events::writeBindings()); } catch (const initialize_error& err) { logger.error() << "could not to initialize engine\n" << err.what(); diff --git a/src/window/Events.cpp b/src/window/Events.cpp index 2b993ad0..adf1ecb1 100644 --- a/src/window/Events.cpp +++ b/src/window/Events.cpp @@ -1,9 +1,12 @@ -#include #include "Events.h" +#include "../debug/Logger.hpp" + #include #include #include +static debug::Logger logger("events"); + inline constexpr short _MOUSE_KEYS_OFFSET = 1024; bool Events::keys[KEYS_BUFFER_SIZE] = {}; @@ -141,3 +144,47 @@ void Events::setPosition(float xpos, float ypos) { Events::cursor.x = xpos; Events::cursor.y = ypos; } + +#include "../data/dynamic.h" +#include "../coders/json.h" + +std::string Events::writeBindings() { + dynamic::Map obj; + for (auto& entry : Events::bindings) { + const auto& binding = entry.second; + + auto& jentry = obj.putMap(entry.first); + switch (binding.type) { + case inputtype::keyboard: jentry.put("type", "keyboard"); break; + case inputtype::mouse: jentry.put("type", "mouse"); break; + default: throw std::runtime_error("unsupported control type"); + } + jentry.put("code", binding.code); + } + return json::stringify(&obj, true, " "); +} + +void Events::loadBindings(const std::string& filename, const std::string& source) { + auto obj = json::parse(filename, source); + for (auto& entry : Events::bindings) { + auto& binding = entry.second; + + auto jentry = obj->map(entry.first); + if (jentry == nullptr) + continue; + inputtype type; + std::string typestr; + jentry->str("type", typestr); + + if (typestr == "keyboard") { + type = inputtype::keyboard; + } else if (typestr == "mouse") { + type = inputtype::mouse; + } else { + logger.error() << "unknown input type '" << typestr << "'"; + continue; + } + binding.type = type; + jentry->num("code", binding.code); + } +} diff --git a/src/window/Events.h b/src/window/Events.h index 1c8a4549..4a57d5f7 100644 --- a/src/window/Events.h +++ b/src/window/Events.h @@ -49,6 +49,9 @@ public: static void setButton(int button, bool b); static void setPosition(float xpos, float ypos); + + static std::string writeBindings(); + static void loadBindings(const std::string& filename, const std::string& source); }; #endif /* WINDOW_EVENTS_H_ */