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)