Added input class

This commit is contained in:
Kah Wei 2022-10-16 03:25:33 +08:00
parent c257842156
commit 3b3492843f
2 changed files with 427 additions and 0 deletions

View File

@ -0,0 +1,102 @@
/************************************************************************************//*!
\file Input.cxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 16, 2022
\brief Contains the definition of the managed Input static class.
Note: This file is written in C++17/CLI.
Copyright (C) 2022 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
#include "SHpch.h"
#include "Input.hxx"
#include "Utility/Convert.hxx"
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* Properties */
/*---------------------------------------------------------------------------------*/
Vector3 Input::MousePosition::get()
{
int x, y;
SHInputManager::GetMouseWindowPosition(&x, &y);
return Vector3(static_cast<float>(x), static_cast<float>(y), 0.0f);
}
int Input::MouseScrollDelta::get()
{
return SHInputManager::GetMouseWheelVerticalDelta();
}
/*---------------------------------------------------------------------------------*/
/* Usage Functions */
/*---------------------------------------------------------------------------------*/
bool Input::GetKey(KeyCode key)
{
return SHInputManager::GetKey(static_cast<SHInputManager::SH_KEYCODE>(key));
}
bool Input::GetKeyDown(KeyCode key)
{
return SHInputManager::GetKeyDown(static_cast<SHInputManager::SH_KEYCODE>(key));
}
bool Input::GetKeyUp(KeyCode key)
{
return SHInputManager::GetKeyUp(static_cast<SHInputManager::SH_KEYCODE>(key));
}
bool Input::GetMouseButton(MouseCode mouseButton)
{
return SHInputManager::GetKey(static_cast<SHInputManager::SH_KEYCODE>(mouseButton));
}
bool Input::GetMouseButtonDown(MouseCode mouseButton)
{
return SHInputManager::GetKeyDown(static_cast<SHInputManager::SH_KEYCODE>(mouseButton));
}
bool Input::GetMouseButtonUp(MouseCode mouseButton)
{
return SHInputManager::GetKeyUp(static_cast<SHInputManager::SH_KEYCODE>(mouseButton));
}
/*---------------------------------------------------------------------------------*/
/* Cursor Functions */
/*---------------------------------------------------------------------------------*/
void Input::SetMousePosition(Vector2 pos)
{
SHInputManager::SetMouseWindowPosition
(
static_cast<int>(pos.x),
static_cast<int>(pos.y)
);
}
/*---------------------------------------------------------------------------------*/
/* Time Functions */
/*---------------------------------------------------------------------------------*/
double Input::GetKeyHeldTime(KeyCode key)
{
return SHInputManager::GetKeyHeldTime(static_cast<SHInputManager::SH_KEYCODE>(key));
}
double Input::GetKeyReleasedTime(KeyCode key)
{
return SHInputManager::GetKeyReleasedTime(static_cast<SHInputManager::SH_KEYCODE>(key));
}
double Input::GetMouseHeldTime(MouseCode key)
{
return SHInputManager::GetKeyHeldTime(static_cast<SHInputManager::SH_KEYCODE>(key));
}
double Input::GetMouseReleasedTime(MouseCode key)
{
return SHInputManager::GetKeyReleasedTime(static_cast<SHInputManager::SH_KEYCODE>(key));
}
}

View File

@ -0,0 +1,325 @@
/************************************************************************************//*!
\file Input.hxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 16, 2022
\brief Contains the definition of the managed Input static class.
Note: This file is written in C++17/CLI.
Copyright (C) 2022 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
#pragma once
#include "Input/SHInputManager.h"
#include "Math/Vector2.hxx"
#include "Math/Vector3.hxx"
namespace SHADE
{
/// <summary>
/// Static class responsible for providing access to Input-related functionality.
/// </summary>
public ref class Input abstract sealed
{
public:
/*-----------------------------------------------------------------------------*/
/* Type Definitions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Represents the available supported keycodes that can be passed into the
/// key-based Input functions.
/// </summary>
enum class KeyCode : int
{
Space = static_cast<int>(SHInputManager::SH_KEYCODE::SPACE),
//Apostrophe = static_cast<int>(SHInputManager::SH_KEYCODE::APOSTROPHE),
Comma = static_cast<int>(SHInputManager::SH_KEYCODE::OEM_COMMA),
Minus = static_cast<int>(SHInputManager::SH_KEYCODE::OEM_MINUS),
Period = static_cast<int>(SHInputManager::SH_KEYCODE::OEM_PERIOD),
//Slash = static_cast<int>(SHInputManager::SH_KEYCODE::SLASH),
Key0 = static_cast<int>(SHInputManager::SH_KEYCODE::NUMBER_0),
Key1 = static_cast<int>(SHInputManager::SH_KEYCODE::NUMBER_1),
Key2 = static_cast<int>(SHInputManager::SH_KEYCODE::NUMBER_2),
Key3 = static_cast<int>(SHInputManager::SH_KEYCODE::NUMBER_3),
Key4 = static_cast<int>(SHInputManager::SH_KEYCODE::NUMBER_4),
Key5 = static_cast<int>(SHInputManager::SH_KEYCODE::NUMBER_5),
Key6 = static_cast<int>(SHInputManager::SH_KEYCODE::NUMBER_6),
Key7 = static_cast<int>(SHInputManager::SH_KEYCODE::NUMBER_7),
Key8 = static_cast<int>(SHInputManager::SH_KEYCODE::NUMBER_8),
Key9 = static_cast<int>(SHInputManager::SH_KEYCODE::NUMBER_9),
//Semicolon = static_cast<int>(SHInputManager::SH_KEYCODE::SEMICOLON),
//Equal = static_cast<int>(SHInputManager::SH_KEYCODE::EQUAL),
A = static_cast<int>(SHInputManager::SH_KEYCODE::A),
B = static_cast<int>(SHInputManager::SH_KEYCODE::B),
C = static_cast<int>(SHInputManager::SH_KEYCODE::C),
D = static_cast<int>(SHInputManager::SH_KEYCODE::D),
E = static_cast<int>(SHInputManager::SH_KEYCODE::E),
F = static_cast<int>(SHInputManager::SH_KEYCODE::F),
G = static_cast<int>(SHInputManager::SH_KEYCODE::G),
H = static_cast<int>(SHInputManager::SH_KEYCODE::H),
I = static_cast<int>(SHInputManager::SH_KEYCODE::I),
J = static_cast<int>(SHInputManager::SH_KEYCODE::J),
K = static_cast<int>(SHInputManager::SH_KEYCODE::K),
L = static_cast<int>(SHInputManager::SH_KEYCODE::L),
M = static_cast<int>(SHInputManager::SH_KEYCODE::M),
N = static_cast<int>(SHInputManager::SH_KEYCODE::N),
O = static_cast<int>(SHInputManager::SH_KEYCODE::O),
P = static_cast<int>(SHInputManager::SH_KEYCODE::P),
Q = static_cast<int>(SHInputManager::SH_KEYCODE::Q),
R = static_cast<int>(SHInputManager::SH_KEYCODE::R),
S = static_cast<int>(SHInputManager::SH_KEYCODE::S),
T = static_cast<int>(SHInputManager::SH_KEYCODE::T),
U = static_cast<int>(SHInputManager::SH_KEYCODE::U),
V = static_cast<int>(SHInputManager::SH_KEYCODE::V),
W = static_cast<int>(SHInputManager::SH_KEYCODE::W),
X = static_cast<int>(SHInputManager::SH_KEYCODE::X),
Y = static_cast<int>(SHInputManager::SH_KEYCODE::Y),
Z = static_cast<int>(SHInputManager::SH_KEYCODE::Z),
//LeftBracket = static_cast<int>(SHInputManager::SH_KEYCODE::LEFTBRACKET),
//BackSlash = static_cast<int>(SHInputManager::SH_KEYCODE::BACKSLASH),
//RightBracket = static_cast<int>(SHInputManager::SH_KEYCODE::RIGHTBRACKET),
//GraveAccent = static_cast<int>(SHInputManager::SH_KEYCODE::GRAVEACCENT),
//WORLD1 = static_cast<int>(SHInputManager::SH_KEYCODE::WORLD1),
//WORLD2 = static_cast<int>(SHInputManager::SH_KEYCODE::WORLD2),
/* Function keys */
Escape = static_cast<int>(SHInputManager::SH_KEYCODE::ESCAPE),
Enter = static_cast<int>(SHInputManager::SH_KEYCODE::ENTER),
Tab = static_cast<int>(SHInputManager::SH_KEYCODE::TAB),
Backspace = static_cast<int>(SHInputManager::SH_KEYCODE::BACKSPACE),
Insert = static_cast<int>(SHInputManager::SH_KEYCODE::INSERT),
Delete = static_cast<int>(SHInputManager::SH_KEYCODE::DEL),
Right = static_cast<int>(SHInputManager::SH_KEYCODE::RIGHT_ARROW),
Left = static_cast<int>(SHInputManager::SH_KEYCODE::LEFT_ARROW),
Down = static_cast<int>(SHInputManager::SH_KEYCODE::DOWN_ARROW),
Up = static_cast<int>(SHInputManager::SH_KEYCODE::UP_ARROW),
PageUp = static_cast<int>(SHInputManager::SH_KEYCODE::PAGE_UP),
PageDown = static_cast<int>(SHInputManager::SH_KEYCODE::PAGE_DOWN),
Home = static_cast<int>(SHInputManager::SH_KEYCODE::HOME),
End = static_cast<int>(SHInputManager::SH_KEYCODE::END),
CapsLock = static_cast<int>(SHInputManager::SH_KEYCODE::CAPS_LOCK),
ScrollLock = static_cast<int>(SHInputManager::SH_KEYCODE::SCROLL_LOCK),
NumLock = static_cast<int>(SHInputManager::SH_KEYCODE::NUM_LOCK),
PrintScreen = static_cast<int>(SHInputManager::SH_KEYCODE::PRINT_SCREEN),
Pause = static_cast<int>(SHInputManager::SH_KEYCODE::PAUSE),
F1 = static_cast<int>(SHInputManager::SH_KEYCODE::F1),
F2 = static_cast<int>(SHInputManager::SH_KEYCODE::F2),
F3 = static_cast<int>(SHInputManager::SH_KEYCODE::F3),
F4 = static_cast<int>(SHInputManager::SH_KEYCODE::F4),
F5 = static_cast<int>(SHInputManager::SH_KEYCODE::F5),
F6 = static_cast<int>(SHInputManager::SH_KEYCODE::F6),
F7 = static_cast<int>(SHInputManager::SH_KEYCODE::F7),
F8 = static_cast<int>(SHInputManager::SH_KEYCODE::F8),
F9 = static_cast<int>(SHInputManager::SH_KEYCODE::F9),
F10 = static_cast<int>(SHInputManager::SH_KEYCODE::F10),
F11 = static_cast<int>(SHInputManager::SH_KEYCODE::F11),
F12 = static_cast<int>(SHInputManager::SH_KEYCODE::F12),
F13 = static_cast<int>(SHInputManager::SH_KEYCODE::F13),
F14 = static_cast<int>(SHInputManager::SH_KEYCODE::F14),
F15 = static_cast<int>(SHInputManager::SH_KEYCODE::F15),
F16 = static_cast<int>(SHInputManager::SH_KEYCODE::F16),
F17 = static_cast<int>(SHInputManager::SH_KEYCODE::F17),
F18 = static_cast<int>(SHInputManager::SH_KEYCODE::F18),
F19 = static_cast<int>(SHInputManager::SH_KEYCODE::F19),
F20 = static_cast<int>(SHInputManager::SH_KEYCODE::F20),
F21 = static_cast<int>(SHInputManager::SH_KEYCODE::F21),
F22 = static_cast<int>(SHInputManager::SH_KEYCODE::F22),
F23 = static_cast<int>(SHInputManager::SH_KEYCODE::F23),
F24 = static_cast<int>(SHInputManager::SH_KEYCODE::F24),
/* Keypad */
KeyPad0 = static_cast<int>(SHInputManager::SH_KEYCODE::NUMPAD_0),
KeyPad1 = static_cast<int>(SHInputManager::SH_KEYCODE::NUMPAD_1),
KeyPad2 = static_cast<int>(SHInputManager::SH_KEYCODE::NUMPAD_2),
KeyPad3 = static_cast<int>(SHInputManager::SH_KEYCODE::NUMPAD_3),
KeyPad4 = static_cast<int>(SHInputManager::SH_KEYCODE::NUMPAD_4),
KeyPad5 = static_cast<int>(SHInputManager::SH_KEYCODE::NUMPAD_5),
KeyPad6 = static_cast<int>(SHInputManager::SH_KEYCODE::NUMPAD_6),
KeyPad7 = static_cast<int>(SHInputManager::SH_KEYCODE::NUMPAD_7),
KeyPad8 = static_cast<int>(SHInputManager::SH_KEYCODE::NUMPAD_8),
KeyPad9 = static_cast<int>(SHInputManager::SH_KEYCODE::NUMPAD_9),
//KeyPadDecimal = static_cast<int>(SHInputManager::SH_KEYCODE::KPDECIMAL),
//KeyPadDivide = static_cast<int>(SHInputManager::SH_KEYCODE::KPDIVIDE),
//KeyPadMultiply = static_cast<int>(SHInputManager::SH_KEYCODE::KPMULTIPLY),
//KeyPadSubtract = static_cast<int>(SHInputManager::SH_KEYCODE::KPSUBTRACT),
//KeyPadAdd = static_cast<int>(SHInputManager::SH_KEYCODE::KPADD),
//KeyPadEnter = static_cast<int>(SHInputManager::SH_KEYCODE::KPENTER),
//KeyPadEqual = static_cast<int>(SHInputManager::SH_KEYCODE::KEYPAD),
Shift = static_cast<int>(SHInputManager::SH_KEYCODE::SHIFT),
LeftControl = static_cast<int>(SHInputManager::SH_KEYCODE::LEFT_CTRL),
LeftAlt = static_cast<int>(SHInputManager::SH_KEYCODE::LEFT_ALT),
LeftSuper = static_cast<int>(SHInputManager::SH_KEYCODE::LEFT_WINDOWS),
RightShift = static_cast<int>(SHInputManager::SH_KEYCODE::RIGHT_SHIFT),
RightControl = static_cast<int>(SHInputManager::SH_KEYCODE::RIGHT_CTRL),
RightAlt = static_cast<int>(SHInputManager::SH_KEYCODE::RIGHT_ALT),
RightSuper = static_cast<int>(SHInputManager::SH_KEYCODE::RIGHT_WINDOWS),
/* Gamepad */
JoystickA = static_cast<int>(SHInputManager::SH_KEYCODE::GAMEPAD_A),
JoystickB = static_cast<int>(SHInputManager::SH_KEYCODE::GAMEPAD_B),
JoystickX = static_cast<int>(SHInputManager::SH_KEYCODE::GAMEPAD_X),
JoystickY = static_cast<int>(SHInputManager::SH_KEYCODE::GAMEPAD_Y),
JoystickLeftBumper = static_cast<int>(SHInputManager::SH_KEYCODE::GAMEPAD_LEFTSHOULDER),
JoystickRightBumper = static_cast<int>(SHInputManager::SH_KEYCODE::GAMEPAD_RIGHTSHOULDER),
JoystickLeftTrigger = static_cast<int>(SHInputManager::SH_KEYCODE::GAMEPAD_LEFTTRIGGER),
JoystickRightTrigger = static_cast<int>(SHInputManager::SH_KEYCODE::GAMEPAD_RIGHTTRIGGER),
JoystickDPadUp = static_cast<int>(SHInputManager::SH_KEYCODE::GAMEPAD_DPAD_UP),
JoystickDPadDown = static_cast<int>(SHInputManager::SH_KEYCODE::GAMEPAD_DPAD_DOWN),
JoystickDPadLeft = static_cast<int>(SHInputManager::SH_KEYCODE::GAMEPAD_DPAD_LEFT),
JoystickDPadRight = static_cast<int>(SHInputManager::SH_KEYCODE::GAMEPAD_DPAD_RIGHT),
JoystickMenu = static_cast<int>(SHInputManager::SH_KEYCODE::GAMEPAD_MENU),
JoystickView = static_cast<int>(SHInputManager::SH_KEYCODE::GAMEPAD_VIEW),
JoystickLeftStick = static_cast<int>(SHInputManager::SH_KEYCODE::GAMEPAD_LEFT_THUMBSTICK_BUTTON),
JoystickRightStick = static_cast<int>(SHInputManager::SH_KEYCODE::GAMEPAD_RIGHT_THUMBSTICK_BUTTON),
/* Unity Gamepad Mappings */
JoystickButton0 = JoystickA,
JoystickButton1 = JoystickB,
JoystickButton2 = JoystickX,
JoystickButton3 = JoystickY,
JoystickButton4 = JoystickLeftBumper,
JoystickButton5 = JoystickRightBumper,
JoystickButton6 = JoystickView,
JoystickButton7 = JoystickMenu,
JoystickButton8 = JoystickLeftStick,
JoystickButton9 = JoystickRightStick
};
/// <summary>
/// Represents the available supported mouse keycodes that can be passed into the
/// mouse-button-based Input functions.
/// </summary>
enum class MouseCode : int
{
LeftButton = static_cast<int>(SHInputManager::SH_KEYCODE::LMB),
RightButton = static_cast<int>(SHInputManager::SH_KEYCODE::RMB),
MiddleButton = static_cast<int>(SHInputManager::SH_KEYCODE::MMB),
Button3 = static_cast<int>(SHInputManager::SH_KEYCODE::XMB1),
Button4 = static_cast<int>(SHInputManager::SH_KEYCODE::XMB2)
};
/*-----------------------------------------------------------------------------*/
/* Properites */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Mouse position in screen coordinates relative to the top left of the window.
/// This value is a Vector3 for compatibility with functions that have Vector3
/// arguments. The z component of the Vector3 is always 0
/// </summary>
static property Vector3 MousePosition
{
Vector3 get();
}
/// <summary>
/// Amnount of vertical mouse scroll in this frame.
/// </summary>
static property int MouseScrollDelta
{
int get();
}
/*-----------------------------------------------------------------------------*/
/* Usage Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Checks if a specified key is being held down.
/// This will also be true if GetKeyDown() is true.
/// </summary>
/// <param name="key">KeyCode of the key to check.</param>
/// <returns>True while the user holds down the key specified.</returns>
static bool GetKey(KeyCode key);
/// <summary>
/// Checks if a specified key is pressed and was not pressed before.
/// </summary>
/// <param name="key">KeyCode of the key to check.</param>
/// <returns>
/// True during the frame the user starts pressing down the key specified.
/// </returns>
static bool GetKeyDown(KeyCode key);
/// <summary>
/// Checks if a specified key is no longer pressed pressed and was pressed
/// before.
/// </summary>
/// <param name="key">KeyCode of the key to check.</param>
/// <returns>
/// True during the frame the user releases the key identified by name.
/// </returns>
static bool GetKeyUp(KeyCode key);
/// <summary>
/// Checks if a specified mouse button is being held down.
/// This will also be true if GetMouseButtonDown() is true.
/// </summary>
/// <param name="mouseButton">MouseCode of the mouse button to check.</param>
/// <returns>True while the user holds down the mouse button specified.</returns>
static bool GetMouseButton(MouseCode mouseButton);
/// <summary>
/// Checks if a specified mouse button is pressed and was not pressed before.
/// </summary>
/// <param name="mouseButton">MouseCode of the mouse button to check.</param>
/// <returns>
/// True during the frame the user pressed the given mouse button.
/// </returns>
static bool GetMouseButtonDown(MouseCode mouseButton);
/// <summary>
/// Checks if a specified mouse button is no longer pressed and was pressed
/// before.
/// </summary>
/// <param name="mouseButton">MouseCode of the mouse button to check.</param>
/// <returns>
/// True during the frame the user releases the given mouse button.
/// </returns>
static bool GetMouseButtonUp(MouseCode mouseButton);
/*-----------------------------------------------------------------------------*/
/* Cursor Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Sets the position of the mouse cursor relative to the top left corner of the
/// window.
/// </summary>
/// <param name="pos">
/// Position of the mouse in window pixel coordinates to set.
/// </param>
static void SetMousePosition(Vector2 pos);
/*-----------------------------------------------------------------------------*/
/* Timing Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Retrieves the duration that the specified key has been held or was last held
/// for.
/// </summary>
/// <param name="key">The key to check.</param>
/// <returns>Time in seconds that the key was held.</returns>
static double GetKeyHeldTime(KeyCode key);
/// <summary>
/// Retrieves the duration that the specified key has not been held or was last
/// not been held for.
/// </summary>
/// <param name="key">The key to check.</param>
/// <returns>Time in seconds that the key was held.</returns>
static double GetKeyReleasedTime(KeyCode key);
/// <summary>
/// Retrieves the duration that the specified key has been held or was last held
/// for.
/// </summary>
/// <param name="key">The key to check.</param>
/// <returns>Time in seconds that the key was held.</returns>
static double GetMouseHeldTime(MouseCode mouseButton);
/// <summary>
/// Retrieves the duration that the specified key has not been held or was last
/// not been held for.
/// </summary>
/// <param name="key">The key to check.</param>
/// <returns>Time in seconds that the key was held.</returns>
static double GetMouseReleasedTime(MouseCode mouseButton);
};
}