Skip to content

Commit

Permalink
3D touch support on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmaclarty committed Apr 8, 2016
1 parent 2dd9735 commit 3f93f78
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 15 deletions.
21 changes: 18 additions & 3 deletions lua/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ end

-- touch

function am._touch_begin(win, id, x, y, nx, ny, px, py)
function am._touch_begin(win, id, x, y, nx, ny, px, py, force)
local u = win._event_data
local free_i = nil
for i, touch in ipairs(u._touches) do
Expand All @@ -208,11 +208,12 @@ function am._touch_begin(win, id, x, y, nx, ny, px, py)
touch.norm_position_y = ny
touch.pixel_position_x = px
touch.pixel_position_y = py
touch.force = force
touch.began = true
end
end

function am._touch_end(win, id, x, y, nx, ny, px, py)
function am._touch_end(win, id, x, y, nx, ny, px, py, force)
local u = win._event_data
for i, touch in ipairs(u._touches) do
if touch.id == id then
Expand All @@ -222,13 +223,14 @@ function am._touch_end(win, id, x, y, nx, ny, px, py)
touch.norm_position_y = ny
touch.pixel_position_x = px
touch.pixel_position_y = py
touch.force = force
touch.ended = true
return
end
end
end

function am._touch_move(win, id, x, y, nx, ny, px, py)
function am._touch_move(win, id, x, y, nx, ny, px, py, force)
local u = win._event_data
for i, touch in ipairs(u._touches) do
if touch.id == id then
Expand All @@ -238,6 +240,7 @@ function am._touch_move(win, id, x, y, nx, ny, px, py)
touch.norm_position_y = ny
touch.pixel_position_x = px
touch.pixel_position_y = py
touch.force = force
return
end
end
Expand Down Expand Up @@ -276,6 +279,17 @@ function window_mt.touch_pixel_position(win, i)
end
end

function window_mt.touch_force(win, i)
i = i or 1
local u = win._event_data
local touch = u._touches[i]
if not touch then
return 0
else
return touch.force
end
end

function window_mt.touch_delta(win, i)
i = i or 1
local u = win._event_data
Expand Down Expand Up @@ -635,6 +649,7 @@ function am._init_window_event_data(window)
prev_norm_position_y = 0,
prev_pixel_position_x = 0,
prev_pixel_position_y = 0,
force = 0,
}
end

Expand Down
27 changes: 24 additions & 3 deletions src/am_backend_ios.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ static bool ios_window_created = false;
static bool ios_running = false;
static bool ios_audio_initialized = false;
static bool ios_audio_paused = false;
static bool ios_force_available = false;
static float *ios_audio_buffer = NULL;
static int ios_audio_offset = 0;
static NSLock *ios_audio_mutex = [[NSLock alloc] init];
Expand Down Expand Up @@ -443,9 +444,13 @@ static void ios_touch_began(NSSet *touches) {
NSEnumerator *e = [touches objectEnumerator];
UITouch *touch;
while ((touch = [e nextObject])) {
double force = 1.0;
if (ios_force_available) {
force = [touch force];
}
CGPoint pos = [touch locationInView:touch.view];
//am_debug("pos = %f %f", pos.x, pos.y);
am_find_window((am_native_window*)ios_view)->touch_begin(ios_eng->L, touch, pos.x, pos.y);
am_find_window((am_native_window*)ios_view)->touch_begin(ios_eng->L, touch, pos.x, pos.y, force);
}
}

Expand All @@ -454,8 +459,12 @@ static void ios_touch_moved(NSSet *touches) {
NSEnumerator *e = [touches objectEnumerator];
UITouch *touch;
while ((touch = [e nextObject])) {
double force = 1.0;
if (ios_force_available) {
force = [touch force];
}
CGPoint pos = [touch locationInView:touch.view];
am_find_window((am_native_window*)ios_view)->touch_move(ios_eng->L, touch, pos.x, pos.y);
am_find_window((am_native_window*)ios_view)->touch_move(ios_eng->L, touch, pos.x, pos.y, force);
}
}

Expand All @@ -464,8 +473,12 @@ static void ios_touch_ended(NSSet *touches) {
NSEnumerator *e = [touches objectEnumerator];
UITouch *touch;
while ((touch = [e nextObject])) {
double force = 1.0;
if (ios_force_available) {
force = [touch force];
}
CGPoint pos = [touch locationInView:touch.view];
am_find_window((am_native_window*)ios_view)->touch_end(ios_eng->L, touch, pos.x, pos.y);
am_find_window((am_native_window*)ios_view)->touch_end(ios_eng->L, touch, pos.x, pos.y, force);
}
}

Expand Down Expand Up @@ -618,6 +631,14 @@ static BOOL handle_orientation(UIInterfaceOrientation orientation) {
ios_view = view;
[view setMultipleTouchEnabled:YES];

if ([view respondsToSelector: NSSelectorFromString(@"traitCollection")]) {
if ([[view traitCollection] respondsToSelector: NSSelectorFromString(@"forceTouchCapability")]) {
if ([[view traitCollection] forceTouchCapability] == UIForceTouchCapabilityAvailable) {
ios_force_available = true;
}
}
}

[EAGLContext setCurrentContext:context];

ios_init_engine();
Expand Down
15 changes: 9 additions & 6 deletions src/am_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ void am_window::mouse_up(lua_State *L, am_mouse_button button) {
am_call_amulet(L, "_mouse_up", 2, 0);
}

void am_window::touch_begin(lua_State *L, void* touchid, double x, double y) {
void am_window::touch_begin(lua_State *L, void* touchid, double x, double y, double force) {
double ux, uy, nx, ny, px, py;
compute_position(x, y, &ux, &uy, &nx, &ny, &px, &py);
push(L);
Expand All @@ -211,10 +211,11 @@ void am_window::touch_begin(lua_State *L, void* touchid, double x, double y) {
lua_pushnumber(L, ny);
lua_pushnumber(L, px);
lua_pushnumber(L, py);
am_call_amulet(L, "_touch_begin", 8, 0);
lua_pushnumber(L, force);
am_call_amulet(L, "_touch_begin", 9, 0);
}

void am_window::touch_end(lua_State *L, void* touchid, double x, double y) {
void am_window::touch_end(lua_State *L, void* touchid, double x, double y, double force) {
double ux, uy, nx, ny, px, py;
compute_position(x, y, &ux, &uy, &nx, &ny, &px, &py);
push(L);
Expand All @@ -225,10 +226,11 @@ void am_window::touch_end(lua_State *L, void* touchid, double x, double y) {
lua_pushnumber(L, ny);
lua_pushnumber(L, px);
lua_pushnumber(L, py);
am_call_amulet(L, "_touch_end", 8, 0);
lua_pushnumber(L, force);
am_call_amulet(L, "_touch_end", 9, 0);
}

void am_window::touch_move(lua_State *L, void* touchid, double x, double y) {
void am_window::touch_move(lua_State *L, void* touchid, double x, double y, double force) {
double ux, uy, nx, ny, px, py;
compute_position(x, y, &ux, &uy, &nx, &ny, &px, &py);
push(L);
Expand All @@ -239,7 +241,8 @@ void am_window::touch_move(lua_State *L, void* touchid, double x, double y) {
lua_pushnumber(L, ny);
lua_pushnumber(L, px);
lua_pushnumber(L, py);
am_call_amulet(L, "_touch_move", 8, 0);
lua_pushnumber(L, force);
am_call_amulet(L, "_touch_move", 9, 0);
}

am_window* am_find_window(am_native_window *nwin) {
Expand Down
6 changes: 3 additions & 3 deletions src/am_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ struct am_window : am_nonatomic_userdata {
void mouse_down(lua_State *L, am_mouse_button button);
void mouse_up(lua_State *L, am_mouse_button button);
void mouse_wheel(lua_State *L, double x, double y);
void touch_begin(lua_State *L, void* touchid, double x, double y);
void touch_end(lua_State *L, void* touchid, double x, double y);
void touch_move(lua_State *L, void* touchid, double x, double y);
void touch_begin(lua_State *L, void* touchid, double x, double y, double force);
void touch_end(lua_State *L, void* touchid, double x, double y, double force);
void touch_move(lua_State *L, void* touchid, double x, double y, double force);
};

void am_open_window_module(lua_State *L);
Expand Down

0 comments on commit 3f93f78

Please sign in to comment.