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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
|
#if defined(WITH_GLFW)
#include "GlfwInputManager.h"
#include "../Input/IInputEventHandler.h"
#include "../Input/JoyMapping.h"
#include "../Application.h"
#include <cstring> // for memset() and memcpy()
#include <cmath> // for fabsf()
#include <Utf8.h>
#if defined(WITH_IMGUI)
# include "ImGuiGlfwInput.h"
#endif
#define GLFW_VERSION_COMBINED (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 + GLFW_VERSION_REVISION)
namespace nCine
{
const std::int32_t IInputManager::MaxNumJoysticks = GLFW_JOYSTICK_LAST - GLFW_JOYSTICK_1 + 1;
}
namespace nCine::Backends
{
bool GlfwInputManager::windowHasFocus_ = true;
GlfwMouseState GlfwInputManager::mouseState_;
MouseEvent GlfwInputManager::mouseEvent_;
GlfwScrollEvent GlfwInputManager::scrollEvent_;
GlfwKeyboardState GlfwInputManager::keyboardState_;
KeyboardEvent GlfwInputManager::keyboardEvent_;
TextInputEvent GlfwInputManager::textInputEvent_;
GlfwJoystickState GlfwInputManager::nullJoystickState_;
SmallVector<GlfwJoystickState, GlfwInputManager::MaxNumJoysticks> GlfwInputManager::joystickStates_(GlfwInputManager::MaxNumJoysticks);
JoyButtonEvent GlfwInputManager::joyButtonEvent_;
JoyHatEvent GlfwInputManager::joyHatEvent_;
JoyAxisEvent GlfwInputManager::joyAxisEvent_;
JoyConnectionEvent GlfwInputManager::joyConnectionEvent_;
const float GlfwInputManager::JoystickEventsSimulator::AxisEventTolerance = 0.001f;
GlfwInputManager::JoystickEventsSimulator GlfwInputManager::joyEventsSimulator_;
int GlfwInputManager::preScalingWidth_ = 0;
int GlfwInputManager::preScalingHeight_ = 0;
unsigned long int GlfwInputManager::lastFrameWindowSizeChanged_ = 0;
namespace
{
MouseButton glfwToNcineMouseButton(int button)
{
switch (button) {
case GLFW_MOUSE_BUTTON_LEFT: return MouseButton::Left;
case GLFW_MOUSE_BUTTON_RIGHT: return MouseButton::Right;
case GLFW_MOUSE_BUTTON_MIDDLE: return MouseButton::Middle;
case GLFW_MOUSE_BUTTON_4: return MouseButton::Fourth;
case GLFW_MOUSE_BUTTON_5: return MouseButton::Fifth;
default: return MouseButton::Left;
}
}
int ncineToGlfwMouseButton(MouseButton button)
{
switch (button) {
case MouseButton::Left: return GLFW_MOUSE_BUTTON_LEFT;
case MouseButton::Right: return GLFW_MOUSE_BUTTON_RIGHT;
case MouseButton::Middle: return GLFW_MOUSE_BUTTON_MIDDLE;
case MouseButton::Fourth: return GLFW_MOUSE_BUTTON_4;
case MouseButton::Fifth: return GLFW_MOUSE_BUTTON_5;
default: return GLFW_MOUSE_BUTTON_LEFT;
}
}
}
GlfwMouseState::GlfwMouseState()
{
}
bool GlfwMouseState::isButtonDown(MouseButton button) const
{
const int glfwButton = ncineToGlfwMouseButton(button);
return glfwGetMouseButton(GlfwGfxDevice::windowHandle(), glfwButton) == GLFW_PRESS;
}
GlfwInputManager::GlfwInputManager()
{
GlfwGfxDevice& gfxDevice = static_cast<GlfwGfxDevice&>(theApplication().GetGfxDevice());
preScalingWidth_ = gfxDevice.width_;
preScalingHeight_ = gfxDevice.height_;
glfwSetMonitorCallback(monitorCallback);
glfwSetWindowCloseCallback(GlfwGfxDevice::windowHandle(), windowCloseCallback);
#if GLFW_VERSION_COMBINED >= 3300
glfwSetWindowContentScaleCallback(GlfwGfxDevice::windowHandle(), windowContentScaleCallback);
#endif
glfwSetWindowSizeCallback(GlfwGfxDevice::windowHandle(), windowSizeCallback);
glfwSetFramebufferSizeCallback(GlfwGfxDevice::windowHandle(), framebufferSizeCallback);
glfwSetKeyCallback(GlfwGfxDevice::windowHandle(), keyCallback);
glfwSetCharCallback(GlfwGfxDevice::windowHandle(), charCallback);
glfwSetCursorPosCallback(GlfwGfxDevice::windowHandle(), cursorPosCallback);
glfwSetMouseButtonCallback(GlfwGfxDevice::windowHandle(), mouseButtonCallback);
glfwSetScrollCallback(GlfwGfxDevice::windowHandle(), scrollCallback);
glfwSetJoystickCallback(joystickCallback);
#if defined(DEATH_TRACE) && !defined(DEATH_TARGET_EMSCRIPTEN)
for (std::int32_t i = GLFW_JOYSTICK_1; i <= GLFW_JOYSTICK_LAST; i++) {
if (glfwJoystickPresent(i)) {
const int joyId = i - GLFW_JOYSTICK_1;
int numButtons = -1;
int numAxes = -1;
int numHats = -1;
glfwGetJoystickButtons(i, &numButtons);
glfwGetJoystickAxes(i, &numAxes);
# if GLFW_VERSION_COMBINED >= 3300
glfwGetJoystickHats(i, &numHats);
# else
numHats = 0;
# endif
if (numButtons <= 0 && numAxes <= 0 && numHats <= 0) {
LOGI("Gamepad {} has been connected, but reports no axes/buttons/hats - skipping", joyId);
continue;
}
# if GLFW_VERSION_COMBINED >= 3300
// It seems `glfwGetJoystickGUID` can cause crash if the gamepad is quickly disconnected
const char* guid = glfwGetJoystickGUID(i);
# else
const char* guid = "default";
# endif
LOGI("Gamepad {} \"{}\" [{}] has been connected - {} axes, {} buttons, {} hats",
joyId, glfwGetJoystickName(i), guid, numAxes, numButtons, numHats);
}
}
#endif
joyMapping_.Init(this);
#if defined(DEATH_TARGET_EMSCRIPTEN)
emscripten_set_touchstart_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, this, false, GlfwInputManager::emscriptenHandleTouch);
emscripten_set_touchend_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, this, false, GlfwInputManager::emscriptenHandleTouch);
emscripten_set_touchmove_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, this, false, GlfwInputManager::emscriptenHandleTouch);
emscripten_set_touchcancel_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, this, false, GlfwInputManager::emscriptenHandleTouch);
#endif
#if defined(WITH_IMGUI)
ImGuiGlfwInput::init(GlfwGfxDevice::windowHandle(), true);
#endif
}
GlfwInputManager::~GlfwInputManager()
{
#if defined(WITH_IMGUI)
ImGuiGlfwInput::shutdown();
#endif
}
bool GlfwJoystickState::isButtonPressed(int buttonId) const
{
return (buttonId >= 0 && buttonId < numButtons_ && buttons_[buttonId] != GLFW_RELEASE);
}
unsigned char GlfwJoystickState::hatState(int hatId) const
{
return (hatId >= 0 && hatId < numHats_ ? hats_[hatId] : HatState::Centered);
}
float GlfwJoystickState::axisValue(int axisId) const
{
return (axisId >= 0 && axisId < numAxes_ ? axesValues_[axisId] : 0.0f);
}
bool GlfwInputManager::hasFocus()
{
const bool glfwFocused = (glfwGetWindowAttrib(GlfwGfxDevice::windowHandle(), GLFW_FOCUSED) != 0);
// A focus event has occurred (either gain or loss)
if (windowHasFocus_ != glfwFocused) {
windowHasFocus_ = glfwFocused;
}
return windowHasFocus_;
}
void GlfwInputManager::updateJoystickStates()
{
for (unsigned int joyId = 0; joyId < MaxNumJoysticks; joyId++) {
if (glfwJoystickPresent(GLFW_JOYSTICK_1 + joyId)) {
joystickStates_[joyId].buttons_ = glfwGetJoystickButtons(joyId, &joystickStates_[joyId].numButtons_);
#if GLFW_VERSION_COMBINED >= 3300
joystickStates_[joyId].hats_ = glfwGetJoystickHats(joyId, &joystickStates_[joyId].numHats_);
#else
joystickStates_[joyId].hats_ = 0;
#endif
joystickStates_[joyId].axesValues_ = glfwGetJoystickAxes(joyId, &joystickStates_[joyId].numAxes_);
joyEventsSimulator_.simulateButtonsEvents(joyId, joystickStates_[joyId].numButtons_, joystickStates_[joyId].buttons_);
joyEventsSimulator_.simulateHatsEvents(joyId, joystickStates_[joyId].numHats_, joystickStates_[joyId].hats_);
joyEventsSimulator_.simulateAxesEvents(joyId, joystickStates_[joyId].numAxes_, joystickStates_[joyId].axesValues_);
}
}
}
String GlfwInputManager::getClipboardText() const
{
return glfwGetClipboardString(GlfwGfxDevice::windowHandle());
}
bool GlfwInputManager::setClipboardText(StringView text)
{
glfwSetClipboardString(GlfwGfxDevice::windowHandle(), String::nullTerminatedView(text).data());
return true;
}
bool GlfwInputManager::isJoyPresent(int joyId) const
{
DEATH_ASSERT(joyId >= 0);
return (GLFW_JOYSTICK_1 + joyId <= GLFW_JOYSTICK_LAST && glfwJoystickPresent(GLFW_JOYSTICK_1 + joyId) != 0);
}
const char* GlfwInputManager::joyName(int joyId) const
{
return (isJoyPresent(joyId) ? glfwGetJoystickName(joyId) : nullptr);
}
const JoystickGuid GlfwInputManager::joyGuid(int joyId) const
{
#if defined(DEATH_TARGET_EMSCRIPTEN)
return JoystickGuidType::Default;
#elif GLFW_VERSION_COMBINED >= 3300
if (isJoyPresent(joyId)) {
static const char XinputPrefix[] = "78696e707574";
const char* guid = glfwGetJoystickGUID(joyId);
if (strncmp(guid, XinputPrefix, sizeof(XinputPrefix) - 1) == 0) {
return JoystickGuidType::Xinput;
} else {
return StringView(guid);
}
} else {
return JoystickGuidType::Unknown;
}
#else
return JoystickGuidType::Unknown;
#endif
}
int GlfwInputManager::joyNumButtons(int joyId) const
{
int numButtons = -1;
if (isJoyPresent(joyId)) {
glfwGetJoystickButtons(GLFW_JOYSTICK_1 + joyId, &numButtons);
}
return numButtons;
}
int GlfwInputManager::joyNumHats(int joyId) const
{
int numHats = -1;
if (isJoyPresent(joyId)) {
#if GLFW_VERSION_COMBINED >= 3300
glfwGetJoystickHats(GLFW_JOYSTICK_1 + joyId, &numHats);
#else
numHats = 0;
#endif
}
return numHats;
}
int GlfwInputManager::joyNumAxes(int joyId) const
{
int numAxes = -1;
if (isJoyPresent(joyId)) {
glfwGetJoystickAxes(GLFW_JOYSTICK_1 + joyId, &numAxes);
}
return numAxes;
}
const JoystickState& GlfwInputManager::joystickState(int joyId) const
{
return (isJoyPresent(joyId) ? joystickStates_[joyId] : nullJoystickState_);
}
bool GlfwInputManager::joystickRumble(int joyId, float lowFreqIntensity, float highFreqIntensity, uint32_t durationMs)
{
// TODO
return false;
}
bool GlfwInputManager::joystickRumbleTriggers(int joyId, float left, float right, uint32_t durationMs)
{
// TODO
return false;
}
void GlfwInputManager::setCursor(Cursor cursor)
{
if (cursor != cursor_) {
switch (cursor) {
case Cursor::Arrow: glfwSetInputMode(GlfwGfxDevice::windowHandle(), GLFW_CURSOR, GLFW_CURSOR_NORMAL); break;
case Cursor::Hidden: glfwSetInputMode(GlfwGfxDevice::windowHandle(), GLFW_CURSOR, GLFW_CURSOR_HIDDEN); break;
case Cursor::HiddenLocked: glfwSetInputMode(GlfwGfxDevice::windowHandle(), GLFW_CURSOR, GLFW_CURSOR_DISABLED); break;
}
#if GLFW_VERSION_COMBINED >= 3300 && !defined(DEATH_TARGET_EMSCRIPTEN)
// Enable raw mouse motion (if supported) when disabling the cursor
const bool enableRawMouseMotion = (cursor == Cursor::HiddenLocked && glfwRawMouseMotionSupported() == GLFW_TRUE);
glfwSetInputMode(GlfwGfxDevice::windowHandle(), GLFW_RAW_MOUSE_MOTION, enableRawMouseMotion ? GLFW_TRUE : GLFW_FALSE);
#endif
// Handling ImGui cursor changes
IInputManager::setCursor(cursor);
cursor_ = cursor;
}
}
void GlfwInputManager::monitorCallback(GLFWmonitor* monitor, int event)
{
GlfwGfxDevice& gfxDevice = static_cast<GlfwGfxDevice&>(theApplication().GetGfxDevice());
gfxDevice.updateMonitors();
}
void GlfwInputManager::windowCloseCallback(GLFWwindow* window)
{
bool shouldQuit = true;
if (inputEventHandler_ != nullptr) {
shouldQuit = inputEventHandler_->OnQuitRequest();
}
if (shouldQuit) {
theApplication().Quit();
} else {
glfwSetWindowShouldClose(window, GLFW_FALSE);
}
}
void GlfwInputManager::windowContentScaleCallback(GLFWwindow* window, float xscale, float yscale)
{
GlfwGfxDevice& gfxDevice = static_cast<GlfwGfxDevice&>(theApplication().GetGfxDevice());
// Revert the window size change if it happened the same frame its scale also changed
if (lastFrameWindowSizeChanged_ == theApplication().GetFrameCount()) {
gfxDevice.width_ = preScalingWidth_;
gfxDevice.height_ = preScalingHeight_;
}
gfxDevice.updateMonitorScaling(gfxDevice.windowMonitorIndex());
}
void GlfwInputManager::windowSizeCallback(GLFWwindow* window, int width, int height)
{
GlfwGfxDevice& gfxDevice = static_cast<GlfwGfxDevice&>(theApplication().GetGfxDevice());
// Save previous resolution for if a content scale event is coming just after a resize
preScalingWidth_ = gfxDevice.width_;
preScalingHeight_ = gfxDevice.height_;
lastFrameWindowSizeChanged_ = theApplication().GetFrameCount();
gfxDevice.width_ = width;
gfxDevice.height_ = height;
bool isFullscreen = (glfwGetWindowMonitor(window) != nullptr);
if (!isFullscreen) {
gfxDevice.lastWindowWidth_ = width;
gfxDevice.lastWindowHeight_ = height;
}
}
void GlfwInputManager::framebufferSizeCallback(GLFWwindow* window, int width, int height)
{
GlfwGfxDevice& gfxDevice = static_cast<GlfwGfxDevice&>(theApplication().GetGfxDevice());
gfxDevice.drawableWidth_ = width;
gfxDevice.drawableHeight_ = height;
theApplication().ResizeScreenViewport(width, height);
}
void GlfwInputManager::keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (inputEventHandler_ == nullptr) {
return;
}
keyboardEvent_.scancode = scancode;
keyboardEvent_.sym = GlfwKeys::keySymValueToEnum(key);
keyboardEvent_.mod = GlfwKeys::keyModMaskToEnumMask(mods);
if (action == GLFW_PRESS) {
inputEventHandler_->OnKeyPressed(keyboardEvent_);
} else if (action == GLFW_RELEASE) {
inputEventHandler_->OnKeyReleased(keyboardEvent_);
}
}
void GlfwInputManager::charCallback(GLFWwindow* window, unsigned int c)
{
if (inputEventHandler_ == nullptr) {
return;
}
// Current GLFW version does not return an UTF-8 string (https://github.com/glfw/glfw/issues/837)
textInputEvent_.length = (std::int32_t)Utf8::FromCodePoint(c, textInputEvent_.text);
if (textInputEvent_.length > 0) {
inputEventHandler_->OnTextInput(textInputEvent_);
}
}
void GlfwInputManager::cursorPosCallback(GLFWwindow* window, double x, double y)
{
if (inputEventHandler_ == nullptr) {
return;
}
mouseState_.x = static_cast<int>(x);
mouseState_.y = static_cast<int>(y);
inputEventHandler_->OnMouseMove(mouseState_);
}
void GlfwInputManager::mouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
{
if (inputEventHandler_ == nullptr) {
return;
}
double xCursor, yCursor;
glfwGetCursorPos(window, &xCursor, &yCursor);
mouseEvent_.x = static_cast<int>(xCursor);
mouseEvent_.y = static_cast<int>(yCursor);
mouseEvent_.button = glfwToNcineMouseButton(button);
if (action == GLFW_PRESS) {
inputEventHandler_->OnMouseDown(mouseEvent_);
} else if (action == GLFW_RELEASE) {
inputEventHandler_->OnMouseUp(mouseEvent_);
}
}
void GlfwInputManager::scrollCallback(GLFWwindow* window, double xoffset, double yoffset)
{
if (inputEventHandler_ == nullptr) {
return;
}
scrollEvent_.x = static_cast<float>(xoffset);
scrollEvent_.y = static_cast<float>(yoffset);
inputEventHandler_->OnMouseWheel(scrollEvent_);
}
void GlfwInputManager::joystickCallback(int joy, int event)
{
const int joyId = joy - GLFW_JOYSTICK_1;
joyConnectionEvent_.joyId = joyId;
if (event == GLFW_CONNECTED) {
#if defined(DEATH_TARGET_EMSCRIPTEN) && defined(EMSCRIPTEN_USE_PORT_CONTRIB_GLFW3)
// `contrib.glfw3` is polling gamepads asynchronously, number of buttons/axes is usually 0 here, so skip these checks instead
# if defined(DEATH_TRACE)
# if GLFW_VERSION_COMBINED >= 3300
// It seems `glfwGetJoystickGUID` can cause crash if the gamepad is quickly disconnected
const char* guid = glfwGetJoystickGUID(joy);
# else
const char* guid = "default";
# endif
LOGI("Gamepad {} \"{}\" [{}] has been connected", joyId, glfwGetJoystickName(joy), guid);
# endif
#else
int numButtons = -1;
int numAxes = -1;
int numHats = -1;
glfwGetJoystickButtons(joy, &numButtons);
glfwGetJoystickAxes(joy, &numAxes);
# if GLFW_VERSION_COMBINED >= 3300
glfwGetJoystickHats(joy, &numHats);
# else
numHats = 0;
# endif
if (numButtons <= 0 && numAxes <= 0 && numHats <= 0) {
LOGI("Gamepad {} has been connected, but reports no axes/buttons/hats - skipping", joyId);
return;
}
# if defined(DEATH_TRACE)
# if GLFW_VERSION_COMBINED >= 3300
// It seems `glfwGetJoystickGUID` can cause crash if the gamepad is quickly disconnected
const char* guid = glfwGetJoystickGUID(joy);
# else
const char* guid = "default";
# endif
LOGI("Gamepad {} \"{}\" [{}] has been connected - {} axes, {} buttons, {} hats",
joyId, glfwGetJoystickName(joy), guid, numAxes, numButtons, numHats);
# endif
#endif
updateJoystickStates();
if (inputEventHandler_ != nullptr) {
joyMapping_.OnJoyConnected(joyConnectionEvent_);
inputEventHandler_->OnJoyConnected(joyConnectionEvent_);
}
} else if (event == GLFW_DISCONNECTED) {
joyEventsSimulator_.resetJoystickState(joyId);
LOGI("Gamepad {} has been disconnected", joyId);
if (inputEventHandler_ != nullptr) {
inputEventHandler_->OnJoyDisconnected(joyConnectionEvent_);
joyMapping_.OnJoyDisconnected(joyConnectionEvent_);
}
}
}
#ifdef DEATH_TARGET_EMSCRIPTEN
EM_BOOL GlfwInputManager::emscriptenHandleTouch(int eventType, const EmscriptenTouchEvent* event, void* userData)
{
GlfwInputManager* inputManager = static_cast<GlfwInputManager*>(userData);
double cssWidth = 0.0;
double cssHeight = 0.0;
emscripten_get_element_css_size("canvas", &cssWidth, &cssHeight);
TouchEvent touchEvent;
touchEvent.count = std::min((unsigned int)event->numTouches, TouchEvent::MaxPointers);
switch (eventType) {
case EMSCRIPTEN_EVENT_TOUCHSTART:
touchEvent.type = (touchEvent.count >= 2 ? TouchEventType::PointerDown : TouchEventType::Down);
break;
case EMSCRIPTEN_EVENT_TOUCHMOVE:
touchEvent.type = TouchEventType::Move;
break;
default:
touchEvent.type = (touchEvent.count >= 2 ? TouchEventType::PointerUp : TouchEventType::Up);
break;
}
for (int i = 0; i < touchEvent.count; i++) {
auto& pointer = touchEvent.pointers[i];
pointer.id = event->touches[i].identifier;
pointer.x = (float)(event->touches[i].targetX / cssWidth);
pointer.y = (float)(event->touches[i].targetY / cssHeight);
pointer.pressure = 1.0f;
if (!event->touches[i].isChanged) {
continue;
}
touchEvent.actionIndex = pointer.id;
}
inputManager->inputEventHandler_->OnTouchEvent(touchEvent);
return 1;
}
#endif
GlfwInputManager::JoystickEventsSimulator::JoystickEventsSimulator()
{
std::memset(buttonsState_, 0, sizeof(buttonsState_));
std::memset(hatsState_, 0, sizeof(hatsState_));
std::memset(axesValuesState_, 0, sizeof(axesValuesState_));
}
void GlfwInputManager::JoystickEventsSimulator::resetJoystickState(int joyId)
{
std::memset(buttonsState_[joyId], 0, sizeof(unsigned char) * MaxNumButtons);
std::memset(hatsState_[joyId], 0, sizeof(unsigned char) * MaxNumHats);
std::memset(axesValuesState_[joyId], 0, sizeof(float) * MaxNumAxes);
}
void GlfwInputManager::JoystickEventsSimulator::simulateButtonsEvents(int joyId, int numButtons, const unsigned char* buttons)
{
for (int buttonId = 0; buttonId < numButtons; buttonId++) {
if (inputEventHandler_ != nullptr && buttonsState_[joyId][buttonId] != buttons[buttonId]) {
joyButtonEvent_.joyId = joyId;
joyButtonEvent_.buttonId = buttonId;
if (joystickStates_[joyId].buttons_[buttonId] == GLFW_PRESS) {
joyMapping_.OnJoyButtonPressed(joyButtonEvent_);
inputEventHandler_->OnJoyButtonPressed(joyButtonEvent_);
} else if (joystickStates_[joyId].buttons_[buttonId] == GLFW_RELEASE) {
joyMapping_.OnJoyButtonReleased(joyButtonEvent_);
inputEventHandler_->OnJoyButtonReleased(joyButtonEvent_);
}
}
}
if (numButtons > 0) {
std::memcpy(buttonsState_[joyId], buttons, sizeof(unsigned char) * numButtons);
}
}
void GlfwInputManager::JoystickEventsSimulator::simulateHatsEvents(int joyId, int numHats, const unsigned char* hats)
{
for (int hatId = 0; hatId < numHats; hatId++) {
if (inputEventHandler_ != nullptr && hatsState_[joyId][hatId] != hats[hatId]) {
joyHatEvent_.joyId = joyId;
joyHatEvent_.hatId = hatId;
joyHatEvent_.hatState = hats[hatId];
joyMapping_.OnJoyHatMoved(joyHatEvent_);
inputEventHandler_->OnJoyHatMoved(joyHatEvent_);
}
}
if (numHats > 0) {
std::memcpy(hatsState_[joyId], hats, sizeof(unsigned char) * numHats);
}
}
void GlfwInputManager::JoystickEventsSimulator::simulateAxesEvents(int joyId, int numAxes, const float* axesValues)
{
for (int axisId = 0; axisId < numAxes; axisId++) {
if (inputEventHandler_ != nullptr && fabsf(axesValuesState_[joyId][axisId] - axesValues[axisId]) > AxisEventTolerance) {
joyAxisEvent_.joyId = joyId;
joyAxisEvent_.axisId = axisId;
joyAxisEvent_.value = axesValues[axisId];
joyMapping_.OnJoyAxisMoved(joyAxisEvent_);
inputEventHandler_->OnJoyAxisMoved(joyAxisEvent_);
}
}
if (numAxes > 0) {
std::memcpy(axesValuesState_[joyId], axesValues, sizeof(float) * numAxes);
}
}
}
#endif
|