MouseDevice.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #ifndef _XBOX
  2. #include "MouseDevice.h"
  3. #define DIRECTINPUT_VERSION 0x0800
  4. #include <dinput.h>
  5. #include "..\..\common_h\systemmsg.h"
  6. #include "..\..\common_h\controls.h"
  7. MouseDevice::MouseDevice(IDirectInput8A* di, const DIDEVICEINSTANCEA& deviceInst)
  8. : m_Controls(_FL_),m_Device(null)
  9. {
  10. Assert(di)
  11. di->CreateDevice(deviceInst.guidInstance,&m_Device,null);
  12. Assert(m_Device)
  13. m_hWnd = (HWND)api->Storage().GetLong("system.hwnd");
  14. m_Device->SetCooperativeLevel(m_hWnd,DISCL_FOREGROUND|DISCL_NONEXCLUSIVE);
  15. m_Device->SetDataFormat(&c_dfDIMouse);
  16. m_Device->Acquire();
  17. InitControls();
  18. this->di = di;
  19. }
  20. MouseDevice::~MouseDevice()
  21. {
  22. if( m_Device )
  23. {
  24. m_Device->Unacquire();
  25. RELEASE(m_Device)
  26. }
  27. }
  28. void __declspec(dllexport) MouseDevice::InitControls()
  29. {
  30. Control ctrl;
  31. ctrl.value = 0.0f;
  32. ctrl.dataItem = DeltaX; ctrl.name = "MouseDeltaH"; m_Controls.Add(ctrl);
  33. ctrl.dataItem = DeltaY; ctrl.name = "MouseDeltaV"; m_Controls.Add(ctrl);
  34. ctrl.dataItem = Wheel; ctrl.name = "MouseWheel"; m_Controls.Add(ctrl);
  35. ctrl.dataItem = X; ctrl.name = "MouseH"; m_Controls.Add(ctrl);
  36. ctrl.dataItem = Y; ctrl.name = "MouseV"; m_Controls.Add(ctrl);
  37. ctrl.dataItem = LButton; ctrl.name = "MouseLClick"; m_Controls.Add(ctrl);
  38. ctrl.dataItem = RButton; ctrl.name = "MouseRClick"; m_Controls.Add(ctrl);
  39. ctrl.dataItem = MButton; ctrl.name = "MouseMClick"; m_Controls.Add(ctrl);
  40. ctrl.dataItem = DblLButton; ctrl.name = "MouseLDouble"; m_Controls.Add(ctrl);
  41. ctrl.dataItem = DblRButton; ctrl.name = "MouseRDouble"; m_Controls.Add(ctrl);
  42. ctrl.dataItem = DblMButton; ctrl.name = "MouseMDouble"; m_Controls.Add(ctrl);
  43. }
  44. void MouseDevice::Update(float DeltaTime)
  45. {
  46. if( !m_Device )
  47. return;
  48. m_Data[DblLButton] =
  49. m_Data[DblRButton] =
  50. m_Data[DblMButton] = 0;
  51. CoreSystemMessage msg;
  52. ControlsMessage *data = null;
  53. for( dword i = 0; i < api->GetSystemMessagesCount() ; i++ )
  54. {
  55. api->GetSystemMessage(i,msg);
  56. if( string::IsEqual(msg.id,CONTROLS_MESSAGE))
  57. {
  58. if( data = (ControlsMessage *)msg.data )
  59. {
  60. switch( data->dwMsgID )
  61. {
  62. case WM_LBUTTONDBLCLK:
  63. m_Data[DblLButton] = 1; break;
  64. case WM_RBUTTONDBLCLK:
  65. m_Data[DblRButton] = 1; break;
  66. case WM_MBUTTONDBLCLK:
  67. m_Data[DblMButton] = 1; break;
  68. }
  69. }
  70. }
  71. }
  72. POINT absMousePos = {0};
  73. DIMOUSESTATE mouseState;
  74. HRESULT hr = m_Device->GetDeviceState(sizeof(mouseState),&mouseState);
  75. if( SUCCEEDED(hr))
  76. {
  77. GetCursorPos(&absMousePos); ScreenToClient(m_hWnd,&absMousePos);
  78. m_Data[DeltaX] = mouseState.lX;
  79. m_Data[DeltaY] = mouseState.lY;
  80. m_Data[Wheel] = mouseState.lZ/120;
  81. m_Data[X] = absMousePos.x;
  82. m_Data[Y] = absMousePos.y;
  83. m_Data[LButton] = (mouseState.rgbButtons[0] & 0x80) >> 7;
  84. m_Data[RButton] = (mouseState.rgbButtons[1] & 0x80) >> 7;
  85. m_Data[MButton] = (mouseState.rgbButtons[2] & 0x80) >> 7;
  86. }
  87. else
  88. {
  89. m_Data[DeltaX] = m_Data[DeltaY] = m_Data[Wheel] = 0;
  90. m_Data[X] = m_Data[Y] = 0;
  91. m_Data[LButton] = m_Data[RButton] = m_Data[MButton] = 0;
  92. }
  93. for( dword i = 0; i < m_Controls.Size() ; i++ )
  94. {
  95. m_Controls[i].value = (float)m_Data[m_Controls[i].dataItem];
  96. }
  97. if( FAILED(hr))
  98. {
  99. m_Device->SetCooperativeLevel(m_hWnd,DISCL_FOREGROUND|DISCL_NONEXCLUSIVE);
  100. m_Device->Acquire();
  101. }
  102. }
  103. long MouseDevice::GetIndex(const char* deviceControl)
  104. {
  105. for( dword i = 0; i < m_Controls.Size() ; i++ )
  106. {
  107. if( deviceControl == m_Controls[i].name )
  108. return i;
  109. }
  110. return INVALID_CODE;
  111. }
  112. float MouseDevice::GetRawValue(long controlIndex) const
  113. {
  114. if((dword)controlIndex >= m_Controls.Size())
  115. return 0;
  116. return m_Controls[controlIndex].value;
  117. }
  118. #endif