Mercurial > hg > orthanc-stone
comparison Deprecated/Samples/MultiPlatform/BasicScene/BasicScene.cpp @ 1402:65e1e4b08302
moved deprecated samples into Deprecated
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Wed, 29 Apr 2020 20:50:53 +0200 |
parents | Samples/Deprecated/MultiPlatform/BasicScene/BasicScene.cpp@eac254fb6791 |
children | 182bf3106ee2 |
comparison
equal
deleted
inserted
replaced
1401:f6a2d46d2b76 | 1402:65e1e4b08302 |
---|---|
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-2020 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 #include "BasicScene.h" | |
22 | |
23 // From Stone | |
24 #include "Framework/Scene2D/Scene2D.h" | |
25 #include "Framework/Scene2D/ColorTextureSceneLayer.h" | |
26 #include "Framework/Scene2D/PolylineSceneLayer.h" | |
27 #include "Framework/Scene2D/TextSceneLayer.h" | |
28 | |
29 #include "Framework/Scene2D/PanSceneTracker.h" | |
30 #include "Framework/Scene2D/ZoomSceneTracker.h" | |
31 #include "Framework/Scene2D/RotateSceneTracker.h" | |
32 | |
33 #include "Framework/Scene2D/CairoCompositor.h" | |
34 | |
35 // From Orthanc framework | |
36 #include <Core/Images/Image.h> | |
37 #include <Core/Images/ImageProcessing.h> | |
38 #include <Core/Images/PngWriter.h> | |
39 | |
40 using namespace OrthancStone; | |
41 | |
42 const unsigned int BASIC_SCENE_FONT_SIZE = 32; | |
43 const int BASIC_SCENE_LAYER_POSITION = 150; | |
44 | |
45 void PrepareScene(Scene2D& scene) | |
46 { | |
47 //Scene2D& scene(*controller->GetScene()); | |
48 // Texture of 2x2 size | |
49 { | |
50 Orthanc::Image i(Orthanc::PixelFormat_RGB24, 2, 2, false); | |
51 | |
52 uint8_t *p = reinterpret_cast<uint8_t*>(i.GetRow(0)); | |
53 p[0] = 255; | |
54 p[1] = 0; | |
55 p[2] = 0; | |
56 | |
57 p[3] = 0; | |
58 p[4] = 255; | |
59 p[5] = 0; | |
60 | |
61 p = reinterpret_cast<uint8_t*>(i.GetRow(1)); | |
62 p[0] = 0; | |
63 p[1] = 0; | |
64 p[2] = 255; | |
65 | |
66 p[3] = 255; | |
67 p[4] = 0; | |
68 p[5] = 0; | |
69 | |
70 scene.SetLayer(12, new ColorTextureSceneLayer(i)); | |
71 | |
72 std::unique_ptr<ColorTextureSceneLayer> l(new ColorTextureSceneLayer(i)); | |
73 l->SetOrigin(-3, 2); | |
74 l->SetPixelSpacing(1.5, 1); | |
75 l->SetAngle(20.0 / 180.0 * 3.14); | |
76 scene.SetLayer(14, l.release()); | |
77 } | |
78 | |
79 // Texture of 1x1 size | |
80 { | |
81 Orthanc::Image i(Orthanc::PixelFormat_RGB24, 1, 1, false); | |
82 | |
83 uint8_t *p = reinterpret_cast<uint8_t*>(i.GetRow(0)); | |
84 p[0] = 255; | |
85 p[1] = 0; | |
86 p[2] = 0; | |
87 | |
88 std::unique_ptr<ColorTextureSceneLayer> l(new ColorTextureSceneLayer(i)); | |
89 l->SetOrigin(-2, 1); | |
90 l->SetAngle(20.0 / 180.0 * 3.14); | |
91 scene.SetLayer(13, l.release()); | |
92 } | |
93 | |
94 // Some lines | |
95 { | |
96 std::unique_ptr<PolylineSceneLayer> layer(new PolylineSceneLayer); | |
97 | |
98 layer->SetThickness(1); | |
99 | |
100 PolylineSceneLayer::Chain chain; | |
101 chain.push_back(ScenePoint2D(0 - 0.5, 0 - 0.5)); | |
102 chain.push_back(ScenePoint2D(0 - 0.5, 2 - 0.5)); | |
103 chain.push_back(ScenePoint2D(2 - 0.5, 2 - 0.5)); | |
104 chain.push_back(ScenePoint2D(2 - 0.5, 0 - 0.5)); | |
105 layer->AddChain(chain, true, 255, 0, 0); | |
106 | |
107 chain.clear(); | |
108 chain.push_back(ScenePoint2D(-5, -5)); | |
109 chain.push_back(ScenePoint2D(5, -5)); | |
110 chain.push_back(ScenePoint2D(5, 5)); | |
111 chain.push_back(ScenePoint2D(-5, 5)); | |
112 layer->AddChain(chain, true, 0, 255, 0); | |
113 | |
114 double dy = 1.01; | |
115 chain.clear(); | |
116 chain.push_back(ScenePoint2D(-4, -4)); | |
117 chain.push_back(ScenePoint2D(4, -4 + dy)); | |
118 chain.push_back(ScenePoint2D(-4, -4 + 2.0 * dy)); | |
119 chain.push_back(ScenePoint2D(4, 2)); | |
120 layer->AddChain(chain, false, 0, 0, 255); | |
121 | |
122 // layer->SetColor(0,255, 255); | |
123 scene.SetLayer(50, layer.release()); | |
124 } | |
125 | |
126 // Some text | |
127 { | |
128 std::unique_ptr<TextSceneLayer> layer(new TextSceneLayer); | |
129 layer->SetText("Hello"); | |
130 scene.SetLayer(100, layer.release()); | |
131 } | |
132 } | |
133 | |
134 #if ORTHANC_SANDBOXED == 0 | |
135 void TakeScreenshot(const std::string& target, | |
136 const OrthancStone::Scene2D& scene, | |
137 unsigned int canvasWidth, | |
138 unsigned int canvasHeight) | |
139 { | |
140 using namespace OrthancStone; | |
141 // Take a screenshot, then save it as PNG file | |
142 CairoCompositor compositor(scene, canvasWidth, canvasHeight); | |
143 compositor.SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT, BASIC_SCENE_FONT_SIZE, Orthanc::Encoding_Latin1); | |
144 compositor.Refresh(); | |
145 | |
146 Orthanc::ImageAccessor canvas; | |
147 compositor.GetCanvas().GetReadOnlyAccessor(canvas); | |
148 | |
149 Orthanc::Image png(Orthanc::PixelFormat_RGB24, canvas.GetWidth(), canvas.GetHeight(), false); | |
150 Orthanc::ImageProcessing::Convert(png, canvas); | |
151 | |
152 Orthanc::PngWriter writer; | |
153 writer.WriteToFile(target, png); | |
154 } | |
155 #endif | |
156 | |
157 void ShowCursorInfo(Scene2D& scene, const PointerEvent& pointerEvent) | |
158 { | |
159 ScenePoint2D p = pointerEvent.GetMainPosition().Apply(scene.GetCanvasToSceneTransform()); | |
160 | |
161 char buf[64]; | |
162 sprintf(buf, "(%0.02f,%0.02f)", p.GetX(), p.GetY()); | |
163 | |
164 if (scene.HasLayer(BASIC_SCENE_LAYER_POSITION)) | |
165 { | |
166 TextSceneLayer& layer = | |
167 dynamic_cast<TextSceneLayer&>(scene.GetLayer(BASIC_SCENE_LAYER_POSITION)); | |
168 layer.SetText(buf); | |
169 layer.SetPosition(p.GetX(), p.GetY()); | |
170 } | |
171 else | |
172 { | |
173 std::unique_ptr<TextSceneLayer> | |
174 layer(new TextSceneLayer); | |
175 layer->SetColor(0, 255, 0); | |
176 layer->SetText(buf); | |
177 layer->SetBorder(20); | |
178 layer->SetAnchor(BitmapAnchor_BottomCenter); | |
179 layer->SetPosition(p.GetX(), p.GetY()); | |
180 scene.SetLayer(BASIC_SCENE_LAYER_POSITION, layer.release()); | |
181 } | |
182 } | |
183 | |
184 | |
185 | |
186 bool BasicScene2DInteractor::OnMouseEvent(const GuiAdapterMouseEvent& event, const PointerEvent& pointerEvent) | |
187 { | |
188 if (currentTracker_.get() != NULL) | |
189 { | |
190 switch (event.type) | |
191 { | |
192 case GUIADAPTER_EVENT_MOUSEUP: | |
193 { | |
194 currentTracker_->PointerUp(pointerEvent); | |
195 if (!currentTracker_->IsAlive()) | |
196 { | |
197 currentTracker_.reset(); | |
198 } | |
199 };break; | |
200 case GUIADAPTER_EVENT_MOUSEMOVE: | |
201 { | |
202 currentTracker_->PointerMove(pointerEvent); | |
203 };break; | |
204 default: | |
205 return false; | |
206 } | |
207 return true; | |
208 } | |
209 else if (event.type == GUIADAPTER_EVENT_MOUSEDOWN) | |
210 { | |
211 if (event.button == GUIADAPTER_MOUSEBUTTON_LEFT) | |
212 { | |
213 currentTracker_.reset(new RotateSceneTracker(viewportController_, pointerEvent)); | |
214 } | |
215 else if (event.button == GUIADAPTER_MOUSEBUTTON_MIDDLE) | |
216 { | |
217 currentTracker_.reset(new PanSceneTracker(viewportController_, pointerEvent)); | |
218 } | |
219 else if (event.button == GUIADAPTER_MOUSEBUTTON_RIGHT) | |
220 { | |
221 currentTracker_.reset(new ZoomSceneTracker(viewportController_, pointerEvent, viewportController_->GetViewport().GetCanvasHeight())); | |
222 } | |
223 } | |
224 else if (event.type == GUIADAPTER_EVENT_MOUSEMOVE) | |
225 { | |
226 if (showCursorInfo_) | |
227 { | |
228 Scene2D& scene(viewportController_->GetScene()); | |
229 ShowCursorInfo(scene, pointerEvent); | |
230 } | |
231 return true; | |
232 } | |
233 return false; | |
234 } | |
235 | |
236 bool BasicScene2DInteractor::OnKeyboardEvent(const GuiAdapterKeyboardEvent& guiEvent) | |
237 { | |
238 if (guiEvent.type == GUIADAPTER_EVENT_KEYDOWN) | |
239 { | |
240 switch (guiEvent.sym[0]) | |
241 { | |
242 case 's': | |
243 { | |
244 //viewportController_->FitContent(viewportController_->GetViewport().GetCanvasWidth(), viewportController_->GetViewport().GetCanvasHeight()); | |
245 viewportController_->FitContent(); | |
246 return true; | |
247 }; | |
248 #if ORTHANC_SANDBOXED == 0 | |
249 case 'c': | |
250 { | |
251 Scene2D& scene(viewportController_->GetScene()); | |
252 TakeScreenshot("screenshot.png", scene, viewportController_->GetViewport().GetCanvasWidth(), viewportController_->GetViewport().GetCanvasHeight()); | |
253 return true; | |
254 } | |
255 #endif | |
256 case 'd': | |
257 { | |
258 showCursorInfo_ = !showCursorInfo_; | |
259 if (!showCursorInfo_) | |
260 { | |
261 Scene2D& scene(viewportController_->GetScene()); | |
262 scene.DeleteLayer(BASIC_SCENE_LAYER_POSITION); | |
263 } | |
264 | |
265 return true; | |
266 } | |
267 } | |
268 } | |
269 return false; | |
270 } | |
271 | |
272 bool BasicScene2DInteractor::OnWheelEvent(const GuiAdapterWheelEvent& guiEvent) | |
273 { | |
274 return false; | |
275 } |