...
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(Style.TextFont)
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) Visible() bool {
38 k.Lock()
39 defer k.Unlock()
40 return k.visible && k.k.Visible()
41 }
42
43
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
56 func (k *Keyboard) Keys() [][]*kibodo.Key {
57 k.Lock()
58 defer k.Unlock()
59 return k.k.GetKeys()
60 }
61
62
63 func (k *Keyboard) SetKeys(keys [][]*kibodo.Key) {
64 k.Lock()
65 defer k.Unlock()
66 k.k.SetKeys(keys)
67 }
68
69
70 func (k *Keyboard) SetExtendedKeys(keys [][]*kibodo.Key) {
71 k.Lock()
72 defer k.Unlock()
73 k.k.SetExtendedKeys(keys)
74 }
75
76
77 func (k *Keyboard) SetShowExtended(show bool) {
78 k.Lock()
79 defer k.Unlock()
80 k.k.SetShowExtended(show)
81 }
82
83
84 func (k *Keyboard) SetScheduleFrameFunc(f func()) {
85 k.Lock()
86 defer k.Unlock()
87 k.k.SetScheduleFrameFunc(f)
88 }
89
90
91
92 func (k *Keyboard) Cursor() ebiten.CursorShapeType {
93 return ebiten.CursorShapePointer
94 }
95
96
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
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