iPhoneのサンプルプログラム

SATCHを用いたナビアプリ(1)

dispALL2.lua(SATCHのスクリプト)
local MLTPlugin = getMLTPluginManager()
local scene = getCurrentScene()
local camera = Camera(scene:getCurrentCamera())
local vidCap = VideoCapture(scene:getObjectByName("vidcap"))
local msg = Text2D(scene:getObjectByName("msg"))
local stage_no = Text2D(scene:getObjectByName("stage"))
	
local componentInterface = getComponentInterface()
	
local err_ret = eOk
local trackingIndex = -1
local targetStatus = {-1, -1, -1}
local action = {
"1101    found!", "1102    found!", "1103    found!",
"1104    found!", "1105    found!", "1106    found!",
"1107    found!", "1108    found!", "1109    found!",
"1110    found!", "1111    found!", "1112    found!",
"1113    found!", "1114    found!", "1115    found!",
"1116    found!", "1117    found!" }
local marker_no = {
1101, 1102, 1103,
1104, 1105, 1106,
1107, 1108, 1109,
1110, 1111, 1112,
1113, 1114, 1115,
1116, 1117
}
	
local found = -1
local i=0
local stage = 1
local found_marker = -1
local cand = 3
	
local dest = -1;
	
msg:setText("Marker   please!")
stage_no:setText("stage=1")
	
err_ret, trackingIndex = MLTPlugin:startTracking("B1/tracker.xml", vidCap:getVidCapID(), camera)
	
function gotoStage(n)
	MLTPlugin:stopTracking(trackingIndex)
    
	stage = n
	
	if n == 1 then
		stage_no:setText("stage=1")
		err_ret, trackingIndex = 
			MLTPlugin:startTracking("B1/tracker.xml", vidCap:getVidCapID(), camera)
	elseif n == 2 then
		stage_no:setText("stage=2")
		err_ret, trackingIndex =
			MLTPlugin:startTracking("B2/tracker.xml", vidCap:getVidCapID(), camera)
	elseif n == 3 then
		stage_no:setText("stage=3")
		err_ret, trackingIndex =
			MLTPlugin:startTracking("B3/tracker.xml", vidCap:getVidCapID(), camera)
	elseif n == 4 then
		stage_no:setText("stage=4")
		err_ret, trackingIndex =
			MLTPlugin:startTracking("B4/tracker.xml", vidCap:getVidCapID(), camera)
	elseif n == 5 then
		stage_no:setText("stage=5")
		err_ret, trackingIndex =
			MLTPlugin:startTracking("B5/tracker.xml", vidCap:getVidCapID(), camera)
	elseif n == 6 then
		stage_no:setText("stage=6")
		err_ret, trackingIndex =
			MLTPlugin:startTracking("B6/tracker.xml", vidCap:getVidCapID(), camera)
	end
end
	
repeat
	
  if (found_marker < 0) then
	
		if stage <= 5 then
			cand = 3
		else
			cand = 2
		end
	
		for i = 1,cand do
    		err_ret, targetStatus[i] = MLTPlugin:getTargetStatus(trackingIndex, i-1)		
		end
	
		found = -1
		for i = 1,cand do
			if (targetStatus[i] == 1) then
				found = i
				break
			end
		end
	
		if (found ~= -1) then
			msg:setText(action[found + 3*(stage-1)])
			found_marker = marker_no[found + 3*(stage-1)]
		else
			if (stage == 1) then
				gotoStage(2)
			elseif (stage == 2) then
				gotoStage(3)
			elseif (stage == 3) then
				gotoStage(4)
			elseif (stage == 4) then
				gotoStage(5)
			elseif (stage == 5) then
				gotoStage(6)
			elseif (stage == 6) then
				gotoStage(1)
			end
 			coroutine.yield()
		end
    else
    	if (dest < 0) then
    		if componentInterface then
    			componentInterface:executeAppFunc("setStartPosition", found_marker);
    		end
    		dest = 1
    	end
	end
until coroutine.yield()

AppDelegate.h
#import <Foundation/Foundation.h>
#import "satchARViewer.h"

@interface AppDelegate : NSObject <UIApplicationDelegate>
{
    UIWindow *window;
    UIView *mRender;
    SatchARViewer *mPlayer;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UIView *mRender;

- (void)start;
- (void)stop;

@end

AppDelegeta.mm
#import "AppDelegate.h"
	
@implementation AppDelegate
	
@synthesize window;
@synthesize mRender;
	
- (void)dealloc
{
    [self stop];
	
    if (mPlayer) {
        [mPlayer terminate];
        [mPlayer release];
        mPlayer = nil;
    }
	
    [mRender release];
    [window release];
    [super dealloc];
}
	
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [SatchARViewer setDialogMessageLoading:@"ロ−ディング..."];
    [SatchARViewer
     setToastMessageNoNetworkConnection:@"ネットワークに接続されていません。"];
    [SatchARViewer setToastMessageNetworkError:@"通信中にエラーが発生しました。"];
    [SatchARViewer setToastMessageLicenceError:@"認証に失敗しました。"];

    mRender = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    [window addSubview:mRender];
	
    mPlayer = [[[SatchARViewer alloc] init] initialize:mRender];
	
    [self.window makeKeyAndVisible];
    [self start];
	
    return YES;
}
	
- (void)applicationWillResignActive:(UIApplication *)application
{
    if (mPlayer != nil) {
        [mPlayer applicationWillResignActive];
    }
}
	
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    if (mPlayer != nil) {
        [mPlayer applicationDidEnterBackground];
    }
}
	
- (void)applicationWillEnterForeground:(UIApplication *)application
{
    if (mPlayer != nil) {
        [mPlayer applicationWillEnterForeground];
    }
}
	
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    if (mPlayer != nil) {
        [mPlayer applicationDidBecomeActive];
    }
}
	
- (void)applicationWillTerminate:(UIApplication *)application
{
    if (mPlayer != nil) {
        [mPlayer applicationWillTerminate];
    }
}
	
- (void)callback:(NSNumber*)num
{
    switch ([num intValue]) {
        case LOAD_SCENARIO_STATUS_COMPLETE:
            [mPlayer registerCommunicationCallback:@"setStartPosition"
                    callbackObject:self
                    callbackMethod:@"TI_SetStartPosNotification"];
            [[NSNotificationCenter defaultCenter] addObserver:self
                    selector:@selector(setStartPosition:)
                    name:@"TI_SetStartPosNotification"
                    object:nil];
            [mPlayer playScenario];
            break;
    }
}
	
- (void)start
{
    if (mPlayer) {
        [mPlayer loadScenario:@"Nov4_iphone/Nov4.dpd"
					callback:@selector(callback:)
					obj:self];
    }
}
	
- (void)stop
{
    if (mPlayer && ![mPlayer isScenarioPaused]) {
        [mPlayer playScenario];
    }
}
	
- (void)setStartPosition:(NSNotification *)notification
{
    NSDictionary* l_userInfo = [notification userInfo];
    NSString* start_pos = (NSString *)[l_userInfo objectForKey:@"arg0"];
	
    [[[[UIAlertView alloc] initWithTitle:@"目的地の選択"
				message:start_pos
				delegate:self
				cancelButtonTitle:nil
				otherButtonTitles:@"1110", @"1111", @"1112", @"1113", nil] autorelease] show];
}
	
- (void)alertView:(UIAlertView *)alertView
	clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
}
	
@end