#include "Keybinds.h" #include "../Core/Action.h" #include #include #include #include Keybinds::Keybinds(int layoutNumber) : layoutNumber(layoutNumber) { for (Action action : ACTION_LIST_IN_ORDER) { this->keybinds.insert({action, std::set()}); } this->loadKeybindsFromFile(); } void Keybinds::loadKeybindsFromFile() { std::ifstream layoutFile("data/config/keybinds/layout" + std::to_string(this->layoutNumber) + ".bin", std::ios::binary); for (Action action : ACTION_LIST_IN_ORDER) { this->keybinds.at(action).clear(); } char byte; while (layoutFile.get(&byte, 1)) { Action action = Action(byte); do { layoutFile.get(&byte, 1); if (byte != 0xFF) { this->keybinds.at(action).insert(sfKey(byte)); } } while (byte != 0xFF); } } void Keybinds::saveKeybindsToFile() const { std::ofstream layoutFile("data/config/keybinds/layout" + std::to_string(this->layoutNumber) + ".bin", std::ios::trunc | std::ios::binary); char byte; for (Action action : ACTION_LIST_IN_ORDER) { byte = action; layoutFile.write(&byte, 1); for (sfKey key : this->keybinds.at(action)) { byte = (int) key; layoutFile.write(&byte, 1); } byte = 0xFF; layoutFile.write(&byte, 1); } } void Keybinds::addKey(Action action, sfKey key) { this->keybinds.at(action).insert(key); } void Keybinds::clearKeys(Action action) { this->keybinds.at(action).clear(); } const std::set Keybinds::getActions(sfKey key) const { std::set actions; for (const auto& [action, keys] : this->keybinds) { if (keys.contains(key)) { actions.insert(action); } } return actions; } const std::set& Keybinds::getKeybinds(Action action) const { return this->keybinds.at(action); }