keyboard_input_view.mm 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**************************************************************************/
  2. /* keyboard_input_view.mm */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #import "display_server_ios.h"
  32. #import "os_ios.h"
  33. #include "core/os/keyboard.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 cursorStart:(NSInteger)start cursorEnd:(NSInteger)end {
  70. self.text = existingString;
  71. self.previousText = existingString;
  72. NSInteger safeStartIndex = MAX(start, 0);
  73. NSRange textRange;
  74. // Either a simple cursor or a selection.
  75. if (end > 0) {
  76. textRange = NSMakeRange(safeStartIndex, end - start);
  77. } else {
  78. textRange = NSMakeRange(safeStartIndex, 0);
  79. }
  80. self.selectedRange = textRange;
  81. self.previousSelectedRange = textRange;
  82. return [self becomeFirstResponder];
  83. }
  84. - (BOOL)resignFirstResponder {
  85. self.text = nil;
  86. self.previousText = nil;
  87. return [super resignFirstResponder];
  88. }
  89. // MARK: OS Messages
  90. - (void)deleteText:(NSInteger)charactersToDelete {
  91. for (int i = 0; i < charactersToDelete; i++) {
  92. DisplayServerIOS::get_singleton()->key(Key::BACKSPACE, 0, Key::BACKSPACE, Key::NONE, 0, true, KeyLocation::UNSPECIFIED);
  93. DisplayServerIOS::get_singleton()->key(Key::BACKSPACE, 0, Key::BACKSPACE, Key::NONE, 0, false, KeyLocation::UNSPECIFIED);
  94. }
  95. }
  96. - (void)enterText:(NSString *)substring {
  97. String characters;
  98. characters.parse_utf8([substring UTF8String]);
  99. for (int i = 0; i < characters.size(); i++) {
  100. int character = characters[i];
  101. Key key = Key::NONE;
  102. if (character == '\t') { // 0x09
  103. key = Key::TAB;
  104. } else if (character == '\n') { // 0x0A
  105. key = Key::ENTER;
  106. } else if (character == 0x2006) {
  107. key = Key::SPACE;
  108. }
  109. DisplayServerIOS::get_singleton()->key(key, character, key, Key::NONE, 0, true, KeyLocation::UNSPECIFIED);
  110. DisplayServerIOS::get_singleton()->key(key, character, key, Key::NONE, 0, false, KeyLocation::UNSPECIFIED);
  111. }
  112. }
  113. // MARK: Observer
  114. - (void)observeTextChange:(NSNotification *)notification {
  115. if (notification.object != self) {
  116. return;
  117. }
  118. NSString *substringToDelete = nil;
  119. if (self.previousSelectedRange.length == 0) {
  120. // Get previous text to delete.
  121. substringToDelete = [self.previousText substringToIndex:self.previousSelectedRange.location];
  122. } else {
  123. // If text was previously selected we are sending only one `backspace`. It will remove all text from text input.
  124. [self deleteText:1];
  125. }
  126. NSString *substringToEnter = nil;
  127. if (self.selectedRange.length == 0) {
  128. // If previous cursor had a selection we have to calculate an inserted text.
  129. if (self.previousSelectedRange.length != 0) {
  130. NSInteger rangeEnd = self.selectedRange.location + self.selectedRange.length;
  131. NSInteger rangeStart = MIN(self.previousSelectedRange.location, self.selectedRange.location);
  132. NSInteger rangeLength = MAX(0, rangeEnd - rangeStart);
  133. NSRange calculatedRange;
  134. if (rangeLength >= 0) {
  135. calculatedRange = NSMakeRange(rangeStart, rangeLength);
  136. } else {
  137. calculatedRange = NSMakeRange(rangeStart, 0);
  138. }
  139. substringToEnter = [self.text substringWithRange:calculatedRange];
  140. } else {
  141. substringToEnter = [self.text substringToIndex:self.selectedRange.location];
  142. }
  143. } else {
  144. substringToEnter = [self.text substringWithRange:self.selectedRange];
  145. }
  146. NSInteger skip = 0;
  147. if (substringToDelete != nil) {
  148. for (NSUInteger i = 0; i < MIN([substringToDelete length], [substringToEnter length]); i++) {
  149. if ([substringToDelete characterAtIndex:i] == [substringToEnter characterAtIndex:i]) {
  150. skip++;
  151. } else {
  152. break;
  153. }
  154. }
  155. [self deleteText:[substringToDelete length] - skip]; // Delete changed part of previous text.
  156. }
  157. [self enterText:[substringToEnter substringFromIndex:skip]]; // Enter changed part of new text.
  158. self.previousText = self.text;
  159. self.previousSelectedRange = self.selectedRange;
  160. }
  161. @end