iPhoneのプログラム

SATCHを用いたナビアプリ(3)その3

シナリオファイル

dispALL3.lua
local MLTPlugin = getMLTPluginManager()
local scene = getCurrentScene()
local camera = Camera(scene:getCurrentCamera())
local vidCap = VideoCapture(scene:getObjectByName("vidcap"))
local msg = Text2D(scene:getObjectByName("msg"))

-- 方向指示用
local boussole = Entity(scene:getObjectByName("Boussole"))
local fleche = Entity(scene:getObjectByName("Fleche"))
boussole:setVisible(false)
fleche:setVisible(false)

-- 色識別用
local mainView = RenderTarget(scene:getMainView())
local targetDirectory = getUserAppDataDirectory()
local targetJpg = targetDirectory .. "c1.jpg"
local camera_mode = 1

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 isCommand = nil
local command = nil
local mode = 1

msg:setText(" ")

err_ret, trackingIndex = MLTPlugin:startTracking("B1/tracker.xml", vidCap:getVidCapID(), camera)

function gotoStage(n)
	MLTPlugin:stopTracking(trackingIndex)
    
	stage = n

	if n == 1 then
		msg:setText("stage=1")
		err_ret, trackingIndex = 
			MLTPlugin:startTracking("B1/tracker.xml", vidCap:getVidCapID(), camera)
	elseif n == 2 then
		msg:setText("stage=2")
		err_ret, trackingIndex =
			MLTPlugin:startTracking("B2/tracker.xml", vidCap:getVidCapID(), camera)
	elseif n == 3 then
		msg:setText("stage=3")
		err_ret, trackingIndex =
			MLTPlugin:startTracking("B3/tracker.xml", vidCap:getVidCapID(), camera)
	elseif n == 4 then
		msg:setText("stage=4")
		err_ret, trackingIndex =
			MLTPlugin:startTracking("B4/tracker.xml", vidCap:getVidCapID(), camera)
	elseif n == 5 then
		msg:setText("stage=5")
		err_ret, trackingIndex =
			MLTPlugin:startTracking("B5/tracker.xml", vidCap:getVidCapID(), camera)
	elseif n == 6 then
		msg:setText("stage=6")
		err_ret, trackingIndex =
			MLTPlugin:startTracking("B6/tracker.xml", vidCap:getVidCapID(), camera)
	end
end

function recognize()

		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)]

			if componentInterface then
    			componentInterface:executeAppFunc("setStartPosition", found_marker);
    	end
			mode = 2
		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

end

repeat
	if (mode == 1) then

		recognize()

	elseif (mode == 2) then
		msg:setText(" ")

		if componentInterface then
			isCommand, command = componentInterface:pullCommand()
			if isCommand then
				if command["CommandName"]=="sendGyro" then
					boussole:setVisible(true)
					fleche:setVisible(true)
					fleche:setOrientationEuler(0,command["arg1"],0,boussole)
					mode = 3;
				end
			end
		end

	elseif (mode == 3) then

		if componentInterface then
			isCommand, command = componentInterface:pullCommand()
			if isCommand then
				if command["CommandName"]=="sendGyro" then
					fleche:setOrientationEuler(0,command["arg1"],0,boussole)
				elseif command["CommandName"]=="prepareCamera" then
					boussole:setVisible(false)
					fleche:setVisible(false)
					camera_mode = 1
					mode = 4
				end
			end
		end

	elseif (mode == 4) then

		if camera_mode == 1 then
			if componentInterface then
				isCommand, command = componentInterface:pullCommand()
				if isCommand then
					if command["CommandName"]=="capture" then
						mainView:dump(targetJpg)
						camera_mode = 2
					end
				end
			end
		elseif camera_mode == 2 then
			if componentInterface then
				componentInterface:executeAppFunc("setCaptureFilename", targetJpg);
			end
			mode = 2
		end

	end

until coroutine.yield()

上記以外のプログラムはすべて、こちらと同じです.