Work in progress: Native Events

1 vote · 0 comments

I'm working on adding the ability to easily trigger native events from within the lua runtime. It makes hooking up native tools/controls much easier than extending the moai framework (although less efficient). Right now I only have it working for ios, but if people are interested I'll clean it up and do a pull request.

raw ·
copy
· download
#ifndef MOAINativeEventIOS_H #define MOAINativeEventIOS_H #import <Foundation/Foundation.h> #import <moaicore/moaicore.h> //================================================================// // MOAINativeEvent //================================================================// /** @name MOAINativeEventIOS @text A class for easily hooking into native events without needing to write moai extention classes */ class MOAINativeEventIOS : public MOAIGlobalClass < MOAINativeEventIOS, MOAILuaObject > { private: //----------------------------------------------------------------// static int _triggerEvent ( lua_State* L ); public: DECL_LUA_SINGLETON ( MOAINativeEventIOS ) //----------------------------------------------------------------// MOAINativeEventIOS (); ~MOAINativeEventIOS (); void RegisterLuaClass ( MOAILuaState& state ); }; typedef NSDictionary* (^GenericEventHandler)(NSDictionary*); @interface MOAINativeEventRegistrar : NSObject { @public }; +(void) RegisterEventListenerWithName:(NSString*)name method:(GenericEventHandler) handler; @end #endif ///////////////////////////////////////////////////////////////////// // // MOAINativeEventIOS.m // libmoai // // #include "pch.h" #import "MOAINativeEventIOS.h" @implementation MOAINativeEventRegistrar static NSMutableDictionary* handlerCollection; +(void) RegisterEventListenerWithName:(NSString*)name method:(GenericEventHandler) handler { if(!handlerCollection) { handlerCollection = [[NSMutableDictionary alloc] init]; } [handlerCollection setValue:(id)handler forKey:name]; } +(NSString*) HandleEventNamed:(NSString*)name parameters:(NSString*)jsonParams { GenericEventHandler handler = (GenericEventHandler)[handlerCollection objectForKey:name]; if(!handler) { return NULL; } NSError *e = nil; NSDictionary * paramDic = [NSJSONSerialization JSONObjectWithData:[jsonParams dataUsingEncoding:NSUTF8StringEncoding] options: NULL error: &e]; NSDictionary * rtnval = handler(paramDic); if(rtnval) { NSData* rtnData = [NSJSONSerialization dataWithJSONObject:rtnval options:NULL error:&e]; if(rtnData) { return [[NSString alloc] initWithData:rtnData encoding:NSUTF8StringEncoding]; } } return NULL; } @end //================================================================// // lua //================================================================// //----------------------------------------------------------------// /** @name triggerEvent @text Triggers a native-code event. There is overhead involved in using this generic method, so it should not be used for frequent events @in string eventName The name of the triggered event @in string jsonParams A string of json data used for input parameters @out string jsonRtnval Json data used as return value */ int MOAINativeEventIOS::_triggerEvent(lua_State* L ) { MOAILuaState state ( L ); cc8* eventName = state.GetValue < cc8* >( 1, "" ); cc8* jsonParams = state.GetValue < cc8* >( 2, "" ); NSString* jsonRtnval = [MOAINativeEventRegistrar HandleEventNamed:[NSString stringWithUTF8String:eventName] parameters:[NSString stringWithUTF8String:jsonParams]]; lua_pushstring ( L, [jsonRtnval UTF8String ] ); return 1; } //================================================================// // MOAINativeEventIOS //================================================================// //----------------------------------------------------------------// MOAINativeEventIOS::MOAINativeEventIOS () { RTTI_SINGLE ( MOAILuaObject ) } //----------------------------------------------------------------// MOAINativeEventIOS::~MOAINativeEventIOS () { } //----------------------------------------------------------------// void MOAINativeEventIOS::RegisterLuaClass ( MOAILuaState& state ) { luaL_Reg regTable [] = { { "triggerEvent", _triggerEvent }, { NULL, NULL } }; luaL_register ( state, 0, regTable ); } /////////////////////////////////////////////// --MOAINativeEvent.lua MOAINativeEvent = {} local platformSpecifcObj = MOAINativeEventIOS or MOAINativeEventAndroid or nil; MOAINativeEvent.triggerEvent = function(eventName, paramTable) if not platformSpecifcObj then return nil end local paramString = MOAIJsonParser.encode(paramTable); local rtnString = MOAINativeEventIOS.triggerEvent(eventName,paramString); return MOAIJsonParser.decode(rtnString); end

Be the first to comment

Sign in with OpenID