Sprite:setGhosts

From GiderosMobile
Revision as of 21:17, 17 September 2023 by MoKaLux (talk | contribs) (Created page with "__NOTOC__ '''Available since:''' Gideros 2023.9<br/> '''Class:''' Sprite<br/> === Description === Ghosts are kind of lightweight Sprites: you tell Gideros to render the s...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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
  • children: table holding ghost children
    • color: the new color of the child ghost
    • text: the new text of the child ghost
Specifying a nil table will clear the ghosts

Parameters

ghosts: (table) table of ghosts

Example

--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)