...

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

View as plain text