feat(ui): implement new ui system
This commit is contained in:
parent
d407eaad8b
commit
6cf0e4abb4
11 changed files with 430 additions and 113 deletions
48
game/ui/widgets/button.go
Normal file
48
game/ui/widgets/button.go
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
package widgets
|
||||
|
||||
import (
|
||||
"tetris/engine/core"
|
||||
"tetris/engine/render"
|
||||
"tetris/game/ui/base"
|
||||
|
||||
rl "github.com/gen2brain/raylib-go/raylib"
|
||||
)
|
||||
|
||||
type Button struct {
|
||||
*base.WithFocus
|
||||
Text string
|
||||
TextSize int32
|
||||
Action func()
|
||||
}
|
||||
|
||||
func NewButton(text string, size int32, action func()) Button {
|
||||
return Button{
|
||||
WithFocus: &base.WithFocus{},
|
||||
Text: text,
|
||||
TextSize: size,
|
||||
Action: action,
|
||||
}
|
||||
}
|
||||
|
||||
func (b Button) Size() core.Vec2i {
|
||||
s := int(b.TextSize)
|
||||
return core.Vec2i{
|
||||
X: len(b.Text) * s,
|
||||
Y: s,
|
||||
}
|
||||
}
|
||||
|
||||
func (b Button) HandleInput() {
|
||||
if rl.IsKeyPressed(rl.KeyEnter) {
|
||||
b.Action()
|
||||
}
|
||||
}
|
||||
|
||||
func (b Button) Draw(x, y int32) {
|
||||
// TODO: use a theme system here so colors are not hardcoded.
|
||||
col := rl.White
|
||||
if b.HasFocus() {
|
||||
col = rl.Red
|
||||
}
|
||||
render.DrawTextCenter(x, y, b.TextSize, b.Text, col)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue