Difference between revisions of "Balls Example"
Jump to navigation
Jump to search
m (line breaks) |
|||
(5 intermediate revisions by the same user not shown) | |||
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 | + | (Code by [[User:Adriweb|Adriweb]] ) |
− | <syntaxhighlight> | + | <syntaxhighlight>ballsTable = {} |
+ | isGrabbing = false | ||
+ | txt2 = "not moving anything" | ||
− | |||
− | + | Ball = class() | |
− | function Ball: | + | function Ball:init(x,y,r,color) |
+ | self.x = x | ||
+ | self.y = y | ||
+ | self.r = r | ||
+ | self.color = color | ||
+ | self.isActive = false | ||
+ | self.id = #ballsTable+1 | ||
+ | end | ||
− | + | function Ball:paint(gc) | |
+ | gc:setColorRGB(unpack(self.color)) | ||
+ | gc:fillArc(self.x,self.y,self.r,self.r,0,360) | ||
+ | if self.isActive then | ||
+ | gc:setColorRGB(0,0,0) | ||
+ | gc:drawArc(self.x,self.y,self.r,self.r,0,360) | ||
+ | end | ||
+ | end | ||
− | function | + | function Ball:move(x,y) |
+ | self.x = x | ||
+ | self.y = y | ||
+ | platform.window:invalidate() | ||
+ | end | ||
− | function on. | + | function on.grabDown(x,y) |
+ | isGrabbing = not isGrabbing | ||
+ | platform.window:invalidate() | ||
+ | end | ||
− | function on. | + | function on.mouseUp(x,y) |
+ | cursor.set("default") | ||
+ | isGrabbing = false | ||
+ | platform.window:invalidate() | ||
+ | end | ||
− | function on. | + | function on.mouseDown(x,y) |
+ | isGrabbing = true | ||
+ | platform.window:invalidate() | ||
+ | end | ||
− | + | function on.mouseMove(x,y) | |
+ | if isGrabbing and (trackedBall and trackedBall.isActive) then | ||
+ | cursor.set("drag grab") | ||
+ | txt2 = "moving ball #"..trackedBall.id | ||
+ | trackedBall:move(x-trackedBall.r/2,y-trackedBall.r/2) | ||
+ | else | ||
+ | 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 | ||
+ | |||
+ | function on.paint(gc) | ||
+ | for _,ball in ipairs(ballsTable) do | ||
+ | ball:paint(gc) | ||
+ | end | ||
+ | gc:setColorRGB(0,0,0) | ||
+ | gc:drawString("grabbing : " .. tostring(isGrabbing),2,0,"top") | ||
+ | gc:drawString(tostring(txt2),2,20,"top") | ||
+ | gc:drawString("Press Enter to create a Ball object", 2, 190, "top") | ||
+ | end | ||
+ | |||
+ | 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)}) | ||
+ | platform.window:invalidate() | ||
+ | end</syntaxhighlight> |
Latest revision as of 03:38, 27 August 2013
Simple lua code showing event handling, class creation, grabbing objects across the screen, cursor setting...
(Code by Adriweb )
ballsTable = {}
isGrabbing = false
txt2 = "not moving anything"
Ball = class()
function Ball:init(x,y,r,color)
self.x = x
self.y = y
self.r = r
self.color = color
self.isActive = false
self.id = #ballsTable+1
end
function Ball:paint(gc)
gc:setColorRGB(unpack(self.color))
gc:fillArc(self.x,self.y,self.r,self.r,0,360)
if self.isActive then
gc:setColorRGB(0,0,0)
gc:drawArc(self.x,self.y,self.r,self.r,0,360)
end
end
function Ball:move(x,y)
self.x = x
self.y = y
platform.window:invalidate()
end
function on.grabDown(x,y)
isGrabbing = not isGrabbing
platform.window:invalidate()
end
function on.mouseUp(x,y)
cursor.set("default")
isGrabbing = false
platform.window:invalidate()
end
function on.mouseDown(x,y)
isGrabbing = true
platform.window:invalidate()
end
function on.mouseMove(x,y)
if isGrabbing and (trackedBall and trackedBall.isActive) then
cursor.set("drag grab")
txt2 = "moving ball #"..trackedBall.id
trackedBall:move(x-trackedBall.r/2,y-trackedBall.r/2)
else
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
function on.paint(gc)
for _,ball in ipairs(ballsTable) do
ball:paint(gc)
end
gc:setColorRGB(0,0,0)
gc:drawString("grabbing : " .. tostring(isGrabbing),2,0,"top")
gc:drawString(tostring(txt2),2,20,"top")
gc:drawString("Press Enter to create a Ball object", 2, 190, "top")
end
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)})
platform.window:invalidate()
end