0031668: Visualization - WebGL sample doesn't work on Emscripten 1.39
[occt.git] / src / Aspect / Aspect_VKeySet.hxx
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.
34   Aspect_VKeyFlags Modifiers() const
35   {
36     Standard_Mutex::Sentry aLock (myLock);
37     return myModifiers;
38   }
39
40   //! Return timestamp of press event.
41   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.
48   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.
55   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.
62   bool IsKeyDown (Aspect_VKey theKey) const
63   {
64     Standard_Mutex::Sentry aLock (myLock);
65     return myKeys[theKey].Status == KeyStatus_Pressed;
66   }
67
68   //! Return mutex for thread-safe updates.
69   //! All operations in class implicitly locks this mutex,
70   //! so this method could be used only for batch processing of keys.
71   Standard_Mutex& Mutex() { return myLock; }
72
73 public:
74
75   //! Reset the key state into unpressed state.
76   Standard_EXPORT void Reset();
77
78   //! Press key.
79   //! @param theKey key pressed
80   //! @param theTime event timestamp
81   Standard_EXPORT void KeyDown (Aspect_VKey theKey,
82                                 double theTime,
83                                 double thePressure = 1.0);
84
85   //! Release key.
86   //! @param theKey key pressed
87   //! @param theTime event timestamp
88   Standard_EXPORT void KeyUp (Aspect_VKey theKey,
89                               double theTime);
90
91   //! Simulate key up/down events from axis value.
92   Standard_EXPORT void KeyFromAxis (Aspect_VKey theNegative,
93                                     Aspect_VKey thePositive,
94                                     double theTime,
95                                     double thePressure);
96
97   //! Return duration of the button in pressed state.
98   //! @param theKey      key to check
99   //! @param theTime     current time (for computing duration from key down time)
100   //! @param theDuration key press duration
101   //! @return TRUE if key was in pressed state
102   bool HoldDuration (Aspect_VKey theKey,
103                      double theTime,
104                      double& theDuration)
105   {
106     double aPressure = -1.0;
107     return HoldDuration (theKey, theTime, theDuration, aPressure);
108   }
109
110   //! Return duration of the button in pressed state.
111   //! @param theKey      key to check
112   //! @param theTime     current time (for computing duration from key down time)
113   //! @param theDuration key press duration
114   //! @param thePressure key pressure
115   //! @return TRUE if key was in pressed state
116   Standard_EXPORT bool HoldDuration (Aspect_VKey theKey,
117                                      double theTime,
118                                      double& theDuration,
119                                      double& thePressure);
120
121 private:
122
123   //! Key state.
124   enum KeyStatus
125   {
126     KeyStatus_Free,     //!< free status
127     KeyStatus_Pressed,  //!< key is in pressed state
128     KeyStatus_Released, //!< key has been just released (transient state before KeyStatus_Free)
129   };
130
131   //! Structure defining key state.
132   struct KeyState
133   {
134     KeyState() : TimeDown (0.0), TimeUp (0.0), Pressure (1.0), Status (KeyStatus_Free) {}
135     void Reset()
136     {
137       Status = KeyStatus_Free;
138       TimeDown = 0.0;
139       TimeUp   = 0.0;
140       Pressure = 1.0;
141     }
142
143     double    TimeDown; //!< time of key press   event
144     double    TimeUp;   //!< time of key release event
145     double    Pressure; //!< key pressure
146     KeyStatus Status;   //!< key status
147   };
148
149 private:
150
151   NCollection_Array1<KeyState> myKeys;      //!< keys state
152   mutable Standard_Mutex       myLock;      //!< mutex for thread-safe updates
153   Aspect_VKeyFlags             myModifiers; //!< active modifiers
154
155 };
156
157 #endif // _Aspect_VKeySet_HeaderFile