Mercurial > hg > orthanc-stone
comparison Platforms/WebAssembly/Defaults.cpp @ 228:210c1ce8e1a6 am
WasmViewport is no more a singleton
author | am@osimis.io |
---|---|
date | Thu, 14 Jun 2018 15:06:29 +0200 |
parents | c8f11437a6fd |
children | b0ba3b38a23c |
comparison
equal
deleted
inserted
replaced
227:c8f11437a6fd | 228:210c1ce8e1a6 |
---|---|
3 #include "WasmWebService.h" | 3 #include "WasmWebService.h" |
4 #include <Framework/dev.h> | 4 #include <Framework/dev.h> |
5 #include "Framework/Widgets/TestCairoWidget.h" | 5 #include "Framework/Widgets/TestCairoWidget.h" |
6 #include <Framework/Viewport/WidgetViewport.h> | 6 #include <Framework/Viewport/WidgetViewport.h> |
7 #include <Framework/Widgets/LayerWidget.h> | 7 #include <Framework/Widgets/LayerWidget.h> |
8 #include <algorithm> | |
8 | 9 |
9 static unsigned int width_ = 0; | 10 static unsigned int width_ = 0; |
10 static unsigned int height_ = 0; | 11 static unsigned int height_ = 0; |
11 | 12 |
12 /**********************************/ | 13 /**********************************/ |
13 | 14 |
14 static std::auto_ptr<OrthancStone::BasicWasmApplication> application; | 15 static std::auto_ptr<OrthancStone::BasicWasmApplication> application; |
15 static std::shared_ptr<OrthancStone::WidgetViewport> viewport_; | |
16 static OrthancStone::ChangeObserver changeObserver_; | 16 static OrthancStone::ChangeObserver changeObserver_; |
17 static OrthancStone::StatusBar statusBar_; | 17 static OrthancStone::StatusBar statusBar_; |
18 | 18 |
19 | 19 |
20 static std::list<std::shared_ptr<OrthancStone::WidgetViewport>> viewports_; | 20 static std::list<std::shared_ptr<OrthancStone::WidgetViewport>> viewports_; |
21 | |
22 std::shared_ptr<OrthancStone::WidgetViewport> FindViewportSharedPtr(ViewportHandle viewport) { | |
23 for (const auto& v : viewports_) { | |
24 if (v.get() == viewport) { | |
25 return v; | |
26 } | |
27 assert(false); | |
28 } | |
29 } | |
21 | 30 |
22 #ifdef __cplusplus | 31 #ifdef __cplusplus |
23 extern "C" { | 32 extern "C" { |
24 #endif | 33 #endif |
25 | 34 |
36 printf("There are now %d viewports in C++\n", viewports_.size()); | 45 printf("There are now %d viewports in C++\n", viewports_.size()); |
37 | 46 |
38 viewport->SetStatusBar(statusBar_); | 47 viewport->SetStatusBar(statusBar_); |
39 viewport->Register(changeObserver_); | 48 viewport->Register(changeObserver_); |
40 | 49 |
41 // TODO: remove once we really want to handle multiple viewports | |
42 viewport_ = viewport; | |
43 return viewport.get(); | 50 return viewport.get(); |
44 } | 51 } |
45 | 52 |
46 // when WASM does not need a viewport anymore, it should release it | 53 // when WASM does not need a viewport anymore, it should release it |
47 void EMSCRIPTEN_KEEPALIVE ReleaseCppViewport(ViewportHandle viewport) { | 54 void EMSCRIPTEN_KEEPALIVE ReleaseCppViewport(ViewportHandle viewport) { |
48 viewports_.remove_if([viewport](const std::shared_ptr<OrthancStone::WidgetViewport>& v) { return v.get() == viewport;}); | 55 viewports_.remove_if([viewport](const std::shared_ptr<OrthancStone::WidgetViewport>& v) { return v.get() == viewport;}); |
49 | 56 |
50 printf("There are now %d viewports in C++\n", viewports_.size()); | 57 printf("There are now %d viewports in C++\n", viewports_.size()); |
51 } | 58 } |
52 | 59 |
53 | |
54 void EMSCRIPTEN_KEEPALIVE CreateWasmApplication(ViewportHandle viewport) { | 60 void EMSCRIPTEN_KEEPALIVE CreateWasmApplication(ViewportHandle viewport) { |
55 | 61 |
56 printf("CreateWasmApplication\n"); | 62 printf("CreateWasmApplication\n"); |
57 | 63 |
58 application.reset(CreateUserApplication()); | 64 application.reset(CreateUserApplication()); |
64 void EMSCRIPTEN_KEEPALIVE SetStartupParameter(const char* keyc, | 70 void EMSCRIPTEN_KEEPALIVE SetStartupParameter(const char* keyc, |
65 const char* value) { | 71 const char* value) { |
66 application->SetStartupParameter(keyc, value); | 72 application->SetStartupParameter(keyc, value); |
67 } | 73 } |
68 | 74 |
69 void EMSCRIPTEN_KEEPALIVE StartWasmApplication() { | 75 void EMSCRIPTEN_KEEPALIVE StartWasmApplication(ViewportHandle viewport) { |
70 | 76 |
71 printf("StartWasmApplication\n"); | 77 printf("StartWasmApplication\n"); |
72 | 78 |
73 // recreate a command line from uri arguments and parse it | 79 // recreate a command line from uri arguments and parse it |
74 boost::program_options::variables_map parameters; | 80 boost::program_options::variables_map parameters; |
75 application->GetStartupParameters(parameters); | 81 application->GetStartupParameters(parameters); |
76 | 82 |
77 BasicWasmApplicationContext& context = dynamic_cast<BasicWasmApplicationContext&>(application->CreateApplicationContext(OrthancStone::WasmWebService::GetInstance(), viewport_));; | 83 BasicWasmApplicationContext& context = dynamic_cast<BasicWasmApplicationContext&>(application->CreateApplicationContext(OrthancStone::WasmWebService::GetInstance(), FindViewportSharedPtr(viewport))); |
78 application->Initialize(statusBar_, parameters); | 84 application->Initialize(statusBar_, parameters); |
79 | 85 |
80 viewport_->SetSize(width_, height_); | 86 viewport->SetSize(width_, height_); |
81 printf("StartWasmApplication - completed\n"); | 87 printf("StartWasmApplication - completed\n"); |
82 } | 88 } |
83 | 89 |
84 // void EMSCRIPTEN_KEEPALIVE ViewportUpdate(const char* _instanceId) { | |
85 // printf("updating viewport content, Instance = [%s]\n", instanceId.c_str()); | |
86 | |
87 // layerSource->LoadFrame(instanceId, 0); | |
88 // printf("frame loaded\n"); | |
89 // instanceWidget->UpdateContent(); | |
90 | |
91 // printf("update should be done\n"); | |
92 // } | |
93 | |
94 // void EMSCRIPTEN_KEEPALIVE ViewportStart() | |
95 // { | |
96 | |
97 // viewport_.reset(new OrthancStone::WidgetViewport); | |
98 // viewport_->SetStatusBar(statusBar_); | |
99 // viewport_->Register(changeObserver_); | |
100 // instanceWidget.reset(new OrthancStone::LayerWidget); | |
101 // layerSource = new OrthancStone::OrthancFrameLayerSource(OrthancStone::WasmWebService::GetInstance()); | |
102 | |
103 // if (!instanceId.empty()) | |
104 // { | |
105 // layerSource->LoadFrame(instanceId, 0); | |
106 // } else { | |
107 // printf("No instance provided so far\n"); | |
108 // } | |
109 | |
110 // instanceWidget->AddLayer(layerSource); | |
111 | |
112 // { | |
113 // OrthancStone::RenderStyle s; | |
114 // //s.drawGrid_ = true; | |
115 // s.alpha_ = 1; | |
116 // s.windowing_ = OrthancStone::ImageWindowing_Bone; | |
117 // instanceWidget->SetLayerStyle(0, s); | |
118 // } | |
119 | |
120 // viewport_->SetCentralWidget(instanceWidget.release()); | |
121 // viewport_->SetSize(width_, height_); | |
122 | |
123 | |
124 // } | |
125 | |
126 void EMSCRIPTEN_KEEPALIVE NotifyUpdateContent() | 90 void EMSCRIPTEN_KEEPALIVE NotifyUpdateContent() |
127 { | 91 { |
128 // TODO Only launch the JavaScript timer if "HasUpdateContent()" | 92 for (auto viewport : viewports_) { |
129 if (viewport_.get() != NULL && | 93 // TODO Only launch the JavaScript timer if "HasUpdateContent()" |
130 viewport_->HasUpdateContent()) | 94 if (viewport->HasUpdateContent()) |
131 { | 95 { |
132 viewport_->UpdateContent(); | 96 viewport->UpdateContent(); |
133 } | 97 } |
134 | 98 |
135 } | 99 } |
136 | 100 |
137 | 101 } |
138 void EMSCRIPTEN_KEEPALIVE ViewportSetSize(unsigned int width, unsigned int height) | 102 |
103 | |
104 void EMSCRIPTEN_KEEPALIVE ViewportSetSize(ViewportHandle viewport, unsigned int width, unsigned int height) | |
139 { | 105 { |
140 width_ = width; | 106 width_ = width; |
141 height_ = height; | 107 height_ = height; |
142 | 108 |
143 if (viewport_.get() != NULL) | 109 viewport->SetSize(width, height); |
144 { | 110 } |
145 viewport_->SetSize(width, height); | 111 |
146 } | 112 int EMSCRIPTEN_KEEPALIVE ViewportRender(ViewportHandle viewport, |
147 } | |
148 | |
149 int EMSCRIPTEN_KEEPALIVE ViewportRender(OrthancStone::WidgetViewport* viewport, | |
150 unsigned int width, | 113 unsigned int width, |
151 unsigned int height, | 114 unsigned int height, |
152 uint8_t* data) | 115 uint8_t* data) |
153 { | 116 { |
154 changeObserver_.Reset(); | 117 changeObserver_.Reset(); |
182 | 145 |
183 return 1; | 146 return 1; |
184 } | 147 } |
185 | 148 |
186 | 149 |
187 void EMSCRIPTEN_KEEPALIVE ViewportMouseDown(unsigned int rawButton, | 150 void EMSCRIPTEN_KEEPALIVE ViewportMouseDown(ViewportHandle viewport, |
151 unsigned int rawButton, | |
188 int x, | 152 int x, |
189 int y, | 153 int y, |
190 unsigned int rawModifiers) | 154 unsigned int rawModifiers) |
191 { | 155 { |
192 OrthancStone::MouseButton button; | 156 OrthancStone::MouseButton button; |
206 | 170 |
207 default: | 171 default: |
208 return; // Unknown button | 172 return; // Unknown button |
209 } | 173 } |
210 | 174 |
211 if (viewport_.get() != NULL) | 175 viewport->MouseDown(button, x, y, OrthancStone::KeyboardModifiers_None /* TODO */); |
212 { | 176 } |
213 viewport_->MouseDown(button, x, y, OrthancStone::KeyboardModifiers_None /* TODO */); | 177 |
214 } | 178 |
215 } | 179 void EMSCRIPTEN_KEEPALIVE ViewportMouseWheel(ViewportHandle viewport, |
216 | 180 int deltaY, |
217 | |
218 void EMSCRIPTEN_KEEPALIVE ViewportMouseWheel(int deltaY, | |
219 int x, | 181 int x, |
220 int y, | 182 int y, |
221 int isControl) | 183 int isControl) |
222 { | 184 { |
223 if (viewport_.get() != NULL && | 185 if (deltaY != 0) |
224 deltaY != 0) | |
225 { | 186 { |
226 OrthancStone::MouseWheelDirection direction = (deltaY < 0 ? | 187 OrthancStone::MouseWheelDirection direction = (deltaY < 0 ? |
227 OrthancStone::MouseWheelDirection_Up : | 188 OrthancStone::MouseWheelDirection_Up : |
228 OrthancStone::MouseWheelDirection_Down); | 189 OrthancStone::MouseWheelDirection_Down); |
229 OrthancStone::KeyboardModifiers modifiers = OrthancStone::KeyboardModifiers_None; | 190 OrthancStone::KeyboardModifiers modifiers = OrthancStone::KeyboardModifiers_None; |
231 if (isControl != 0) | 192 if (isControl != 0) |
232 { | 193 { |
233 modifiers = OrthancStone::KeyboardModifiers_Control; | 194 modifiers = OrthancStone::KeyboardModifiers_Control; |
234 } | 195 } |
235 | 196 |
236 viewport_->MouseWheel(direction, x, y, modifiers); | 197 viewport->MouseWheel(direction, x, y, modifiers); |
237 } | 198 } |
238 } | 199 } |
239 | 200 |
240 | 201 |
241 void EMSCRIPTEN_KEEPALIVE ViewportMouseMove(int x, | 202 void EMSCRIPTEN_KEEPALIVE ViewportMouseMove(ViewportHandle viewport, |
203 int x, | |
242 int y) | 204 int y) |
243 { | 205 { |
244 if (viewport_.get() != NULL) | 206 viewport->MouseMove(x, y); |
245 { | 207 } |
246 viewport_->MouseMove(x, y); | 208 |
247 } | 209 void EMSCRIPTEN_KEEPALIVE ViewportKeyPressed(ViewportHandle viewport, |
248 } | 210 const char* key, |
249 | |
250 void EMSCRIPTEN_KEEPALIVE ViewportKeyPressed(const char* key, | |
251 bool isShiftPressed, | 211 bool isShiftPressed, |
252 bool isControlPressed, | 212 bool isControlPressed, |
253 bool isAltPressed) | 213 bool isAltPressed) |
254 | 214 |
255 { | 215 { |
256 if (viewport_.get() != NULL) | 216 OrthancStone::KeyboardModifiers modifiers = OrthancStone::KeyboardModifiers_None; |
257 { | 217 if (isShiftPressed) { |
258 OrthancStone::KeyboardModifiers modifiers = OrthancStone::KeyboardModifiers_None; | 218 modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Shift); |
259 if (isShiftPressed) { | 219 } |
260 modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Shift); | 220 if (isControlPressed) { |
261 } | 221 modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Control); |
262 if (isControlPressed) { | 222 } |
263 modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Control); | 223 if (isAltPressed) { |
264 } | 224 modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Alt); |
265 if (isAltPressed) { | 225 } |
266 modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Alt); | 226 printf("key pressed : %c\n", key[0]); |
267 } | 227 viewport->KeyPressed(key[0], modifiers); |
268 printf("key pressed : %c\n", key[0]); | 228 } |
269 viewport_->KeyPressed(key[0], modifiers); | 229 |
270 } | 230 |
271 } | 231 void EMSCRIPTEN_KEEPALIVE ViewportMouseUp(ViewportHandle viewport) |
272 | 232 { |
273 | 233 viewport->MouseUp(); |
274 void EMSCRIPTEN_KEEPALIVE ViewportMouseUp() | 234 } |
275 { | 235 |
276 if (viewport_.get() != NULL) | 236 |
277 { | 237 void EMSCRIPTEN_KEEPALIVE ViewportMouseEnter(ViewportHandle viewport) |
278 viewport_->MouseUp(); | 238 { |
279 } | 239 viewport->MouseEnter(); |
280 } | 240 } |
281 | 241 |
282 | 242 |
283 void EMSCRIPTEN_KEEPALIVE ViewportMouseEnter() | 243 void EMSCRIPTEN_KEEPALIVE ViewportMouseLeave(ViewportHandle viewport) |
284 { | 244 { |
285 if (viewport_.get() != NULL) | 245 viewport->MouseLeave(); |
286 { | |
287 viewport_->MouseEnter(); | |
288 } | |
289 } | |
290 | |
291 | |
292 void EMSCRIPTEN_KEEPALIVE ViewportMouseLeave() | |
293 { | |
294 if (viewport_.get() != NULL) | |
295 { | |
296 viewport_->MouseLeave(); | |
297 } | |
298 } | 246 } |
299 | 247 |
300 | 248 |
301 #ifdef __cplusplus | 249 #ifdef __cplusplus |
302 } | 250 } |