...

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

Documentation: code.rocket9labs.com/tslocum/etk

     1  package etk
     2  
     3  import (
     4  	"image"
     5  
     6  	"code.rocket9labs.com/tslocum/etk/kibodo"
     7  	"github.com/hajimehoshi/ebiten/v2"
     8  )
     9  
    10  // Keyboard is an on-screen keyboard widget. User input is automatically passed
    11  // to the focused widget.
    12  type Keyboard struct {
    13  	*Box
    14  	k        *kibodo.Keyboard
    15  	incoming []*kibodo.Input
    16  }
    17  
    18  // NewKeyboard returns a new Keyboard widget.
    19  func NewKeyboard() *Keyboard {
    20  	k := kibodo.NewKeyboard(Style.TextFont)
    21  	k.Show()
    22  	return &Keyboard{
    23  		Box: NewBox(),
    24  		k:   k,
    25  	}
    26  }
    27  
    28  // SetRect sets the position and size of the keyboard.
    29  func (k *Keyboard) SetRect(r image.Rectangle) {
    30  	k.Lock()
    31  	defer k.Unlock()
    32  	k.rect = r
    33  	k.k.SetRect(r.Min.X, r.Min.Y, r.Dx(), r.Dy())
    34  }
    35  
    36  // Visible returns the visibility of the keyboard.
    37  func (k *Keyboard) Visible() bool {
    38  	k.Lock()
    39  	defer k.Unlock()
    40  	return k.visible && k.k.Visible()
    41  }
    42  
    43  // SetVisible sets the visibility of the keyboard.
    44  func (k *Keyboard) SetVisible(visible bool) {
    45  	k.Lock()
    46  	defer k.Unlock()
    47  	k.visible = visible
    48  	if visible {
    49  		k.k.Show()
    50  	} else {
    51  		k.k.Hide()
    52  	}
    53  }
    54  
    55  // Keys returns the keys of the keyboard.
    56  func (k *Keyboard) Keys() [][]*kibodo.Key {
    57  	k.Lock()
    58  	defer k.Unlock()
    59  	return k.k.GetKeys()
    60  }
    61  
    62  // SetKeys sets the keys of the keyboard.
    63  func (k *Keyboard) SetKeys(keys [][]*kibodo.Key) {
    64  	k.Lock()
    65  	defer k.Unlock()
    66  	k.k.SetKeys(keys)
    67  }
    68  
    69  // SetExtendedKeys sets the keys of the keyboard when the .
    70  func (k *Keyboard) SetExtendedKeys(keys [][]*kibodo.Key) {
    71  	k.Lock()
    72  	defer k.Unlock()
    73  	k.k.SetExtendedKeys(keys)
    74  }
    75  
    76  // SetShowExtended sets whether the normal or extended keyboard is shown.
    77  func (k *Keyboard) SetShowExtended(show bool) {
    78  	k.Lock()
    79  	defer k.Unlock()
    80  	k.k.SetShowExtended(show)
    81  }
    82  
    83  // SetScheduleFrameFunc sets the function called whenever the screen should be redrawn.
    84  func (k *Keyboard) SetScheduleFrameFunc(f func()) {
    85  	k.Lock()
    86  	defer k.Unlock()
    87  	k.k.SetScheduleFrameFunc(f)
    88  }
    89  
    90  // Cursor returns the cursor shape shown when a mouse cursor hovers over the
    91  // widget, or -1 to let widgets beneath determine the cursor shape.
    92  func (k *Keyboard) Cursor() ebiten.CursorShapeType {
    93  	return ebiten.CursorShapePointer
    94  }
    95  
    96  // HandleMouse is called when a mouse event occurs.
    97  func (k *Keyboard) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) {
    98  	k.Lock()
    99  	defer k.Unlock()
   100  	return k.k.HandleMouse(cursor, pressed, clicked)
   101  }
   102  
   103  // Draw draws the keyboard on the screen.
   104  func (k *Keyboard) Draw(screen *ebiten.Image) error {
   105  	k.Lock()
   106  	defer k.Unlock()
   107  	k.incoming = k.k.AppendInput(k.incoming[:0])
   108  	w := Focused()
   109  	if w != nil {
   110  		for _, key := range k.incoming {
   111  			_, err := w.HandleKeyboard(key.Key, key.Rune)
   112  			if err != nil {
   113  				return err
   114  			}
   115  		}
   116  	}
   117  	k.k.Draw(screen)
   118  	return nil
   119  }
   120  

View as plain text