62 lines
1.1 KiB
Go
62 lines
1.1 KiB
Go
package widgets
|
|
|
|
import (
|
|
"tetris/engine/core"
|
|
"tetris/engine/input"
|
|
"tetris/engine/render"
|
|
"tetris/game/ui/base"
|
|
|
|
rl "github.com/gen2brain/raylib-go/raylib"
|
|
)
|
|
|
|
type CheckboxAction func(value bool)
|
|
|
|
type Checkbox struct {
|
|
*base.WithFocus
|
|
action CheckboxAction
|
|
value bool
|
|
}
|
|
|
|
func NewCheckbox(value bool, action CheckboxAction) *Checkbox {
|
|
return &Checkbox{
|
|
WithFocus: &base.WithFocus{},
|
|
value: value,
|
|
action: action,
|
|
}
|
|
}
|
|
|
|
func (cb Checkbox) Size() core.Vec2i {
|
|
return core.NewVec2(24, 24)
|
|
}
|
|
|
|
func (cb *Checkbox) HandleInput() {
|
|
if input.KeyPressedWithRepeat(rl.KeyEnter) {
|
|
cb.value = !cb.value
|
|
cb.action(cb.value)
|
|
}
|
|
}
|
|
|
|
func (cb Checkbox) Draw(x, y int32) {
|
|
size := cb.Size()
|
|
space := int32(2)
|
|
color := rl.White
|
|
if cb.HasFocus() {
|
|
color = rl.Red
|
|
}
|
|
|
|
render.DrawRectOutlineBorder(rl.RectangleInt32{
|
|
X: x,
|
|
Y: y,
|
|
Width: int32(size.X),
|
|
Height: int32(size.Y),
|
|
}, 4, color)
|
|
|
|
if cb.value {
|
|
render.DrawRectBorder(rl.RectangleInt32{
|
|
X: x + space,
|
|
Y: y + space,
|
|
Width: int32(size.X) - (space * 2),
|
|
Height: int32(size.Y) - (space * 2),
|
|
}, color, 0, rl.Blank)
|
|
}
|
|
}
|