bindings.toml, config/bindings.toml file

This commit is contained in:
MihailRis 2024-05-20 04:06:49 +03:00
parent 7fdacffe53
commit 66476ee642
11 changed files with 79 additions and 31 deletions

17
res/bindings.toml Normal file
View File

@ -0,0 +1,17 @@
devtools.console="key:grave-accent"
movement.forward="key:w"
movement.back="key:s"
movement.left="key:a"
movement.right="key:d"
movement.jump="key:space"
movement.sprint="key:left-ctrl"
movement.crouch="key:left-shift"
movement.cheat="key:r"
camera.zoom="key:c"
camera.mode="key:f4"
player.noclip="key:n"
player.flight="key:f"
player.attack="mouse:left"
player.build="mouse:right"
player.pick="mouse:middle"
hud.inventory="key:tab"

View File

@ -17,6 +17,7 @@ function on_open()
local panel = document.bindings_panel local panel = document.bindings_panel
local bindings = core.get_bindings() local bindings = core.get_bindings()
table.sort(bindings, function(a, b) return a > b end)
for i,name in ipairs(bindings) do for i,name in ipairs(bindings) do
panel:add(gui.template("binding", { panel:add(gui.template("binding", {
id=name, name=gui.str(name) id=name, name=gui.str(name)

View File

@ -3,13 +3,15 @@
#include "items/ItemDef.hpp" #include "items/ItemDef.hpp"
#include "content/Content.hpp" #include "content/Content.hpp"
#include "content/ContentBuilder.hpp" #include "content/ContentBuilder.hpp"
#include "files/files.hpp"
#include "files/engine_paths.hpp"
#include "window/Window.hpp" #include "window/Window.hpp"
#include "window/Events.hpp" #include "window/Events.hpp"
#include "window/input.hpp" #include "window/input.hpp"
#include "voxels/Block.hpp" #include "voxels/Block.hpp"
// All in-game definitions (blocks, items, etc..) // All in-game definitions (blocks, items, etc..)
void corecontent::setup(ContentBuilder* builder) { void corecontent::setup(EnginePaths* paths, ContentBuilder* builder) {
Block& block = builder->createBlock("core:air"); Block& block = builder->createBlock("core:air");
block.replaceable = true; block.replaceable = true;
block.drawGroup = 1; block.drawGroup = 1;
@ -22,24 +24,11 @@ void corecontent::setup(ContentBuilder* builder) {
ItemDef& item = builder->createItem("core:empty"); ItemDef& item = builder->createItem("core:empty");
item.iconType = item_icon_type::none; item.iconType = item_icon_type::none;
}
void corecontent::setup_bindings() { auto bindsFile = paths->getResources()/fs::path("bindings.toml");
Events::bind(BIND_DEVTOOLS_CONSOLE, inputtype::keyboard, keycode::GRAVE_ACCENT); if (fs::is_regular_file(bindsFile)) {
Events::bind(BIND_MOVE_FORWARD, inputtype::keyboard, keycode::W); Events::loadBindingsToml(
Events::bind(BIND_MOVE_BACK, inputtype::keyboard, keycode::S); bindsFile.u8string(), files::read_string(bindsFile)
Events::bind(BIND_MOVE_RIGHT, inputtype::keyboard, keycode::D); );
Events::bind(BIND_MOVE_LEFT, inputtype::keyboard, keycode::A); }
Events::bind(BIND_MOVE_JUMP, inputtype::keyboard, keycode::SPACE);
Events::bind(BIND_MOVE_SPRINT, inputtype::keyboard, keycode::LEFT_CONTROL);
Events::bind(BIND_MOVE_CROUCH, inputtype::keyboard, keycode::LEFT_SHIFT);
Events::bind(BIND_MOVE_CHEAT, inputtype::keyboard, keycode::R);
Events::bind(BIND_CAM_ZOOM, inputtype::keyboard, keycode::C);
Events::bind(BIND_CAM_MODE, inputtype::keyboard, keycode::F4);
Events::bind(BIND_PLAYER_NOCLIP, inputtype::keyboard, keycode::N);
Events::bind(BIND_PLAYER_FLIGHT, inputtype::keyboard, keycode::F);
Events::bind(BIND_PLAYER_ATTACK, inputtype::mouse, mousecode::BUTTON_1);
Events::bind(BIND_PLAYER_BUILD, inputtype::mouse, mousecode::BUTTON_2);
Events::bind(BIND_PLAYER_PICK, inputtype::mouse, mousecode::BUTTON_3);
Events::bind(BIND_HUD_INVENTORY, inputtype::keyboard, keycode::TAB);
} }

View File

@ -27,11 +27,11 @@ inline const std::string BIND_PLAYER_BUILD = "player.build";
inline const std::string BIND_PLAYER_PICK = "player.pick"; inline const std::string BIND_PLAYER_PICK = "player.pick";
inline const std::string BIND_HUD_INVENTORY = "hud.inventory"; inline const std::string BIND_HUD_INVENTORY = "hud.inventory";
class EnginePaths;
class ContentBuilder; class ContentBuilder;
namespace corecontent { namespace corecontent {
void setup_bindings(); void setup(EnginePaths* paths, ContentBuilder* builder);
void setup(ContentBuilder* builder);
} }
#endif // CORE_DEFS_HPP_ #endif // CORE_DEFS_HPP_

View File

@ -64,7 +64,6 @@ Engine::Engine(EngineSettings& settings, SettingsHandler& settingsHandler, Engin
: settings(settings), settingsHandler(settingsHandler), paths(paths), : settings(settings), settingsHandler(settingsHandler), paths(paths),
interpreter(std::make_unique<cmd::CommandsInterpreter>()) interpreter(std::make_unique<cmd::CommandsInterpreter>())
{ {
corecontent::setup_bindings();
loadSettings(); loadSettings();
controller = std::make_unique<EngineController>(this); controller = std::make_unique<EngineController>(this);
@ -255,7 +254,7 @@ void Engine::loadAssets() {
void Engine::loadContent() { void Engine::loadContent() {
auto resdir = paths->getResources(); auto resdir = paths->getResources();
ContentBuilder contentBuilder; ContentBuilder contentBuilder;
corecontent::setup(&contentBuilder); corecontent::setup(paths, &contentBuilder);
paths->setContentPacks(&contentPacks); paths->setContentPacks(&contentPacks);
std::vector<std::string> names; std::vector<std::string> names;
@ -273,6 +272,14 @@ void Engine::loadContent() {
ContentLoader loader(&pack); ContentLoader loader(&pack);
loader.load(contentBuilder); loader.load(contentBuilder);
auto configFolder = pack.folder/fs::path("config");
auto bindsFile = configFolder/fs::path("bindings.toml");
if (fs::is_regular_file(bindsFile)) {
Events::loadBindingsToml(
bindsFile.u8string(), files::read_string(bindsFile)
);
}
} }
content = contentBuilder.build(); content = contentBuilder.build();
resPaths = std::make_unique<ResPaths>(resdir, resRoots); resPaths = std::make_unique<ResPaths>(resdir, resRoots);

View File

@ -2,6 +2,7 @@
#include "../coders/commons.hpp" #include "../coders/commons.hpp"
#include "../coders/json.hpp" #include "../coders/json.hpp"
#include "../coders/toml.hpp"
#include "../coders/gzip.hpp" #include "../coders/gzip.hpp"
#include "../util/stringutil.hpp" #include "../util/stringutil.hpp"
#include "../data/dynamic.hpp" #include "../data/dynamic.hpp"
@ -121,6 +122,10 @@ std::shared_ptr<dynamic::Map> files::read_binary_json(fs::path file) {
return json::from_binary(bytes.get(), size); return json::from_binary(bytes.get(), size);
} }
std::shared_ptr<dynamic::Map> files::read_toml(fs::path file) {
return toml::parse(file.u8string(), files::read_string(file));
}
std::vector<std::string> files::read_list(fs::path filename) { std::vector<std::string> files::read_list(fs::path filename) {
std::ifstream file(filename); std::ifstream file(filename);
if (!file) { if (!file) {

View File

@ -44,10 +44,7 @@ namespace files {
/// @brief Write dynamic data to the JSON file /// @brief Write dynamic data to the JSON file
/// @param nice if true, human readable format will be used, otherwise minimal /// @param nice if true, human readable format will be used, otherwise minimal
bool write_json( bool write_json(fs::path filename, const dynamic::Map* obj, bool nice=true);
fs::path filename,
const dynamic::Map* obj,
bool nice=true);
/// @brief Write dynamic data to the binary JSON file /// @brief Write dynamic data to the binary JSON file
/// (see src/coders/binary_json_spec.md) /// (see src/coders/binary_json_spec.md)
@ -66,6 +63,7 @@ namespace files {
/// @param file *.json or *.bjson file /// @param file *.json or *.bjson file
std::shared_ptr<dynamic::Map> read_json(fs::path file); std::shared_ptr<dynamic::Map> read_json(fs::path file);
std::shared_ptr<dynamic::Map> read_binary_json(fs::path file); std::shared_ptr<dynamic::Map> read_binary_json(fs::path file);
std::shared_ptr<dynamic::Map> read_toml(fs::path file);
std::vector<std::string> read_list(fs::path file); std::vector<std::string> read_list(fs::path file);
} }

View File

@ -6,6 +6,7 @@
#include "../../coders/imageio.hpp" #include "../../coders/imageio.hpp"
#include "../../debug/Logger.hpp" #include "../../debug/Logger.hpp"
#include "../../engine.hpp" #include "../../engine.hpp"
#include "../../files/files.hpp"
#include "../../graphics/core/DrawContext.hpp" #include "../../graphics/core/DrawContext.hpp"
#include "../../graphics/core/ImageData.hpp" #include "../../graphics/core/ImageData.hpp"
#include "../../graphics/core/PostProcessing.hpp" #include "../../graphics/core/PostProcessing.hpp"
@ -15,6 +16,7 @@
#include "../../graphics/ui/GUI.hpp" #include "../../graphics/ui/GUI.hpp"
#include "../../logic/LevelController.hpp" #include "../../logic/LevelController.hpp"
#include "../../logic/scripting/scripting_hud.hpp" #include "../../logic/scripting/scripting_hud.hpp"
#include "../../util/stringutil.hpp"
#include "../../physics/Hitbox.hpp" #include "../../physics/Hitbox.hpp"
#include "../../voxels/Chunks.hpp" #include "../../voxels/Chunks.hpp"
#include "../../window/Camera.hpp" #include "../../window/Camera.hpp"
@ -66,7 +68,6 @@ void LevelScreen::initializePack(ContentPackRuntime* pack) {
if (fs::is_regular_file(scriptFile)) { if (fs::is_regular_file(scriptFile)) {
scripting::load_hud_script(pack->getEnvironment(), info.id, scriptFile); scripting::load_hud_script(pack->getEnvironment(), info.id, scriptFile);
} }
auto configFolder = info.folder/fs::path("config");
} }
LevelScreen::~LevelScreen() { LevelScreen::~LevelScreen() {

View File

@ -1,6 +1,7 @@
#include "Events.hpp" #include "Events.hpp"
#include "Window.hpp" #include "Window.hpp"
#include "../debug/Logger.hpp" #include "../debug/Logger.hpp"
#include "../util/stringutil.hpp"
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
@ -148,6 +149,7 @@ void Events::setPosition(float xpos, float ypos) {
#include "../data/dynamic.hpp" #include "../data/dynamic.hpp"
#include "../coders/json.hpp" #include "../coders/json.hpp"
#include "../coders/toml.hpp"
std::string Events::writeBindings() { std::string Events::writeBindings() {
dynamic::Map obj; dynamic::Map obj;
@ -189,3 +191,28 @@ void Events::loadBindings(const std::string& filename, const std::string& source
jentry->num("code", binding.code); jentry->num("code", binding.code);
} }
} }
void Events::loadBindingsToml(const std::string& filename, const std::string& source) {
auto map = toml::parse(filename, source);
for (auto& entry : map->values) {
if (auto value = std::get_if<std::string>(&entry.second)) {
auto [prefix, codename] = util::split_at(*value, ':');
inputtype type;
int code;
if (prefix == "key") {
type = inputtype::keyboard;
code = static_cast<int>(input_util::keycode_from(codename));
} else if (prefix == "mouse") {
type = inputtype::mouse;
code = static_cast<int>(input_util::mousecode_from(codename));
} else {
logger.error() << "unknown input type: " << prefix
<< " (binding " << util::quote(entry.first) << ")";
continue;
}
Events::bind(entry.first, type, code);
} else {
logger.error() << "invalid binding entry: " << entry.first;
}
}
}

View File

@ -2,11 +2,11 @@
#define WINDOW_EVENTS_HPP_ #define WINDOW_EVENTS_HPP_
#include "input.hpp" #include "input.hpp"
#include "../typedefs.hpp"
#include <string> #include <string>
#include <vector> #include <vector>
#include <unordered_map> #include <unordered_map>
#include "../typedefs.hpp"
inline constexpr short KEYS_BUFFER_SIZE = 1036; inline constexpr short KEYS_BUFFER_SIZE = 1036;
@ -51,6 +51,7 @@ public:
static std::string writeBindings(); static std::string writeBindings();
static void loadBindings(const std::string& filename, const std::string& source); static void loadBindings(const std::string& filename, const std::string& source);
static void loadBindingsToml(const std::string& filename, const std::string& source);
}; };
#endif // WINDOW_EVENTS_HPP_ #endif // WINDOW_EVENTS_HPP_

View File

@ -15,6 +15,7 @@ static std::unordered_map<std::string, int> keycodes {
{"delete", GLFW_KEY_DELETE}, {"delete", GLFW_KEY_DELETE},
{"home", GLFW_KEY_HOME}, {"home", GLFW_KEY_HOME},
{"end", GLFW_KEY_END}, {"end", GLFW_KEY_END},
{"tab", GLFW_KEY_TAB},
{"insert", GLFW_KEY_INSERT}, {"insert", GLFW_KEY_INSERT},
{"page-down", GLFW_KEY_PAGE_DOWN}, {"page-down", GLFW_KEY_PAGE_DOWN},
{"page-up", GLFW_KEY_PAGE_UP}, {"page-up", GLFW_KEY_PAGE_UP},
@ -26,6 +27,7 @@ static std::unordered_map<std::string, int> keycodes {
{"right-alt", GLFW_KEY_RIGHT_ALT}, {"right-alt", GLFW_KEY_RIGHT_ALT},
{"left-super", GLFW_KEY_LEFT_SUPER}, {"left-super", GLFW_KEY_LEFT_SUPER},
{"right-super", GLFW_KEY_RIGHT_SUPER}, {"right-super", GLFW_KEY_RIGHT_SUPER},
{"grave-accent", GLFW_KEY_GRAVE_ACCENT},
{"left", GLFW_KEY_LEFT}, {"left", GLFW_KEY_LEFT},
{"right", GLFW_KEY_RIGHT}, {"right", GLFW_KEY_RIGHT},
{"down", GLFW_KEY_DOWN}, {"down", GLFW_KEY_DOWN},
@ -59,7 +61,7 @@ void input_util::initialize() {
keycodes["f"+std::to_string(i)] = GLFW_KEY_F1+i; keycodes["f"+std::to_string(i)] = GLFW_KEY_F1+i;
} }
for (char i = 'a'; i <= 'z'; i++) { for (char i = 'a'; i <= 'z'; i++) {
keycodes[std::to_string(i)] = GLFW_KEY_A-'a'+i; keycodes[std::string({i})] = GLFW_KEY_A-'a'+i;
} }
} }