...

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

View as plain text