PageRenderTime 119ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Interaction/Widgets/vtkBoxWidget2.cxx

https://github.com/aashish24/VTK-old
C++ | 332 lines | 233 code | 42 blank | 57 comment | 35 complexity | 386cd75aa80518bb9c5e51ec2f77ff86 MD5 | raw file
  1. /*=========================================================================
  2. Program: Visualization Toolkit
  3. Module: vtkBoxWidget2.cxx
  4. Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  5. All rights reserved.
  6. See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
  7. This software is distributed WITHOUT ANY WARRANTY; without even
  8. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  9. PURPOSE. See the above copyright notice for more information.
  10. =========================================================================*/
  11. #include "vtkBoxWidget2.h"
  12. #include "vtkBoxRepresentation.h"
  13. #include "vtkCommand.h"
  14. #include "vtkCallbackCommand.h"
  15. #include "vtkRenderWindowInteractor.h"
  16. #include "vtkObjectFactory.h"
  17. #include "vtkWidgetEventTranslator.h"
  18. #include "vtkWidgetCallbackMapper.h"
  19. #include "vtkEvent.h"
  20. #include "vtkWidgetEvent.h"
  21. #include "vtkRenderWindow.h"
  22. #include "vtkRenderer.h"
  23. vtkStandardNewMacro(vtkBoxWidget2);
  24. //----------------------------------------------------------------------------
  25. vtkBoxWidget2::vtkBoxWidget2()
  26. {
  27. this->WidgetState = vtkBoxWidget2::Start;
  28. this->ManagesCursor = 1;
  29. this->TranslationEnabled = 1;
  30. this->ScalingEnabled = 1;
  31. this->RotationEnabled = 1;
  32. this->MoveFacesEnabled = 1;
  33. // Define widget events
  34. this->CallbackMapper->SetCallbackMethod(vtkCommand::LeftButtonPressEvent,
  35. vtkEvent::NoModifier,
  36. 0, 0, NULL,
  37. vtkWidgetEvent::Select,
  38. this, vtkBoxWidget2::SelectAction);
  39. this->CallbackMapper->SetCallbackMethod(vtkCommand::LeftButtonReleaseEvent,
  40. vtkEvent::NoModifier,
  41. 0, 0, NULL,
  42. vtkWidgetEvent::EndSelect,
  43. this, vtkBoxWidget2::EndSelectAction);
  44. this->CallbackMapper->SetCallbackMethod(vtkCommand::MiddleButtonPressEvent,
  45. vtkWidgetEvent::Translate,
  46. this, vtkBoxWidget2::TranslateAction);
  47. this->CallbackMapper->SetCallbackMethod(vtkCommand::MiddleButtonReleaseEvent,
  48. vtkWidgetEvent::EndTranslate,
  49. this, vtkBoxWidget2::EndSelectAction);
  50. this->CallbackMapper->SetCallbackMethod(vtkCommand::LeftButtonPressEvent,
  51. vtkEvent::ControlModifier,
  52. 0, 0, NULL,
  53. vtkWidgetEvent::Translate,
  54. this, vtkBoxWidget2::TranslateAction);
  55. this->CallbackMapper->SetCallbackMethod(vtkCommand::LeftButtonReleaseEvent,
  56. vtkEvent::ControlModifier,
  57. 0, 0, NULL,
  58. vtkWidgetEvent::EndTranslate,
  59. this, vtkBoxWidget2::EndSelectAction);
  60. this->CallbackMapper->SetCallbackMethod(vtkCommand::LeftButtonPressEvent,
  61. vtkEvent::ShiftModifier,
  62. 0, 0, NULL,
  63. vtkWidgetEvent::Translate,
  64. this, vtkBoxWidget2::TranslateAction);
  65. this->CallbackMapper->SetCallbackMethod(vtkCommand::LeftButtonReleaseEvent,
  66. vtkEvent::ShiftModifier,
  67. 0, 0, NULL,
  68. vtkWidgetEvent::EndTranslate,
  69. this, vtkBoxWidget2::EndSelectAction);
  70. this->CallbackMapper->SetCallbackMethod(vtkCommand::RightButtonPressEvent,
  71. vtkWidgetEvent::Scale,
  72. this, vtkBoxWidget2::ScaleAction);
  73. this->CallbackMapper->SetCallbackMethod(vtkCommand::RightButtonReleaseEvent,
  74. vtkWidgetEvent::EndScale,
  75. this, vtkBoxWidget2::EndSelectAction);
  76. this->CallbackMapper->SetCallbackMethod(vtkCommand::MouseMoveEvent,
  77. vtkWidgetEvent::Move,
  78. this, vtkBoxWidget2::MoveAction);
  79. }
  80. //----------------------------------------------------------------------------
  81. vtkBoxWidget2::~vtkBoxWidget2()
  82. {
  83. }
  84. //----------------------------------------------------------------------
  85. void vtkBoxWidget2::SelectAction(vtkAbstractWidget *w)
  86. {
  87. // We are in a static method, cast to ourself
  88. vtkBoxWidget2 *self = reinterpret_cast<vtkBoxWidget2*>(w);
  89. // Get the event position
  90. int X = self->Interactor->GetEventPosition()[0];
  91. int Y = self->Interactor->GetEventPosition()[1];
  92. // Okay, make sure that the pick is in the current renderer
  93. if ( !self->CurrentRenderer ||
  94. !self->CurrentRenderer->IsInViewport(X,Y) )
  95. {
  96. self->WidgetState = vtkBoxWidget2::Start;
  97. return;
  98. }
  99. // Begin the widget interaction which has the side effect of setting the
  100. // interaction state.
  101. double e[2];
  102. e[0] = static_cast<double>(X);
  103. e[1] = static_cast<double>(Y);
  104. self->WidgetRep->StartWidgetInteraction(e);
  105. int interactionState = self->WidgetRep->GetInteractionState();
  106. if ( interactionState == vtkBoxRepresentation::Outside )
  107. {
  108. return;
  109. }
  110. // Test for states that involve face or handle picking here so
  111. // selection highlighting doesn't happen if that interaction is disabled.
  112. // Non-handle-grabbing transformations are tested in the "Action" methods.
  113. // Rotation
  114. if (interactionState == vtkBoxRepresentation::Rotating
  115. && self->RotationEnabled == 0)
  116. {
  117. return;
  118. }
  119. // Face Movement
  120. if ((interactionState == vtkBoxRepresentation::MoveF0 ||
  121. interactionState == vtkBoxRepresentation::MoveF1 ||
  122. interactionState == vtkBoxRepresentation::MoveF2 ||
  123. interactionState == vtkBoxRepresentation::MoveF3 ||
  124. interactionState == vtkBoxRepresentation::MoveF4 ||
  125. interactionState == vtkBoxRepresentation::MoveF5)
  126. && self->MoveFacesEnabled == 0)
  127. {
  128. return;
  129. }
  130. // Translation
  131. if (interactionState == vtkBoxRepresentation::Translating
  132. && self->TranslationEnabled == 0)
  133. {
  134. return;
  135. }
  136. // We are definitely selected
  137. self->WidgetState = vtkBoxWidget2::Active;
  138. self->GrabFocus(self->EventCallbackCommand);
  139. // The SetInteractionState has the side effect of highlighting the widget
  140. reinterpret_cast<vtkBoxRepresentation*>(self->WidgetRep)->
  141. SetInteractionState(interactionState);
  142. // start the interaction
  143. self->EventCallbackCommand->SetAbortFlag(1);
  144. self->StartInteraction();
  145. self->InvokeEvent(vtkCommand::StartInteractionEvent,NULL);
  146. self->Render();
  147. }
  148. //----------------------------------------------------------------------
  149. void vtkBoxWidget2::TranslateAction(vtkAbstractWidget *w)
  150. {
  151. // We are in a static method, cast to ourself
  152. vtkBoxWidget2 *self = reinterpret_cast<vtkBoxWidget2*>(w);
  153. if (self->TranslationEnabled == 0)
  154. {
  155. return;
  156. }
  157. // Get the event position
  158. int X = self->Interactor->GetEventPosition()[0];
  159. int Y = self->Interactor->GetEventPosition()[1];
  160. // Okay, make sure that the pick is in the current renderer
  161. if ( !self->CurrentRenderer ||
  162. !self->CurrentRenderer->IsInViewport(X,Y) )
  163. {
  164. self->WidgetState = vtkBoxWidget2::Start;
  165. return;
  166. }
  167. // Begin the widget interaction which has the side effect of setting the
  168. // interaction state.
  169. double e[2];
  170. e[0] = static_cast<double>(X);
  171. e[1] = static_cast<double>(Y);
  172. self->WidgetRep->StartWidgetInteraction(e);
  173. int interactionState = self->WidgetRep->GetInteractionState();
  174. if ( interactionState == vtkBoxRepresentation::Outside )
  175. {
  176. return;
  177. }
  178. // We are definitely selected
  179. self->WidgetState = vtkBoxWidget2::Active;
  180. self->GrabFocus(self->EventCallbackCommand);
  181. reinterpret_cast<vtkBoxRepresentation*>(self->WidgetRep)->
  182. SetInteractionState(vtkBoxRepresentation::Translating);
  183. // start the interaction
  184. self->EventCallbackCommand->SetAbortFlag(1);
  185. self->StartInteraction();
  186. self->InvokeEvent(vtkCommand::StartInteractionEvent,NULL);
  187. self->Render();
  188. }
  189. //----------------------------------------------------------------------
  190. void vtkBoxWidget2::ScaleAction(vtkAbstractWidget *w)
  191. {
  192. // We are in a static method, cast to ourself
  193. vtkBoxWidget2 *self = reinterpret_cast<vtkBoxWidget2*>(w);
  194. if (self->ScalingEnabled == 0)
  195. {
  196. return;
  197. }
  198. // Get the event position
  199. int X = self->Interactor->GetEventPosition()[0];
  200. int Y = self->Interactor->GetEventPosition()[1];
  201. // Okay, make sure that the pick is in the current renderer
  202. if ( !self->CurrentRenderer ||
  203. !self->CurrentRenderer->IsInViewport(X,Y) )
  204. {
  205. self->WidgetState = vtkBoxWidget2::Start;
  206. return;
  207. }
  208. // Begin the widget interaction which has the side effect of setting the
  209. // interaction state.
  210. double e[2];
  211. e[0] = static_cast<double>(X);
  212. e[1] = static_cast<double>(Y);
  213. self->WidgetRep->StartWidgetInteraction(e);
  214. int interactionState = self->WidgetRep->GetInteractionState();
  215. if ( interactionState == vtkBoxRepresentation::Outside )
  216. {
  217. return;
  218. }
  219. // We are definitely selected
  220. self->WidgetState = vtkBoxWidget2::Active;
  221. self->GrabFocus(self->EventCallbackCommand);
  222. reinterpret_cast<vtkBoxRepresentation*>(self->WidgetRep)->
  223. SetInteractionState(vtkBoxRepresentation::Scaling);
  224. // start the interaction
  225. self->EventCallbackCommand->SetAbortFlag(1);
  226. self->StartInteraction();
  227. self->InvokeEvent(vtkCommand::StartInteractionEvent,NULL);
  228. self->Render();
  229. }
  230. //----------------------------------------------------------------------
  231. void vtkBoxWidget2::MoveAction(vtkAbstractWidget *w)
  232. {
  233. vtkBoxWidget2 *self = reinterpret_cast<vtkBoxWidget2*>(w);
  234. // See whether we're active
  235. if ( self->WidgetState == vtkBoxWidget2::Start )
  236. {
  237. return;
  238. }
  239. // compute some info we need for all cases
  240. int X = self->Interactor->GetEventPosition()[0];
  241. int Y = self->Interactor->GetEventPosition()[1];
  242. // Okay, adjust the representation
  243. double e[2];
  244. e[0] = static_cast<double>(X);
  245. e[1] = static_cast<double>(Y);
  246. self->WidgetRep->WidgetInteraction(e);
  247. // moving something
  248. self->EventCallbackCommand->SetAbortFlag(1);
  249. self->InvokeEvent(vtkCommand::InteractionEvent,NULL);
  250. self->Render();
  251. }
  252. //----------------------------------------------------------------------
  253. void vtkBoxWidget2::EndSelectAction(vtkAbstractWidget *w)
  254. {
  255. vtkBoxWidget2 *self = reinterpret_cast<vtkBoxWidget2*>(w);
  256. if ( self->WidgetState == vtkBoxWidget2::Start )
  257. {
  258. return;
  259. }
  260. // Return state to not active
  261. self->WidgetState = vtkBoxWidget2::Start;
  262. reinterpret_cast<vtkBoxRepresentation*>(self->WidgetRep)->
  263. SetInteractionState(vtkBoxRepresentation::Outside);
  264. self->ReleaseFocus();
  265. self->EventCallbackCommand->SetAbortFlag(1);
  266. self->EndInteraction();
  267. self->InvokeEvent(vtkCommand::EndInteractionEvent,NULL);
  268. self->Render();
  269. }
  270. //----------------------------------------------------------------------
  271. void vtkBoxWidget2::CreateDefaultRepresentation()
  272. {
  273. if ( ! this->WidgetRep )
  274. {
  275. this->WidgetRep = vtkBoxRepresentation::New();
  276. }
  277. }
  278. //----------------------------------------------------------------------------
  279. void vtkBoxWidget2::PrintSelf(ostream& os, vtkIndent indent)
  280. {
  281. this->Superclass::PrintSelf(os,indent);
  282. os << indent << "Translation Enabled: " << (this->TranslationEnabled ? "On\n" : "Off\n");
  283. os << indent << "Scaling Enabled: " << (this->ScalingEnabled ? "On\n" : "Off\n");
  284. os << indent << "Rotation Enabled: " << (this->RotationEnabled ? "On\n" : "Off\n");
  285. os << indent << "Move Faces Enabled: " << (this->MoveFacesEnabled ? "On\n" : "Off\n");
  286. }