CPLayoutSprite
This is an off-screen sprite that provides layout capabilities for other dynamic sprites. This sprite relies on provided layout specification that dictates how every sprite should be positioned in relation to the entire screen or other sprite.
Here are several important notes about how this sprite works:
- This sprite only deals with the XY-position of sprites and does NOT deal with Z-index or the size of the sprite. In other words, it never resizes the sprite it is laying out and expects that all constituent sprites have their size correctly set in CPSceneObject.update method.
- Only sprites that extend CPDynamicSprite can be using with this layout sprite.
- This sprite perform the layout on each frame.
- Laid out sprites can overlap.
Layout Specification
Layout specification is a string that consists of semi-colon separated commands. Each command starts with the sprite ID, followed by '=' character and a set of instructions for this sprite position.
Here's basic BNF form for the layout specification:
layout: decls* EOF;
decls
: decl
| decls decl
;
decl: ID '='' items ';';
items
: item
| items ','' item
;
item
: offItem
| xItem
| yItem
;
offItem: 'off' ':'' '[' NUM '.' NUM ']';
xItem: 'x' ':' ('same' | 'before' | 'left' | 'center' | 'right' | 'after') '(' ID? ')'';
yItem: 'y' ':' ('same' | 'above' | 'top' | 'center' | 'bottom' | 'below') '(' ID? ')';
NUM: '-'? [0-9] [0-9]*;
ID: [a-zA-Z0-9-_$]+;
Here's an example of the layout specification (from CPLayoutExample):
// This is the main panel centered on the screen.
Panel-1 = x: center(), y: center();
// These panels are placed in the 4 corners of the panel 1.
Panel-2 = off: [1, 0], x: left(Panel-1), y: top(Panel-1);
Panel-3 = off: [-1, 0], x: right(Panel-1), y: top(Panel-1);
Panel-4 = off: [1, 0], x: left(Panel-1), y: bottom(Panel-1);
Panel-5 = off: [-1, 0], x: right(Panel-1), y: bottom(Panel-1);
// Panels 6 and 7 go after each other.
Panel-6 = x: after(Panel-2), y: below(Panel-2);
Panel-7 = x: after(Panel-6), y: same(Panel-6);
// Centered image with 2-row offset.
img = off: [0, 2], x: center(Panel-1), y: center(Panel-1);
Notes:
- Each sprite is positioned by its top-left XY-coordinates.
- Offsets can be negative and are applied to the top-left XY-coordinates of the sprite.
- Default specification is 'off: [0,0], x: left(), y: top();'
- C-style comments are allowed in any place in the specification.
- 'left', 'right', 'top' and 'bottom' are referring to a relative position inside of the referral.
- 'before', 'after', 'above', and 'below' are referring to a relative position outside of the referral.
- '()' (empty brackets) refer to a entire scene screen as a referral "sprite".
- Only sprites extending CPDynamicSprite can be used in layout specification. However, any scene object can act as a referral.
- 'center' provides centering for either X or Y-coordinates.
- 'same' allows to use the same X or Y-coordinates as a referral.
- Use CPSpacerSprite to help fill up space in your layout.
UI Framework
Although CosPlay does not define an opinionated UI framework, several sprites and supporting classes are often used for constructing UI element on the screen. These include:
- CPLayoutSprite
- CPDynamicSprite
- CPLabelSprite
- CPSpacerSprite
- CPTitlePanelSprite
- CPListBoxSprite
- CPTextInputSprite
- CPSystemFont
You can can also look at the following UI-related examples:
- org.cosplay.examples.listbox.CPListBoxExample
- org.cosplay.examples.dialog.CPDialogExample
- org.cosplay.examples.layout.CPLayoutExample
- org.cosplay.examples.textinput.CPTextInputExample
Value parameters
- id
-
ID of this scene object.
- shaders
-
Optional sequence of shaders for this sprite. Default value is an empty sequence.
- spec
-
Layout specification as described above. Note that specification can be updated later using CPLayoutSprite.updateSpec method.
- tags
-
Optional set of organizational or grouping tags. By default, the empty set is used.
Attributes
- See also
- Example
-
See CPLayoutExample class for the example of using this sprite.
- Source
- CPLayoutSprite.scala
- Graph
-
- Supertypes
-
class CPOffScreenSpriteclass CPSceneObjecttrait CPLifecycleclass CPGameObjectclass Objecttrait Matchableclass AnyShow all
Members list
Value members
Concrete methods
Called after all scene objects have been updated but before any of them were rendered. It allows, for example, to rearrange UI sprites on the screen after all of them had a chance to update their dimensions but before they are actually rendered on the screen. Essentially, it provides for "post-update, pre-render" notification. The order in which scene objects are called is undefined.
Called after all scene objects have been updated but before any of them were rendered. It allows, for example, to rearrange UI sprites on the screen after all of them had a chance to update their dimensions but before they are actually rendered on the screen. Essentially, it provides for "post-update, pre-render" notification. The order in which scene objects are called is undefined.
Note that in most cases one should not override this callback. It is only meant for the use cases when one needs to be notified when all scene objects to be updated but before any of them are rendered. Default implementation is no-op. No rendering should be done in this callback.
Default implementation is no-op.
Value parameters
- ctx
-
Frame context. This context provides bulk of functionality that a scene object can do in a game, e.g. interact with other scene objects, check collisions, read input events and manage input focus, add or remove scene objects, add new and switch between scenes, etc.
Attributes
- See also
- Definition Classes
- Source
- CPLayoutSprite.scala
Updates the layout spec to given value. It will take an effect on the next frame.
Updates the layout spec to given value. It will take an effect on the next frame.
Value parameters
- spec
-
Layout specification.
Attributes
- Source
- CPLayoutSprite.scala
Inherited methods
Compares the receiver object (this
) with the argument object (that
) for equivalence.
Compares the receiver object (this
) with the argument object (that
) for equivalence.
Any implementation of this method should be an equivalence relation:
- It is reflexive: for any instance
x
of typeAny
,x.equals(x)
should returntrue
. - It is symmetric: for any instances
x
andy
of typeAny
,x.equals(y)
should returntrue
if and only ify.equals(x)
returnstrue
. - It is transitive: for any instances
x
,y
, andz
of typeAny
ifx.equals(y)
returnstrue
andy.equals(z)
returnstrue
, thenx.equals(z)
should returntrue
.
If you override this method, you should verify that your implementation remains an equivalence relation. Additionally, when overriding this method it is usually necessary to override hashCode
to ensure that objects which are "equal" (o1.equals(o2)
returns true
) hash to the same scala.Int. (o1.hashCode.equals(o2.hashCode)
).
Value parameters
- that
-
the object to compare against this object for equality.
Attributes
- Returns
-
true
if the receiver object is equivalent to the argument;false
otherwise. - Definition Classes
-
CPGameObject -> Any
- Inherited from:
- CPGameObject
- Source
- CPGameObject.scala
Gets optional collision shape or hit box for this sprite.
Gets optional collision shape or hit box for this sprite.
Attributes
- Returns
-
Optional collision shape,
None
if this sprite does not support collisions. - See also
- Inherited from:
- CPSceneObject
- Source
- CPSceneObject.scala
Gets current dimension (width and height) of this object.
Gets current dimension (width and height) of this object.
Attributes
- Definition Classes
- Inherited from:
- CPOffScreenSprite
- Source
- CPOffScreenSprite.scala
Gets current height of this object.
Gets current height of this object.
Attributes
- See also
- Inherited from:
- CPSceneObject
- Source
- CPSceneObject.scala
Gets unique ID of this game object.
Gets unique ID of this game object.
Attributes
- Inherited from:
- CPGameObject
- Source
- CPGameObject.scala
Gets rectangular shape of this sprite. It is basically a combination of its top-left corner XY-coordinate and sprite's dimension.
Gets rectangular shape of this sprite. It is basically a combination of its top-left corner XY-coordinate and sprite's dimension.
Attributes
- Definition Classes
- Inherited from:
- CPOffScreenSprite
- Source
- CPOffScreenSprite.scala
Gets the optional list of shaders attached to this scene object. By default, returns an empty list. Note that shaders are called regardless of whether the object visible, in camera frame or invisible.
Gets the optional list of shaders attached to this scene object. By default, returns an empty list. Note that shaders are called regardless of whether the object visible, in camera frame or invisible.
Attributes
- Definition Classes
- Inherited from:
- CPOffScreenSprite
- Source
- CPOffScreenSprite.scala
Gets current lifecycle state.
Gets optional set of organizational tags. Note that by default the set of tags is empty.
Gets optional set of organizational tags. Note that by default the set of tags is empty.
Attributes
- See also
- Inherited from:
- CPGameObject
- Source
- CPGameObject.scala
Gets current width of this object.
Gets current width of this object.
Attributes
- See also
- Inherited from:
- CPSceneObject
- Source
- CPSceneObject.scala
Gets current X-coordinate of this object within dimensions of its scene. Note that returned value is allowed to be outside scene's dimension (e.g. negative value). In such cases, the clipping of the scene rendering will result in showing only portion or none of the object.
Gets current X-coordinate of this object within dimensions of its scene. Note that returned value is allowed to be outside scene's dimension (e.g. negative value). In such cases, the clipping of the scene rendering will result in showing only portion or none of the object.
Attributes
- Definition Classes
- Inherited from:
- CPOffScreenSprite
- Source
- CPOffScreenSprite.scala
Gets last X-coordinate which is
Gets last X-coordinate which is
getX + getWidth - 1
. Note the value when the width is zero.
Attributes
- Returns
-
Last X-coordinate for this sprite.
- Inherited from:
- CPSceneObject
- Source
- CPSceneObject.scala
Gets current Y-coordinate of this object within dimensions of its scene. Note that returned value is allowed to be outside scene's dimension (e.g. negative value). In such cases, the clipping of the scene rendering will result in showing only portion or none of the object.
Gets current Y-coordinate of this object within dimensions of its scene. Note that returned value is allowed to be outside scene's dimension (e.g. negative value). In such cases, the clipping of the scene rendering will result in showing only portion or none of the object.
Attributes
- Definition Classes
- Inherited from:
- CPOffScreenSprite
- Source
- CPOffScreenSprite.scala
Gets last Y-coordinate which is
Gets last Y-coordinate which is
getY + getHeight - 1
. Note the value when the height is zero.
Attributes
- Returns
-
Last Y-coordinate for this sprite.
- Inherited from:
- CPSceneObject
- Source
- CPSceneObject.scala
Gets Z-index or order to use in rendering of this object. A pixel with higher Z-index visually overrides the overlapping pixel in the same XY-coordinate with equal or smaller Z-index.
Gets Z-index or order to use in rendering of this object. A pixel with higher Z-index visually overrides the overlapping pixel in the same XY-coordinate with equal or smaller Z-index.
Attributes
- Definition Classes
- Inherited from:
- CPOffScreenSprite
- Source
- CPOffScreenSprite.scala
Shortcut method for hiding this object. Note that by default all scene objects are visible. Note also that this change happens immediately in the same frame. If you want this to happen in the next frame only you need to use CPSceneObjectContext.runNextFrame.
Shortcut method for hiding this object. Note that by default all scene objects are visible. Note also that this change happens immediately in the same frame. If you want this to happen in the next frame only you need to use CPSceneObjectContext.runNextFrame.
Attributes
- See also
- Inherited from:
- CPSceneObject
- Source
- CPSceneObject.scala
Checks the visibility flag.
Checks the visibility flag.
If object is invisible than only update method will be called on each frame. If object is visible and in camera frame - method render will be called as well to render itself. Note that shaders are called regardless of whether the object visible, in camera frame or invisible.
Attributes
- See also
- Inherited from:
- CPSceneObject
- Source
- CPSceneObject.scala
Callback on lifecycle object activation. Default implementation is no-op.
Callback on lifecycle object activation. Default implementation is no-op.
Attributes
- See also
-
CPLifecycle.State.LF_ACTIVE
- Inherited from:
- CPLifecycle
- Source
- CPLifecycle.scala
Callback on lifecycle object deactivation. Default implementation is no-op.
Callback on lifecycle object deactivation. Default implementation is no-op.
Attributes
- See also
-
CPLifecycle.State.LF_INACTIVE
- Inherited from:
- CPLifecycle
- Source
- CPLifecycle.scala
Callback on lifecycle object start. Default implementation is no-op.
Callback on lifecycle object start. Default implementation is no-op.
Attributes
- See also
-
CPLifecycle.State.LF_STARTED
- Inherited from:
- CPLifecycle
- Source
- CPLifecycle.scala
Callback on lifecycle object stop. Default implementation is no-op.
Callback on lifecycle object stop. Default implementation is no-op.
Attributes
- See also
-
CPLifecycle.State.LF_STOPPED
- Inherited from:
- CPLifecycle
- Source
- CPLifecycle.scala
Called to render this scene object. Only visible and in camera frame objects will receive this callback. This callback is called on scene object after all scene objects received update and monitor callbacks. Note that unlike update and monitor callbacks and shaders that are called for all scene objects on each frame, this callback is only called for scene objects that are visible and, at least partially, in camera frame. The order in which scene objects are called is undefined.
Called to render this scene object. Only visible and in camera frame objects will receive this callback. This callback is called on scene object after all scene objects received update and monitor callbacks. Note that unlike update and monitor callbacks and shaders that are called for all scene objects on each frame, this callback is only called for scene objects that are visible and, at least partially, in camera frame. The order in which scene objects are called is undefined.
Note that generally this callback should not modify scene object state (which should be done in either update or monitor callbacks.
Default implementation is no-op.
Value parameters
- ctx
-
Frame context. This context provides bulk of functionality that a scene object can do in a game, e.g. interact with other scene objects, check collisions, read input events and manage input focus, add or remove scene objects, add new and switch between scenes, etc.
Attributes
- See also
- Inherited from:
- CPSceneObject
- Source
- CPSceneObject.scala
Sets visibility flag. Note that by default all scene objects are visible. Note also that this change happens immediately in the same frame. If you want this to happen in the next frame only you need to use CPSceneObjectContext.runNextFrame.
Sets visibility flag. Note that by default all scene objects are visible. Note also that this change happens immediately in the same frame. If you want this to happen in the next frame only you need to use CPSceneObjectContext.runNextFrame.
Value parameters
- vis
-
true
to make this object visible,false
otherwise.
Attributes
- See also
- Inherited from:
- CPSceneObject
- Source
- CPSceneObject.scala
Shortcut method for showing this object. Note that by default all scene objects are visible. Note also that this change happens immediately in the same frame. If you want this to happen in the next frame only you need to use CPSceneObjectContext.runNextFrame.
Shortcut method for showing this object. Note that by default all scene objects are visible. Note also that this change happens immediately in the same frame. If you want this to happen in the next frame only you need to use CPSceneObjectContext.runNextFrame.
Attributes
- See also
- Inherited from:
- CPSceneObject
- Source
- CPSceneObject.scala
Called to update the internal state of this scene object. This callback is called each frame on every object in the scene and it is called before any render callback. Note that all scene object will receive this callback before first render callback. The order in which scene objects are called is undefined.
Called to update the internal state of this scene object. This callback is called each frame on every object in the scene and it is called before any render callback. Note that all scene object will receive this callback before first render callback. The order in which scene objects are called is undefined.
Default implementation is no-op. No rendering should be done in this callback.
Value parameters
- ctx
-
Frame context. This context provides bulk of functionality that a scene object can do in a game, e.g. interact with other scene objects, check collisions, read input events and manage input focus, add or remove scene objects, add new and switch between scenes, etc.
Attributes
- See also
- Inherited from:
- CPSceneObject
- Source
- CPSceneObject.scala