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

[WIP] Native keyboard in games #93

Open
wants to merge 4 commits into
base: mindsnacks-dev
Choose a base branch
from
Open
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
33 changes: 32 additions & 1 deletion src/moaiext-iphone/MOAIKeyboardIOS.h
Expand Up @@ -8,6 +8,29 @@
#import <UIKit/UIKit.h>
#import <moaicore/moaicore.h>

@interface MOAIKeyboardIOSEventListener : NSObject {

}
- ( void ) keyboardDidShow :(NSNotification*)notification;
@end

//================================================================//
// MOAITextFieldDelegate
//================================================================//
@interface MOAITextFieldDelegate : NSObject < UITextFieldDelegate > {
@private

NSRange mRange;
}

//----------------------------------------------------------------//
-( void ) onChanged :( NSString* )string;
-( BOOL ) textField :( UITextField* )textField shouldChangeCharactersInRange:( NSRange )range replacementString:( NSString* )string;
-( BOOL ) textFieldShouldReturn :( UITextField* )textField;
-( BOOL ) textFieldShouldEndEditing :(UITextField *)textField;

@end

//================================================================//
// MOAIKeyboardIOS
//================================================================//
Expand Down Expand Up @@ -46,6 +69,7 @@

@const EVENT_INPUT
@const EVENT_RETURN
@const EVENT_SHOW

@const AUTOCAP_ALL
@const AUTOCAP_NONE
Expand Down Expand Up @@ -107,14 +131,20 @@ class MOAIKeyboardIOS :
RETURN_KEY_SEND = UIReturnKeySend,
};

UITextField* mTextField;
UITextField* mTextField;
MOAITextFieldDelegate* mDelegate;
MOAIKeyboardIOSEventListener* mListener;

//----------------------------------------------------------------//
static int _getText ( lua_State* L );
static int _showKeyboard ( lua_State* L );
static int _hideKeyboard ( lua_State* L );
static int _resetText ( lua_State* L );

//----------------------------------------------------------------//
void ShowKeyboard ( cc8* text, int type, int returnKey, bool secure, int autocap, int appearance );
void ResetText ();
void KeyboardDidShow (NSNotification* notification);

public:

Expand All @@ -123,6 +153,7 @@ class MOAIKeyboardIOS :
enum {
EVENT_INPUT,
EVENT_RETURN,
EVENT_SHOW
};

//----------------------------------------------------------------//
Expand Down
79 changes: 63 additions & 16 deletions src/moaiext-iphone/MOAIKeyboardIOS.mm
Expand Up @@ -4,20 +4,19 @@
#include "pch.h"
#import <moaiext-iphone/MOAIKeyboardIOS.h>

//================================================================//
// MOAIGameCenterIOSLeaderboardDelegate
//================================================================//
@interface MOAITextFieldDelegate : NSObject < UITextFieldDelegate > {
@private

NSRange mRange;
}

//----------------------------------------------------------------//
-( void ) onChanged :( NSString* )string;
-( BOOL ) textField :( UITextField* )textField shouldChangeCharactersInRange:( NSRange )range replacementString:( NSString* )string;
-( BOOL ) textFieldShouldReturn :( UITextField* )textField;

@implementation MOAIKeyboardIOSEventListener
- (void) keyboardDidShow:(NSNotification*)notification {
CGFloat height = [[notification.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.height;

MOAILuaStateHandle state = MOAILuaRuntime::Get ().State ();
MOAIKeyboardIOS& keyboard = MOAIKeyboardIOS::Get ();

if ( keyboard.PushListener ( MOAIKeyboardIOS::EVENT_SHOW, state )) {
state.Push ( height );
state.DebugCall ( 1, 0 );
}

}
@end

@implementation MOAITextFieldDelegate
Expand Down Expand Up @@ -67,6 +66,11 @@ -( BOOL ) textFieldShouldReturn :( UITextField* )textField {
return result;
}


-( BOOL ) textFieldShouldEndEditing:(UITextField *)textField {
UNUSED (textField);
return NO;
}
@end

//================================================================//
Expand Down Expand Up @@ -121,6 +125,27 @@ -( BOOL ) textFieldShouldReturn :( UITextField* )textField {
return 0;
}

//----------------------------------------------------------------//
/** @name hideKeyboard
@text Hide the native software keyboard.

@out nil
*/
int MOAIKeyboardIOS::_hideKeyboard ( lua_State* L ) {
UNUSED (L);

MOAIKeyboardIOS::Get ().Finish();
return 0;
}

int MOAIKeyboardIOS::_resetText ( lua_State* L ) {
UNUSED (L);

MOAIKeyboardIOS::Get ().ResetText ();

return 0;
}

//================================================================//
// MOAIKeyboardIOS
//================================================================//
Expand Down Expand Up @@ -167,6 +192,7 @@ -( BOOL ) textFieldShouldReturn :( UITextField* )textField {

state.SetField ( -1, "EVENT_INPUT", ( u32 )EVENT_INPUT );
state.SetField ( -1, "EVENT_RETURN", ( u32 )EVENT_RETURN );
state.SetField ( -1, "EVENT_SHOW", ( u32 )EVENT_SHOW );

state.SetField ( -1, "AUTOCAP_ALL", ( u32 )AUTOCAP_ALL );
state.SetField ( -1, "AUTOCAP_NONE", ( u32 )AUTOCAP_NONE );
Expand Down Expand Up @@ -199,6 +225,8 @@ -( BOOL ) textFieldShouldReturn :( UITextField* )textField {
{ "getText", _getText },
{ "setListener", &MOAIGlobalEventSource::_setListener < MOAIKeyboardIOS > },
{ "showKeyboard", _showKeyboard },
{ "hideKeyboard", _hideKeyboard },
{ "resetText", _resetText },
{ NULL, NULL }
};

Expand All @@ -211,13 +239,19 @@ -( BOOL ) textFieldShouldReturn :( UITextField* )textField {
if ( !this->mTextField ) {
UIWindow* window = [[ UIApplication sharedApplication ] keyWindow ];

this->mDelegate = [[ MOAITextFieldDelegate alloc ] init ];

CGRect frame = CGRectMake ( 0, 0, 320, 24 );
this->mTextField = [[ UITextField alloc ] initWithFrame:frame ];
[ this->mTextField setDelegate:[[ MOAITextFieldDelegate alloc ] init ]];
[ this->mTextField setDelegate:this->mDelegate ];

[ window addSubview:this->mTextField ];
}

if ( !this->mListener) {
this->mListener = [[MOAIKeyboardIOSEventListener alloc] init];
}

[ this->mTextField setHidden:YES ];
[ this->mTextField setText:[ NSString stringWithUTF8String:text ]];

Expand All @@ -231,4 +265,17 @@ -( BOOL ) textFieldShouldReturn :( UITextField* )textField {
[ this->mTextField setSecureTextEntry:secure ];

[ this->mTextField becomeFirstResponder ];
}


[[NSNotificationCenter defaultCenter] addObserver:this->mListener selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification object:nil];
}

void MOAIKeyboardIOS::ResetText () {
[this->mTextField setText:@""];
}

void MOAIKeyboardIOS::KeyboardDidShow (NSNotification* notification)
{
UNUSED (notification);
}