فهرست منبع

Merge pull request #1013 from kambala-decapitator/ios-chat-text-enter

use SDL's standard input handling for in-game console
Andrii Danylchenko 3 سال پیش
والد
کامیت
5f5a259974

+ 2 - 2
client/CMakeLists.txt

@@ -148,14 +148,14 @@ set(client_HEADERS
 if(APPLE_IOS)
 	set(client_SRCS ${client_SRCS}
 		CFocusableHelper.cpp
-		ios/GameChatKeyboardHanlder.m
+		ios/GameChatKeyboardHandler.m
 		ios/main.m
 		ios/startSDL.mm
 		ios/utils.mm
 	)
 	set(client_HEADERS ${client_HEADERS}
 		CFocusableHelper.h
-		ios/GameChatKeyboardHanlder.h
+		ios/GameChatKeyboardHandler.h
 		ios/startSDL.h
 		ios/utils.h
 	)

+ 2 - 4
client/ios/GameChatKeyboardHanlder.h → client/ios/GameChatKeyboardHandler.h

@@ -1,5 +1,5 @@
 /*
- * GameChatKeyboardHanlder.h, part of VCMI engine
+ * GameChatKeyboardHandler.h, part of VCMI engine
  *
  * Authors: listed in file AUTHORS in main folder
  *
@@ -12,9 +12,7 @@
 
 NS_ASSUME_NONNULL_BEGIN
 
-@interface GameChatKeyboardHanlder : NSObject
-
-@property (nonatomic, weak) UITextField * textFieldSDL;
+@interface GameChatKeyboardHandler : NSObject
 
 - (void)triggerInput;
 

+ 70 - 0
client/ios/GameChatKeyboardHandler.m

@@ -0,0 +1,70 @@
+/*
+ * GameChatKeyboardHandler.m, part of VCMI engine
+ *
+ * Authors: listed in file AUTHORS in main folder
+ *
+ * License: GNU General Public License v2.0 or later
+ * Full text of license available in license.txt file, in main folder
+ *
+ */
+
+#import "GameChatKeyboardHandler.h"
+
+#include <SDL_events.h>
+
+static int watchReturnKey(void * userdata, SDL_Event * event);
+
+static void sendKeyEvent(SDL_KeyCode keyCode)
+{
+	SDL_Event keyEvent;
+	keyEvent.key = (SDL_KeyboardEvent){
+		.type = SDL_KEYDOWN,
+		.keysym.sym = keyCode,
+	};
+	SDL_PushEvent(&keyEvent);
+}
+
+
+@interface GameChatKeyboardHandler ()
+@property (nonatomic) BOOL wasChatMessageSent;
+@end
+
+@implementation GameChatKeyboardHandler
+
+- (void)triggerInput {
+	__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];
+
+	self.wasChatMessageSent = NO;
+	sendKeyEvent(SDLK_TAB);
+}
+
+#pragma mark - Notifications
+
+- (void)textDidBeginEditing:(NSNotification *)n {
+	// 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];
+
+	// discard chat message
+	if(!self.wasChatMessageSent)
+		sendKeyEvent(SDLK_ESCAPE);
+}
+
+@end
+
+
+static int watchReturnKey(void * userdata, SDL_Event * event)
+{
+	if(event->type == SDL_KEYDOWN && event->key.keysym.scancode == SDL_SCANCODE_RETURN)
+	{
+		__auto_type self = (__bridge GameChatKeyboardHandler *)userdata;
+		self.wasChatMessageSent = YES;
+		SDL_DelEventWatch(watchReturnKey, userdata);
+	}
+	return 1;
+}

+ 0 - 107
client/ios/GameChatKeyboardHanlder.m

@@ -1,107 +0,0 @@
-/*
- * GameChatKeyboardHanlder.m, part of VCMI engine
- *
- * Authors: listed in file AUTHORS in main folder
- *
- * License: GNU General Public License v2.0 or later
- * Full text of license available in license.txt file, in main folder
- *
- */
-
-#import "GameChatKeyboardHanlder.h"
-
-#include <SDL_events.h>
-
-static int watchReturnKey(void * userdata, SDL_Event * event);
-
-static void sendKeyEvent(SDL_KeyCode keyCode)
-{
-	SDL_Event keyEvent;
-	keyEvent.key = (SDL_KeyboardEvent){
-		.type = SDL_KEYDOWN,
-		.keysym.sym = 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 GameChatKeyboardHanlder ()
-@property (nonatomic) BOOL wasChatMessageSent;
-@end
-
-@implementation GameChatKeyboardHanlder
-
-- (void)triggerInput {
-	__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
-
-
-static int watchReturnKey(void * userdata, SDL_Event * event)
-{
-	if(event->type == SDL_KEYDOWN && event->key.keysym.scancode == SDL_SCANCODE_RETURN)
-	{
-		__auto_type self = (__bridge GameChatKeyboardHanlder *)userdata;
-		self.wasChatMessageSent = YES;
-		SDL_DelEventWatch(watchReturnKey, userdata);
-	}
-	return 1;
-}

+ 3 - 23
client/ios/startSDL.mm

@@ -8,7 +8,7 @@
  *
  */
 #import "startSDL.h"
-#import "GameChatKeyboardHanlder.h"
+#import "GameChatKeyboardHandler.h"
 
 #include "../Global.h"
 #include "CMT.h"
@@ -23,7 +23,7 @@
 #import <UIKit/UIKit.h>
 
 @interface SDLViewObserver : NSObject <UIGestureRecognizerDelegate>
-@property (nonatomic, strong) GameChatKeyboardHanlder * gameChatHandler;
+@property (nonatomic, strong) GameChatKeyboardHandler * gameChatHandler;
 @end
 
 @implementation SDLViewObserver
@@ -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];
@@ -131,7 +111,7 @@ int startSDL(int argc, char * argv[], BOOL startManually)
 {
 	@autoreleasepool {
 		auto observer = [SDLViewObserver new];
-		observer.gameChatHandler = [GameChatKeyboardHanlder new];
+		observer.gameChatHandler = [GameChatKeyboardHandler new];
 
 		id __block sdlWindowCreationObserver = [NSNotificationCenter.defaultCenter addObserverForName:UIWindowDidBecomeKeyNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
 			[NSNotificationCenter.defaultCenter removeObserver:sdlWindowCreationObserver];

+ 1 - 1
client/widgets/AdventureMapClasses.cpp

@@ -1129,7 +1129,7 @@ void CInGameConsole::textEdited(const SDL_TextEditingEvent & event)
 
 void CInGameConsole::startEnteringText()
 {
-	CSDL_Ext::startTextInput(&pos);
+	CSDL_Ext::startTextInput(&GH.statusbar->pos);
 
 	enteredText = "_";
 	if(GH.topInt() == adventureInt)