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