Mercurial > hg > orthanc-stone
comparison OrthancStone/Sources/Platforms/WebAssembly/WebAssemblyViewport.h @ 1899:917500c46fe0
moved the Platform folder from the Applications folder to the Stone library itself
author | Alain Mazy <am@osimis.io> |
---|---|
date | Sat, 29 Jan 2022 12:47:32 +0100 |
parents | |
children | 184b0aeae1af |
comparison
equal
deleted
inserted
replaced
1898:a5e54bd87b25 | 1899:917500c46fe0 |
---|---|
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-2022 Osimis S.A., Belgium | |
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium | |
7 * | |
8 * This program is free software: you can redistribute it and/or | |
9 * modify it under the terms of the GNU Lesser General Public License | |
10 * as published by the Free Software Foundation, either version 3 of | |
11 * the License, or (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * Lesser General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU Lesser General Public | |
19 * License along with this program. If not, see | |
20 * <http://www.gnu.org/licenses/>. | |
21 **/ | |
22 | |
23 | |
24 #pragma once | |
25 | |
26 #include "../../../OrthancStone/Sources/OrthancStone.h" | |
27 | |
28 #if !defined(ORTHANC_ENABLE_WASM) | |
29 # error Macro ORTHANC_ENABLE_WASM must be defined | |
30 #endif | |
31 | |
32 #if ORTHANC_ENABLE_WASM != 1 | |
33 # error This file can only be used if targeting WebAssembly | |
34 #endif | |
35 | |
36 #include "../../../OrthancStone/Sources/Viewport/IViewport.h" | |
37 #include "../../../OrthancStone/Sources/Viewport/IViewportInteractor.h" | |
38 | |
39 #include <Compatibility.h> | |
40 | |
41 #include <emscripten.h> | |
42 #include <emscripten/html5.h> | |
43 | |
44 #include <memory> | |
45 #include <string> | |
46 #include <vector> | |
47 #include <boost/enable_shared_from_this.hpp> | |
48 | |
49 namespace OrthancStone | |
50 { | |
51 class WebAssemblyViewport : public IViewport, | |
52 public boost::enable_shared_from_this<WebAssemblyViewport> | |
53 | |
54 { | |
55 private: | |
56 class WasmLock; | |
57 | |
58 std::string canvasId_; | |
59 std::string canvasCssSelector_; | |
60 std::unique_ptr<ICompositor> compositor_; | |
61 std::unique_ptr<ViewportController> controller_; | |
62 std::unique_ptr<IViewportInteractor> interactor_; | |
63 bool enableEmscriptenMouseEvents_; | |
64 unsigned int canvasWidth_; | |
65 unsigned int canvasHeight_; | |
66 | |
67 static EM_BOOL OnRequestAnimationFrame(double time, void *userData); | |
68 | |
69 static EM_BOOL OnResize(int eventType, const EmscriptenUiEvent *uiEvent, void *userData); | |
70 | |
71 static EM_BOOL OnMouseDown(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData); | |
72 | |
73 static EM_BOOL OnMouseMove(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData); | |
74 | |
75 static EM_BOOL OnMouseUp(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData); | |
76 | |
77 protected: | |
78 void Invalidate(); | |
79 | |
80 void ClearCompositor() | |
81 { | |
82 compositor_.reset(); | |
83 } | |
84 | |
85 bool HasCompositor() const | |
86 { | |
87 return compositor_.get() != NULL; | |
88 } | |
89 | |
90 void AcquireCompositor(ICompositor* compositor /* takes ownership */); | |
91 | |
92 virtual void Paint(ICompositor& compositor, | |
93 ViewportController& controller) = 0; | |
94 | |
95 /** | |
96 The second argument is temporary and should be deleted once the migration | |
97 to interactors is finished. It should be set to "true" for new applications. | |
98 */ | |
99 WebAssemblyViewport(const std::string& canvasId, | |
100 bool enableEmscriptenMouseEvents); | |
101 | |
102 void PostConstructor(); | |
103 | |
104 | |
105 /** | |
106 * This method can be called to retrieve a cookie that can be passed to | |
107 * C-style callbacks that expect the object to be passed as a void* | |
108 * | |
109 * This cookie is a resource and must be freed when it is guaranteed | |
110 * not to be used anymore, with ReleaseObjectCookie | |
111 */ | |
112 void* CreateObjectCookie(); | |
113 | |
114 /** | |
115 * This static method can be used to dereference a cookie (i.e. retrieve | |
116 * a pointer to the underlying object) that has been created with | |
117 * WebAssemblyViewport::CreateObjectCookie() | |
118 * | |
119 * If this method returns NULL, it basically means that the | |
120 * WebAssemblyViewport has already been deleted and that you should NOT | |
121 * attempt to use it! | |
122 * | |
123 * This method never releases the cookie (for other in-flight callbacks | |
124 * could possibly require to cookie to be valid) | |
125 * | |
126 * If this method is called AFTER ReleaseObjectCookie has been called on | |
127 * the same cookie, the behavior is undefined and things will CERTAINLY | |
128 * go wrong. | |
129 * | |
130 * NEVER call this method on a void* that has not been generated by the | |
131 * CreateObjectCookie method of this class | |
132 */ | |
133 static WebAssemblyViewport* DereferenceObjectCookie(void* cookie); | |
134 | |
135 /** | |
136 * This method must be used when the object cookie will not be used anymore. | |
137 * | |
138 * You must call it when you are certain that no entity will attempt to | |
139 * dereference the cookie. | |
140 */ | |
141 static void ReleaseObjectCookie(void* cookie); | |
142 | |
143 void RefreshCanvasSize(); | |
144 | |
145 unsigned int GetCanvasWidth() const | |
146 { | |
147 return canvasWidth_; | |
148 } | |
149 | |
150 unsigned int GetCanvasHeight() | |
151 { | |
152 return canvasHeight_; | |
153 } | |
154 | |
155 public: | |
156 virtual ILock* Lock() ORTHANC_OVERRIDE; | |
157 | |
158 ~WebAssemblyViewport(); | |
159 | |
160 /** | |
161 This method takes ownership | |
162 */ | |
163 void AcquireInteractor(IViewportInteractor* interactor); | |
164 | |
165 const std::string& GetCanvasId() const | |
166 { | |
167 return canvasId_; | |
168 } | |
169 | |
170 /** | |
171 emscripten functions requires the css selector for the canvas. This is | |
172 different from the canvas id (the syntax is '#mycanvasid') | |
173 */ | |
174 const std::string& GetCanvasCssSelector() const | |
175 { | |
176 return canvasCssSelector_; | |
177 } | |
178 | |
179 void FitForPrint(); | |
180 }; | |
181 } |