textinput.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2023 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. // Package textinput provides a text-inputting controller.
  15. // This package is experimental and the API might be changed in the future.
  16. //
  17. // This package is supported by macOS and Web browsers so far.
  18. package textinput
  19. import (
  20. "unicode/utf16"
  21. "github.com/hajimehoshi/ebiten/v2/internal/ui"
  22. )
  23. // State represents the current state of text inputting.
  24. //
  25. // State is the low-level API. For most use cases, Field is easier to use.
  26. type State struct {
  27. // Text represents the current inputting text.
  28. Text string
  29. // CompositionSelectionStartInBytes represents the start position of the selection in bytes.
  30. CompositionSelectionStartInBytes int
  31. // CompositionSelectionStartInBytes represents the end position of the selection in bytes.
  32. CompositionSelectionEndInBytes int
  33. // Committed reports whether the current Text is the settled text.
  34. Committed bool
  35. // Error is an error that happens during text inputting.
  36. Error error
  37. }
  38. // Start starts text inputting.
  39. // Start returns a channel to send the state repeatedly, and a function to end the text inputting.
  40. //
  41. // Start is the low-level API. For most use cases, Field is easier to use.
  42. //
  43. // Start returns nil and nil if the current environment doesn't support this package.
  44. func Start(x, y int) (states chan State, close func()) {
  45. cx, cy := ui.Get().LogicalPositionToClientPositionInNativePixels(float64(x), float64(y))
  46. return theTextInput.Start(int(cx), int(cy))
  47. }
  48. func convertUTF16CountToByteCount(text string, c int) int {
  49. return len(string(utf16.Decode(utf16.Encode([]rune(text))[:c])))
  50. }
  51. type session struct {
  52. ch chan State
  53. done chan struct{}
  54. }
  55. func newSession() *session {
  56. return &session{
  57. ch: make(chan State, 1),
  58. done: make(chan struct{}),
  59. }
  60. }
  61. func (s *session) end() {
  62. if s.ch == nil {
  63. return
  64. }
  65. close(s.ch)
  66. s.ch = nil
  67. close(s.done)
  68. }
  69. func (s *session) trySend(state State) {
  70. for {
  71. select {
  72. case s.ch <- state:
  73. return
  74. default:
  75. // Only the last value matters.
  76. select {
  77. case <-s.ch:
  78. case <-s.done:
  79. return
  80. }
  81. }
  82. }
  83. }