Sprite:setGhosts
From GiderosMobile
Available since: Gideros 2023.9
Class: Sprite
Description
Ghosts are kind of lightweight Sprites: you tell Gideros to render the same real Sprite with slight variations. Not everything is possible, but it currently supports changing the layout position (gridx/gridy in layout constraints), the color of a pixel, the text and color of a textfield. More will be added in the future.
Ghosts only take a hundred of bytes of memory, since they share the rest of the settings with the original real Sprite. With this system, Gideros can now render a table of several millions of cells at a fraction of the memory cost of full Sprites.
Sprite:setGhosts(ghosts)
The ghosts table can contain the following fields:
- model: specify the ghost model
- gridx: the 0-based index of the column the ghost will be placed into
- gridy: the 0-based index of the row the ghost will be placed into
- color: the color of the ghost
- text: the text of the ghost
- children: table holding ghost children
- color: the color of the child ghost
- text: the text of the child ghost
- transform: matrix to set the ghost transform (position, rotation, scale, ...) since Gideros 2023.9.1
Specifying a nil table will clear the ghosts
Parameters
ghosts: (table) table of ghosts
Examples
Grid
--Layouts are computed from stage, configure a single sub cell, taking all space
stage:setLayoutParameters({rowWeights={1},columnWeights={1}})
--Make a grid of 24 rows, 8 columns with cells of 40x20 units
local rn, cn = 24, 8
local grid = Pixel.new()
local rh, cw = {}, {}
for i = 1, rn do rh[#rh+1] = 40 end
for i = 1, cn do cw[#cw+1] = 20 end
grid:setLayoutParameters({rowHeights=rh, columnWidths=cw})
grid:setLayoutConstraints({fill=1})
stage:addChild(grid)
--Create cell template
cell=Pixel.new()
cell:setLayoutConstraints({fill=1})
local ic=Pixel.new(0,1,10,10)
ic:setPosition(5,5)
cell:addChild(ic)
local tf=TextField.new(nil,"",{ flags=FontBase.TLF_REF_TOP|FontBase.TLF_VCENTER|FontBase.TLF_CENTER, w=20, h=10})
tf:setPosition(20,5)
cell:addChild(tf)
--Create ghosts
local ghosts={}
for i=1,rn do
for j=1,cn do
ghosts[#ghosts+1]={
model=cell, -- Specify ghost model
gridx=j-1, gridy=i-1, -- Place this ghost in the grid
color=math.random(0xFFFFFF), -- A random color for this cell
children={
{ color=math.random(0xFFFFFF), }, -- First child is the icon (ic), set a random color
{ text=math.random(99) }, -- Second child is the textfield, set a random number
}
}
end
end
--Apply ghosts
grid:setGhosts(ghosts)
Repeating text
local originsprite = Sprite.new()
local tf = TextField.new(nil, "")
tf:setScale(4)
tf:setPosition(1, 1)
-- position
originsprite:setPosition(64*2, 64*2)
-- order
originsprite:addChild(tf)
stage:addChild(originsprite)
-- create ghosts
local ghosts = {}
for i = 1, 8 do
ghosts[#ghosts+1] = {
model = tf, -- specify ghost model
-- gridx = 1, gridy = i+1, -- place this ghost in the grid
color=math.random(0xFFFFFF), -- a random color for this cell
-- children={
-- { color=math.random(0xFFFFFF), }, -- First child is the icon (ic), set a random color
-- { text=math.random(99) }, -- Second child is the textfield, set a random number
-- }
text="Gideros",
transform=Matrix.new(8, math.tan(5), 0, 8, 0, i*32),
}
end
-- apply ghosts
originsprite:setGhosts(ghosts)
- Sprite:addChild
- Sprite:addChildAt
- Sprite:clearBlendMode
- Sprite:clone
- Sprite:contains
- Sprite:get
- Sprite:getAlpha
- Sprite:getAnchorPoint
- Sprite:getAnchorPosition
- Sprite:getBounds
- Sprite:getChildAt
- Sprite:getChildIndex
- Sprite:getChildrenAtPoint
- Sprite:getClip
- Sprite:getColorTransform
- Sprite:getDimensions
- Sprite:getDrawCount
- Sprite:getHeight
- Sprite:getLayoutConstraints
- Sprite:getLayoutInfo
- Sprite:getLayoutParameters
- Sprite:getMatrix
- Sprite:getNumChildren
- Sprite:getParent
- Sprite:getPosition
- Sprite:getRotation
- Sprite:getRotationX
- Sprite:getRotationY
- Sprite:getScale
- Sprite:getScaleX
- Sprite:getScaleY
- Sprite:getScaleZ
- Sprite:getSize
- Sprite:getSkew
- Sprite:getSkewX
- Sprite:getSkewY
- Sprite:getWidth
- Sprite:getX
- Sprite:getY
- Sprite:getZ
- Sprite:globalToLocal
- Sprite:hitTestPoint
- Sprite:isVisible
- Sprite:localToGlobal
- Sprite:redrawEffects
- Sprite:removeChild
- Sprite:removeChildAt
- Sprite:removeFromParent
- Sprite:set
- Sprite:setAlpha
- Sprite:setAnchorPoint
- Sprite:setAnchorPosition
- Sprite:setBlendMode
- Sprite:setClip
- Sprite:setColorTransform
- Sprite:setDimensions
- Sprite:setEffectConstant
- Sprite:setEffectStack
- Sprite:setGhosts
- Sprite:setLayoutConstraints
- Sprite:setLayoutParameters
- Sprite:setMatrix
- Sprite:setPosition
- Sprite:setRotation
- Sprite:setRotationX
- Sprite:setRotationY
- Sprite:setScale
- Sprite:setScaleX
- Sprite:setScaleY
- Sprite:setScaleZ
- Sprite:setShader
- Sprite:setShaderConstant
- Sprite:setSkew
- Sprite:setSkewX
- Sprite:setSkewY
- Sprite:setStencilOperation
- Sprite:setStopEventPropagation
- Sprite:setVisible
- Sprite:setX
- Sprite:setY
- Sprite:setZ
- Sprite:spriteToLocal
- Sprite:swapChildren
- Sprite:swapChildrenAt
- Sprite.ADD
- Sprite.ALPHA
- Sprite.LAYOUT ANCHOR CENTER
- Sprite.LAYOUT ANCHOR EAST
- Sprite.LAYOUT ANCHOR NORTH
- Sprite.LAYOUT ANCHOR NORTHEAST
- Sprite.LAYOUT ANCHOR NORTHWEST
- Sprite.LAYOUT ANCHOR SOUTH
- Sprite.LAYOUT ANCHOR SOUTHEAST
- Sprite.LAYOUT ANCHOR SOUTHWEST
- Sprite.LAYOUT ANCHOR WEST
- Sprite.LAYOUT FILL BOTH
- Sprite.LAYOUT FILL HORIZONTAL
- Sprite.LAYOUT FILL NONE
- Sprite.LAYOUT FILL VERTICAL
- Sprite.MULTIPLY
- Sprite.NO ALPHA
- Sprite.SCREEN
- Sprite.new
- Event.ADDED_TO_STAGE
- Event.ENTER_FRAME
- Event.KEY_CHAR
- Event.KEY_DOWN
- Event.KEY_UP
- Event.LAYOUT_RESIZED
- Event.MOUSE_DOWN
- Event.MOUSE_ENTER
- Event.MOUSE_HOVER
- Event.MOUSE_LEAVE
- Event.MOUSE_MOVE
- Event.MOUSE_UP
- Event.MOUSE_WHEEL
- Event.REMOVED_FROM_STAGE
- Event.TOUCHES_BEGIN
- Event.TOUCHES_CANCEL
- Event.TOUCHES_END
- Event.TOUCHES_MOVE