diff --git a/src/input/CMakeLists.txt b/src/input/CMakeLists.txt new file mode 100644 index 00000000..81d68f18 --- /dev/null +++ b/src/input/CMakeLists.txt @@ -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 +) \ No newline at end of file diff --git a/src/input/Input.cpp b/src/input/Input.cpp new file mode 100644 index 00000000..b62e414d --- /dev/null +++ b/src/input/Input.cpp @@ -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) { +} \ No newline at end of file diff --git a/src/input/Input.hpp b/src/input/Input.hpp new file mode 100644 index 00000000..ca43c6e7 --- /dev/null +++ b/src/input/Input.hpp @@ -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 states; + std::vector 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); + }; +} \ No newline at end of file diff --git a/src/input/InputButton.cpp b/src/input/InputButton.cpp new file mode 100644 index 00000000..53996e7f --- /dev/null +++ b/src/input/InputButton.cpp @@ -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("") +{ + +} \ No newline at end of file diff --git a/src/input/InputButton.hpp b/src/input/InputButton.hpp new file mode 100644 index 00000000..fc357e19 --- /dev/null +++ b/src/input/InputButton.hpp @@ -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; + }; +} \ No newline at end of file