Layout
Dill pixel provides two main ways to handle layouts in your game: FlexContainer
and UICanvas
. These classes offer different approaches to positioning and organizing UI elements.
FlexContainer
The FlexContainer provides a flexible box layout model similar to CSS Flexbox. It’s ideal for creating dynamic layouts that need to adapt to different screen sizes or content.
Basic Usage
Configuration Options
The FlexContainer supports several layout properties:
flexDirection
: Controls the direction of items (‘row’ | ‘column’)flexWrap
: Determines if items should wrap (‘wrap’ | ‘nowrap’)alignItems
: Aligns items on the cross axis (‘center’ | ‘flex-start’ | ‘flex-end’)justifyContent
: Aligns items on the main axis (‘center’ | ‘flex-start’ | ‘flex-end’ | ‘space-between’ | ‘space-around’ | ‘space-evenly’)gap
: Space between items (number)
Nested Containers
FlexContainers can be nested to create more complex layouts:
UICanvas
The UICanvas provides edge-based alignment for UI elements, making it perfect for HUD elements, menus, and other screen-anchored UI components.
Basic Usage
Alignment Options
UICanvas supports various edge alignments:
- Cardinal directions: ‘top’, ‘bottom’, ‘left’, ‘right’
- Corners: ‘top left’, ‘top right’, ‘bottom left’, ‘bottom right’
- Center positions: ‘top center’, ‘bottom center’, ‘left center’, ‘right center’
- Absolute center: ‘center’
Adding Padding
Elements can be positioned with padding from their aligned edges:
Combining with FlexContainer
UICanvas and FlexContainer can be used together for complex layouts:
Handling Anchored Elements
FlexContainer
and UICanvas
use a sophisticated system to handle elements with anchors or pivot points. When a child is added to a FlexContainer
or UICanvas
, it’s automatically wrapped in an inner container to ensure consistent positioning:
How It Works
When a child is added to a FlexContainer, the following process occurs:
- The child is wrapped in an inner container
- The inner container’s bounds are calculated
- The container’s pivot is adjusted to ensure consistent positioning:
Implications
- Accessing Children: When you need to access a child element, be aware it’s wrapped in a container
- Positioning: All positioning is handled relative to the top-left corner of the inner container
- Layout Updates: Changes to the child’s anchor or pivot will be automatically accommodated by the container system
Example with Mixed Elements
Best Practices
-
Choose the Right Tool:
- Use FlexContainer for dynamic content that needs to flow and adapt
- Use UICanvas for screen-anchored UI elements like HUDs and menus
-
Performance:
- Minimize nested containers to reduce layout calculations
- Use
size
constraints when possible to help optimize layout calculations
-
Responsive Design:
- Use
useAppSize: true
with UICanvas for responsive layouts - Combine FlexContainer’s
flexWrap
with appropriate sizing for adaptive layouts
- Use