This is a simple example of integrating iOS functionality with Monkey.
For this tutorial, I am going to call my class "apple",
Step 1. Folders
Under your project folder create a folder for your class (In this case "apple"), and under that create a "native" folder.
So you will have
mygame.monkey
/apple
/apple/native
Step 2. - CPP
Under your "native" folder, create your "cpp" file (class.ios.cpp), in this case "apple.ios.cpp"
/*
filename: apple.ios.cpp
author: Rob Pearmain
description: Class for monkey, to allow access to ios functions
*/
//.h
class apple;
#import <UIKit/UIKit.h>
//.cpp
class apple : public gxtkObject
{
public:
virtual float GetBatteryLevel();
virtual String GetDeviceName();
virtual String GetSystemVersion();
virtual String GetSystemName();
virtual String GetDeviceModel();
};
float apple::GetBatteryLevel()
{
// EDIT: Added setBatteryMonitoringEnabled
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
float BatteryLevel = [[UIDevice currentDevice] batteryLevel];
return BatteryLevel;
}
String apple::GetDeviceName()
{
NSString *name = [[UIDevice currentDevice] name];
return String(name);
}
String apple::GetSystemVersion()
{
NSString *ver = [[UIDevice currentDevice] systemVersion];
return String(ver);
}
String apple::GetDeviceModel()
{
NSString *model = [[UIDevice currentDevice] model];
return String(model);
}
String apple::GetSystemName()
{
NSString *systemname = [[UIDevice currentDevice] systemName];
return String(systemname);
}
Step 3. - Extern
In your "apple" folder create "apple.monkey" and add the following code:
' Point to the C++
Import "native/apple.ios.cpp"
' Let monkey know we are referencing an external class
Extern
' Create a monkey class to map to the external class
Class Apple="apple"
' Battery Level (Returns -1 in Simulator)
Method GetBatteryLevel:Float()
' Device Name (e.g. iPad Simulator)
Method GetDeviceName:String()
' iOS Version (e.g. 4.3.2)
Method GetSystemVersion:String()
' Device Model (e.g. iPhone)
Method GetDeviceModel:String()
' System Name (e.g. iPhone)
Method GetSystemName:String()
Method SetOrientationLeft:Void()
End
Step 4. - Using the functions
In your "mygame.monkey" add an Import:
Import apple
And add code to access the class
app:Apple = New Apple()
Local devicename:String = app.GetDeviceName()
Local systemversion:String = app.GetSystemVersion()
Local devicemodel:String = app.GetDeviceModel()
Local systemname:String = app.GetSystemName()
Local batterylevel:Float = app.GetBatteryLevel()
(Note: batterylevel will always return -1 on the simulator)
|