1 // Created on: 1998-08-27
2 // Created by: Robert COUBLANC
3 // Copyright (c) 1998-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
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
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <ViewerTest_EventManager.hxx>
19 #include <AIS_AnimationCamera.hxx>
20 #include <AIS_InteractiveContext.hxx>
21 #include <AIS_Shape.hxx>
22 #include <Aspect_Grid.hxx>
24 #include <ViewerTest_ContinuousRedrawer.hxx>
25 #include <ViewerTest_V3dView.hxx>
27 Standard_IMPORT Standard_Boolean Draw_Interprete (const char* theCommand);
29 IMPLEMENT_STANDARD_RTTIEXT(ViewerTest_EventManager,Standard_Transient)
31 //=======================================================================
32 //function : GlobalViewAnimation
34 //=======================================================================
35 const Handle(AIS_AnimationCamera)& ViewerTest_EventManager::GlobalViewAnimation()
37 static Handle(AIS_AnimationCamera) THE_CAMERA_ANIM = new AIS_AnimationCamera ("ViewerTest_EventManager_ViewAnimation", Handle(V3d_View)());
38 return THE_CAMERA_ANIM;
41 //=======================================================================
42 //function : ViewerTest_EventManager
44 //=======================================================================
45 ViewerTest_EventManager::ViewerTest_EventManager (const Handle(V3d_View)& theView,
46 const Handle(AIS_InteractiveContext)& theCtx)
49 myToPickPnt (Standard_False),
50 myIsTmpContRedraw (Standard_False)
52 myViewAnimation = GlobalViewAnimation();
54 addActionHotKeys (Aspect_VKey_NavForward, Aspect_VKey_W, Aspect_VKey_W | Aspect_VKeyFlags_SHIFT);
55 addActionHotKeys (Aspect_VKey_NavBackward , Aspect_VKey_S, Aspect_VKey_S | Aspect_VKeyFlags_SHIFT);
56 addActionHotKeys (Aspect_VKey_NavSlideLeft, Aspect_VKey_A, Aspect_VKey_A | Aspect_VKeyFlags_SHIFT);
57 addActionHotKeys (Aspect_VKey_NavSlideRight, Aspect_VKey_D, Aspect_VKey_D | Aspect_VKeyFlags_SHIFT);
58 addActionHotKeys (Aspect_VKey_NavRollCCW, Aspect_VKey_Q, Aspect_VKey_Q | Aspect_VKeyFlags_SHIFT);
59 addActionHotKeys (Aspect_VKey_NavRollCW, Aspect_VKey_E, Aspect_VKey_E | Aspect_VKeyFlags_SHIFT);
61 addActionHotKeys (Aspect_VKey_NavSpeedIncrease, Aspect_VKey_Plus, Aspect_VKey_Plus | Aspect_VKeyFlags_SHIFT,
63 Aspect_VKey_NumpadAdd, Aspect_VKey_NumpadAdd | Aspect_VKeyFlags_SHIFT);
64 addActionHotKeys (Aspect_VKey_NavSpeedDecrease, Aspect_VKey_Minus, Aspect_VKey_Minus | Aspect_VKeyFlags_SHIFT,
65 Aspect_VKey_NumpadSubtract, Aspect_VKey_NumpadSubtract | Aspect_VKeyFlags_SHIFT);
67 addActionHotKeys (Aspect_VKey_NavLookUp, Aspect_VKey_Up);
68 addActionHotKeys (Aspect_VKey_NavLookDown, Aspect_VKey_Down);
69 addActionHotKeys (Aspect_VKey_NavLookLeft, Aspect_VKey_Left);
70 addActionHotKeys (Aspect_VKey_NavLookRight, Aspect_VKey_Right);
71 addActionHotKeys (Aspect_VKey_NavSlideLeft, Aspect_VKey_Left | Aspect_VKeyFlags_SHIFT);
72 addActionHotKeys (Aspect_VKey_NavSlideRight, Aspect_VKey_Right | Aspect_VKeyFlags_SHIFT);
73 addActionHotKeys (Aspect_VKey_NavSlideUp, Aspect_VKey_Up | Aspect_VKeyFlags_SHIFT);
74 addActionHotKeys (Aspect_VKey_NavSlideDown, Aspect_VKey_Down | Aspect_VKeyFlags_SHIFT);
77 //=======================================================================
78 //function : ~ViewerTest_EventManager
80 //=======================================================================
81 ViewerTest_EventManager::~ViewerTest_EventManager()
83 if (!myViewAnimation.IsNull()
84 && myViewAnimation->View() == myView)
86 myViewAnimation->Stop();
87 myViewAnimation->SetView (Handle(V3d_View)());
91 //=======================================================================
92 //function : UpdateMouseButtons
94 //=======================================================================
95 bool ViewerTest_EventManager::UpdateMouseButtons (const Graphic3d_Vec2i& thePoint,
96 Aspect_VKeyMouse theButtons,
97 Aspect_VKeyFlags theModifiers,
100 SetAllowRotation (!ViewerTest_V3dView::IsCurrentViewIn2DMode());
102 if (theButtons == Aspect_VKeyMouse_LeftButton)
104 if (myToPickPnt && (theModifiers & Aspect_VKeyFlags_CTRL) != 0)
106 Graphic3d_Vec3d anXYZ;
107 myView->Convert (thePoint.x(), thePoint.y(), anXYZ.x(), anXYZ.y(), anXYZ.z());
108 Draw::Set (myPickPntArgVec[0].ToCString(), anXYZ.x());
109 Draw::Set (myPickPntArgVec[1].ToCString(), anXYZ.y());
110 Draw::Set (myPickPntArgVec[2].ToCString(), anXYZ.z());
115 return AIS_ViewController::UpdateMouseButtons (thePoint, theButtons, theModifiers, theIsEmulated);
118 //==============================================================================
119 //function : ProcessExpose
121 //==============================================================================
122 void ViewerTest_EventManager::ProcessExpose()
124 if (!myView.IsNull())
126 myView->Invalidate();
127 FlushViewEvents (myCtx, myView, true);
131 //==============================================================================
132 //function : handleViewRedraw
134 //==============================================================================
135 void ViewerTest_EventManager::handleViewRedraw (const Handle(AIS_InteractiveContext)& theCtx,
136 const Handle(V3d_View)& theView)
138 AIS_ViewController::handleViewRedraw (theCtx, theView);
140 // On non-Windows platforms Aspect_Window::InvalidateContent() from rendering thread does not work as expected
141 // as in Tcl event loop the new message might go to sleep with new event remaining in queue.
142 // As a workaround - use dedicated background thread to ping Tcl event loop.
143 if (myToAskNextFrame)
145 ViewerTest_ContinuousRedrawer& aRedrawer = ViewerTest_ContinuousRedrawer::Instance();
146 if (!myIsTmpContRedraw
147 && (!aRedrawer.IsStarted() || aRedrawer.IsPaused()))
149 myIsTmpContRedraw = true;
151 aRedrawer.Start (theView->Window(), 60.0);
155 else if (myIsTmpContRedraw)
157 myIsTmpContRedraw = false;
159 ViewerTest_ContinuousRedrawer& aRedrawer = ViewerTest_ContinuousRedrawer::Instance();
165 //==============================================================================
166 //function : ProcessConfigure
168 //==============================================================================
169 void ViewerTest_EventManager::ProcessConfigure()
171 if (!myView.IsNull())
173 myView->MustBeResized();
174 FlushViewEvents (myCtx, myView, true);
178 // =======================================================================
179 // function : navigationKeyModifierSwitch
181 // =======================================================================
182 bool ViewerTest_EventManager::navigationKeyModifierSwitch (unsigned int theModifOld,
183 unsigned int theModifNew,
186 bool hasActions = false;
187 for (unsigned int aKeyIter = 0; aKeyIter < Aspect_VKey_ModifiersLower; ++aKeyIter)
189 if (!myKeys.IsKeyDown (aKeyIter))
194 Aspect_VKey anActionOld = Aspect_VKey_UNKNOWN, anActionNew = Aspect_VKey_UNKNOWN;
195 myNavKeyMap.Find (aKeyIter | theModifOld, anActionOld);
196 myNavKeyMap.Find (aKeyIter | theModifNew, anActionNew);
197 if (anActionOld == anActionNew)
202 if (anActionOld != Aspect_VKey_UNKNOWN)
204 myKeys.KeyUp (anActionOld, theTimeStamp);
206 if (anActionNew != Aspect_VKey_UNKNOWN)
209 myKeys.KeyDown (anActionNew, theTimeStamp);
215 //=======================================================================
218 //=======================================================================
219 void ViewerTest_EventManager::KeyDown (Aspect_VKey theKey,
223 const unsigned int aModifOld = myKeys.Modifiers();
224 AIS_ViewController::KeyDown (theKey, theTime, thePressure);
226 const unsigned int aModifNew = myKeys.Modifiers();
227 if (aModifNew != aModifOld
228 && navigationKeyModifierSwitch (aModifOld, aModifNew, theTime))
230 // modifier key just pressed
233 Aspect_VKey anAction = Aspect_VKey_UNKNOWN;
234 if (myNavKeyMap.Find (theKey | myKeys.Modifiers(), anAction)
235 && anAction != Aspect_VKey_UNKNOWN)
237 AIS_ViewController::KeyDown (anAction, theTime, thePressure);
241 //=======================================================================
244 //=======================================================================
245 void ViewerTest_EventManager::KeyUp (Aspect_VKey theKey,
248 const unsigned int aModifOld = myKeys.Modifiers();
249 AIS_ViewController::KeyUp (theKey, theTime);
251 Aspect_VKey anAction = Aspect_VKey_UNKNOWN;
252 if (myNavKeyMap.Find (theKey | myKeys.Modifiers(), anAction)
253 && anAction != Aspect_VKey_UNKNOWN)
255 AIS_ViewController::KeyUp (anAction, theTime);
256 ProcessKeyPress (anAction);
259 const unsigned int aModifNew = myKeys.Modifiers();
260 if (aModifNew != aModifOld
261 && navigationKeyModifierSwitch (aModifOld, aModifNew, theTime))
263 // modifier key released
266 ProcessKeyPress (theKey | aModifNew);
269 //==============================================================================
270 //function : ProcessKeyPress
272 //==============================================================================
273 void ViewerTest_EventManager::ProcessKeyPress (Aspect_VKey theKey)
283 case Aspect_VKey_Backspace: // AXO
285 if (!ViewerTest_V3dView::IsCurrentViewIn2DMode())
287 myView->SetProj(V3d_XposYnegZpos);
293 if (myCtx->NbSelected() > 0)
295 myCtx->FitSelected (myView);
303 case Aspect_VKey_H: // HLR
305 std::cout << "HLR\n";
306 myView->SetComputedMode (!myView->ComputedMode());
310 case Aspect_VKey_P: // Type of HLR
312 myCtx->DefaultDrawer()->SetTypeOfHLR (myCtx->DefaultDrawer()->TypeOfHLR() == Prs3d_TOH_Algo
315 if (myCtx->NbSelected() == 0)
317 AIS_ListOfInteractive aListOfShapes;
318 myCtx->DisplayedObjects (aListOfShapes);
319 for (AIS_ListIteratorOfListOfInteractive anIter (aListOfShapes); anIter.More(); anIter.Next())
321 if (Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast (anIter.Value()))
323 aShape->SetTypeOfHLR (aShape->TypeOfHLR() == Prs3d_TOH_PolyAlgo
325 : Prs3d_TOH_PolyAlgo);
326 myCtx->Redisplay (aShape, Standard_False);
332 for (myCtx->InitSelected(); myCtx->MoreSelected(); myCtx->NextSelected())
334 if (Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast (myCtx->SelectedInteractive()))
336 aShape->SetTypeOfHLR (aShape->TypeOfHLR() == Prs3d_TOH_PolyAlgo
338 : Prs3d_TOH_PolyAlgo);
339 myCtx->Redisplay (aShape, Standard_False);
343 myCtx->UpdateCurrentViewer();
346 case Aspect_VKey_S | Aspect_VKeyFlags_CTRL:
347 case Aspect_VKey_W | Aspect_VKeyFlags_CTRL:
349 Standard_Integer aDispMode = AIS_Shaded;
350 if (theKey == (Aspect_VKey_S | Aspect_VKeyFlags_CTRL))
352 aDispMode = AIS_Shaded;
353 std::cout << "setup Shaded display mode\n";
357 aDispMode = AIS_WireFrame;
358 std::cout << "setup WireFrame display mode\n";
361 if (myCtx->NbSelected() == 0)
363 myCtx->SetDisplayMode (aDispMode, true);
367 for (myCtx->InitSelected(); myCtx->MoreSelected(); myCtx->NextSelected())
369 myCtx->SetDisplayMode (myCtx->SelectedInteractive(), aDispMode, false);
371 myCtx->UpdateCurrentViewer();
375 case Aspect_VKey_U: // Unset display mode
377 std::cout << "reset display mode to defaults\n";
378 if (myCtx->NbSelected() == 0)
380 myCtx->SetDisplayMode (AIS_WireFrame, true);
384 for (myCtx->InitSelected(); myCtx->MoreSelected(); myCtx->NextSelected())
386 myCtx->UnsetDisplayMode (myCtx->SelectedInteractive(), false);
388 myCtx->UpdateCurrentViewer();
394 if (!ViewerTest_V3dView::IsCurrentViewIn2DMode())
396 myView->SetProj (V3d_TypeOfOrientation_Zup_Top);
402 if (!ViewerTest_V3dView::IsCurrentViewIn2DMode())
404 myView->SetProj (V3d_TypeOfOrientation_Zup_Bottom);
410 if (!ViewerTest_V3dView::IsCurrentViewIn2DMode())
412 myView->SetProj (V3d_TypeOfOrientation_Zup_Left);
418 if (!ViewerTest_V3dView::IsCurrentViewIn2DMode())
420 myView->SetProj (V3d_TypeOfOrientation_Zup_Right);
424 case Aspect_VKey_Comma:
426 myCtx->HilightNextDetected (myView);
429 case Aspect_VKey_Period:
431 myCtx->HilightPreviousDetected (myView);
434 case Aspect_VKey_Slash:
435 case Aspect_VKey_NumpadDivide:
437 Handle(Graphic3d_Camera) aCamera = myView->Camera();
438 if (aCamera->IsStereo())
440 aCamera->SetIOD (aCamera->GetIODType(), aCamera->IOD() - 0.01);
445 case Aspect_VKey_NumpadMultiply:
447 Handle(Graphic3d_Camera) aCamera = myView->Camera();
448 if (aCamera->IsStereo())
450 aCamera->SetIOD (aCamera->GetIODType(), aCamera->IOD() + 0.01);
455 case Aspect_VKey_Delete:
458 && myCtx->NbSelected() > 0)
460 Draw_Interprete ("verase");
464 case Aspect_VKey_Escape:
467 && ViewerTest_EventManager::ToCloseViewOnEscape())
469 Draw_Interprete (ViewerTest_EventManager::ToExitOnCloseView() ? "exit" : "vclose");
473 case Aspect_VKey_NavSpeedDecrease:
474 case Aspect_VKey_NavSpeedIncrease:
476 // handle slide speed
477 float aNewSpeed = theKey == Aspect_VKey_NavSpeedDecrease
478 ? myWalkSpeedRelative * 0.5f
479 : myWalkSpeedRelative * 2.0f;
480 if (aNewSpeed >= 0.00001f
481 && aNewSpeed <= 10.0f)
483 if (Abs (aNewSpeed - 0.1f) < 0.001f)
487 myWalkSpeedRelative = aNewSpeed;
493 if (theKey >= Aspect_VKey_0
494 && theKey <= Aspect_VKey_7)
496 const Standard_Integer aSelMode = theKey - Aspect_VKey_0;
497 bool toEnable = true;
500 AIS_ListOfInteractive aPrsList;
501 myCtx->DisplayedObjects (aPrsList);
502 for (AIS_ListOfInteractive::Iterator aPrsIter (aPrsList); aPrsIter.More() && toEnable; aPrsIter.Next())
504 TColStd_ListOfInteger aModes;
505 myCtx->ActivatedModes (aPrsIter.Value(), aModes);
506 for (TColStd_ListOfInteger::Iterator aModeIter (aModes); aModeIter.More() && toEnable; aModeIter.Next())
508 if (aModeIter.Value() == aSelMode)
515 TCollection_AsciiString aCmd = TCollection_AsciiString ("vselmode ") + aSelMode + (toEnable ? " 1" : " 0");
516 Draw_Interprete (aCmd.ToCString());