Implemented Input Binding Serialisation #304

Merged
mushgunAX merged 4 commits from SP3-10-input-management into main 2023-01-09 16:56:59 +08:00
3 changed files with 232 additions and 0 deletions
Showing only changes of commit baaeb9ee10 - Show all commits

52
Assets/Bindings.SHConfig Normal file
View File

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

View File

@ -11,6 +11,7 @@
#pragma once #pragma once
#include <SHpch.h> #include <SHpch.h>
#include <fstream>
#include "SHInputManager.h" #include "SHInputManager.h"
#include "../Tools/SHException.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<int>(lbd.bindingType) << std::endl;
file << static_cast<int>(lbd.inverted) << std::endl;
file << lbd.gravity << std::endl;
file << lbd.dead << std::endl;
file << lbd.sensitivity << std::endl;
file << static_cast<int>(lbd.snap) << std::endl;
//Bindings
file << lbd.positiveKeyCodes.size() << std::endl;
for (auto kc : lbd.positiveKeyCodes)
file << static_cast<int>(kc) << std::endl;
file << lbd.negativeKeyCodes.size() << std::endl;
for (auto kc : lbd.negativeKeyCodes)
file << static_cast<int>(kc) << std::endl;
file << lbd.positiveControllerCodes.size() << std::endl;
for (auto cc : lbd.positiveControllerCodes)
file << static_cast<int>(cc) << std::endl;
file << lbd.negativeControllerCodes.size() << std::endl;
for (auto cc : lbd.negativeControllerCodes)
file << static_cast<int>(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<SH_BINDINGTYPE>(std::stoi(read)));
//Inversion
std::getline(file, read);
SHLOGV_CRITICAL("Inversion: {}", read);
SetBindingInverted(bindingName, static_cast<bool>(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<bool>(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<SH_KEYCODE>(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<SH_KEYCODE>(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<SH_CONTROLLERCODE>(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<SH_CONTROLLERCODE>(std::stoi(read)));
}
}
file.close();
}
void SHInputManager::UpdateInput(double dt) noexcept void SHInputManager::UpdateInput(double dt) noexcept
{ {
//Keyboard and Mouse Buttons//////////////////////////////////////////////// //Keyboard and Mouse Buttons////////////////////////////////////////////////

View File

@ -681,6 +681,17 @@ namespace SHADE
return controllersReleasedTime[controllerNum][static_cast<size_t>(code)]; return controllersReleasedTime[controllerNum][static_cast<size_t>(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 */ /* Binding Functions */
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/