Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-27 09:19:39

0001 // Created by: Kirill Gavrilov
0002 // Copyright (c) 2021 OPEN CASCADE SAS
0003 //
0004 // This file is part of Open CASCADE Technology software library.
0005 //
0006 // This library is free software; you can redistribute it and/or modify it under
0007 // the terms of the GNU Lesser General Public License version 2.1 as published
0008 // by the Free Software Foundation, with special exception defined in the file
0009 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0010 // distribution for complete text of the license and disclaimer of any warranty.
0011 //
0012 // Alternatively, this file may be used under the terms of Open CASCADE
0013 // commercial license or contractual agreement.
0014 
0015 #ifndef _Wasm_Window_HeaderFile
0016 #define _Wasm_Window_HeaderFile
0017 
0018 #include <Aspect_Window.hxx>
0019 
0020 #include <Aspect_VKey.hxx>
0021 #include <NCollection_Vec2.hxx>
0022 #include <Standard_TypeDef.hxx>
0023 
0024 class Aspect_WindowInputListener;
0025 
0026 struct EmscriptenMouseEvent;
0027 struct EmscriptenWheelEvent;
0028 struct EmscriptenTouchEvent;
0029 struct EmscriptenKeyboardEvent;
0030 struct EmscriptenUiEvent;
0031 struct EmscriptenFocusEvent;
0032 
0033 //! This class defines WebAssembly window (HTML5 canvas) intended for creation of OpenGL (WebGL)
0034 //! context.
0035 //!
0036 //! Note that canvas may define an independent dimensions for backing store (WebGL buffer to render)
0037 //! and for CSS (logical units to present buffer onto screen).
0038 //! These dimensions differ when browser is dragged into a high pixel density screen (HiDPI),
0039 //! or when user scales page in the browser (in both cases window.devicePixelRatio JavaScript
0040 //! property becomes not equal to 1.0).
0041 //!
0042 //! By default, Wasm_Window::DoResize() will scale backing store of a canvas basing on
0043 //! DevicePixelRatio() scale factor to ensure canvas content being rendered with the native
0044 //! resolution and not stretched by browser. This, however, might have side effects:
0045 //! - a slow GPU might experience performance issues on drawing into larger buffer (e.g. HiDPI);
0046 //! - user interface displayed in 3D Viewer (e.g. AIS presentations) should be scaled proportionally
0047 //! to be accessible,
0048 //!   which might require extra processing at application level.
0049 //! Consider changing ToScaleBacking flag passed to Wasm_Window constructor in case of issues.
0050 class Wasm_Window : public Aspect_Window
0051 {
0052   DEFINE_STANDARD_RTTIEXT(Wasm_Window, Aspect_Window)
0053 public:
0054   //! Convert Emscripten mouse buttons into Aspect_VKeyMouse.
0055   Standard_EXPORT static Aspect_VKeyMouse MouseButtonsFromNative(unsigned short theButtons);
0056 
0057   //! Convert DOM virtual key into Aspect_VKey.
0058   Standard_EXPORT static Aspect_VKey VirtualKeyFromNative(int theKey);
0059 
0060 public:
0061   //! Wraps existing HTML5 canvas into window.
0062   //! @param[in] theCanvasId target HTML element id defined in a querySelector() syntax
0063   //! @param[in] theToScaleBacking when TRUE, window will automatically scale backing store of
0064   //! canvas
0065   //!                              basing on DevicePixelRatio() scale factor within DoResize()
0066   Standard_EXPORT Wasm_Window(const TCollection_AsciiString& theCanvasId,
0067                               const bool                     theToScaleBacking = true);
0068 
0069   //! Destroys the window.
0070   Standard_EXPORT ~Wasm_Window() override;
0071 
0072   //! Return true if window is not hidden.
0073   bool IsMapped() const override { return myIsMapped; }
0074 
0075   //! Change window mapped flag to TRUE.
0076   void Map() const override { myIsMapped = true; }
0077 
0078   //! Change window mapped flag to FALSE.
0079   void Unmap() const override { myIsMapped = false; }
0080 
0081   //! Resize window.
0082   //! In case of ToScaleBacking flag, this method will resize the backing store of canvas
0083   //! basing on DevicePixelRatio() scale factor and CSS canvas size.
0084   Standard_EXPORT Aspect_TypeOfResize DoResize() override;
0085 
0086   //! Apply the mapping change to the window.
0087   bool DoMapping() const override { return true; }
0088 
0089   //! Returns window ratio equal to the physical width/height dimensions.
0090   Standard_EXPORT double Ratio() const override;
0091 
0092   //! Returns The Window POSITION in PIXEL
0093   Standard_EXPORT void Position(int& theX1, int& theY1, int& theX2, int& theY2) const override;
0094 
0095   //! Return the window size in pixels.
0096   Standard_EXPORT void Size(int& theWidth, int& theHeight) const override;
0097 
0098   //! Set new window size in logical (density-independent units).
0099   //! Backing store will be resized basing on DevicePixelRatio().
0100   Standard_EXPORT void SetSizeLogical(const NCollection_Vec2<double>& theSize);
0101 
0102   //! Set new window size in pixels.
0103   //! Logical size of the element will be resized basing on DevicePixelRatio().
0104   Standard_EXPORT void SetSizeBacking(const NCollection_Vec2<int>& theSize);
0105 
0106   //! Returns canvas id.
0107   const TCollection_AsciiString& CanvasId() const { return myCanvasId; }
0108 
0109   //! Current EGL implementation in Emscripten accepts only 0 for native window id.
0110   Aspect_Drawable NativeHandle() const override { return 0; }
0111 
0112   //! Always returns 0 for this class.
0113   Aspect_Drawable NativeParentHandle() const override { return 0; }
0114 
0115   //! Always returns 0 for this class.
0116   Aspect_FBConfig NativeFBConfig() const override { return nullptr; }
0117 
0118   //! Return device pixel ratio (logical to backing store scale factor).
0119   double DevicePixelRatio() const override { return myDevicePixelRatio; }
0120 
0121   //! Sets device pixel ratio for a window with IsVirtual() flag.
0122   void SetDevicePixelRatio(double theDevicePixelRatio) { myDevicePixelRatio = theDevicePixelRatio; }
0123 
0124   //! Invalidate entire window content through generation of Expose event.
0125   Standard_EXPORT void InvalidateContent(
0126     const occ::handle<Aspect_DisplayConnection>& theDisp) override;
0127 
0128 public:
0129   //! Process a single window message.
0130   //! @param[in,out] theListener listener to redirect message
0131   //! @param[in] theEventType message type to process
0132   //! @param[in] theEvent message to process
0133   //! @return TRUE if message has been processed
0134   Standard_EXPORT virtual bool ProcessMessage(Aspect_WindowInputListener& theListener,
0135                                               int                         theEventType,
0136                                               const void*                 theEvent);
0137 
0138   //! Process a mouse input message.
0139   //! @param[in,out] theListener listener to redirect message
0140   //! @param[in] theEventType message type to process
0141   //! @param[in] theEvent message to process
0142   //! @return TRUE if message has been processed
0143   Standard_EXPORT virtual bool ProcessMouseEvent(Aspect_WindowInputListener& theListener,
0144                                                  int                         theEventType,
0145                                                  const EmscriptenMouseEvent* theEvent);
0146 
0147   //! Process a (mouse) wheel input message.
0148   //! @param[in,out] theListener listener to redirect message
0149   //! @param[in] theEventType message type to process
0150   //! @param[in] theEvent message to process
0151   //! @return TRUE if message has been processed
0152   Standard_EXPORT virtual bool ProcessWheelEvent(Aspect_WindowInputListener& theListener,
0153                                                  int                         theEventType,
0154                                                  const EmscriptenWheelEvent* theEvent);
0155 
0156   //! Process a mouse input message.
0157   //! @param[in,out] theListener listener to redirect message
0158   //! @param[in] theEventType message type to process
0159   //! @param[in] theEvent message to process
0160   //! @return TRUE if message has been processed
0161   Standard_EXPORT virtual bool ProcessTouchEvent(Aspect_WindowInputListener& theListener,
0162                                                  int                         theEventType,
0163                                                  const EmscriptenTouchEvent* theEvent);
0164 
0165   //! Process a keyboard input message.
0166   //! @param[in,out] theListener listener to redirect message
0167   //! @param[in] theEventType message type to process
0168   //! @param[in] theEvent message to process
0169   //! @return TRUE if message has been processed
0170   Standard_EXPORT virtual bool ProcessKeyEvent(Aspect_WindowInputListener&    theListener,
0171                                                int                            theEventType,
0172                                                const EmscriptenKeyboardEvent* theEvent);
0173 
0174   //! Process a UI input message (like window resize).
0175   //! @param[in,out] theListener listener to redirect message
0176   //! @param[in] theEventType message type to process
0177   //! @param[in] theEvent message to process
0178   //! @return TRUE if message has been processed
0179   Standard_EXPORT virtual bool ProcessUiEvent(Aspect_WindowInputListener& theListener,
0180                                               int                         theEventType,
0181                                               const EmscriptenUiEvent*    theEvent);
0182 
0183   //! Process a focus input change message.
0184   //! @param[in,out] theListener listener to redirect message
0185   //! @param[in] theEventType message type to process
0186   //! @param[in] theEvent message to process
0187   //! @return TRUE if message has been processed
0188   Standard_EXPORT virtual bool ProcessFocusEvent(Aspect_WindowInputListener& theListener,
0189                                                  int                         theEventType,
0190                                                  const EmscriptenFocusEvent* theEvent);
0191 
0192 protected:
0193   TCollection_AsciiString myCanvasId;
0194   NCollection_Vec2<int>   mySize;
0195   double                  myDevicePixelRatio;
0196   bool                    myToScaleBacking;
0197   mutable bool            myIsMapped;
0198 };
0199 
0200 #endif // _Wasm_Window_HeaderFile