Line 1: |
Line 1: |
| Simple lua code showing event handling, class creation, grabbing objects across the screen, cursor setting...<br /> | | Simple lua code showing event handling, class creation, grabbing objects across the screen, cursor setting...<br /> |
| | | |
− | (Code by Adrien "Adriweb" Bertrand) | + | (Code by [[User:Adriweb|Adriweb]] ) |
| | | |
| | | |
− | <syntaxhighlight>Ball = class() | + | <syntaxhighlight>ballsTable = {} |
| + | isGrabbing = false |
| + | txt2 = "not moving anything" |
| + | |
| + | |
| + | Ball = class() |
| | | |
| function Ball:init(x,y,r,color) | | function Ball:init(x,y,r,color) |
Line 19: |
Line 24: |
| gc:fillArc(self.x,self.y,self.r,self.r,0,360) | | gc:fillArc(self.x,self.y,self.r,self.r,0,360) |
| if self.isActive then | | if self.isActive then |
− | gc:setColorRGB(0) | + | gc:setColorRGB(0,0,0) |
| gc:drawArc(self.x,self.y,self.r,self.r,0,360) | | gc:drawArc(self.x,self.y,self.r,self.r,0,360) |
| end | | end |
Line 36: |
Line 41: |
| | | |
| function on.mouseUp(x,y) | | function on.mouseUp(x,y) |
| + | cursor.set("default") |
| isGrabbing = false | | isGrabbing = false |
| platform.window:invalidate() | | platform.window:invalidate() |
Line 47: |
Line 53: |
| function on.mouseMove(x,y) | | function on.mouseMove(x,y) |
| if isGrabbing and (trackedBall and trackedBall.isActive) then | | if isGrabbing and (trackedBall and trackedBall.isActive) then |
− | txt2 = "moving ball #"..trackedBall.id
| + | cursor.set("drag grab") |
− | trackedBall:move(x-trackedBall.r/2,y-trackedBall.r/2)
| + | txt2 = "moving ball #"..trackedBall.id |
− | else
| + | trackedBall:move(x-trackedBall.r/2,y-trackedBall.r/2) |
− | txt2 = "not moving anything"
| |
− | for i,ball in ipairs(ballsTable) do
| |
− | if math.abs(ball.x+ball.r/2-x) <= ball.r/1.8 and math.abs(ball.y+ball.r/2-y) <= ball.r/1.8 then
| |
− | ball.isActive = true
| |
− | trackedBall = ball
| |
| else | | else |
− | ball.isActive = false
| + | txt2 = "not moving anything" |
| + | for i,ball in ipairs(ballsTable) do |
| + | ball.isActive = false |
| + | cursor.set("pointer") |
| + | if math.abs(ball.x+ball.r/2-x) <= ball.r/1.8 and math.abs(ball.y+ball.r/2-y) <= ball.r/1.8 then |
| + | ball.isActive = true |
| + | cursor.set("hand pointer") |
| + | trackedBall = ball |
| + | break |
| + | end |
| + | end |
| end | | end |
− | end
| |
− | end
| |
| end | | end |
| | | |
Line 71: |
Line 80: |
| gc:drawString("Press Enter to create a Ball object", 2, 190, "top") | | gc:drawString("Press Enter to create a Ball object", 2, 190, "top") |
| end | | end |
− |
| |
− | ballsTable = {}
| |
− | grabFlag = false
| |
− | isGrabbing = false
| |
− | txt2 = "not moving anything"
| |
| | | |
| function on.enterKey() | | function on.enterKey() |
| ballsTable[#ballsTable+1] = Ball(math.random(10,250), math.random(10,180), 15, {math.random(0,255),math.random(0,255),math.random(0,255)}) | | ballsTable[#ballsTable+1] = Ball(math.random(10,250), math.random(10,180), 15, {math.random(0,255),math.random(0,255),math.random(0,255)}) |
| + | platform.window:invalidate() |
| end</syntaxhighlight> | | end</syntaxhighlight> |