1 // Copyright (c) 2016-2019 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
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.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13 
14 #ifndef _Aspect_VKeySet_HeaderFile
15 #define _Aspect_VKeySet_HeaderFile
16 
17 #include <Aspect_VKey.hxx>
18 
19 #include <NCollection_Array1.hxx>
20 #include <OSD_Timer.hxx>
21 #include <Standard_Mutex.hxx>
22 #include <Standard_Transient.hxx>
23 
24 //! Structure defining key state.
25 class Aspect_VKeySet : public Standard_Transient
26 {
27   DEFINE_STANDARD_RTTIEXT(Aspect_VKeySet, Standard_Transient)
28 public:
29 
30   //! Main constructor.
31   Standard_EXPORT Aspect_VKeySet();
32 
33   //! Return active modifiers.
Modifiers() const34   Aspect_VKeyFlags Modifiers() const
35   {
36     Standard_Mutex::Sentry aLock (myLock);
37     return myModifiers;
38   }
39 
40   //! Return timestamp of press event.
DownTime(Aspect_VKey theKey) const41   double DownTime (Aspect_VKey theKey) const
42   {
43     Standard_Mutex::Sentry aLock (myLock);
44     return myKeys[theKey].TimeDown;
45   }
46 
47   //! Return timestamp of release event.
TimeUp(Aspect_VKey theKey) const48   double TimeUp (Aspect_VKey theKey) const
49   {
50     Standard_Mutex::Sentry aLock (myLock);
51     return myKeys[theKey].TimeUp;
52   }
53 
54   //! Return TRUE if key is in Free state.
IsFreeKey(Aspect_VKey theKey) const55   bool IsFreeKey (Aspect_VKey theKey) const
56   {
57     Standard_Mutex::Sentry aLock (myLock);
58     return myKeys[theKey].Status == KeyStatus_Free;
59   }
60 
61   //! Return TRUE if key is in Pressed state.
IsKeyDown(Aspect_VKey theKey) const62   bool IsKeyDown (Aspect_VKey theKey) const
63   {
64     Standard_Mutex::Sentry aLock (myLock);
65     return myKeys[theKey].Status == KeyStatus_Pressed;
66   }
67 
68 public:
69 
70   //! Reset the key state into unpressed state.
71   Standard_EXPORT void Reset();
72 
73   //! Press key.
74   //! @param theKey key pressed
75   //! @param theTime event timestamp
76   Standard_EXPORT void KeyDown (Aspect_VKey theKey,
77                                 double theTime,
78                                 double thePressure = 1.0);
79 
80   //! Release key.
81   //! @param theKey key pressed
82   //! @param theTime event timestamp
83   Standard_EXPORT void KeyUp (Aspect_VKey theKey,
84                               double theTime);
85 
86   //! Simulate key up/down events from axis value.
87   Standard_EXPORT void KeyFromAxis (Aspect_VKey theNegative,
88                                     Aspect_VKey thePositive,
89                                     double theTime,
90                                     double thePressure);
91 
92   //! Return duration of the button in pressed state.
93   //! @param theKey      key to check
94   //! @param theTime     current time (for computing duration from key down time)
95   //! @param theDuration key press duration
96   //! @return TRUE if key was in pressed state
HoldDuration(Aspect_VKey theKey,double theTime,double & theDuration)97   bool HoldDuration (Aspect_VKey theKey,
98                      double theTime,
99                      double& theDuration)
100   {
101     double aPressure = -1.0;
102     return HoldDuration (theKey, theTime, theDuration, aPressure);
103   }
104 
105   //! Return duration of the button in pressed state.
106   //! @param theKey      key to check
107   //! @param theTime     current time (for computing duration from key down time)
108   //! @param theDuration key press duration
109   //! @param thePressure key pressure
110   //! @return TRUE if key was in pressed state
111   Standard_EXPORT bool HoldDuration (Aspect_VKey theKey,
112                                      double theTime,
113                                      double& theDuration,
114                                      double& thePressure);
115 
116 private:
117 
118   //! Key state.
119   enum KeyStatus
120   {
121     KeyStatus_Free,     //!< free status
122     KeyStatus_Pressed,  //!< key is in pressed state
123     KeyStatus_Released, //!< key has been just released (transient state before KeyStatus_Free)
124   };
125 
126   //! Structure defining key state.
127   struct KeyState
128   {
KeyStateAspect_VKeySet::KeyState129     KeyState() : TimeDown (0.0), TimeUp (0.0), Pressure (1.0), Status (KeyStatus_Free) {}
ResetAspect_VKeySet::KeyState130     void Reset()
131     {
132       Status = KeyStatus_Free;
133       TimeDown = 0.0;
134       TimeUp   = 0.0;
135       Pressure = 1.0;
136     }
137 
138     double    TimeDown; //!< time of key press   event
139     double    TimeUp;   //!< time of key release event
140     double    Pressure; //!< key pressure
141     KeyStatus Status;   //!< key status
142   };
143 
144 private:
145 
146   NCollection_Array1<KeyState> myKeys;      //!< keys state
147   mutable Standard_Mutex       myLock;      //!< mutex for thread-safe updates
148   Aspect_VKeyFlags             myModifiers; //!< active modifiers
149 
150 };
151 
152 #endif // _Aspect_VKeySet_HeaderFile
153