Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve iOS inputs #1706

Merged
merged 5 commits into from Jun 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion backends/platform/ios7/ios7_common.h
Expand Up @@ -40,7 +40,8 @@ enum InputEvent {
kInputApplicationSuspended,
kInputApplicationResumed,
kInputSwipe,
kInputTap
kInputTap,
kInputMainMenu
};

enum ScreenOrientation {
Expand Down
5 changes: 4 additions & 1 deletion backends/platform/ios7/ios7_keyboard.h
Expand Up @@ -26,15 +26,18 @@
#include <UIKit/UIKit.h>
#include <UIKit/UITextView.h>

@class TextInputHandler;

@interface SoftKeyboard : UIView<UITextViewDelegate> {
id inputDelegate;
UITextView *inputView;
TextInputHandler *inputView;
}

- (id)initWithFrame:(CGRect)frame;
- (UITextView *)inputView;
- (void)setInputDelegate:(id)delegate;
- (void)handleKeyPress:(unichar)c;
- (void)handleMainMenuKey;

- (void)showKeyboard;
- (void)hideKeyboard;
Expand Down
153 changes: 149 additions & 4 deletions backends/platform/ios7/ios7_keyboard.mm
Expand Up @@ -21,6 +21,7 @@
*/

#include "backends/platform/ios7/ios7_keyboard.h"
#include "common/keyboard.h"

@interface UITextInputTraits
- (void)setAutocorrectionType:(int)type;
Expand All @@ -30,9 +31,12 @@ - (void)setEnablesReturnKeyAutomatically:(BOOL)val;

@interface TextInputHandler : UITextView {
SoftKeyboard *softKeyboard;
UIToolbar *toolbar;
UIScrollView *scrollView;
}

- (id)initWithKeyboard:(SoftKeyboard *)keyboard;
- (void)updateToolbarSize;

@end

Expand All @@ -50,9 +54,65 @@ - (id)initWithKeyboard:(SoftKeyboard *)keyboard {
//item.leadingBarButtonGroups = @[];
//item.trailingBarButtonGroups = @[];

toolbar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.0f)] autorelease];
toolbar.barTintColor = keyboard.backgroundColor;
toolbar.tintColor = keyboard.tintColor;
toolbar.translucent = NO;

toolbar.items = @[
// GMM button
[[[UIBarButtonItem alloc] initWithTitle:@"\u2630" style:UIBarButtonItemStylePlain target:self action:@selector(mainMenuKey)] autorelease],
// Escape key
[[[UIBarButtonItem alloc] initWithTitle:@"Esc" style:UIBarButtonItemStylePlain target:self action:@selector(escapeKey)] autorelease],
// Tab key
[[[UIBarButtonItem alloc] initWithTitle:@"Tab" style:UIBarButtonItemStylePlain target:self action:@selector(tabKey)] autorelease],
// Return key
[[[UIBarButtonItem alloc] initWithTitle:@"\u23ce" style:UIBarButtonItemStylePlain target:self action:@selector(returnKey)] autorelease],
// Function keys
[[[UIBarButtonItem alloc] initWithTitle:@"F1" style:UIBarButtonItemStylePlain target:self action:@selector(fn1Key)] autorelease],
[[[UIBarButtonItem alloc] initWithTitle:@"F2" style:UIBarButtonItemStylePlain target:self action:@selector(fn2Key)] autorelease],
[[[UIBarButtonItem alloc] initWithTitle:@"F3" style:UIBarButtonItemStylePlain target:self action:@selector(fn3Key)] autorelease],
[[[UIBarButtonItem alloc] initWithTitle:@"F4" style:UIBarButtonItemStylePlain target:self action:@selector(fn4Key)] autorelease],
[[[UIBarButtonItem alloc] initWithTitle:@"F5" style:UIBarButtonItemStylePlain target:self action:@selector(fn5Key)] autorelease],
[[[UIBarButtonItem alloc] initWithTitle:@"F6" style:UIBarButtonItemStylePlain target:self action:@selector(fn6Key)] autorelease],
[[[UIBarButtonItem alloc] initWithTitle:@"F7" style:UIBarButtonItemStylePlain target:self action:@selector(fn7Key)] autorelease],
[[[UIBarButtonItem alloc] initWithTitle:@"F8" style:UIBarButtonItemStylePlain target:self action:@selector(fn8Key)] autorelease],
[[[UIBarButtonItem alloc] initWithTitle:@"F9" style:UIBarButtonItemStylePlain target:self action:@selector(fn9Key)] autorelease],
[[[UIBarButtonItem alloc] initWithTitle:@"F10" style:UIBarButtonItemStylePlain target:self action:@selector(fn10Key)] autorelease],
[[[UIBarButtonItem alloc] initWithTitle:@"F11" style:UIBarButtonItemStylePlain target:self action:@selector(fn11Key)] autorelease],
[[[UIBarButtonItem alloc] initWithTitle:@"F12" style:UIBarButtonItemStylePlain target:self action:@selector(fn12Key)] autorelease],
// Arrow keys
[[[UIBarButtonItem alloc] initWithTitle:@"\u2190" style:UIBarButtonItemStylePlain target:self action:@selector(leftArrowKey)] autorelease],
[[[UIBarButtonItem alloc] initWithTitle:@"\u2191" style:UIBarButtonItemStylePlain target:self action:@selector(upArrowKey)] autorelease],
[[[UIBarButtonItem alloc] initWithTitle:@"\u2192" style:UIBarButtonItemStylePlain target:self action:@selector(rightArrowKey)] autorelease],
[[[UIBarButtonItem alloc] initWithTitle:@"\u2193" style:UIBarButtonItemStylePlain target:self action:@selector(downArrowKey)] autorelease],
// Spacer at the end
[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease],
];

self.inputAccessoryView = toolbar;
[toolbar sizeToFit];

scrollView = [[UIScrollView alloc] init];
scrollView.frame = toolbar.frame;
scrollView.bounds = toolbar.bounds;
scrollView.autoresizingMask = toolbar.autoresizingMask;
scrollView.showsVerticalScrollIndicator = false;
scrollView.showsHorizontalScrollIndicator = false;
toolbar.autoresizingMask = UIViewAutoresizingNone;
[scrollView addSubview:toolbar];
self.inputAccessoryView = scrollView;

return self;
}

- (void)updateToolbarSize {
// We need at least a width of 768 pt for the toolbar. If we add more buttons this may need to be increased.
toolbar.frame = CGRectMake(0, 0, MAX(768, [[UIScreen mainScreen] bounds].size.width), toolbar.frame.size.height);
toolbar.bounds = toolbar.frame;
scrollView.contentSize = toolbar.frame.size;
}

- (NSArray *)keyCommands {
UIKeyCommand *upArrow = [UIKeyCommand keyCommandWithInput: UIKeyInputUpArrow modifierFlags: 0 action: @selector(upArrow:)];
UIKeyCommand *downArrow = [UIKeyCommand keyCommandWithInput: UIKeyInputDownArrow modifierFlags: 0 action: @selector(downArrow:)];
Expand All @@ -62,19 +122,99 @@ - (NSArray *)keyCommands {
}

- (void) upArrow: (UIKeyCommand *) keyCommand {
[softKeyboard handleKeyPress:273];
[softKeyboard handleKeyPress:Common::KEYCODE_UP];
}

- (void) downArrow: (UIKeyCommand *) keyCommand {
[softKeyboard handleKeyPress:274];
[softKeyboard handleKeyPress:Common::KEYCODE_DOWN];
}

- (void) leftArrow: (UIKeyCommand *) keyCommand {
[softKeyboard handleKeyPress:276];
[softKeyboard handleKeyPress:Common::KEYCODE_LEFT];
}

- (void) rightArrow: (UIKeyCommand *) keyCommand {
[softKeyboard handleKeyPress:275];
[softKeyboard handleKeyPress:Common::KEYCODE_RIGHT];
}

- (void) mainMenuKey {
[softKeyboard handleMainMenuKey];
}

- (void) escapeKey {
[softKeyboard handleKeyPress:Common::KEYCODE_ESCAPE];
}

- (void) tabKey {
[softKeyboard handleKeyPress:Common::KEYCODE_TAB];
}

- (void) fn1Key {
[softKeyboard handleKeyPress:Common::KEYCODE_F1];
}

- (void) fn2Key {
[softKeyboard handleKeyPress:Common::KEYCODE_F2];
}

- (void) fn3Key {
[softKeyboard handleKeyPress:Common::KEYCODE_F3];
}

- (void) fn4Key {
[softKeyboard handleKeyPress:Common::KEYCODE_F4];
}

- (void) fn5Key {
[softKeyboard handleKeyPress:Common::KEYCODE_F5];
}

- (void) fn6Key {
[softKeyboard handleKeyPress:Common::KEYCODE_F6];
}

- (void) fn7Key {
[softKeyboard handleKeyPress:Common::KEYCODE_F7];
}

- (void) fn8Key {
[softKeyboard handleKeyPress:Common::KEYCODE_F8];
}

- (void) fn9Key {
[softKeyboard handleKeyPress:Common::KEYCODE_F9];
}

- (void) fn10Key {
[softKeyboard handleKeyPress:Common::KEYCODE_F10];
}

- (void) fn11Key {
[softKeyboard handleKeyPress:Common::KEYCODE_F11];
}

- (void) fn12Key {
[softKeyboard handleKeyPress:Common::KEYCODE_F12];
}

- (void) leftArrowKey {
[softKeyboard handleKeyPress:Common::KEYCODE_LEFT];
}

- (void) upArrowKey {
[softKeyboard handleKeyPress:Common::KEYCODE_UP];
}

- (void) rightArrowKey {
[softKeyboard handleKeyPress:Common::KEYCODE_RIGHT];
}

- (void) downArrowKey {
[softKeyboard handleKeyPress:Common::KEYCODE_DOWN];
}

- (void) returnKey {
[softKeyboard handleKeyPress:Common::KEYCODE_RETURN];
}

@end
Expand Down Expand Up @@ -114,7 +254,12 @@ - (void)handleKeyPress:(unichar)c {
[inputDelegate handleKeyPress:c];
}

- (void)handleMainMenuKey {
[inputDelegate handleMainMenuKey];
}

- (void)showKeyboard {
[inputView updateToolbarSize];
[inputView becomeFirstResponder];
}

Expand Down
126 changes: 22 additions & 104 deletions backends/platform/ios7/ios7_osys_events.cpp
Expand Up @@ -106,6 +106,12 @@ bool OSystem_iOS7::pollEvent(Common::Event &event) {
return false;
break;

case kInputMainMenu:
event.type = Common::EVENT_MAINMENU;
_queuedInputEvent.type = Common::EVENT_INVALID;
_queuedEventTime = getMillis() + kQueuedInputEventDelay;
break;

default:
break;
}
Expand Down Expand Up @@ -383,54 +389,6 @@ void OSystem_iOS7::handleEvent_keyPressed(Common::Event &event, int keyPressed)
int ascii = keyPressed;
//printf("key: %i\n", keyPressed);

// We remap some of the iPhone keyboard keys.
// The first ten here are the row of symbols below the numeric keys.
switch (keyPressed) {
case 45:
keyPressed = Common::KEYCODE_F1;
ascii = Common::ASCII_F1;
break;
case 47:
keyPressed = Common::KEYCODE_F2;
ascii = Common::ASCII_F2;
break;
case 58:
keyPressed = Common::KEYCODE_F3;
ascii = Common::ASCII_F3;
break;
case 59:
keyPressed = Common::KEYCODE_F4;
ascii = Common::ASCII_F4;
break;
case 40:
keyPressed = Common::KEYCODE_F5;
ascii = Common::ASCII_F5;
break;
case 41:
keyPressed = Common::KEYCODE_F6;
ascii = Common::ASCII_F6;
break;
case 36:
keyPressed = Common::KEYCODE_F7;
ascii = Common::ASCII_F7;
break;
case 38:
keyPressed = Common::KEYCODE_F8;
ascii = Common::ASCII_F8;
break;
case 64:
keyPressed = Common::KEYCODE_F9;
ascii = Common::ASCII_F9;
break;
case 34:
keyPressed = Common::KEYCODE_F10;
ascii = Common::ASCII_F10;
break;
case 10:
keyPressed = Common::KEYCODE_RETURN;
ascii = Common::ASCII_RETURN;
break;
}
event.type = Common::EVENT_KEYDOWN;
_queuedInputEvent.type = Common::EVENT_KEYUP;

Expand All @@ -441,63 +399,23 @@ void OSystem_iOS7::handleEvent_keyPressed(Common::Event &event, int keyPressed)
}

bool OSystem_iOS7::handleEvent_swipe(Common::Event &event, int direction, int touches) {
if (touches == 1) {
if (touches == 3) {
Common::KeyCode keycode = Common::KEYCODE_INVALID;
switch (_screenOrientation) {
case kScreenOrientationPortrait:
switch ((UIViewSwipeDirection)direction) {
case kUIViewSwipeUp:
keycode = Common::KEYCODE_UP;
break;
case kUIViewSwipeDown:
keycode = Common::KEYCODE_DOWN;
break;
case kUIViewSwipeLeft:
keycode = Common::KEYCODE_LEFT;
break;
case kUIViewSwipeRight:
keycode = Common::KEYCODE_RIGHT;
break;
default:
return false;
}
break;
case kScreenOrientationLandscape:
switch ((UIViewSwipeDirection)direction) {
case kUIViewSwipeUp:
keycode = Common::KEYCODE_LEFT;
break;
case kUIViewSwipeDown:
keycode = Common::KEYCODE_RIGHT;
break;
case kUIViewSwipeLeft:
keycode = Common::KEYCODE_DOWN;
break;
case kUIViewSwipeRight:
keycode = Common::KEYCODE_UP;
break;
default:
return false;
}
break;
case kScreenOrientationFlippedLandscape:
switch ((UIViewSwipeDirection)direction) {
case kUIViewSwipeUp:
keycode = Common::KEYCODE_RIGHT;
break;
case kUIViewSwipeDown:
keycode = Common::KEYCODE_LEFT;
break;
case kUIViewSwipeLeft:
keycode = Common::KEYCODE_UP;
break;
case kUIViewSwipeRight:
keycode = Common::KEYCODE_DOWN;
break;
default:
return false;
}
break;
switch ((UIViewSwipeDirection)direction) {
case kUIViewSwipeUp:
keycode = Common::KEYCODE_UP;
break;
case kUIViewSwipeDown:
keycode = Common::KEYCODE_DOWN;
break;
case kUIViewSwipeLeft:
keycode = Common::KEYCODE_LEFT;
break;
case kUIViewSwipeRight:
keycode = Common::KEYCODE_RIGHT;
break;
default:
return false;
}

event.kbd.keycode = _queuedInputEvent.kbd.keycode = keycode;
Expand Down