source: touchless.lua

Last change on this file was 102, checked in by drazed, 7 years ago

fix touch-release to work around fractional-total-touch-area change.

File size: 2.5 KB
Line 
1-- this file only loads if touch enabled
2declare("touchless",{})
3
4local xres = gkinterface.GetXResolution()
5local yres = gkinterface.GetYResolution()
6
7local rcreate = gkinterface.CreateTouchRegion
8local rdestroy = gkinterface.DestroyTouchRegion
9
10touchless.region = nil
11
12function touchless.destroy_touch_region()
13    rdestroy(touchless.region)
14    touchless.regions = nil
15end
16
17function touchless.create_touch_region()
18    touchless.destroy_touch_region()
19
20    -- this is the targetless touch region, more-or-less overlapping targets
21    touchless.region = rcreate(nil, nil, nil, false, false, false, false, false, false, 0.75*xres, 0.20*yres, (xres - 1), (0.6*yres - 1))
22end
23
24local pressX = 0
25local pressY = 0
26function touchless:on_touch_pressed(id, screenX, screenY)
27    if(id == touchless.region) then
28        -- store the x/y values for compare on release
29        pressX = screenX
30        pressY = screenY
31    end
32end
33
34function touchless:on_touch_released(id, screenX, screenY)
35    if(id == touchless.region) then
36        local diffX = screenX - pressX
37        local diffY = screenY - pressY
38
39        -- recent changes make touch release return the fraction of total touch area moved rather then actual pixels, since pixels are superior lets convert this fraction back to pixels and keep our original logic in tact :)
40        -- we user 0.25 of the available xres and 0.4 of the available yres
41        diffX = diffX*xres*0.25
42        diffY = diffY*yres*0.40
43
44        -- defualt pin/unip
45        local command = "pin"
46        if(math.abs(diffX) > math.abs(diffY)) then
47            -- swip left/right
48            -- check min 5 pixel
49            if(diffX > 5) then
50                command = "lsswitch"
51            elseif(diffX < -5) then
52                command = "lsback"
53            end
54        else
55            -- swip up/down
56            -- check min 5 pixel
57            if(diffY > 5) then
58                command = "nextLS"
59            elseif(diffY < -5) then
60                command = "prevLS"
61            end
62        end
63
64        gkinterface.GKProcessCommand(command)
65    end
66end
67
68function touchless:OnEvent(event, ...)
69    if     (event == 'HUD_SHOW') then
70        self:create_touch_region()
71
72    elseif (event == 'HUD_HIDE') then
73        self:destroy_touch_region()
74
75    elseif (event == 'TOUCH_PRESSED') then
76        self:on_touch_pressed(...)
77
78    elseif (event == 'TOUCH_RELEASED') then
79        self:on_touch_released(...)
80    end
81end
82
83RegisterEvent(touchless, 'HUD_SHOW')
84RegisterEvent(touchless, 'HUD_HIDE')
85RegisterEvent(touchless, 'TOUCH_PRESSED')
86RegisterEvent(touchless, 'TOUCH_RELEASED')
Note: See TracBrowser for help on using the repository browser.