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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/********************************************************************
*
*	Created:	3/3/2014   16:26
*
*	File:		fsIInputManager.h
*
*	Author:		Mick Waites
*	
*	Purpose:	Generating events from user input and pushing the
*				events to any listeners.
*
*********************************************************************/

#ifndef fsIInputManagerhdr
#define fsIInputManagerhdr

#include <vector>

#include <fsCore/fsPoint.h>
#include <fsCore/src/fsTSingletonBackend.h>

#include "fsIMessageDispatcher.h"
#include "fsINode.h"

class fsCInputPoint;
class fsCEntity;
class fsCInputSession;
class fsCTextEntryComponent;
class fsCTextEntry;

class fsIInputManager : public fsIMessageDispatcher, public fsTSingletonBackend<fsIInputManager>
{
	friend class fsTSingletonBackend<fsIInputManager>;

public:

    void ondDPADClickEvent(fsPoint pPos, fsBool pDown);
	enum
	{
		eKEY_CODE_ENTER = 13,
		eKEY_CODE_BACKSPACE = '\b',
		eKEY_CODE_CURSOR_LEFT = 26,
		eKEY_CODE_CURSOR_RIGHT = 27,
		eKEY_CODE_CURSOR_UP = 28,
		eKEY_CODE_CURSOR_DOWN = 29,

		eKEY_DPAD_LEFT = 30,
		eKEY_DPAD_RIGHT = 31,
		eKEY_DPAD_UP = 32,
		eKEY_DPAD_DOWN = 33,
		eKEY_DPAD_CENTRE = 34,

	};

	static fsBool DPADKey(fsS32 pKeyCode)
	{
		return (eKEY_DPAD_LEFT <= pKeyCode) && (pKeyCode <= eKEY_DPAD_CENTRE);

	}

	enum
	{
		ePOINTER_TYPE_MOUSE = 0,
		ePOINTER_TYPE_SINGLE_TOUCH,
		ePOINTER_TYPE_MULTI_TOUCH
	};

	struct sClonableMessageParameters : public sMessageParameters
	{
		const static fsS32 mPointerMessageTypeId;
		const static fsS32 mKeyboardMessageTypeId;
                const static fsS32 mFrameUpdateMessageTypeId;
                const static fsS32 mDPADMessageTypeId;

		void s32Save(fsStr& pSaveStr, fsS32 pVal);
		fsS32 s32Load(fsStr& pLoadStr);
		void boolSave(fsStr& pSaveStr, fsBool pVal);
		fsBool boolLoad(fsStr& pLoadStr);
		void strSave(fsStr& pSaveStr, const fsStr& pVal);
		fsStr strLoad(fsStr& pLoadStr);
		void pointSave(fsStr& pSaveStr, const fsPoint& pVal);
		fsPoint pointLoad(fsStr& pLoadStr);

		virtual sClonableMessageParameters* clone() const = 0;
		virtual void saveStr(fsStr& pSaveStr) = 0;
		static sClonableMessageParameters* create(fsStr& pSaveStr);
	};

    struct sDPADMsgParam : public sClonableMessageParameters
    {
		sDPADMsgParam(fsPoint pPos, fsS32 pId, fsBool pDown) : mPos(pPos), mId(pId), mDown(pDown) {}

		sDPADMsgParam(fsStr& pLoadStr);

        sClonableMessageParameters* clone() const override;
        void saveStr(fsStr& pSaveStr) override;

        fsS32 mId;
        fsBool mDown;
        fsPoint mPos;
    };

	struct sPointerMsgParam : public sClonableMessageParameters
	{
		sPointerMsgParam() : mPos(0, 0), mIdx(0), mDown(false), mStealableFrom(nullptr)
		{
		}

		sPointerMsgParam(fsStr& pLoadStr);

		sClonableMessageParameters* clone() const override;
		void saveStr(fsStr& pSaveStr) override;

		fsPoint mPos;
		fsS32 mIdx;
		fsBool mDown;
		mutable fsIMessageListener* mStealableFrom;
	};

	struct sKeyboardMsgParam : public sClonableMessageParameters
	{
		sKeyboardMsgParam() : mKey(0), mPressed(true), mAltKeyChanged(false), mShiftKeyChanged(false), mCommandChanged(false), mTime(0) {}
		sKeyboardMsgParam(
			fsS32 pKey,
			fsBool pPressed,
			fsBool pAltKeyDown,
			fsBool pShiftKeyDown,
			fsBool pCommandDown,
			fsS64 pTime) :
			mKey(pKey),
			mPressed(pPressed),
			mAltKeyChanged(pAltKeyDown),
			mShiftKeyChanged(pShiftKeyDown),
			mCommandChanged(pCommandDown),
			mTime(pTime)
		{
		}

		sKeyboardMsgParam(fsStr& pLoadStr);

		sClonableMessageParameters* clone() const override;
		void saveStr(fsStr& pSaveStr) override;

		fsS32 mKey;
		fsBool mPressed;
		fsBool mAltKeyChanged;
		fsBool mShiftKeyChanged;
		fsBool mCommandChanged;
		fsS64 mTime;
	};

	struct sFrameUpdateParam : public sClonableMessageParameters
	{
		sFrameUpdateParam(fsS32 pTime) : mFrameDelta(pTime)
		{
		}

		sFrameUpdateParam(fsStr& pLoadStr);

		sClonableMessageParameters* clone() const override;
		void saveStr(fsStr& pSaveStr) override;

		fsS32 mFrameDelta;
	};

	static const fsCUniqueId mPointerDownStealMsg;
	static const fsCUniqueId mPointerDownMsg;
	static const fsCUniqueId mPointerUpMsg;
	static const fsCUniqueId mPointerMoveMsg;
	static const fsCUniqueId mFrameUpdateMsg;
	static const fsCUniqueId mDPADDownMsg;
	static const fsCUniqueId mDPADUpMsg;

	static const fsCUniqueId mKeyDownMsg;
	static const fsCUniqueId mKeyUpMsg;
	static const fsCUniqueId mTypedCharMsg;



	fsBool keyDecode(fsS32& pKeyCode);
	void keyDownEvent(sKeyboardMsgParam& pKeyboardEvent);
	void keyUpEvent(sKeyboardMsgParam& pKeyboardEvent);


	fsPoint const& mousePosGet() const;
	void mouseEvent(sPointerMsgParam const& pEvent);
	void mouseUpDownEvent(sPointerMsgParam const& pEvent);

	void keyEvent(sKeyboardMsgParam const& pEvent);

	fsS32 frameReady(fsS32 pDeltaMs);

	void messageDispatchLocal(const fsCUniqueId& pMessageType, const sClonableMessageParameters& pParam);

	virtual fsS32 pointerTypeGet() const = 0;
	virtual fsBool hasPhysicalKeyboardGet() const = 0;

	fsS32 maxInputPointsGet() const;

	void update(fsS32 pDeltaMs);

protected:

	static fsIInputManager* backendInstanceCreate();

	void postCreateDo() override { initialise(); }
	void preDestroyDo() override { uninitialise(); }

	virtual fsBool keyDecodeDo(fsS32& pKeyCode) = 0;

	virtual void pollKeys()	{}

	static void altEnterHandle();

	virtual void initialiseDo() = 0;
	virtual void uninitialiseDo() = 0;

	fsIInputManager();
	virtual ~fsIInputManager();<--- Destructor in derived class

	std::vector<fsS32> mPressedKeys;
	std::vector<fsS32> mTypedChars;
	std::vector<sDPADMsgParam> mDPADPresses;
	std::vector<fsCInputPoint*> mInputPoints;

	fsS32 mMaxInputPoints;

	fsBool mAltKeyDown;
	fsBool mShiftKeyDown;
	fsBool mCommandDown;

	std::map<fsS32, fsS64> mKeysDown;


private:

	void playback();

	void pointerEventsProcess();
	void keyboardEventsProcess();

	void initialise();
	void uninitialise();

	fsCInputSession* mInputSession;
	fsPoint mMousePos;

};

#endif