//------------------------------------------------------------------------------
// 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 "Core/Utility/stdafx.h"
#include "Load/Request/AnimationLoadRequest.h"

//------------------------------------------------------------------------------
// 生成、破棄
//------------------------------------------------------------------------------
// コンストラクタ
AnimationLoadRequest::AnimationLoadRequest(const String& fileName,
	AnimationManager* animationManager, Scene* bindScene) :
	LoadRequest(), fileName_(fileName), animationManager_(animationManager),
	bindScene_(bindScene){
}
//------------------------------------------------------------------------------
// デストラクタ
AnimationLoadRequest::~AnimationLoadRequest(){
	// ロードの中断には対応しない
	Assert(animationManager_ == NULL);
}
//------------------------------------------------------------------------------
// ロード
//------------------------------------------------------------------------------
// ロード
bool AnimationLoadRequest::load(){
	// ファイルパスのチェック
	FilePath filePath(fileName_);
	if(!filePath.existFile()){
		ErrorOut("AnimationLoadRequest::load() "
			"アニメーションファイルが見つかりません %s",
			filePath.getPath().getBytes());
		return true;
	}

	// アニメーションのロード
	if(filePath.getExtension() == "tam"){
		TextAnimationLoader* loader = new TextAnimationLoader();
		loader->load(filePath.getPath(), animationManager_);
		delete loader;
	}else if(filePath.getExtension() == "bam"){
		BinaryAnimationLoader* loader = new BinaryAnimationLoader();
		loader->load(filePath.getPath(), animationManager_);
		delete loader;
	}else{
		ErrorOut("AnimationLoadRequest::load() "
			"拡張子がアニメーションファイルではありません %s",
			filePath.getPath().getBytes());
		return true;
	}

	// アニメーションのバインド
	Animation* animation = animationManager_->search(filePath.getName());
	if((animation != NULL) && (bindScene_ != NULL)){
		if(!animation->bind(bindScene_)){
			ErrorOut("AnimationLoadRequest::load() "
				"アニメーションのバインドに失敗しました %s",
				filePath.getPath().getBytes());
			animationManager_->clear();
			return true;
		}
	}

	// 終了時にアニメーションポインタをクリアする
	animationManager_ = NULL;
	return true;
}
//------------------------------------------------------------------------------
