ManipulationEventSink.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include "pch.h"
  2. #include <windows.h>
  3. #include <assert.h>
  4. #define ASSERT assert
  5. #include <stdio.h>
  6. #include "ManipulationEventSink.h"
  7. namespace ManipulationHelper
  8. {
  9. ManipulationEventSink::ManipulationEventSink() :
  10. m_cRefCount(1),
  11. m_pConnection(NULL),
  12. m_dwCookie(0)
  13. { }
  14. bool ManipulationEventSink::Connect(IManipulationProcessor* pManipulationProcessor)
  15. {
  16. // Check input arguments
  17. if (pManipulationProcessor == NULL)
  18. {
  19. ASSERT((pManipulationProcessor != NULL) && L"CManipulationEventSink::Create : incorrect arguments");
  20. return false;
  21. }
  22. // Check object state
  23. if ((m_dwCookie != 0) || (m_pConnection != NULL))
  24. {
  25. ASSERT((m_dwCookie == 0) && (m_pConnection == NULL) && L"CManipulationEventSink::Connect : connection already established");
  26. return false;
  27. }
  28. // Get the container with the connection points.
  29. IConnectionPointContainer* pConnectionContainer = NULL;
  30. HRESULT hr = pManipulationProcessor->QueryInterface(&pConnectionContainer);
  31. if (FAILED(hr))
  32. {
  33. ASSERT(SUCCEEDED(hr) && L"CManipulationEventSink::Connect : failed to get the container with the connection points");
  34. return false;
  35. }
  36. // Get a connection point.
  37. hr = pConnectionContainer->FindConnectionPoint(__uuidof(_IManipulationEvents), &m_pConnection);
  38. if (FAILED(hr))
  39. {
  40. ASSERT(SUCCEEDED(hr) && L"CManipulationEventSink::Connect : failed to get a connection point");
  41. pConnectionContainer->Release();
  42. return false;
  43. }
  44. // Release the connection container.
  45. pConnectionContainer->Release();
  46. // Advise. Establishes an advisory connection between the connection point and the
  47. // caller's sink object.
  48. hr = m_pConnection->Advise(this, &m_dwCookie);
  49. if (FAILED(hr))
  50. {
  51. ASSERT(SUCCEEDED(hr) && L"CManipulationEventSink::Connect : failed to Advise");
  52. m_pConnection->Release();
  53. m_pConnection = NULL;
  54. return false;
  55. }
  56. m_pIManipProc = pManipulationProcessor;
  57. return true;
  58. }
  59. bool ManipulationEventSink::Disconnect()
  60. {
  61. // Check object state
  62. if ((m_dwCookie == 0) || (m_pConnection == NULL))
  63. {
  64. ASSERT((m_dwCookie != 0) && (m_pConnection != NULL) && L"CManipulationEventSink::Disconnect : connection does not exist");
  65. return false;
  66. }
  67. // Unadvise. Terminate the connection.
  68. HRESULT hr = m_pConnection->Unadvise(m_dwCookie);
  69. ASSERT(SUCCEEDED(hr) && L"CManipulationEventSink::Disconnect : failed to Unadvise");
  70. UNREFERENCED_PARAMETER(hr);
  71. m_pConnection->Release();
  72. m_pConnection = NULL;
  73. m_dwCookie = 0;
  74. return true;
  75. }
  76. void ManipulationEventSink::SetManipulationStartedCallBack(ManipulationStartedCallBack msCallback)
  77. {
  78. m_mStartedCallBack = msCallback;
  79. }
  80. void ManipulationEventSink::SetManipulationDeltaCallBack(ManipulationDeltaCallBack mdCallback)
  81. {
  82. m_mDeltaCallBack = mdCallback;
  83. }
  84. void ManipulationEventSink::SetManipulationCompletedCallBack(ManipulationCompletedCallBack mcCallback)
  85. {
  86. m_mCompletedCallBack = mcCallback;
  87. }
  88. ManipulationEventSink::~ManipulationEventSink()
  89. {
  90. ASSERT((m_dwCookie == 0) && (m_pConnection == NULL) && L"CManipulationEventSink destructor : connection is not properly terminated");
  91. }
  92. HRESULT STDMETHODCALLTYPE ManipulationEventSink::ManipulationStarted(
  93. FLOAT x,
  94. FLOAT y)
  95. {
  96. if (m_mStartedCallBack != NULL)
  97. {
  98. m_mStartedCallBack(x, y);
  99. }
  100. return S_OK;
  101. }
  102. // This event is called by the ManipulationProcessor during the movement of
  103. // the fingers.
  104. // in:
  105. // x - x coordiante of the initial point of manipulation
  106. // (1/100 of pixel)
  107. // y - y coordiante of the initial point of manipulation
  108. // (1/100 of pixel)
  109. // translationDeltaX - shift of the x-coordinate (1/100 of pixel)
  110. // translationDeltaY - shift of the y-coordinate (1/100 of pixel)
  111. // scaleDelta - scale factor (zoom in/out)
  112. // expansionDelta - the current rate of scale change
  113. // rotationDelta - rotation angle in radians
  114. // cumulativeTranslationX - cumulative shift of x-coordinate (1/100 of pixel)
  115. // cumulativeTranslationY - cumulative shift of y-coordinate (1/100 of pixel)
  116. // cumulativeScale - cumulative scale factor (zoom in/out)
  117. // cumulativeExpansion - cumulative rate of scale change
  118. // cumulativeRotation - cumulative rotation angle in radians
  119. HRESULT STDMETHODCALLTYPE ManipulationEventSink::ManipulationDelta(
  120. FLOAT x,
  121. FLOAT y,
  122. FLOAT translationDeltaX,
  123. FLOAT translationDeltaY,
  124. FLOAT scaleDelta,
  125. FLOAT expansionDelta,
  126. FLOAT rotationDelta,
  127. FLOAT cumulativeTranslationX,
  128. FLOAT cumulativeTranslationY,
  129. FLOAT cumulativeScale,
  130. FLOAT cumulativeExpansion,
  131. FLOAT cumulativeRotation)
  132. {
  133. FLOAT verlocityX = 0;
  134. FLOAT verlocityY = 0;
  135. m_pIManipProc->GetVelocityX(&verlocityX);
  136. m_pIManipProc->GetVelocityY(&verlocityY);
  137. if (m_mDeltaCallBack != NULL)
  138. {
  139. m_mDeltaCallBack(
  140. x,
  141. y,
  142. translationDeltaX,
  143. translationDeltaY,
  144. scaleDelta,
  145. expansionDelta,
  146. rotationDelta,
  147. cumulativeTranslationX,
  148. cumulativeTranslationY,
  149. cumulativeScale,
  150. cumulativeExpansion,
  151. cumulativeRotation,
  152. verlocityX,
  153. verlocityY);
  154. }
  155. return S_OK;
  156. }
  157. HRESULT STDMETHODCALLTYPE ManipulationEventSink::ManipulationCompleted(
  158. FLOAT x,
  159. FLOAT y,
  160. FLOAT cumulativeTranslationX,
  161. FLOAT cumulativeTranslationY,
  162. FLOAT cumulativeScale,
  163. FLOAT cumulativeExpansion,
  164. FLOAT cumulativeRotation)
  165. {
  166. m_mCompletedCallBack(
  167. x,
  168. y,
  169. cumulativeTranslationX,
  170. cumulativeTranslationY,
  171. cumulativeScale,
  172. cumulativeExpansion,
  173. cumulativeRotation);
  174. return S_OK;
  175. }
  176. // IUnknown implementation
  177. ULONG ManipulationEventSink::AddRef(void)
  178. {
  179. return InterlockedIncrement(&m_cRefCount);
  180. }
  181. ULONG ManipulationEventSink::Release(void)
  182. {
  183. ULONG cNewRefCount = InterlockedDecrement(&m_cRefCount);
  184. if (cNewRefCount == 0)
  185. {
  186. delete this;
  187. }
  188. return cNewRefCount;
  189. }
  190. HRESULT ManipulationEventSink::QueryInterface(REFIID riid, LPVOID* ppvObj)
  191. {
  192. if ((riid == __uuidof(_IManipulationEvents)) || (riid == IID_IUnknown))
  193. {
  194. *ppvObj = this;
  195. AddRef();
  196. return S_OK;
  197. }
  198. *ppvObj = NULL;
  199. return E_NOINTERFACE;
  200. }
  201. }