//------------------------------------------------------------------------------
// Lamp : Open source game middleware
// Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//------------------------------------------------------------------------------

/** @file
 * 変換カメラマネージャ実装
 * @author Junpee
 */

#include "System/stdafx.h"
#include "Translator/Camera/TranslationCameraManager.h"

namespace LampForMaya{

//------------------------------------------------------------------------------
// コンストラクタ
TranslationCameraManager::TranslationCameraManager() :
	database_(256, 0.75f), array_(256){
}
//------------------------------------------------------------------------------
// デストラクタ
TranslationCameraManager::~TranslationCameraManager(){
	Assert(database_.getCount() == 0);
	Assert(array_.getCount() == 0);
	if(getCount() != 0){ clear(); }
}
//------------------------------------------------------------------------------
// カメラの収集
bool TranslationCameraManager::collectCameras(){
	MStatus result;
	MItDag dagIterator(MItDag::kBreadthFirst, MFn::kCamera, &result);
	MayaStatusCheck(result);
	MDagPath dagPath;
	for( ; !dagIterator.isDone(); dagIterator.next()){
		result = dagIterator.getPath(dagPath);
		MayaStatusCheck(result);
		MFnDagNode dagNode(dagPath, &result);
		MayaStatusCheck(result);
		// インスタンス化されていればキャンセル
		u_int instanceNumber = dagPath.instanceNumber(&result);
		MayaStatusCheck(result);
		if(instanceNumber > 0){ continue; }
		// 名前の取得
		MString dagName = dagNode.name(&result);
		MayaStatusCheck(result);
		// デフォルトカメラをはじく
		if((dagName == "perspShape") ||
			(dagName == "topShape") ||
			(dagName == "frontShape") ||
			(dagName == "sideShape")){ continue; }
		// カメラの解析
		if(!analysisCamera(dagPath)){ return false; }
	}
	return true;
}
//------------------------------------------------------------------------------
// カメラの解析
bool TranslationCameraManager::analysisCamera(MDagPath dagPath){
	MStatus result;
	MFnDagNode dagNode(dagPath, &result);
	MayaStatusCheck(result);
	// 名前の重複が無いかチェック
	String cameraName = dagNode.name(&result).asChar();
	MayaStatusCheck(result);
	TranslationCamera* exist = database_.get(cameraName);
	if(exist != NULL){
		MayaErrorOut(String("TranslationCameraManager::analysisCamera() "
			"名前が重複しています ") + cameraName);
		return false;
	}
	TranslationCamera* camera =
		new TranslationCamera(dagPath, cameraName);
	if(!camera->analyze()){
		delete camera;
		return false;
	}
	database_.put(cameraName, camera);
	array_.add(camera);
	return true;
}
//------------------------------------------------------------------------------
// アニメーションの収集
bool TranslationCameraManager::collectAnimations(){
	for(int i = 0; i < getCount(); i++){
		if(!get(i)->analyzeAnimation()){ return false; }
	}
	return true;
}
//------------------------------------------------------------------------------
// Lampへの変換
bool TranslationCameraManager::convertToLamp(Scene* scene) const{
	// カメラのコンバート
	for(int i = 0; i < getCount(); i++){
		if(!get(i)->convertToLamp(scene)){ return false; }
	}
	return true;
}
//------------------------------------------------------------------------------
// アニメーションの変換
bool TranslationCameraManager::convertAnimation(
	AnimationManager* animationManager, AnimationSet* animationSet){
	for(int i = 0; i < getCount(); i++){
		if(!get(i)->convertAnimation(animationManager, animationSet)){
			return false;
		}
	}
	return true;
}
//------------------------------------------------------------------------------
// クリア
int TranslationCameraManager::clear(){
	int result = getCount();
	// 要素の削除
	for(int i = 0; i < result; i++){ delete array_.get(i); }
	array_.clear();
	database_.clear();
	return result;
}
//------------------------------------------------------------------------------
} // End of namespace LampForMaya
//------------------------------------------------------------------------------
