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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of NVIDIA CORPORATION nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2008-2018 NVIDIA Corporation. All rights reserved.
#ifndef SAMPLE_USER_INPUT_H
#define SAMPLE_USER_INPUT_H
#include <foundation/PxSimpleTypes.h>
#include <vector>
#include "PsString.h"
#if PX_VC
#pragma warning(push)
#pragma warning(disable:4702)
#include <map>
#pragma warning(pop)
#else
#include <map>
#endif
namespace SampleRenderer
{
class Renderer;
}
namespace SampleFramework
{
class InputEventListener;
struct UserInput
{
physx::PxU16 m_Id;
char m_IdName[256]; // this name is used for mapping (enum name)
char m_Name[256]; // this name is used for help
};
struct InputEvent
{
InputEvent(physx::PxU16 id, bool analog = false, float sens = 1.0f)
:m_Id(id), m_Analog(analog), m_Sensitivity(sens)
{
}
InputEvent(const InputEvent& e)
:m_Id(e.m_Id), m_Analog(e.m_Analog), m_Sensitivity(e.m_Sensitivity)
{
}
InputEvent()
: m_Analog(false), m_Sensitivity(1.0f)
{
}
physx::PxU16 m_Id;
bool m_Analog;
float m_Sensitivity;
};
struct InputEventName
{
char m_Name[256];
};
struct SampleInputData
{
char m_InputEventName[256];
char m_UserInputName[256];
};
struct SampleInputMapping
{
physx::PxU16 m_InputEventId;
size_t m_InputEventIndex;
physx::PxU16 m_UserInputId;
size_t m_UserInputIndex;
};
typedef std::vector<SampleInputData> T_SampleInputData;
enum InputType
{
UNDEFINED_INPUT = 0,
KEYBOARD_INPUT = (1 << 0),
GAMEPAD_INPUT = (1 << 1),
TOUCH_BUTTON_INPUT = (1 << 2),
TOUCH_PAD_INPUT = (1 << 3),
MOUSE_INPUT = (1 << 4),
};
enum InputDataReadState
{
STATE_INPUT_EVENT_ID,
STATE_USER_INPUT_ID,
STATE_DIGITAL,
STATE_INPUT_EVENT_NAME,
};
class SampleUserInput
{
public:
// key codes for console and raw key info
enum KeyCode
{
KEY_UNKNOWN = 0,
KEY_A, KEY_B, KEY_C, KEY_D, KEY_E, KEY_F, KEY_G,
KEY_H, KEY_I, KEY_J, KEY_K, KEY_L, KEY_M, KEY_N,
KEY_O, KEY_P, KEY_Q, KEY_R, KEY_S, KEY_T, KEY_U,
KEY_V, KEY_W, KEY_X, KEY_Y, KEY_Z,
KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6,
KEY_7, KEY_8, KEY_9,
KEY_SPACE, KEY_RETURN, KEY_SHIFT, KEY_CONTROL, KEY_ESCAPE, KEY_COMMA,
KEY_NUMPAD0, KEY_NUMPAD1, KEY_NUMPAD2, KEY_NUMPAD3, KEY_NUMPAD4, KEY_NUMPAD5, KEY_NUMPAD6, KEY_NUMPAD7, KEY_NUMPAD8, KEY_NUMPAD9,
KEY_MULTIPLY, KEY_ADD, KEY_SEPARATOR, KEY_SUBTRACT, KEY_DECIMAL, KEY_DIVIDE,
KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6,
KEY_F7, KEY_F8, KEY_F9, KEY_F10, KEY_F11, KEY_F12,
KEY_TAB, KEY_PRIOR, KEY_NEXT,
KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT,
NUM_KEY_CODES,
};
SampleUserInput();
virtual ~SampleUserInput();
void registerUserInput(physx::PxU16 id, const char* idName, const char* name);
virtual const InputEvent* registerInputEvent(const InputEvent& inputEvent, physx::PxU16 userInputId, const char* name);
virtual const InputEvent* registerTouchInputEvent(const InputEvent& inputEvent, physx::PxU16 userInputId, const char* caption, const char* name)
{
PX_UNUSED(inputEvent);
PX_UNUSED(userInputId);
PX_UNUSED(caption);
return NULL;
}
virtual void unregisterInputEvent(physx::PxU16 inputEventId);
virtual void registerInputEvent(const SampleInputMapping& mapping);
virtual bool keyboardSupported() const { return false; }
virtual bool gamepadSupported() const { return false; }
virtual bool mouseSupported() const { return false; }
virtual InputType getInputType(const UserInput& ) const { return UNDEFINED_INPUT; }
void registerInputEventListerner(InputEventListener* listener) { mListener = listener; }
InputEventListener* getInputEventListener() const { return mListener; }
virtual void updateInput();
virtual void shutdown();
virtual void setRenderer(SampleRenderer::Renderer* ) {}
virtual bool getDigitalInputEventState(physx::PxU16 inputEventId ) const = 0;
virtual float getAnalogInputEventState(physx::PxU16 inputEventId ) const = 0;
const std::vector<size_t>* getUserInputs(physx::PxI32 inputEventId) const;
const std::vector<size_t>* getInputEvents(physx::PxU16 userInputId) const;
const std::vector<InputEvent>& getInputEventList() const { return mInputEvents; }
const std::vector<InputEventName>& getInputEventNameList() const { return mInputEventNames; }
const std::vector<UserInput>& getUserInputList() const { return mUserInputs; }
const std::map<physx::PxU16, std::vector<size_t> >& getInputEventUserInputMap() const { return mInputEventUserInputMap; }
physx::PxU16 getUserInputKeys(physx::PxU16 inputEventId, const char* names[], physx::PxU16 maxNames, physx::PxU32 inputTypeMask) const;
physx::PxI32 translateUserInputNameToId(const char* name, size_t& index) const;
physx::PxI32 translateInputEventNameToId(const char* name, size_t& index) const;
const char* translateInputEventIdToName(physx::PxI32 id) const;
const InputEvent* getInputEventSlow(physx::PxU16 inputEventId) const;
protected:
virtual void processGamepads();
std::vector<UserInput> mUserInputs;
std::vector<InputEvent> mInputEvents;
std::vector<InputEventName> mInputEventNames;
private:
InputEventListener* mListener;
std::map<physx::PxU16, std::vector<size_t> > mInputEventUserInputMap;
std::map<physx::PxU16, std::vector<size_t> > mUserInputInputEventMap;
};
class InputEventListener
{
public:
InputEventListener() {}
virtual ~InputEventListener() {}
// special case for text console
virtual void onKeyDownEx(SampleUserInput::KeyCode, physx::PxU32) {}
virtual void onPointerInputEvent(const InputEvent&, physx::PxU32, physx::PxU32, physx::PxReal, physx::PxReal, bool val) {}
virtual void onAnalogInputEvent(const InputEvent& , float val) = 0;
virtual void onDigitalInputEvent(const InputEvent& , bool val) = 0;
};
}
#endif
|