Skip to content

Instantly share code, notes, and snippets.

@HoraceBury
Created March 21, 2018 09:44
Show Gist options
  • Save HoraceBury/1e2ce033e3441823038eb88b551ad981 to your computer and use it in GitHub Desktop.
Save HoraceBury/1e2ce033e3441823038eb88b551ad981 to your computer and use it in GitHub Desktop.
Display library, adding to and modifying the existing Corona display library.
-- display lib
local newShapeList = {
"newCircle",
"newContainer",
"newEmbossedText",
"newEmitter",
"newGroup",
"newImage",
"newImageRect",
"newLine",
"newMesh",
"newPolygon",
"newRect",
"newRoundedRect",
"newSnapshot",
"newSprite",
"newText",
}
--[[
Adds a property called 'type' to the returned display object created by
every display.* library function which creates a display object.
Example:
local circle = display.newCircle( 10, 10, 100 )
print( circle.type )
-- This will print "newCircle" to the console.
]]--
local function addDisplayShapeNamings()
for name,v in pairs(display) do
if (string.starts( name, "new" ) and type(v) == "function" and name ~= "newGroups") then
local oldFunc = display[ name ]
display[ name ] = function( ... )
local displayObj = oldFunc( unpack( arg ) )
displayObj.type = name
return displayObj
end
end
end
end
addDisplayShapeNamings()
local function testAddDisplayShapeNamings()
local group = display.newGroup()
local circle = display.newCircle( 0, 0, 100 )
local rect = display.newRect( 0, 0, 200, 100 )
local roundedrect = display.newRoundedRect( 0, 0, 200, 100, 50 )
print( group.type )
print( circle.type )
print( rect.type )
print( roundedrect.type )
group:removeSelf()
circle:removeSelf()
rect:removeSelf()
roundedrect:removeSelf()
end
-- testAddDisplayShapeNamings()
local function addLocalToContentOverride( displayObj )
local oldFunc = displayObj.contentToLocal
displayObj.contentToLocal = function( x, y, rotation )
local _x, _y = oldFunc( displayObj, x, y )
-- perform rotation to local here
return _x, _y
end
end
local function addShapeOverrides()
for i=1, #newShapeList do
local funcName = newShapeList[i]
local funcOld = display[ funcName ]
local function funcNew( ... )
local displayObj = oldFunc( unpack( arg ) )
addLocalToContentOverride( displayObj )
return object
end
display[ funcName ] = funcNew
end
end
--addShapeOverrides()
function display.newRadialFillCircle( parent, x, y, r, c )
local group = display.newGroup()
group.x, group.y = x, y
parent = parent or display.currentStage
parent:insert( group )
local ringinc = 100/(r/4)
local a = 0
for i=r, 2, -4 do
local circle = display.newCircle( group, 0, 0, i-2 )
circle.fill = {0,0,0,0}
circle.strokeWidth = 4
a = a + ringinc
c[4] = a/100
circle.stroke = c
end
return group
end
function display.newDonut( ... )
local parent, x, y, width
if (#arg == 3) then
x, y, width = unpack( arg )
elseif (#arg == 4) then
parent, x, y, width = unpack( arg )
end
local g = display.newGroup()
g.x, g.y = x, y
if (parent) then
parent:insert( g )
end
local b = display.newCircle( g, 0, 0, width/6+1 )
b.fill = {1,1,1,0}
b.stroke = {0,0,0,0}
b.strokeWidth = 0
local a = display.newCircle( g, 0, 0, math.floor(width/3) )
a.fill = {0,0,0,0}
a.stroke = {1,1,1}
a.strokeWidth = (width/3)
function g:setFill( r, g, _b, a )
b.fill = { r, g, _b, a }
end
function g:setStroke( r, g, b, _a )
a.stroke = { r, g, b, _a }
end
g.width, g.height = width, width
return g
end
function display.newDonutRect( ... )
local parent, x, y, width, height
if (#arg == 4) then
x, y, width, height = unpack( arg )
elseif (#arg == 5) then
parent, x, y, width, height = unpack( arg )
end
if (height > width) then
height = width
end
local stroke = height/3
local w, h = width-math.floor(stroke), height-math.floor(stroke)
local r = h/2
local g = display.newGroup()
g.x, g.y = x, y
if (parent) then
parent:insert( g )
end
local fill = display.newRoundedRect( g, 0, 0, math.ceil(w-stroke)+2, math.ceil(h-stroke)+2, r )
fill.fill = {1,1,1,0}
fill.stroke = {0,0,0,0}
fill.strokeWidth = 0
local rect = display.newRoundedRect( g, 0, 0, math.floor(w), math.floor(h), math.floor(r) )
rect.fill = {0,0,0,0}
rect.stroke = {1,1,1,1}
rect.strokeWidth = math.floor(stroke)
function g:setFill( r, g, _b, a )
fill.fill = { r, g, _b, a }
end
function g:setStroke( r, g, b, _a )
rect.stroke = { r, g, b, _a }
end
g.width, g.height = width, height
return g
end
function display.newHoleBar( ... )
local parent, x, y, width, count
if (#arg == 4) then
x, y, width, count = unpack( arg )
elseif (#arg == 5) then
parent, x, y, width, count = unpack( arg )
end
local g = display.newGroup()
g.x, g.y = x, y
if (parent) then
parent:insert( g )
end
local s = display.newDonutRect( g, 0, 0, width+(count-1)*((width/3)*2), width )
s:setFill( 0,0,0,0 )
s:setStroke( 1,1,1 )
local d = display.newDonut( g, 0, 0, width )
d.x, d.y = s.x-s.width/2+d.width/2, s.y
d:setStroke( 1,1,1 )
local step = (d.width/3)*2
for i=2, count do
local e = display.newDonut( g, 0, 0, width )
e.x = g[i].x + step
e:setStroke( 1,1,1 )
end
function g:setFill( r, _g, b, a )
for i=1, g.numChildren do
g[i]:setFill(r,_g,b,a)
end
end
function g:setStroke( r, _g, b, a )
for i=1, g.numChildren do
g[i]:setStroke(r,_g,b,a)
end
end
return g
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment