summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2021-04-20 16:47:05 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2021-04-20 16:47:05 +0300
commit992e3d00aa407d0d1d481051e2504bc56395dfb8 (patch)
tree5961b052b2c20daf10f2c35c2c6f37656b64659e /src
parentd41d2e4f68ac990cff543f5e5cffa098714e0de7 (diff)
Mobile: Fixed invalid touch positions
Diffstat (limited to 'src')
-rw-r--r--src/ui/touch.c9
-rw-r--r--src/ui/touch.h3
-rw-r--r--src/ui/window.c7
3 files changed, 18 insertions, 1 deletions
diff --git a/src/ui/touch.c b/src/ui/touch.c
index 98dabc39..4bbabece 100644
--- a/src/ui/touch.c
+++ b/src/ui/touch.c
@@ -100,6 +100,7 @@ struct Impl_TouchState {
100 iArray *pinches; 100 iArray *pinches;
101 iArray *moms; 101 iArray *moms;
102 double lastMomTime; 102 double lastMomTime;
103 iInt2 currentTouchPos; /* for emulating SDL_GetMouseState() */
103}; 104};
104 105
105static iTouchState *touchState_(void) { 106static iTouchState *touchState_(void) {
@@ -150,6 +151,7 @@ static void clearWidgetMomentum_TouchState_(iTouchState *d, iWidget *widget) {
150} 151}
151 152
152static void dispatchMotion_Touch_(iFloat3 pos, int buttonState) { 153static void dispatchMotion_Touch_(iFloat3 pos, int buttonState) {
154 touchState_()->currentTouchPos = initF3_I2(pos);
153 dispatchEvent_Widget(get_Window()->root, (SDL_Event *) &(SDL_MouseMotionEvent){ 155 dispatchEvent_Widget(get_Window()->root, (SDL_Event *) &(SDL_MouseMotionEvent){
154 .type = SDL_MOUSEMOTION, 156 .type = SDL_MOUSEMOTION,
155 .timestamp = SDL_GetTicks(), 157 .timestamp = SDL_GetTicks(),
@@ -162,6 +164,7 @@ static void dispatchMotion_Touch_(iFloat3 pos, int buttonState) {
162 164
163static iBool dispatchClick_Touch_(const iTouch *d, int button) { 165static iBool dispatchClick_Touch_(const iTouch *d, int button) {
164 const iFloat3 tapPos = d->pos[0]; 166 const iFloat3 tapPos = d->pos[0];
167 touchState_()->currentTouchPos = initF3_I2(tapPos);
165 iWindow *window = get_Window(); 168 iWindow *window = get_Window();
166 SDL_MouseButtonEvent btn = { 169 SDL_MouseButtonEvent btn = {
167 .type = SDL_MOUSEBUTTONDOWN, 170 .type = SDL_MOUSEBUTTONDOWN,
@@ -186,6 +189,7 @@ static iBool dispatchClick_Touch_(const iTouch *d, int button) {
186} 189}
187 190
188static void dispatchButtonDown_Touch_(iFloat3 pos) { 191static void dispatchButtonDown_Touch_(iFloat3 pos) {
192 touchState_()->currentTouchPos = initF3_I2(pos);
189 dispatchEvent_Widget(get_Window()->root, (SDL_Event *) &(SDL_MouseButtonEvent){ 193 dispatchEvent_Widget(get_Window()->root, (SDL_Event *) &(SDL_MouseButtonEvent){
190 .type = SDL_MOUSEBUTTONDOWN, 194 .type = SDL_MOUSEBUTTONDOWN,
191 .timestamp = SDL_GetTicks(), 195 .timestamp = SDL_GetTicks(),
@@ -199,6 +203,7 @@ static void dispatchButtonDown_Touch_(iFloat3 pos) {
199} 203}
200 204
201static void dispatchButtonUp_Touch_(iFloat3 pos) { 205static void dispatchButtonUp_Touch_(iFloat3 pos) {
206 touchState_()->currentTouchPos = initF3_I2(pos);
202 dispatchEvent_Widget(get_Window()->root, (SDL_Event *) &(SDL_MouseButtonEvent){ 207 dispatchEvent_Widget(get_Window()->root, (SDL_Event *) &(SDL_MouseButtonEvent){
203 .type = SDL_MOUSEBUTTONUP, 208 .type = SDL_MOUSEBUTTONUP,
204 .timestamp = SDL_GetTicks(), 209 .timestamp = SDL_GetTicks(),
@@ -694,6 +699,10 @@ void widgetDestroyed_Touch(iWidget *widget) {
694 } 699 }
695} 700}
696 701
702iInt2 latestPosition_Touch(void) {
703 return touchState_()->currentTouchPos;
704}
705
697size_t numFingers_Touch(void) { 706size_t numFingers_Touch(void) {
698 return size_Array(touchState_()->touches); 707 return size_Array(touchState_()->touches);
699} 708}
diff --git a/src/ui/touch.h b/src/ui/touch.h
index 19a927ab..3e9c3859 100644
--- a/src/ui/touch.h
+++ b/src/ui/touch.h
@@ -22,7 +22,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
22 22
23#pragma once 23#pragma once
24 24
25#include <the_Foundation/defs.h> 25#include <the_Foundation/vec2.h>
26#include <SDL_events.h> 26#include <SDL_events.h>
27 27
28iDeclareType(Widget) 28iDeclareType(Widget)
@@ -31,4 +31,5 @@ iBool processEvent_Touch (const SDL_Event *);
31void update_Touch (void); 31void update_Touch (void);
32void widgetDestroyed_Touch (iWidget *widget); 32void widgetDestroyed_Touch (iWidget *widget);
33 33
34iInt2 latestPosition_Touch (void); /* valid during processing of current event */
34size_t numFingers_Touch (void); 35size_t numFingers_Touch (void);
diff --git a/src/ui/window.c b/src/ui/window.c
index 05db9a80..cf611e31 100644
--- a/src/ui/window.c
+++ b/src/ui/window.c
@@ -2110,12 +2110,19 @@ iInt2 coord_Window(const iWindow *d, int x, int y) {
2110} 2110}
2111 2111
2112iInt2 mouseCoord_Window(const iWindow *d) { 2112iInt2 mouseCoord_Window(const iWindow *d) {
2113#if defined (iPlatformMobile)
2114 /* At least on iOS the coordinates returned by SDL_GetMouseState() do no match up with
2115 our touch coordinates on the Y axis (?). Maybe a pixel ratio thing? */
2116 iUnused(d);
2117 return latestPosition_Touch();
2118#else
2113 if (!d->isMouseInside) { 2119 if (!d->isMouseInside) {
2114 return init_I2(-1000000, -1000000); 2120 return init_I2(-1000000, -1000000);
2115 } 2121 }
2116 int x, y; 2122 int x, y;
2117 SDL_GetMouseState(&x, &y); 2123 SDL_GetMouseState(&x, &y);
2118 return coord_Window(d, x, y); 2124 return coord_Window(d, x, y);
2125#endif
2119} 2126}
2120 2127
2121float uiScale_Window(const iWindow *d) { 2128float uiScale_Window(const iWindow *d) {