Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:05:32

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