etk/text.go

98 lines
1.7 KiB
Go
Raw Normal View History

2022-07-07 21:53:14 +00:00
package etk
import (
"image"
"image/color"
2022-07-07 21:53:14 +00:00
"code.rocketnine.space/tslocum/messeji"
"github.com/hajimehoshi/ebiten/v2"
2022-07-07 21:53:14 +00:00
)
type Text struct {
2023-09-29 05:43:29 +00:00
*messeji.TextField
background color.RGBA
children []Widget
2022-07-07 21:53:14 +00:00
}
func NewText(text string) *Text {
textColor := Style.TextColorLight
l := messeji.NewTextField(Style.TextFont)
l.SetText(text)
l.SetForegroundColor(textColor)
l.SetBackgroundColor(Style.TextBgColor)
l.SetScrollBarColors(Style.ScrollAreaColor, Style.ScrollHandleColor)
2023-10-28 05:04:50 +00:00
l.SetHandleKeyboard(true)
2022-07-07 21:53:14 +00:00
return &Text{
2023-09-29 05:43:29 +00:00
TextField: l,
2022-07-07 21:53:14 +00:00
}
}
func (t *Text) Background() color.RGBA {
t.Lock()
defer t.Unlock()
return t.background
}
func (t *Text) SetBackground(background color.RGBA) {
t.Lock()
defer t.Unlock()
t.background = background
}
2023-10-29 05:23:18 +00:00
func (t *Text) Focus() bool {
return false
}
2023-10-29 05:23:18 +00:00
func (t *Text) SetFocus(focus bool) bool {
return false
2023-10-21 22:30:09 +00:00
}
2023-09-29 05:43:29 +00:00
func (t *Text) Children() []Widget {
t.Lock()
defer t.Unlock()
return t.children
}
func (t *Text) AddChild(w ...Widget) {
t.Lock()
defer t.Unlock()
t.children = append(t.children, w...)
}
2023-01-03 19:37:01 +00:00
// Clear clears the field's buffer.
func (t *Text) Clear() {
2023-10-27 20:36:27 +00:00
t.TextField.SetText("")
2023-01-03 19:37:01 +00:00
}
2022-07-07 21:53:14 +00:00
// Write writes to the field's buffer.
func (t *Text) Write(p []byte) (n int, err error) {
2023-10-27 20:36:27 +00:00
return t.TextField.Write(p)
2022-07-07 21:53:14 +00:00
}
2023-06-08 04:07:08 +00:00
func (t *Text) Text() string {
2023-10-27 20:36:27 +00:00
return t.TextField.Text()
2022-07-07 21:53:14 +00:00
}
2023-10-28 05:04:50 +00:00
func (t *Text) HandleKeyboard(key ebiten.Key, r rune) (handled bool, err error) {
return t.TextField.HandleKeyboardEvent(key, r)
2022-07-07 21:53:14 +00:00
}
2023-10-28 05:04:50 +00:00
func (t *Text) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) {
return t.TextField.HandleMouseEvent(cursor, pressed, clicked)
}
2022-07-07 21:53:14 +00:00
func (t *Text) Draw(screen *ebiten.Image) error {
// Draw label.
2023-09-29 05:43:29 +00:00
t.TextField.Draw(screen)
2022-07-07 21:53:14 +00:00
return nil
}
var _ Widget = &Text{}