Skip to content

UI Components

Daniel S edited this page Sep 23, 2018 · 6 revisions

A ui component needs to implement the Component interface, which itself is a combination of the ComponentAttributes and the ComponentLogic interface. To make it easier there is already a ComponentBase struct that implements the ComponentAttributes interface so you only need to embed the ComponentBase into your component struct and implement a Update and a Draw function.

Adding a component to a console is rather simple. You just create a component of your choice and call the AddComponent function of the console or sub-console that you would like the component to be on.

yc := NewYourComponent(2, 2, 5, 5)
yourConsole.AddComponent(yc)

Minimal Component Skeleton

// YourComponent that embeds ComponentBase
type YourComponent struct {
	*console.ComponentBase
}

// NewYourComponent creates your component.
func NewYourComponent(x, y, width, height int) *YourComponent {
	yc := YourComponent{
		ComponentBase: console.NewComponentBase(x, y, width, height),
	}

	return &yc
}

// Update updates your component
func (yc *YourComponent) Update(con *console.Console, timeElapsed float64) bool {
        // Return true if the component should keep on living.
        // Return false if the component should be deleted from the console.
        // Use yc.Close() to close it from outside.
	return true
}

// Draw draws your component
func (yc *YourComponent) Draw(con *console.Console, timeElapsed float64) {
	con.Clear(yc.X, yc.Y, yc.Width, yc.Height, t.Background(consolecolor.NewHex("#000")))
}
Clone this wiki locally