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();
55 //=======================================================================
56 //function : ~ViewerTest_EventManager
58 //=======================================================================
59 ViewerTest_EventManager::~ViewerTest_EventManager()
61 if (!myViewAnimation.IsNull()
62 && myViewAnimation->View() == myView)
64 myViewAnimation->Stop();
65 myViewAnimation->SetView (Handle(V3d_View)());
69 //=======================================================================
70 //function : UpdateMouseButtons
72 //=======================================================================
73 bool ViewerTest_EventManager::UpdateMouseButtons (const Graphic3d_Vec2i& thePoint,
74 Aspect_VKeyMouse theButtons,
75 Aspect_VKeyFlags theModifiers,
78 SetAllowRotation (!ViewerTest_V3dView::IsCurrentViewIn2DMode());
80 if (theButtons == Aspect_VKeyMouse_LeftButton)
82 if (myToPickPnt && (theModifiers & Aspect_VKeyFlags_CTRL) != 0)
84 Graphic3d_Vec3d anXYZ;
85 myView->Convert (thePoint.x(), thePoint.y(), anXYZ.x(), anXYZ.y(), anXYZ.z());
86 Draw::Set (myPickPntArgVec[0].ToCString(), anXYZ.x());
87 Draw::Set (myPickPntArgVec[1].ToCString(), anXYZ.y());
88 Draw::Set (myPickPntArgVec[2].ToCString(), anXYZ.z());
93 return AIS_ViewController::UpdateMouseButtons (thePoint, theButtons, theModifiers, theIsEmulated);
96 //==============================================================================
97 //function : ProcessExpose
99 //==============================================================================
100 void ViewerTest_EventManager::ProcessExpose()
102 if (!myView.IsNull())
104 myView->Invalidate();
105 FlushViewEvents (myCtx, myView, true);
109 //==============================================================================
110 //function : handleViewRedraw
112 //==============================================================================
113 void ViewerTest_EventManager::handleViewRedraw (const Handle(AIS_InteractiveContext)& theCtx,
114 const Handle(V3d_View)& theView)
116 AIS_ViewController::handleViewRedraw (theCtx, theView);
118 // On non-Windows platforms Aspect_Window::InvalidateContent() from rendering thread does not work as expected
119 // as in Tcl event loop the new message might go to sleep with new event remaining in queue.
120 // As a workaround - use dedicated background thread to ping Tcl event loop.
121 if (myToAskNextFrame)
123 ViewerTest_ContinuousRedrawer& aRedrawer = ViewerTest_ContinuousRedrawer::Instance();
124 if (!myIsTmpContRedraw
125 && (!aRedrawer.IsStarted() || aRedrawer.IsPaused()))
127 myIsTmpContRedraw = true;
129 aRedrawer.Start (theView->Window(), 60.0);
133 else if (myIsTmpContRedraw)
135 myIsTmpContRedraw = false;
137 ViewerTest_ContinuousRedrawer& aRedrawer = ViewerTest_ContinuousRedrawer::Instance();
143 //==============================================================================
144 //function : ProcessConfigure
146 //==============================================================================
147 void ViewerTest_EventManager::ProcessConfigure()
149 if (!myView.IsNull())
151 myView->MustBeResized();
152 FlushViewEvents (myCtx, myView, true);
156 //=======================================================================
159 //=======================================================================
160 void ViewerTest_EventManager::KeyUp (Aspect_VKey theKey,
163 AIS_ViewController::KeyUp (theKey, theTime);
164 ProcessKeyPress (theKey);
167 //==============================================================================
168 //function : ProcessKeyPress
170 //==============================================================================
171 void ViewerTest_EventManager::ProcessKeyPress (Aspect_VKey theKey)
181 case Aspect_VKey_A: // AXO
183 if (!ViewerTest_V3dView::IsCurrentViewIn2DMode())
185 myView->SetProj(V3d_XposYnegZpos);
189 case Aspect_VKey_D: // Reset
191 if (!ViewerTest_V3dView::IsCurrentViewIn2DMode())
199 if (myCtx->NbSelected() > 0)
201 myCtx->FitSelected (myView);
209 case Aspect_VKey_H: // HLR
211 std::cout << "HLR\n";
212 myView->SetComputedMode (!myView->ComputedMode());
216 case Aspect_VKey_P: // Type of HLR
218 myCtx->DefaultDrawer()->SetTypeOfHLR (myCtx->DefaultDrawer()->TypeOfHLR() == Prs3d_TOH_Algo
221 if (myCtx->NbSelected() == 0)
223 AIS_ListOfInteractive aListOfShapes;
224 myCtx->DisplayedObjects (aListOfShapes);
225 for (AIS_ListIteratorOfListOfInteractive anIter (aListOfShapes); anIter.More(); anIter.Next())
227 if (Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast (anIter.Value()))
229 aShape->SetTypeOfHLR (aShape->TypeOfHLR() == Prs3d_TOH_PolyAlgo
231 : Prs3d_TOH_PolyAlgo);
232 myCtx->Redisplay (aShape, Standard_False);
238 for (myCtx->InitSelected(); myCtx->MoreSelected(); myCtx->NextSelected())
240 if (Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast (myCtx->SelectedInteractive()))
242 aShape->SetTypeOfHLR (aShape->TypeOfHLR() == Prs3d_TOH_PolyAlgo
244 : Prs3d_TOH_PolyAlgo);
245 myCtx->Redisplay (aShape, Standard_False);
249 myCtx->UpdateCurrentViewer();
255 Standard_Integer aDispMode = AIS_Shaded;
256 if (theKey == Aspect_VKey_S)
258 aDispMode = AIS_Shaded;
259 std::cout << "setup Shaded display mode\n";
263 aDispMode = AIS_WireFrame;
264 std::cout << "setup WireFrame display mode\n";
267 if (myCtx->NbSelected() == 0)
269 myCtx->SetDisplayMode (aDispMode, true);
273 for (myCtx->InitSelected(); myCtx->MoreSelected(); myCtx->NextSelected())
275 myCtx->SetDisplayMode (myCtx->SelectedInteractive(), aDispMode, false);
277 myCtx->UpdateCurrentViewer();
281 case Aspect_VKey_U: // Unset display mode
283 std::cout << "reset display mode to defaults\n";
284 if (myCtx->NbSelected() == 0)
286 myCtx->SetDisplayMode (AIS_WireFrame, true);
290 for (myCtx->InitSelected(); myCtx->MoreSelected(); myCtx->NextSelected())
292 myCtx->UnsetDisplayMode (myCtx->SelectedInteractive(), false);
294 myCtx->UpdateCurrentViewer();
300 if (!ViewerTest_V3dView::IsCurrentViewIn2DMode())
302 myView->SetProj (V3d_TypeOfOrientation_Zup_Top);
308 if (!ViewerTest_V3dView::IsCurrentViewIn2DMode())
310 myView->SetProj (V3d_TypeOfOrientation_Zup_Bottom);
316 if (!ViewerTest_V3dView::IsCurrentViewIn2DMode())
318 myView->SetProj (V3d_TypeOfOrientation_Zup_Left);
324 if (!ViewerTest_V3dView::IsCurrentViewIn2DMode())
326 myView->SetProj (V3d_TypeOfOrientation_Zup_Right);
330 case Aspect_VKey_Comma:
332 myCtx->HilightNextDetected (myView);
335 case Aspect_VKey_Period:
337 myCtx->HilightPreviousDetected (myView);
340 case Aspect_VKey_Slash:
341 case Aspect_VKey_NumpadDivide:
343 Handle(Graphic3d_Camera) aCamera = myView->Camera();
344 if (aCamera->IsStereo())
346 aCamera->SetIOD (aCamera->GetIODType(), aCamera->IOD() - 0.01);
351 case Aspect_VKey_NumpadMultiply:
353 Handle(Graphic3d_Camera) aCamera = myView->Camera();
354 if (aCamera->IsStereo())
356 aCamera->SetIOD (aCamera->GetIODType(), aCamera->IOD() + 0.01);
361 case Aspect_VKey_Delete:
364 && myCtx->NbSelected() > 0)
366 Draw_Interprete ("verase");
370 case Aspect_VKey_Escape:
373 && ViewerTest_EventManager::ToCloseViewOnEscape())
375 Draw_Interprete (ViewerTest_EventManager::ToExitOnCloseView() ? "exit" : "vclose");
380 if (theKey >= Aspect_VKey_0
381 && theKey <= Aspect_VKey_7)
383 const Standard_Integer aSelMode = theKey - Aspect_VKey_0;
384 bool toEnable = true;
387 AIS_ListOfInteractive aPrsList;
388 myCtx->DisplayedObjects (aPrsList);
389 for (AIS_ListOfInteractive::Iterator aPrsIter (aPrsList); aPrsIter.More() && toEnable; aPrsIter.Next())
391 TColStd_ListOfInteger aModes;
392 myCtx->ActivatedModes (aPrsIter.Value(), aModes);
393 for (TColStd_ListOfInteger::Iterator aModeIter (aModes); aModeIter.More() && toEnable; aModeIter.Next())
395 if (aModeIter.Value() == aSelMode)
402 TCollection_AsciiString aCmd = TCollection_AsciiString ("vselmode ") + aSelMode + (toEnable ? " 1" : " 0");
403 Draw_Interprete (aCmd.ToCString());