From baaeb9ee102d2b80b1b32d007f91d095cb5d0aaa Mon Sep 17 00:00:00 2001 From: mushgunAX Date: Sun, 8 Jan 2023 21:05:09 +0800 Subject: [PATCH 1/3] Input Binding File I/O --- Assets/Bindings.SHConfig | 52 +++++++ SHADE_Engine/src/Input/SHInputManager.cpp | 169 ++++++++++++++++++++++ SHADE_Engine/src/Input/SHInputManager.h | 11 ++ 3 files changed, 232 insertions(+) create mode 100644 Assets/Bindings.SHConfig diff --git a/Assets/Bindings.SHConfig b/Assets/Bindings.SHConfig new file mode 100644 index 00000000..e0023603 --- /dev/null +++ b/Assets/Bindings.SHConfig @@ -0,0 +1,52 @@ +4 +Horizontal +0 +0 +5 +0.2 +5 +0 +2 +39 +68 +2 +37 +65 +2 +3 +16 +1 +2 +Mouse Wheel +3 +0 +1 +0.2 +1 +0 +0 +0 +0 +0 +Mouse X +1 +0 +1 +0.2 +1 +0 +0 +0 +0 +0 +Mouse Y +2 +0 +1 +0.2 +1 +0 +0 +0 +0 +0 diff --git a/SHADE_Engine/src/Input/SHInputManager.cpp b/SHADE_Engine/src/Input/SHInputManager.cpp index cec78648..a94b66d4 100644 --- a/SHADE_Engine/src/Input/SHInputManager.cpp +++ b/SHADE_Engine/src/Input/SHInputManager.cpp @@ -11,6 +11,7 @@ #pragma once #include +#include #include "SHInputManager.h" #include "../Tools/SHException.h" @@ -99,6 +100,174 @@ namespace SHADE } } + //The Binding File format presently goes as such: + /* + * Name + Binding Type Enum + Inverted Bool + Gravity Double + Dead Double + Sensitivity Double + Snap Bool + PositiveKeyCode count + PositiveKeyCodes + NegativeKeyCode count + NegativeKeyCodes + PositiveControllerCode Count + PositiveControllerCodes + NegativeControllerCode Count + NegativeControllerCodes + */ + void SHInputManager::SaveBindings(std::string const& targetFile) noexcept + { + std::ofstream file; + file.open("../../Assets/Bindings.SHConfig"); + + //File cannot be written to + if (!file) return; + + //First write the number of bindings + file << bindings.size() << std::endl; + + for (auto& b : bindings) + { + //Name + file << b.first << std::endl; + + //Data + auto& lbd = b.second; + + file << static_cast(lbd.bindingType) << std::endl; + file << static_cast(lbd.inverted) << std::endl; + file << lbd.gravity << std::endl; + file << lbd.dead << std::endl; + file << lbd.sensitivity << std::endl; + file << static_cast(lbd.snap) << std::endl; + + //Bindings + file << lbd.positiveKeyCodes.size() << std::endl; + for (auto kc : lbd.positiveKeyCodes) + file << static_cast(kc) << std::endl; + file << lbd.negativeKeyCodes.size() << std::endl; + for (auto kc : lbd.negativeKeyCodes) + file << static_cast(kc) << std::endl; + file << lbd.positiveControllerCodes.size() << std::endl; + for (auto cc : lbd.positiveControllerCodes) + file << static_cast(cc) << std::endl; + file << lbd.negativeControllerCodes.size() << std::endl; + for (auto cc : lbd.negativeControllerCodes) + file << static_cast(cc) << std::endl; + } + + file.close(); + } + + void SHInputManager::LoadBindings(std::string const& sourceFile) noexcept + { + std::ifstream file; + file.open("../../Assets/Bindings.SHConfig"); + + //Check + if (!file) return; + + //Erase + ClearBindings(); + + //Read + std::string read; + + int count = 0; + std::getline(file, read); + count = std::stoi(read); + + std::string bindingName; + for (int b = 0; b < count; ++b) + { + //Name + std::getline(file, read); + SHLOGV_CRITICAL("Binding Name: {}", read); + bindingName = read; + AddBinding(bindingName); + + //Type + std::getline(file, read); + SHLOGV_CRITICAL("Binding Type: {}", read); + SetBindingType(bindingName, static_cast(std::stoi(read))); + + //Inversion + std::getline(file, read); + SHLOGV_CRITICAL("Inversion: {}", read); + SetBindingInverted(bindingName, static_cast(std::stoi(read))); + + //Gravity + std::getline(file, read); + SHLOGV_CRITICAL("Gravity: {}", read); + SetBindingGravity(bindingName, std::stod(read)); + + //Dead + std::getline(file, read); + SHLOGV_CRITICAL("Dead: {}", read); + SetBindingDead(bindingName, std::stod(read)); + + //Sensitivity + std::getline(file, read); + SHLOGV_CRITICAL("Sensitivity: {}", read); + SetBindingSensitivity(bindingName, std::stod(read)); + + //Snap + std::getline(file, read); + SHLOGV_CRITICAL("Snap: {}", read); + SetBindingSnap(bindingName, static_cast(std::stoi(read))); + + int count = 0; + //Positive Key Codes + std::getline(file, read); + SHLOGV_CRITICAL("Positive Key Count: {}", read); + count = std::stoi(read); + for (int i = 0; i < count; ++i) + { + std::getline(file, read); + SHLOGV_CRITICAL("Positive Key: {}", read); + AddBindingPositiveKeyCode(bindingName, static_cast(std::stoi(read))); + } + + //Negative Key Codes + std::getline(file, read); + SHLOGV_CRITICAL("Negative Key Count: {}", read); + count = std::stoi(read); + for (int i = 0; i < count; ++i) + { + std::getline(file, read); + SHLOGV_CRITICAL("Negative Key: {}", read); + AddBindingNegativeKeyCode(bindingName, static_cast(std::stoi(read))); + } + + //Positive Controller Codes + std::getline(file, read); + SHLOGV_CRITICAL("Positive Controller Count: {}", read); + count = std::stoi(read); + for (int i = 0; i < count; ++i) + { + std::getline(file, read); + SHLOGV_CRITICAL("Positive Controller: {}", read); + AddBindingPositiveControllerCode(bindingName, static_cast(std::stoi(read))); + } + + //Negative Controller Codes + std::getline(file, read); + SHLOGV_CRITICAL("Negative Controller Count: {}", read); + count = std::stoi(read); + for (int i = 0; i < count; ++i) + { + std::getline(file, read); + SHLOGV_CRITICAL("Negative Controller: {}", read); + AddBindingNegativeControllerCode(bindingName, static_cast(std::stoi(read))); + } + } + + file.close(); + } + void SHInputManager::UpdateInput(double dt) noexcept { //Keyboard and Mouse Buttons//////////////////////////////////////////////// diff --git a/SHADE_Engine/src/Input/SHInputManager.h b/SHADE_Engine/src/Input/SHInputManager.h index 3f708124..977a2d08 100644 --- a/SHADE_Engine/src/Input/SHInputManager.h +++ b/SHADE_Engine/src/Input/SHInputManager.h @@ -681,6 +681,17 @@ namespace SHADE return controllersReleasedTime[controllerNum][static_cast(code)]; } + /*------------------------------------------------------------------------*/ + /* Binding I/O */ + /*------------------------------------------------------------------------*/ + + //Save bindings registered into a file + static void SaveBindings(std::string const& targetFile = "Assets/InputBindings.SHConfig") noexcept; + + //Load and register bindings from a file + //The current list of bindings will be overwritten, so save them somewhere else before loading + static void LoadBindings(std::string const& sourceFile = "Assets/InputBindings.SHConfig") noexcept; + /*------------------------------------------------------------------------*/ /* Binding Functions */ /*------------------------------------------------------------------------*/ -- 2.40.1 From 4123e76a7d991add7f03f57b2b6ba9512b5f92b0 Mon Sep 17 00:00:00 2001 From: mushgunAX Date: Sun, 8 Jan 2023 21:36:19 +0800 Subject: [PATCH 2/3] Checking Input Binding I/O --- SHADE_Engine/src/Input/SHInputManager.cpp | 21 ++++----------------- SHADE_Engine/src/Input/SHInputManager.h | 4 ++-- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/SHADE_Engine/src/Input/SHInputManager.cpp b/SHADE_Engine/src/Input/SHInputManager.cpp index a94b66d4..b8f329b9 100644 --- a/SHADE_Engine/src/Input/SHInputManager.cpp +++ b/SHADE_Engine/src/Input/SHInputManager.cpp @@ -102,6 +102,8 @@ namespace SHADE //The Binding File format presently goes as such: /* + * Binding count + * (For each binding:) * Name Binding Type Enum Inverted Bool @@ -121,7 +123,7 @@ namespace SHADE void SHInputManager::SaveBindings(std::string const& targetFile) noexcept { std::ofstream file; - file.open("../../Assets/Bindings.SHConfig"); + file.open(targetFile); //File cannot be written to if (!file) return; @@ -165,7 +167,7 @@ namespace SHADE void SHInputManager::LoadBindings(std::string const& sourceFile) noexcept { std::ifstream file; - file.open("../../Assets/Bindings.SHConfig"); + file.open(sourceFile); //Check if (!file) return; @@ -185,82 +187,67 @@ namespace SHADE { //Name std::getline(file, read); - SHLOGV_CRITICAL("Binding Name: {}", read); bindingName = read; AddBinding(bindingName); //Type std::getline(file, read); - SHLOGV_CRITICAL("Binding Type: {}", read); SetBindingType(bindingName, static_cast(std::stoi(read))); //Inversion std::getline(file, read); - SHLOGV_CRITICAL("Inversion: {}", read); SetBindingInverted(bindingName, static_cast(std::stoi(read))); //Gravity std::getline(file, read); - SHLOGV_CRITICAL("Gravity: {}", read); SetBindingGravity(bindingName, std::stod(read)); //Dead std::getline(file, read); - SHLOGV_CRITICAL("Dead: {}", read); SetBindingDead(bindingName, std::stod(read)); //Sensitivity std::getline(file, read); - SHLOGV_CRITICAL("Sensitivity: {}", read); SetBindingSensitivity(bindingName, std::stod(read)); //Snap std::getline(file, read); - SHLOGV_CRITICAL("Snap: {}", read); SetBindingSnap(bindingName, static_cast(std::stoi(read))); int count = 0; //Positive Key Codes std::getline(file, read); - SHLOGV_CRITICAL("Positive Key Count: {}", read); count = std::stoi(read); for (int i = 0; i < count; ++i) { std::getline(file, read); - SHLOGV_CRITICAL("Positive Key: {}", read); AddBindingPositiveKeyCode(bindingName, static_cast(std::stoi(read))); } //Negative Key Codes std::getline(file, read); - SHLOGV_CRITICAL("Negative Key Count: {}", read); count = std::stoi(read); for (int i = 0; i < count; ++i) { std::getline(file, read); - SHLOGV_CRITICAL("Negative Key: {}", read); AddBindingNegativeKeyCode(bindingName, static_cast(std::stoi(read))); } //Positive Controller Codes std::getline(file, read); - SHLOGV_CRITICAL("Positive Controller Count: {}", read); count = std::stoi(read); for (int i = 0; i < count; ++i) { std::getline(file, read); - SHLOGV_CRITICAL("Positive Controller: {}", read); AddBindingPositiveControllerCode(bindingName, static_cast(std::stoi(read))); } //Negative Controller Codes std::getline(file, read); - SHLOGV_CRITICAL("Negative Controller Count: {}", read); count = std::stoi(read); for (int i = 0; i < count; ++i) { std::getline(file, read); - SHLOGV_CRITICAL("Negative Controller: {}", read); AddBindingNegativeControllerCode(bindingName, static_cast(std::stoi(read))); } } diff --git a/SHADE_Engine/src/Input/SHInputManager.h b/SHADE_Engine/src/Input/SHInputManager.h index 977a2d08..01ef6c42 100644 --- a/SHADE_Engine/src/Input/SHInputManager.h +++ b/SHADE_Engine/src/Input/SHInputManager.h @@ -686,11 +686,11 @@ namespace SHADE /*------------------------------------------------------------------------*/ //Save bindings registered into a file - static void SaveBindings(std::string const& targetFile = "Assets/InputBindings.SHConfig") noexcept; + static void SaveBindings(std::string const& targetFile = "../../Assets/Bindings.SHConfig") noexcept; //Load and register bindings from a file //The current list of bindings will be overwritten, so save them somewhere else before loading - static void LoadBindings(std::string const& sourceFile = "Assets/InputBindings.SHConfig") noexcept; + static void LoadBindings(std::string const& sourceFile = "../../Assets/Bindings.SHConfig") noexcept; /*------------------------------------------------------------------------*/ /* Binding Functions */ -- 2.40.1 From 7dbd0b93b3cff3bb85bb48565e7a85ee68cb0caf Mon Sep 17 00:00:00 2001 From: mushgunAX Date: Sun, 8 Jan 2023 21:39:48 +0800 Subject: [PATCH 3/3] Minor comment fix --- SHADE_Engine/src/Input/SHInputManager.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SHADE_Engine/src/Input/SHInputManager.h b/SHADE_Engine/src/Input/SHInputManager.h index 01ef6c42..30ee00e8 100644 --- a/SHADE_Engine/src/Input/SHInputManager.h +++ b/SHADE_Engine/src/Input/SHInputManager.h @@ -689,7 +689,7 @@ namespace SHADE static void SaveBindings(std::string const& targetFile = "../../Assets/Bindings.SHConfig") noexcept; //Load and register bindings from a file - //The current list of bindings will be overwritten, so save them somewhere else before loading + //If specified file exists, the current list of bindings will be overwritten, so save them somewhere else before loading static void LoadBindings(std::string const& sourceFile = "../../Assets/Bindings.SHConfig") noexcept; /*------------------------------------------------------------------------*/ -- 2.40.1