change touchscreen control method

This commit is contained in:
sonil 2022-10-16 19:43:33 +08:00
parent 21aec548af
commit 339fc41327
1 changed files with 102 additions and 76 deletions

View File

@ -1,4 +1,6 @@
#include "dinput.h"
#include "display_monitor.h"
#include "game_mouse.h"
namespace fallout {
@ -67,57 +69,6 @@ bool mouseDeviceUnacquire()
{
return true;
}
// 0x4E053C
bool mouseDeviceGetData(MouseData* mouseState)
{
if (gLastInputType == INPUT_TYPE_TOUCH) {
mouseState->x = gTouchMouseDeltaX;
mouseState->y = gTouchMouseDeltaY;
mouseState->buttons[0] = 0;
mouseState->buttons[1] = 0;
mouseState->wheelX = 0;
mouseState->wheelY = 0;
gTouchMouseDeltaX = 0;
gTouchMouseDeltaY = 0;
if (gTouchFingers == 0) {
if (SDL_GetTicks() - gTouchGestureLastTouchUpTimestamp > 150) {
if (!gTouchGestureHandled) {
if (gTouchGestureTaps == 2) {
mouseState->buttons[0] = 1;
gTouchGestureHandled = true;
} else if (gTouchGestureTaps == 3) {
mouseState->buttons[1] = 1;
gTouchGestureHandled = true;
}
}
}
} else if (gTouchFingers == 1) {
if (SDL_GetTicks() - gTouchGestureLastTouchDownTimestamp > 150) {
if (gTouchGestureTaps == 1) {
mouseState->buttons[0] = 1;
gTouchGestureHandled = true;
} else if (gTouchGestureTaps == 2) {
mouseState->buttons[1] = 1;
gTouchGestureHandled = true;
}
}
}
} else {
Uint32 buttons = SDL_GetRelativeMouseState(&(mouseState->x), &(mouseState->y));
mouseState->buttons[0] = (buttons & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0;
mouseState->buttons[1] = (buttons & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0;
mouseState->wheelX = gMouseWheelDeltaX;
mouseState->wheelY = gMouseWheelDeltaY;
gMouseWheelDeltaX = 0;
gMouseWheelDeltaY = 0;
}
return true;
}
// 0x4E05A8
bool keyboardDeviceAcquire()
{
@ -192,44 +143,119 @@ void handleMouseEvent(SDL_Event* event)
}
}
static bool em_mode = false;
static SDL_FingerID lastTouchId = -1;
static Uint32 lastType = -1;
static bool longPressed = false;
static bool clicked = false;
bool mouseDeviceGetData(MouseData* mouseState)
{
if (gLastInputType == INPUT_TYPE_TOUCH) {
mouseState->x = gTouchMouseDeltaX;
mouseState->y = gTouchMouseDeltaY;
mouseState->buttons[0] = 0;
mouseState->buttons[1] = 0;
mouseState->wheelX = 0;
mouseState->wheelY = 0;
gTouchMouseDeltaX = 0;
gTouchMouseDeltaY = 0;
if (lastType == SDL_FINGERDOWN && SDL_GetTicks() - gTouchGestureLastTouchUpTimestamp > 250) {
longPressed = true;
}
if (longPressed) {
mouseState->buttons[0] = 1;
clicked = false;
} else if (clicked) {
clicked = false;
if (em_mode) {
mouseState->x = gTouchMouseDeltaX;
mouseState->y = gTouchMouseDeltaY;
}
if (gTouchMouseLastX < screenGetWidth() / 4) {
mouseState->buttons[1] = 1;
} else {
mouseState->buttons[0] = 1;
}
}
} else {
Uint32 buttons = SDL_GetRelativeMouseState(&(mouseState->x), &(mouseState->y));
mouseState->buttons[0] = (buttons & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0;
mouseState->buttons[1] = (buttons & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0;
mouseState->wheelX = gMouseWheelDeltaX;
mouseState->wheelY = gMouseWheelDeltaY;
gMouseWheelDeltaX = 0;
gMouseWheelDeltaY = 0;
}
return true;
}
void handleTouchEvent(SDL_Event* event)
{
int windowWidth = screenGetWidth();
int windowHeight = screenGetHeight();
if (event->tfinger.type == SDL_FINGERDOWN) {
gTouchFingers++;
Uint32 type = event->tfinger.type;
SDL_FingerID id = event->tfinger.fingerId;
if (id >= 2)
return;
if (type == SDL_FINGERDOWN) {
if (lastTouchId != -1 && id != lastTouchId) {
//displayMonitorAddMessage("em true");
em_mode = true;
}
lastTouchId = id;
gTouchGestureLastTouchUpTimestamp = event->tfinger.timestamp;
gTouchMouseLastX = (int)(event->tfinger.x * windowWidth);
gTouchMouseLastY = (int)(event->tfinger.y * windowHeight);
gTouchMouseDeltaX = 0;
gTouchMouseDeltaY = 0;
if (event->tfinger.timestamp - gTouchGestureLastTouchDownTimestamp > 250) {
gTouchGestureTaps = 0;
gTouchGestureHandled = false;
lastType = SDL_FINGERDOWN;
clicked = false;
} else if (type == SDL_FINGERMOTION) {
if (id == lastTouchId) {
int prevX = gTouchMouseLastX;
int prevY = gTouchMouseLastY;
gTouchMouseLastX = (int)(event->tfinger.x * windowWidth);
gTouchMouseLastY = (int)(event->tfinger.y * windowHeight);
if(event->tfinger.x < 0.5){
gTouchMouseDeltaX += (gTouchMouseLastX - prevX) * 2;
gTouchMouseDeltaY += (gTouchMouseLastY - prevY) * 2;
} else {
gTouchMouseDeltaX += gTouchMouseLastX - prevX;
gTouchMouseDeltaY += gTouchMouseLastY - prevY;
}
gTouchGestureLastTouchUpTimestamp = -1;
lastType = SDL_FINGERMOTION;
clicked = false;
}
} else if (type == SDL_FINGERUP) {
if (id != lastTouchId) {
em_mode = false;
} else {
clicked = false;
gTouchGestureLastTouchDownTimestamp = event->tfinger.timestamp;
} else if (event->tfinger.type == SDL_FINGERMOTION) {
int prevX = gTouchMouseLastX;
int prevY = gTouchMouseLastY;
gTouchMouseLastX = (int)(event->tfinger.x * windowWidth);
gTouchMouseLastY = (int)(event->tfinger.y * windowHeight);
gTouchMouseDeltaX += gTouchMouseLastX - prevX;
gTouchMouseDeltaY += gTouchMouseLastY - prevY;
} else if (event->tfinger.type == SDL_FINGERUP) {
gTouchFingers--;
int prevX = gTouchMouseLastX;
int prevY = gTouchMouseLastY;
gTouchMouseLastX = (int)(event->tfinger.x * windowWidth);
gTouchMouseLastY = (int)(event->tfinger.y * windowHeight);
gTouchMouseDeltaX += gTouchMouseLastX - prevX;
gTouchMouseDeltaY += gTouchMouseLastY - prevY;
gTouchGestureTaps++;
gTouchGestureLastTouchUpTimestamp = event->tfinger.timestamp;
if (lastType == SDL_FINGERDOWN && longPressed == false) {
clicked = true;
}
longPressed = false;
gTouchGestureLastTouchUpTimestamp = -1;
lastTouchId = -1;
lastType = SDL_FINGERUP;
}
}
if (gLastInputType != INPUT_TYPE_TOUCH) {