Optimize drawing pressed keys

This commit is contained in:
Trevor Slocum 2021-09-15 08:21:13 -07:00
parent 0aae5255cc
commit ff90b25a4b
1 changed files with 17 additions and 9 deletions

View File

@ -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