...

Source file src/code.rocket9labs.com/tslocum/etk/text.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  // Text is a text display widget.
    13  type Text struct {
    14  	*Box
    15  	field    *messeji.TextField
    16  	children []Widget
    17  }
    18  
    19  // NewText returns a new Text widget.
    20  func NewText(text string) *Text {
    21  	f := newText()
    22  	f.SetText(text)
    23  	f.SetForegroundColor(Style.TextColorLight)
    24  	f.SetHandleKeyboard(true)
    25  
    26  	return &Text{
    27  		Box:   NewBox(),
    28  		field: f,
    29  	}
    30  }
    31  
    32  // SetRect sets the position and size of the widget.
    33  func (t *Text) SetRect(r image.Rectangle) {
    34  	t.Lock()
    35  	defer t.Unlock()
    36  
    37  	t.rect = r
    38  	t.field.SetRect(r)
    39  }
    40  
    41  // Foreground return the color of the text within the field.
    42  func (t *Text) Foreground() color.RGBA {
    43  	t.Lock()
    44  	defer t.Unlock()
    45  
    46  	return t.field.ForegroundColor()
    47  }
    48  
    49  // SetForegroundColor sets the color of the text within the field.
    50  func (t *Text) SetForeground(c color.RGBA) {
    51  	t.Lock()
    52  	defer t.Unlock()
    53  
    54  	t.field.SetForegroundColor(c)
    55  }
    56  
    57  // Focus returns the focus state of the widget.
    58  func (t *Text) Focus() bool {
    59  	return false
    60  }
    61  
    62  // SetFocus sets the focus state of the widget.
    63  func (t *Text) SetFocus(focus bool) bool {
    64  	return false
    65  }
    66  
    67  // SetScrollBarWidth sets the width of the scroll bar.
    68  func (t *Text) SetScrollBarWidth(width int) {
    69  	t.Lock()
    70  	defer t.Unlock()
    71  
    72  	t.field.SetScrollBarWidth(width)
    73  }
    74  
    75  // SetScrollBarColors sets the color of the scroll bar area and handle.
    76  func (t *Text) SetScrollBarColors(area color.RGBA, handle color.RGBA) {
    77  	t.Lock()
    78  	defer t.Unlock()
    79  
    80  	t.field.SetScrollBarColors(Style.ScrollAreaColor, Style.ScrollHandleColor)
    81  }
    82  
    83  // SetScrollBorderColor sets the color of the top, right, bottom and left border
    84  // of the scroll bar handle.
    85  func (t *Text) SetScrollBorderColors(top color.RGBA, right color.RGBA, bottom color.RGBA, left color.RGBA) {
    86  	t.Lock()
    87  	defer t.Unlock()
    88  
    89  	t.field.SetScrollBorderColors(top, right, bottom, left)
    90  }
    91  
    92  // SetWordWrap sets a flag which, when enabled, causes text to wrap without breaking words.
    93  func (t *Text) SetWordWrap(wrap bool) {
    94  	t.Lock()
    95  	defer t.Unlock()
    96  
    97  	t.field.SetWordWrap(wrap)
    98  }
    99  
   100  // SetHorizontal sets the horizontal alignment of the text within the field.
   101  func (t *Text) SetHorizontal(h Alignment) {
   102  	t.Lock()
   103  	defer t.Unlock()
   104  
   105  	t.field.SetHorizontal(messeji.Alignment(h))
   106  }
   107  
   108  // SetVertical sets the vertical alignment of the text within the field.
   109  func (t *Text) SetVertical(h Alignment) {
   110  	t.Lock()
   111  	defer t.Unlock()
   112  
   113  	t.field.SetVertical(messeji.Alignment(h))
   114  }
   115  
   116  // Write writes to the text buffer.
   117  func (t *Text) Write(p []byte) (n int, err error) {
   118  	t.Lock()
   119  	defer t.Unlock()
   120  
   121  	return t.field.Write(p)
   122  }
   123  
   124  // Text returns the content of the text buffer.
   125  func (t *Text) Text() string {
   126  	t.Lock()
   127  	defer t.Unlock()
   128  
   129  	return t.field.Text()
   130  }
   131  
   132  // SetText sets the text in the field.
   133  func (t *Text) SetText(text string) {
   134  	t.Lock()
   135  	defer t.Unlock()
   136  
   137  	t.field.SetText(text)
   138  }
   139  
   140  // SetScrollBarVisible sets whether the scroll bar is visible on the screen.
   141  func (t *Text) SetScrollBarVisible(scrollVisible bool) {
   142  	t.Lock()
   143  	defer t.Unlock()
   144  
   145  	t.field.SetScrollBarVisible(scrollVisible)
   146  }
   147  
   148  // SetAutoHideScrollBar sets whether the scroll bar is automatically hidden
   149  // when the entire text buffer is visible.
   150  func (t *Text) SetAutoHideScrollBar(autoHide bool) {
   151  	t.Lock()
   152  	defer t.Unlock()
   153  
   154  	t.field.SetAutoHideScrollBar(autoHide)
   155  }
   156  
   157  // SetFont sets the font and text size of the field. Scaling is not applied.
   158  func (t *Text) SetFont(fnt *sfnt.Font, size int) {
   159  	t.Lock()
   160  	defer t.Unlock()
   161  
   162  	t.field.SetFont(FontFace(fnt, size), fontMutex)
   163  }
   164  
   165  // Padding returns the amount of padding around the text within the field.
   166  func (t *Text) Padding() int {
   167  	t.Lock()
   168  	defer t.Unlock()
   169  
   170  	return t.field.Padding()
   171  }
   172  
   173  // SetPadding sets the amount of padding around the text within the field.
   174  func (t *Text) SetPadding(padding int) {
   175  	t.Lock()
   176  	defer t.Unlock()
   177  
   178  	t.field.SetPadding(padding)
   179  }
   180  
   181  // SetFollow sets whether the field should automatically scroll to the end when
   182  // content is added to the buffer.
   183  func (t *Text) SetFollow(follow bool) {
   184  	t.Lock()
   185  	defer t.Unlock()
   186  
   187  	t.field.SetFollow(follow)
   188  }
   189  
   190  // SetSingleLine sets whether the field displays all text on a single line.
   191  // When enabled, the field scrolls horizontally. Otherwise, it scrolls vertically.
   192  func (t *Text) SetSingleLine(single bool) {
   193  	t.Lock()
   194  	defer t.Unlock()
   195  
   196  	t.field.SetSingleLine(single)
   197  }
   198  
   199  // SetMask sets the rune used to mask the text buffer contents. Set to 0 to disable.
   200  func (t *Text) SetMask(r rune) {
   201  	t.Lock()
   202  	defer t.Unlock()
   203  
   204  	t.field.SetMask(r)
   205  }
   206  
   207  // HandleKeyboard is called when a keyboard event occurs.
   208  func (t *Text) HandleKeyboard(key ebiten.Key, r rune) (handled bool, err error) {
   209  	return t.field.HandleKeyboardEvent(key, r)
   210  }
   211  
   212  // HandleMouse is called when a mouse event occurs.
   213  func (t *Text) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) {
   214  	return t.field.HandleMouseEvent(cursor, pressed, clicked)
   215  }
   216  
   217  // Draw draws the widget on the screen.
   218  func (t *Text) Draw(screen *ebiten.Image) error {
   219  	t.field.Draw(screen)
   220  	return nil
   221  }
   222  
   223  // Children returns the children of the widget.
   224  func (t *Text) Children() []Widget {
   225  	t.Lock()
   226  	defer t.Unlock()
   227  
   228  	return t.children
   229  }
   230  
   231  // AddChild adds a child to the widget.
   232  func (t *Text) AddChild(w ...Widget) {
   233  	t.Lock()
   234  	defer t.Unlock()
   235  
   236  	t.children = append(t.children, w...)
   237  }
   238  
   239  var _ Widget = &Text{}
   240  

View as plain text