...

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.
    46  	HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error)
    47  
    48  	// Draw draws the widget on the screen.
    49  	Draw(screen *ebiten.Image) error
    50  
    51  	// Children returns the children of the widget. Children are drawn in the
    52  	// order they are returned. Keyboard and mouse events are passed to children
    53  	// in reverse order.
    54  	Children() []Widget
    55  }
    56  
    57  // WithoutFocus wraps a widget to ignore focus.
    58  type WithoutFocus struct {
    59  	Widget
    60  }
    61  
    62  // Focus returns the focus state of the widget.
    63  func (w *WithoutFocus) Focus() bool {
    64  	return false
    65  }
    66  
    67  // SetFocus sets the focus state of the widget.
    68  func (w *WithoutFocus) SetFocus(focus bool) (accept bool) {
    69  	return false
    70  }
    71  
    72  // WithoutMouse wraps a widget to ignore all mouse events.
    73  type WithoutMouse struct {
    74  	Widget
    75  }
    76  
    77  // HandleMouse is called when a mouse event occurs. Only mouse events that are
    78  // on top of the widget are passed to the widget.
    79  func (w *WithoutMouse) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) {
    80  	return false, nil
    81  }
    82  
    83  // WithoutMouseExceptScroll wraps a widget to ignore all mouse events except
    84  // scroll events.
    85  type WithoutMouseExceptScroll struct {
    86  	Widget
    87  	handleOnce bool
    88  }
    89  
    90  // HandleMouse is called when a mouse event occurs. Only mouse events that are
    91  // on top of the widget are passed to the widget.
    92  func (w *WithoutMouseExceptScroll) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) {
    93  	if pressed || clicked {
    94  		w.handleOnce = true
    95  		return w.Widget.HandleMouse(cursor, pressed, clicked)
    96  	} else if w.handleOnce {
    97  		w.handleOnce = false
    98  		return w.Widget.HandleMouse(cursor, pressed, clicked)
    99  	}
   100  	return false, nil
   101  }
   102  

View as plain text