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 219 additions and 0 deletions

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
#include <SHpch.h>
#include <fstream>
#include "SHInputManager.h"
#include "../Tools/SHException.h"
@ -99,6 +100,161 @@ namespace SHADE
}
}
//The Binding File format presently goes as such:
/*
* Binding count
* (For each binding:)
* 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(targetFile);
//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(sourceFile);
//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);
bindingName = read;
AddBinding(bindingName);
//Type
std::getline(file, read);
SetBindingType(bindingName, static_cast<SH_BINDINGTYPE>(std::stoi(read)));
//Inversion
std::getline(file, read);
SetBindingInverted(bindingName, static_cast<bool>(std::stoi(read)));
//Gravity
std::getline(file, read);
SetBindingGravity(bindingName, std::stod(read));
//Dead
std::getline(file, read);
SetBindingDead(bindingName, std::stod(read));
//Sensitivity
std::getline(file, read);
SetBindingSensitivity(bindingName, std::stod(read));
//Snap
std::getline(file, read);
SetBindingSnap(bindingName, static_cast<bool>(std::stoi(read)));
int count = 0;
//Positive Key Codes
std::getline(file, read);
count = std::stoi(read);
for (int i = 0; i < count; ++i)
{
std::getline(file, read);
AddBindingPositiveKeyCode(bindingName, static_cast<SH_KEYCODE>(std::stoi(read)));
}
//Negative Key Codes
std::getline(file, read);
count = std::stoi(read);
for (int i = 0; i < count; ++i)
{
std::getline(file, read);
AddBindingNegativeKeyCode(bindingName, static_cast<SH_KEYCODE>(std::stoi(read)));
}
//Positive Controller Codes
std::getline(file, read);
count = std::stoi(read);
for (int i = 0; i < count; ++i)
{
std::getline(file, read);
AddBindingPositiveControllerCode(bindingName, static_cast<SH_CONTROLLERCODE>(std::stoi(read)));
}
//Negative Controller Codes
std::getline(file, read);
count = std::stoi(read);
for (int i = 0; i < count; ++i)
{
std::getline(file, read);
AddBindingNegativeControllerCode(bindingName, static_cast<SH_CONTROLLERCODE>(std::stoi(read)));
}
}
file.close();
}
void SHInputManager::UpdateInput(double dt) noexcept
{
//Keyboard and Mouse Buttons////////////////////////////////////////////////

View File

@ -681,6 +681,17 @@ namespace SHADE
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/Bindings.SHConfig") noexcept;
//Load and register bindings from a file
//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;
/*------------------------------------------------------------------------*/
/* Binding Functions */
/*------------------------------------------------------------------------*/