...

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()
    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  // GetKeys returns the keys of the keyboard.
    37  func (k *Keyboard) GetKeys() [][]*kibodo.Key {
    38  	k.Lock()
    39  	defer k.Unlock()
    40  	return k.k.GetKeys()
    41  }
    42  
    43  // SetKeys sets the keys of the keyboard.
    44  func (k *Keyboard) SetKeys(keys [][]*kibodo.Key) {
    45  	k.Lock()
    46  	defer k.Unlock()
    47  	k.k.SetKeys(keys)
    48  }
    49  
    50  // SetExtendedKeys sets the keys of the keyboard when the .
    51  func (k *Keyboard) SetExtendedKeys(keys [][]*kibodo.Key) {
    52  	k.Lock()
    53  	defer k.Unlock()
    54  	k.k.SetExtendedKeys(keys)
    55  }
    56  
    57  // SetShowExtended sets whether the normal or extended keyboard is shown.
    58  func (k *Keyboard) SetShowExtended(show bool) {
    59  	k.Lock()
    60  	defer k.Unlock()
    61  	k.k.SetShowExtended(show)
    62  }
    63  
    64  // SetScheduleFrameFunc sets the function called whenever the screen should be redrawn.
    65  func (k *Keyboard) SetScheduleFrameFunc(f func()) {
    66  	k.Lock()
    67  	defer k.Unlock()
    68  	k.k.SetScheduleFrameFunc(f)
    69  }
    70  
    71  // HandleMouse is called when a mouse event occurs.
    72  func (k *Keyboard) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) {
    73  	k.Lock()
    74  	defer k.Unlock()
    75  	return k.k.HandleMouse(cursor, pressed, clicked)
    76  }
    77  
    78  // Draw draws the keyboard on the screen.
    79  func (k *Keyboard) Draw(screen *ebiten.Image) error {
    80  	k.Lock()
    81  	defer k.Unlock()
    82  	k.incoming = k.k.AppendInput(k.incoming[:0])
    83  	w := Focused()
    84  	if w != nil {
    85  		for _, key := range k.incoming {
    86  			_, err := w.HandleKeyboard(key.Key, key.Rune)
    87  			if err != nil {
    88  				return err
    89  			}
    90  		}
    91  	}
    92  	k.k.Draw(screen)
    93  	return nil
    94  }
    95  

View as plain text