Debian Codegraph @

drivers/apple_embedded/godot_keyboard_input_view.mm

raw
1 /**************************************************************************/
2 /* godot_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
31 #import "godot_keyboard_input_view.h"
32
33 #import "display_server_apple_embedded.h"
34 #import "os_apple_embedded.h"
35
36 #include "core/os/keyboard.h"
37
38 @interface GDTKeyboardInputView () <UITextViewDelegate>
39
40 @property(nonatomic, copy) NSString *previousText;
41 @property(nonatomic, assign) NSRange previousSelectedRange;
42
43 @end
44
45 @implementation GDTKeyboardInputView
46
47 - (instancetype)initWithCoder:(NSCoder *)coder {
48 self = [super initWithCoder:coder];
49
50 if (self) {
51 [self godot_commonInit];
52 }
53
54 return self;
55 }
56
57 - (instancetype)initWithFrame:(CGRect)frame textContainer:(NSTextContainer *)textContainer {
58 self = [super initWithFrame:frame textContainer:textContainer];
59
60 if (self) {
61 [self godot_commonInit];
62 }
63
64 return self;
65 }
66
67 - (void)godot_commonInit {
68 self.hidden = YES;
69 self.delegate = self;
70
71 [[NSNotificationCenter defaultCenter] addObserver:self
72 selector:@selector(observeTextChange:)
73 name:UITextViewTextDidChangeNotification
74 object:self];
75 }
76
77 - (void)dealloc {
78 self.delegate = nil;
79 [[NSNotificationCenter defaultCenter] removeObserver:self];
80 }
81
82 // MARK: Keyboard
83
84 - (BOOL)canBecomeFirstResponder {
85 return YES;
86 }
87
88 - (BOOL)becomeFirstResponderWithString:(NSString *)existingString cursorStart:(NSInteger)start cursorEnd:(NSInteger)end {
89 self.text = existingString;
90 self.previousText = existingString;
91
92 NSInteger safeStartIndex = MAX(start, 0);
93
94 NSRange textRange;
95
96 // Either a simple cursor or a selection.
97 if (end > 0) {
98 textRange = NSMakeRange(safeStartIndex, end - start);
99 } else {
100 textRange = NSMakeRange(safeStartIndex, 0);
101 }
102
103 self.selectedRange = textRange;
104 self.previousSelectedRange = textRange;
105
106 return [self becomeFirstResponder];
107 }
108
109 - (BOOL)resignFirstResponder {
110 self.text = nil;
111 self.previousText = nil;
112 return [super resignFirstResponder];
113 }
114
115 // MARK: OS Messages
116
117 - (void)deleteText:(NSInteger)charactersToDelete {
118 for (int i = 0; i < charactersToDelete; i++) {
119 DisplayServerAppleEmbedded::get_singleton()->key(Key::BACKSPACE, 0, Key::BACKSPACE, Key::NONE, 0, true, KeyLocation::UNSPECIFIED);
120 DisplayServerAppleEmbedded::get_singleton()->key(Key::BACKSPACE, 0, Key::BACKSPACE, Key::NONE, 0, false, KeyLocation::UNSPECIFIED);
121 }
122 }
123
124 - (void)enterText:(NSString *)substring {
125 String characters = String::utf8([substring UTF8String]);
126
127 for (int i = 0; i < characters.size(); i++) {
128 int character = characters[i];
129 Key key = Key::NONE;
130
131 if (character == '\t') { // 0x09
132 key = Key::TAB;
133 } else if (character == '\n') { // 0x0A
134 key = Key::ENTER;
135 } else if (character == 0x2006) {
136 key = Key::SPACE;
137 }
138
139 DisplayServerAppleEmbedded::get_singleton()->key(key, character, key, Key::NONE, 0, true, KeyLocation::UNSPECIFIED);
140 DisplayServerAppleEmbedded::get_singleton()->key(key, character, key, Key::NONE, 0, false, KeyLocation::UNSPECIFIED);
141 }
142 }
143
144 // MARK: Observer
145
146 - (void)observeTextChange:(NSNotification *)notification {
147 if (notification.object != self) {
148 return;
149 }
150
151 NSString *substringToDelete = nil;
152 if (self.previousSelectedRange.length == 0) {
153 // Get previous text to delete.
154 substringToDelete = [self.previousText substringToIndex:self.previousSelectedRange.location];
155 } else {
156 // If text was previously selected we are sending only one `backspace`. It will remove all text from text input.
157 [self deleteText:1];
158 }
159
160 NSString *substringToEnter = nil;
161 if (self.selectedRange.length == 0) {
162 // If previous cursor had a selection we have to calculate an inserted text.
163 if (self.previousSelectedRange.length != 0) {
164 NSInteger rangeEnd = self.selectedRange.location + self.selectedRange.length;
165 NSInteger rangeStart = MIN(self.previousSelectedRange.location, self.selectedRange.location);
166 NSInteger rangeLength = MAX(0, rangeEnd - rangeStart);
167
168 NSRange calculatedRange;
169
170 if (rangeLength >= 0) {
171 calculatedRange = NSMakeRange(rangeStart, rangeLength);
172 } else {
173 calculatedRange = NSMakeRange(rangeStart, 0);
174 }
175
176 substringToEnter = [self.text substringWithRange:calculatedRange];
177 } else {
178 substringToEnter = [self.text substringToIndex:self.selectedRange.location];
179 }
180 } else {
181 substringToEnter = [self.text substringWithRange:self.selectedRange];
182 }
183
184 NSInteger skip = 0;
185 if (substringToDelete != nil) {
186 for (NSUInteger i = 0; i < MIN([substringToDelete length], [substringToEnter length]); i++) {
187 if ([substringToDelete characterAtIndex:i] == [substringToEnter characterAtIndex:i]) {
188 skip++;
189 } else {
190 break;
191 }
192 }
193 [self deleteText:[substringToDelete length] - skip]; // Delete changed part of previous text.
194 }
195 [self enterText:[substringToEnter substringFromIndex:skip]]; // Enter changed part of new text.
196
197 self.previousText = self.text;
198 self.previousSelectedRange = self.selectedRange;
199 }
200
201 @end