main.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2017 The Ebiten Authors
  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. "log"
  17. "strings"
  18. "github.com/hajimehoshi/ebiten/v2"
  19. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  20. "github.com/hajimehoshi/ebiten/v2/inpututil"
  21. )
  22. const (
  23. screenWidth = 640
  24. screenHeight = 480
  25. )
  26. // repeatingKeyPressed return true when key is pressed considering the repeat state.
  27. func repeatingKeyPressed(key ebiten.Key) bool {
  28. const (
  29. delay = 30
  30. interval = 3
  31. )
  32. d := inpututil.KeyPressDuration(key)
  33. if d == 1 {
  34. return true
  35. }
  36. if d >= delay && (d-delay)%interval == 0 {
  37. return true
  38. }
  39. return false
  40. }
  41. type Game struct {
  42. runes []rune
  43. text string
  44. counter int
  45. }
  46. func (g *Game) Update() error {
  47. // Add runes that are input by the user by AppendInputChars.
  48. // Note that AppendInputChars result changes every frame, so you need to call this
  49. // every frame.
  50. g.runes = ebiten.AppendInputChars(g.runes[:0])
  51. g.text += string(g.runes)
  52. // Adjust the string to be at most 10 lines.
  53. ss := strings.Split(g.text, "\n")
  54. if len(ss) > 10 {
  55. g.text = strings.Join(ss[len(ss)-10:], "\n")
  56. }
  57. // If the enter key is pressed, add a line break.
  58. if repeatingKeyPressed(ebiten.KeyEnter) || repeatingKeyPressed(ebiten.KeyNumpadEnter) {
  59. g.text += "\n"
  60. }
  61. // If the backspace key is pressed, remove one character.
  62. if repeatingKeyPressed(ebiten.KeyBackspace) {
  63. if len(g.text) >= 1 {
  64. g.text = g.text[:len(g.text)-1]
  65. }
  66. }
  67. g.counter++
  68. return nil
  69. }
  70. func (g *Game) Draw(screen *ebiten.Image) {
  71. // Blink the cursor.
  72. t := g.text
  73. if g.counter%60 < 30 {
  74. t += "_"
  75. }
  76. ebitenutil.DebugPrint(screen, t)
  77. }
  78. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  79. return screenWidth, screenHeight
  80. }
  81. func main() {
  82. g := &Game{
  83. text: "Type on the keyboard:\n",
  84. counter: 0,
  85. }
  86. ebiten.SetWindowSize(screenWidth, screenHeight)
  87. ebiten.SetWindowTitle("TypeWriter (Ebitengine Demo)")
  88. if err := ebiten.RunGame(g); err != nil {
  89. log.Fatal(err)
  90. }
  91. }