...

Source file src/codeberg.org/tslocum/etk/keyboard.go

Documentation: codeberg.org/tslocum/etk

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

View as plain text