Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-13 09:15:36

0001 // Copyright (c) 2021 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_WindowInputListener_HeaderFile
0015 #define _Aspect_WindowInputListener_HeaderFile
0016 
0017 #include <Aspect_VKeySet.hxx>
0018 #include <Aspect_Touch.hxx>
0019 #include <NCollection_IndexedDataMap.hxx>
0020 #include <NCollection_Vec2.hxx>
0021 #include <Standard_TypeDef.hxx>
0022 #include <NCollection_Vec3.hxx>
0023 #include <NCollection_Vec4.hxx>
0024 #include <NCollection_Mat4.hxx>
0025 
0026 struct Aspect_ScrollDelta;
0027 class WNT_HIDSpaceMouse;
0028 
0029 //! Defines a listener for window input events.
0030 class Aspect_WindowInputListener
0031 {
0032 public:
0033   //! Destructor.
0034   Standard_EXPORT virtual ~Aspect_WindowInputListener();
0035 
0036   //! Return event time (e.g. current time).
0037   double EventTime() const { return myEventTimer.ElapsedTime(); }
0038 
0039   //! Handle expose event (window content has been invalidation and should be redrawn).
0040   virtual void ProcessExpose() = 0;
0041 
0042   //! Handle window resize event.
0043   virtual void ProcessConfigure(bool theIsResized) = 0;
0044 
0045   //! Handle window input event immediately (flush input buffer or ignore).
0046   virtual void ProcessInput() = 0;
0047 
0048   //! Handle focus event.
0049   virtual void ProcessFocus(bool theIsActivated) = 0;
0050 
0051   //! Handle window close event.
0052   virtual void ProcessClose() = 0;
0053 
0054 public: //! @name keyboard input
0055   //! Return keyboard state.
0056   const Aspect_VKeySet& Keys() const { return myKeys; }
0057 
0058   //! Return keyboard state.
0059   Aspect_VKeySet& ChangeKeys() { return myKeys; }
0060 
0061   //! Press key.
0062   //! Default implementation updates internal cache.
0063   //! @param theKey key pressed
0064   //! @param theTime event timestamp
0065   Standard_EXPORT virtual void KeyDown(Aspect_VKey theKey,
0066                                        double      theTime,
0067                                        double      thePressure = 1.0) = 0;
0068 
0069   //! Release key.
0070   //! Default implementation updates internal cache.
0071   //! @param theKey key pressed
0072   //! @param theTime event timestamp
0073   Standard_EXPORT virtual void KeyUp(Aspect_VKey theKey, double theTime) = 0;
0074 
0075   //! Simulate key up/down events from axis value.
0076   //! Default implementation updates internal cache.
0077   Standard_EXPORT virtual void KeyFromAxis(Aspect_VKey theNegative,
0078                                            Aspect_VKey thePositive,
0079                                            double      theTime,
0080                                            double      thePressure) = 0;
0081 
0082 public: //! @name mouse input
0083   //! Update mouse scroll event.
0084   //! This method is expected to be called from UI thread.
0085   //! @param theDelta mouse cursor position and delta
0086   //! @return TRUE if new event has been created or FALSE if existing one has been updated
0087   virtual bool UpdateMouseScroll(const Aspect_ScrollDelta& theDelta) = 0;
0088 
0089   //! Handle mouse button press/release event.
0090   //! This method is expected to be called from UI thread.
0091   //! @param thePoint      mouse cursor position
0092   //! @param theButtons    pressed buttons
0093   //! @param theModifiers  key modifiers
0094   //! @param theIsEmulated if TRUE then mouse event comes NOT from real mouse
0095   //!                      but emulated from non-precise input like touch on screen
0096   //! @return TRUE if window content should be redrawn
0097   virtual bool UpdateMouseButtons(const NCollection_Vec2<int>& thePoint,
0098                                   Aspect_VKeyMouse             theButtons,
0099                                   Aspect_VKeyFlags             theModifiers,
0100                                   bool                         theIsEmulated) = 0;
0101 
0102   //! Handle mouse cursor movement event.
0103   //! This method is expected to be called from UI thread.
0104   //! Default implementation does nothing.
0105   //! @param thePoint      mouse cursor position
0106   //! @param theButtons    pressed buttons
0107   //! @param theModifiers  key modifiers
0108   //! @param theIsEmulated if TRUE then mouse event comes NOT from real mouse
0109   //!                      but emulated from non-precise input like touch on screen
0110   //! @return TRUE if window content should be redrawn
0111   virtual bool UpdateMousePosition(const NCollection_Vec2<int>& thePoint,
0112                                    Aspect_VKeyMouse             theButtons,
0113                                    Aspect_VKeyFlags             theModifiers,
0114                                    bool                         theIsEmulated) = 0;
0115 
0116   //! Handle mouse button press event.
0117   //! This method is expected to be called from UI thread.
0118   //! Default implementation redirects to UpdateMousePosition().
0119   //! @param thePoint      mouse cursor position
0120   //! @param theButton     pressed button
0121   //! @param theModifiers  key modifiers
0122   //! @param theIsEmulated if TRUE then mouse event comes NOT from real mouse
0123   //!                      but emulated from non-precise input like touch on screen
0124   //! @return TRUE if window content should be redrawn
0125   bool PressMouseButton(const NCollection_Vec2<int>& thePoint,
0126                         Aspect_VKeyMouse             theButton,
0127                         Aspect_VKeyFlags             theModifiers,
0128                         bool                         theIsEmulated)
0129   {
0130     return UpdateMouseButtons(thePoint, myMousePressed | theButton, theModifiers, theIsEmulated);
0131   }
0132 
0133   //! Handle mouse button release event.
0134   //! This method is expected to be called from UI thread.
0135   //! Default implementation redirects to UpdateMousePosition().
0136   //! @param thePoint      mouse cursor position
0137   //! @param theButton     released button
0138   //! @param theModifiers  key modifiers
0139   //! @param theIsEmulated if TRUE then mouse event comes NOT from real mouse
0140   //!                      but emulated from non-precise input like touch on screen
0141   //! @return TRUE if window content should be redrawn
0142   bool ReleaseMouseButton(const NCollection_Vec2<int>& thePoint,
0143                           Aspect_VKeyMouse             theButton,
0144                           Aspect_VKeyFlags             theModifiers,
0145                           bool                         theIsEmulated)
0146   {
0147     Aspect_VKeyMouse aButtons = myMousePressed & (~theButton);
0148     return UpdateMouseButtons(thePoint, aButtons, theModifiers, theIsEmulated);
0149   }
0150 
0151   //! Return currently pressed mouse buttons.
0152   Aspect_VKeyMouse PressedMouseButtons() const { return myMousePressed; }
0153 
0154   //! Return active key modifiers passed with last mouse event.
0155   Aspect_VKeyFlags LastMouseFlags() const { return myMouseModifiers; }
0156 
0157   //! Return last mouse position.
0158   const NCollection_Vec2<int>& LastMousePosition() const { return myMousePositionLast; }
0159 
0160 public: //! @name multi-touch input
0161   //! Return TRUE if touches map is not empty.
0162   bool HasTouchPoints() const { return !myTouchPoints.IsEmpty(); }
0163 
0164   //! Return map of active touches.
0165   const NCollection_IndexedDataMap<size_t, Aspect_Touch>& TouchPoints() const
0166   {
0167     return myTouchPoints;
0168   }
0169 
0170   //! Add touch point with the given ID.
0171   //! This method is expected to be called from UI thread.
0172   //! @param theId touch unique identifier
0173   //! @param thePnt touch coordinates
0174   //! @param theClearBefore if TRUE previously registered touches will be removed
0175   Standard_EXPORT virtual void AddTouchPoint(size_t                          theId,
0176                                              const NCollection_Vec2<double>& thePnt,
0177                                              bool theClearBefore = false);
0178 
0179   //! Remove touch point with the given ID.
0180   //! This method is expected to be called from UI thread.
0181   //! @param theId touch unique identifier
0182   //! @param theClearSelectPnts if TRUE will initiate clearing of selection points
0183   //! @return TRUE if point has been removed
0184   Standard_EXPORT virtual bool RemoveTouchPoint(size_t theId, bool theClearSelectPnts = false);
0185 
0186   //! Update touch point with the given ID.
0187   //! If point with specified ID was not registered before, it will be added.
0188   //! This method is expected to be called from UI thread.
0189   //! @param theId touch unique identifier
0190   //! @param thePnt touch coordinates
0191   Standard_EXPORT virtual void UpdateTouchPoint(size_t                          theId,
0192                                                 const NCollection_Vec2<double>& thePnt);
0193 
0194 public: //! @name 3d mouse input
0195   //! Return acceleration ratio for translation event; 2.0 by default.
0196   float Get3dMouseTranslationScale() const { return my3dMouseAccelTrans; }
0197 
0198   //! Set acceleration ratio for translation event.
0199   void Set3dMouseTranslationScale(float theScale) { my3dMouseAccelTrans = theScale; }
0200 
0201   //! Return acceleration ratio for rotation event; 4.0 by default.
0202   float Get3dMouseRotationScale() const { return my3dMouseAccelRotate; }
0203 
0204   //! Set acceleration ratio for rotation event.
0205   void Set3dMouseRotationScale(float theScale) { my3dMouseAccelRotate = theScale; }
0206 
0207   //! Return quadric acceleration flag; TRUE by default.
0208   bool To3dMousePreciseInput() const { return my3dMouseIsQuadric; }
0209 
0210   //! Set quadric acceleration flag.
0211   void Set3dMousePreciseInput(bool theIsQuadric) { my3dMouseIsQuadric = theIsQuadric; }
0212 
0213   //! Return 3d mouse rotation axes (tilt/roll/spin) ignore flag; (FALSE, FALSE, FALSE) by default.
0214   const NCollection_Vec3<bool>& Get3dMouseIsNoRotate() const { return my3dMouseNoRotate; }
0215 
0216   //! Return 3d mouse rotation axes (tilt/roll/spin) ignore flag; (FALSE, FALSE, FALSE) by default.
0217   NCollection_Vec3<bool>& Change3dMouseIsNoRotate() { return my3dMouseNoRotate; }
0218 
0219   //! Return 3d mouse rotation axes (tilt/roll/spin) reverse flag; (TRUE, FALSE, FALSE) by default.
0220   const NCollection_Vec3<bool>& Get3dMouseToReverse() const { return my3dMouseToReverse; }
0221 
0222   //! Return 3d mouse rotation axes (tilt/roll/spin) reverse flag; (TRUE, FALSE, FALSE) by default.
0223   NCollection_Vec3<bool>& Change3dMouseToReverse() { return my3dMouseToReverse; }
0224 
0225   //! Process 3d mouse input event (redirects to translation, rotation and keys).
0226   virtual bool Update3dMouse(const WNT_HIDSpaceMouse& theEvent) = 0;
0227 
0228   //! Process 3d mouse input translation event.
0229   Standard_EXPORT virtual bool update3dMouseTranslation(const WNT_HIDSpaceMouse& theEvent);
0230 
0231   //! Process 3d mouse input rotation event.
0232   Standard_EXPORT virtual bool update3dMouseRotation(const WNT_HIDSpaceMouse& theEvent);
0233 
0234   //! Process 3d mouse input keys event.
0235   Standard_EXPORT virtual bool update3dMouseKeys(const WNT_HIDSpaceMouse& theEvent);
0236 
0237 protected:
0238   //! Empty constructor.
0239   Standard_EXPORT Aspect_WindowInputListener();
0240 
0241 protected:
0242   OSD_Timer myEventTimer; //!< timer for timestamping events
0243 
0244 protected:               //! @name keyboard input variables
0245   Aspect_VKeySet myKeys; //!< keyboard state
0246 
0247 protected:                                   //! @name mouse input variables
0248   NCollection_Vec2<int> myMousePositionLast; //!< last mouse position
0249   Aspect_VKeyMouse      myMousePressed;      //!< active mouse buttons
0250   Aspect_VKeyFlags      myMouseModifiers;    //!< active key modifiers passed with last mouse event
0251 
0252 protected:
0253   NCollection_IndexedDataMap<size_t, Aspect_Touch> myTouchPoints; //!< map of active touches
0254 
0255 protected:                                         //! @name 3d mouse input variables
0256   bool                   my3dMouseButtonState[32]; //!< cached button state
0257   NCollection_Vec3<bool> my3dMouseNoRotate;        //!< ignore  3d mouse rotation axes
0258   NCollection_Vec3<bool> my3dMouseToReverse;       //!< reverse 3d mouse rotation axes
0259   float                  my3dMouseAccelTrans;      //!< acceleration ratio for translation event
0260   float                  my3dMouseAccelRotate;     //!< acceleration ratio for rotation event
0261   bool                   my3dMouseIsQuadric;       //!< quadric acceleration
0262 };
0263 
0264 #endif // _Aspect_WindowInputListener_HeaderFile