|
@@ -10,49 +10,49 @@
|
|
|
|
|
|
#import "GameChatKeyboardHandler.h"
|
|
|
|
|
|
-#include <SDL_events.h>
|
|
|
-
|
|
|
static int watchReturnKey(void * userdata, SDL_Event * event);
|
|
|
|
|
|
-static void sendKeyEvent(SDL_KeyCode keyCode)
|
|
|
+
|
|
|
+@interface GameChatKeyboardHandler ()
|
|
|
+@property (nonatomic) BOOL wasChatMessageSent;
|
|
|
+@end
|
|
|
+
|
|
|
+@implementation GameChatKeyboardHandler
|
|
|
+
|
|
|
++ (void)sendKeyEventWithKeyCode:(SDL_KeyCode)keyCode
|
|
|
{
|
|
|
SDL_Event keyEvent;
|
|
|
keyEvent.key = (SDL_KeyboardEvent){
|
|
|
.type = SDL_KEYDOWN,
|
|
|
+ .state = SDL_PRESSED,
|
|
|
.keysym.sym = keyCode,
|
|
|
};
|
|
|
SDL_PushEvent(&keyEvent);
|
|
|
}
|
|
|
|
|
|
+- (instancetype)init {
|
|
|
+ self = [super init];
|
|
|
|
|
|
-@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);
|
|
|
+ return self;
|
|
|
}
|
|
|
|
|
|
#pragma mark - Notifications
|
|
|
|
|
|
- (void)textDidBeginEditing:(NSNotification *)n {
|
|
|
+ self.wasChatMessageSent = NO;
|
|
|
+
|
|
|
// 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);
|
|
|
+ [[self class] sendKeyEventWithKeyCode:SDLK_ESCAPE];
|
|
|
}
|
|
|
|
|
|
@end
|