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. Either a key or a 42 // rune is set, specifying the pressed key. When a key is set, its value is 43 // greater than or equal to 0. When a rune is set, the value of key is -1. 44 HandleKeyboard(key ebiten.Key, r rune) (handled bool, err error) 45 46 // HandleMouse is called when a mouse event occurs. Only mouse events that 47 // are on top of the widget are passed to the widget, except after clicking 48 // within the widget and then dragging the cursor outside of the widget. 49 HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) 50 51 // Clip returns whether the widget and its children are restricted to drawing 52 // within the widget's rect area of the screen. For best performance, Clip 53 // should return false unless clipping is actually needed. 54 Clip() bool 55 56 // Draw draws the widget on the screen. 57 Draw(screen *ebiten.Image) error 58 59 // Children returns the children of the widget. Children are drawn in the 60 // order they are returned. Keyboard and mouse events are passed to children 61 // in reverse order. 62 Children() []Widget 63 } 64 65 // WithoutFocus wraps a widget to ignore focus. 66 type WithoutFocus struct { 67 Widget 68 } 69 70 // Focus returns the focus state of the widget. 71 func (w *WithoutFocus) Focus() bool { 72 return false 73 } 74 75 // SetFocus sets the focus state of the widget. 76 func (w *WithoutFocus) SetFocus(focus bool) (accept bool) { 77 return false 78 } 79 80 // WithoutMouse wraps a widget to ignore all mouse events. 81 type WithoutMouse struct { 82 Widget 83 } 84 85 // HandleMouse is called when a mouse event occurs. Only mouse events that are 86 // on top of the widget are passed to the widget. 87 func (w *WithoutMouse) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) { 88 return false, nil 89 } 90 91 // WithoutMouseExceptScroll wraps a widget to ignore all mouse events except 92 // scroll events. 93 type WithoutMouseExceptScroll struct { 94 Widget 95 handleOnce bool 96 } 97 98 // HandleMouse is called when a mouse event occurs. Only mouse events that are 99 // on top of the widget are passed to the widget. 100 func (w *WithoutMouseExceptScroll) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) { 101 if pressed || clicked { 102 w.handleOnce = true 103 return w.Widget.HandleMouse(cursor, pressed, clicked) 104 } else if w.handleOnce { 105 w.handleOnce = false 106 return w.Widget.HandleMouse(cursor, pressed, clicked) 107 } 108 return false, nil 109 } 110