0031620: Samples - update Android JNI sample to use AIS_ViewController
[occt.git] / src / ViewerTest / ViewerTest_EventManager.cxx
CommitLineData
b311480e 1// Created on: 1998-08-27
2// Created by: Robert COUBLANC
3// Copyright (c) 1998-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 8// This library is free software; you can redistribute it and/or modify it under
9// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 10// by the Free Software Foundation, with special exception defined in the file
11// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12// distribution for complete text of the license and disclaimer of any warranty.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
780ee4e2 17#include <ViewerTest_EventManager.hxx>
42cf5bc1 18
2108d9a2 19#include <AIS_AnimationCamera.hxx>
7fd59977 20#include <AIS_InteractiveContext.hxx>
49582f9d 21#include <AIS_Shape.hxx>
679ecdee 22#include <Aspect_Grid.hxx>
49582f9d 23#include <Draw.hxx>
24#include <ViewerTest_V3dView.hxx>
25
26Standard_IMPORT Standard_Boolean Draw_Interprete (const char* theCommand);
7fd59977 27
25e59720 28IMPLEMENT_STANDARD_RTTIEXT(ViewerTest_EventManager,Standard_Transient)
92efcf78 29
2108d9a2 30//=======================================================================
31//function : GlobalViewAnimation
32//purpose :
33//=======================================================================
34const Handle(AIS_AnimationCamera)& ViewerTest_EventManager::GlobalViewAnimation()
35{
36 static Handle(AIS_AnimationCamera) THE_CAMERA_ANIM = new AIS_AnimationCamera ("ViewerTest_EventManager_ViewAnimation", Handle(V3d_View)());
37 return THE_CAMERA_ANIM;
38}
39
7fd59977 40//=======================================================================
41//function : ViewerTest_EventManager
679ecdee 42//purpose :
7fd59977 43//=======================================================================
679ecdee 44ViewerTest_EventManager::ViewerTest_EventManager (const Handle(V3d_View)& theView,
45 const Handle(AIS_InteractiveContext)& theCtx)
46: myCtx (theCtx),
47 myView (theView),
49582f9d 48 myToPickPnt (Standard_False)
2108d9a2 49{
50 myViewAnimation = GlobalViewAnimation();
51}
52
53//=======================================================================
54//function : ~ViewerTest_EventManager
55//purpose :
56//=======================================================================
57ViewerTest_EventManager::~ViewerTest_EventManager()
58{
59 if (!myViewAnimation.IsNull()
60 && myViewAnimation->View() == myView)
61 {
62 myViewAnimation->Stop();
63 myViewAnimation->SetView (Handle(V3d_View)());
64 }
65}
7fd59977 66
67//=======================================================================
49582f9d 68//function : UpdateMouseButtons
679ecdee 69//purpose :
7fd59977 70//=======================================================================
49582f9d 71bool ViewerTest_EventManager::UpdateMouseButtons (const Graphic3d_Vec2i& thePoint,
72 Aspect_VKeyMouse theButtons,
73 Aspect_VKeyFlags theModifiers,
74 bool theIsEmulated)
7fd59977 75{
49582f9d 76 SetAllowRotation (!ViewerTest_V3dView::IsCurrentViewIn2DMode());
77
78 if (theButtons == Aspect_VKeyMouse_LeftButton)
679ecdee 79 {
49582f9d 80 if (myToPickPnt && (theModifiers & Aspect_VKeyFlags_CTRL) != 0)
679ecdee 81 {
49582f9d 82 Graphic3d_Vec3d anXYZ;
83 myView->Convert (thePoint.x(), thePoint.y(), anXYZ.x(), anXYZ.y(), anXYZ.z());
84 Draw::Set (myPickPntArgVec[0].ToCString(), anXYZ.x());
85 Draw::Set (myPickPntArgVec[1].ToCString(), anXYZ.y());
86 Draw::Set (myPickPntArgVec[2].ToCString(), anXYZ.z());
87 myToPickPnt = false;
679ecdee 88 }
89 }
90
49582f9d 91 return AIS_ViewController::UpdateMouseButtons (thePoint, theButtons, theModifiers, theIsEmulated);
7fd59977 92}
93
49582f9d 94//==============================================================================
95//function : ProcessExpose
679ecdee 96//purpose :
49582f9d 97//==============================================================================
98void ViewerTest_EventManager::ProcessExpose()
7fd59977 99{
49582f9d 100 if (!myView.IsNull())
780ee4e2 101 {
49582f9d 102 myView->Invalidate();
103 FlushViewEvents (myCtx, myView, true);
679ecdee 104 }
7fd59977 105}
106
49582f9d 107//==============================================================================
108//function : ProcessConfigure
679ecdee 109//purpose :
49582f9d 110//==============================================================================
111void ViewerTest_EventManager::ProcessConfigure()
679ecdee 112{
49582f9d 113 if (!myView.IsNull())
679ecdee 114 {
49582f9d 115 myView->MustBeResized();
116 FlushViewEvents (myCtx, myView, true);
679ecdee 117 }
7fd59977 118}
119
120//=======================================================================
49582f9d 121//function : KeyUp
679ecdee 122//purpose :
7fd59977 123//=======================================================================
49582f9d 124void ViewerTest_EventManager::KeyUp (Aspect_VKey theKey,
125 double theTime)
7fd59977 126{
49582f9d 127 AIS_ViewController::KeyUp (theKey, theTime);
128 ProcessKeyPress (theKey);
7fd59977 129}
130
49582f9d 131//==============================================================================
132//function : ProcessKeyPress
679ecdee 133//purpose :
49582f9d 134//==============================================================================
135void ViewerTest_EventManager::ProcessKeyPress (Aspect_VKey theKey)
7fd59977 136{
49582f9d 137 if (myCtx.IsNull()
138 || myView.IsNull())
679ecdee 139 {
140 return;
141 }
679ecdee 142
49582f9d 143 switch (theKey)
679ecdee 144 {
49582f9d 145 case Aspect_VKey_A: // AXO
146 {
147 if (!ViewerTest_V3dView::IsCurrentViewIn2DMode())
148 {
149 myView->SetProj(V3d_XposYnegZpos);
150 }
151 break;
152 }
153 case Aspect_VKey_D: // Reset
154 {
155 if (!ViewerTest_V3dView::IsCurrentViewIn2DMode())
156 {
157 myView->Reset();
158 }
159 break;
160 }
161 case Aspect_VKey_F:
162 {
163 if (myCtx->NbSelected() > 0)
164 {
165 myCtx->FitSelected (myView);
166 }
167 else
168 {
169 myView->FitAll();
170 }
171 break;
172 }
173 case Aspect_VKey_H: // HLR
174 {
175 std::cout << "HLR\n";
176 myView->SetComputedMode (!myView->ComputedMode());
177 myView->Redraw();
178 break;
179 }
180 case Aspect_VKey_P: // Type of HLR
181 {
182 myCtx->DefaultDrawer()->SetTypeOfHLR (myCtx->DefaultDrawer()->TypeOfHLR() == Prs3d_TOH_Algo
183 ? Prs3d_TOH_PolyAlgo
184 : Prs3d_TOH_Algo);
185 if (myCtx->NbSelected() == 0)
186 {
187 AIS_ListOfInteractive aListOfShapes;
188 myCtx->DisplayedObjects (aListOfShapes);
189 for (AIS_ListIteratorOfListOfInteractive anIter (aListOfShapes); anIter.More(); anIter.Next())
190 {
191 if (Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast (anIter.Value()))
192 {
193 aShape->SetTypeOfHLR (aShape->TypeOfHLR() == Prs3d_TOH_PolyAlgo
194 ? Prs3d_TOH_Algo
195 : Prs3d_TOH_PolyAlgo);
196 myCtx->Redisplay (aShape, Standard_False);
197 }
198 }
199 }
200 else
201 {
202 for (myCtx->InitSelected(); myCtx->MoreSelected(); myCtx->NextSelected())
203 {
204 if (Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast (myCtx->SelectedInteractive()))
205 {
206 aShape->SetTypeOfHLR (aShape->TypeOfHLR() == Prs3d_TOH_PolyAlgo
207 ? Prs3d_TOH_Algo
208 : Prs3d_TOH_PolyAlgo);
209 myCtx->Redisplay (aShape, Standard_False);
210 }
211 }
212 }
213 myCtx->UpdateCurrentViewer();
214 break;
215 }
216 case Aspect_VKey_S:
217 case Aspect_VKey_W:
218 {
219 Standard_Integer aDispMode = AIS_Shaded;
220 if (theKey == Aspect_VKey_S)
221 {
222 aDispMode = AIS_Shaded;
223 std::cout << "setup Shaded display mode\n";
224 }
225 else
226 {
227 aDispMode = AIS_WireFrame;
228 std::cout << "setup WireFrame display mode\n";
229 }
4754e164 230
49582f9d 231 if (myCtx->NbSelected() == 0)
232 {
233 myCtx->SetDisplayMode (aDispMode, true);
234 }
235 else
236 {
237 for (myCtx->InitSelected(); myCtx->MoreSelected(); myCtx->NextSelected())
238 {
239 myCtx->SetDisplayMode (myCtx->SelectedInteractive(), aDispMode, false);
240 }
241 myCtx->UpdateCurrentViewer();
242 }
243 break;
244 }
245 case Aspect_VKey_U: // Unset display mode
246 {
247 std::cout << "reset display mode to defaults\n";
248 if (myCtx->NbSelected() == 0)
249 {
250 myCtx->SetDisplayMode (AIS_WireFrame, true);
251 }
252 else
253 {
254 for (myCtx->InitSelected(); myCtx->MoreSelected(); myCtx->NextSelected())
255 {
256 myCtx->UnsetDisplayMode (myCtx->SelectedInteractive(), false);
257 }
258 myCtx->UpdateCurrentViewer();
259 }
260 break;
261 }
262 case Aspect_VKey_T:
263 {
264 if (!ViewerTest_V3dView::IsCurrentViewIn2DMode())
265 {
266 myView->SetProj (V3d_TypeOfOrientation_Zup_Top);
267 }
268 break;
269 }
270 case Aspect_VKey_B:
271 {
272 if (!ViewerTest_V3dView::IsCurrentViewIn2DMode())
273 {
274 myView->SetProj (V3d_TypeOfOrientation_Zup_Bottom);
275 }
276 break;
277 }
278 case Aspect_VKey_L:
279 {
280 if (!ViewerTest_V3dView::IsCurrentViewIn2DMode())
281 {
282 myView->SetProj (V3d_TypeOfOrientation_Zup_Left);
283 }
284 break;
285 }
286 case Aspect_VKey_R:
287 {
288 if (!ViewerTest_V3dView::IsCurrentViewIn2DMode())
289 {
290 myView->SetProj (V3d_TypeOfOrientation_Zup_Right);
291 }
292 break;
293 }
294 case Aspect_VKey_Comma:
295 {
296 myCtx->HilightNextDetected (myView);
297 break;
298 }
299 case Aspect_VKey_Period:
300 {
301 myCtx->HilightPreviousDetected (myView);
302 break;
303 }
304 case Aspect_VKey_Slash:
305 case Aspect_VKey_NumpadDivide:
306 {
307 Handle(Graphic3d_Camera) aCamera = myView->Camera();
308 if (aCamera->IsStereo())
309 {
310 aCamera->SetIOD (aCamera->GetIODType(), aCamera->IOD() - 0.01);
311 myView->Redraw();
312 }
313 break;
314 }
315 case Aspect_VKey_NumpadMultiply:
316 {
317 Handle(Graphic3d_Camera) aCamera = myView->Camera();
318 if (aCamera->IsStereo())
319 {
320 aCamera->SetIOD (aCamera->GetIODType(), aCamera->IOD() + 0.01);
321 myView->Redraw();
322 }
323 break;
324 }
325 case Aspect_VKey_Delete:
326 {
327 if (!myCtx.IsNull()
328 && myCtx->NbSelected() > 0)
329 {
330 Draw_Interprete ("verase");
331 }
332 break;
333 }
334 case Aspect_VKey_Escape:
335 {
336 if (!myCtx.IsNull()
337 && ViewerTest_EventManager::ToCloseViewOnEscape())
338 {
339 Draw_Interprete (ViewerTest_EventManager::ToExitOnCloseView() ? "exit" : "vclose");
340 }
341 }
342 }
4754e164 343
49582f9d 344 if (theKey >= Aspect_VKey_0
345 && theKey <= Aspect_VKey_7)
679ecdee 346 {
49582f9d 347 const Standard_Integer aSelMode = theKey - Aspect_VKey_0;
348 bool toEnable = true;
349 if (!myCtx.IsNull())
350 {
351 AIS_ListOfInteractive aPrsList;
352 myCtx->DisplayedObjects (aPrsList);
353 for (AIS_ListOfInteractive::Iterator aPrsIter (aPrsList); aPrsIter.More() && toEnable; aPrsIter.Next())
354 {
355 TColStd_ListOfInteger aModes;
356 myCtx->ActivatedModes (aPrsIter.Value(), aModes);
357 for (TColStd_ListOfInteger::Iterator aModeIter (aModes); aModeIter.More() && toEnable; aModeIter.Next())
358 {
359 if (aModeIter.Value() == aSelMode)
360 {
361 toEnable = false;
362 }
363 }
364 }
365 }
366 TCollection_AsciiString aCmd = TCollection_AsciiString ("vselmode ") + aSelMode + (toEnable ? " 1" : " 0");
367 Draw_Interprete (aCmd.ToCString());
679ecdee 368 }
c398b00e 369}