diff --git a/src/moaiext-iphone/MOAIKeyboardIOS.h b/src/moaiext-iphone/MOAIKeyboardIOS.h index 46f4a63e1f..31eaea07ea 100644 --- a/src/moaiext-iphone/MOAIKeyboardIOS.h +++ b/src/moaiext-iphone/MOAIKeyboardIOS.h @@ -8,6 +8,29 @@ #import #import +@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 //================================================================// @@ -46,6 +69,7 @@ @const EVENT_INPUT @const EVENT_RETURN + @const EVENT_SHOW @const AUTOCAP_ALL @const AUTOCAP_NONE @@ -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: @@ -123,6 +153,7 @@ class MOAIKeyboardIOS : enum { EVENT_INPUT, EVENT_RETURN, + EVENT_SHOW }; //----------------------------------------------------------------// diff --git a/src/moaiext-iphone/MOAIKeyboardIOS.mm b/src/moaiext-iphone/MOAIKeyboardIOS.mm index f5c8ee2937..ca869279a0 100644 --- a/src/moaiext-iphone/MOAIKeyboardIOS.mm +++ b/src/moaiext-iphone/MOAIKeyboardIOS.mm @@ -4,20 +4,19 @@ #include "pch.h" #import -//================================================================// -// 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 @@ -67,6 +66,11 @@ -( BOOL ) textFieldShouldReturn :( UITextField* )textField { return result; } + + -( BOOL ) textFieldShouldEndEditing:(UITextField *)textField { + UNUSED (textField); + return NO; + } @end //================================================================// @@ -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 //================================================================// @@ -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 ); @@ -199,6 +225,8 @@ -( BOOL ) textFieldShouldReturn :( UITextField* )textField { { "getText", _getText }, { "setListener", &MOAIGlobalEventSource::_setListener < MOAIKeyboardIOS > }, { "showKeyboard", _showKeyboard }, + { "hideKeyboard", _hideKeyboard }, + { "resetText", _resetText }, { NULL, NULL } }; @@ -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 ]]; @@ -231,4 +265,17 @@ -( BOOL ) textFieldShouldReturn :( UITextField* )textField { [ this->mTextField setSecureTextEntry:secure ]; [ this->mTextField becomeFirstResponder ]; -} \ No newline at end of file + + + [[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); +}