ymobe/rapanui — Issue #11: newShape using MOAIDraw
Author: anddrewscott
Created: 2012-01-01 13:12:49+00:00
State: open

Pull requests are too complicated for me.

Can be called using:
local shape = R.Factory.newShape(100,8) --distance from center to each point is 100, 8 sided shape.
-- if #sides is not declared then it defaults to 32 (a circle)

RNfactory:

function newShape(ra,si, params)
local r = ra
local s = si or 32
local parentGroup, top, left = nil, 0,0

if params then
if type(params) == "table" then
parentGroup = params.parentGroup or mainGroups
top = params.top or 0
left = params.left or 0
end
end
local shape = RN.Object:new()
shape:initWithShape(r,s)
screen:addRNObject(shape)
shape.x = shape.originalWidth *.5 + left
shape.y = shape.originalHeight *.5 + top

if parentGroup ~= nil then
parentGroup:insert(shape)
end
return shape
end

RNObject:

function RNObject:initWithShape(r,s)
self.visible = true
self.childrenSize = 0

self.alpha = 1
self:loadShape(r,s)
end

function RNObject:loadShape(r,s)
self.name = "shape"
local function onDraw()
MOAIGfxDevice.setPenColor ( 1, 1, 1, 1 )
MOAIDraw.fillCircle ( 0, 0, r, s )
end

self.gfxQuad = MOAIScriptDeck.new ()
self.gfxQuad:setRect ( -r, -r, r, r )
self.gfxQuad:setDrawCallback ( onDraw )

self.originalWidth, self.originalHeight = r,r

self.pow2Widht, self.pow2Height = r,r
self.prop = MOAIProp2D.new()
if math.mod(s, 2) == 0 then
self.prop:setRot(360/(s*2))
end

self.prop:setDeck(self.gfxQuad)

self.prop:setPriority(1)
end