input prpg
This commit is contained in:
11
src/input/CMakeLists.txt
Normal file
11
src/input/CMakeLists.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
# Copyright (c) 2025 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
Input.cpp
|
||||
InputButton.cpp
|
||||
)
|
61
src/input/Input.cpp
Normal file
61
src/input/Input.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
// Copyright (c) 2025 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "Input.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
Input::Input(void) :
|
||||
states({}),
|
||||
buttons({})
|
||||
{
|
||||
}
|
||||
|
||||
float_t Input::getCurrent(const InputBind &bind) const {
|
||||
auto it = states.find(bind);
|
||||
if(it != states.end()) {
|
||||
return it->second.current;
|
||||
}
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
float_t Input::getPrevious(const InputBind &bind) const {
|
||||
auto it = states.find(bind);
|
||||
if(it != states.end()) {
|
||||
return it->second.previous;
|
||||
}
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
float_t Input::getWhen(const InputBind &bind) const {
|
||||
auto it = states.find(bind);
|
||||
if(it != states.end()) {
|
||||
return it->second.when;
|
||||
}
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
bool_t Input::isDown(const InputBind &bind) const {
|
||||
return this->getCurrent(bind) != 0.0f;
|
||||
}
|
||||
|
||||
bool_t Input::isUp(const InputBind &bind) const {
|
||||
return this->getCurrent(bind) == 0.0f;
|
||||
}
|
||||
|
||||
bool_t Input::wasPressed(const InputBind &bind) const {
|
||||
return this->getPrevious(bind) == 0.0f && this->getCurrent(bind) != 0.0f;
|
||||
}
|
||||
|
||||
bool_t Input::wasReleased(const InputBind &bind) const {
|
||||
return this->getPrevious(bind) != 0.0f && this->getCurrent(bind) == 0.0f;
|
||||
}
|
||||
|
||||
void Input::update(void) {
|
||||
|
||||
}
|
||||
|
||||
Input::~Input(void) {
|
||||
}
|
102
src/input/Input.hpp
Normal file
102
src/input/Input.hpp
Normal file
@@ -0,0 +1,102 @@
|
||||
// Copyright (c) 2025 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "InputButton.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
enum class InputBind {
|
||||
UP = "UP",
|
||||
DOWN = "DOWN",
|
||||
LEFT = "LEFT",
|
||||
RIGHT = "RIGHT",
|
||||
ACCEPT = "ACCEPT",
|
||||
CANCEL = "CANCEL"
|
||||
};
|
||||
|
||||
struct InputBindState {
|
||||
float_t current;
|
||||
float_t previous;
|
||||
float_t when;
|
||||
};
|
||||
|
||||
struct Input {
|
||||
private:
|
||||
std::map<InputBind, InputBindState> states;
|
||||
std::vector<InputButton> buttons;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Initializes the input manager.
|
||||
*/
|
||||
Input(void);
|
||||
|
||||
/**
|
||||
* Returns the current state of the input bind.
|
||||
*
|
||||
* @param bind The input bind to check.
|
||||
* @return The current state of the input bind.
|
||||
*/
|
||||
float_t getCurrent(const InputBind &bind) const;
|
||||
|
||||
/**
|
||||
* Returns the previous state of the input bind.
|
||||
*
|
||||
* @param bind The input bind to check.
|
||||
* @return The previous state of the input bind.
|
||||
*/
|
||||
float_t getPrevious(const InputBind &bind) const;
|
||||
|
||||
/**
|
||||
* Returns the time when the input last changed states.
|
||||
*
|
||||
* @param bind The input bind to check.
|
||||
* @return The time when the input last changed states.
|
||||
*/
|
||||
float_t getWhen(const InputBind &bind) const;
|
||||
|
||||
/**
|
||||
* Returns true if the input bind is currently down.
|
||||
*
|
||||
* @param bind The input bind to check.
|
||||
* @return True if the input bind is currently down.
|
||||
*/
|
||||
bool_t isDown(const InputBind &bind) const;
|
||||
|
||||
/**
|
||||
* Returns true if the input bind is currently up.
|
||||
*
|
||||
* @param bind The input bind to check.
|
||||
* @return True if the input bind is currently up.
|
||||
*/
|
||||
bool_t isUp(const InputBind &bind) const;
|
||||
|
||||
/**
|
||||
* Returns true if the input bind was pressed this frame.
|
||||
*
|
||||
* @param bind The input bind to check.
|
||||
* @return True if the input bind was pressed this frame.
|
||||
*/
|
||||
bool_t wasPressed(const InputBind &bind) const;
|
||||
|
||||
/**
|
||||
* Returns true if the input bind was released this frame.
|
||||
*
|
||||
* @param bind The input bind to check.
|
||||
* @return True if the input bind was released this frame.
|
||||
*/
|
||||
bool_t wasReleased(const InputBind &bind) const;
|
||||
|
||||
/**
|
||||
* Updates the input manager.
|
||||
*/
|
||||
void update(void);
|
||||
|
||||
/**
|
||||
* Cleans up the input manager.
|
||||
*/
|
||||
~Input(void);
|
||||
};
|
||||
}
|
20
src/input/InputButton.cpp
Normal file
20
src/input/InputButton.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
// Copyright (c) 2025 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "InputButton.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
InputButton::InputButton(
|
||||
const std::string &name,
|
||||
const InputButtonType buttonType
|
||||
) :
|
||||
name(name),
|
||||
buttonType(buttonType),
|
||||
data(data),
|
||||
command("")
|
||||
{
|
||||
|
||||
}
|
62
src/input/InputButton.hpp
Normal file
62
src/input/InputButton.hpp
Normal file
@@ -0,0 +1,62 @@
|
||||
// Copyright (c) 2025 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dusk.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
enum class InputButtonType {
|
||||
BUTTON,
|
||||
AXIS
|
||||
};
|
||||
|
||||
struct InputButtonData {
|
||||
};
|
||||
|
||||
struct InputButton {
|
||||
private:
|
||||
const std::string name;
|
||||
const InputButtonType buttonType;
|
||||
const InputButtonData data;
|
||||
std::string command;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Constructor for an InputButton. An input button is basically any
|
||||
* physical input on any device, such as a keyboard key, mouse button,
|
||||
* gamepad button, or gamepad axis.
|
||||
*
|
||||
* @param name Human name for the input, used by the console for binding.
|
||||
* @param buttonType The type of input button.
|
||||
* @param data Data used to initialize the button, usually a scancode.
|
||||
*/
|
||||
InputButton(
|
||||
const std::string &name,
|
||||
const InputButtonType buttonType,
|
||||
const InputButtonData data
|
||||
);
|
||||
|
||||
/**
|
||||
* Returns the human name of the input button.
|
||||
*
|
||||
* @return The human name of the input button.
|
||||
*/
|
||||
std::string getName(void) const;
|
||||
|
||||
/**
|
||||
* Returns the type of the input button.
|
||||
*
|
||||
* @return The type of the input button.
|
||||
*/
|
||||
InputButtonType getType(void) const;
|
||||
|
||||
/**
|
||||
* Command executed by the input button when pressed.
|
||||
*
|
||||
* @return The command that is executed when the input button is pressed.
|
||||
*/
|
||||
std::string getCommand(void) const;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user