//------------------------------------------------------------------------------
// 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 "LampBasic.h"
#include "Input/Mouse/MouseDevice.h"

namespace Lamp{

//------------------------------------------------------------------------------
// コンストラクタ
MouseDevice::MouseDevice() : InputDevice(false){
	zResolution_ = 120;// とりあえず一般的な120にしておく
}
//------------------------------------------------------------------------------
// デストラクタ
MouseDevice::~MouseDevice(){
}
//------------------------------------------------------------------------------
// 初期化
bool MouseDevice::initialize(DirectInputDevice* inputDevice, HWND windowHandle){
	if(!InputDevice::initialize(inputDevice, windowHandle)){ return false; }
	// データフォーマットの指定
	if(DirectXFailed(inputDevice->SetDataFormat(&c_dfDIMouse2))){
		ErrorOut("MouseDevice::initialize() "
			"マウスデバイスのデータフォーマット指定に失敗しました。");
		return false;
	}
	// Z軸があれば解像度を調べる
	if((getAxisCount() >= 3) && isAttached()){
		DIPROPDWORD property;
		property.diph.dwSize = sizeof(DIPROPDWORD);
		property.diph.dwHeaderSize = sizeof(DIPROPHEADER);
		property.diph.dwHow = DIPH_BYOFFSET;
		property.diph.dwObj = DIMOFS_Z;
		HRESULT result =
			inputDevice->GetProperty(DIPROP_GRANULARITY, &property.diph);
		if(DirectXFailed(result)){
			ErrorOut("MouseDevice::initialize() Z解像度の取得に失敗しました。");
		}else{
			zResolution_ = property.dwData;
		}
	}
	// 優先度の指定
	if(!setCooperativeLevel(isExclusive(), isForeground())){ return false; }
	return true;
}
//------------------------------------------------------------------------------
// ポーリング
bool MouseDevice::polling(){
	InputDevice::polling();
	// マウスデバイスステート取得
	DIMOUSESTATE2 state;
	HRESULT result = inputDevice_->GetDeviceState(
		sizeof(DIMOUSESTATE2), &state);
	if(DirectXSucceeded(result)){
		mouseState_.setXAxis(state.lX);
		mouseState_.setYAxis(state.lY);
		mouseState_.setZAxis(state.lZ);
		for(int i = 0; i < maxButtonCount; i++){
			mouseState_.setButtonPressed(i,
				((state.rgbButtons[i] & 0x80) != 0));
		}
		return true;
	}else{
		// エラーなら入力無し
		mouseState_.clear();
		// 深刻なエラーでないかチェック
		if((result != DIERR_INPUTLOST) && (result != DIERR_NOTACQUIRED)){
			ErrorOut("GetDeviceState()に失敗しました。");
		}else{
			// デバイス再取得
			acquire();
		}
	}
	return false;
}
//------------------------------------------------------------------------------
} // End of namespace Lamp
//------------------------------------------------------------------------------
