Mercurial > hg > orthanc-stone
comparison Samples/Sdl/BasicScene.cpp @ 602:03c4b998fcd0
display scene positions in the basic sample
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 29 Apr 2019 14:40:01 +0200 |
parents | 6129b1e5ba42 |
children | f4b37a991dac |
comparison
equal
deleted
inserted
replaced
600:6129b1e5ba42 | 602:03c4b998fcd0 |
---|---|
37 #include <Core/Images/PngWriter.h> | 37 #include <Core/Images/PngWriter.h> |
38 | 38 |
39 #include <SDL.h> | 39 #include <SDL.h> |
40 #include <stdio.h> | 40 #include <stdio.h> |
41 | 41 |
42 static const unsigned int FONT_SIZE = 64; | 42 static const unsigned int FONT_SIZE = 32; |
43 static const int LAYER_POSITION = 150; | |
43 | 44 |
44 | 45 |
45 void PrepareScene(OrthancStone::Scene2D& scene) | 46 void PrepareScene(OrthancStone::Scene2D& scene) |
46 { | 47 { |
47 using namespace OrthancStone; | 48 using namespace OrthancStone; |
123 layer->SetColor(0,255, 255); | 124 layer->SetColor(0,255, 255); |
124 scene.SetLayer(50, layer.release()); | 125 scene.SetLayer(50, layer.release()); |
125 } | 126 } |
126 | 127 |
127 // Some text | 128 // Some text |
128 scene.SetLayer(170, new TextSceneLayer(0, 0, "Hello", 0, BitmapAnchor_Center, 20)); | 129 { |
130 std::auto_ptr<TextSceneLayer> layer(new TextSceneLayer); | |
131 layer->SetText("Hello"); | |
132 scene.SetLayer(100, layer.release()); | |
133 } | |
129 } | 134 } |
130 | 135 |
131 | 136 |
132 void TakeScreenshot(const std::string& target, | 137 void TakeScreenshot(const std::string& target, |
133 const OrthancStone::Scene2D& scene, | 138 const OrthancStone::Scene2D& scene, |
154 const SDL_Event& event, | 159 const SDL_Event& event, |
155 std::auto_ptr<OrthancStone::IPointerTracker>& activeTracker, | 160 std::auto_ptr<OrthancStone::IPointerTracker>& activeTracker, |
156 unsigned int windowWidth, | 161 unsigned int windowWidth, |
157 unsigned int windowHeight) | 162 unsigned int windowHeight) |
158 { | 163 { |
159 if (event.type == SDL_MOUSEBUTTONDOWN) | 164 bool hasPositionLayer = false; |
165 | |
166 if (event.type == SDL_MOUSEMOTION) | |
167 { | |
168 int scancodeCount = 0; | |
169 const uint8_t* keyboardState = SDL_GetKeyboardState(&scancodeCount); | |
170 | |
171 if (activeTracker.get() == NULL && | |
172 SDL_SCANCODE_LCTRL < scancodeCount && | |
173 keyboardState[SDL_SCANCODE_LCTRL]) | |
174 { | |
175 // The "left-ctrl" key is down, while no tracker is present | |
176 | |
177 OrthancStone::PointerEvent e; | |
178 e.AddIntegerPosition(event.button.x - static_cast<int>(windowWidth) / 2, | |
179 event.button.y - static_cast<int>(windowHeight) / 2); | |
180 OrthancStone::ScenePoint2D p = e.GetMainPosition().Apply(scene.GetCanvasToSceneTransform()); | |
181 | |
182 char buf[64]; | |
183 sprintf(buf, "(%0.02f,%0.02f)", p.GetX(), p.GetY()); | |
184 | |
185 if (scene.HasLayer(LAYER_POSITION)) | |
186 { | |
187 OrthancStone::TextSceneLayer& layer = | |
188 dynamic_cast<OrthancStone::TextSceneLayer&>(scene.GetLayer(LAYER_POSITION)); | |
189 layer.SetText(buf); | |
190 layer.SetPosition(p.GetX(), p.GetY()); | |
191 } | |
192 else | |
193 { | |
194 std::auto_ptr<OrthancStone::TextSceneLayer> layer(new OrthancStone::TextSceneLayer); | |
195 layer->SetColor(0, 255, 0); | |
196 layer->SetText(buf); | |
197 layer->SetBorder(20); | |
198 layer->SetAnchor(OrthancStone::BitmapAnchor_BottomCenter); | |
199 layer->SetPosition(p.GetX(), p.GetY()); | |
200 scene.SetLayer(LAYER_POSITION, layer.release()); | |
201 } | |
202 | |
203 hasPositionLayer = true; | |
204 } | |
205 } | |
206 else if (event.type == SDL_MOUSEBUTTONDOWN) | |
160 { | 207 { |
161 OrthancStone::PointerEvent e; | 208 OrthancStone::PointerEvent e; |
162 e.AddIntegerPosition(event.button.x, event.button.y); | 209 e.AddIntegerPosition(event.button.x, event.button.y); |
163 | 210 |
164 switch (event.button.button) | 211 switch (event.button.button) |
195 break; | 242 break; |
196 | 243 |
197 default: | 244 default: |
198 break; | 245 break; |
199 } | 246 } |
247 } | |
248 | |
249 if (!hasPositionLayer) | |
250 { | |
251 scene.DeleteLayer(LAYER_POSITION); | |
200 } | 252 } |
201 } | 253 } |
202 | 254 |
203 | 255 |
204 static void GLAPIENTRY | 256 static void GLAPIENTRY |
282 case SDLK_q: | 334 case SDLK_q: |
283 stop = true; | 335 stop = true; |
284 break; | 336 break; |
285 | 337 |
286 default: | 338 default: |
287 HandleApplicationEvent(scene, event, tracker, window.GetCanvasWidth(), window.GetCanvasHeight()); | |
288 break; | 339 break; |
289 } | 340 } |
290 } | 341 } |
291 else | 342 |
292 { | 343 HandleApplicationEvent(scene, event, tracker, window.GetCanvasWidth(), window.GetCanvasHeight()); |
293 HandleApplicationEvent(scene, event, tracker, window.GetCanvasWidth(), window.GetCanvasHeight()); | |
294 } | |
295 } | 344 } |
296 | 345 |
297 SDL_Delay(1); | 346 SDL_Delay(1); |
298 } | 347 } |
299 } | 348 } |