...

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

View as plain text