...

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  // WithoutMouse wraps a widget to ignore all mouse events.
    58  type WithoutMouse struct {
    59  	Widget
    60  }
    61  
    62  // HandleMouse is called when a mouse event occurs. Only mouse events that are
    63  // on top of the widget are passed to the widget.
    64  func (w *WithoutMouse) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) {
    65  	return false, nil
    66  }
    67  
    68  // WithoutMouseExceptScroll wraps a widget to ignore all mouse events except
    69  // scroll events.
    70  type WithoutMouseExceptScroll struct {
    71  	Widget
    72  	handleOnce bool
    73  }
    74  
    75  // HandleMouse is called when a mouse event occurs. Only mouse events that are
    76  // on top of the widget are passed to the widget.
    77  func (w *WithoutMouseExceptScroll) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) {
    78  	if pressed || clicked {
    79  		w.handleOnce = true
    80  		return w.Widget.HandleMouse(cursor, pressed, clicked)
    81  	} else if w.handleOnce {
    82  		w.handleOnce = false
    83  		return w.Widget.HandleMouse(cursor, pressed, clicked)
    84  	}
    85  	return false, nil
    86  }
    87  

View as plain text