Difference between revisions of "Sprite:setLayoutConstraints"

From GiderosMobile
(added the start of an example + fixed some typo)
 
(12 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
 +
'''Available since:''' Gideros 2018.9<br/>
 +
'''Class:''' [[Sprite]]<br/>
  
<languages />
+
=== Description ===
 +
Gideros layout system is heavily based on Java GridBagLayout principle. See here for more explanation '''https://www.math.uni-hamburg.de/doc/java/tutorial/uiswing/layout/gridbag.html'''
  
'''<translate>Available since</translate>:''' Gideros 2018.9
 
<br/>
 
  
'''<translate>Class</translate>:''' [[Special:MyLanguage/Sprite|Sprite]]
+
This function specifies the child placement rules within the grid defined on its parent by [[Sprite:setLayoutParameters]].
<br/>
+
<syntaxhighlight lang="lua">
 +
Sprite:setLayoutConstraints(constraints)
 +
</syntaxhighlight>
 +
 
 +
 
 +
Specifying a '''nil''' table will clear layout constraints.
 +
 
 +
'''When a Sprite is resized by the layout system, an ''Event.LAYOUT_RESIZED'' is triggered'''
 +
 
  
=== <translate>Description</translate> ===
+
'''TODO''': add/reorder/document missing fields from
<translate>
+
*'''https://github.com/gideros/gideros/blob/e75e76c2857f8d8892c2383cea97fff174ff9a96/2dsg/gridbaglayout.h#L220'''
This function specify the child placement rules within the grid defined on its parent by [[Special:MyLanguage/Sprite:setLayoutParameters|Sprite:setLayoutParameters]].
+
*'''https://github.com/gideros/gideros/blob/e75e76c2857f8d8892c2383cea97fff174ff9a96/luabinding/spritebinder.cpp#L786'''
<br/>
+
*'''...'''
  
Gideros layout system is heavily based on Java GridBagLayout principle. See here for more explanation:
 
https://www.math.uni-hamburg.de/doc/java/tutorial/uiswing/layout/gridbag.html
 
<br/>
 
  
 
The constraint table can contain the following fields:
 
The constraint table can contain the following fields:
* '''gridx''': The 0-based index of the column the child must be placed into
+
* '''gridx''': the 0-based index of the column the child must be placed into
* '''gridy''': The 0-based index of the row the child must be placed into
+
* '''gridy''': the 0-based index of the row the child must be placed into
* '''gridwidth''': The number of column this child will take
+
* '''gridwidth''': the number of column this child will take
* '''gridheight''': The number of row this child will take
+
* '''gridheight''': the number of row this child will take
* '''weightx''': The horizontal weight of the child. Used to distribute extra space among children
+
* '''gridRelative''': if '''gridx''' and '''gridy' are relative to the previous child
* '''weighty''': The vertical weight of the child. Used to distribute extra space among children
+
* '''weightx''': the horizontal weight of the child. Used to distribute extra space among children
* '''anchor''': Defines where the child should be placed inside its grid space.  
+
* '''weighty''': the vertical weight of the child. Used to distribute extra space among children
Can be [[Special:MyLanguage/Sprite.LAYOUT_ANCHOR_NORTHWEST|Sprite.LAYOUT_ANCHOR_NORTHWEST]], [[Special:MyLanguage/Sprite.LAYOUT_ANCHOR_NORTH|Sprite.LAYOUT_ANCHOR_NORTH]], [[Special:MyLanguage/Sprite.LAYOUT_ANCHOR_NORTHEAST|Sprite.LAYOUT_ANCHOR_NORTHEAST]],  
+
* '''anchor''': defines where the child should be placed inside its grid space. Can be: [[Sprite.LAYOUT_ANCHOR_NORTHWEST]], [[Sprite.LAYOUT_ANCHOR_NORTH]], [[Sprite.LAYOUT_ANCHOR_NORTHEAST]], [[Sprite.LAYOUT_ANCHOR_WEST]], [[Sprite.LAYOUT_ANCHOR_CENTER]], [[Sprite.LAYOUT_ANCHOR_EAST]], [[Sprite.LAYOUT_ANCHOR_SOUTHWEST]], [[Sprite.LAYOUT_ANCHOR_SOUTH]] or [[Sprite.LAYOUT_ANCHOR_SOUTHEAST]]
[[Special:MyLanguage/Sprite.LAYOUT_ANCHOR_WEST|Sprite.LAYOUT_ANCHOR_WEST]], [[Special:MyLanguage/Sprite.LAYOUT_ANCHOR_CENTER|Sprite.LAYOUT_ANCHOR_CENTER]], [[Special:MyLanguage/Sprite.LAYOUT_ANCHOR_EAST|Sprite.LAYOUT_ANCHOR_EAST]],  
+
* '''anchorx''': specifies relative placement of the child on X axis. anchor mustn't be set '''since 2020.7'''
[[Special:MyLanguage/Sprite.LAYOUT_ANCHOR_SOUTHWEST|Sprite.LAYOUT_ANCHOR_SOUTHWEST]], [[Special:MyLanguage/Sprite.LAYOUT_ANCHOR_SOUTH|Sprite.LAYOUT_ANCHOR_SOUTH]] or [[Special:MyLanguage/Sprite.LAYOUT_ANCHOR_SOUTHEAST|Sprite.LAYOUT_ANCHOR_SOUTHEAST]]
+
* '''anchory''': specifies relative placement of the child on Y axis. anchor mustn't be set '''since 2020.7'''
* '''fill''': In which directions the child should be expanded to fit the grid space.
+
* '''fill''': in which directions the child should be expanded to fit the grid space. Can be: [[Sprite.LAYOUT_FILL_NONE]], [[Sprite.LAYOUT_FILL_HORIZONTAL]], [[Sprite.LAYOUT_FILL_VERTICAL]] or [[Sprite.LAYOUT_FILL_BOTH]]
Can be [[Special:MyLanguage/Sprite.LAYOUT_FILL_NONE|Sprite.LAYOUT_FILL_NONE]], [[Special:MyLanguage/Sprite.LAYOUT_FILL_HORIZONTAL|Sprite.LAYOUT_FILL_HORIZONTAL]], [[Special:MyLanguage/Sprite.LAYOUT_FILL_VERTICAL|Sprite.LAYOUT_FILL_VERTICAL]] or [[Special:MyLanguage/Sprite.LAYOUT_FILL_BOTH|Sprite.LAYOUT_FILL_BOTH]]
+
* '''fillX''': filling amount in X direction (replace '''fill''') 
* '''ipadx''': Internal horizontal padding
+
* '''fillY''': filling amount in Y direction (replace '''fill''') 
* '''ipady''': Internal vertical padding
+
* '''ipadx''': internal horizontal padding
* '''minWidth''': Minimum width
+
* '''ipady''': internal vertical padding
* '''minHeight''': Minimum height
+
* '''offsetx''': final offset applied to the child on X axis, in logical units '''since 2020.7'''
* '''prefWidth''': Preferred width
+
* '''offsety''': final offset applied to the child on Y axis, in logical units '''since 2020.7'''
* '''prefHeight''': Preferred height
+
* '''originx''': final offset applied to the child on X axis, as a fraction of the child width '''since 2020.7'''
 +
* '''originy''': final offset applied to the child on Y axis, as a fraction of the child height '''since 2020.7'''
 +
* '''minWidth''': minimum width
 +
* '''minHeight''': minimum height
 +
* '''prefWidth''': preferred width
 +
* '''prefHeight''': preferred height
 +
* '''width''': short hand for setting both minWidth and prefWidth
 +
* '''height''': short hand for setting both minHeight and prefHeight
 +
* '''extraw''': extends this sprite's width by the following amount
 +
* '''extrah''': extends this sprite's height by the following amount
 +
* '''shrink''': allows grid shrinking '''since 2021.2'''
 
* '''insetTop''': the top margin
 
* '''insetTop''': the top margin
 
* '''insetLeft''': the left margin
 
* '''insetLeft''': the left margin
 
* '''insetBottom''': the bottom margin
 
* '''insetBottom''': the bottom margin
 
* '''insetRight''': the right margin
 
* '''insetRight''': the right margin
<br/>
+
* '''insets''': sets the above four margins to the same value at once '''since 2020.7'''
 +
* '''overflowMode''': enable overflow mode
 +
* '''hidePriority''': hide this child by priority over others
 +
* '''aspectRatio''': when filling, restricts the computed size to fit the given aspect ratio
 +
* '''optimizeSize''': shrink back the child to its really used size
 +
* '''group''': layout all this sprite's children as if they were on this grid
 +
* '''autoClip''': clip this sprite according to computed bounds
 +
* '''contentAspectRatio''': when filling, restricts the computed size so that inner size fits the given aspect ratio
  
Specifying a '''nil''' table will clear layout constraints.
+
=== Parameters ===
<br/>
+
'''constraints''': (table) table of layout constraints<br/>
When a [[Special:MyLanguage/Sprite|Sprite]] is resized by the layout system, an ''Event.LAYOUT_RESIZED'' is triggered.
+
 
</translate>
+
=== Examples ===
 +
'''Example''': pixels
 +
 
 +
<gallery widths=136px heights=240px>Layout parameters example.png</gallery>
 +
<syntaxhighlight lang="lua">
 +
-- LAYOUT CONSTRAINTS @Nanocore
 +
-- BUTTONS HOLDER
 +
local myholder = Pixel.new(0x0, 0.5, 256, 256)
 +
myholder:setPosition(8, 8)
 +
myholder:setLayoutParameters({
 +
columnWeights = {1, 1, 1}, -- 3 columns
 +
rowWeights = {1, 1, 1, 1}, -- 4 rows
 +
columnWidths = {32, 32, 32}, -- min columns width
 +
rowHeights = {32, 32, 32, 32}, -- min rows height
 +
})
  
 +
-- BUTTONS
 +
local mybtn1 = Pixel.new(0xff0000, 1, 32, 32)
 +
mybtn1:setLayoutConstraints({
 +
gridx = 0,
 +
gridy = 0,
 +
gridwidth = 2,
 +
gridheight = 2,
 +
anchor = Sprite.LAYOUT_ANCHOR_NORTHWEST,
 +
fill = Sprite.LAYOUT_FILL_BOTH,
 +
})
 +
myholder:addChild(mybtn1)
  
<source lang="lua">
+
local mybtn2 = Pixel.new(0x00ff00, 1, 32, 32)
Sprite:setLayoutConstraints(constraints)
+
mybtn2:setLayoutConstraints({
</source>
+
gridx = 1,
 +
gridy = 2,
 +
gridwidth = 1,
 +
anchor = Sprite.LAYOUT_ANCHOR_NORTHWEST,
 +
fill = Sprite.LAYOUT_FILL_VERTICAL,
 +
})
 +
myholder:addChild(mybtn2)
  
=== <translate>Parameters</translate> ===
+
local mybtn3 = Pixel.new(0x0000ff, 1, 32, 32)
'''constraints''': (table) <translate>Table of layout constraints.</translate>
+
mybtn3:setLayoutConstraints({
<br/>
+
gridx = 2,
 +
gridy = 3,
 +
gridwidth = 1,
 +
anchor = Sprite.LAYOUT_ANCHOR_NORTHWEST,
 +
fill = Sprite.LAYOUT_FILL_BOTH,
 +
})
 +
myholder:addChild(mybtn3)
  
=== <translate>Examples</translate> ===
+
-- ADD TO STAGE
'''Example''':
+
stage:addChild(myholder)
<br/>
+
</syntaxhighlight>
  
<source lang="lua">
+
=== See also ===
-- LAYOUT CONSTRAINTS @hgy29
+
'''[[Sprite:setLayoutParameters]]'''
local ticket = Bitmap.new(Texture.new("gfx/vip/ticket.png"))
 
local text = Bitmap.new(Texture.new("gfx/vip/text.png"))
 
 
-- Use auto layout on ticket to center text automatically
 
ticket:setLayoutParameters{ rowWeights = {1}, columnWeights = {1} }
 
text:setLayoutConstraints{ }
 
  
ticket:addChild(text)
+
{{Sprite}}
stage:addChild(ticket)
 
</source>
 
<br/>
 
<br/>
 

Latest revision as of 12:10, 6 November 2024

Available since: Gideros 2018.9
Class: Sprite

Description

Gideros layout system is heavily based on Java GridBagLayout principle. See here for more explanation https://www.math.uni-hamburg.de/doc/java/tutorial/uiswing/layout/gridbag.html


This function specifies the child placement rules within the grid defined on its parent by Sprite:setLayoutParameters.

Sprite:setLayoutConstraints(constraints)


Specifying a nil table will clear layout constraints.

When a Sprite is resized by the layout system, an Event.LAYOUT_RESIZED is triggered


TODO: add/reorder/document missing fields from


The constraint table can contain the following fields:

  • gridx: the 0-based index of the column the child must be placed into
  • gridy: the 0-based index of the row the child must be placed into
  • gridwidth: the number of column this child will take
  • gridheight: the number of row this child will take
  • gridRelative: if gridx and gridy' are relative to the previous child
  • weightx: the horizontal weight of the child. Used to distribute extra space among children
  • weighty: the vertical weight of the child. Used to distribute extra space among children
  • anchor: defines where the child should be placed inside its grid space. Can be: Sprite.LAYOUT_ANCHOR_NORTHWEST, Sprite.LAYOUT_ANCHOR_NORTH, Sprite.LAYOUT_ANCHOR_NORTHEAST, Sprite.LAYOUT_ANCHOR_WEST, Sprite.LAYOUT_ANCHOR_CENTER, Sprite.LAYOUT_ANCHOR_EAST, Sprite.LAYOUT_ANCHOR_SOUTHWEST, Sprite.LAYOUT_ANCHOR_SOUTH or Sprite.LAYOUT_ANCHOR_SOUTHEAST
  • anchorx: specifies relative placement of the child on X axis. anchor mustn't be set since 2020.7
  • anchory: specifies relative placement of the child on Y axis. anchor mustn't be set since 2020.7
  • fill: in which directions the child should be expanded to fit the grid space. Can be: Sprite.LAYOUT_FILL_NONE, Sprite.LAYOUT_FILL_HORIZONTAL, Sprite.LAYOUT_FILL_VERTICAL or Sprite.LAYOUT_FILL_BOTH
  • fillX: filling amount in X direction (replace fill)
  • fillY: filling amount in Y direction (replace fill)
  • ipadx: internal horizontal padding
  • ipady: internal vertical padding
  • offsetx: final offset applied to the child on X axis, in logical units since 2020.7
  • offsety: final offset applied to the child on Y axis, in logical units since 2020.7
  • originx: final offset applied to the child on X axis, as a fraction of the child width since 2020.7
  • originy: final offset applied to the child on Y axis, as a fraction of the child height since 2020.7
  • minWidth: minimum width
  • minHeight: minimum height
  • prefWidth: preferred width
  • prefHeight: preferred height
  • width: short hand for setting both minWidth and prefWidth
  • height: short hand for setting both minHeight and prefHeight
  • extraw: extends this sprite's width by the following amount
  • extrah: extends this sprite's height by the following amount
  • shrink: allows grid shrinking since 2021.2
  • insetTop: the top margin
  • insetLeft: the left margin
  • insetBottom: the bottom margin
  • insetRight: the right margin
  • insets: sets the above four margins to the same value at once since 2020.7
  • overflowMode: enable overflow mode
  • hidePriority: hide this child by priority over others
  • aspectRatio: when filling, restricts the computed size to fit the given aspect ratio
  • optimizeSize: shrink back the child to its really used size
  • group: layout all this sprite's children as if they were on this grid
  • autoClip: clip this sprite according to computed bounds
  • contentAspectRatio: when filling, restricts the computed size so that inner size fits the given aspect ratio

Parameters

constraints: (table) table of layout constraints

Examples

Example: pixels

-- LAYOUT CONSTRAINTS @Nanocore
-- BUTTONS HOLDER
local myholder = Pixel.new(0x0, 0.5, 256, 256)
myholder:setPosition(8, 8)
myholder:setLayoutParameters({
	columnWeights = {1, 1, 1}, -- 3 columns
	rowWeights = {1, 1, 1, 1}, -- 4 rows
	columnWidths = {32, 32, 32}, -- min columns width
	rowHeights = {32, 32, 32, 32}, -- min rows height
})

-- BUTTONS
local mybtn1 = Pixel.new(0xff0000, 1, 32, 32)
mybtn1:setLayoutConstraints({
	gridx = 0,
	gridy = 0,
	gridwidth = 2,
	gridheight = 2,
	anchor = Sprite.LAYOUT_ANCHOR_NORTHWEST,
	fill = Sprite.LAYOUT_FILL_BOTH,
})
myholder:addChild(mybtn1)

local mybtn2 = Pixel.new(0x00ff00, 1, 32, 32)
mybtn2:setLayoutConstraints({
	gridx = 1,
	gridy = 2,
	gridwidth = 1,
	anchor = Sprite.LAYOUT_ANCHOR_NORTHWEST,
	fill = Sprite.LAYOUT_FILL_VERTICAL,
})
myholder:addChild(mybtn2)

local mybtn3 = Pixel.new(0x0000ff, 1, 32, 32)
mybtn3:setLayoutConstraints({
	gridx = 2,
	gridy = 3,
	gridwidth = 1,
	anchor = Sprite.LAYOUT_ANCHOR_NORTHWEST,
	fill = Sprite.LAYOUT_FILL_BOTH,
})
myholder:addChild(mybtn3)

-- ADD TO STAGE
stage:addChild(myholder)

See also

Sprite:setLayoutParameters