1 // Copyright (c) 2021 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <Aspect_WindowInputListener.hxx>
16 #include <WNT_HIDSpaceMouse.hxx>
18 // =======================================================================
19 // function : Aspect_WindowInputListener
21 // =======================================================================
22 Aspect_WindowInputListener::Aspect_WindowInputListener()
23 : myMousePressed (Aspect_VKeyMouse_NONE),
24 myMouseModifiers (Aspect_VKeyFlags_NONE),
26 my3dMouseNoRotate (false, false, false),
27 my3dMouseToReverse (true, false, false),
28 my3dMouseAccelTrans (2.0f),
29 my3dMouseAccelRotate (4.0f),
30 my3dMouseIsQuadric (true)
32 memset(my3dMouseButtonState, 0, sizeof(my3dMouseButtonState));
36 // =======================================================================
37 // function : ~Aspect_WindowInputListener
39 // =======================================================================
40 Aspect_WindowInputListener::~Aspect_WindowInputListener()
45 // =======================================================================
48 // =======================================================================
49 void Aspect_WindowInputListener::KeyDown (Aspect_VKey theKey,
53 myKeys.KeyDown (theKey, theTime, thePressure);
56 // =======================================================================
59 // =======================================================================
60 void Aspect_WindowInputListener::KeyUp (Aspect_VKey theKey,
63 myKeys.KeyUp (theKey, theTime);
66 // =======================================================================
67 // function : KeyFromAxis
69 // =======================================================================
70 void Aspect_WindowInputListener::KeyFromAxis (Aspect_VKey theNegative,
71 Aspect_VKey thePositive,
75 myKeys.KeyFromAxis (theNegative, thePositive, theTime, thePressure);
78 // =======================================================================
79 // function : AddTouchPoint
81 // =======================================================================
82 void Aspect_WindowInputListener::AddTouchPoint (Standard_Size theId,
83 const Graphic3d_Vec2d& thePnt,
84 Standard_Boolean theClearBefore)
88 RemoveTouchPoint ((Standard_Size )-1);
91 myTouchPoints.Add (theId, Aspect_Touch (thePnt, false));
94 // =======================================================================
95 // function : RemoveTouchPoint
97 // =======================================================================
98 bool Aspect_WindowInputListener::RemoveTouchPoint (Standard_Size theId,
99 Standard_Boolean theClearSelectPnts)
101 (void )theClearSelectPnts;
102 if (theId == (Standard_Size )-1)
104 myTouchPoints.Clear (false);
108 const Standard_Integer anOldExtent = myTouchPoints.Extent();
109 myTouchPoints.RemoveKey (theId);
110 if (myTouchPoints.Extent() == anOldExtent)
116 if (myTouchPoints.Extent() == 1)
118 // avoid incorrect transition from pinch to one finger
119 Aspect_Touch& aFirstTouch = myTouchPoints.ChangeFromIndex (1);
120 aFirstTouch.To = aFirstTouch.From;
125 // =======================================================================
126 // function : UpdateTouchPoint
128 // =======================================================================
129 void Aspect_WindowInputListener::UpdateTouchPoint (Standard_Size theId,
130 const Graphic3d_Vec2d& thePnt)
132 if (Aspect_Touch* aTouch = myTouchPoints.ChangeSeek (theId))
138 AddTouchPoint (theId, thePnt);
142 // =======================================================================
143 // function : update3dMouseTranslation
145 // =======================================================================
146 bool Aspect_WindowInputListener::update3dMouseTranslation (const WNT_HIDSpaceMouse& theEvent)
148 if (!theEvent.IsTranslation())
154 const double aTimeStamp = EventTime();
155 const Graphic3d_Vec3d aTrans = theEvent.Translation (isIdle, my3dMouseIsQuadric) * my3dMouseAccelTrans;
156 myKeys.KeyFromAxis (Aspect_VKey_NavSlideLeft, Aspect_VKey_NavSlideRight, aTimeStamp, aTrans.x());
157 myKeys.KeyFromAxis (Aspect_VKey_NavForward, Aspect_VKey_NavBackward, aTimeStamp, aTrans.y());
158 myKeys.KeyFromAxis (Aspect_VKey_NavSlideUp, Aspect_VKey_NavSlideDown, aTimeStamp, aTrans.z());
162 // =======================================================================
163 // function : update3dMouseRotation
165 // =======================================================================
166 bool Aspect_WindowInputListener::update3dMouseRotation (const WNT_HIDSpaceMouse& theEvent)
168 if (!theEvent.IsRotation())
173 bool isIdle = true, toUpdate = false;
174 const double aTimeStamp = EventTime();
175 const Graphic3d_Vec3d aRot3 = theEvent.Rotation (isIdle, my3dMouseIsQuadric) * my3dMouseAccelRotate;
176 if (!my3dMouseNoRotate.x())
178 KeyFromAxis (Aspect_VKey_NavLookUp, Aspect_VKey_NavLookDown, aTimeStamp, !my3dMouseToReverse.x() ? aRot3.x() : -aRot3.x());
181 if (!my3dMouseNoRotate.y())
183 KeyFromAxis (Aspect_VKey_NavRollCW, Aspect_VKey_NavRollCCW, aTimeStamp, !my3dMouseToReverse.y() ? aRot3.y() : -aRot3.y());
186 if (!my3dMouseNoRotate.z())
188 KeyFromAxis (Aspect_VKey_NavLookLeft, Aspect_VKey_NavLookRight, aTimeStamp, !my3dMouseToReverse.z() ? aRot3.z() : -aRot3.z());
194 // =======================================================================
195 // function : update3dMouseKeys
197 // =======================================================================
198 bool Aspect_WindowInputListener::update3dMouseKeys (const WNT_HIDSpaceMouse& theEvent)
200 bool toUpdate = false;
201 const double aTimeStamp = EventTime();
202 if (theEvent.IsKeyState())
204 const uint32_t aKeyState = theEvent.KeyState();
205 for (unsigned short aKeyBit = 0; aKeyBit < 32; ++aKeyBit)
207 const bool isPressed = (aKeyState & (1 << aKeyBit)) != 0;
208 const bool isReleased = my3dMouseButtonState[aKeyBit] && !isPressed;
209 //const bool isRepeated = my3dMouseButtonState[aKeyBit] && isPressed;
210 my3dMouseButtonState[aKeyBit] = isPressed;
211 if (!isReleased && !isPressed)
216 const Aspect_VKey aVKey = theEvent.HidToSpaceKey (aKeyBit);
217 if (aVKey != Aspect_VKey_UNKNOWN)
222 KeyDown (aVKey, aTimeStamp);
226 KeyUp (aVKey, aTimeStamp);