qtk/src/qtk/input.h

156 lines
4.8 KiB
C
Raw Normal View History

2021-09-03 16:56:57 +00:00
/*##############################################################################
## Author: Shaun Reed ##
2023-01-02 03:26:58 +00:00
## Legal: All Content (c) 2023 Shaun Reed, all rights reserved ##
2021-09-03 16:56:57 +00:00
## About: Input class from tutorials followed at trentreed.net ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################*/
#ifndef QTOPENGL_INPUT_H
#define QTOPENGL_INPUT_H
#include <QPoint>
#include <Qt>
#include "qtkapi.h"
2021-09-03 16:56:57 +00:00
namespace Qtk {
class QTKAPI Input {
2022-11-26 18:24:38 +00:00
public:
/*************************************************************************
* Typedefs
************************************************************************/
2022-11-26 18:24:38 +00:00
/**
2023-01-02 03:26:58 +00:00
* Possible key states. See UpdateStates for state transitions.
*
* When a key is pressed we enter states Registered->Triggered->Pressed.
* InputTriggered state will be met only once if a key is pressed or held.
* While a key is held down the state is InputPressed.
*
* When a key is released we enter InputUnregistered->InputReleased
* When an active InputInstance for a key has this state it is removed.
2022-11-26 18:24:38 +00:00
*/
2022-11-24 22:26:53 +00:00
enum InputState {
InputInvalid,
2023-01-02 03:26:58 +00:00
InputRegistered, // Initial state. Transitions to InputTriggered
InputUnregistered, // Transition to InputReleased
InputTriggered, // Transition to InputPressed
InputPressed, // State of a key while it is held down.
InputReleased // Released keys are removed from state containers.
2022-11-24 22:26:53 +00:00
};
2022-11-26 18:24:38 +00:00
/*************************************************************************
* Public Methods
************************************************************************/
2023-01-02 03:26:58 +00:00
//
// State updating.
/**
* Update state for all mouse button and key instances.
*/
static void update();
/**
* @param key Key to set InputRegistered state.
*/
static void registerKeyPress(int key);
/**
* @param key Key to set InputReleased state.
*/
static void registerKeyRelease(int key);
/**
* @param button Mouse button to set InputRegistered state.
*/
static void registerMousePress(Qt::MouseButton button);
/**
* @param button Mouse button to set InputReleased state.
*/
static void registerMouseRelease(Qt::MouseButton button);
/**
* Reset input state for all key and mouse buttons.
*/
static void reset();
//
// State Checking.
/**
* @param key Key to check state.
* @return True if the key is in InputTriggered state.
*/
2022-11-24 22:26:53 +00:00
inline static bool keyTriggered(Qt::Key key) {
return keyState(key) == InputTriggered;
}
2021-09-03 16:56:57 +00:00
2023-01-02 03:26:58 +00:00
/**
* @param key Key to check state.
* @return True if the key is in InputPressed state.
*/
2022-11-24 22:26:53 +00:00
inline static bool keyPressed(Qt::Key key) {
return keyState(key) == InputPressed;
}
2023-01-02 03:26:58 +00:00
/**
* @param key Key to check state.
* @return True if the key is in InputReleased state.
*/
2022-11-24 22:26:53 +00:00
inline static bool keyReleased(Qt::Key key) {
return keyState(key) == InputReleased;
}
2023-01-02 03:26:58 +00:00
/**
* @param button Mouse button to check state.
* @return True if the key is in InputTriggered state.
*/
2022-11-24 22:26:53 +00:00
inline static bool buttonTriggered(Qt::MouseButton button) {
return buttonState(button) == InputTriggered;
}
2023-01-02 03:26:58 +00:00
/**
* @param button Mouse button to check state.
* @return True if the key is in InputPressed state.
*/
2022-11-24 22:26:53 +00:00
inline static bool buttonPressed(Qt::MouseButton button) {
return buttonState(button) == InputPressed;
}
2023-01-02 03:26:58 +00:00
/**
* @param button Mouse button to check state.
* @return True if the key is in InputReleased state.
*/
2022-11-24 22:26:53 +00:00
inline static bool buttonReleased(Qt::MouseButton button) {
return buttonState(button) == InputReleased;
}
2023-01-02 03:26:58 +00:00
/**
* @param key The key to check InputState.
* @return The current InputState for the given key.
*/
2022-11-24 22:26:53 +00:00
static InputState keyState(Qt::Key key);
2023-01-02 03:26:58 +00:00
/**
* @param button The mouse button to check InputState.
* @return The current InputState for the mouse button.
*/
2022-11-24 22:26:53 +00:00
static InputState buttonState(Qt::MouseButton button);
2023-01-02 03:26:58 +00:00
/**
* @return QPoint representing the mouse position within the widget.
*/
2022-11-24 22:26:53 +00:00
static QPoint mousePosition();
2023-01-02 03:26:58 +00:00
/**
* @return Delta movement of mouse from previous to current position.
*/
static QPoint mouseDelta();
};
2022-11-24 22:26:53 +00:00
} // namespace Qtk
2021-09-03 16:56:57 +00:00
2022-11-24 22:26:53 +00:00
#endif // QTOPENGL_INPUT_H