libnx: improve touch scaling calc

This commit is contained in:
natinusala 2018-10-03 18:50:36 +02:00
parent 826c77d523
commit a5f122a84e

View File

@ -16,6 +16,7 @@
#include "../input_driver.h"
#define MAX_PADS 10
#define TOUCH_AXIS_MAX 0x7fff /* abstraction of pointer coords */
/* TODO/FIXME -
* fix game focus toggle */
@ -29,6 +30,12 @@ typedef struct switch_input
uint32_t touch_x;
uint32_t touch_y;
uint32_t touch_scale_x;
uint32_t touch_scale_y;
uint32_t touch_half_resolution_x;
uint32_t touch_half_resolution_y;
bool touch_state;
#endif
} switch_input_t;
@ -57,24 +64,18 @@ static void switch_input_poll(void *data)
}
#ifdef HAVE_LIBNX
static int16_t scale_touch(int16_t from_min, int16_t from_max,
int16_t to_min, int16_t to_max,
int16_t value)
void calc_touch_scaling(switch_input_t *sw, uint32_t x, uint32_t y, uint32_t axis_max)
{
int32_t from_range = from_max - from_min;
int32_t to_range = to_max - to_min;
sw->touch_half_resolution_x = x/2;
sw->touch_half_resolution_y = y/2;
return (((value - from_min) * to_range) / from_range) + to_min;
sw->touch_scale_x = axis_max / sw->touch_half_resolution_x;
sw->touch_scale_y = axis_max / sw->touch_half_resolution_y;
}
static int16_t switch_pointer_device_state(switch_input_t *sw,
unsigned id, unsigned idx)
{
/*
Here we assume that the touch screen is always 1280x720
Call me back when a Nintendo Switch XL is out
*/
if (idx != 0)
return 0;
@ -83,9 +84,9 @@ static int16_t switch_pointer_device_state(switch_input_t *sw,
case RETRO_DEVICE_ID_POINTER_PRESSED:
return sw->touch_state;
case RETRO_DEVICE_ID_POINTER_X:
return scale_touch(0, 1280, -0x7fff, 0x7fff, (uint16_t) sw->touch_x);
return ((sw->touch_x - sw->touch_half_resolution_x) * sw->touch_scale_x);
case RETRO_DEVICE_ID_POINTER_Y:
return scale_touch(0, 720, -0x7fff, 0x7fff, (uint16_t) sw->touch_y);
return ((sw->touch_y - sw->touch_half_resolution_y) * sw->touch_scale_y);
}
return 0;
@ -142,6 +143,15 @@ static void* switch_input_init(const char *joypad_driver)
sw->joypad = input_joypad_init_driver(joypad_driver, sw);
#ifdef HAVE_LIBNX
/*
Here we assume that the touch screen is always 1280x720
Call me back when a Nintendo Switch XL is out
*/
calc_touch_scaling(sw, 1280, 720, TOUCH_AXIS_MAX);
#endif
return sw;
}