Browse Source

Optimize drawing pressed keys

master
Trevor Slocum 3 days ago
parent
commit
ff90b25a4b
  1. 26
      keyboard.go

26
keyboard.go

@ -243,7 +243,6 @@ func (k *Keyboard) Update() error {
}
}
key.pressed = true
k.backgroundDirty = true
k.Hit(key)
@ -252,7 +251,6 @@ func (k *Keyboard) Update() error {
time.Sleep(50 * time.Millisecond)
key.pressed = false
k.backgroundDirty = true
ebiten.ScheduleFrame()
}()
}
@ -262,7 +260,6 @@ func (k *Keyboard) Update() error {
key := k.at(x, y)
if key != nil {
key.pressed = true
k.backgroundDirty = true
}
}
// Handle touch input
@ -273,7 +270,6 @@ func (k *Keyboard) Update() error {
key := k.at(x, y)
if key != nil {
key.pressed = true
k.backgroundDirty = true
k.Hit(key)
@ -282,7 +278,6 @@ func (k *Keyboard) Update() error {
time.Sleep(50 * time.Millisecond)
key.pressed = false
k.backgroundDirty = true
ebiten.ScheduleFrame()
}()
}
@ -326,10 +321,6 @@ func (k *Keyboard) drawBackground() {
y := key.h / 2
text.Draw(img, label, k.labelFont, x, y, color.White)
if key.pressed {
k.op.ColorM.Scale(0.75, 0.75, 0.75, 1)
}
// Draw key
k.op.GeoM.Reset()
k.op.GeoM.Translate(float64(key.x), float64(key.y))
@ -356,6 +347,23 @@ func (k *Keyboard) Draw(target *ebiten.Image) {
k.op.ColorM.Scale(1, 1, 1, k.alpha)
target.DrawImage(k.background, k.op)
k.op.ColorM.Reset()
// Draw pressed keys
for _, rowKeys := range k.keys {
for _, key := range rowKeys {
if !key.pressed {
continue
}
// TODO buffer to prevent issues with alpha channel
k.op.GeoM.Reset()
k.op.GeoM.Translate(float64(k.x+key.x), float64(k.y+key.y))
k.op.ColorM.Scale(0.75, 0.75, 0.75, k.alpha)
target.DrawImage(k.background.SubImage(image.Rect(key.x, key.y, key.x+key.w, key.y+key.h)).(*ebiten.Image), k.op)
k.op.ColorM.Reset()
}
}
}
// SetAllowUserHide sets a flag that controls whether the widget may be hidden

Loading…
Cancel
Save