textinput_noime.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2024 The Ebitengine 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. //go:build (!darwin && !js && !windows) || ios
  15. package textinput
  16. import (
  17. "github.com/hajimehoshi/ebiten/v2"
  18. "github.com/hajimehoshi/ebiten/v2/internal/ui"
  19. )
  20. type textInput struct {
  21. rs []rune
  22. lastTick uint64
  23. }
  24. var theTextInput textInput
  25. func (t *textInput) Start(x, y int) (chan State, func()) {
  26. // AppendInputChars is updated only when the tick is updated.
  27. // If the tick is not updated, return nil immediately.
  28. tick := ui.Get().Tick()
  29. if t.lastTick == tick {
  30. return nil, nil
  31. }
  32. defer func() {
  33. t.lastTick = tick
  34. }()
  35. s := newSession()
  36. // This is a pseudo implementation with AppendInputChars without IME.
  37. // This is tentative and should be replaced with IME in the future.
  38. t.rs = ebiten.AppendInputChars(t.rs[:0])
  39. if len(t.rs) == 0 {
  40. return nil, nil
  41. }
  42. s.ch <- State{
  43. Text: string(t.rs),
  44. Committed: true,
  45. }
  46. // Keep the channel as end() resets s.ch.
  47. ch := s.ch
  48. s.end()
  49. return ch, nil
  50. }