1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-02-03 13:01:33 +02:00

[iOS] use SDL's standard input handling for in-game chat

This commit is contained in:
Andrey Filipenkov 2022-09-29 13:23:15 +03:00
parent e8a1709fc4
commit 593df25df5
3 changed files with 0 additions and 59 deletions

View File

@ -14,8 +14,6 @@ NS_ASSUME_NONNULL_BEGIN
@interface GameChatKeyboardHandler : NSObject
@property (nonatomic, weak) UITextField * textFieldSDL;
- (void)triggerInput;
@end

View File

@ -24,13 +24,6 @@ static void sendKeyEvent(SDL_KeyCode keyCode)
SDL_PushEvent(&keyEvent);
}
static CGRect keyboardFrame(NSNotification * n, NSString * userInfoKey)
{
return [n.userInfo[userInfoKey] CGRectValue];
}
static CGRect keyboardFrameBegin(NSNotification * n) { return keyboardFrame(n, UIKeyboardFrameBeginUserInfoKey); }
static CGRect keyboardFrameEnd (NSNotification * n) { return keyboardFrame(n, UIKeyboardFrameEndUserInfoKey); }
@interface GameChatKeyboardHandler ()
@property (nonatomic) BOOL wasChatMessageSent;
@ -42,56 +35,26 @@ static CGRect keyboardFrameEnd (NSNotification * n) { return keyboardFrame(n, U
__auto_type notificationCenter = NSNotificationCenter.defaultCenter;
[notificationCenter addObserver:self selector:@selector(textDidBeginEditing:) name:UITextFieldTextDidBeginEditingNotification object:nil];
[notificationCenter addObserver:self selector:@selector(textDidEndEditing:) name:UITextFieldTextDidEndEditingNotification object:nil];
[notificationCenter addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
[notificationCenter addObserver:self selector:@selector(keyboardDidChangeFrame:) name:UIKeyboardDidChangeFrameNotification object:nil];
self.wasChatMessageSent = NO;
sendKeyEvent(SDLK_TAB);
}
- (void)positionTextFieldAboveKeyboardRect:(CGRect)kbFrame {
__auto_type r = kbFrame;
r.size.height = CGRectGetHeight(self.textFieldSDL.frame);
r.origin.y -= r.size.height;
self.textFieldSDL.frame = r;
}
#pragma mark - Notifications
- (void)textDidBeginEditing:(NSNotification *)n {
self.textFieldSDL.hidden = NO;
self.textFieldSDL.text = nil;
// watch for pressing Return to ignore sending Escape key after keyboard is closed
SDL_AddEventWatch(watchReturnKey, (__bridge void *)self);
}
- (void)textDidEndEditing:(NSNotification *)n {
[NSNotificationCenter.defaultCenter removeObserver:self];
self.textFieldSDL.hidden = YES;
// discard chat message
if(!self.wasChatMessageSent)
sendKeyEvent(SDLK_ESCAPE);
}
- (void)keyboardWillChangeFrame:(NSNotification *)n {
// animate textfield together with keyboard
[UIView performWithoutAnimation:^{
[self positionTextFieldAboveKeyboardRect:keyboardFrameBegin(n)];
}];
NSTimeInterval kbAnimationDuration = [n.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
NSUInteger kbAnimationCurve = [n.userInfo[UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue];
[UIView animateWithDuration:kbAnimationDuration delay:0 options:(kbAnimationCurve << 16) animations:^{
[self positionTextFieldAboveKeyboardRect:keyboardFrameEnd(n)];
} completion:nil];
}
- (void)keyboardDidChangeFrame:(NSNotification *)n {
[self positionTextFieldAboveKeyboardRect:keyboardFrameEnd(n)];
}
@end

View File

@ -33,26 +33,6 @@
UIView * view = [object valueForKeyPath:keyPath];
UITextField * textField;
for (UIView * v in view.subviews) {
if ([v isKindOfClass:[UITextField class]]) {
textField = (UITextField *)v;
break;
}
}
auto r = textField.frame;
r.size.height = 40;
textField.frame = r;
self.gameChatHandler.textFieldSDL = textField;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
if(@available(iOS 13.0, *))
textField.backgroundColor = UIColor.systemBackgroundColor;
else
#endif
textField.backgroundColor = UIColor.whiteColor;
auto longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
longPress.minimumPressDuration = 0.2;
[view addGestureRecognizer:longPress];