Part one - removed references and smart pointers
This commit is contained in:
@ -1,161 +1,163 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dawnlibs.hpp"
|
||||
#include "util/mathutils.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class DawnGame;
|
||||
typedef int_fast16_t inputbind_t;
|
||||
|
||||
template<typename T>
|
||||
class IInputManager {
|
||||
protected:
|
||||
std::map<inputbind_t, std::vector<T>> binds;
|
||||
std::map<inputbind_t, float_t> valuesLeft;
|
||||
std::map<inputbind_t, float_t> valuesRight;
|
||||
bool_t currentIsLeft = true;
|
||||
|
||||
/**
|
||||
* Method to be overwritten by the platform, reads a RAW input value from
|
||||
* the pad/input device directly.
|
||||
*
|
||||
* @param axis Axis to get the value of.
|
||||
* @return The current input value (between 0 and 1).
|
||||
*/
|
||||
virtual float_t getInputValue(T axis) = 0;
|
||||
|
||||
public:
|
||||
DawnGame &game;
|
||||
|
||||
IInputManager(DawnGame &game) : game(game) {}
|
||||
|
||||
/**
|
||||
* Binds an axis to a bind.
|
||||
*
|
||||
* @param bind Bind to bind the axis to.
|
||||
* @param axis Axis to use for this bind.
|
||||
*/
|
||||
void bind(inputbind_t bind, T axis) {
|
||||
this->binds[bind].push_back(axis);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unbind a previously bound axis from a bind.
|
||||
*
|
||||
* @param bind Bind to remove all binds from.
|
||||
*/
|
||||
void unbind(inputbind_t bind) {
|
||||
this->binds[bind].clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Unbind all values, all of them.
|
||||
*/
|
||||
void unbindAll() {
|
||||
this->binds.clear();
|
||||
this->values.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current bind value.
|
||||
*
|
||||
* @param bind Bind to get the value of.
|
||||
* @return The current input state (between 0 and 1).
|
||||
*/
|
||||
float_t getValue(inputbind_t bind) {
|
||||
if(this->currentIsLeft) {
|
||||
auto exist = this->valuesLeft.find(bind);
|
||||
return exist == this->valuesLeft.end() ? 0.0f : exist->second;
|
||||
} else {
|
||||
auto exist = this->valuesRight.find(bind);
|
||||
return exist == this->valuesRight.end() ? 0.0f : exist->second;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the bind value from the previous frame.
|
||||
*
|
||||
* @param bind Bind to get the value of.
|
||||
* @return The value of the bind, last frame.
|
||||
*/
|
||||
float_t getValueLastUpdate(inputbind_t bind) {
|
||||
if(this->currentIsLeft) {
|
||||
auto exist = this->valuesRight.find(bind);
|
||||
return exist == this->valuesRight.end() ? 0.0f : exist->second;
|
||||
} else {
|
||||
auto exist = this->valuesLeft.find(bind);
|
||||
return exist == this->valuesLeft.end() ? 0.0f : exist->second;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the given bind is currently being pressed (a non-zero
|
||||
* value).
|
||||
*
|
||||
* @param bind Bind to check if pressed.
|
||||
* @return True if value is non-zero, or false for zero.
|
||||
*/
|
||||
bool_t isDown(inputbind_t bind) {
|
||||
return this->getValue(bind) != 0.0f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true on the first frame an input was pressed (when the state
|
||||
* had changed from 0 to non-zero).
|
||||
*
|
||||
* @param bind Bind to check if pressed.
|
||||
* @return True if down this frame and not down last frame.
|
||||
*/
|
||||
bool_t isPressed(inputbind_t bind) {
|
||||
return this->getValue(bind)!=0 && this->getValueLastUpdate(bind)==0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true on the first frame an input was released (when the state
|
||||
* had changed from non-zero to 0).
|
||||
*
|
||||
* @param bind Bind to check if released.
|
||||
* @return True if up this frame, and down last frame.
|
||||
*/
|
||||
bool_t wasReleased(inputbind_t bind) {
|
||||
return this->getValue(bind)==0 && this->getValueLastUpdate(bind)!=0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal method to update the input state, checks current input raws
|
||||
* and decides what values are set for the inputs.
|
||||
*/
|
||||
void update() {
|
||||
auto it = this->binds.begin();
|
||||
this->currentIsLeft = !this->currentIsLeft;
|
||||
|
||||
// For each bind...
|
||||
while(it != this->binds.end()) {
|
||||
float_t value = 0.0f;
|
||||
|
||||
// For each input axis...
|
||||
auto bindIt = it->second.begin();
|
||||
while(bindIt != it->second.end()) {
|
||||
// Get value and make the new max.
|
||||
float_t inputValue = this->getInputValue(*bindIt);
|
||||
value = mathMax<float_t>(value, inputValue);
|
||||
++bindIt;
|
||||
}
|
||||
|
||||
// Set into current values
|
||||
if(this->currentIsLeft) {
|
||||
this->valuesLeft[it->first] = value;
|
||||
} else {
|
||||
this->valuesRight[it->first] = value;
|
||||
}
|
||||
++it;
|
||||
}
|
||||
|
||||
// TODO: trigger events
|
||||
}
|
||||
};
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dawnlibs.hpp"
|
||||
#include "util/mathutils.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class DawnGame;
|
||||
typedef int_fast16_t inputbind_t;
|
||||
|
||||
template<typename T>
|
||||
class IInputManager {
|
||||
protected:
|
||||
std::map<inputbind_t, std::vector<T>> binds;
|
||||
std::map<inputbind_t, float_t> valuesLeft;
|
||||
std::map<inputbind_t, float_t> valuesRight;
|
||||
bool_t currentIsLeft = true;
|
||||
|
||||
/**
|
||||
* Method to be overwritten by the platform, reads a RAW input value from
|
||||
* the pad/input device directly.
|
||||
*
|
||||
* @param axis Axis to get the value of.
|
||||
* @return The current input value (between 0 and 1).
|
||||
*/
|
||||
virtual float_t getInputValue(T axis) = 0;
|
||||
|
||||
public:
|
||||
DawnGame *game;
|
||||
|
||||
IInputManager(DawnGame *game) {
|
||||
this->game = game;
|
||||
}
|
||||
|
||||
/**
|
||||
* Binds an axis to a bind.
|
||||
*
|
||||
* @param bind Bind to bind the axis to.
|
||||
* @param axis Axis to use for this bind.
|
||||
*/
|
||||
void bind(inputbind_t bind, T axis) {
|
||||
this->binds[bind].push_back(axis);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unbind a previously bound axis from a bind.
|
||||
*
|
||||
* @param bind Bind to remove all binds from.
|
||||
*/
|
||||
void unbind(inputbind_t bind) {
|
||||
this->binds[bind].clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Unbind all values, all of them.
|
||||
*/
|
||||
void unbindAll() {
|
||||
this->binds.clear();
|
||||
this->values.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current bind value.
|
||||
*
|
||||
* @param bind Bind to get the value of.
|
||||
* @return The current input state (between 0 and 1).
|
||||
*/
|
||||
float_t getValue(inputbind_t bind) {
|
||||
if(this->currentIsLeft) {
|
||||
auto exist = this->valuesLeft.find(bind);
|
||||
return exist == this->valuesLeft.end() ? 0.0f : exist->second;
|
||||
} else {
|
||||
auto exist = this->valuesRight.find(bind);
|
||||
return exist == this->valuesRight.end() ? 0.0f : exist->second;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the bind value from the previous frame.
|
||||
*
|
||||
* @param bind Bind to get the value of.
|
||||
* @return The value of the bind, last frame.
|
||||
*/
|
||||
float_t getValueLastUpdate(inputbind_t bind) {
|
||||
if(this->currentIsLeft) {
|
||||
auto exist = this->valuesRight.find(bind);
|
||||
return exist == this->valuesRight.end() ? 0.0f : exist->second;
|
||||
} else {
|
||||
auto exist = this->valuesLeft.find(bind);
|
||||
return exist == this->valuesLeft.end() ? 0.0f : exist->second;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the given bind is currently being pressed (a non-zero
|
||||
* value).
|
||||
*
|
||||
* @param bind Bind to check if pressed.
|
||||
* @return True if value is non-zero, or false for zero.
|
||||
*/
|
||||
bool_t isDown(inputbind_t bind) {
|
||||
return this->getValue(bind) != 0.0f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true on the first frame an input was pressed (when the state
|
||||
* had changed from 0 to non-zero).
|
||||
*
|
||||
* @param bind Bind to check if pressed.
|
||||
* @return True if down this frame and not down last frame.
|
||||
*/
|
||||
bool_t isPressed(inputbind_t bind) {
|
||||
return this->getValue(bind)!=0 && this->getValueLastUpdate(bind)==0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true on the first frame an input was released (when the state
|
||||
* had changed from non-zero to 0).
|
||||
*
|
||||
* @param bind Bind to check if released.
|
||||
* @return True if up this frame, and down last frame.
|
||||
*/
|
||||
bool_t wasReleased(inputbind_t bind) {
|
||||
return this->getValue(bind)==0 && this->getValueLastUpdate(bind)!=0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal method to update the input state, checks current input raws
|
||||
* and decides what values are set for the inputs.
|
||||
*/
|
||||
void update() {
|
||||
auto it = this->binds.begin();
|
||||
this->currentIsLeft = !this->currentIsLeft;
|
||||
|
||||
// For each bind...
|
||||
while(it != this->binds.end()) {
|
||||
float_t value = 0.0f;
|
||||
|
||||
// For each input axis...
|
||||
auto bindIt = it->second.begin();
|
||||
while(bindIt != it->second.end()) {
|
||||
// Get value and make the new max.
|
||||
float_t inputValue = this->getInputValue(*bindIt);
|
||||
value = mathMax<float_t>(value, inputValue);
|
||||
++bindIt;
|
||||
}
|
||||
|
||||
// Set into current values
|
||||
if(this->currentIsLeft) {
|
||||
this->valuesLeft[it->first] = value;
|
||||
} else {
|
||||
this->valuesRight[it->first] = value;
|
||||
}
|
||||
++it;
|
||||
}
|
||||
|
||||
// TODO: trigger events
|
||||
}
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user