...

Source file src/code.rocketnine.space/tslocum/etk/text.go

Documentation: code.rocketnine.space/tslocum/etk

     1  package etk
     2  
     3  import (
     4  	"image"
     5  
     6  	"github.com/hajimehoshi/ebiten/v2"
     7  
     8  	"code.rocketnine.space/tslocum/messeji"
     9  )
    10  
    11  type Text struct {
    12  	*Box
    13  	Field *messeji.TextField
    14  }
    15  
    16  func NewText(text string) *Text {
    17  	textColor := Style.TextColorLight
    18  
    19  	l := messeji.NewTextField(Style.TextFont)
    20  	l.SetText(text)
    21  	l.SetForegroundColor(textColor)
    22  	l.SetBackgroundColor(Style.TextBgColor)
    23  	l.SetHorizontal(messeji.AlignCenter)
    24  	l.SetVertical(messeji.AlignCenter)
    25  
    26  	return &Text{
    27  		Box:   NewBox(),
    28  		Field: l,
    29  	}
    30  }
    31  
    32  // Clear clears the field's buffer.
    33  func (t *Text) Clear() {
    34  	t.Field.SetText("")
    35  }
    36  
    37  // Write writes to the field's buffer.
    38  func (t *Text) Write(p []byte) (n int, err error) {
    39  	return t.Field.Write(p)
    40  }
    41  
    42  func (t *Text) Text() string {
    43  	return t.Field.Text()
    44  }
    45  
    46  func (t *Text) SetRect(r image.Rectangle) {
    47  	t.Box.rect = r
    48  
    49  	t.Field.SetRect(r)
    50  }
    51  
    52  func (t *Text) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) {
    53  	return false, nil
    54  }
    55  
    56  func (t *Text) HandleKeyboard() (handled bool, err error) {
    57  	return false, nil
    58  }
    59  
    60  func (t *Text) Draw(screen *ebiten.Image) error {
    61  	// Draw label.
    62  	t.Field.Draw(screen)
    63  	return nil
    64  }
    65  

View as plain text