| 1 |
|
| 2 |
#ifdef HAVE_CONFIG_H |
| 3 |
#include <config.h> |
| 4 |
#endif |
| 5 |
|
| 6 |
#include "IOTouchDragAnywhere.h" |
| 7 |
|
| 8 |
#include "SDL.h" |
| 9 |
|
| 10 |
IOTouchDragAnywhere::IOTouchDragAnywhere() |
| 11 |
: up(false), down(false), |
| 12 |
left(false), right(false) { |
| 13 |
memset(&anchor, 0, sizeof(anchor)); |
| 14 |
} |
| 15 |
|
| 16 |
void IOTouchDragAnywhere::convertInput(SDL_Event* ev) { |
| 17 |
SDL_TouchFingerEvent* tev = &ev->tfinger; |
| 18 |
if (tev->type == SDL_FINGERDOWN && tev->fingerId == 0) { |
| 19 |
anchor.touchId = tev->touchId; |
| 20 |
anchor.fingerId = tev->fingerId; |
| 21 |
anchor.x = tev->x; |
| 22 |
anchor.y = tev->y; |
| 23 |
} else if (tev->type == SDL_FINGERUP) { |
| 24 |
if (tev->touchId == anchor.touchId && tev->fingerId == anchor.fingerId) { |
| 25 |
tev->touchId = 0; |
| 26 |
tev->fingerId = 0; |
| 27 |
|
| 28 |
if (up == true) { |
| 29 |
SDL_Event ev; memset(&ev, 0, sizeof(ev)); |
| 30 |
ev.type = SDL_KEYUP; |
| 31 |
ev.key.state = SDL_RELEASED; |
| 32 |
ev.key.keysym.scancode = SDL_SCANCODE_UP; |
| 33 |
SDL_PushEvent(&ev); |
| 34 |
} |
| 35 |
if (down == true) { |
| 36 |
SDL_Event ev; memset(&ev, 0, sizeof(ev)); |
| 37 |
ev.type = SDL_KEYUP; |
| 38 |
ev.key.state = SDL_RELEASED; |
| 39 |
ev.key.keysym.scancode = SDL_SCANCODE_DOWN; |
| 40 |
SDL_PushEvent(&ev); |
| 41 |
} |
| 42 |
if (left == true) { |
| 43 |
SDL_Event ev; memset(&ev, 0, sizeof(ev)); |
| 44 |
ev.type = SDL_KEYUP; |
| 45 |
ev.key.state = SDL_RELEASED; |
| 46 |
ev.key.keysym.scancode = SDL_SCANCODE_LEFT; |
| 47 |
SDL_PushEvent(&ev); |
| 48 |
} |
| 49 |
if (right == true) { |
| 50 |
SDL_Event ev; memset(&ev, 0, sizeof(ev)); |
| 51 |
ev.type = SDL_KEYUP; |
| 52 |
ev.key.state = SDL_RELEASED; |
| 53 |
ev.key.keysym.scancode = SDL_SCANCODE_RIGHT; |
| 54 |
SDL_PushEvent(&ev); |
| 55 |
} |
| 56 |
|
| 57 |
up = down = left = right = false; |
| 58 |
} |
| 59 |
} else if (tev->type == SDL_FINGERMOTION) { |
| 60 |
if (tev->touchId == anchor.touchId && tev->fingerId == anchor.fingerId) { |
| 61 |
bool new_up = false, new_down = false, |
| 62 |
new_left = false, new_right = false; |
| 63 |
float x = tev->x - anchor.x; |
| 64 |
float y = tev->y - anchor.y; |
| 65 |
float treshold = 0.05; |
| 66 |
|
| 67 |
if (y < -treshold) { |
| 68 |
new_up = true; |
| 69 |
} |
| 70 |
if (y > +treshold) { |
| 71 |
new_down = true; |
| 72 |
} |
| 73 |
if (x < -treshold) { |
| 74 |
new_left = true; |
| 75 |
} |
| 76 |
if (x > +treshold) { |
| 77 |
new_right = true; |
| 78 |
} |
| 79 |
|
| 80 |
if (up == false && new_up == true) { |
| 81 |
SDL_Event ev; memset(&ev, 0, sizeof(ev)); |
| 82 |
ev.type = SDL_KEYDOWN; |
| 83 |
ev.key.state = SDL_PRESSED; |
| 84 |
ev.key.keysym.scancode = SDL_SCANCODE_UP; |
| 85 |
SDL_PushEvent(&ev); |
| 86 |
} |
| 87 |
if (down == false && new_down == true) { |
| 88 |
SDL_Event ev; memset(&ev, 0, sizeof(ev)); |
| 89 |
ev.type = SDL_KEYDOWN; |
| 90 |
ev.key.state = SDL_PRESSED; |
| 91 |
ev.key.keysym.scancode = SDL_SCANCODE_DOWN; |
| 92 |
SDL_PushEvent(&ev); |
| 93 |
} |
| 94 |
if (left == false && new_left == true) { |
| 95 |
SDL_Event ev; memset(&ev, 0, sizeof(ev)); |
| 96 |
ev.type = SDL_KEYDOWN; |
| 97 |
ev.key.state = SDL_PRESSED; |
| 98 |
ev.key.keysym.scancode = SDL_SCANCODE_LEFT; |
| 99 |
SDL_PushEvent(&ev); |
| 100 |
} |
| 101 |
if (right == false && new_right == true) { |
| 102 |
SDL_Event ev; memset(&ev, 0, sizeof(ev)); |
| 103 |
ev.type = SDL_KEYDOWN; |
| 104 |
ev.key.state = SDL_PRESSED; |
| 105 |
ev.key.keysym.scancode = SDL_SCANCODE_RIGHT; |
| 106 |
SDL_PushEvent(&ev); |
| 107 |
} |
| 108 |
|
| 109 |
if (up == true && new_up == false) { |
| 110 |
SDL_Event ev; memset(&ev, 0, sizeof(ev)); |
| 111 |
ev.type = SDL_KEYUP; |
| 112 |
ev.key.state = SDL_RELEASED; |
| 113 |
ev.key.keysym.scancode = SDL_SCANCODE_UP; |
| 114 |
SDL_PushEvent(&ev); |
| 115 |
} |
| 116 |
if (down == true && new_down == false) { |
| 117 |
SDL_Event ev; memset(&ev, 0, sizeof(ev)); |
| 118 |
ev.type = SDL_KEYUP; |
| 119 |
ev.key.state = SDL_RELEASED; |
| 120 |
ev.key.keysym.scancode = SDL_SCANCODE_DOWN; |
| 121 |
SDL_PushEvent(&ev); |
| 122 |
} |
| 123 |
if (left == true && new_left == false) { |
| 124 |
SDL_Event ev; memset(&ev, 0, sizeof(ev)); |
| 125 |
ev.type = SDL_KEYUP; |
| 126 |
ev.key.state = SDL_RELEASED; |
| 127 |
ev.key.keysym.scancode = SDL_SCANCODE_LEFT; |
| 128 |
SDL_PushEvent(&ev); |
| 129 |
} |
| 130 |
if (right == true && new_right == false) { |
| 131 |
SDL_Event ev; memset(&ev, 0, sizeof(ev)); |
| 132 |
ev.type = SDL_KEYUP; |
| 133 |
ev.key.state = SDL_RELEASED; |
| 134 |
ev.key.keysym.scancode = SDL_SCANCODE_RIGHT; |
| 135 |
SDL_PushEvent(&ev); |
| 136 |
} |
| 137 |
|
| 138 |
up = new_up; |
| 139 |
down = new_down; |
| 140 |
left = new_left; |
| 141 |
right = new_right; |
| 142 |
} |
| 143 |
} |
| 144 |
} |