...
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
12
13 type Keyboard struct {
14 *Box
15 k *kibodo.Keyboard
16 incoming []*kibodo.Input
17 }
18
19
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
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
38 func (k *Keyboard) Visible() bool {
39 k.Lock()
40 defer k.Unlock()
41 return k.visible && k.k.Visible()
42 }
43
44
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
57 func (k *Keyboard) Keys() [][]*kibodo.Key {
58 k.Lock()
59 defer k.Unlock()
60 return k.k.GetKeys()
61 }
62
63
64 func (k *Keyboard) SetKeys(keys [][]*kibodo.Key) {
65 k.Lock()
66 defer k.Unlock()
67 k.k.SetKeys(keys)
68 }
69
70
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
78 func (k *Keyboard) SetExtendedKeys(keys [][]*kibodo.Key) {
79 k.Lock()
80 defer k.Unlock()
81 k.k.SetExtendedKeys(keys)
82 }
83
84
85 func (k *Keyboard) SetShowExtended(show bool) {
86 k.Lock()
87 defer k.Unlock()
88 k.k.SetShowExtended(show)
89 }
90
91
92 func (k *Keyboard) SetScheduleFrameFunc(f func()) {
93 k.Lock()
94 defer k.Unlock()
95 k.k.SetScheduleFrameFunc(f)
96 }
97
98
99
100 func (k *Keyboard) Cursor() ebiten.CursorShapeType {
101 return ebiten.CursorShapePointer
102 }
103
104
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
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