...

Source file src/codeberg.org/tslocum/etk/text.go

Documentation: codeberg.org/tslocum/etk

     1  package etk
     2  
     3  import (
     4  	"image"
     5  	"image/color"
     6  
     7  	"codeberg.org/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  // SetLineHeight sets the height of each line. The line height is normally
   214  // detected automatically and you will not need to call SetLineHeight.
   215  // Set to 0 to restore default line height.
   216  func (t *Text) SetLineHeight(lineHeight int) {
   217  	t.Lock()
   218  	defer t.Unlock()
   219  
   220  	t.field.SetLineHeight(lineHeight)
   221  }
   222  
   223  // Padding returns the amount of padding around the text within the field.
   224  func (t *Text) Padding() int {
   225  	t.Lock()
   226  	defer t.Unlock()
   227  
   228  	return t.field.Padding()
   229  }
   230  
   231  // SetPadding sets the amount of padding around the text within the field.
   232  func (t *Text) SetPadding(padding int) {
   233  	t.Lock()
   234  	defer t.Unlock()
   235  
   236  	t.field.SetPadding(padding)
   237  }
   238  
   239  // SetFollow sets whether the field should automatically scroll to the end when
   240  // content is added to the buffer.
   241  func (t *Text) SetFollow(follow bool) {
   242  	t.Lock()
   243  	defer t.Unlock()
   244  
   245  	t.field.SetFollow(follow)
   246  }
   247  
   248  // SetSingleLine sets whether the field displays all text on a single line.
   249  // When enabled, the field scrolls horizontally. Otherwise, it scrolls vertically.
   250  func (t *Text) SetSingleLine(single bool) {
   251  	t.Lock()
   252  	defer t.Unlock()
   253  
   254  	t.field.SetSingleLine(single)
   255  }
   256  
   257  // SetMask sets the rune used to mask the text buffer contents. Set to 0 to disable.
   258  func (t *Text) SetMask(r rune) {
   259  	t.Lock()
   260  	defer t.Unlock()
   261  
   262  	t.field.SetMask(r)
   263  }
   264  
   265  // HandleKeyboard is called when a keyboard event occurs.
   266  func (t *Text) HandleKeyboard(key ebiten.Key, r rune) (handled bool, err error) {
   267  	return t.field.HandleKeyboardEvent(key, r)
   268  }
   269  
   270  // HandleMouse is called when a mouse event occurs.
   271  func (t *Text) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) {
   272  	return t.field.HandleMouseEvent(cursor, pressed, clicked)
   273  }
   274  
   275  // Draw draws the widget on the screen.
   276  func (t *Text) Draw(screen *ebiten.Image) error {
   277  	t.field.Draw(screen)
   278  	return nil
   279  }
   280  
   281  // Children returns the children of the widget.
   282  func (t *Text) Children() []Widget {
   283  	t.Lock()
   284  	defer t.Unlock()
   285  
   286  	return t.children
   287  }
   288  
   289  // AddChild adds a child to the widget.
   290  func (t *Text) AddChild(w ...Widget) {
   291  	t.Lock()
   292  	defer t.Unlock()
   293  
   294  	t.children = append(t.children, w...)
   295  }
   296  
   297  var _ Widget = &Text{}
   298  

View as plain text