keyboard_input_view.mm 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*************************************************************************/
  2. /* keyboard_input_view.mm */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #import "keyboard_input_view.h"
  31. #include "core/os/keyboard.h"
  32. #include "display_server_iphone.h"
  33. #include "os_iphone.h"
  34. @interface GodotKeyboardInputView () <UITextViewDelegate>
  35. @property(nonatomic, copy) NSString *previousText;
  36. @property(nonatomic, assign) NSRange previousSelectedRange;
  37. @end
  38. @implementation GodotKeyboardInputView
  39. - (instancetype)initWithCoder:(NSCoder *)coder {
  40. self = [super initWithCoder:coder];
  41. if (self) {
  42. [self godot_commonInit];
  43. }
  44. return self;
  45. }
  46. - (instancetype)initWithFrame:(CGRect)frame textContainer:(NSTextContainer *)textContainer {
  47. self = [super initWithFrame:frame textContainer:textContainer];
  48. if (self) {
  49. [self godot_commonInit];
  50. }
  51. return self;
  52. }
  53. - (void)godot_commonInit {
  54. self.hidden = YES;
  55. self.delegate = self;
  56. [[NSNotificationCenter defaultCenter] addObserver:self
  57. selector:@selector(observeTextChange:)
  58. name:UITextViewTextDidChangeNotification
  59. object:self];
  60. }
  61. - (void)dealloc {
  62. self.delegate = nil;
  63. [[NSNotificationCenter defaultCenter] removeObserver:self];
  64. }
  65. // MARK: Keyboard
  66. - (BOOL)canBecomeFirstResponder {
  67. return YES;
  68. }
  69. - (BOOL)becomeFirstResponderWithString:(NSString *)existingString multiline:(BOOL)flag cursorStart:(NSInteger)start cursorEnd:(NSInteger)end {
  70. self.text = existingString;
  71. self.previousText = existingString;
  72. NSRange textRange;
  73. // Either a simple cursor or a selection.
  74. if (end > 0) {
  75. textRange = NSMakeRange(start, end - start);
  76. } else {
  77. textRange = NSMakeRange(start, 0);
  78. }
  79. self.selectedRange = textRange;
  80. self.previousSelectedRange = textRange;
  81. return [self becomeFirstResponder];
  82. }
  83. - (BOOL)resignFirstResponder {
  84. self.text = nil;
  85. self.previousText = nil;
  86. return [super resignFirstResponder];
  87. }
  88. // MARK: OS Messages
  89. - (void)deleteText:(NSInteger)charactersToDelete {
  90. for (int i = 0; i < charactersToDelete; i++) {
  91. DisplayServerIPhone::get_singleton()->key(KEY_BACKSPACE, true);
  92. DisplayServerIPhone::get_singleton()->key(KEY_BACKSPACE, false);
  93. }
  94. }
  95. - (void)enterText:(NSString *)substring {
  96. String characters;
  97. characters.parse_utf8([substring UTF8String]);
  98. for (int i = 0; i < characters.size(); i++) {
  99. int character = characters[i];
  100. switch (character) {
  101. case 10:
  102. character = KEY_ENTER;
  103. break;
  104. case 8198:
  105. character = KEY_SPACE;
  106. break;
  107. default:
  108. break;
  109. }
  110. DisplayServerIPhone::get_singleton()->key(character, true);
  111. DisplayServerIPhone::get_singleton()->key(character, false);
  112. }
  113. }
  114. // MARK: Observer
  115. - (void)observeTextChange:(NSNotification *)notification {
  116. if (notification.object != self) {
  117. return;
  118. }
  119. if (self.previousSelectedRange.length == 0) {
  120. // We are deleting all text before cursor if no range was selected.
  121. // This way any inserted or changed text will be updated.
  122. NSString *substringToDelete = [self.previousText substringToIndex:self.previousSelectedRange.location];
  123. [self deleteText:substringToDelete.length];
  124. } else {
  125. // If text was previously selected
  126. // we are sending only one `backspace`.
  127. // It will remove all text from text input.
  128. [self deleteText:1];
  129. }
  130. NSString *substringToEnter;
  131. if (self.selectedRange.length == 0) {
  132. // If previous cursor had a selection
  133. // we have to calculate an inserted text.
  134. if (self.previousSelectedRange.length != 0) {
  135. NSInteger rangeEnd = self.selectedRange.location + self.selectedRange.length;
  136. NSInteger rangeStart = MIN(self.previousSelectedRange.location, self.selectedRange.location);
  137. NSInteger rangeLength = MAX(0, rangeEnd - rangeStart);
  138. NSRange calculatedRange;
  139. if (rangeLength >= 0) {
  140. calculatedRange = NSMakeRange(rangeStart, rangeLength);
  141. } else {
  142. calculatedRange = NSMakeRange(rangeStart, 0);
  143. }
  144. substringToEnter = [self.text substringWithRange:calculatedRange];
  145. } else {
  146. substringToEnter = [self.text substringToIndex:self.selectedRange.location];
  147. }
  148. } else {
  149. substringToEnter = [self.text substringWithRange:self.selectedRange];
  150. }
  151. [self enterText:substringToEnter];
  152. self.previousText = self.text;
  153. self.previousSelectedRange = self.selectedRange;
  154. }
  155. @end