feat(ui): implement new ui system
This commit is contained in:
parent
d407eaad8b
commit
6cf0e4abb4
11 changed files with 430 additions and 113 deletions
57
game/ui/layouts/input_control.go
Normal file
57
game/ui/layouts/input_control.go
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
package layouts
|
||||
|
||||
import (
|
||||
"tetris/engine/core"
|
||||
"tetris/game/ui"
|
||||
"tetris/game/ui/base"
|
||||
"tetris/game/ui/widgets"
|
||||
|
||||
rl "github.com/gen2brain/raylib-go/raylib"
|
||||
)
|
||||
|
||||
// InputControl is a layout that has a label and an a widget associated with it.
|
||||
type InputControl struct {
|
||||
*base.WithFocus
|
||||
label *widgets.Label
|
||||
widget ui.InputWidget
|
||||
spacing int
|
||||
}
|
||||
|
||||
func NewInputControl(label *widgets.Label, wiget ui.InputWidget) *InputControl {
|
||||
return &InputControl{
|
||||
WithFocus: &base.WithFocus{},
|
||||
label: label,
|
||||
widget: wiget,
|
||||
spacing: 10,
|
||||
}
|
||||
}
|
||||
|
||||
func (ic InputControl) Size() core.Vec2i {
|
||||
return core.Vec2i{
|
||||
X: ic.label.Size().X + ic.widget.Size().X + ic.spacing,
|
||||
Y: max(ic.label.Size().Y, ic.widget.Size().Y),
|
||||
}
|
||||
}
|
||||
|
||||
func (ic *InputControl) SetFocus(value bool) {
|
||||
ic.WithFocus.SetFocus(value)
|
||||
ic.widget.SetFocus(value)
|
||||
|
||||
// TODO: use a theme system here so colors are not hardcoded.
|
||||
if value {
|
||||
ic.label.SetColor(rl.Red)
|
||||
} else {
|
||||
ic.label.SetColor(rl.White)
|
||||
}
|
||||
}
|
||||
|
||||
func (ic InputControl) HandleInput() {
|
||||
ic.widget.HandleInput()
|
||||
}
|
||||
|
||||
func (ic InputControl) Draw(x, y int32) {
|
||||
size := ic.Size()
|
||||
label_y := (int32(size.Y) - int32(ic.label.Size().Y)) / 2
|
||||
ic.label.Draw(x, y+label_y)
|
||||
ic.widget.Draw(x+int32(ic.label.Size().X+ic.spacing), y)
|
||||
}
|
||||
84
game/ui/layouts/list_box.go
Normal file
84
game/ui/layouts/list_box.go
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
package layouts
|
||||
|
||||
import (
|
||||
"tetris/game/ui"
|
||||
"tetris/game/ui/base"
|
||||
|
||||
rl "github.com/gen2brain/raylib-go/raylib"
|
||||
)
|
||||
|
||||
type OnSelectCallback func()
|
||||
|
||||
type ListBox struct {
|
||||
*base.WithFocus
|
||||
selected int
|
||||
entries []ui.InputWidget
|
||||
onSelect OnSelectCallback
|
||||
spacing int
|
||||
}
|
||||
|
||||
func NewListBox(entries []ui.InputWidget) *ListBox {
|
||||
lb := &ListBox{
|
||||
WithFocus: &base.WithFocus{},
|
||||
entries: entries,
|
||||
onSelect: func() {},
|
||||
}
|
||||
lb.entries[0].SetFocus(true)
|
||||
return lb
|
||||
}
|
||||
|
||||
func (lb *ListBox) Spacing(value int) *ListBox {
|
||||
lb.spacing = value
|
||||
return lb
|
||||
}
|
||||
|
||||
func (lb *ListBox) OnSelect(callback OnSelectCallback) *ListBox {
|
||||
lb.onSelect = callback
|
||||
return lb
|
||||
}
|
||||
|
||||
func (lb ListBox) Entries() []ui.InputWidget {
|
||||
return lb.entries
|
||||
}
|
||||
|
||||
func (lb *ListBox) Select(index int) {
|
||||
if index >= 0 && index < len(lb.entries) {
|
||||
lb.entries[lb.selected].SetFocus(false)
|
||||
lb.selected = index
|
||||
lb.entries[lb.selected].SetFocus(true)
|
||||
lb.onSelect()
|
||||
}
|
||||
}
|
||||
|
||||
func (lb ListBox) Selected() ui.InputWidget {
|
||||
return lb.entries[lb.selected]
|
||||
}
|
||||
|
||||
func (lb ListBox) IsSelected(index int) bool {
|
||||
return lb.selected == index
|
||||
}
|
||||
|
||||
func (lb *ListBox) Next() {
|
||||
lb.Select(lb.selected + 1)
|
||||
}
|
||||
|
||||
func (lb *ListBox) Previous() {
|
||||
lb.Select(lb.selected - 1)
|
||||
}
|
||||
|
||||
func (lb *ListBox) HandleInput() {
|
||||
if rl.IsKeyPressed(rl.KeyDown) {
|
||||
lb.Next()
|
||||
} else if rl.IsKeyPressed(rl.KeyUp) {
|
||||
lb.Previous()
|
||||
} else {
|
||||
lb.Selected().HandleInput()
|
||||
}
|
||||
}
|
||||
|
||||
func (lb *ListBox) Draw(x, y int32) {
|
||||
for _, ent := range lb.entries {
|
||||
ent.Draw(x, y)
|
||||
y += int32(ent.Size().Y + lb.spacing)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue