Mercurial > hg > orthanc-stone
comparison Framework/Widgets/WorldSceneWidget.cpp @ 53:c2dc924f1a63 wasm
removing threading out of the framework
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 27 Apr 2017 16:57:49 +0200 |
parents | 25befef48c35 |
children | 01aa453d4d5b |
comparison
equal
deleted
inserted
replaced
52:37e504582af6 | 53:c2dc924f1a63 |
---|---|
127 int y) : | 127 int y) : |
128 that_(that), | 128 that_(that), |
129 downX_(x), | 129 downX_(x), |
130 downY_(y) | 130 downY_(y) |
131 { | 131 { |
132 SharedValue<ViewportGeometry>::Locker locker(that_.view_); | 132 that_.view_.GetPan(previousPanX_, previousPanY_); |
133 locker.GetValue().GetPan(previousPanX_, previousPanY_); | |
134 } | 133 } |
135 | 134 |
136 virtual void Render(Orthanc::ImageAccessor& surface) | 135 virtual void Render(Orthanc::ImageAccessor& surface) |
137 { | 136 { |
138 } | 137 } |
142 } | 141 } |
143 | 142 |
144 virtual void MouseMove(int x, | 143 virtual void MouseMove(int x, |
145 int y) | 144 int y) |
146 { | 145 { |
147 SharedValue<ViewportGeometry>::Locker locker(that_.view_); | 146 that_.view_.SetPan(previousPanX_ + x - downX_, |
148 locker.GetValue().SetPan(previousPanX_ + x - downX_, | 147 previousPanY_ + y - downY_); |
149 previousPanY_ + y - downY_); | 148 |
150 | 149 ViewChangeFunctor functor(that_.view_); |
151 ViewChangeFunctor functor(locker.GetValue()); | |
152 that_.observers_.Notify(&that_, functor); | 150 that_.observers_.Notify(&that_, functor); |
153 } | 151 } |
154 }; | 152 }; |
155 | 153 |
156 | 154 |
170 int y) : | 168 int y) : |
171 that_(that), | 169 that_(that), |
172 downX_(x), | 170 downX_(x), |
173 downY_(y) | 171 downY_(y) |
174 { | 172 { |
175 SharedValue<ViewportGeometry>::Locker locker(that_.view_); | 173 oldZoom_ = that_.view_.GetZoom(); |
176 oldZoom_ = locker.GetValue().GetZoom(); | 174 MapMouseToScene(centerX_, centerY_, that_.view_, downX_, downY_); |
177 MapMouseToScene(centerX_, centerY_, locker.GetValue(), downX_, downY_); | |
178 } | 175 } |
179 | 176 |
180 virtual void Render(Orthanc::ImageAccessor& surface) | 177 virtual void Render(Orthanc::ImageAccessor& surface) |
181 { | 178 { |
182 } | 179 } |
189 int y) | 186 int y) |
190 { | 187 { |
191 static const double MIN_ZOOM = -4; | 188 static const double MIN_ZOOM = -4; |
192 static const double MAX_ZOOM = 4; | 189 static const double MAX_ZOOM = 4; |
193 | 190 |
194 SharedValue<ViewportGeometry>::Locker locker(that_.view_); | 191 if (that_.view_.GetDisplayHeight() <= 3) |
195 | |
196 if (locker.GetValue().GetDisplayHeight() <= 3) | |
197 { | 192 { |
198 return; // Cannot zoom on such a small image | 193 return; // Cannot zoom on such a small image |
199 } | 194 } |
200 | 195 |
201 double dy = (static_cast<double>(y - downY_) / | 196 double dy = (static_cast<double>(y - downY_) / |
202 static_cast<double>(locker.GetValue().GetDisplayHeight() - 1)); // In the range [-1,1] | 197 static_cast<double>(that_.view_.GetDisplayHeight() - 1)); // In the range [-1,1] |
203 double z; | 198 double z; |
204 | 199 |
205 // Linear interpolation from [-1, 1] to [MIN_ZOOM, MAX_ZOOM] | 200 // Linear interpolation from [-1, 1] to [MIN_ZOOM, MAX_ZOOM] |
206 if (dy < -1.0) | 201 if (dy < -1.0) |
207 { | 202 { |
216 z = MIN_ZOOM + (MAX_ZOOM - MIN_ZOOM) * (dy + 1.0) / 2.0; | 211 z = MIN_ZOOM + (MAX_ZOOM - MIN_ZOOM) * (dy + 1.0) / 2.0; |
217 } | 212 } |
218 | 213 |
219 z = pow(2.0, z); | 214 z = pow(2.0, z); |
220 | 215 |
221 locker.GetValue().SetZoom(oldZoom_ * z); | 216 that_.view_.SetZoom(oldZoom_ * z); |
222 | 217 |
223 // Correct the pan so that the original click point is kept at | 218 // Correct the pan so that the original click point is kept at |
224 // the same location on the display | 219 // the same location on the display |
225 double panX, panY; | 220 double panX, panY; |
226 locker.GetValue().GetPan(panX, panY); | 221 that_.view_.GetPan(panX, panY); |
227 | 222 |
228 int tx, ty; | 223 int tx, ty; |
229 locker.GetValue().MapSceneToDisplay(tx, ty, centerX_, centerY_); | 224 that_.view_.MapSceneToDisplay(tx, ty, centerX_, centerY_); |
230 locker.GetValue().SetPan(panX + static_cast<double>(downX_ - tx), | 225 that_.view_.SetPan(panX + static_cast<double>(downX_ - tx), |
231 panY + static_cast<double>(downY_ - ty)); | 226 panY + static_cast<double>(downY_ - ty)); |
232 | 227 |
233 ViewChangeFunctor functor(locker.GetValue()); | 228 ViewChangeFunctor functor(that_.view_); |
234 that_.observers_.Notify(&that_, functor); | 229 that_.observers_.Notify(&that_, functor); |
235 } | 230 } |
236 }; | 231 }; |
237 | 232 |
238 | 233 |
239 bool WorldSceneWidget::RenderCairo(CairoContext& context) | 234 bool WorldSceneWidget::RenderCairo(CairoContext& context) |
240 { | 235 { |
241 ViewportGeometry view; | 236 view_.ApplyTransform(context); |
242 | 237 return RenderScene(context, view_); |
243 { | |
244 SharedValue<ViewportGeometry>::Locker locker(view_); | |
245 view = locker.GetValue(); | |
246 } | |
247 | |
248 view.ApplyTransform(context); | |
249 | |
250 return RenderScene(context, view); | |
251 } | 238 } |
252 | 239 |
253 | 240 |
254 void WorldSceneWidget::RenderMouseOverCairo(CairoContext& context, | 241 void WorldSceneWidget::RenderMouseOverCairo(CairoContext& context, |
255 int x, | 242 int x, |
262 MapMouseToScene(sceneX, sceneY, view, x, y); | 249 MapMouseToScene(sceneX, sceneY, view, x, y); |
263 RenderSceneMouseOver(context, view, sceneX, sceneY); | 250 RenderSceneMouseOver(context, view, sceneX, sceneY); |
264 } | 251 } |
265 | 252 |
266 | 253 |
267 void WorldSceneWidget::SetSceneExtent(SharedValue<ViewportGeometry>::Locker& locker) | 254 void WorldSceneWidget::SetSceneExtent(ViewportGeometry& view) |
268 { | 255 { |
269 double x1, y1, x2, y2; | 256 double x1, y1, x2, y2; |
270 GetSceneExtent(x1, y1, x2, y2); | 257 GetSceneExtent(x1, y1, x2, y2); |
271 locker.GetValue().SetSceneExtent(x1, y1, x2, y2); | 258 view.SetSceneExtent(x1, y1, x2, y2); |
272 } | 259 } |
273 | 260 |
274 | 261 |
275 void WorldSceneWidget::SetSize(unsigned int width, | 262 void WorldSceneWidget::SetSize(unsigned int width, |
276 unsigned int height) | 263 unsigned int height) |
277 { | 264 { |
278 CairoWidget::SetSize(width, height); | 265 CairoWidget::SetSize(width, height); |
279 | 266 |
280 { | 267 view_.SetDisplaySize(width, height); |
281 SharedValue<ViewportGeometry>::Locker locker(view_); | 268 |
282 locker.GetValue().SetDisplaySize(width, height); | 269 if (observers_.IsEmpty()) |
283 | 270 { |
284 if (observers_.IsEmpty()) | 271 // Without a size observer, use the default view |
285 { | 272 view_.SetDefaultView(); |
286 // Without a size observer, use the default view | 273 } |
287 locker.GetValue().SetDefaultView(); | 274 else |
288 } | 275 { |
289 else | 276 // With a size observer, let it decide which view to use |
290 { | 277 SizeChangeFunctor functor(view_); |
291 // With a size observer, let it decide which view to use | 278 observers_.Notify(this, functor); |
292 SizeChangeFunctor functor(locker.GetValue()); | |
293 observers_.Notify(this, functor); | |
294 } | |
295 } | 279 } |
296 } | 280 } |
297 | 281 |
298 | 282 |
299 void WorldSceneWidget::SetInteractor(IWorldSceneInteractor& interactor) | 283 void WorldSceneWidget::SetInteractor(IWorldSceneInteractor& interactor) |
307 } | 291 } |
308 | 292 |
309 | 293 |
310 void WorldSceneWidget::Start() | 294 void WorldSceneWidget::Start() |
311 { | 295 { |
312 ViewportGeometry geometry; | 296 SetSceneExtent(view_); |
313 | 297 |
314 { | |
315 SharedValue<ViewportGeometry>::Locker locker(view_); | |
316 SetSceneExtent(locker); | |
317 geometry = locker.GetValue(); | |
318 } | |
319 | |
320 WidgetBase::Start(); | 298 WidgetBase::Start(); |
321 | 299 |
322 ViewChangeFunctor functor(geometry); | 300 ViewChangeFunctor functor(view_); |
323 observers_.Notify(this, functor); | 301 observers_.Notify(this, functor); |
324 } | 302 } |
325 | 303 |
326 | 304 |
327 void WorldSceneWidget::SetDefaultView() | 305 void WorldSceneWidget::SetDefaultView() |
328 { | 306 { |
329 ViewportGeometry geometry; | 307 SetSceneExtent(view_); |
330 | 308 view_.SetDefaultView(); |
331 { | |
332 SharedValue<ViewportGeometry>::Locker locker(view_); | |
333 SetSceneExtent(locker); | |
334 locker.GetValue().SetDefaultView(); | |
335 geometry = locker.GetValue(); | |
336 } | |
337 | 309 |
338 NotifyChange(); | 310 NotifyChange(); |
339 | 311 |
340 ViewChangeFunctor functor(geometry); | 312 ViewChangeFunctor functor(view_); |
341 observers_.Notify(this, functor); | 313 observers_.Notify(this, functor); |
342 } | 314 } |
343 | 315 |
344 | 316 |
345 void WorldSceneWidget::SetView(const ViewportGeometry& view) | 317 void WorldSceneWidget::SetView(const ViewportGeometry& view) |
346 { | 318 { |
347 { | 319 view_ = view; |
348 SharedValue<ViewportGeometry>::Locker locker(view_); | |
349 locker.GetValue() = view; | |
350 } | |
351 | 320 |
352 NotifyChange(); | 321 NotifyChange(); |
353 | 322 |
354 ViewChangeFunctor functor(view); | 323 ViewChangeFunctor functor(view_); |
355 observers_.Notify(this, functor); | 324 observers_.Notify(this, functor); |
356 } | 325 } |
357 | 326 |
358 | 327 |
359 ViewportGeometry WorldSceneWidget::GetView() | 328 ViewportGeometry WorldSceneWidget::GetView() |
360 { | 329 { |
361 SharedValue<ViewportGeometry>::Locker locker(view_); | 330 return view_; |
362 return locker.GetValue(); | |
363 } | 331 } |
364 | 332 |
365 | 333 |
366 IMouseTracker* WorldSceneWidget::CreateMouseTracker(MouseButton button, | 334 IMouseTracker* WorldSceneWidget::CreateMouseTracker(MouseButton button, |
367 int x, | 335 int x, |
368 int y, | 336 int y, |
369 KeyboardModifiers modifiers) | 337 KeyboardModifiers modifiers) |
370 { | 338 { |
371 ViewportGeometry view = GetView(); | |
372 | |
373 double sceneX, sceneY; | 339 double sceneX, sceneY; |
374 MapMouseToScene(sceneX, sceneY, view, x, y); | 340 MapMouseToScene(sceneX, sceneY, view_, x, y); |
375 | 341 |
376 std::auto_ptr<IWorldSceneMouseTracker> tracker(CreateMouseSceneTracker(view, button, sceneX, sceneY, modifiers)); | 342 std::auto_ptr<IWorldSceneMouseTracker> tracker |
343 (CreateMouseSceneTracker(view_, button, sceneX, sceneY, modifiers)); | |
344 | |
377 if (tracker.get() != NULL) | 345 if (tracker.get() != NULL) |
378 { | 346 { |
379 return new SceneMouseTracker(view, tracker.release()); | 347 return new SceneMouseTracker(view_, tracker.release()); |
380 } | 348 } |
381 | 349 |
382 switch (button) | 350 switch (button) |
383 { | 351 { |
384 case MouseButton_Middle: | 352 case MouseButton_Middle: |