comparison Samples/Sdl/TrackerSampleApp.cpp @ 761:07adcffba38c

truncation warning fixes + CRLF -> LF + random measuring tool creation (keyb shortcut: 'm')
author Benjamin Golinvaux <bgo@osimis.io>
date Thu, 23 May 2019 10:25:48 +0200
parents 92c400a09f1b
children 1a28fce57ff3
comparison
equal deleted inserted replaced
760:1181e1ad98ec 761:07adcffba38c
69 Scene2DPtr TrackerSampleApp::GetScene() 69 Scene2DPtr TrackerSampleApp::GetScene()
70 { 70 {
71 return controller_->GetScene(); 71 return controller_->GetScene();
72 } 72 }
73 73
74 Scene2DConstPtr TrackerSampleApp::GetScene() const
75 {
76 return controller_->GetScene();
77 }
78
74 void TrackerSampleApp::SelectNextTool() 79 void TrackerSampleApp::SelectNextTool()
75 { 80 {
76 currentTool_ = static_cast<GuiTool>(currentTool_ + 1); 81 currentTool_ = static_cast<GuiTool>(currentTool_ + 1);
77 if (currentTool_ == GuiTool_LAST) 82 if (currentTool_ == GuiTool_LAST)
78 currentTool_ = static_cast<GuiTool>(0);; 83 currentTool_ = static_cast<GuiTool>(0);;
151 void TrackerSampleApp::HideInfoText() 156 void TrackerSampleApp::HideInfoText()
152 { 157 {
153 GetScene()->DeleteLayer(FLOATING_INFOTEXT_LAYER_ZINDEX); 158 GetScene()->DeleteLayer(FLOATING_INFOTEXT_LAYER_ZINDEX);
154 } 159 }
155 160
161 ScenePoint2D TrackerSampleApp::GetRandomPointInScene() const
162 {
163 unsigned int w = compositor_->GetCanvasWidth();
164 LOG(TRACE) << "compositor_->GetCanvasWidth() = " <<
165 compositor_->GetCanvasWidth();
166 unsigned int h = compositor_->GetCanvasHeight();
167 LOG(TRACE) << "compositor_->GetCanvasHeight() = " <<
168 compositor_->GetCanvasHeight();
169
170 if ((w >= RAND_MAX) || (h >= RAND_MAX))
171 LOG(WARNING) << "Canvas is too big : tools will not be randomly placed";
172
173 int x = rand() % w;
174 int y = rand() % h;
175 LOG(TRACE) << "random x = " << x << "random y = " << y;
176
177 ScenePoint2D p = compositor_->GetPixelCenterCoordinates(x, y);
178 LOG(TRACE) << "--> p.GetX() = " << p.GetX() << " p.GetY() = " << p.GetY();
179
180 ScenePoint2D r = p.Apply(GetScene()->GetCanvasToSceneTransform());
181 LOG(TRACE) << "--> r.GetX() = " << r.GetX() << " r.GetY() = " << r.GetY();
182 return r;
183 }
184
185 void TrackerSampleApp::CreateRandomMeasureTool()
186 {
187 static bool srandCalled = false;
188 if (!srandCalled)
189 {
190 srand(42);
191 srandCalled = true;
192 }
193
194 int i = rand() % 2;
195 LOG(TRACE) << "random i = " << i;
196 switch (i)
197 {
198 case 0:
199 // line measure
200 {
201 CreateLineMeasureCommandPtr cmd =
202 boost::make_shared<CreateLineMeasureCommand>(
203 IObserver::GetBroker(),
204 controller_,
205 GetRandomPointInScene());
206 cmd->SetEnd(GetRandomPointInScene());
207 controller_->PushCommand(cmd);
208 }
209 break;
210 case 1:
211 // angle measure
212 {
213 CreateAngleMeasureCommandPtr cmd =
214 boost::make_shared<CreateAngleMeasureCommand>(
215 IObserver::GetBroker(),
216 controller_,
217 GetRandomPointInScene());
218 cmd->SetCenter(GetRandomPointInScene());
219 cmd->SetSide2End(GetRandomPointInScene());
220 controller_->PushCommand(cmd);
221 }
222 break;
223 }
224 }
225
156 void TrackerSampleApp::HandleApplicationEvent( 226 void TrackerSampleApp::HandleApplicationEvent(
157 const SDL_Event & event) 227 const SDL_Event & event)
158 { 228 {
159 DisplayInfoText(); 229 DisplayInfoText();
160
161 // we seed the random number generator for random measure tool
162 // generation
163 srand(42);
164 230
165 if (event.type == SDL_MOUSEMOTION) 231 if (event.type == SDL_MOUSEMOTION)
166 { 232 {
167 int scancodeCount = 0; 233 int scancodeCount = 0;
168 const uint8_t* keyboardState = SDL_GetKeyboardState(&scancodeCount); 234 const uint8_t* keyboardState = SDL_GetKeyboardState(&scancodeCount);
190 e.AddPosition(compositor_->GetPixelCenterCoordinates( 256 e.AddPosition(compositor_->GetPixelCenterCoordinates(
191 event.button.x, event.button.y)); 257 event.button.x, event.button.y));
192 258
193 //LOG(TRACE) << "event.button.x = " << event.button.x << " " << 259 //LOG(TRACE) << "event.button.x = " << event.button.x << " " <<
194 // "event.button.y = " << event.button.y; 260 // "event.button.y = " << event.button.y;
195 //LOG(TRACE) << "activeTracker_->PointerMove(e); " << 261 LOG(TRACE) << "activeTracker_->PointerMove(e); " <<
196 // e.GetMainPosition().GetX() << " " << e.GetMainPosition().GetY(); 262 e.GetMainPosition().GetX() << " " << e.GetMainPosition().GetY();
197 263
198 activeTracker_->PointerMove(e); 264 activeTracker_->PointerMove(e);
199 if (!activeTracker_->IsAlive()) 265 if (!activeTracker_->IsAlive())
200 activeTracker_.reset(); 266 activeTracker_.reset();
201 } 267 }
252 " is taking place"; 318 " is taking place";
253 } 319 }
254 break; 320 break;
255 321
256 case SDLK_m: 322 case SDLK_m:
257 // let's create a random measuring tool 323 CreateRandomMeasureTool();
258 324 break;
259
260
261 case SDLK_s: 325 case SDLK_s:
262 controller_->FitContent(compositor_->GetCanvasWidth(), 326 controller_->FitContent(compositor_->GetCanvasWidth(),
263 compositor_->GetCanvasHeight()); 327 compositor_->GetCanvasHeight());
264 break; 328 break;
265 329
266 case SDLK_z: 330 case SDLK_z:
267 LOG(INFO) << "SDLK_z has been pressed. event.key.keysym.mod == " << event.key.keysym.mod; 331 LOG(TRACE) << "SDLK_z has been pressed. event.key.keysym.mod == " << event.key.keysym.mod;
268 if (event.key.keysym.mod & KMOD_CTRL) 332 if (event.key.keysym.mod & KMOD_CTRL)
269 { 333 {
270 if (controller_->CanUndo()) 334 if (controller_->CanUndo())
271 { 335 {
272 LOG(INFO) << "Undoing..."; 336 LOG(TRACE) << "Undoing...";
273 controller_->Undo(); 337 controller_->Undo();
274 } 338 }
275 else 339 else
276 { 340 {
277 LOG(WARNING) << "Nothing to undo!!!"; 341 LOG(WARNING) << "Nothing to undo!!!";
278 } 342 }
279 } 343 }
280 break; 344 break;
281 345
282 case SDLK_y: 346 case SDLK_y:
283 LOG(INFO) << "SDLK_y has been pressed. event.key.keysym.mod == " << event.key.keysym.mod; 347 LOG(TRACE) << "SDLK_y has been pressed. event.key.keysym.mod == " << event.key.keysym.mod;
284 if (event.key.keysym.mod & KMOD_CTRL) 348 if (event.key.keysym.mod & KMOD_CTRL)
285 { 349 {
286 if (controller_->CanRedo()) 350 if (controller_->CanRedo())
287 { 351 {
288 LOG(INFO) << "Redoing..."; 352 LOG(TRACE) << "Redoing...";
289 controller_->Redo(); 353 controller_->Redo();
290 } 354 }
291 else 355 else
292 { 356 {
293 LOG(WARNING) << "Nothing to redo!!!"; 357 LOG(WARNING) << "Nothing to redo!!!";