track-width fixes

This commit is contained in:
MihailRis 2024-04-28 20:09:55 +03:00
parent 788bd6bf0f
commit 167388aefb
12 changed files with 66 additions and 57 deletions

View File

@ -1,7 +1,7 @@
<panel size='400' color='0' padding='8'>
<label id='sensitivity_label'>-</label>
<trackbar id='sensitivity_track'
min='0.1' max='10' value='2' step='0.1' track-width='4'
min='0.1' max='10' value='2' step='0.1'
consumer='change_sensitivity'>
</trackbar>
<panel id='bindings_panel' size='380,200' padding='2' interval='1' max-length='400' color='#0000004C'>

View File

@ -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

View File

@ -11,7 +11,7 @@ function new_volume_control(setting, id, name)
document.tracks_panel:add("<label id='l_"..id.."'>-</label>")
-- value track-bar
document.tracks_panel:add(string.format(
"<trackbar id='t_%s' min='0' max='1' value='%s' step='0.01' track-width='5' "..
"<trackbar id='t_%s' min='0' max='1' value='%s' step='0.01' "..
" consumer='function(x) on_volume_change(%q, %q, %q, x) end'/>"
, id, core.get_setting(setting), setting, id, name))
refresh_label(setting, id, name)

View File

@ -1,7 +1,6 @@
<panel color='0' context='settings'>
<label id='%{id}.L'>%{name}: %{value}%{postfix}</label>
<trackbar
value='%{value}' min='%{min}' max='%{max}'
step='%{step}' track-width="%{track_width}"
value='%{value}' min='%{min}' max='%{max}' step='%{step}'
consumer='function(x) update_setting(x, "%{id}", "%{name}", "%{postfix}") end'/>
</panel>

View File

@ -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"

View File

@ -5,6 +5,7 @@
#include "../coders/toml.h"
#include "../coders/json.h"
#include "../debug/Logger.hpp"
#include "../settings.h"
#include <memory>
@ -166,44 +167,3 @@ void SettingsHandler::setValue(const std::string& name, const dynamic::Value& va
std::vector<Section>& 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);
}
}

View File

@ -1,12 +1,15 @@
#ifndef FILES_SETTINGS_IO_HPP_
#define FILES_SETTINGS_IO_HPP_
#include "../data/dynamic.h"
#include <string>
#include <memory>
#include <vector>
#include <unordered_map>
#include "../settings.h"
#include "../data/dynamic.h"
class Setting;
struct EngineSettings;
struct Section {
std::string name;
@ -27,8 +30,4 @@ public:
std::vector<Section>& getSections();
};
std::string write_controls();
void load_controls(std::string filename, std::string source);
#endif // FILES_SETTINGS_IO_HPP_

View File

@ -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);

View File

@ -351,7 +351,7 @@ static std::shared_ptr<UINode> 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<TrackBar>(min, max, def, step, trackWidth);
_readUINode(reader, element, *bar);
if (element->has("consumer")) {

View File

@ -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();

View File

@ -1,9 +1,12 @@
#include <iostream>
#include "Events.h"
#include "../debug/Logger.hpp"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <string.h>
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);
}
}

View File

@ -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_ */