forked from gameprogcpp/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputSystem.h
More file actions
149 lines (130 loc) · 3.93 KB
/
Copy pathInputSystem.h
File metadata and controls
149 lines (130 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------
#pragma once
#include <SDL/SDL_scancode.h>
#include <SDL/SDL_gamecontroller.h>
#include <SDL/SDL_mouse.h>
#include <unordered_map>
#include "Math.h"
// The different button states
enum ButtonState
{
ENone,
EPressed,
EReleased,
EHeld
};
// Helper for keyboard input
class KeyboardState
{
public:
// Friend so InputSystem can easily update it
friend class InputSystem;
// Get just the boolean true/false value of key
bool GetKeyValue(SDL_Scancode keyCode) const;
// Get a state based on current and previous frame
ButtonState GetKeyState(SDL_Scancode keyCode) const;
private:
const Uint8* mCurrState;
Uint8 mPrevState[SDL_NUM_SCANCODES];
};
// Helper for mouse input
class MouseState
{
public:
friend class InputSystem;
// For mouse position
const Vector2& GetPosition() const { return mMousePos; }
const Vector2& GetScrollWheel() const { return mScrollWheel; }
bool IsRelative() const { return mIsRelative; }
// For buttons
bool GetButtonValue(int button) const;
ButtonState GetButtonState(int button) const;
private:
// Store current mouse position
Vector2 mMousePos;
// Motion of scroll wheel
Vector2 mScrollWheel;
// Store button data
Uint32 mCurrButtons;
Uint32 mPrevButtons;
// Are we in relative mouse mode
bool mIsRelative;
};
// Helper for controller input
class ControllerState
{
public:
friend class InputSystem;
// For buttons
bool GetButtonValue(SDL_GameControllerButton button) const;
ButtonState GetButtonState(SDL_GameControllerButton button) const;
const Vector2& GetLeftStick() const { return mLeftStick; }
const Vector2& GetRightStick() const { return mRightStick; }
float GetLeftTrigger() const { return mLeftTrigger; }
float GetRightTrigger() const { return mRightTrigger; }
bool GetIsConnected() const { return mIsConnected; }
private:
// Current/previous buttons
Uint8 mCurrButtons[SDL_CONTROLLER_BUTTON_MAX];
Uint8 mPrevButtons[SDL_CONTROLLER_BUTTON_MAX];
// Left/right sticks
Vector2 mLeftStick;
Vector2 mRightStick;
// Left/right trigger
float mLeftTrigger;
float mRightTrigger;
// Is this controller connected?
bool mIsConnected;
};
class InputDetector;
// Wrapper that contains current state of input
class InputState
{
public:
InputState();
~InputState();
KeyboardState Keyboard;
MouseState Mouse;
ControllerState Controller;
bool GetBoolValue(const std::string& name) const;
ButtonState GetButtonValue(const std::string& name) const;
float GetFloatValue(const std::string& name) const;
Vector2 GetAxisValue(const std::string& name) const;
void ParseBindingFromFile(const std::string& path);
void ParseBindingFromString(const std::string& source);
void AddBinding(const std::string& oneline);
void PutBinding(const std::string& key, InputDetector* detector);
void RemoveBinding(const std::string& key);
bool HasBinding(const std::string& key) const;
void ClearBinding();
private:
const InputState& Self() const;
std::unordered_map<std::string, InputDetector*> bindings;
std::unordered_map<std::string, SDL_GameControllerButton> cButtons;
static size_t split(const std::string &txt, std::vector<std::string> &strs, char ch);
};
class InputSystem
{
public:
bool Initialize(const std::string& bindingFile);
void Shutdown();
// Called right before SDL_PollEvents loop
void PrepareForUpdate();
// Called after SDL_PollEvents loop
void Update();
// Called to process an SDL event in input system
void ProcessEvent(union SDL_Event& event);
const InputState& GetState() const { return mState; }
void SetRelativeMouseMode(bool value);
private:
float Filter1D(int input);
Vector2 Filter2D(int inputX, int inputY);
InputState mState;
SDL_GameController* mController;
};