661a078c18637a3d575730fff0d5a1e864bd6943
[occt.git] / src / ViewerTest / ViewerTest_EventManager.cxx
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
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <ViewerTest_EventManager.hxx>
18
19 #include <AIS_InteractiveContext.hxx>
20 #include <Aspect_Grid.hxx>
21 #include <Standard_Type.hxx>
22 #include <V3d_View.hxx>
23
24 IMPLEMENT_STANDARD_RTTIEXT(ViewerTest_EventManager,Standard_Transient)
25
26 //=======================================================================
27 //function : ViewerTest_EventManager
28 //purpose  :
29 //=======================================================================
30 ViewerTest_EventManager::ViewerTest_EventManager (const Handle(V3d_View)&               theView,
31                                                   const Handle(AIS_InteractiveContext)& theCtx)
32 : myCtx  (theCtx),
33   myView (theView),
34   myX    (-1),
35   myY    (-1)
36 {}
37
38 //=======================================================================
39 //function : MoveTo
40 //purpose  :
41 //=======================================================================
42
43 void ViewerTest_EventManager::MoveTo (const Standard_Integer theXPix,
44                                       const Standard_Integer theYPix)
45 {
46   Standard_Real aPnt3d[3] = {0.0, 0.0, 0.0};
47   if (!myCtx.IsNull()
48    && !myView.IsNull())
49   {
50     const Standard_Boolean toEchoGrid = myView->Viewer()->Grid()->IsActive()
51                                      && myView->Viewer()->GridEcho();
52     switch (myCtx->MoveTo (theXPix, theYPix, myView, !toEchoGrid))
53     {
54       case AIS_SOD_Nothing:
55       {
56         if (toEchoGrid)
57         {
58           myView->ConvertToGrid (theXPix, theYPix, aPnt3d[0], aPnt3d[1], aPnt3d[2]);
59           myView->Viewer()->ShowGridEcho (myView, Graphic3d_Vertex (aPnt3d[0], aPnt3d[1], aPnt3d[2]));
60           myView->RedrawImmediate();
61         }
62         break;
63       }
64       default:
65       {
66         if (toEchoGrid)
67         {
68           myView->Viewer()->HideGridEcho (myView);
69           myView->RedrawImmediate();
70         }
71         break;
72       }
73     }
74   }
75
76   myX = theXPix;
77   myY = theYPix;
78 }
79
80 //=======================================================================
81 //function : Select
82 //purpose  :
83 //=======================================================================
84
85 void ViewerTest_EventManager::Select (const Standard_Integer theXPressed,
86                                       const Standard_Integer theYPressed,
87                                       const Standard_Integer theXMotion,
88                                       const Standard_Integer theYMotion,
89                                       const Standard_Boolean theIsAutoAllowOverlap)
90 {
91   if (myView.IsNull()
92    || myCtx.IsNull()
93    || Abs (theXPressed - theXMotion) < 2
94    || Abs (theYPressed - theYMotion) < 2)
95   {
96     return;
97   }
98
99   if (theIsAutoAllowOverlap)
100   {
101     const Standard_Boolean toAllowOverlap = theYPressed != Min (theYPressed, theYMotion);
102     myCtx->MainSelector()->AllowOverlapDetection (toAllowOverlap);
103   }
104   myCtx->Select (Min (theXPressed, theXMotion),
105                  Min (theYPressed, theYMotion),
106                  Max (theXPressed, theXMotion),
107                  Max (theYPressed, theYMotion),
108                  myView,
109                  Standard_False);
110
111   // to restore default state of viewer selector
112   if (theIsAutoAllowOverlap)
113   {
114     myCtx->MainSelector()->AllowOverlapDetection (Standard_False);
115   }
116   myView->Redraw();
117 }
118
119 //=======================================================================
120 //function : ShiftSelect
121 //purpose  :
122 //=======================================================================
123
124 void ViewerTest_EventManager::ShiftSelect (const Standard_Integer theXPressed,
125                                            const Standard_Integer theYPressed,
126                                            const Standard_Integer theXMotion,
127                                            const Standard_Integer theYMotion,
128                                            const Standard_Boolean theIsAutoAllowOverlap)
129 {
130   if (myView.IsNull()
131    || myCtx.IsNull()
132    || Abs (theXPressed - theXMotion) < 2
133    || Abs (theYPressed - theYMotion) < 2)
134   {
135     return;
136   }
137
138   if (theIsAutoAllowOverlap)
139   {
140     const Standard_Boolean toAllowOverlap = theYPressed != Min (theYPressed, theYMotion);
141     myCtx->MainSelector()->AllowOverlapDetection (toAllowOverlap);
142   }
143   myCtx->ShiftSelect (Min (theXPressed, theXMotion),
144                       Min (theYPressed, theYMotion),
145                       Max (theXPressed, theXMotion),
146                       Max (theYPressed, theYMotion),
147                       myView,
148                       Standard_False);
149
150   // to restore default state of viewer selector
151   if (theIsAutoAllowOverlap)
152   {
153     myCtx->MainSelector()->AllowOverlapDetection (Standard_False);
154   }
155   myView->Redraw();
156 }
157
158 //=======================================================================
159 //function : Select
160 //purpose  :
161 //=======================================================================
162
163 void ViewerTest_EventManager::Select()
164 {
165   if (myView.IsNull()
166    || myCtx.IsNull())
167   {
168     return;
169   }
170
171   myCtx->Select (Standard_False);
172   myView->Redraw();
173 }
174
175 //=======================================================================
176 //function : ShiftSelect
177 //purpose  :
178 //=======================================================================
179
180 void ViewerTest_EventManager::ShiftSelect()
181 {
182   if (myView.IsNull()
183    || myCtx.IsNull())
184   {
185     return;
186   }
187
188   myCtx->ShiftSelect (Standard_False);
189   myView->Redraw();
190 }
191
192 //=======================================================================
193 //function : Select
194 //purpose  : Selection with polyline
195 //=======================================================================
196
197 void ViewerTest_EventManager::Select (const TColgp_Array1OfPnt2d& thePolyline)
198 {
199   if (myView.IsNull()
200    || myCtx.IsNull())
201   {
202     return;
203   }
204
205   myCtx->Select (thePolyline, myView, Standard_False);
206   myView->Redraw();
207 }
208
209 //=======================================================================
210 //function : ShiftSelect
211 //purpose  : Selection with polyline without erasing of current selection
212 //=======================================================================
213
214 void ViewerTest_EventManager::ShiftSelect (const TColgp_Array1OfPnt2d& thePolyline)
215 {
216   if (myView.IsNull()
217    || myCtx.IsNull())
218   {
219     return;
220   }
221
222   myCtx->ShiftSelect (thePolyline, myView, Standard_False);
223   myView->Redraw();
224 }
225
226 //=======================================================================
227 //function : GetCurrentPosition
228 //purpose  :
229 //=======================================================================
230 void ViewerTest_EventManager::GetCurrentPosition (Standard_Integer& theXPix, Standard_Integer& theYPix) const
231 {
232   theXPix = myX;
233   theYPix = myY;
234 }