comparison Framework/Scene2D/ZoomSceneTracker.cpp @ 596:b716763571ad

IPointerTracker
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 26 Apr 2019 17:05:14 +0200
parents
children 7efa2543699d
comparison
equal deleted inserted replaced
595:6e471e6cf09b 596:b716763571ad
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 #include "ZoomSceneTracker.h"
23
24 namespace OrthancStone
25 {
26 ZoomSceneTracker::ZoomSceneTracker(Scene2D& scene,
27 const PointerEvent& event,
28 unsigned int canvasWidth,
29 unsigned int canvasHeight) :
30 scene_(scene),
31 clickY_(event.GetMainPosition().GetY()),
32 aligner_(scene, event.GetMainPosition(), canvasWidth, canvasHeight),
33 originalSceneToCanvas_(scene.GetSceneToCanvasTransform())
34 {
35 if (canvasHeight <= 3)
36 {
37 active_ = false;
38 }
39 else
40 {
41 normalization_ = 1.0 / static_cast<double>(canvasHeight - 1);
42 active_ = true;
43 }
44 }
45
46
47 void ZoomSceneTracker::Update(const PointerEvent& event)
48 {
49 static const double MIN_ZOOM = -4;
50 static const double MAX_ZOOM = 4;
51
52 if (active_)
53 {
54 double y = event.GetMainPosition().GetY();
55 double dy = static_cast<double>(y - clickY_) * normalization_; // In the range [-1,1]
56 double z;
57
58 // Linear interpolation from [-1, 1] to [MIN_ZOOM, MAX_ZOOM]
59 if (dy < -1.0)
60 {
61 z = MIN_ZOOM;
62 }
63 else if (dy > 1.0)
64 {
65 z = MAX_ZOOM;
66 }
67 else
68 {
69 z = MIN_ZOOM + (MAX_ZOOM - MIN_ZOOM) * (dy + 1.0) / 2.0;
70 }
71
72 double zoom = pow(2.0, z);
73
74 scene_.SetSceneToCanvasTransform(
75 AffineTransform2D::Combine(
76 AffineTransform2D::CreateScaling(zoom, zoom),
77 originalSceneToCanvas_));
78
79 aligner_.Apply();
80 }
81 }
82 }