last_light/game/ui/container.go

83 lines
1.5 KiB
Go
Raw Permalink Normal View History

2024-04-24 17:11:33 +03:00
package ui
import (
2024-05-06 21:19:08 +03:00
"mvvasilev/last_light/engine"
2024-04-24 17:11:33 +03:00
"github.com/gdamore/tcell/v2"
"github.com/gdamore/tcell/v2/views"
"github.com/google/uuid"
)
type UIContainerLayout int
const (
// These change the provided ui positions
UpperLeft UIContainerLayout = iota
MiddleLeft
LowerLeft
UpperRight
MiddleRight
LowerRight
UpperCenter
MiddleCenter
LowerCenter
// This uses the positions as provided in the ui elements
Manual
)
type UIContainer struct {
id uuid.UUID
layout UIContainerLayout
2024-05-06 21:19:08 +03:00
position engine.Position
size engine.Size
2024-04-24 17:11:33 +03:00
elements []UIElement
}
func CreateUIContainer(x, y int, width, height int, layout UIContainerLayout) *UIContainer {
2024-04-24 17:11:33 +03:00
container := new(UIContainer)
container.id = uuid.New()
container.layout = layout
2024-05-06 21:19:08 +03:00
container.position = engine.PositionAt(x, y)
container.size = engine.SizeOf(width, height)
2024-04-24 17:11:33 +03:00
container.elements = make([]UIElement, 0)
return container
}
func (uic *UIContainer) Push(element UIElement) {
uic.elements = append(uic.elements, element)
}
func (uic *UIContainer) Clear() {
uic.elements = make([]UIElement, 0)
}
func (uic *UIContainer) UniqueId() uuid.UUID {
return uic.id
}
func (uic *UIContainer) MoveTo(x, y int) {
2024-05-06 21:19:08 +03:00
uic.position = engine.PositionAt(x, y)
2024-04-24 17:11:33 +03:00
}
2024-05-06 21:19:08 +03:00
func (uic *UIContainer) Position() engine.Position {
2024-04-24 17:11:33 +03:00
return uic.position
}
2024-05-06 21:19:08 +03:00
func (uic *UIContainer) Size() engine.Size {
2024-04-24 17:11:33 +03:00
return uic.size
}
func (uic *UIContainer) Draw(v views.View) {
for _, e := range uic.elements {
e.Draw(v)
}
}
func (uic *UIContainer) Input(ev *tcell.EventKey) {
for _, e := range uic.elements {
e.Input(ev)
}
}