Merge branch 'main' into PhySH

This commit is contained in:
Diren D Bharwani 2023-01-12 20:12:26 +08:00
commit eda1147b5c
4 changed files with 196 additions and 1 deletions

1
Assets/Bindings.SHConfig Normal file
View File

@ -0,0 +1 @@
0

View File

@ -23,7 +23,7 @@
#include "SH_API.h"
#include "Events/SHEventManager.hpp"
#include <tuple>
#include <cassert>
namespace SHADE
@ -151,6 +151,32 @@ namespace SHADE
return (componentSet.GetSparseSet<T>()->GetElement_s(EntityHandleGenerator::GetIndex(entityID)));
}
/*!*************************************************************************
* \brief
* Gets the Component of the entity with the specified entityID
*
* This is the safe version of GetComponent_s which does a HasComponent to make
* sure that the entity has such a component and returns nullptr if it doesn't
*
* This safe version also checks if the sparse set of this component type
* has been created in SHComponentManager and creates one if it doesn't
*
* @tparam T...
* Pack of Types for all the Components to get.
* \param entityID
* EntityID of the entity that we are trying to get the component of.
* \return
* A tuple of pointers to all the components specified.
* Returns nullptr if the entity does not contain such a component.
***************************************************************************/
template<typename ...T>
static std::enable_if_t<(... && std::is_base_of_v<SHComponent, T>), std::tuple<T*...>> GetComponents(EntityID entityID) noexcept
{
return std::make_tuple<T*...>(GetComponent_s<T>(entityID)...);
}
/*!*************************************************************************
* \brief
* Gets the Component of the entity with the specified entityID

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

@ -14,6 +14,7 @@
#include <map>
#include <set>
#include "../../SHADE_Managed/src/SHpch.h"
#include "../../SHADE_Engine/src/Assets/SHAssetMacros.h"
#include "SH_API.h"
#pragma comment(lib, "xinput.lib")
@ -681,6 +682,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 = std::string(ASSET_ROOT) + "/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 = std::string(ASSET_ROOT) + "/Bindings.SHConfig") noexcept;
/*------------------------------------------------------------------------*/
/* Binding Functions */
/*------------------------------------------------------------------------*/