1
0

main.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2015 Hajime Hoshi
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "bytes"
  17. "image"
  18. _ "image/png"
  19. "log"
  20. "strings"
  21. "github.com/hajimehoshi/bitmapfont/v3"
  22. "github.com/hajimehoshi/ebiten/v2"
  23. "github.com/hajimehoshi/ebiten/v2/examples/keyboard/keyboard"
  24. rkeyboard "github.com/hajimehoshi/ebiten/v2/examples/resources/images/keyboard"
  25. "github.com/hajimehoshi/ebiten/v2/inpututil"
  26. "github.com/hajimehoshi/ebiten/v2/text/v2"
  27. )
  28. const (
  29. screenWidth = 320
  30. screenHeight = 240
  31. )
  32. var fontFace = text.NewGoXFace(bitmapfont.Face)
  33. var keyboardImage *ebiten.Image
  34. func init() {
  35. img, _, err := image.Decode(bytes.NewReader(rkeyboard.Keyboard_png))
  36. if err != nil {
  37. log.Fatal(err)
  38. }
  39. keyboardImage = ebiten.NewImageFromImage(img)
  40. }
  41. type Game struct {
  42. keys []ebiten.Key
  43. }
  44. func (g *Game) Update() error {
  45. g.keys = inpututil.AppendPressedKeys(g.keys[:0])
  46. return nil
  47. }
  48. func (g *Game) Draw(screen *ebiten.Image) {
  49. const (
  50. offsetX = 24
  51. offsetY = 40
  52. )
  53. // Draw the base (grayed) keyboard image.
  54. op := &ebiten.DrawImageOptions{}
  55. op.GeoM.Translate(offsetX, offsetY)
  56. op.ColorScale.Scale(0.5, 0.5, 0.5, 1)
  57. screen.DrawImage(keyboardImage, op)
  58. // Draw the highlighted keys.
  59. op = &ebiten.DrawImageOptions{}
  60. for _, p := range g.keys {
  61. op.GeoM.Reset()
  62. r, ok := keyboard.KeyRect(p)
  63. if !ok {
  64. continue
  65. }
  66. op.GeoM.Translate(float64(r.Min.X), float64(r.Min.Y))
  67. op.GeoM.Translate(offsetX, offsetY)
  68. screen.DrawImage(keyboardImage.SubImage(r).(*ebiten.Image), op)
  69. }
  70. var keyStrs []string
  71. var keyNames []string
  72. for _, k := range g.keys {
  73. keyStrs = append(keyStrs, k.String())
  74. if name := ebiten.KeyName(k); name != "" {
  75. keyNames = append(keyNames, name)
  76. }
  77. }
  78. // Use bitmapfont.Face instead of ebitenutil.DebugPrint, since some key names might not be printed with DebugPrint.
  79. textOp := &text.DrawOptions{}
  80. textOp.LineSpacing = fontFace.Metrics().HLineGap + fontFace.Metrics().HAscent + fontFace.Metrics().HDescent
  81. text.Draw(screen, strings.Join(keyStrs, ", ")+"\n"+strings.Join(keyNames, ", "), fontFace, textOp)
  82. }
  83. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  84. return screenWidth, screenHeight
  85. }
  86. func main() {
  87. ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
  88. ebiten.SetWindowTitle("Keyboard (Ebitengine Demo)")
  89. if err := ebiten.RunGame(&Game{}); err != nil {
  90. log.Fatal(err)
  91. }
  92. }