comparison Platforms/Wasm/Defaults.cpp @ 236:f73d722d98c8 am

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