comparison Applications/Platforms/WebAssembly/WebAssemblyViewport.h @ 1591:5887a4f8594b

moving platform-specific files out of the "OrthancStone" folder
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 23 Oct 2020 13:15:03 +0200
parents OrthancStone/Sources/Viewport/WebAssemblyViewport.h@1f812f4c95be
children 9a52bac0c2a7
comparison
equal deleted inserted replaced
1590:7b963bccafef 1591:5887a4f8594b
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #pragma once
23
24 #include "../../../OrthancStone/Sources/OrthancStone.h"
25
26 #if !defined(ORTHANC_ENABLE_WASM)
27 # error Macro ORTHANC_ENABLE_WASM must be defined
28 #endif
29
30 #if ORTHANC_ENABLE_WASM != 1
31 # error This file can only be used if targeting WebAssembly
32 #endif
33
34 #include "../../../OrthancStone/Sources/Viewport/IViewport.h"
35 #include "../../../OrthancStone/Sources/Viewport/IViewportInteractor.h"
36
37 #include <Compatibility.h>
38
39 #include <emscripten.h>
40 #include <emscripten/html5.h>
41
42 #include <memory>
43 #include <string>
44 #include <boost/enable_shared_from_this.hpp>
45
46 namespace OrthancStone
47 {
48 class WebAssemblyViewport : public IViewport,
49 public boost::enable_shared_from_this<WebAssemblyViewport>
50
51 {
52 private:
53 class WasmLock;
54
55 std::string canvasId_;
56 std::string canvasCssSelector_;
57 std::unique_ptr<ICompositor> compositor_;
58 std::unique_ptr<ViewportController> controller_;
59 std::unique_ptr<IViewportInteractor> interactor_;
60 bool enableEmscriptenMouseEvents_;
61 unsigned int canvasWidth_;
62 unsigned int canvasHeight_;
63
64 static EM_BOOL OnRequestAnimationFrame(double time, void *userData);
65
66 static EM_BOOL OnResize(int eventType, const EmscriptenUiEvent *uiEvent, void *userData);
67
68 static EM_BOOL OnMouseDown(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData);
69
70 static EM_BOOL OnMouseMove(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData);
71
72 static EM_BOOL OnMouseUp(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData);
73
74 protected:
75 void Invalidate();
76
77 void ClearCompositor()
78 {
79 compositor_.reset();
80 }
81
82 bool HasCompositor() const
83 {
84 return compositor_.get() != NULL;
85 }
86
87 void AcquireCompositor(ICompositor* compositor /* takes ownership */);
88
89 virtual void Paint(ICompositor& compositor,
90 ViewportController& controller) = 0;
91
92 /**
93 The second argument is temporary and should be deleted once the migration
94 to interactors is finished. It should be set to "true" for new applications.
95 */
96 WebAssemblyViewport(const std::string& canvasId,
97 bool enableEmscriptenMouseEvents);
98
99 void PostConstructor();
100
101 void RefreshCanvasSize();
102
103 unsigned int GetCanvasWidth() const
104 {
105 return canvasWidth_;
106 }
107
108 unsigned int GetCanvasHeight()
109 {
110 return canvasHeight_;
111 }
112
113 public:
114 virtual ILock* Lock() ORTHANC_OVERRIDE;
115
116 ~WebAssemblyViewport();
117
118 /**
119 This method takes ownership
120 */
121 void AcquireInteractor(IViewportInteractor* interactor);
122
123 const std::string& GetCanvasId() const
124 {
125 return canvasId_;
126 }
127
128 /**
129 emscripten functions requires the css selector for the canvas. This is
130 different from the canvas id (the syntax is '#mycanvasid')
131 */
132 const std::string& GetCanvasCssSelector() const
133 {
134 return canvasCssSelector_;
135 }
136
137 void FitForPrint();
138 };
139 }