...
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
11
12 type Keyboard struct {
13 *Box
14 k *kibodo.Keyboard
15 incoming []*kibodo.Input
16 }
17
18
19 func NewKeyboard() *Keyboard {
20 k := kibodo.NewKeyboard()
21 k.Show()
22 return &Keyboard{
23 Box: NewBox(),
24 k: k,
25 }
26 }
27
28
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
37 func (k *Keyboard) GetKeys() [][]*kibodo.Key {
38 k.Lock()
39 defer k.Unlock()
40 return k.k.GetKeys()
41 }
42
43
44 func (k *Keyboard) SetKeys(keys [][]*kibodo.Key) {
45 k.Lock()
46 defer k.Unlock()
47 k.k.SetKeys(keys)
48 }
49
50
51 func (k *Keyboard) SetExtendedKeys(keys [][]*kibodo.Key) {
52 k.Lock()
53 defer k.Unlock()
54 k.k.SetExtendedKeys(keys)
55 }
56
57
58 func (k *Keyboard) SetShowExtended(show bool) {
59 k.Lock()
60 defer k.Unlock()
61 k.k.SetShowExtended(show)
62 }
63
64
65 func (k *Keyboard) SetScheduleFrameFunc(f func()) {
66 k.Lock()
67 defer k.Unlock()
68 k.k.SetScheduleFrameFunc(f)
69 }
70
71
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
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