Difference between revisions of "Sprite:setStyle"

From GiderosMobile
(Created page with "__NOTOC__ '''Available since:''' Gideros 2022.6<br/> '''Class:''' Sprite<br/> === Description === Attaches a style table to a Sprite and mark it as dirty. This means that...")
 
 
Line 44: Line 44:
 
local spriteStyle={
 
local spriteStyle={
 
__Parent=stageStyle, --Inherits from 'stageStyle'
 
__Parent=stageStyle, --Inherits from 'stageStyle'
color1="=baseColor:brightness:1.1",
+
color1="=baseColor:brightness:1.1", -- alpha, mix, brightness
 
color2="=baseColor:brightness:0.8",
 
color2="=baseColor:brightness:0.8",
 
color3="=baseColor:brightness:0.0",
 
color3="=baseColor:brightness:0.0",

Latest revision as of 21:27, 17 October 2025

Available since: Gideros 2022.6
Class: Sprite

Description

Attaches a style table to a Sprite and mark it as dirty. This means that a Sprite can now be given style references instead of plain values. Gideros will resolve the references to actual values thanks to Style tables at runtime.

Sprite:setStyle(style)
Currently styles are handled for Sprite layout constraints, layout parameters and for Pixel color (more may come in the future)

Parameters

style: (table) the styling table

Examples

local style={
	cellColor=vector(1,0,0,1) --cellColor is Red initially
}
for i=1,10 do
	local p=Pixel.new(vector(0,0,0,0),10,10)	
	p:setX((i-1)*15)
	p:setStyle(style)
	p:setColor("cellColor")
	stage:addChild(p)
end
Core.asyncCall(function()
	Core.yield(3) --Wait 3s
	style.cellColor=vector(0,1,0,1) --Set cellColor to green
	for i=1,10 do
		stage:getChildAt(i):setStyle(style)
		print(stage:getChildAt(i):resolveStyle("cellColor",style))
	end
end)

A checker board

local stageStyle={
	baseColor=ColorValue(0.7,0.6,0,1)
}
 
local spriteStyle={
	__Parent=stageStyle, --Inherits from 'stageStyle'
	color1="=baseColor:brightness:1.1", -- alpha, mix, brightness
	color2="=baseColor:brightness:0.8",
	color3="=baseColor:brightness:0.0",
}
for x=1,4 do
	for y=1,4 do
		local gradient=Pixel.new(0xFFFFFF,1,32,32)
		gradient:setPosition(128+x*32,128+y*32)
		gradient:setStyle(spriteStyle)
--		gradient:setColor("color"..(1+((x+y)%2))) -- alternates between color1 and color2
		gradient:setColor("color"..(1+((x+y)%3))) -- alternates between color1, color2 and color3
		stage:addChild(gradient)
	end
end
 
stage:addEventListener(Event.MOUSE_DOWN,function() 
	stageStyle.baseColor=ColorValue(math.random(),math.random(),math.random(),1)
	stage:setStyle(stageStyle,true)
end)