...

Source file src/code.rocket9labs.com/tslocum/etk/box.go

Documentation: code.rocket9labs.com/tslocum/etk

     1  package etk
     2  
     3  import (
     4  	"image"
     5  	"image/color"
     6  	"sync"
     7  
     8  	"github.com/hajimehoshi/ebiten/v2"
     9  )
    10  
    11  // Box is a building block for other widgets. It may also be used as a spacer
    12  // in layout widgets.
    13  type Box struct {
    14  	rect       image.Rectangle
    15  	children   []Widget
    16  	background color.RGBA
    17  	visible    bool
    18  
    19  	sync.Mutex
    20  }
    21  
    22  // NewBox returns a new Box widget.
    23  func NewBox() *Box {
    24  	return &Box{
    25  		background: transparent,
    26  		visible:    true,
    27  	}
    28  }
    29  
    30  // Rect returns the position and size of the widget.
    31  func (b *Box) Rect() image.Rectangle {
    32  	b.Lock()
    33  	defer b.Unlock()
    34  
    35  	return b.rect
    36  }
    37  
    38  // SetRect sets the position and size of the widget.
    39  func (b *Box) SetRect(r image.Rectangle) {
    40  	b.Lock()
    41  	b.rect = r
    42  	b.Unlock()
    43  
    44  	for _, w := range b.children {
    45  		w.SetRect(r)
    46  	}
    47  }
    48  
    49  // Background returns the background color of the widget.
    50  func (b *Box) Background() color.RGBA {
    51  	b.Lock()
    52  	defer b.Unlock()
    53  
    54  	return b.background
    55  }
    56  
    57  // SetBackground sets the background color of the widget.
    58  func (b *Box) SetBackground(background color.RGBA) {
    59  	b.Lock()
    60  	defer b.Unlock()
    61  
    62  	b.background = background
    63  }
    64  
    65  // Focus returns the focus state of the widget.
    66  func (b *Box) Focus() bool {
    67  	return false
    68  }
    69  
    70  // SetFocus sets the focus state of the widget.
    71  func (b *Box) SetFocus(focus bool) bool {
    72  	return false
    73  }
    74  
    75  // Visible returns the visibility of the widget.
    76  func (b *Box) Visible() bool {
    77  	return b.visible
    78  }
    79  
    80  // SetVisible sets the visibility of the widget.
    81  func (b *Box) SetVisible(visible bool) {
    82  	b.visible = visible
    83  }
    84  
    85  // Clip returns whether the widget and its children are restricted to drawing
    86  // within the widget's rect area of the screen. For best performance, Clip
    87  // should return false unless clipping is actually needed.
    88  func (b *Box) Clip() bool {
    89  	return false
    90  }
    91  
    92  // Cursor returns the cursor shape shown when a mouse cursor hovers over the
    93  // widget, or -1 to let widgets beneath determine the cursor shape.
    94  func (b *Box) Cursor() ebiten.CursorShapeType {
    95  	return -1
    96  }
    97  
    98  // HandleKeyboard is called when a keyboard event occurs.
    99  func (b *Box) HandleKeyboard(key ebiten.Key, r rune) (handled bool, err error) {
   100  	return false, nil
   101  }
   102  
   103  // HandleMouse is called when a mouse event occurs. Only mouse events that
   104  // are on top of the widget are passed to the widget.
   105  func (b *Box) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) {
   106  	return false, nil
   107  }
   108  
   109  // Draw draws the widget on the screen.
   110  func (b *Box) Draw(screen *ebiten.Image) error {
   111  	return nil
   112  }
   113  
   114  // Children returns the children of the widget. Children are drawn in the
   115  // order they are returned. Keyboard and mouse events are passed to children
   116  // in reverse order.
   117  func (b *Box) Children() []Widget {
   118  	b.Lock()
   119  	defer b.Unlock()
   120  
   121  	return b.children
   122  }
   123  
   124  // AddChild adds a child to the widget.
   125  func (b *Box) AddChild(w ...Widget) {
   126  	b.Lock()
   127  	defer b.Unlock()
   128  
   129  	b.children = append(b.children, w...)
   130  }
   131  
   132  // Clear removes all children from the widget.
   133  func (b *Box) Clear() {
   134  	b.Lock()
   135  	defer b.Unlock()
   136  
   137  	b.children = b.children[:0]
   138  }
   139  
   140  var _ Widget = &Box{}
   141  

View as plain text