Created
April 9, 2013 14:35
-
-
Save dermotbalson/5346172 to your computer and use it in GitHub Desktop.
11.Touches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- test | |
-- Use this function to perform your initial setup | |
function setup() | |
if backup then Backup("pinch") end | |
img=readImage("Small World:Mine Medium") | |
p=pinch() | |
end | |
function draw() | |
background(0) | |
p:processTouches() | |
sprite(img,WIDTH/2,HEIGHT/2,500*p.zoom) | |
end | |
function touched(touch) | |
p:touched(touch) | |
end | |
pinch=class() | |
function pinch:init() | |
self.tb={} | |
self.zoom=1 | |
end | |
function pinch:touched(touch) | |
if touch.state==ENDED then self.tb={} self.d1=nil self.d2=nil | |
else table.insert(self.tb,{x=touch.x,y=touch.y}) end | |
end | |
function pinch:processTouches() | |
if #self.tb==2 then | |
local v1,v2=vec2(self.tb[1].x,self.tb[1].y), vec2(self.tb[2].x,self.tb[2].y) | |
self.d1=v1:dist(v2) | |
end | |
if self.d2~=nil then self.zoom=self.zoom*self.d1/self.d2 end | |
self.d2=self.d1 self.tb={} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment