0031458: Visualization - refine classes across Prs3d and StdPrs packages
[occt.git] / samples / webgl / WasmOcctView.h
CommitLineData
565baee6 1// Copyright (c) 2019 OPEN CASCADE SAS
2//
3// This file is part of the examples of the Open CASCADE Technology software library.
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
21
22#ifndef _WasmOcctView_HeaderFile
23#define _WasmOcctView_HeaderFile
24
25#include <AIS_InteractiveContext.hxx>
26#include <AIS_ViewController.hxx>
27#include <V3d_View.hxx>
28
29#include <emscripten.h>
30#include <emscripten/html5.h>
31
32class AIS_ViewCube;
33
34//! Sample class creating 3D Viewer within Emscripten canvas.
35class WasmOcctView : protected AIS_ViewController
36{
37public:
38 //! Default constructor.
39 WasmOcctView();
40
41 //! Destructor.
42 virtual ~WasmOcctView();
43
44 //! Main application entry point.
45 void run();
46
47 //! Return interactive context.
48 const Handle(AIS_InteractiveContext)& Context() const { return myContext; }
49
50 //! Return view.
51 const Handle(V3d_View)& View() const { return myView; }
52
53 //! Return device pixel ratio for handling high DPI displays.
54 float DevicePixelRatio() const { return myDevicePixelRatio; }
55
56private:
57
58 //! Create window.
59 void initWindow();
60
61 //! Create 3D Viewer.
62 bool initViewer();
63
64 //! Fill 3D Viewer with a DEMO items.
65 void initDemoScene();
66
67 //! Application event loop.
68 void mainloop();
69
70 //! Request view redrawing.
71 void updateView();
72
73 //! Flush events and redraw view.
74 void redrawView();
75
76 //! Handle view redraw.
77 virtual void handleViewRedraw (const Handle(AIS_InteractiveContext)& theCtx,
78 const Handle(V3d_View)& theView) override;
79
80 //! Dump WebGL context information.
81 void dumpGlInfo (bool theIsBasic);
82
83 //! Initialize pixel scale ratio.
84 void initPixelScaleRatio();
85
86 //! Return point from logical units to backing store.
87 Graphic3d_Vec2d convertPointToBacking (const Graphic3d_Vec2d& thePnt) const
88 {
89 return thePnt * myDevicePixelRatio;
90 }
91
92 //! Return point from logical units to backing store.
93 Graphic3d_Vec2i convertPointToBacking (const Graphic3d_Vec2i& thePnt) const
94 {
95 Graphic3d_Vec2d aPnt = Graphic3d_Vec2d (thePnt) * myDevicePixelRatio + Graphic3d_Vec2d (0.5);
96 return Graphic3d_Vec2i (aPnt);
97 }
98
99//! @name Emscripten callbacks
100private:
101 //! Window resize event.
102 EM_BOOL onResizeEvent (int theEventType, const EmscriptenUiEvent* theEvent);
103
104 //! Mouse event.
105 EM_BOOL onMouseEvent (int theEventType, const EmscriptenMouseEvent* theEvent);
106
107 //! Scroll event.
108 EM_BOOL onWheelEvent (int theEventType, const EmscriptenWheelEvent* theEvent);
109
110 //! Touch event.
111 EM_BOOL onTouchEvent (int theEventType, const EmscriptenTouchEvent* theEvent);
112
113 //! Key down event.
114 EM_BOOL onKeyDownEvent (int theEventType, const EmscriptenKeyboardEvent* theEvent);
115
116 //! Key up event.
117 EM_BOOL onKeyUpEvent (int theEventType, const EmscriptenKeyboardEvent* theEvent);
118
119//! @name Emscripten callbacks (static functions)
120private:
121
122 static EM_BOOL onResizeCallback (int theEventType, const EmscriptenUiEvent* theEvent, void* theView)
123 { return ((WasmOcctView* )theView)->onResizeEvent (theEventType, theEvent); }
124
125 static void onRedrawView (void* theView)
126 { return ((WasmOcctView* )theView)->redrawView(); }
127
128 static EM_BOOL onMouseCallback (int theEventType, const EmscriptenMouseEvent* theEvent, void* theView)
129 { return ((WasmOcctView* )theView)->onMouseEvent (theEventType, theEvent); }
130
131 static EM_BOOL onWheelCallback (int theEventType, const EmscriptenWheelEvent* theEvent, void* theView)
132 { return ((WasmOcctView* )theView)->onWheelEvent (theEventType, theEvent); }
133
134 static EM_BOOL onTouchCallback (int theEventType, const EmscriptenTouchEvent* theEvent, void* theView)
135 { return ((WasmOcctView* )theView)->onTouchEvent (theEventType, theEvent); }
136
137 static EM_BOOL onKeyDownCallback (int theEventType, const EmscriptenKeyboardEvent* theEvent, void* theView)
138 { return ((WasmOcctView* )theView)->onKeyDownEvent (theEventType, theEvent); }
139
140 static EM_BOOL onKeyUpCallback (int theEventType, const EmscriptenKeyboardEvent* theEvent, void* theView)
141 { return ((WasmOcctView* )theView)->onKeyUpEvent (theEventType, theEvent); }
142
143private:
144
145 Handle(AIS_InteractiveContext) myContext; //!< interactive context
146 Handle(V3d_View) myView; //!< 3D view
147 Handle(Prs3d_TextAspect) myTextStyle; //!< text style for OSD elements
148 Handle(AIS_ViewCube) myViewCube; //!< view cube object
149 TCollection_AsciiString myCanvasId; //!< canvas element id on HTML page
150 Aspect_Touch myClickTouch; //!< single touch position for handling clicks
151 OSD_Timer myDoubleTapTimer; //!< timer for handling double tap
152 float myDevicePixelRatio; //!< device pixel ratio for handling high DPI displays
153 unsigned int myUpdateRequests; //!< counter for unhandled update requests
154
155};
156
157#endif // _WasmOcctView_HeaderFile