Skip to content

Commit 4fa2c32

Browse files
committed
Review touch input scaling for PLATFORM_WEB #163
Now touch inputs scale proportionally to the canvas size
1 parent 7e9bed5 commit 4fa2c32

1 file changed

Lines changed: 21 additions & 36 deletions

File tree

src/core.c

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2615,9 +2615,9 @@ Vector2 GetTouchPosition(int index)
26152615
if (index < MAX_TOUCH_POINTS) position = touchPosition[index];
26162616
else TraceLog(LOG_WARNING, "Required touch point out of range (Max touch points: %i)", MAX_TOUCH_POINTS);
26172617

2618+
#if defined(PLATFORM_ANDROID)
26182619
if ((screenWidth > displayWidth) || (screenHeight > displayHeight))
26192620
{
2620-
// TODO: Review touch position scaling for screenSize vs displaySize
26212621
position.x = position.x*((float)screenWidth/(float)(displayWidth - renderOffsetX)) - renderOffsetX/2;
26222622
position.y = position.y*((float)screenHeight/(float)(displayHeight - renderOffsetY)) - renderOffsetY/2;
26232623
}
@@ -2626,6 +2626,7 @@ Vector2 GetTouchPosition(int index)
26262626
position.x = position.x*((float)renderWidth/(float)displayWidth) - renderOffsetX/2;
26272627
position.y = position.y*((float)renderHeight/(float)displayHeight) - renderOffsetY/2;
26282628
}
2629+
#endif
26292630
#elif defined(PLATFORM_RPI)
26302631

26312632
position = touchPosition[index];
@@ -4336,7 +4337,7 @@ static EM_BOOL EmscriptenFullscreenChangeCallback(int eventType, const Emscripte
43364337
//fullscreenEnabled: int e->fullscreenEnabled
43374338
//fs element nodeName: (char *) e->nodeName
43384339
//fs element id: (char *) e->id
4339-
//Current element size: (int) e->elementWidth, (int) e->elementHeight
4340+
//Current element size: (int) e->elementWidth, (int) e->elementHeight // WARNING: Not canvas size but full page!
43404341
//Screen size:(int) e->screenWidth, (int) e->screenHeight
43414342

43424343
if (e->isFullscreen)
@@ -4347,6 +4348,8 @@ static EM_BOOL EmscriptenFullscreenChangeCallback(int eventType, const Emscripte
43474348
{
43484349
TraceLog(LOG_INFO, "Canvas scaled to windowed. ElementSize: (%ix%i), ScreenSize(%ix%i)", e->elementWidth, e->elementHeight, e->screenWidth, e->screenHeight);
43494350
}
4351+
4352+
TraceLog(LOG_INFO, "Canvas resize?");
43504353

43514354
// TODO: Depending on scaling factor (screen vs element), calculate factor to scale mouse/touch input
43524355

@@ -4391,29 +4394,6 @@ static EM_BOOL EmscriptenMouseCallback(int eventType, const EmscriptenMouseEvent
43914394
static EM_BOOL EmscriptenTouchCallback(int eventType, const EmscriptenTouchEvent *touchEvent, void *userData)
43924395
{
43934396
touchDetected = true;
4394-
/*
4395-
for (int i = 0; i < touchEvent->numTouches; i++)
4396-
{
4397-
long x, y, id;
4398-
4399-
if (!touchEvent->touches[i].isChanged) continue;
4400-
4401-
id = touchEvent->touches[i].identifier;
4402-
x = touchEvent->touches[i].canvasX;
4403-
y = touchEvent->touches[i].canvasY;
4404-
}
4405-
4406-
TraceLog(LOG_DEBUG, "%s, numTouches: %d %s%s%s%s", emscripten_event_type_to_string(eventType), event->numTouches,
4407-
event->ctrlKey? " CTRL" : "", event->shiftKey? " SHIFT" : "", event->altKey? " ALT" : "", event->metaKey? " META" : "");
4408-
4409-
for (int i = 0; i < event->numTouches; ++i)
4410-
{
4411-
const EmscriptenTouchPoint *t = &event->touches[i];
4412-
4413-
TraceLog(LOG_DEBUG, " %ld: screen: (%ld,%ld), client: (%ld,%ld), page: (%ld,%ld), isChanged: %d, onTarget: %d, canvas: (%ld, %ld)",
4414-
t->identifier, t->screenX, t->screenY, t->clientX, t->clientY, t->pageX, t->pageY, t->isChanged, t->onTarget, t->canvasX, t->canvasY);
4415-
}
4416-
*/
44174397

44184398
#if defined(SUPPORT_GESTURES_SYSTEM)
44194399
GestureEvent gestureEvent;
@@ -4432,21 +4412,21 @@ static EM_BOOL EmscriptenTouchCallback(int eventType, const EmscriptenTouchEvent
44324412

44334413
// Register touch points position
44344414
// NOTE: Only two points registered
4435-
// TODO: Touch data should be scaled accordingly!
4436-
//gestureEvent.position[0] = (Vector2){ touchEvent->touches[0].canvasX, touchEvent->touches[0].canvasY };
4437-
//gestureEvent.position[1] = (Vector2){ touchEvent->touches[1].canvasX, touchEvent->touches[1].canvasY };
44384415
gestureEvent.position[0] = (Vector2){ touchEvent->touches[0].targetX, touchEvent->touches[0].targetY };
44394416
gestureEvent.position[1] = (Vector2){ touchEvent->touches[1].targetX, touchEvent->touches[1].targetY };
44404417

4441-
touchPosition[0] = gestureEvent.position[0];
4442-
touchPosition[1] = gestureEvent.position[1];
4418+
double canvasWidth, canvasHeight;
4419+
//EMSCRIPTEN_RESULT res = emscripten_get_canvas_element_size("#canvas", &canvasWidth, &canvasHeight);
4420+
emscripten_get_element_css_size("#canvas", &canvasWidth, &canvasHeight);
44434421

44444422
// Normalize gestureEvent.position[x] for screenWidth and screenHeight
4445-
gestureEvent.position[0].x /= (float)GetScreenWidth();
4446-
gestureEvent.position[0].y /= (float)GetScreenHeight();
4423+
gestureEvent.position[0].x *= ((float)GetScreenWidth()/(float)canvasWidth);
4424+
gestureEvent.position[0].y *= ((float)GetScreenHeight()/(float)canvasHeight);
4425+
gestureEvent.position[1].x *= ((float)GetScreenWidth()/(float)canvasWidth);
4426+
gestureEvent.position[1].y *= ((float)GetScreenHeight()/(float)canvasHeight);
44474427

4448-
gestureEvent.position[1].x /= (float)GetScreenWidth();
4449-
gestureEvent.position[1].y /= (float)GetScreenHeight();
4428+
touchPosition[0] = gestureEvent.position[0];
4429+
touchPosition[1] = gestureEvent.position[1];
44504430

44514431
// Gesture data is sent to gestures system for processing
44524432
ProcessGestureEvent(gestureEvent);
@@ -4457,8 +4437,13 @@ static EM_BOOL EmscriptenTouchCallback(int eventType, const EmscriptenTouchEvent
44574437
// Get first touch position
44584438
touchPosition[0] = (Vector2){ touchEvent->touches[0].targetX, touchEvent->touches[0].targetY };
44594439

4460-
touchPosition[0].x /= (float)GetScreenWidth();
4461-
touchPosition[0].y /= (float)GetScreenHeight();
4440+
double canvasWidth, canvasHeight;
4441+
//EMSCRIPTEN_RESULT res = emscripten_get_canvas_element_size("#canvas", &canvasWidth, &canvasHeight);
4442+
emscripten_get_element_css_size("#canvas", &canvasWidth, &canvasHeight);
4443+
4444+
// Normalize gestureEvent.position[x] for screenWidth and screenHeight
4445+
gestureEvent.position[0].x *= ((float)GetScreenWidth()/(float)canvasWidth);
4446+
gestureEvent.position[0].y *= ((float)GetScreenHeight()/(float)canvasHeight);
44624447
}
44634448
#endif
44644449

0 commit comments

Comments
 (0)