iPhoneのサンプルプログラム その2

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>

@class ViewController;

@interface AppDelegate : UIResponder 

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;

@end

AppDelegeta.mm
#import "AppDelegate.h"
#import "ViewController.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize viewController = _viewController;

- (void)dealloc
{
    [_window release];
    [_viewController release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    
    [self.window makeKeyAndVisible];
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

ViewController.h
#import <UIKit/UIKit.h>
#import "satchARViewer.h"

@interface ViewController : UIViewController {
    IBOutlet UIButton *btnCorner;
    
    SatchARViewer *mPlayer;
    NSString *start_pos;
}

@property (retain, nonatomic) IBOutlet UIView *mRender;
@property (retain, nonatomic) IBOutlet UILabel *labMsg;

- (IBAction)pushCornerButton:(id)sender;

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

@end

ViewController.m
#import "AppDelegate.h"
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize mRender;
@synthesize labMsg;

- (void)viewDidLoad
{
    [super viewDidLoad];
        //メッセージのローカライズ
    [SatchARViewer        setDialogMessageLoading:@"ローディング..."];
    [SatchARViewer        setToastMessageNoNetworkConnection:@"ネットワークに接続されていません。"];
    [SatchARViewer        setToastMessageNetworkError:@"通信中にエラーが発生しました。"];
    [SatchARViewer        setToastMessageLicenceError:@"認証に失敗しました"];

    mPlayer = [[[SatchARViewer alloc] init] initialize:mRender];
    [self start];
    
    btnCorner.enabled = NO;
    labMsg.text = @"出発地のマーカをかざしてください!";
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [self setMRender:nil];
    [self setLabMsg:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)dealloc {
    if (mPlayer) {
        [mPlayer terminate];
        [mPlayer release];
        mPlayer = nil;
    }
    
    [mRender release];
    [labMsg release];
    [super dealloc];
}

- (void)start
{
    if (mPlayer) {
        [mPlayer loadScenario:@"Nov4_iphone/Nov4.dpd"
                     callback:@selector(callback:)
                          obj:self];
    }
}


- (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)stop
{
    if (mPlayer && ![mPlayer isScenarioPaused]) {
        [mPlayer pauseScenario];
    }    
}

- (IBAction)pushCornerButton:(id)sender {
}

- (void)setStartPosition:(NSNotification *)notification
{
    NSDictionary* l_userInfo = [notification userInfo];
    start_pos = (NSString *)[l_userInfo objectForKey:@"arg0"];
    
    labMsg.text = [NSString stringWithFormat:@"出発地:%@", start_pos];
}

@end