2022-07-07 21:53:14 +00:00
|
|
|
package etk
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
2023-10-26 03:51:19 +00:00
|
|
|
"image/color"
|
2022-07-07 21:53:14 +00:00
|
|
|
|
|
|
|
"code.rocketnine.space/tslocum/messeji"
|
2023-10-21 23:09:53 +00:00
|
|
|
"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
|
|
|
|
|
2023-10-26 03:51:19 +00:00
|
|
|
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)
|
2023-10-28 07:30:09 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-26 03:51:19 +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 {
|
2023-10-23 07:30:39 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-10-29 05:23:18 +00:00
|
|
|
func (t *Text) SetFocus(focus bool) bool {
|
2023-10-23 07:30:39 +00:00
|
|
|
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)
|
2023-10-22 19:29:32 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2023-10-26 03:51:19 +00:00
|
|
|
|
|
|
|
var _ Widget = &Text{}
|