API Panel - shmellyorc/Box GitHub Wiki
Panel
Namespace: Box.Entities.Container
Description
Represents a generic container for UI elements, managing child entities and layout updates. The panel tracks a dirty state to know when its contents or layout have changed, and provides hooks for recalculating layout and rendering debug outlines.
Constructor
public Panel(params Entity[] children)
Stores the provided child entities for addition on screen entry and marks the panel dirty for initial layout.
Properties
Property | Description |
---|---|
bool IsDirty |
Indicates whether the panel’s content or layout needs updating. |
Methods
Method Signature | Description | Returns |
---|---|---|
protected override void OnEnter() |
Called when the panel enters the active state; adds initial children, connects entity added/removed signals, and clears the child buffer. | void |
protected override void Update() |
Called each frame; if dirty, calls UpdateDirtyState() then resets dirty flag, optionally draws debug outline, and updates child entities. |
void |
protected virtual void UpdateDirtyState() |
Hook for derived panels to recalculate layout when the panel is marked dirty. | void |
new void AddChild(Entity entity) |
Adds a single child entity to the panel and marks the panel (and its parents) dirty. | void |
new void AddChild(params Entity[] children) |
Adds multiple child entities to the panel and marks the panel (and its parents) dirty. | void |
new bool RemoveChild(Entity entity) |
Removes a child entity from the panel and marks the panel (and its parents) dirty. | bool |
new bool RemoveChild(params Entity[] children) |
Removes multiple child entities and marks dirty. | bool |
new void ClearChildren() |
Clears all child entities from the panel and marks dirty. | void |
Examples
// Create some UI elements
var button = new Button("Play");
var label = new Label("Score: 0");
// Initialize a panel with them
var uiPanel = new Panel(button, label);
// Add to the screen (children attach on OnEnter)
mainScreen.AddEntity(uiPanel);
// Subclass to implement custom layout
public class CustomVerticalPanel : Panel
{
protected override void CustomVerticalPanel()
{
float y = 10;
foreach (var child in Children)
{
child.Position = new Vect2(10, y);
y += child.Size.Y + 5;
}
}
}
// Use the custom panel
var verticalPanel = new CustomVerticalPanel(button, label);
mainScreen.AddEntity(verticalPanel);