Difference between revisions of "Pixel"

From GiderosMobile
 
(23 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
<languages />
 
 
<!-- GIDEROSOBJ:Pixel -->
 
<!-- GIDEROSOBJ:Pixel -->
'''<translate>Supported platforms</translate>:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform mac.png]][[File:Platform pc.png]][[File:Platform html5.png]][[File:Platform winrt.png]][[File:Platform win32.png]]<br/>
+
'''Supported platforms:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform mac.png]][[File:Platform pc.png]][[File:Platform html5.png]][[File:Platform winrt.png]][[File:Platform win32.png]]<br/>
'''<translate>Available since</translate>:''' Gideros 2016.06<br/>
+
'''Available since:''' Gideros 2016.06<br/>
'''<translate>Inherits from</translate>:''' [[Special:MyLanguage/Sprite|Sprite]]<br/>
+
'''Inherits from:''' [[Sprite]]<br/>
=== <translate>Description</translate> ===
+
 
<translate>A rectangular Sprite which can be filled with solid colors, gradients or textures.
+
=== Description ===
Pixel aims at being a simpler and faster alternative to Shape when needing to display a coloured box or box with a gradient. It is also useful as Bitmap replacement since every texture will be fitted into Pixel dimensions automatically.</translate>
+
A rectangular Sprite which can be filled with solid colors, gradients or textures.
 +
Pixel aims at being a simpler and faster alternative to Shape when needing to display a coloured box or box with a gradient. It is also useful as Bitmap replacement since every texture will be fitted into Pixel dimensions automatically.
 +
 
 +
=== Examples ===
 +
<syntaxhighlight lang="lua">
 +
local mypixel = Pixel.new(0x0000FF, 0.75, 128, 128)
 +
mypixel:setAnchorPoint(0.5, 0.5)
 +
mypixel:setPosition(application:getContentWidth() / 2, 64)
 +
 
 +
stage:addChild(mypixel)
 +
</syntaxhighlight>
 +
 
 +
<syntaxhighlight lang="lua">
 +
application:setBackgroundColor(0x323232)
 +
local p = Pixel.new(0xffffff, 1, 32, 32)
 +
 +
p:set("redMultiplier", 1) -- OK
 +
p:set("greenMultiplier", 0) -- OK
 +
p:set("blueMultiplier", 0) -- OK
 +
p:set("alphaMultiplier", 1) -- OK
 +
--p:set("anchorX", .5) -- NOT OK
 +
--p:set("anchorY", .5) -- NOT OK
 +
p:set("anchorX", 16) -- OK
 +
p:set("anchorY", 16) -- OK
 +
p:set("alpha", .5) -- OK
 +
p:set("scaleX", 1.5) -- OK
 +
p:set("rotation", 10) -- OK
 +
p:set("x", 32) -- OK
 +
p:set("y", 32) -- OK
 +
 
 +
stage:addChild(p)
 +
</syntaxhighlight>
 +
 
 +
'''Moving stars in a gradient sky'''
 +
<syntaxhighlight lang="lua">
 +
-- a gradient bg
 +
local bggradient = Pixel.new(0xffffff, 1, 800, 480) -- set a white color to start with
 +
bggradient:setColor(0x0, 1, 0x00007f, 1, -90) -- a gradient set horizontally
 +
bggradient:setAnchorPoint(0.5, 0.5)
 +
-- stars
 +
local spritestars = Sprite.new() -- a sprite to hold all star shapes
 +
for i = 1, 512 do -- @PaulH
 +
local s = Shape.new()
 +
s:setLineStyle(2, 0xffffff)
 +
s:moveTo(0, 0)
 +
s:lineTo(1, 1)
 +
s:endPath()
 +
s:setPosition(math.random(0, 800), math.random(0, 480))
 +
s:setScale(math.random(5, 15) / 10)
 +
s:setAlpha(math.random(10, 50) / 50)
 +
spritestars:addChild(s)
 +
end
 +
-- render stars to a render target
 +
--local rtstars = RenderTarget.new(800, 480, nil, true, nil, nil, nil, false) -- BUG?
 +
local rtstars = RenderTarget.new(800, 480)
 +
rtstars:draw(spritestars)
 +
rtstars:save("|T|bgstars.png") -- TO FIX BUG
 +
-- create an image (Pixel) of the stars
 +
--local bgstars = Pixel.new(rtstars, 800, 480) -- BUG?
 +
local texstars = Texture.new("|T|bgstars.png", nil, {wrap=TextureBase.REPEAT, extend=false}) -- TO FIX BUG
 +
local bgstars = Pixel.new(texstars, 800, 480) -- TO FIX BUG
 +
bgstars:setColorTransform(7/255, 221/255, 219/255, 1) -- stars color
 +
bgstars:setAnchorPoint(0.5, 0.5)
 +
local texscale = 0.7
 +
bgstars:setTextureScale(texscale, texscale)
 +
-- position
 +
bggradient:setPosition(800/2, 480/2)
 +
bgstars:setPosition(800/2, 480/2)
 +
-- order
 +
stage:addChild(bggradient)
 +
stage:addChild(bgstars)
 +
 
 +
-- game loop
 +
local velx = 0
 +
function onEnterFrame(e)
 +
-- move the stars
 +
velx -= e.deltaTime * 128
 +
bgstars:setTexturePosition(velx, 0)
 +
end
 +
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
 +
</syntaxhighlight>
 +
 
 
{|-
 
{|-
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
=== <translate>Methods</translate> ===
+
 
[[Special:MyLanguage/Pixel.new|Pixel.new]] ''<translate>Create new pixel</translate>''<br/><!-- GIDEROSMTD:Pixel.new -->
+
=== Methods ===
[[Special:MyLanguage/Pixel.new|Pixel.new]] ''<translate>Constructor to create a Pixel with texture in letterbox mode.</translate>''<br/><!-- GIDEROSMTD:Pixel.new -->
+
[[Pixel.new]] ''creates a new Pixel''<br/><!--GIDEROSMTD:Pixel.new(color,alpha,width,height) creates a new Pixel-->
[[Special:MyLanguage/Pixel:getColor|Pixel:getColor]] ''<translate>Gets the color(s) of the Pixel</translate>''<br/><!-- GIDEROSMTD:Pixel:getColor -->
+
[[Pixel.new]] ''constructor to create a Pixel with texture in letterbox mode''<br/><!--GIDEROSMTD:Pixel.new(texture,width,height,texture_scale_x,texture_scale_y,texture_x,texture_y) constructor to create a Pixel with texture in letterbox mode-->
[[Special:MyLanguage/Pixel:getDimensions|Pixel:getDimensions]] <br/><!-- GIDEROSMTD:Pixel:getDimensions -->
+
[[Pixel:getColor]] ''returns the color(s) of the Pixel''<br/><!--GIDEROSMTD:Pixel:getColor() gets the color(s) of the Pixel-->
[[Special:MyLanguage/Pixel:getTexturePosition|Pixel:getTexturePosition]] <br/><!-- GIDEROSMTD:Pixel:getTexturePosition -->
+
[[Pixel:getDimensions]] ''returns the current size of the Pixel''<br/><!--GIDEROSMTD:Pixel:getDimensions() returns the current size of the Pixel-->
[[Special:MyLanguage/Pixel:getTextureScale|Pixel:getTextureScale]] <br/><!-- GIDEROSMTD:Pixel:getTextureScale -->
+
[[Pixel:getTexturePosition]] ''returns the texture position''<br/><!--GIDEROSMTD:Pixel:getTexturePosition() returns the texture position-->
[[Special:MyLanguage/Pixel:setColor|Pixel:setColor]] ''<translate>Sets the color of the Pixel</translate>''<br/><!-- GIDEROSMTD:Pixel:setColor -->
+
[[Pixel:getTextureScale]] ''returns the texture scale''<br/><!--GIDEROSMTD:Pixel:getTextureScale() returns the texture scale-->
[[Special:MyLanguage/Pixel:setColor|Pixel:setColor]] <br/><!-- GIDEROSMTD:Pixel:setColor -->
+
[[Pixel:setColor]] ''sets the color of the Pixel''<br/><!--GIDEROSMTD:Pixel:setColor(color,alpha) sets the color of the Pixel-->
[[Special:MyLanguage/Pixel:setColor|Pixel:setColor]] ''<translate>Sets 4-colour gradient.</translate>''<br/><!-- GIDEROSMTD:Pixel:setColor -->
+
[[Pixel:setColor]] ''sets a gradient''<br/><!--GIDEROSMTD:Pixel:setColor(color1,alpha1,color2,alpha2,angle) sets a gradient-->
[[Special:MyLanguage/Pixel:setDimensions|Pixel:setDimensions]] ''<translate>Sets both width and height of the Pixel.</translate>''<br/><!-- GIDEROSMTD:Pixel:setDimensions -->
+
[[Pixel:setColor]] ''sets a 4-colour gradient''<br/><!--GIDEROSMTD:Pixel:setColor(color1,alpha1,color2,alpha2,color3,alpha3,color4,alpha4) sets a 4-colour gradient-->
[[Special:MyLanguage/Pixel:setHeight|Pixel:setHeight]] ''<translate>Sets the height of the pixel sprite.</translate>''<br/><!-- GIDEROSMTD:Pixel:setHeight -->
+
[[Pixel:setDimensions]] ''sets both width and height of the Pixel''<br/><!--GIDEROSMTD:Pixel:setDimensions(w,h) sets both width and height of the Pixel-->
[[Special:MyLanguage/Pixel:setTexture|Pixel:setTexture]] <br/><!-- GIDEROSMTD:Pixel:setTexture -->
+
[[Pixel:setHeight]] ''sets the height of the Pixel sprite''<br/><!--GIDEROSMTD:Pixel:setHeight(h) sets the height of the Pixel sprite-->
[[Special:MyLanguage/Pixel:setTextureMatrix|Pixel:setTextureMatrix]] <br/><!-- GIDEROSMTD:Pixel:setTextureMatrix -->
+
[[Pixel:setNinePatch]] ''sets a 9-patch style texture rendering''<br/><!--GIDEROSMTD:Pixel:setNinePatch(vl,vr,vt,vb,tl,tr,tt,tb) sets a 9-patch style texture rendering-->
[[Special:MyLanguage/Pixel:setTexturePosition|Pixel:setTexturePosition]] <br/><!-- GIDEROSMTD:Pixel:setTexturePosition -->
+
[[Pixel:setTexture]] ''attaches a texture to the Pixel''<br/><!--GIDEROSMTD:Pixel:setTexture(texture,slot,matrix) attaches a texture to the Pixel-->
[[Special:MyLanguage/Pixel:setTextureScale|Pixel:setTextureScale]] <br/><!-- GIDEROSMTD:Pixel:setTextureScale -->
+
[[Pixel:setTextureMatrix]] ''specifies a transform matrix for the texture''<br/><!--GIDEROSMTD:Pixel:setTextureMatrix(matrix) specifies a transform matrix for the texture-->
[[Special:MyLanguage/Pixel:setWidth|Pixel:setWidth]] ''<translate>Sets the width of the pixel sprite.</translate>''<br/><!-- GIDEROSMTD:Pixel:setWidth -->
+
[[Pixel:setTexturePosition]] ''sets a texture position''<br/><!--GIDEROSMTD:Pixel:setTexturePosition(x,y) sets a texture position-->
 +
[[Pixel:setTextureScale]] ''sets a texture scale''<br/><!--GIDEROSMTD:Pixel:setTextureScale(sx,sy) sets a texture scale-->
 +
[[Pixel:setWidth]] ''sets the width of the Pixel sprite''<br/><!--GIDEROSMTD:Pixel:setWidth(w) sets the width of the Pixel sprite-->
 +
 
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
=== <translate>Events</translate> ===
+
 
=== <translate>Constants</translate> ===
+
=== Events ===
 +
=== Constants ===
 
|}
 
|}
 +
 +
{{GIDEROS IMPORTANT LINKS}}

Latest revision as of 02:41, 19 July 2023

Supported platforms: Platform android.pngPlatform ios.pngPlatform mac.pngPlatform pc.pngPlatform html5.pngPlatform winrt.pngPlatform win32.png
Available since: Gideros 2016.06
Inherits from: Sprite

Description

A rectangular Sprite which can be filled with solid colors, gradients or textures. Pixel aims at being a simpler and faster alternative to Shape when needing to display a coloured box or box with a gradient. It is also useful as Bitmap replacement since every texture will be fitted into Pixel dimensions automatically.

Examples

local mypixel = Pixel.new(0x0000FF, 0.75, 128, 128)
mypixel:setAnchorPoint(0.5, 0.5)
mypixel:setPosition(application:getContentWidth() / 2, 64)

stage:addChild(mypixel)
application:setBackgroundColor(0x323232)
local p = Pixel.new(0xffffff, 1, 32, 32)
 
p:set("redMultiplier", 1) -- OK
p:set("greenMultiplier", 0) -- OK
p:set("blueMultiplier", 0) -- OK
p:set("alphaMultiplier", 1) -- OK
--p:set("anchorX", .5) -- NOT OK
--p:set("anchorY", .5) -- NOT OK
p:set("anchorX", 16) -- OK
p:set("anchorY", 16) -- OK
p:set("alpha", .5) -- OK
p:set("scaleX", 1.5) -- OK
p:set("rotation", 10) -- OK
p:set("x", 32) -- OK
p:set("y", 32) -- OK

stage:addChild(p)

Moving stars in a gradient sky

-- a gradient bg
local bggradient = Pixel.new(0xffffff, 1, 800, 480) -- set a white color to start with
bggradient:setColor(0x0, 1, 0x00007f, 1, -90) -- a gradient set horizontally
bggradient:setAnchorPoint(0.5, 0.5)
-- stars
local spritestars = Sprite.new() -- a sprite to hold all star shapes
for i = 1, 512 do -- @PaulH
	local s = Shape.new()
	s:setLineStyle(2, 0xffffff)
	s:moveTo(0, 0)
	s:lineTo(1, 1)
	s:endPath()
	s:setPosition(math.random(0, 800), math.random(0, 480))
	s:setScale(math.random(5, 15) / 10)
	s:setAlpha(math.random(10, 50) / 50)
	spritestars:addChild(s)
end
-- render stars to a render target
--local rtstars = RenderTarget.new(800, 480, nil, true, nil, nil, nil, false) -- BUG?
local rtstars = RenderTarget.new(800, 480)
rtstars:draw(spritestars)
rtstars:save("|T|bgstars.png") -- TO FIX BUG
-- create an image (Pixel) of the stars
--local bgstars = Pixel.new(rtstars, 800, 480) -- BUG?
local texstars = Texture.new("|T|bgstars.png", nil, {wrap=TextureBase.REPEAT, extend=false}) -- TO FIX BUG
local bgstars = Pixel.new(texstars, 800, 480) -- TO FIX BUG
bgstars:setColorTransform(7/255, 221/255, 219/255, 1) -- stars color
bgstars:setAnchorPoint(0.5, 0.5)
local texscale = 0.7
bgstars:setTextureScale(texscale, texscale)
-- position
bggradient:setPosition(800/2, 480/2)
bgstars:setPosition(800/2, 480/2)
-- order
stage:addChild(bggradient)
stage:addChild(bgstars)

-- game loop
local velx = 0
function onEnterFrame(e)
	-- move the stars
	velx -= e.deltaTime * 128
	bgstars:setTexturePosition(velx, 0)
end
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)

Methods

Pixel.new creates a new Pixel
Pixel.new constructor to create a Pixel with texture in letterbox mode
Pixel:getColor returns the color(s) of the Pixel
Pixel:getDimensions returns the current size of the Pixel
Pixel:getTexturePosition returns the texture position
Pixel:getTextureScale returns the texture scale
Pixel:setColor sets the color of the Pixel
Pixel:setColor sets a gradient
Pixel:setColor sets a 4-colour gradient
Pixel:setDimensions sets both width and height of the Pixel
Pixel:setHeight sets the height of the Pixel sprite
Pixel:setNinePatch sets a 9-patch style texture rendering
Pixel:setTexture attaches a texture to the Pixel
Pixel:setTextureMatrix specifies a transform matrix for the texture
Pixel:setTexturePosition sets a texture position
Pixel:setTextureScale sets a texture scale
Pixel:setWidth sets the width of the Pixel sprite

Events

Constants