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 : update3dMouseTranslation
81 // =======================================================================
82 bool Aspect_WindowInputListener::update3dMouseTranslation (const WNT_HIDSpaceMouse& theEvent)
84 if (!theEvent.IsTranslation())
90 const double aTimeStamp = EventTime();
91 const Graphic3d_Vec3d aTrans = theEvent.Translation (isIdle, my3dMouseIsQuadric) * my3dMouseAccelTrans;
92 myKeys.KeyFromAxis (Aspect_VKey_NavSlideLeft, Aspect_VKey_NavSlideRight, aTimeStamp, aTrans.x());
93 myKeys.KeyFromAxis (Aspect_VKey_NavForward, Aspect_VKey_NavBackward, aTimeStamp, aTrans.y());
94 myKeys.KeyFromAxis (Aspect_VKey_NavSlideUp, Aspect_VKey_NavSlideDown, aTimeStamp, aTrans.z());
98 // =======================================================================
99 // function : update3dMouseRotation
101 // =======================================================================
102 bool Aspect_WindowInputListener::update3dMouseRotation (const WNT_HIDSpaceMouse& theEvent)
104 if (!theEvent.IsRotation())
109 bool isIdle = true, toUpdate = false;
110 const double aTimeStamp = EventTime();
111 const Graphic3d_Vec3d aRot3 = theEvent.Rotation (isIdle, my3dMouseIsQuadric) * my3dMouseAccelRotate;
112 if (!my3dMouseNoRotate.x())
114 KeyFromAxis (Aspect_VKey_NavLookUp, Aspect_VKey_NavLookDown, aTimeStamp, !my3dMouseToReverse.x() ? aRot3.x() : -aRot3.x());
117 if (!my3dMouseNoRotate.y())
119 KeyFromAxis (Aspect_VKey_NavRollCW, Aspect_VKey_NavRollCCW, aTimeStamp, !my3dMouseToReverse.y() ? aRot3.y() : -aRot3.y());
122 if (!my3dMouseNoRotate.z())
124 KeyFromAxis (Aspect_VKey_NavLookLeft, Aspect_VKey_NavLookRight, aTimeStamp, !my3dMouseToReverse.z() ? aRot3.z() : -aRot3.z());
130 // =======================================================================
131 // function : update3dMouseKeys
133 // =======================================================================
134 bool Aspect_WindowInputListener::update3dMouseKeys (const WNT_HIDSpaceMouse& theEvent)
136 bool toUpdate = false;
137 const double aTimeStamp = EventTime();
138 if (theEvent.IsKeyState())
140 const uint32_t aKeyState = theEvent.KeyState();
141 for (unsigned short aKeyBit = 0; aKeyBit < 32; ++aKeyBit)
143 const bool isPressed = (aKeyState & (1 << aKeyBit)) != 0;
144 const bool isReleased = my3dMouseButtonState[aKeyBit] && !isPressed;
145 //const bool isRepeated = my3dMouseButtonState[aKeyBit] && isPressed;
146 my3dMouseButtonState[aKeyBit] = isPressed;
147 if (!isReleased && !isPressed)
152 const Aspect_VKey aVKey = theEvent.HidToSpaceKey (aKeyBit);
153 if (aVKey != Aspect_VKey_UNKNOWN)
158 KeyDown (aVKey, aTimeStamp);
162 KeyUp (aVKey, aTimeStamp);