Mercurial > hg > orthanc-stone
annotate Platforms/Wasm/Defaults.cpp @ 272:d7d91d96b9d8 am-2
fix
author | am@osimis.io |
---|---|
date | Fri, 24 Aug 2018 09:11:06 +0200 |
parents | 65562a28fe05 |
children | 8a86695fcbc3 |
rev | line source |
---|---|
222 | 1 #include "Defaults.h" |
2 | |
223 | 3 #include "WasmWebService.h" |
4 #include <Framework/dev.h> | |
5 #include "Framework/Widgets/TestCairoWidget.h" | |
6 #include <Framework/Viewport/WidgetViewport.h> | |
7 #include <Framework/Widgets/LayerWidget.h> | |
228 | 8 #include <algorithm> |
242 | 9 #include "Applications/Wasm/StartupParametersBuilder.h" |
223 | 10 |
11 static unsigned int width_ = 0; | |
12 static unsigned int height_ = 0; | |
13 | |
14 /**********************************/ | |
15 | |
242 | 16 static std::unique_ptr<OrthancStone::IBasicApplication> application; |
17 static std::unique_ptr<OrthancStone::BasicApplicationContext> context; | |
18 static OrthancStone::StartupParametersBuilder startupParametersBuilder; | |
253 | 19 static OrthancStone::MessageBroker broker; |
242 | 20 |
223 | 21 static OrthancStone::ChangeObserver changeObserver_; |
22 static OrthancStone::StatusBar statusBar_; | |
23 | |
227 | 24 static std::list<std::shared_ptr<OrthancStone::WidgetViewport>> viewports_; |
25 | |
228 | 26 std::shared_ptr<OrthancStone::WidgetViewport> FindViewportSharedPtr(ViewportHandle viewport) { |
27 for (const auto& v : viewports_) { | |
28 if (v.get() == viewport) { | |
29 return v; | |
30 } | |
31 } | |
229 | 32 assert(false); |
33 return std::shared_ptr<OrthancStone::WidgetViewport>(); | |
228 | 34 } |
35 | |
223 | 36 #ifdef __cplusplus |
37 extern "C" { | |
38 #endif | |
39 | |
40 using namespace OrthancStone; | |
227 | 41 |
42 // when WASM needs a C++ viewport | |
43 ViewportHandle EMSCRIPTEN_KEEPALIVE CreateCppViewport() { | |
44 | |
45 std::shared_ptr<OrthancStone::WidgetViewport> viewport(new OrthancStone::WidgetViewport); | |
46 printf("viewport %x\n", viewport.get()); | |
47 | |
48 viewports_.push_back(viewport); | |
49 | |
50 printf("There are now %d viewports in C++\n", viewports_.size()); | |
51 | |
52 viewport->SetStatusBar(statusBar_); | |
53 viewport->Register(changeObserver_); | |
54 | |
55 return viewport.get(); | |
56 } | |
57 | |
58 // when WASM does not need a viewport anymore, it should release it | |
59 void EMSCRIPTEN_KEEPALIVE ReleaseCppViewport(ViewportHandle viewport) { | |
60 viewports_.remove_if([viewport](const std::shared_ptr<OrthancStone::WidgetViewport>& v) { return v.get() == viewport;}); | |
61 | |
62 printf("There are now %d viewports in C++\n", viewports_.size()); | |
63 } | |
64 | |
65 void EMSCRIPTEN_KEEPALIVE CreateWasmApplication(ViewportHandle viewport) { | |
223 | 66 |
67 printf("CreateWasmApplication\n"); | |
68 | |
253 | 69 application.reset(CreateUserApplication(broker)); |
255 | 70 WasmWebService::SetBroker(broker); |
223 | 71 |
242 | 72 startupParametersBuilder.Clear(); |
223 | 73 } |
74 | |
75 void EMSCRIPTEN_KEEPALIVE SetStartupParameter(const char* keyc, | |
76 const char* value) { | |
242 | 77 startupParametersBuilder.SetStartupParameter(keyc, value); |
223 | 78 } |
79 | |
231
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
229
diff
changeset
|
80 void EMSCRIPTEN_KEEPALIVE StartWasmApplication() { |
223 | 81 |
82 printf("StartWasmApplication\n"); | |
83 | |
84 // recreate a command line from uri arguments and parse it | |
85 boost::program_options::variables_map parameters; | |
242 | 86 boost::program_options::options_description options; |
87 application->DeclareStartupOptions(options); | |
88 startupParametersBuilder.GetStartupParameters(parameters, options); | |
223 | 89 |
272 | 90 context.reset(new OrthancStone::BasicApplicationContext()); |
91 context->SetWebService(OrthancStone::WasmWebService::GetInstance()); | |
242 | 92 application->Initialize(context.get(), statusBar_, parameters); |
93 application->InitializeWasm(); | |
223 | 94 |
231
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
229
diff
changeset
|
95 // viewport->SetSize(width_, height_); |
223 | 96 printf("StartWasmApplication - completed\n"); |
97 } | |
98 | |
99 void EMSCRIPTEN_KEEPALIVE NotifyUpdateContent() | |
100 { | |
228 | 101 for (auto viewport : viewports_) { |
102 // TODO Only launch the JavaScript timer if "HasUpdateContent()" | |
103 if (viewport->HasUpdateContent()) | |
104 { | |
105 viewport->UpdateContent(); | |
106 } | |
107 | |
223 | 108 } |
109 | |
110 } | |
111 | |
112 | |
228 | 113 void EMSCRIPTEN_KEEPALIVE ViewportSetSize(ViewportHandle viewport, unsigned int width, unsigned int height) |
223 | 114 { |
115 width_ = width; | |
116 height_ = height; | |
117 | |
228 | 118 viewport->SetSize(width, height); |
223 | 119 } |
120 | |
228 | 121 int EMSCRIPTEN_KEEPALIVE ViewportRender(ViewportHandle viewport, |
227 | 122 unsigned int width, |
223 | 123 unsigned int height, |
124 uint8_t* data) | |
125 { | |
126 changeObserver_.Reset(); | |
127 | |
128 //printf("ViewportRender called %dx%d\n", width, height); | |
129 if (width == 0 || | |
130 height == 0) | |
131 { | |
132 return 1; | |
133 } | |
134 | |
135 Orthanc::ImageAccessor surface; | |
136 surface.AssignWritable(Orthanc::PixelFormat_BGRA32, width, height, 4 * width, data); | |
137 | |
227 | 138 viewport->Render(surface); |
223 | 139 |
140 // Convert from BGRA32 memory layout (only color mode supported by | |
141 // Cairo, which corresponds to CAIRO_FORMAT_ARGB32) to RGBA32 (as | |
142 // expected by HTML5 canvas). This simply amounts to swapping the | |
143 // B and R channels. | |
144 uint8_t* p = data; | |
145 for (unsigned int y = 0; y < height; y++) { | |
146 for (unsigned int x = 0; x < width; x++) { | |
147 uint8_t tmp = p[0]; | |
148 p[0] = p[2]; | |
149 p[2] = tmp; | |
150 | |
151 p += 4; | |
152 } | |
153 } | |
154 | |
155 return 1; | |
156 } | |
157 | |
158 | |
228 | 159 void EMSCRIPTEN_KEEPALIVE ViewportMouseDown(ViewportHandle viewport, |
160 unsigned int rawButton, | |
223 | 161 int x, |
162 int y, | |
163 unsigned int rawModifiers) | |
164 { | |
165 OrthancStone::MouseButton button; | |
166 switch (rawButton) | |
167 { | |
168 case 0: | |
169 button = OrthancStone::MouseButton_Left; | |
170 break; | |
171 | |
172 case 1: | |
173 button = OrthancStone::MouseButton_Middle; | |
174 break; | |
175 | |
176 case 2: | |
177 button = OrthancStone::MouseButton_Right; | |
178 break; | |
179 | |
180 default: | |
181 return; // Unknown button | |
182 } | |
183 | |
228 | 184 viewport->MouseDown(button, x, y, OrthancStone::KeyboardModifiers_None /* TODO */); |
223 | 185 } |
186 | |
187 | |
228 | 188 void EMSCRIPTEN_KEEPALIVE ViewportMouseWheel(ViewportHandle viewport, |
189 int deltaY, | |
223 | 190 int x, |
191 int y, | |
192 int isControl) | |
193 { | |
228 | 194 if (deltaY != 0) |
223 | 195 { |
196 OrthancStone::MouseWheelDirection direction = (deltaY < 0 ? | |
197 OrthancStone::MouseWheelDirection_Up : | |
198 OrthancStone::MouseWheelDirection_Down); | |
199 OrthancStone::KeyboardModifiers modifiers = OrthancStone::KeyboardModifiers_None; | |
200 | |
201 if (isControl != 0) | |
202 { | |
203 modifiers = OrthancStone::KeyboardModifiers_Control; | |
204 } | |
205 | |
228 | 206 viewport->MouseWheel(direction, x, y, modifiers); |
223 | 207 } |
208 } | |
209 | |
210 | |
228 | 211 void EMSCRIPTEN_KEEPALIVE ViewportMouseMove(ViewportHandle viewport, |
212 int x, | |
223 | 213 int y) |
214 { | |
228 | 215 viewport->MouseMove(x, y); |
223 | 216 } |
217 | |
228 | 218 void EMSCRIPTEN_KEEPALIVE ViewportKeyPressed(ViewportHandle viewport, |
219 const char* key, | |
223 | 220 bool isShiftPressed, |
221 bool isControlPressed, | |
222 bool isAltPressed) | |
223 | |
224 { | |
228 | 225 OrthancStone::KeyboardModifiers modifiers = OrthancStone::KeyboardModifiers_None; |
226 if (isShiftPressed) { | |
227 modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Shift); | |
223 | 228 } |
228 | 229 if (isControlPressed) { |
230 modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Control); | |
231 } | |
232 if (isAltPressed) { | |
233 modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Alt); | |
234 } | |
235 printf("key pressed : %c\n", key[0]); | |
236 viewport->KeyPressed(key[0], modifiers); | |
223 | 237 } |
238 | |
239 | |
228 | 240 void EMSCRIPTEN_KEEPALIVE ViewportMouseUp(ViewportHandle viewport) |
223 | 241 { |
228 | 242 viewport->MouseUp(); |
223 | 243 } |
244 | |
245 | |
228 | 246 void EMSCRIPTEN_KEEPALIVE ViewportMouseEnter(ViewportHandle viewport) |
223 | 247 { |
228 | 248 viewport->MouseEnter(); |
223 | 249 } |
250 | |
251 | |
228 | 252 void EMSCRIPTEN_KEEPALIVE ViewportMouseLeave(ViewportHandle viewport) |
223 | 253 { |
228 | 254 viewport->MouseLeave(); |
223 | 255 } |
256 | |
257 | |
258 #ifdef __cplusplus | |
259 } | |
260 #endif |