Mercurial > hg > orthanc-stone
annotate Samples/WebAssembly/dev.h @ 1199:922d2e61aa5d
RadiograpyScene: can now remove any layer + new key wrappers for Delete/Backspace
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Thu, 28 Nov 2019 18:28:15 +0100 |
parents | 9c2f6d6b9f4a |
children | 2d8ab34c8c91 |
rev | line source |
---|---|
826 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #pragma once | |
23 | |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
24 #include "../../Framework/Viewport/WebAssemblyViewport.h" |
826 | 25 #include "../../Framework/Scene2D/OpenGLCompositor.h" |
26 #include "../../Framework/Scene2D/PanSceneTracker.h" | |
27 #include "../../Framework/Scene2D/RotateSceneTracker.h" | |
28 #include "../../Framework/Scene2D/ZoomSceneTracker.h" | |
890
77c96ba899f9
removing OpenGLCompositor::UpdateSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
826
diff
changeset
|
29 #include "../../Framework/Scene2DViewport/UndoStack.h" |
826 | 30 #include "../../Framework/Scene2DViewport/ViewportController.h" |
31 | |
32 #include <Core/OrthancException.h> | |
33 | |
34 #include <emscripten/html5.h> | |
890
77c96ba899f9
removing OpenGLCompositor::UpdateSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
826
diff
changeset
|
35 #include <boost/make_shared.hpp> |
826 | 36 |
37 static const unsigned int FONT_SIZE = 32; | |
38 | |
39 namespace OrthancStone | |
40 { | |
41 class ActiveTracker : public boost::noncopyable | |
42 { | |
43 private: | |
44 boost::shared_ptr<IFlexiblePointerTracker> tracker_; | |
45 std::string canvasIdentifier_; | |
46 bool insideCanvas_; | |
47 | |
48 public: | |
49 ActiveTracker(const boost::shared_ptr<IFlexiblePointerTracker>& tracker, | |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
50 const std::string& canvasId) : |
826 | 51 tracker_(tracker), |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
52 canvasIdentifier_(canvasId), |
826 | 53 insideCanvas_(true) |
54 { | |
55 if (tracker_.get() == NULL) | |
56 { | |
57 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
58 } | |
59 } | |
60 | |
61 bool IsAlive() const | |
62 { | |
63 return tracker_->IsAlive(); | |
64 } | |
65 | |
66 void PointerMove(const PointerEvent& event) | |
67 { | |
68 tracker_->PointerMove(event); | |
69 } | |
70 | |
71 void PointerUp(const PointerEvent& event) | |
72 { | |
73 tracker_->PointerUp(event); | |
74 } | |
75 }; | |
76 } | |
77 | |
78 static OrthancStone::PointerEvent* ConvertMouseEvent( | |
79 const EmscriptenMouseEvent& source, | |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
80 OrthancStone::IViewport& viewport) |
826 | 81 { |
82 std::auto_ptr<OrthancStone::PointerEvent> target( | |
83 new OrthancStone::PointerEvent); | |
84 | |
85 target->AddPosition(viewport.GetPixelCenterCoordinates( | |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
86 source.targetX, source.targetY)); |
826 | 87 target->SetAltModifier(source.altKey); |
88 target->SetControlModifier(source.ctrlKey); | |
89 target->SetShiftModifier(source.shiftKey); | |
90 | |
91 return target.release(); | |
92 } | |
93 | |
94 std::auto_ptr<OrthancStone::ActiveTracker> tracker_; | |
95 | |
96 EM_BOOL OnMouseEvent(int eventType, | |
97 const EmscriptenMouseEvent *mouseEvent, | |
98 void *userData) | |
99 { | |
100 if (mouseEvent != NULL && | |
101 userData != NULL) | |
102 { | |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
103 boost::shared_ptr<OrthancStone::ViewportController>& controller = |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
104 *reinterpret_cast<boost::shared_ptr<OrthancStone::ViewportController>*>(userData); |
826 | 105 |
106 switch (eventType) | |
107 { | |
108 case EMSCRIPTEN_EVENT_CLICK: | |
109 { | |
110 static unsigned int count = 0; | |
111 char buf[64]; | |
112 sprintf(buf, "click %d", count++); | |
113 | |
114 std::auto_ptr<OrthancStone::TextSceneLayer> layer(new OrthancStone::TextSceneLayer); | |
115 layer->SetText(buf); | |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
116 controller->GetViewport().GetScene().SetLayer(100, layer.release()); |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
117 controller->GetViewport().Refresh(); |
826 | 118 break; |
119 } | |
120 | |
121 case EMSCRIPTEN_EVENT_MOUSEDOWN: | |
122 { | |
123 boost::shared_ptr<OrthancStone::IFlexiblePointerTracker> t; | |
124 | |
125 { | |
126 std::auto_ptr<OrthancStone::PointerEvent> event( | |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
127 ConvertMouseEvent(*mouseEvent, controller->GetViewport())); |
826 | 128 |
129 switch (mouseEvent->button) | |
130 { | |
131 case 0: // Left button | |
132 emscripten_console_log("Creating RotateSceneTracker"); | |
133 t.reset(new OrthancStone::RotateSceneTracker( | |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
134 controller, *event)); |
826 | 135 break; |
136 | |
137 case 1: // Middle button | |
138 emscripten_console_log("Creating PanSceneTracker"); | |
139 LOG(INFO) << "Creating PanSceneTracker" ; | |
140 t.reset(new OrthancStone::PanSceneTracker( | |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
141 controller, *event)); |
826 | 142 break; |
143 | |
144 case 2: // Right button | |
145 emscripten_console_log("Creating ZoomSceneTracker"); | |
146 t.reset(new OrthancStone::ZoomSceneTracker( | |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
147 controller, *event, controller->GetViewport().GetCanvasWidth())); |
826 | 148 break; |
149 | |
150 default: | |
151 break; | |
152 } | |
153 } | |
154 | |
155 if (t.get() != NULL) | |
156 { | |
157 tracker_.reset( | |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
158 new OrthancStone::ActiveTracker(t, controller->GetViewport().GetCanvasIdentifier())); |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
159 controller->GetViewport().Refresh(); |
826 | 160 } |
161 | |
162 break; | |
163 } | |
164 | |
165 case EMSCRIPTEN_EVENT_MOUSEMOVE: | |
166 if (tracker_.get() != NULL) | |
167 { | |
168 std::auto_ptr<OrthancStone::PointerEvent> event( | |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
169 ConvertMouseEvent(*mouseEvent, controller->GetViewport())); |
826 | 170 tracker_->PointerMove(*event); |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
171 controller->GetViewport().Refresh(); |
826 | 172 } |
173 break; | |
174 | |
175 case EMSCRIPTEN_EVENT_MOUSEUP: | |
176 if (tracker_.get() != NULL) | |
177 { | |
178 std::auto_ptr<OrthancStone::PointerEvent> event( | |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
179 ConvertMouseEvent(*mouseEvent, controller->GetViewport())); |
826 | 180 tracker_->PointerUp(*event); |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
181 controller->GetViewport().Refresh(); |
826 | 182 if (!tracker_->IsAlive()) |
183 tracker_.reset(); | |
184 } | |
185 break; | |
186 | |
187 default: | |
188 break; | |
189 } | |
190 } | |
191 | |
192 return true; | |
193 } | |
194 | |
195 | |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
196 void SetupEvents(const std::string& canvas, |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
197 boost::shared_ptr<OrthancStone::ViewportController>& controller) |
826 | 198 { |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
199 emscripten_set_mousedown_callback(canvas.c_str(), &controller, false, OnMouseEvent); |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
200 emscripten_set_mousemove_callback(canvas.c_str(), &controller, false, OnMouseEvent); |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
201 emscripten_set_mouseup_callback(canvas.c_str(), &controller, false, OnMouseEvent); |
826 | 202 } |