...

Source file src/code.rocket9labs.com/tslocum/etk/input.go

Documentation: code.rocket9labs.com/tslocum/etk

     1  package etk
     2  
     3  import (
     4  	"image"
     5  	"image/color"
     6  
     7  	"code.rocket9labs.com/tslocum/etk/messeji"
     8  	"github.com/hajimehoshi/ebiten/v2"
     9  	"golang.org/x/image/font/sfnt"
    10  )
    11  
    12  // Input is a text input widget. The Input widget is simply a Text widget that
    13  // also accepts user input.
    14  type Input struct {
    15  	*Box
    16  	field  *messeji.InputField
    17  	cursor string
    18  	focus  bool
    19  }
    20  
    21  // NewInput returns a new Input widget.
    22  func NewInput(text string, onSelected func(text string) (handled bool)) *Input {
    23  	textColor := Style.TextColorDark
    24  	/*if TextColor == nil {
    25  		textColor = Style.InputColor
    26  	}*/
    27  
    28  	f := messeji.NewInputField(FontFace(Style.TextFont, Scale(Style.TextSize)), fontMutex)
    29  	f.SetForegroundColor(textColor)
    30  	f.SetBackgroundColor(transparent)
    31  	f.SetScrollBarColors(Style.ScrollAreaColor, Style.ScrollHandleColor)
    32  	f.SetScrollBorderSize(Scale(Style.ScrollBorderSize))
    33  	f.SetScrollBorderColors(Style.ScrollBorderColorTop, Style.ScrollBorderColorRight, Style.ScrollBorderColorBottom, Style.ScrollBorderColorLeft)
    34  	f.SetPrefix("")
    35  	f.SetSuffix("")
    36  	f.SetText(text)
    37  	f.SetHandleKeyboard(true)
    38  	f.SetSelectedFunc(func() (accept bool) {
    39  		return onSelected(f.Text())
    40  	})
    41  
    42  	i := &Input{
    43  		Box:    NewBox(),
    44  		field:  f,
    45  		cursor: "_",
    46  	}
    47  	i.SetBackground(Style.InputBgColor)
    48  	return i
    49  }
    50  
    51  // SetRect sets the position and size of the widget.
    52  func (i *Input) SetRect(r image.Rectangle) {
    53  	i.Box.rect = r
    54  
    55  	i.field.SetRect(r)
    56  
    57  	for _, w := range i.children {
    58  		w.SetRect(r)
    59  	}
    60  }
    61  
    62  // Foreground return the color of the text within the field.
    63  func (i *Input) Foreground() color.RGBA {
    64  	i.Lock()
    65  	defer i.Unlock()
    66  
    67  	return i.field.ForegroundColor()
    68  }
    69  
    70  // SetForegroundColor sets the color of the text within the field.
    71  func (i *Input) SetForeground(c color.RGBA) {
    72  	i.Lock()
    73  	defer i.Unlock()
    74  
    75  	i.field.SetForegroundColor(c)
    76  }
    77  
    78  // SetPrefix sets the text shown before the input text.
    79  func (i *Input) SetPrefix(prefix string) {
    80  	i.Lock()
    81  	defer i.Unlock()
    82  
    83  	i.field.SetPrefix(prefix)
    84  }
    85  
    86  // SetSuffix sets the text shown after the input text.
    87  func (i *Input) SetSuffix(suffix string) {
    88  	i.Lock()
    89  	defer i.Unlock()
    90  
    91  	i.field.SetSuffix(suffix)
    92  }
    93  
    94  // SetCursor sets the cursor appended to the text buffer when focused.
    95  func (i *Input) SetCursor(cursor string) {
    96  	i.Lock()
    97  	defer i.Unlock()
    98  
    99  	i.cursor = cursor
   100  	if i.focus {
   101  		i.field.SetSuffix(cursor)
   102  	}
   103  }
   104  
   105  // Focus returns the focus state of the widget.
   106  func (i *Input) Focus() bool {
   107  	return i.focus
   108  }
   109  
   110  // SetFocus sets the focus state of the widget.
   111  func (i *Input) SetFocus(focus bool) bool {
   112  	i.focus = focus
   113  
   114  	var cursor string
   115  	if focus {
   116  		cursor = i.cursor
   117  	}
   118  	i.field.SetSuffix(cursor)
   119  	return true
   120  }
   121  
   122  // Text returns the content of the text buffer.
   123  func (i *Input) Text() string {
   124  	i.Lock()
   125  	defer i.Unlock()
   126  
   127  	return i.field.Text()
   128  }
   129  
   130  // SetText sets the text in the field.
   131  func (i *Input) SetText(text string) {
   132  	i.Lock()
   133  	defer i.Unlock()
   134  
   135  	i.field.SetText(text)
   136  }
   137  
   138  // SetScrollBarWidth sets the width of the scroll bar.
   139  func (i *Input) SetScrollBarWidth(width int) {
   140  	i.Lock()
   141  	defer i.Unlock()
   142  
   143  	i.field.SetScrollBarWidth(width)
   144  }
   145  
   146  // SetScrollBarColors sets the color of the scroll bar area and handle.
   147  func (i *Input) SetScrollBarColors(area color.RGBA, handle color.RGBA) {
   148  	i.Lock()
   149  	defer i.Unlock()
   150  
   151  	i.field.SetScrollBarColors(Style.ScrollAreaColor, Style.ScrollHandleColor)
   152  }
   153  
   154  // SetScrollBarVisible sets whether the scroll bar is visible on the screen.
   155  func (i *Input) SetScrollBarVisible(scrollVisible bool) {
   156  	i.Lock()
   157  	defer i.Unlock()
   158  
   159  	i.field.SetScrollBarVisible(scrollVisible)
   160  }
   161  
   162  // SetAutoHideScrollBar sets whether the scroll bar is automatically hidden
   163  // when the entire text buffer is visible.
   164  func (i *Input) SetAutoHideScrollBar(autoHide bool) {
   165  	i.Lock()
   166  	defer i.Unlock()
   167  
   168  	i.field.SetAutoHideScrollBar(autoHide)
   169  }
   170  
   171  // SetFont sets the font and text size of the field. Scaling is not applied.
   172  func (t *Input) SetFont(fnt *sfnt.Font, size int) {
   173  	t.Lock()
   174  	defer t.Unlock()
   175  
   176  	t.field.SetFont(FontFace(fnt, size), fontMutex)
   177  }
   178  
   179  // Padding returns the amount of padding around the text within the field.
   180  func (i *Input) Padding() int {
   181  	i.Lock()
   182  	defer i.Unlock()
   183  
   184  	return i.field.Padding()
   185  }
   186  
   187  // SetPadding sets the amount of padding around the text within the field.
   188  func (i *Input) SetPadding(padding int) {
   189  	i.Lock()
   190  	defer i.Unlock()
   191  
   192  	i.field.SetPadding(padding)
   193  }
   194  
   195  // SetWordWrap sets a flag which, when enabled, causes text to wrap without breaking words.
   196  func (i *Input) SetWordWrap(wrap bool) {
   197  	i.Lock()
   198  	defer i.Unlock()
   199  
   200  	i.field.SetWordWrap(wrap)
   201  }
   202  
   203  // SetHorizontal sets the horizontal alignment of the text within the field.
   204  func (i *Input) SetHorizontal(h Alignment) {
   205  	i.Lock()
   206  	defer i.Unlock()
   207  
   208  	i.field.SetHorizontal(messeji.Alignment(h))
   209  }
   210  
   211  // SetVertical sets the vertical alignment of the text within the field.
   212  func (i *Input) SetVertical(h Alignment) {
   213  	i.Lock()
   214  	defer i.Unlock()
   215  
   216  	i.field.SetVertical(messeji.Alignment(h))
   217  }
   218  
   219  // SetMask sets the rune used to mask the text buffer contents. Set to 0 to disable.
   220  func (i *Input) SetMask(r rune) {
   221  	i.Lock()
   222  	defer i.Unlock()
   223  
   224  	i.field.SetMask(r)
   225  }
   226  
   227  // Write writes to the text buffer.
   228  func (i *Input) Write(p []byte) (n int, err error) {
   229  	return i.field.Write(p)
   230  }
   231  
   232  // HandleKeyboard is called when a keyboard event occurs.
   233  func (i *Input) HandleKeyboard(key ebiten.Key, r rune) (handled bool, err error) {
   234  	if !i.focus {
   235  		return false, nil
   236  	}
   237  
   238  	return i.field.HandleKeyboardEvent(key, r)
   239  }
   240  
   241  // HandleMouse is called when a mouse event occurs.
   242  func (i *Input) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) {
   243  	return i.field.HandleMouseEvent(cursor, pressed, clicked)
   244  }
   245  
   246  // Draw draws the widget on the screen.
   247  func (i *Input) Draw(screen *ebiten.Image) error {
   248  	i.field.Draw(screen)
   249  	return nil
   250  }
   251  

View as plain text