Add widgets
parent
1b8a1f536a
commit
8d5f5af3c5
@ -0,0 +1,63 @@
|
||||
package etk
|
||||
|
||||
import (
|
||||
"image"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
|
||||
"code.rocketnine.space/tslocum/messeji"
|
||||
)
|
||||
|
||||
type Input struct {
|
||||
*Box
|
||||
field *messeji.InputField
|
||||
}
|
||||
|
||||
func NewInput(prefix string, text string, onSelected func(text string) (handled bool)) *Input {
|
||||
textColor := Style.TextColorDark
|
||||
/*if TextColor == nil {
|
||||
textColor = Style.InputColor
|
||||
}*/
|
||||
|
||||
i := messeji.NewInputField(Style.TextFont)
|
||||
i.SetPrefix(prefix)
|
||||
i.SetText(text)
|
||||
i.SetForegroundColor(textColor)
|
||||
i.SetBackgroundColor(Style.InputBgColor)
|
||||
i.SetHandleKeyboard(true)
|
||||
i.SetSelectedFunc(func() (accept bool) {
|
||||
return onSelected(i.Text())
|
||||
})
|
||||
|
||||
return &Input{
|
||||
Box: NewBox(),
|
||||
field: i,
|
||||
}
|
||||
}
|
||||
|
||||
// Write writes to the field's buffer.
|
||||
func (i *Input) Write(p []byte) (n int, err error) {
|
||||
return i.field.Write(p)
|
||||
}
|
||||
|
||||
func (i *Input) SetRect(r image.Rectangle) {
|
||||
i.Box.rect = r
|
||||
|
||||
i.field.SetRect(r)
|
||||
}
|
||||
|
||||
func (i *Input) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (i *Input) HandleKeyboard() (handled bool, err error) {
|
||||
err = i.field.Update()
|
||||
|
||||
return false, err
|
||||
}
|
||||
|
||||
func (i *Input) Draw(screen *ebiten.Image) error {
|
||||
// Draw label.
|
||||
i.field.Draw(screen)
|
||||
return nil
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package etk
|
||||
|
||||
import "github.com/hajimehoshi/ebiten/v2"
|
||||
|
||||
type Shortcuts struct {
|
||||
ConfirmKeyboard []ebiten.Key
|
||||
ConfirmMouse []ebiten.MouseButton
|
||||
ConfirmGamepad []ebiten.GamepadButton
|
||||
}
|
||||
|
||||
var Bindings = &Shortcuts{
|
||||
ConfirmKeyboard: []ebiten.Key{ebiten.KeyEnter, ebiten.KeyKPEnter},
|
||||
ConfirmMouse: []ebiten.MouseButton{ebiten.MouseButtonLeft},
|
||||
ConfirmGamepad: []ebiten.GamepadButton{ebiten.GamepadButton0},
|
||||
}
|
@ -1,22 +1,62 @@
|
||||
package etk
|
||||
|
||||
import "image/color"
|
||||
import (
|
||||
"image/color"
|
||||
"log"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
|
||||
"golang.org/x/image/font"
|
||||
"golang.org/x/image/font/opentype"
|
||||
)
|
||||
|
||||
var transparent = color.RGBA{0, 0, 0, 0}
|
||||
|
||||
func defaultFont() font.Face {
|
||||
tt, err := opentype.Parse(fonts.MPlus1pRegular_ttf)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
const dpi = 72
|
||||
defaultFont, err := opentype.NewFace(tt, &opentype.FaceOptions{
|
||||
Size: 32,
|
||||
DPI: dpi,
|
||||
Hinting: font.HintingFull,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return defaultFont
|
||||
}
|
||||
|
||||
type Attributes struct {
|
||||
TextColor color.Color
|
||||
TextFont font.Face
|
||||
|
||||
TextColorLight color.Color
|
||||
TextColorDark color.Color
|
||||
|
||||
TextBgColor color.Color
|
||||
|
||||
BorderColor color.Color
|
||||
|
||||
InputBgColor color.Color
|
||||
|
||||
ButtonTextColor color.Color
|
||||
ButtonBgColor color.Color
|
||||
ButtonBgColorDisabled color.Color
|
||||
}
|
||||
|
||||
var Style = &Attributes{
|
||||
TextColor: color.RGBA{0, 0, 0, 255},
|
||||
TextFont: defaultFont(),
|
||||
|
||||
TextColorLight: color.RGBA{255, 255, 255, 255},
|
||||
TextColorDark: color.RGBA{0, 0, 0, 255},
|
||||
|
||||
TextBgColor: transparent,
|
||||
|
||||
BorderColor: color.RGBA{0, 0, 0, 255},
|
||||
|
||||
InputBgColor: color.RGBA{0, 128, 0, 255},
|
||||
|
||||
ButtonBgColor: color.RGBA{255, 255, 255, 255},
|
||||
ButtonBgColorDisabled: color.RGBA{110, 110, 110, 255},
|
||||
}
|
||||
|
@ -0,0 +1,55 @@
|
||||
package etk
|
||||
|
||||
import (
|
||||
"image"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
|
||||
"code.rocketnine.space/tslocum/messeji"
|
||||
)
|
||||
|
||||
type Text struct {
|
||||
*Box
|
||||
field *messeji.TextField
|
||||
}
|
||||
|
||||
func NewText(text string) *Text {
|
||||
textColor := Style.TextColorLight
|
||||
|
||||
l := messeji.NewTextField(Style.TextFont)
|
||||
l.SetText(text)
|
||||
l.SetForegroundColor(textColor)
|
||||
l.SetBackgroundColor(Style.TextBgColor)
|
||||
l.SetHorizontal(messeji.AlignCenter)
|
||||
l.SetVertical(messeji.AlignCenter)
|
||||
|
||||
return &Text{
|
||||
Box: NewBox(),
|
||||
field: l,
|
||||
}
|
||||
}
|
||||
|
||||
// Write writes to the field's buffer.
|
||||
func (t *Text) Write(p []byte) (n int, err error) {
|
||||
return t.field.Write(p)
|
||||
}
|
||||
|
||||
func (t *Text) SetRect(r image.Rectangle) {
|
||||
t.Box.rect = r
|
||||
|
||||
t.field.SetRect(r)
|
||||
}
|
||||
|
||||
func (t *Text) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (t *Text) HandleKeyboard() (handled bool, err error) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (t *Text) Draw(screen *ebiten.Image) error {
|
||||
// Draw label.
|
||||
t.field.Draw(screen)
|
||||
return nil
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
package etk
|
||||
|
||||
import (
|
||||
"image"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
||||
// Window displays and passes input to only one child widget at a time.
|
||||
type Window struct {
|
||||
*Box
|
||||
|
||||
allChildren []Widget
|
||||
|
||||
active int
|
||||
labels []string
|
||||
hasLabel bool
|
||||
}
|
||||
|
||||
func NewWindow() *Window {
|
||||
return &Window{
|
||||
Box: NewBox(),
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Window) childrenUpdated() {
|
||||
if len(w.allChildren) == 0 {
|
||||
w.children = nil
|
||||
return
|
||||
}
|
||||
w.children = []Widget{w.allChildren[w.active]}
|
||||
}
|
||||
|
||||
func (w *Window) SetRect(r image.Rectangle) {
|
||||
w.Lock()
|
||||
defer w.Unlock()
|
||||
|
||||
w.rect = r
|
||||
for _, wgt := range w.children {
|
||||
wgt.SetRect(r)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Window) AddChild(wgt ...Widget) {
|
||||
w.allChildren = append(w.allChildren, wgt...)
|
||||
|
||||
for _, widget := range wgt {
|
||||
widget.SetRect(w.rect)
|
||||
}
|
||||
|
||||
blankLabels := make([]string, len(wgt))
|
||||
w.labels = append(w.labels, blankLabels...)
|
||||
|
||||
w.childrenUpdated()
|
||||
}
|
||||
|
||||
func (w *Window) AddChildWithLabel(wgt Widget, label string) {
|
||||
w.Lock()
|
||||
defer w.Unlock()
|
||||
|
||||
wgt.SetRect(w.rect)
|
||||
|
||||
w.allChildren = append(w.allChildren, wgt)
|
||||
w.labels = append(w.labels, label)
|
||||
|
||||
if label != "" {
|
||||
w.hasLabel = true
|
||||
}
|
||||
|
||||
w.childrenUpdated()
|
||||
}
|
||||
|
||||
func (w *Window) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (w *Window) HandleKeyboard() (handled bool, err error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (w *Window) Draw(screen *ebiten.Image) error {
|
||||
// TODO draw labels
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue