comparison Platforms/WebAssembly/Defaults.cpp @ 223:d30a10d574ec am

refactoring continued - not working
author am@osimis.io
date Thu, 14 Jun 2018 10:57:02 +0200
parents 84844649a8fd
children c8f11437a6fd
comparison
equal deleted inserted replaced
222:84844649a8fd 223:d30a10d574ec
1 #include "Defaults.h" 1 #include "Defaults.h"
2 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
9 static unsigned int width_ = 0;
10 static unsigned int height_ = 0;
11
12 /**********************************/
13
14 static std::auto_ptr<OrthancStone::BasicWasmApplication> application;
15 static std::shared_ptr<OrthancStone::WidgetViewport> viewport_;
16 static OrthancStone::ChangeObserver changeObserver_;
17 static OrthancStone::StatusBar statusBar_;
18
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 using namespace OrthancStone;
25
26 void EMSCRIPTEN_KEEPALIVE CreateWasmApplication() {
27
28 printf("CreateWasmApplication\n");
29 viewport_.reset(new OrthancStone::WidgetViewport);
30 viewport_->SetStatusBar(statusBar_);
31 viewport_->Register(changeObserver_);
32
33
34 application.reset(CreateUserApplication());
35
36 boost::program_options::options_description options;
37 application->DeclareStartupOptions(options);
38 }
39
40 void EMSCRIPTEN_KEEPALIVE SetStartupParameter(const char* keyc,
41 const char* value) {
42 application->SetStartupParameter(keyc, value);
43 }
44
45 void EMSCRIPTEN_KEEPALIVE StartWasmApplication() {
46
47 printf("StartWasmApplication\n");
48
49 // recreate a command line from uri arguments and parse it
50 boost::program_options::variables_map parameters;
51 application->GetStartupParameters(parameters);
52
53 BasicWasmApplicationContext& context = dynamic_cast<BasicWasmApplicationContext&>(application->CreateApplicationContext(OrthancStone::WasmWebService::GetInstance(), viewport_));;
54 application->Initialize(statusBar_, parameters);
55
56 viewport_->SetSize(width_, height_);
57 printf("StartWasmApplication - completed\n");
58 }
59
60 // void EMSCRIPTEN_KEEPALIVE ViewportUpdate(const char* _instanceId) {
61 // printf("updating viewport content, Instance = [%s]\n", instanceId.c_str());
62
63 // layerSource->LoadFrame(instanceId, 0);
64 // printf("frame loaded\n");
65 // instanceWidget->UpdateContent();
66
67 // printf("update should be done\n");
68 // }
69
70 // void EMSCRIPTEN_KEEPALIVE ViewportStart()
71 // {
72
73 // viewport_.reset(new OrthancStone::WidgetViewport);
74 // viewport_->SetStatusBar(statusBar_);
75 // viewport_->Register(changeObserver_);
76 // instanceWidget.reset(new OrthancStone::LayerWidget);
77 // layerSource = new OrthancStone::OrthancFrameLayerSource(OrthancStone::WasmWebService::GetInstance());
78
79 // if (!instanceId.empty())
80 // {
81 // layerSource->LoadFrame(instanceId, 0);
82 // } else {
83 // printf("No instance provided so far\n");
84 // }
85
86 // instanceWidget->AddLayer(layerSource);
87
88 // {
89 // OrthancStone::RenderStyle s;
90 // //s.drawGrid_ = true;
91 // s.alpha_ = 1;
92 // s.windowing_ = OrthancStone::ImageWindowing_Bone;
93 // instanceWidget->SetLayerStyle(0, s);
94 // }
95
96 // viewport_->SetCentralWidget(instanceWidget.release());
97 // viewport_->SetSize(width_, height_);
98
99
100 // }
101
102 void EMSCRIPTEN_KEEPALIVE NotifyUpdateContent()
103 {
104 // TODO Only launch the JavaScript timer if "HasUpdateContent()"
105 if (viewport_.get() != NULL &&
106 viewport_->HasUpdateContent())
107 {
108 viewport_->UpdateContent();
109 }
110
111 }
112
113
114 void EMSCRIPTEN_KEEPALIVE ViewportSetSize(unsigned int width, unsigned int height)
115 {
116 width_ = width;
117 height_ = height;
118
119 if (viewport_.get() != NULL)
120 {
121 viewport_->SetSize(width, height);
122 }
123 }
124
125 int EMSCRIPTEN_KEEPALIVE ViewportRender(unsigned int width,
126 unsigned int height,
127 uint8_t* data)
128 {
129 changeObserver_.Reset();
130
131 //printf("ViewportRender called %dx%d\n", width, height);
132 if (width == 0 ||
133 height == 0)
134 {
135 return 1;
136 }
137
138 Orthanc::ImageAccessor surface;
139 surface.AssignWritable(Orthanc::PixelFormat_BGRA32, width, height, 4 * width, data);
140
141 if (viewport_.get() != NULL)
142 {
143 viewport_->Render(surface);
144 }
145
146 // Convert from BGRA32 memory layout (only color mode supported by
147 // Cairo, which corresponds to CAIRO_FORMAT_ARGB32) to RGBA32 (as
148 // expected by HTML5 canvas). This simply amounts to swapping the
149 // B and R channels.
150 uint8_t* p = data;
151 for (unsigned int y = 0; y < height; y++) {
152 for (unsigned int x = 0; x < width; x++) {
153 uint8_t tmp = p[0];
154 p[0] = p[2];
155 p[2] = tmp;
156
157 p += 4;
158 }
159 }
160
161 return 1;
162 }
163
164
165 void EMSCRIPTEN_KEEPALIVE ViewportMouseDown(unsigned int rawButton,
166 int x,
167 int y,
168 unsigned int rawModifiers)
169 {
170 OrthancStone::MouseButton button;
171 switch (rawButton)
172 {
173 case 0:
174 button = OrthancStone::MouseButton_Left;
175 break;
176
177 case 1:
178 button = OrthancStone::MouseButton_Middle;
179 break;
180
181 case 2:
182 button = OrthancStone::MouseButton_Right;
183 break;
184
185 default:
186 return; // Unknown button
187 }
188
189 if (viewport_.get() != NULL)
190 {
191 viewport_->MouseDown(button, x, y, OrthancStone::KeyboardModifiers_None /* TODO */);
192 }
193 }
194
195
196 void EMSCRIPTEN_KEEPALIVE ViewportMouseWheel(int deltaY,
197 int x,
198 int y,
199 int isControl)
200 {
201 if (viewport_.get() != NULL &&
202 deltaY != 0)
203 {
204 OrthancStone::MouseWheelDirection direction = (deltaY < 0 ?
205 OrthancStone::MouseWheelDirection_Up :
206 OrthancStone::MouseWheelDirection_Down);
207 OrthancStone::KeyboardModifiers modifiers = OrthancStone::KeyboardModifiers_None;
208
209 if (isControl != 0)
210 {
211 modifiers = OrthancStone::KeyboardModifiers_Control;
212 }
213
214 viewport_->MouseWheel(direction, x, y, modifiers);
215 }
216 }
217
218
219 void EMSCRIPTEN_KEEPALIVE ViewportMouseMove(int x,
220 int y)
221 {
222 if (viewport_.get() != NULL)
223 {
224 viewport_->MouseMove(x, y);
225 }
226 }
227
228 void EMSCRIPTEN_KEEPALIVE ViewportKeyPressed(const char* key,
229 bool isShiftPressed,
230 bool isControlPressed,
231 bool isAltPressed)
232
233 {
234 if (viewport_.get() != NULL)
235 {
236 OrthancStone::KeyboardModifiers modifiers = OrthancStone::KeyboardModifiers_None;
237 if (isShiftPressed) {
238 modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Shift);
239 }
240 if (isControlPressed) {
241 modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Control);
242 }
243 if (isAltPressed) {
244 modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Alt);
245 }
246 printf("key pressed : %c\n", key[0]);
247 viewport_->KeyPressed(key[0], modifiers);
248 }
249 }
250
251
252 void EMSCRIPTEN_KEEPALIVE ViewportMouseUp()
253 {
254 if (viewport_.get() != NULL)
255 {
256 viewport_->MouseUp();
257 }
258 }
259
260
261 void EMSCRIPTEN_KEEPALIVE ViewportMouseEnter()
262 {
263 if (viewport_.get() != NULL)
264 {
265 viewport_->MouseEnter();
266 }
267 }
268
269
270 void EMSCRIPTEN_KEEPALIVE ViewportMouseLeave()
271 {
272 if (viewport_.get() != NULL)
273 {
274 viewport_->MouseLeave();
275 }
276 }
277
278
279 #ifdef __cplusplus
280 }
281 #endif