1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 10:49:24 +02:00

refactoring: scrolling direction is now calculated inside an animation provider

This commit is contained in:
Nikolay Pultsin 2011-04-13 23:27:15 +01:00
parent 744934e3b7
commit 91906854da
9 changed files with 204 additions and 108 deletions

View file

@ -151,10 +151,15 @@ public final class FBView extends ZLTextView {
}
private void startManualScrolling(int x, int y) {
if (isFlickScrollingEnabled()) {
if (!isFlickScrollingEnabled()) {
return;
}
myStartX = x;
myStartY = y;
}
final boolean horizontal = ScrollingPreferences.Instance().HorizontalOption.getValue();
final Direction direction = horizontal ? Direction.rightToLeft : Direction.up;
myReader.getViewWidget().startManualScrolling(x, y, direction);
}
public boolean onFingerMove(int x, int y) {
@ -175,9 +180,7 @@ public final class FBView extends ZLTextView {
}
if (isFlickScrollingEnabled()) {
final boolean horizontal = ScrollingPreferences.Instance().HorizontalOption.getValue();
final Direction direction = horizontal ? Direction.rightToLeft : Direction.up;
myReader.getViewWidget().scrollManually(myStartX, myStartY, x, y, direction);
myReader.getViewWidget().scrollManuallyTo(x, y);
}
}
return true;
@ -193,11 +196,7 @@ public final class FBView extends ZLTextView {
final int minDiff = horizontal ?
(w > h ? w / 4 : w / 3) :
(h > w ? h / 4 : h / 3);
final PageIndex pageIndex =
Math.abs(diff) < minDiff
? PageIndex.current
: (diff < 0 ? PageIndex.next : PageIndex.previous);
myReader.getViewWidget().startAutoScrolling(pageIndex, direction, x, y, animationSpeed);
myReader.getViewWidget().startAutoScrolling(x, y, Math.abs(diff) > minDiff, animationSpeed);
}
public boolean onFingerRelease(int x, int y) {

View file

@ -45,7 +45,6 @@ class TurnPageAction extends FBAction {
myForward ? FBView.PageIndex.next : FBView.PageIndex.previous,
preferences.HorizontalOption.getValue()
? FBView.Direction.rightToLeft : FBView.Direction.up,
null, null,
preferences.AnimationSpeedOption.getValue()
);
}
@ -54,9 +53,9 @@ class TurnPageAction extends FBAction {
final ScrollingPreferences preferences = ScrollingPreferences.Instance();
Reader.getViewWidget().startAutoScrolling(
myForward ? FBView.PageIndex.next : FBView.PageIndex.previous,
x, y,
preferences.HorizontalOption.getValue()
? FBView.Direction.rightToLeft : FBView.Direction.up,
x, y,
preferences.AnimationSpeedOption.getValue()
);
}

View file

@ -45,7 +45,6 @@ class VolumeKeyTurnPageAction extends FBAction {
forward ? FBView.PageIndex.next : FBView.PageIndex.previous,
preferences.HorizontalOption.getValue()
? FBView.Direction.rightToLeft : FBView.Direction.up,
null, null,
preferences.AnimationSpeedOption.getValue()
);
}

View file

@ -23,6 +23,9 @@ public interface ZLViewWidget {
void reset();
void repaint();
void scrollManually(int startX, int startY, int endX, int endY, ZLView.Direction direction);
void startAutoScrolling(ZLView.PageIndex pageIndex, ZLView.Direction direction, Integer x, Integer y, int speed);
void startManualScrolling(int x, int y, ZLView.Direction direction);
void scrollManuallyTo(int x, int y);
void startAutoScrolling(ZLView.PageIndex pageIndex, int x, int y, ZLView.Direction direction, int speed);
void startAutoScrolling(ZLView.PageIndex pageIndex, ZLView.Direction direction, int speed);
void startAutoScrolling(int x, int y, boolean forward, int speed);
}

View file

@ -60,15 +60,18 @@ abstract class AnimationProvider {
return myMode;
}
void terminate() {
final void terminate() {
myMode = Mode.NoScrolling;
mySpeed = 0;
myDrawInfos.clear();
}
void startManualScrolling(int x, int y, ZLView.Direction direction, int w, int h) {
final void startManualScrolling(int x, int y) {
if (!myMode.Auto) {
myMode = Mode.ManualScrolling;
setup(x, y, direction, w, h);
myEndX = myStartX = x;
myEndY = myStartY = y;
}
}
void scrollTo(int x, int y) {
@ -78,18 +81,27 @@ abstract class AnimationProvider {
}
}
final void startAutoScrolling(boolean forward, float startSpeed, ZLView.Direction direction, int w, int h, Integer x, Integer y, int speed) {
if (myDrawInfos.size() <= 1) {
startSpeed *= 5;
} else {
void startAutoScrolling(int x, int y, boolean forward, int speed) {
if (myMode != Mode.ManualScrolling) {
return;
}
if (getPageToScrollTo(x, y) == ZLView.PageIndex.current) {
return;
}
myMode = forward ? Mode.AutoScrollingForward : Mode.AutoScrollingBackward;
float velocity = 15;
if (myDrawInfos.size() > 1) {
int duration = 0;
for (DrawInfo info : myDrawInfos) {
duration += info.Duration;
}
duration /= myDrawInfos.size();
final long time = System.currentTimeMillis();
myDrawInfos.add(new DrawInfo(myEndX, myEndY, time, time + duration));
float velocity = 0;
myDrawInfos.add(new DrawInfo(x, y, time, time + duration));
velocity = 0;
for (int i = 1; i < myDrawInfos.size(); ++i) {
final DrawInfo info0 = myDrawInfos.get(i - 1);
final DrawInfo info1 = myDrawInfos.get(i);
@ -100,32 +112,48 @@ abstract class AnimationProvider {
velocity /= myDrawInfos.size() - 1;
velocity *= duration;
velocity = Math.min(100, Math.max(15, velocity));
startSpeed = startSpeed > 0 ? velocity : -velocity;
}
myDrawInfos.clear();
startAutoScrollingInternal(forward, startSpeed, direction, w, h, x, y, speed);
if (getPageToScrollTo() == ZLView.PageIndex.previous) {
forward = !forward;
}
protected void startAutoScrollingInternal(boolean forward, float startSpeed, ZLView.Direction direction, int w, int h, Integer x, Integer y, int speed) {
if (!inProgress()) {
if (x == null || y == null) {
if (direction.IsHorizontal) {
x = speed < 0 ? w : 0;
y = 0;
} else {
x = 0;
y = speed < 0 ? h : 0;
}
}
setup(x, y, direction, w, h);
switch (myDirection) {
case up:
case rightToLeft:
mySpeed = forward ? -velocity : velocity;
break;
case leftToRight:
case down:
mySpeed = forward ? velocity : -velocity;
break;
}
myMode = forward
? Mode.AutoScrollingForward
: Mode.AutoScrollingBackward;
mySpeed = startSpeed;
startAutoScrollingInternal(speed);
}
public void startAutoScrolling(ZLView.PageIndex pageIndex, Integer x, Integer y, int speed) {
terminate();
myMode = Mode.AutoScrollingForward;
switch (myDirection) {
case up:
case rightToLeft:
mySpeed = pageIndex == ZLView.PageIndex.next ? -15 : 15;
break;
case leftToRight:
case down:
mySpeed = pageIndex == ZLView.PageIndex.next ? 15 : -15;
break;
}
setupAutoScrollingStart(x, y);
startAutoScrollingInternal(speed);
}
protected abstract void startAutoScrollingInternal(int speed);
protected abstract void setupAutoScrollingStart(Integer x, Integer y);
boolean inProgress() {
return myMode != Mode.NoScrolling;
}
@ -134,11 +162,7 @@ abstract class AnimationProvider {
return myDirection.IsHorizontal ? myEndX - myStartX : myEndY - myStartY;
}
private void setup(int x, int y, ZLView.Direction direction, int width, int height) {
myStartX = x;
myStartY = y;
myEndX = x;
myEndY = y;
final void setup(ZLView.Direction direction, int width, int height) {
myDirection = direction;
myWidth = width;
myHeight = height;
@ -177,7 +201,11 @@ abstract class AnimationProvider {
protected abstract void drawInternal(Canvas canvas);
abstract ZLView.PageIndex getPageToScrollTo();
abstract ZLView.PageIndex getPageToScrollTo(int x, int y);
final ZLView.PageIndex getPageToScrollTo() {
return getPageToScrollTo(myEndX, myEndY);
}
protected Bitmap getBitmapFrom() {
return myBitmapManager.getBitmap(ZLView.PageIndex.current);

View file

@ -33,7 +33,7 @@ class CurlAnimationProvider extends AnimationProvider {
final Path myEdgePath = new Path();
final Path myQuadPath = new Path();
private float mySpeedFactor;
private float mySpeedFactor = 1;
CurlAnimationProvider(BitmapManager bitmapManager) {
super(bitmapManager);
@ -196,7 +196,7 @@ class CurlAnimationProvider extends AnimationProvider {
}
@Override
ZLView.PageIndex getPageToScrollTo() {
ZLView.PageIndex getPageToScrollTo(int x, int y) {
if (myDirection == null) {
return ZLView.PageIndex.current;
}
@ -215,21 +215,28 @@ class CurlAnimationProvider extends AnimationProvider {
}
@Override
protected void startAutoScrollingInternal(boolean forward, float startSpeed, ZLView.Direction direction, int w, int h, Integer x, Integer y, int speed) {
protected void startAutoScrollingInternal(int speed) {
mySpeedFactor = (float)Math.pow(2.0, 0.25 * speed);
mySpeed *= 1.5;
doStep();
}
@Override
protected void setupAutoScrollingStart(Integer x, Integer y) {
if (x == null || y == null) {
if (direction.IsHorizontal) {
x = startSpeed < 0 ? w - 3 : 3;
if (myDirection.IsHorizontal) {
x = mySpeed < 0 ? myWidth - 3 : 3;
y = 1;
} else {
x = 1;
y = startSpeed < 0 ? h - 3 : 3;
y = mySpeed < 0 ? myHeight - 3 : 3;
}
} else {
final int cornerX = x > w / 2 ? w : 0;
final int cornerY = y > h / 2 ? h : 0;
int deltaX = Math.min(Math.abs(x - cornerX), w / 5);
int deltaY = Math.min(Math.abs(y - cornerY), h / 5);
if (direction.IsHorizontal) {
final int cornerX = x > myWidth / 2 ? myWidth : 0;
final int cornerY = y > myHeight / 2 ? myHeight : 0;
int deltaX = Math.min(Math.abs(x - cornerX), myWidth / 5);
int deltaY = Math.min(Math.abs(y - cornerY), myHeight / 5);
if (myDirection.IsHorizontal) {
deltaY = Math.min(deltaY, deltaX / 3);
} else {
deltaX = Math.min(deltaX, deltaY / 3);
@ -237,10 +244,8 @@ class CurlAnimationProvider extends AnimationProvider {
x = Math.abs(cornerX - deltaX);
y = Math.abs(cornerY - deltaY);
}
super.startAutoScrollingInternal(forward, startSpeed, direction, w, h, x, y, speed);
mySpeedFactor = (float)Math.pow(2.0, 0.25 * speed);
mySpeed *= 1.5;
doStep();
myEndX = myStartX = x;
myEndY = myStartY = y;
}
@Override

View file

@ -42,27 +42,39 @@ class NoneAnimationProvider extends AnimationProvider {
}
}
private ZLView.PageIndex myPageToScrollTo = ZLView.PageIndex.current;
@Override
protected void setupAutoScrollingStart(Integer x, Integer y) {
if (myDirection.IsHorizontal) {
myStartX = mySpeed < 0 ? myWidth : 0;
myEndX = myWidth - myStartX;
myEndY = myStartY = 0;
} else {
myEndX = myStartX = 0;
myStartY = mySpeed < 0 ? myHeight : 0;
myEndY = myHeight - myStartY;
}
}
@Override
protected void startAutoScrollingInternal(boolean forward, float startSpeed, ZLView.Direction direction, int w, int h, Integer x, Integer y, int speed) {
super.startAutoScrollingInternal(forward, startSpeed, direction, w, h, x, y, speed);
switch (direction) {
protected void startAutoScrollingInternal(int speed) {
}
@Override
ZLView.PageIndex getPageToScrollTo(int x, int y) {
if (myDirection == null) {
return ZLView.PageIndex.current;
}
switch (myDirection) {
case rightToLeft:
case up:
myPageToScrollTo =
startSpeed > 0 ? ZLView.PageIndex.previous : ZLView.PageIndex.next;
break;
return myStartX < x ? ZLView.PageIndex.previous : ZLView.PageIndex.next;
case leftToRight:
return myStartX < x ? ZLView.PageIndex.next : ZLView.PageIndex.previous;
case up:
return myStartY < y ? ZLView.PageIndex.previous : ZLView.PageIndex.next;
case down:
myPageToScrollTo =
startSpeed > 0 ? ZLView.PageIndex.next : ZLView.PageIndex.previous;
break;
}
}
@Override
ZLView.PageIndex getPageToScrollTo() {
return myPageToScrollTo;
return myStartY < y ? ZLView.PageIndex.next : ZLView.PageIndex.previous;
}
return ZLView.PageIndex.current;
}
}

View file

@ -29,27 +29,41 @@ abstract class SimpleAnimationProvider extends AnimationProvider {
}
@Override
ZLView.PageIndex getPageToScrollTo() {
ZLView.PageIndex getPageToScrollTo(int x, int y) {
if (myDirection == null) {
return ZLView.PageIndex.current;
}
switch (myDirection) {
case rightToLeft:
return myStartX < myEndX ? ZLView.PageIndex.previous : ZLView.PageIndex.next;
return myStartX < x ? ZLView.PageIndex.previous : ZLView.PageIndex.next;
case leftToRight:
return myStartX < myEndX ? ZLView.PageIndex.next : ZLView.PageIndex.previous;
return myStartX < x ? ZLView.PageIndex.next : ZLView.PageIndex.previous;
case up:
return myStartY < myEndY ? ZLView.PageIndex.previous : ZLView.PageIndex.next;
return myStartY < y ? ZLView.PageIndex.previous : ZLView.PageIndex.next;
case down:
return myStartY < myEndY ? ZLView.PageIndex.next : ZLView.PageIndex.previous;
return myStartY < y ? ZLView.PageIndex.next : ZLView.PageIndex.previous;
}
return ZLView.PageIndex.current;
}
@Override
protected void startAutoScrollingInternal(boolean forward, float startSpeed, ZLView.Direction direction, int w, int h, Integer x, Integer y, int speed) {
super.startAutoScrollingInternal(forward, startSpeed, direction, w, h, x, y, speed);
protected void setupAutoScrollingStart(Integer x, Integer y) {
if (x == null || y == null) {
if (myDirection.IsHorizontal) {
x = mySpeed < 0 ? myWidth : 0;
y = 0;
} else {
x = 0;
y = mySpeed < 0 ? myHeight : 0;
}
}
myEndX = myStartX = x;
myEndY = myStartY = y;
}
@Override
protected void startAutoScrollingInternal(int speed) {
mySpeedFactor = (float)Math.pow(1.5, 0.25 * speed);
doStep();
}

View file

@ -62,9 +62,9 @@ public class ZLAndroidWidget extends View implements ZLViewWidget, View.OnLongCl
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
getAnimationProvider().terminate();
if (myScreenIsTouched) {
final ZLView view = ZLApplication.Instance().getCurrentView();
getAnimationProvider().terminate();
myScreenIsTouched = false;
view.onScrollingFinished(ZLView.PageIndex.current);
}
@ -157,32 +157,68 @@ public class ZLAndroidWidget extends View implements ZLViewWidget, View.OnLongCl
postInvalidate();
}
public void scrollManually(int startX, int startY, int endX, int endY, ZLView.Direction direction) {
final ZLView view = ZLApplication.Instance().getCurrentView();
final int diff = direction.IsHorizontal ? endX - startX : endY - startY;
if (!view.canScroll(diff >= 0 ? ZLView.PageIndex.previous : ZLView.PageIndex.next)) {
return;
public void startManualScrolling(int x, int y, ZLView.Direction direction) {
final AnimationProvider animator = getAnimationProvider();
animator.setup(direction, getWidth(), getMainAreaHeight());
animator.startManualScrolling(x, y);
}
public void scrollManuallyTo(int x, int y) {
final ZLView view = ZLApplication.Instance().getCurrentView();
final AnimationProvider animator = getAnimationProvider();
if (!animator.inProgress()) {
animator.startManualScrolling(
startX, startY,
direction,
getWidth(), getMainAreaHeight()
);
}
animator.scrollTo(endX, endY);
if (view.canScroll(animator.getPageToScrollTo(x, y))) {
animator.scrollTo(x, y);
postInvalidate();
}
}
public void startAutoScrolling(ZLView.PageIndex pageIndex, int x, int y, ZLView.Direction direction, int speed) {
final ZLView view = ZLApplication.Instance().getCurrentView();
if (pageIndex == ZLView.PageIndex.current || !view.canScroll(pageIndex)) {
return;
}
final AnimationProvider animator = getAnimationProvider();
animator.setup(direction, getWidth(), getMainAreaHeight());
animator.startAutoScrolling(pageIndex, x, y, speed);
if (animator.getMode().Auto) {
postInvalidate();
}
}
public void startAutoScrolling(ZLView.PageIndex pageIndex, ZLView.Direction direction, int speed) {
final ZLView view = ZLApplication.Instance().getCurrentView();
if (pageIndex == ZLView.PageIndex.current || !view.canScroll(pageIndex)) {
return;
}
final AnimationProvider animator = getAnimationProvider();
animator.setup(direction, getWidth(), getMainAreaHeight());
animator.startAutoScrolling(pageIndex, null, null, speed);
if (animator.getMode().Auto) {
postInvalidate();
}
}
public void startAutoScrolling(int x, int y, boolean forward, int speed) {
final ZLView view = ZLApplication.Instance().getCurrentView();
final AnimationProvider animator = getAnimationProvider();
if (!view.canScroll(animator.getPageToScrollTo(x, y))) {
animator.terminate();
return;
}
animator.startAutoScrolling(x, y, forward, speed);
if (animator.getMode().Auto) {
postInvalidate();
}
}
/*
public void startAutoScrolling(ZLView.PageIndex pageIndex, ZLView.Direction direction, Integer x, Integer y, int speed) {
final ZLView view = ZLApplication.Instance().getCurrentView();
final AnimationProvider animator = getAnimationProvider();
final int w = getWidth();
final int h = getMainAreaHeight();
if (!view.canScroll(pageIndex)) {
if (animator.getMode().Auto || !view.canScroll(pageIndex)) {
return;
}
@ -209,6 +245,7 @@ public class ZLAndroidWidget extends View implements ZLViewWidget, View.OnLongCl
}
postInvalidate();
}
*/
void drawOnBitmap(Bitmap bitmap, ZLView.PageIndex index) {
final ZLView view = ZLApplication.Instance().getCurrentView();