Input & Resource Managers
This commit is contained in:
@ -14,13 +14,24 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
#include <InputManager.hpp>
|
||||
#include <IAEngine/Engine.hpp>
|
||||
#include <InputManager.hpp>
|
||||
|
||||
namespace ia::iae
|
||||
{
|
||||
EXTERN SDL_Window *g_windowHandle;
|
||||
|
||||
BOOL InputManager::s_keys[256];
|
||||
BOOL InputManager::s_prevKeys[256];
|
||||
InputKey InputManager::s_axisInputs[4];
|
||||
Vec2 InputManager::s_pointerPosition{};
|
||||
Map<String, Handle> InputManager::s_actionNames;
|
||||
Vector<Vector<InputKey>> InputManager::s_actions;
|
||||
|
||||
VOID InputManager::Initialize()
|
||||
{
|
||||
memset(s_keys, 0, sizeof(s_keys));
|
||||
memset(s_prevKeys, 0, sizeof(s_prevKeys));
|
||||
}
|
||||
|
||||
VOID InputManager::Terminate()
|
||||
@ -29,80 +40,197 @@ namespace ia::iae
|
||||
|
||||
VOID InputManager::OnSDLEvent(IN SDL_Event *event)
|
||||
{
|
||||
memcpy(s_prevKeys, s_keys, sizeof(s_prevKeys));
|
||||
switch (event->type)
|
||||
{
|
||||
case SDL_EVENT_KEY_DOWN:
|
||||
s_keys[event->key.scancode] = true;
|
||||
break;
|
||||
|
||||
case SDL_EVENT_KEY_UP:
|
||||
s_keys[event->key.scancode] = false;
|
||||
break;
|
||||
|
||||
case SDL_EVENT_MOUSE_MOTION:
|
||||
s_pointerPosition = {event->motion.x, event->motion.y};
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Vec2 InputManager::GetAxis()
|
||||
{
|
||||
return Vec2{
|
||||
IsKeyDown(s_axisInputs[1]) + IsKeyDown(s_axisInputs[0]) * -1,
|
||||
IsKeyDown(s_axisInputs[3]) + IsKeyDown(s_axisInputs[2]) * -1
|
||||
};
|
||||
}
|
||||
|
||||
VOID InputManager::SwitchModeToText()
|
||||
{
|
||||
SDL_StartTextInput(g_windowHandle);
|
||||
}
|
||||
|
||||
VOID InputManager::SwitchModeToAction()
|
||||
{
|
||||
SDL_StopTextInput(g_windowHandle);
|
||||
}
|
||||
|
||||
Vec2 InputManager::GetPointerPosition()
|
||||
{
|
||||
return s_pointerPosition;
|
||||
}
|
||||
|
||||
BOOL InputManager::IsKeyDown(IN InputKey key)
|
||||
{
|
||||
return s_keys[(UINT8) key];
|
||||
}
|
||||
|
||||
BOOL InputManager::WasKeyPressed(IN InputKey key)
|
||||
{
|
||||
return !s_prevKeys[(UINT8) key] && s_keys[(UINT8) key];
|
||||
}
|
||||
|
||||
BOOL InputManager::WasKeyReleased(IN InputKey key)
|
||||
{
|
||||
return s_prevKeys[(UINT8) key] && !s_keys[(UINT8) key];
|
||||
}
|
||||
|
||||
BOOL InputManager::IsActionDown(IN Handle action)
|
||||
{
|
||||
const auto& t = s_actions[action];
|
||||
for(const auto& k : t)
|
||||
if(IsKeyDown(k))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
BOOL InputManager::WasActionPressed(IN Handle action)
|
||||
{
|
||||
const auto& t = s_actions[action];
|
||||
for(const auto& k : t)
|
||||
if(WasKeyPressed(k))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
BOOL InputManager::WasActionReleased(IN Handle action)
|
||||
{
|
||||
const auto& t = s_actions[action];
|
||||
for(const auto& k : t)
|
||||
if(WasKeyReleased(k))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
BOOL InputManager::IsActionDown(IN CONST String& action)
|
||||
{
|
||||
return IsActionDown(s_actionNames[action]);
|
||||
}
|
||||
|
||||
BOOL InputManager::WasActionPressed(IN CONST String& action)
|
||||
{
|
||||
return WasActionPressed(s_actionNames[action]);
|
||||
}
|
||||
|
||||
BOOL InputManager::WasActionReleased(IN CONST String& action)
|
||||
{
|
||||
return WasActionReleased(s_actionNames[action]);
|
||||
}
|
||||
|
||||
Handle InputManager::BindAction(IN CONST String &name, IN InputKey key)
|
||||
{
|
||||
s_actions.pushBack({key});
|
||||
const auto handle = s_actions.size() - 1;
|
||||
s_actionNames[name] = handle;
|
||||
return handle;
|
||||
}
|
||||
|
||||
VOID InputManager::BindAxis(IN InputKey upKey, IN InputKey downKey, IN InputKey leftKey, IN InputKey rightKey)
|
||||
{
|
||||
s_axisInputs[0] = upKey;
|
||||
s_axisInputs[1] = downKey;
|
||||
s_axisInputs[2] = leftKey;
|
||||
s_axisInputs[3] = rightKey;
|
||||
}
|
||||
} // namespace ia::iae
|
||||
|
||||
namespace ia::iae
|
||||
{
|
||||
Vec2 Engine::GetInputAxis()
|
||||
{
|
||||
return {};
|
||||
return InputManager::GetAxis();
|
||||
}
|
||||
|
||||
VOID Engine::SwitchInputModeToText()
|
||||
{
|
||||
InputManager::SwitchModeToText();
|
||||
}
|
||||
|
||||
VOID Engine::SwitchInputModeToAction()
|
||||
{
|
||||
InputManager::SwitchModeToAction();
|
||||
}
|
||||
|
||||
Vec2 Engine::GetInputPointerPosition()
|
||||
{
|
||||
return {};
|
||||
return InputManager::GetPointerPosition();
|
||||
}
|
||||
|
||||
BOOL Engine::IsInputKeyDown(IN InputKey key)
|
||||
{
|
||||
return false;
|
||||
return InputManager::IsKeyDown(key);
|
||||
}
|
||||
|
||||
BOOL Engine::WasInputKeyPressed(IN InputKey key)
|
||||
{
|
||||
return false;
|
||||
return InputManager::WasKeyPressed(key);
|
||||
}
|
||||
|
||||
BOOL Engine::WasInputKeyReleased(IN InputKey key)
|
||||
{
|
||||
return false;
|
||||
return InputManager::WasKeyReleased(key);
|
||||
}
|
||||
|
||||
BOOL Engine::IsInputActionDown(IN Handle action)
|
||||
{
|
||||
return false;
|
||||
return InputManager::IsActionDown(action);
|
||||
}
|
||||
|
||||
BOOL Engine::WasInputActionPressed(IN Handle action)
|
||||
{
|
||||
return false;
|
||||
return InputManager::WasActionPressed(action);
|
||||
}
|
||||
|
||||
BOOL Engine::WasInputActionReleased(IN Handle action)
|
||||
{
|
||||
return false;
|
||||
return InputManager::WasActionReleased(action);
|
||||
}
|
||||
|
||||
BOOL Engine::IsInputActionDown(IN PCCHAR action)
|
||||
BOOL Engine::IsInputActionDown(IN CONST String& action)
|
||||
{
|
||||
return false;
|
||||
return InputManager::IsActionDown(action);
|
||||
}
|
||||
|
||||
BOOL Engine::WasInputActionPressed(IN PCCHAR action)
|
||||
BOOL Engine::WasInputActionPressed(IN CONST String& action)
|
||||
{
|
||||
return false;
|
||||
return InputManager::WasActionPressed(action);
|
||||
}
|
||||
|
||||
BOOL Engine::WasInputActionReleased(IN PCCHAR action)
|
||||
BOOL Engine::WasInputActionReleased(IN CONST String& action)
|
||||
{
|
||||
return false;
|
||||
return InputManager::WasActionReleased(action);
|
||||
}
|
||||
|
||||
Handle Engine::BindInputAction(IN CONST String &name, IN InputKey key)
|
||||
{
|
||||
return INVALID_HANDLE;
|
||||
return InputManager::BindAction(name, key);
|
||||
}
|
||||
|
||||
VOID Engine::BindInputAxis(IN InputKey upKey, IN InputKey downKey, IN InputKey leftKey, IN InputKey rightKey)
|
||||
{
|
||||
InputManager::BindAxis(upKey, downKey, leftKey, rightKey);
|
||||
}
|
||||
} // namespace ia::iae
|
||||
Reference in New Issue
Block a user