...

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

Documentation: code.rocket9labs.com/tslocum/etk

     1  package etk
     2  
     3  import (
     4  	"image"
     5  	"image/color"
     6  
     7  	"github.com/hajimehoshi/ebiten/v2"
     8  )
     9  
    10  // Widget represents an interface element. Most widgets will embed Box and build
    11  // on top of it.
    12  type Widget interface {
    13  	// Rect returns the position and size of the widget.
    14  	Rect() image.Rectangle
    15  
    16  	// SetRect sets the position and size of the widget.
    17  	SetRect(r image.Rectangle)
    18  
    19  	// Background returns the background color of the widget.
    20  	Background() color.RGBA
    21  
    22  	// SetBackground sets the background color of the widget.
    23  	SetBackground(background color.RGBA)
    24  
    25  	// Focus returns the focus state of the widget.
    26  	Focus() bool
    27  
    28  	// SetFocus sets the focus state of the widget.
    29  	SetFocus(focus bool) (accept bool)
    30  
    31  	// Visible returns the visibility of the widget.
    32  	Visible() bool
    33  
    34  	// SetVisible sets the visibility of the widget.
    35  	SetVisible(visible bool)
    36  
    37  	// Cursor returns the cursor shape shown when a mouse cursor hovers over
    38  	// the widget, or -1 to let widgets beneath determine the cursor shape.
    39  	Cursor() ebiten.CursorShapeType
    40  
    41  	// HandleKeyboard is called when a keyboard event occurs.
    42  	HandleKeyboard(ebiten.Key, rune) (handled bool, err error)
    43  
    44  	// HandleMouse is called when a mouse event occurs. Only mouse events that
    45  	// are on top of the widget are passed to the widget, except after clicking
    46  	// within the widget and then dragging the cursor outside of the widget.
    47  	HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error)
    48  
    49  	// Clip returns whether the widget and its children are restricted to drawing
    50  	// within the widget's rect area of the screen. For best performance, Clip
    51  	// should return false unless clipping is actually needed.
    52  	Clip() bool
    53  
    54  	// Draw draws the widget on the screen.
    55  	Draw(screen *ebiten.Image) error
    56  
    57  	// Children returns the children of the widget. Children are drawn in the
    58  	// order they are returned. Keyboard and mouse events are passed to children
    59  	// in reverse order.
    60  	Children() []Widget
    61  }
    62  
    63  // WithoutFocus wraps a widget to ignore focus.
    64  type WithoutFocus struct {
    65  	Widget
    66  }
    67  
    68  // Focus returns the focus state of the widget.
    69  func (w *WithoutFocus) Focus() bool {
    70  	return false
    71  }
    72  
    73  // SetFocus sets the focus state of the widget.
    74  func (w *WithoutFocus) SetFocus(focus bool) (accept bool) {
    75  	return false
    76  }
    77  
    78  // WithoutMouse wraps a widget to ignore all mouse events.
    79  type WithoutMouse struct {
    80  	Widget
    81  }
    82  
    83  // HandleMouse is called when a mouse event occurs. Only mouse events that are
    84  // on top of the widget are passed to the widget.
    85  func (w *WithoutMouse) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) {
    86  	return false, nil
    87  }
    88  
    89  // WithoutMouseExceptScroll wraps a widget to ignore all mouse events except
    90  // scroll events.
    91  type WithoutMouseExceptScroll struct {
    92  	Widget
    93  	handleOnce bool
    94  }
    95  
    96  // HandleMouse is called when a mouse event occurs. Only mouse events that are
    97  // on top of the widget are passed to the widget.
    98  func (w *WithoutMouseExceptScroll) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) {
    99  	if pressed || clicked {
   100  		w.handleOnce = true
   101  		return w.Widget.HandleMouse(cursor, pressed, clicked)
   102  	} else if w.handleOnce {
   103  		w.handleOnce = false
   104  		return w.Widget.HandleMouse(cursor, pressed, clicked)
   105  	}
   106  	return false, nil
   107  }
   108  

View as plain text