Mercurial > hg > orthanc-stone
comparison Framework/Widgets/PanZoomMouseTracker.cpp @ 457:3b4df9925db6 am-touch-events
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Thu, 24 Jan 2019 16:42:27 +0100 |
parents | |
children | 4f2416d519b4 |
comparison
equal
deleted
inserted
replaced
456:b70fcc134ba4 | 457:3b4df9925db6 |
---|---|
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 "PanZoomMouseTracker.h" | |
23 | |
24 #include <Core/Logging.h> | |
25 #include <Core/OrthancException.h> | |
26 #include <math.h> | |
27 | |
28 namespace OrthancStone | |
29 { | |
30 Touch GetCenter(const std::vector<Touch>& touches) | |
31 { | |
32 return Touch((touches[0].x + touches[1].x) / 2.0f, (touches[0].y + touches[1].y) / 2.0f); | |
33 } | |
34 | |
35 double GetDistance(const std::vector<Touch>& touches) | |
36 { | |
37 float dx = touches[0].x - touches[1].x; | |
38 float dy = touches[0].y - touches[1].y; | |
39 return sqrt((double)(dx * dx) + (double)(dy * dy)); | |
40 } | |
41 | |
42 | |
43 PanZoomMouseTracker::PanZoomMouseTracker(WorldSceneWidget& that, | |
44 const std::vector<Touch>& startTouches) | |
45 : that_(that), | |
46 originalZoom_(that.GetView().GetZoom()) | |
47 { | |
48 that.GetView().GetPan(originalPanX_, originalPanY_); | |
49 that.GetView().MapPixelCenterToScene(originalSceneTouches_, startTouches); | |
50 | |
51 originalDisplayCenter_ = GetCenter(startTouches); | |
52 originalSceneCenter_ = GetCenter(originalSceneTouches_); | |
53 originalDisplayDistanceBetweenTouches_ = GetDistance(startTouches); | |
54 | |
55 // printf("original Pan %f %f\n", originalPanX_, originalPanY_); | |
56 // printf("original Zoom %f \n", originalZoom_); | |
57 // printf("original distance %f \n", (float)originalDisplayDistanceBetweenTouches_); | |
58 // printf("original display touches 0 %f %f\n", startTouches[0].x, startTouches[0].y); | |
59 // printf("original display touches 1 %f %f\n", startTouches[1].x, startTouches[1].y); | |
60 // printf("original Scene center %f %f\n", originalSceneCenter_.x, originalSceneCenter_.y); | |
61 | |
62 unsigned int height = that.GetView().GetDisplayHeight(); | |
63 | |
64 if (height <= 3) | |
65 { | |
66 idle_ = true; | |
67 LOG(WARNING) << "image is too small to zoom (current height = " << height << ")"; | |
68 } | |
69 else | |
70 { | |
71 idle_ = false; | |
72 normalization_ = 1.0 / static_cast<double>(height - 1); | |
73 } | |
74 | |
75 } | |
76 | |
77 | |
78 void PanZoomMouseTracker::Render(CairoContext& context, | |
79 double zoom) | |
80 { | |
81 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
82 } | |
83 | |
84 | |
85 void PanZoomMouseTracker::MouseMove(int displayX, | |
86 int displayY, | |
87 double x, | |
88 double y, | |
89 const std::vector<Touch>& displayTouches, | |
90 const std::vector<Touch>& sceneTouches) | |
91 { | |
92 ViewportGeometry view = that_.GetView(); | |
93 | |
94 // printf("Display touches 0 %f %f\n", displayTouches[0].x, displayTouches[0].y); | |
95 // printf("Display touches 1 %f %f\n", displayTouches[1].x, displayTouches[1].y); | |
96 // printf("Scene touches 0 %f %f\n", sceneTouches[0].x, sceneTouches[0].y); | |
97 // printf("Scene touches 1 %f %f\n", sceneTouches[1].x, sceneTouches[1].y); | |
98 | |
99 // printf("zoom = %f\n", view.GetZoom()); | |
100 Touch currentSceneCenter = GetCenter(sceneTouches); | |
101 double panX = originalPanX_ + (currentSceneCenter.x - originalSceneCenter_.x) * view.GetZoom(); | |
102 double panY = originalPanY_ + (currentSceneCenter.y - originalSceneCenter_.y) * view.GetZoom(); | |
103 | |
104 view.SetPan(panX, panY); | |
105 | |
106 static const double MIN_ZOOM = -4; | |
107 static const double MAX_ZOOM = 4; | |
108 | |
109 if (!idle_) | |
110 { | |
111 double currentDistanceBetweenTouches = GetDistance(displayTouches); | |
112 | |
113 double dy = static_cast<double>(currentDistanceBetweenTouches - originalDisplayDistanceBetweenTouches_) * normalization_; // In the range [-1,1] | |
114 double z; | |
115 | |
116 // Linear interpolation from [-1, 1] to [MIN_ZOOM, MAX_ZOOM] | |
117 if (dy < -1.0) | |
118 { | |
119 z = MIN_ZOOM; | |
120 } | |
121 else if (dy > 1.0) | |
122 { | |
123 z = MAX_ZOOM; | |
124 } | |
125 else | |
126 { | |
127 z = MIN_ZOOM + (MAX_ZOOM - MIN_ZOOM) * (dy + 1.0) / 2.0; | |
128 } | |
129 | |
130 z = pow(2.0, z); | |
131 | |
132 view.SetZoom(z * originalZoom_); | |
133 } | |
134 | |
135 that_.SetView(view); | |
136 } | |
137 } |