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

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

#include <vector>

#include <GLFW/glfw3.h>

#include <fsCore/impl/bgfx/fsCBgfxWindow.h>

namespace
{
	// Filled by the GLFW char callback, drained once per poll
	std::vector<unsigned int> sTypedChars;
}

void fsCInputManager::charEvent(GLFWwindow* pWindow, unsigned int pCodepoint)
{
	sTypedChars.push_back(pCodepoint);
}

void fsCInputManager::pollKeys()
{
	GLFWwindow* const window = fsNBgfxWindow::windowGet();
	if (!window)
	{
		return;
	}

	glfwPollEvents();

	double x = 0.0;
	double y = 0.0;
	glfwGetCursorPos(window, &x, &y);
	const fsF32 posX = static_cast<fsF32>(x);
	const fsF32 posY = static_cast<fsF32>(y);

	if (posX != mLastPosX || posY != mLastPosY)
	{
		mLastPosX = posX;
		mLastPosY = posY;
		mInputPoints[0]->onMoveEvent(
			fsPoint(posX, posY));
	}

	const fsBool buttonDown = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS;
	if (buttonDown != mMouseButtonDown)
	{
		mMouseButtonDown = buttonDown;
		mInputPoints[0]->onUpDownEvent(
			fsPoint(posX, posY), buttonDown);
	}

	for (const unsigned int typed : sTypedChars)
	{
		mTypedChars.push_back(static_cast<fsS32>(typed));<--- Consider using std::transform algorithm instead of a raw loop.
	}
	sTypedChars.clear();
}

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

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

fsBool fsCInputManager::keyDecodeDo(fsS32& pKeyCode)
{
	return true;
}

void fsCInputManager::keyboardListenerCreate()
{
	if (GLFWwindow* const window = fsNBgfxWindow::windowGet())
	{
		glfwSetCharCallback(window, &fsCInputManager::charEvent);
	}
}

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

	keyboardListenerCreate();
}

void fsCInputManager::uninitialiseDo()
{
	if (GLFWwindow* const window = fsNBgfxWindow::windowGet())
	{
		glfwSetCharCallback(window, nullptr);
	}
}

fsCInputManager::fsCInputManager()
{
}

fsCInputManager::~fsCInputManager()
{
}