Mercurial > hg > orthanc-stone
annotate Platforms/Wasm/Defaults.cpp @ 242:092db46c6291 am
improved SDL/Wasm unification
author | am@osimis.io |
---|---|
date | Wed, 20 Jun 2018 14:20:55 +0200 |
parents | f73d722d98c8 |
children | 8ff70c04c6df |
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; | |
19 | |
223 | 20 static OrthancStone::ChangeObserver changeObserver_; |
21 static OrthancStone::StatusBar statusBar_; | |
22 | |
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 | |
69 application.reset(CreateUserApplication()); | |
70 | |
242 | 71 startupParametersBuilder.Clear(); |
223 | 72 } |
73 | |
74 void EMSCRIPTEN_KEEPALIVE SetStartupParameter(const char* keyc, | |
75 const char* value) { | |
242 | 76 startupParametersBuilder.SetStartupParameter(keyc, value); |
223 | 77 } |
78 | |
231
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
229
diff
changeset
|
79 void EMSCRIPTEN_KEEPALIVE StartWasmApplication() { |
223 | 80 |
81 printf("StartWasmApplication\n"); | |
82 | |
83 // recreate a command line from uri arguments and parse it | |
84 boost::program_options::variables_map parameters; | |
242 | 85 boost::program_options::options_description options; |
86 application->DeclareStartupOptions(options); | |
87 startupParametersBuilder.GetStartupParameters(parameters, options); | |
223 | 88 |
242 | 89 context.reset(new OrthancStone::BasicApplicationContext(OrthancStone::WasmWebService::GetInstance())); |
90 application->Initialize(context.get(), statusBar_, parameters); | |
91 application->InitializeWasm(); | |
223 | 92 |
231
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
229
diff
changeset
|
93 // viewport->SetSize(width_, height_); |
223 | 94 printf("StartWasmApplication - completed\n"); |
95 } | |
96 | |
97 void EMSCRIPTEN_KEEPALIVE NotifyUpdateContent() | |
98 { | |
228 | 99 for (auto viewport : viewports_) { |
100 // TODO Only launch the JavaScript timer if "HasUpdateContent()" | |
101 if (viewport->HasUpdateContent()) | |
102 { | |
103 viewport->UpdateContent(); | |
104 } | |
105 | |
223 | 106 } |
107 | |
108 } | |
109 | |
110 | |
228 | 111 void EMSCRIPTEN_KEEPALIVE ViewportSetSize(ViewportHandle viewport, unsigned int width, unsigned int height) |
223 | 112 { |
113 width_ = width; | |
114 height_ = height; | |
115 | |
228 | 116 viewport->SetSize(width, height); |
223 | 117 } |
118 | |
228 | 119 int EMSCRIPTEN_KEEPALIVE ViewportRender(ViewportHandle viewport, |
227 | 120 unsigned int width, |
223 | 121 unsigned int height, |
122 uint8_t* data) | |
123 { | |
124 changeObserver_.Reset(); | |
125 | |
126 //printf("ViewportRender called %dx%d\n", width, height); | |
127 if (width == 0 || | |
128 height == 0) | |
129 { | |
130 return 1; | |
131 } | |
132 | |
133 Orthanc::ImageAccessor surface; | |
134 surface.AssignWritable(Orthanc::PixelFormat_BGRA32, width, height, 4 * width, data); | |
135 | |
227 | 136 viewport->Render(surface); |
223 | 137 |
138 // Convert from BGRA32 memory layout (only color mode supported by | |
139 // Cairo, which corresponds to CAIRO_FORMAT_ARGB32) to RGBA32 (as | |
140 // expected by HTML5 canvas). This simply amounts to swapping the | |
141 // B and R channels. | |
142 uint8_t* p = data; | |
143 for (unsigned int y = 0; y < height; y++) { | |
144 for (unsigned int x = 0; x < width; x++) { | |
145 uint8_t tmp = p[0]; | |
146 p[0] = p[2]; | |
147 p[2] = tmp; | |
148 | |
149 p += 4; | |
150 } | |
151 } | |
152 | |
153 return 1; | |
154 } | |
155 | |
156 | |
228 | 157 void EMSCRIPTEN_KEEPALIVE ViewportMouseDown(ViewportHandle viewport, |
158 unsigned int rawButton, | |
223 | 159 int x, |
160 int y, | |
161 unsigned int rawModifiers) | |
162 { | |
163 OrthancStone::MouseButton button; | |
164 switch (rawButton) | |
165 { | |
166 case 0: | |
167 button = OrthancStone::MouseButton_Left; | |
168 break; | |
169 | |
170 case 1: | |
171 button = OrthancStone::MouseButton_Middle; | |
172 break; | |
173 | |
174 case 2: | |
175 button = OrthancStone::MouseButton_Right; | |
176 break; | |
177 | |
178 default: | |
179 return; // Unknown button | |
180 } | |
181 | |
228 | 182 viewport->MouseDown(button, x, y, OrthancStone::KeyboardModifiers_None /* TODO */); |
223 | 183 } |
184 | |
185 | |
228 | 186 void EMSCRIPTEN_KEEPALIVE ViewportMouseWheel(ViewportHandle viewport, |
187 int deltaY, | |
223 | 188 int x, |
189 int y, | |
190 int isControl) | |
191 { | |
228 | 192 if (deltaY != 0) |
223 | 193 { |
194 OrthancStone::MouseWheelDirection direction = (deltaY < 0 ? | |
195 OrthancStone::MouseWheelDirection_Up : | |
196 OrthancStone::MouseWheelDirection_Down); | |
197 OrthancStone::KeyboardModifiers modifiers = OrthancStone::KeyboardModifiers_None; | |
198 | |
199 if (isControl != 0) | |
200 { | |
201 modifiers = OrthancStone::KeyboardModifiers_Control; | |
202 } | |
203 | |
228 | 204 viewport->MouseWheel(direction, x, y, modifiers); |
223 | 205 } |
206 } | |
207 | |
208 | |
228 | 209 void EMSCRIPTEN_KEEPALIVE ViewportMouseMove(ViewportHandle viewport, |
210 int x, | |
223 | 211 int y) |
212 { | |
228 | 213 viewport->MouseMove(x, y); |
223 | 214 } |
215 | |
228 | 216 void EMSCRIPTEN_KEEPALIVE ViewportKeyPressed(ViewportHandle viewport, |
217 const char* key, | |
223 | 218 bool isShiftPressed, |
219 bool isControlPressed, | |
220 bool isAltPressed) | |
221 | |
222 { | |
228 | 223 OrthancStone::KeyboardModifiers modifiers = OrthancStone::KeyboardModifiers_None; |
224 if (isShiftPressed) { | |
225 modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Shift); | |
223 | 226 } |
228 | 227 if (isControlPressed) { |
228 modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Control); | |
229 } | |
230 if (isAltPressed) { | |
231 modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Alt); | |
232 } | |
233 printf("key pressed : %c\n", key[0]); | |
234 viewport->KeyPressed(key[0], modifiers); | |
223 | 235 } |
236 | |
237 | |
228 | 238 void EMSCRIPTEN_KEEPALIVE ViewportMouseUp(ViewportHandle viewport) |
223 | 239 { |
228 | 240 viewport->MouseUp(); |
223 | 241 } |
242 | |
243 | |
228 | 244 void EMSCRIPTEN_KEEPALIVE ViewportMouseEnter(ViewportHandle viewport) |
223 | 245 { |
228 | 246 viewport->MouseEnter(); |
223 | 247 } |
248 | |
249 | |
228 | 250 void EMSCRIPTEN_KEEPALIVE ViewportMouseLeave(ViewportHandle viewport) |
223 | 251 { |
228 | 252 viewport->MouseLeave(); |
223 | 253 } |
254 | |
255 | |
256 #ifdef __cplusplus | |
257 } | |
258 #endif |