Index: tools/viewer/Viewer.cpp |
diff --git a/tools/viewer/Viewer.cpp b/tools/viewer/Viewer.cpp |
index d492cdeefeb854246b90d5e721cfa43c28662cb9..82c28ae9b1843b46fda2de7da5e1842d116fd7d8 100644 |
--- a/tools/viewer/Viewer.cpp |
+++ b/tools/viewer/Viewer.cpp |
@@ -190,6 +190,9 @@ void Viewer::updateTitle() { |
} |
void Viewer::setupCurrentSlide(int previousSlide) { |
+ fGesture.reset(); |
+ fOffsetMatrix.reset(); |
+ fPreTouchMatrix.reset(); |
this->updateTitle(); |
fSlides[fCurrentSlide]->load(); |
if (previousSlide >= 0) { |
@@ -230,8 +233,44 @@ SkMatrix Viewer::computeMatrix() { |
m.postTranslate(cx, cy); |
} |
- m.preConcat(fGesture.localM()); |
- m.preConcat(fGesture.globalM()); |
+ SkMatrix touchMatrixWithOffset; |
djsollen
2016/05/19 17:04:23
I think you can do this computation by just applyi
liyuqian
2016/05/19 19:23:15
Done.
|
+ touchMatrixWithOffset.reset(); |
+ touchMatrixWithOffset.postConcat(fGesture.globalM()); |
+ touchMatrixWithOffset.postConcat(fGesture.localM()); |
+ if (touchMatrixWithOffset.isIdentity()) { |
+ // The touch gesture has reset the matrix, so we reset the offset too. |
+ fOffsetMatrix.reset(); |
+ } |
+ touchMatrixWithOffset.preConcat(fOffsetMatrix); |
+ |
+ // Begin checking if more offset is needed |
+ |
+ const SkScalar scaleX = touchMatrixWithOffset.getScaleX(); |
+ const SkScalar scaleY = touchMatrixWithOffset.getScaleY(); |
+ // Bring the X/Y translation to the slide's scale |
+ const SkScalar tX = touchMatrixWithOffset.getTranslateX() / scaleX; |
+ const SkScalar tY = touchMatrixWithOffset.getTranslateY() / scaleY; |
+ |
+ const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions(); |
+ const SkScalar SW = slideSize.width(), SH = slideSize.height(), ZERO = 0; |
+ |
+ // Bring contentRect's widht/height to the slide's scale |
+ const SkRect contentRect = fWindow->getContentRect(); |
djsollen
2016/05/19 17:04:23
on windows that don't return true for supportsCont
liyuqian
2016/05/19 19:23:15
Done.
|
+ const SkScalar CW = contentRect.width() / fPreTouchMatrix.getScaleX() / scaleX; |
+ const SkScalar CH = contentRect.height() / fPreTouchMatrix.getScaleY() / scaleY; |
+ |
+ // X Translation could be no less than -SW and no more than CW |
+ const SkScalar overX = std::min(ZERO, tX + SW) + std::max(ZERO, tX - CW); |
+ // Y Translation is similar to the X translation |
+ const SkScalar overY = std::min(ZERO, tY + SH) + std::max(ZERO, tY - CH); |
+ |
+ // Apply additional offset |
+ fOffsetMatrix.preTranslate(-overX * scaleX, -overY * scaleY); |
+ touchMatrixWithOffset.preTranslate(-overX * scaleX, -overY * scaleY); |
+ |
+ // Finished checking offset |
+ |
+ m.preConcat(touchMatrixWithOffset); |
return m; |
} |
@@ -252,8 +291,9 @@ void Viewer::onPaint(SkCanvas* canvas) { |
const SkISize slideSize = fSlides[fCurrentSlide]->getDimensions(); |
const SkRect slideBounds = SkRect::MakeIWH(slideSize.width(), slideSize.height()); |
SkMatrix matrix; |
- matrix.setRectToRect(slideBounds, contentRect, SkMatrix::kCenter_ScaleToFit); |
+ matrix.setRectToRect(slideBounds, contentRect, SkMatrix::kStart_ScaleToFit); |
canvas->concat(matrix); |
+ fPreTouchMatrix = matrix; |
} |
canvas->concat(computeMatrix()); |
@@ -268,17 +308,19 @@ void Viewer::onPaint(SkCanvas* canvas) { |
bool Viewer::onTouch(int owner, Window::InputState state, float x, float y) { |
void* castedOwner = reinterpret_cast<void*>(owner); |
+ float castedX = (x - fPreTouchMatrix.getTranslateX()) / fPreTouchMatrix.getScaleX(); |
+ float castedY = (y - fPreTouchMatrix.getTranslateY()) / fPreTouchMatrix.getScaleY(); |
djsollen
2016/05/19 17:04:23
SkPoint touchPoint = SkPoint::Make(x, y);
SkMatrix
liyuqian
2016/05/19 19:23:15
Done.
|
switch (state) { |
case Window::kUp_InputState: { |
fGesture.touchEnd(castedOwner); |
break; |
} |
case Window::kDown_InputState: { |
- fGesture.touchBegin(castedOwner, x, y); |
+ fGesture.touchBegin(castedOwner, castedX, castedY); |
break; |
} |
case Window::kMove_InputState: { |
- fGesture.touchMoved(castedOwner, x, y); |
+ fGesture.touchMoved(castedOwner, castedX, castedY); |
break; |
} |
} |