...

Source file src/code.rocketnine.space/tslocum/etk/input.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 Input struct {
    12  	*Box
    13  	Field *messeji.InputField
    14  }
    15  
    16  func NewInput(prefix string, text string, onSelected func(text string) (handled bool)) *Input {
    17  	textColor := Style.TextColorDark
    18  	/*if TextColor == nil {
    19  		textColor = Style.InputColor
    20  	}*/
    21  
    22  	i := messeji.NewInputField(Style.TextFont)
    23  	i.SetPrefix(prefix)
    24  	i.SetText(text)
    25  	i.SetForegroundColor(textColor)
    26  	i.SetBackgroundColor(Style.InputBgColor)
    27  	i.SetHandleKeyboard(true)
    28  	i.SetSelectedFunc(func() (accept bool) {
    29  		return onSelected(i.Text())
    30  	})
    31  
    32  	return &Input{
    33  		Box:   NewBox(),
    34  		Field: i,
    35  	}
    36  }
    37  
    38  // Clear clears the field's buffer.
    39  func (i *Input) Clear() {
    40  	i.Field.SetText("")
    41  }
    42  
    43  // Write writes to the field's buffer.
    44  func (i *Input) Write(p []byte) (n int, err error) {
    45  	return i.Field.Write(p)
    46  }
    47  
    48  func (i *Input) Text() string {
    49  	return i.Field.Text()
    50  }
    51  
    52  func (i *Input) SetRect(r image.Rectangle) {
    53  	i.Box.rect = r
    54  
    55  	i.Field.SetRect(r)
    56  }
    57  
    58  func (i *Input) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) {
    59  	return false, nil
    60  }
    61  
    62  func (i *Input) HandleKeyboard() (handled bool, err error) {
    63  	err = i.Field.Update()
    64  
    65  	return false, err
    66  }
    67  
    68  func (i *Input) Draw(screen *ebiten.Image) error {
    69  	// Draw label.
    70  	i.Field.Draw(screen)
    71  	return nil
    72  }
    73  

View as plain text