Skip to content

UI Components

Daniel S edited this page Oct 3, 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, Draw and FocusOnClick 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
}

// FocusOnClick returns true if a click should focus the component permanently until
// TAB is pressed or another FocusOnClickable component is pressed with the mouse.
// Simple components like buttons that don't need to stay focused after a press
// should return false. They will still be able to get focused through TAB and can
// be used by keyboard only users.
func (y *YourComponent) FocusOnClick() bool {
	return false
}

// 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