Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-10-28 09:15:48

0001 // Copyright (c) 2016-2019 OPEN CASCADE SAS
0002 //
0003 // This file is part of Open CASCADE Technology software library.
0004 //
0005 // This library is free software; you can redistribute it and/or modify it under
0006 // the terms of the GNU Lesser General Public License version 2.1 as published
0007 // by the Free Software Foundation, with special exception defined in the file
0008 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0009 // distribution for complete text of the license and disclaimer of any warranty.
0010 //
0011 // Alternatively, this file may be used under the terms of Open CASCADE
0012 // commercial license or contractual agreement.
0013 
0014 #ifndef _Aspect_VKeySet_HeaderFile
0015 #define _Aspect_VKeySet_HeaderFile
0016 
0017 #include <Aspect_VKey.hxx>
0018 
0019 #include <NCollection_Array1.hxx>
0020 #include <OSD_Timer.hxx>
0021 #include <Standard_Transient.hxx>
0022 
0023 #include <shared_mutex>
0024 
0025 //! Structure defining key state.
0026 class Aspect_VKeySet : public Standard_Transient
0027 {
0028   DEFINE_STANDARD_RTTIEXT(Aspect_VKeySet, Standard_Transient)
0029 public:
0030   //! Main constructor.
0031   Standard_EXPORT Aspect_VKeySet();
0032 
0033   //! Return active modifiers.
0034   Aspect_VKeyFlags Modifiers() const
0035   {
0036     std::shared_lock<std::shared_mutex> aLock(myLock);
0037     return myModifiers;
0038   }
0039 
0040   //! Return timestamp of press event.
0041   double DownTime(Aspect_VKey theKey) const
0042   {
0043     std::shared_lock<std::shared_mutex> aLock(myLock);
0044     return myKeys[theKey].TimeDown;
0045   }
0046 
0047   //! Return timestamp of release event.
0048   double TimeUp(Aspect_VKey theKey) const
0049   {
0050     std::shared_lock<std::shared_mutex> aLock(myLock);
0051     return myKeys[theKey].TimeUp;
0052   }
0053 
0054   //! Return TRUE if key is in Free state.
0055   bool IsFreeKey(Aspect_VKey theKey) const
0056   {
0057     std::shared_lock<std::shared_mutex> aLock(myLock);
0058     return myKeys[theKey].KStatus == KeyStatus_Free;
0059   }
0060 
0061   //! Return TRUE if key is in Pressed state.
0062   bool IsKeyDown(Aspect_VKey theKey) const
0063   {
0064     std::shared_lock<std::shared_mutex> aLock(myLock);
0065     return myKeys[theKey].KStatus == KeyStatus_Pressed;
0066   }
0067 
0068   //! Return mutex for thread-safe updates.
0069   //! All operations in class implicitly locks this mutex,
0070   //! so this method could be used only for batch processing of keys.
0071   std::shared_mutex& Mutex() { return myLock; }
0072 
0073 public:
0074   //! Reset the key state into unpressed state.
0075   Standard_EXPORT void Reset();
0076 
0077   //! Press key.
0078   //! @param theKey key pressed
0079   //! @param theTime event timestamp
0080   Standard_EXPORT void KeyDown(Aspect_VKey theKey, double theTime, double thePressure = 1.0);
0081 
0082   //! Release key.
0083   //! @param theKey key pressed
0084   //! @param theTime event timestamp
0085   Standard_EXPORT void KeyUp(Aspect_VKey theKey, double theTime);
0086 
0087   //! Simulate key up/down events from axis value.
0088   Standard_EXPORT void KeyFromAxis(Aspect_VKey theNegative,
0089                                    Aspect_VKey thePositive,
0090                                    double      theTime,
0091                                    double      thePressure);
0092 
0093   //! Return duration of the button in pressed state.
0094   //! @param theKey      key to check
0095   //! @param theTime     current time (for computing duration from key down time)
0096   //! @param theDuration key press duration
0097   //! @return TRUE if key was in pressed state
0098   bool HoldDuration(Aspect_VKey theKey, double theTime, double& theDuration)
0099   {
0100     double aPressure = -1.0;
0101     return HoldDuration(theKey, theTime, theDuration, aPressure);
0102   }
0103 
0104   //! Return duration of the button in pressed state.
0105   //! @param theKey      key to check
0106   //! @param theTime     current time (for computing duration from key down time)
0107   //! @param theDuration key press duration
0108   //! @param thePressure key pressure
0109   //! @return TRUE if key was in pressed state
0110   Standard_EXPORT bool HoldDuration(Aspect_VKey theKey,
0111                                     double      theTime,
0112                                     double&     theDuration,
0113                                     double&     thePressure);
0114 
0115 private:
0116   //! Press key without locking (assumes lock is already held).
0117   void KeyDown_Unlocked(Aspect_VKey theKey, double theTime, double thePressure);
0118 
0119   //! Release key without locking (assumes lock is already held).
0120   void KeyUp_Unlocked(Aspect_VKey theKey, double theTime);
0121 
0122 private:
0123   //! Key state.
0124   enum KeyStatus
0125   {
0126     KeyStatus_Free,     //!< free status
0127     KeyStatus_Pressed,  //!< key is in pressed state
0128     KeyStatus_Released, //!< key has been just released (transient state before KeyStatus_Free)
0129   };
0130 
0131   //! Structure defining key state.
0132   struct KeyState
0133   {
0134     KeyState()
0135         : TimeDown(0.0),
0136           TimeUp(0.0),
0137           Pressure(1.0),
0138           KStatus(KeyStatus_Free)
0139     {
0140     }
0141 
0142     void Reset()
0143     {
0144       KStatus  = KeyStatus_Free;
0145       TimeDown = 0.0;
0146       TimeUp   = 0.0;
0147       Pressure = 1.0;
0148     }
0149 
0150     double    TimeDown; //!< time of key press   event
0151     double    TimeUp;   //!< time of key release event
0152     double    Pressure; //!< key pressure
0153     KeyStatus KStatus;  //!< key status
0154   };
0155 
0156 private:
0157   NCollection_Array1<KeyState> myKeys;      //!< keys state
0158   mutable std::shared_mutex    myLock;      //!< mutex for thread-safe updates
0159   Aspect_VKeyFlags             myModifiers; //!< active modifiers
0160 };
0161 
0162 #endif // _Aspect_VKeySet_HeaderFile