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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285

#include "fsCInputManager.h"

#include <base/Director.h>
#include <base/EventDispatcher.h>
#include <base/Utils.h>

#include <fsPlatform/system/fsCSystem.h>
#if defined fsDesktop
	#include <base/EventMouse.h>
#endif

#include <base/Controller.h>
#include <fsCore/fsIHardware.h>
#include <fsCore/src/fsNMaths.h>

#include "../../src/fsCInputPoint.h"


void fsCInputManager::pollKeys()
{
    if (!fsCPlatformSystem::getInstance()->isFireTV())
    {
        return;
    }
    static const auto repeatDelay(0);
	const auto timeMs = axmol::utils::getTimeInMilliseconds();

	std::for_each(std::begin(mKeysDown), std::end(mKeysDown),
	              [&](std::pair<const fsS32, fsS64>& press)
				  {
					  if (timeMs - press.second >= repeatDelay)
					  {
						  if (DPADKey(press.first))
						  {
							  mDPADPresses.push_back(fsIInputManager::sDPADMsgParam(fsPoint(0, 0), press.first,
																					true));
						  }
                          else
						  {
							  mTypedChars.push_back(press.first);
							  press.second = timeMs;
						  }
					  }
				  });
}

fsBool fsCInputManager::hasPhysicalKeyboardGet() const
{
#if (defined(fsDesktop) || defined(fsWeb))
	return true;
#else
	return false;
#endif
}

fsS32 fsCInputManager::pointerTypeGet() const
{
#if (defined(fsDesktop) || defined(fsWeb))
	return ePOINTER_TYPE_MOUSE;
#else
	return ePOINTER_TYPE_MULTI_TOUCH;
#endif
}

#if (defined(fsDesktop) || defined(fsWeb))

fsBool fsCInputManager::mouseButtonDownEventCallback(axmol::EventMouse* pEvent)
{
	axmol::EventMouse* const mouseEvent = static_cast<axmol::EventMouse*>(pEvent);
	if (static_cast<fsS32>(mouseEvent->getMouseButton()) < 2)
	{
		mInputPoints[static_cast<fsS32>(mouseEvent->getMouseButton())]->onUpDownEvent(
			fsPoint(mouseEvent->getLocation().x, mouseEvent->getLocation().y), true);
	}
	return true;
}

fsBool fsCInputManager::mouseButtonUpEventCallback(axmol::EventMouse* pEvent)
{
	axmol::EventMouse* const mouseEvent = static_cast<axmol::EventMouse*>(pEvent);
	if (static_cast<fsS32>(mouseEvent->getMouseButton()) < 2)
	{
		mInputPoints[static_cast<fsS32>(mouseEvent->getMouseButton())]->onUpDownEvent(
			fsPoint(mouseEvent->getLocation().x, mouseEvent->getLocation().y), false);
	}
	return true;
}

fsBool fsCInputManager::mouseMoveEventCallback(axmol::EventMouse* pEvent)
{
	axmol::EventMouse* const mouseEvent = static_cast<axmol::EventMouse*>(pEvent);
	mInputPoints[0]->onMoveEvent(fsPoint(mouseEvent->getLocation().x, mouseEvent->getLocation().y));
	return true;
}

#endif

fsBool fsCInputManager::touchStartEventCallback(axmol::Touch* pTouch, axmol::Event* pEvent)
{
	mInputPoints[pTouch->getID()]->onUpDownEvent(fsPoint(pTouch->getLocation().x, pTouch->getLocation().y), true);
	return true;
}

void fsCInputManager::touchEndEventCallback(axmol::Touch* pTouch, axmol::Event* pEvent)
{
	mInputPoints[pTouch->getID()]->onUpDownEvent(fsPoint(pTouch->getLocation().x, pTouch->getLocation().y), false);
}

void fsCInputManager::touchMoveEventCallback(axmol::Touch* pTouch, axmol::Event* pEvent)
{
	mInputPoints[pTouch->getID()]->onMoveEvent(fsPoint(pTouch->getLocation().x, pTouch->getLocation().y));
}

void fsCInputManager::controllerKeyUpEventCallback(ax::Controller* pController, int pKeyCode, axmol::Event* pEvent)<--- Either there is a missing 'override', or the member function 'fsCInputManager::controllerKeyUpEventCallback' can be static.<--- Either there is a missing 'override', or the member function 'fsCInputManager::controllerKeyUpEventCallback' can be static.
{
}

void fsCInputManager::controllerKeyDownEventCallback(ax::Controller* pController, int pKeyCode, axmol::Event* pEvent)<--- Either there is a missing 'override', or the member function 'fsCInputManager::controllerKeyDownEventCallback' can be static.<--- Either there is a missing 'override', or the member function 'fsCInputManager::controllerKeyDownEventCallback' can be static.
{
}

fsBool fsCInputManager::keyDecodeDo(fsS32& pKeyCode)
{
	if ((pKeyCode >= static_cast<fsS32>(axmol::EventKeyboard::KeyCode::KEY_CAPITAL_A) &&
		pKeyCode <= static_cast<fsS32>(axmol::EventKeyboard::KeyCode::KEY_CAPITAL_Z)))
	{
		pKeyCode = pKeyCode - static_cast<fsS32>(axmol::EventKeyboard::KeyCode::KEY_CAPITAL_A) + 'A';
	}

	if ((pKeyCode >= static_cast<fsS32>(axmol::EventKeyboard::KeyCode::KEY_A) &&
		pKeyCode <= static_cast<fsS32>(axmol::EventKeyboard::KeyCode::KEY_Z)))
	{
		pKeyCode = pKeyCode - static_cast<fsS32>(axmol::EventKeyboard::KeyCode::KEY_A) + (mShiftKeyDown ? 'A' : 'a');
	}
	else if ((pKeyCode >= static_cast<fsS32>(axmol::EventKeyboard::KeyCode::KEY_0) &&
		pKeyCode <= static_cast<fsS32>(axmol::EventKeyboard::KeyCode::KEY_9)))
	{
		pKeyCode = pKeyCode - static_cast<fsS32>(axmol::EventKeyboard::KeyCode::KEY_0) + '0';
	}
	else if (static_cast<fsS32>(axmol::EventKeyboard::KeyCode::KEY_ENTER) == pKeyCode)
	{
		pKeyCode = eKEY_CODE_ENTER;
	}
	else if (static_cast<fsS32>(axmol::EventKeyboard::KeyCode::KEY_BACKSPACE) == pKeyCode)
	{
		pKeyCode = eKEY_CODE_BACKSPACE;
	}
	else if (static_cast<fsS32>(axmol::EventKeyboard::KeyCode::KEY_LEFT_ARROW) <= pKeyCode
		&& static_cast<fsS32>(axmol::EventKeyboard::KeyCode::KEY_DOWN_ARROW) >= pKeyCode)
	{
	}
    else if (static_cast<fsS32>(axmol::EventKeyboard::KeyCode::KEY_DPAD_LEFT) <= pKeyCode &&
            static_cast<fsS32>(axmol::EventKeyboard::KeyCode::KEY_DPAD_CENTER) >= pKeyCode)
    {
			pKeyCode = pKeyCode - (static_cast<fsS32>(axmol::EventKeyboard::KeyCode::KEY_DPAD_LEFT) -
				static_cast<fsS32>(eKEY_DPAD_LEFT));
    }
	else
	{
		return false; // ignore all other keys
	}

	return true;
}

void fsCInputManager::keyDownEventCallback(axmol::EventKeyboard::KeyCode pKeyCode, axmol::Event* pEvent)
{
	sKeyboardMsgParam keyboard_msg(
		static_cast<fsS32>(pKeyCode),
		true,
		pKeyCode == axmol::EventKeyboard::KeyCode::KEY_ALT,
		pKeyCode == axmol::EventKeyboard::KeyCode::KEY_RIGHT_SHIFT ||
			pKeyCode == axmol::EventKeyboard::KeyCode::KEY_LEFT_SHIFT,
		pKeyCode == axmol::EventKeyboard::KeyCode::KEY_HYPER,
		axmol::utils::getTimeInMilliseconds());

	keyDownEvent(keyboard_msg);
}

void fsCInputManager::keyUpEventCallback(axmol::EventKeyboard::KeyCode pKeyCode, axmol::Event* pEvent)
{
	sKeyboardMsgParam keyboard_msg(
		static_cast<fsS32>(pKeyCode),
		false,
		pKeyCode == axmol::EventKeyboard::KeyCode::KEY_ALT,
		pKeyCode == axmol::EventKeyboard::KeyCode::KEY_RIGHT_SHIFT ||
			pKeyCode == axmol::EventKeyboard::KeyCode::KEY_LEFT_SHIFT,
		pKeyCode == axmol::EventKeyboard::KeyCode::KEY_HYPER,
		axmol::utils::getTimeInMilliseconds());
	keyUpEvent(keyboard_msg);
}


#if (defined(fsDesktop) || defined(fsWeb))

void fsCInputManager::mouseListenerCreate()
{
	mMouseListener = axmol::EventListenerMouse::create();
	mMouseListener->onMouseDown = AX_CALLBACK_1(fsCInputManager::mouseButtonDownEventCallback, this);
	mMouseListener->onMouseUp = AX_CALLBACK_1(fsCInputManager::mouseButtonUpEventCallback, this);
	mMouseListener->onMouseMove = AX_CALLBACK_1(fsCInputManager::mouseMoveEventCallback, this);
	axmol::Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(mMouseListener, 1);
}

#endif

void fsCInputManager::keyboardListenerCreate()
{
	mKeyboardListener = axmol::EventListenerKeyboard::create();
	mKeyboardListener->onKeyPressed = AX_CALLBACK_2(fsCInputManager::keyDownEventCallback, this);
	mKeyboardListener->onKeyReleased = AX_CALLBACK_2(fsCInputManager::keyUpEventCallback, this);
	axmol::Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(mKeyboardListener, 1);
}

void fsCInputManager::touchListenerCreate()
{
	mTouchListener = axmol::EventListenerTouchOneByOne::create();
	mTouchListener->onTouchBegan = AX_CALLBACK_2(fsCInputManager::touchStartEventCallback, this);
	mTouchListener->onTouchEnded = AX_CALLBACK_2(fsCInputManager::touchEndEventCallback, this);
	mTouchListener->onTouchMoved = AX_CALLBACK_2(fsCInputManager::touchMoveEventCallback, this);
	axmol::Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(mTouchListener, 1);
}

void fsCInputManager::gamePadListenerCreate()
{
	axmol::Controller::startDiscoveryController();
	// auto controllers = axmol::Controller::getAllController();
	// mGamePadListener = axmol::EventListenerController::create();
    // mGamePadListener->onKeyDown = AX_CALLBACK_3(fsCInputManager::controllerKeyDownEventCallback, this);
    // mGamePadListener->onKeyRepeat = AX_CALLBACK_3(fsCInputManager::controllerKeyDownEventCallback, this);
	// mGamePadListener->onKeyUp = AX_CALLBACK_3(fsCInputManager::controllerKeyUpEventCallback, this);
	// axmol::Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(mGamePadListener, 1);
}


void fsCInputManager::initialiseDo()
{
	mMaxInputPoints = 2;

#if (defined(fsDesktop) || defined(fsWeb))
	mouseListenerCreate();
#endif

	keyboardListenerCreate();
	touchListenerCreate();
//	gamePadListenerCreate();

	// 	if(hasPhysicalKeyboardGet())
	// 	{
	// 		s3eKeyboardRegister( S3E_KEYBOARD_KEY_EVENT, keyUpDownEventCallback, this );
	// 		s3eKeyboardSetInt( S3E_KEYBOARD_GET_CHAR, 1 );
	// 		s3eKeyboardRegister( S3E_KEYBOARD_CHAR_EVENT, typedCharEventCallback, this );
	// 	}
}

void fsCInputManager::uninitialiseDo()
{
	// 	s3ePointerUnRegister( S3E_POINTER_BUTTON_EVENT, mouseButtonEventCallback );
	// 	s3ePointerUnRegister( S3E_POINTER_MOTION_EVENT, mouseMoveEventCallback );
	// 	s3ePointerUnRegister( S3E_POINTER_TOUCH_EVENT, touchUpDownEventCallback );
	// 	s3ePointerUnRegister( S3E_POINTER_TOUCH_MOTION_EVENT, touchMoveEventCallback );
	// 	s3eKeyboardUnRegister( S3E_KEYBOARD_KEY_EVENT, keyUpDownEventCallback );
	// 	s3eKeyboardUnRegister( S3E_KEYBOARD_CHAR_EVENT, typedCharEventCallback );
}

fsCInputManager::fsCInputManager():
#if (defined(fsDesktop) || defined(fsWeb))
	mMouseListener(nullptr),
#endif
	mKeyboardListener(nullptr),
	mTouchListener(nullptr),
    mGamePadListener(nullptr)
{
}

fsCInputManager::~fsCInputManager()
{
#if (defined(fsDesktop) || defined(fsWeb))
	axmol::Director::getInstance()->getEventDispatcher()->removeEventListener(mMouseListener);
#endif
	axmol::Director::getInstance()->getEventDispatcher()->removeEventListener(mKeyboardListener);
    axmol::Director::getInstance()->getEventDispatcher()->removeEventListener(mTouchListener);
//    axmol::Director::getInstance()->getEventDispatcher()->removeEventListener(mGamePadListener);
}