1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-04 18:29:23 +02:00

refactoring: ZLView.Direction is used instead of 'boolean horizontal'

This commit is contained in:
Nikolay Pultsin 2011-04-02 11:35:54 +01:00
parent 3074f93d8d
commit f8b653b06b
14 changed files with 84 additions and 52 deletions

View file

@ -188,7 +188,7 @@ public final class FBView extends ZLTextView {
return false; return false;
} }
if (!cursor.isStartOfParagraph() || !cursor.getParagraphCursor().isFirst()) { if (!cursor.isStartOfParagraph() || !cursor.getParagraphCursor().isFirst()) {
myReader.scrollViewManually(myStartX, myStartY, x, y, horizontal); myReader.scrollViewManually(myStartX, myStartY, x, y, Direction.rightToLeft);
} }
} else if (diff < 0) { } else if (diff < 0) {
final ZLTextWordCursor cursor = getEndCursor(); final ZLTextWordCursor cursor = getEndCursor();
@ -196,7 +196,7 @@ public final class FBView extends ZLTextView {
return false; return false;
} }
if (!cursor.isEndOfParagraph() || !cursor.getParagraphCursor().isLast()) { if (!cursor.isEndOfParagraph() || !cursor.getParagraphCursor().isLast()) {
myReader.scrollViewManually(myStartX, myStartY, x, y, horizontal); myReader.scrollViewManually(myStartX, myStartY, x, y, Direction.up);
} }
} else { } else {
myReader.scrollViewToCenter(); myReader.scrollViewToCenter();
@ -243,7 +243,7 @@ public final class FBView extends ZLTextView {
? PageIndex.current ? PageIndex.current
: (diff < 0 ? PageIndex.next : PageIndex.previous); : (diff < 0 ? PageIndex.next : PageIndex.previous);
if (getAnimationType() != Animation.none) { if (getAnimationType() != Animation.none) {
startAutoScrolling(pageIndex, horizontal); startAutoScrolling(pageIndex, horizontal ? Direction.rightToLeft : Direction.up);
} else { } else {
myReader.scrollViewToCenter(); myReader.scrollViewToCenter();
onScrollingFinished(pageIndex); onScrollingFinished(pageIndex);

View file

@ -61,6 +61,7 @@ class TurnPageAction extends FBAction {
view.startAutoScrolling( view.startAutoScrolling(
myForward ? FBView.PageIndex.next : FBView.PageIndex.previous, myForward ? FBView.PageIndex.next : FBView.PageIndex.previous,
preferences.HorizontalOption.getValue() preferences.HorizontalOption.getValue()
? FBView.Direction.rightToLeft : FBView.Direction.up
); );
} else { } else {
view.scrollPage(myForward, FBView.ScrollingMode.NO_OVERLAPPING, 0); view.scrollPage(myForward, FBView.ScrollingMode.NO_OVERLAPPING, 0);

View file

@ -59,6 +59,7 @@ class VolumeKeyTurnPageAction extends FBAction {
view.startAutoScrolling( view.startAutoScrolling(
forward ? FBView.PageIndex.next : FBView.PageIndex.previous, forward ? FBView.PageIndex.next : FBView.PageIndex.previous,
preferences.HorizontalOption.getValue() preferences.HorizontalOption.getValue()
? FBView.Direction.rightToLeft : FBView.Direction.up
); );
} else { } else {
view.scrollPage(forward, FBView.ScrollingMode.NO_OVERLAPPING, 0); view.scrollPage(forward, FBView.ScrollingMode.NO_OVERLAPPING, 0);

View file

@ -68,9 +68,9 @@ public abstract class ZLApplication {
} }
} }
public final void scrollViewManually(int startX, int startY, int endX, int endY, boolean horizontally) { public final void scrollViewManually(int startX, int startY, int endX, int endY, ZLView.Direction direction) {
if (myWindow != null) { if (myWindow != null) {
myWindow.scrollViewManually(startX, startY, endX, endY, horizontally); myWindow.scrollViewManually(startX, startY, endX, endY, direction);
} }
} }
@ -80,9 +80,9 @@ public abstract class ZLApplication {
} }
} }
public final void startViewAutoScrolling(ZLView.PageIndex pageIndex, boolean horizontally) { public final void startViewAutoScrolling(ZLView.PageIndex pageIndex, ZLView.Direction direction) {
if (myWindow != null) { if (myWindow != null) {
myWindow.startViewAutoScrolling(pageIndex, horizontally); myWindow.startViewAutoScrolling(pageIndex, direction);
} }
} }

View file

@ -36,9 +36,9 @@ abstract public class ZLApplicationWindow {
abstract protected void refreshMenu(); abstract protected void refreshMenu();
abstract protected void repaintView(); abstract protected void repaintView();
abstract protected void scrollViewManually(int startX, int startY, int endX, int endY, boolean horizontally); abstract protected void scrollViewManually(int startX, int startY, int endX, int endY, ZLView.Direction direction);
abstract protected void scrollViewToCenter(); abstract protected void scrollViewToCenter();
abstract protected void startViewAutoScrolling(ZLView.PageIndex pageIndex, boolean horizontally); abstract protected void startViewAutoScrolling(ZLView.PageIndex pageIndex, ZLView.Direction direction);
abstract protected void rotate(); abstract protected void rotate();
abstract protected boolean canRotate(); abstract protected boolean canRotate();

View file

@ -37,7 +37,17 @@ abstract public class ZLView {
current, previous, next current, previous, next
}; };
public static enum Direction { public static enum Direction {
leftToRight, rightToLeft, up, down leftToRight(true), rightToLeft(true), up(false), down(false);
private final boolean myIsHorizontal;
Direction(boolean isHorizontal) {
myIsHorizontal = isHorizontal;
}
public boolean isHorizontal() {
return myIsHorizontal;
}
}; };
public static enum Animation { public static enum Animation {
none, curl, slide, shift none, curl, slide, shift

View file

@ -199,13 +199,13 @@ public abstract class ZLTextView extends ZLTextViewBase {
myScrollingIsActive = active; myScrollingIsActive = active;
} }
public final synchronized void startAutoScrolling(PageIndex pageIndex, boolean horizontally) { public final synchronized void startAutoScrolling(PageIndex pageIndex, Direction direction) {
if (isScrollingActive()) { if (isScrollingActive()) {
return; return;
} }
setScrollingActive(true); setScrollingActive(true);
ZLApplication.Instance().startViewAutoScrolling(pageIndex, horizontally); ZLApplication.Instance().startViewAutoScrolling(pageIndex, direction);
} }
@Override @Override

View file

@ -75,10 +75,10 @@ public final class ZLAndroidApplicationWindow extends ZLApplicationWindow {
} }
@Override @Override
protected void scrollViewManually(int startX, int startY, int endX, int endY, boolean horizontally) { protected void scrollViewManually(int startX, int startY, int endX, int endY, ZLView.Direction direction) {
final ZLAndroidWidget widget = final ZLAndroidWidget widget =
((ZLAndroidLibrary)ZLAndroidLibrary.Instance()).getWidget(); ((ZLAndroidLibrary)ZLAndroidLibrary.Instance()).getWidget();
widget.scrollManually(startX, startY, endX, endY, horizontally); widget.scrollManually(startX, startY, endX, endY, direction);
} }
@Override @Override
@ -89,10 +89,10 @@ public final class ZLAndroidApplicationWindow extends ZLApplicationWindow {
} }
@Override @Override
protected void startViewAutoScrolling(ZLView.PageIndex pageIndex, boolean horizontally) { protected void startViewAutoScrolling(ZLView.PageIndex pageIndex, ZLView.Direction direction) {
final ZLAndroidWidget widget = final ZLAndroidWidget widget =
((ZLAndroidLibrary)ZLAndroidLibrary.Instance()).getWidget(); ((ZLAndroidLibrary)ZLAndroidLibrary.Instance()).getWidget();
widget.startAutoScrolling(pageIndex, horizontally); widget.startAutoScrolling(pageIndex, direction);
} }
public void rotate() { public void rotate() {

View file

@ -29,7 +29,7 @@ abstract class AnimationProvider {
protected int myStartY; protected int myStartY;
protected int myEndX; protected int myEndX;
protected int myEndY; protected int myEndY;
protected boolean myHorizontal; protected ZLView.Direction myDirection;
protected int myWidth; protected int myWidth;
protected int myHeight; protected int myHeight;
@ -39,15 +39,15 @@ abstract class AnimationProvider {
} }
int getScrollingShift() { int getScrollingShift() {
return myHorizontal ? myEndX - myStartX : myEndY - myStartY; return myDirection.isHorizontal() ? myEndX - myStartX : myEndY - myStartY;
} }
void setup(int startX, int startY, int endX, int endY, boolean horizontal, int width, int height) { void setup(int startX, int startY, int endX, int endY, ZLView.Direction direction, int width, int height) {
myStartX = startX; myStartX = startX;
myStartY = startY; myStartY = startY;
myEndX = endX; myEndX = endX;
myEndY = endY; myEndY = endY;
myHorizontal = horizontal; myDirection = direction;
myWidth = width; myWidth = width;
myHeight = height; myHeight = height;
} }

View file

@ -39,7 +39,7 @@ class CurlAnimationProvider extends AnimationProvider {
final int oppositeX = Math.abs(myWidth - cornerX); final int oppositeX = Math.abs(myWidth - cornerX);
final int oppositeY = Math.abs(myHeight - cornerY); final int oppositeY = Math.abs(myHeight - cornerY);
final int x, y; final int x, y;
if (myHorizontal) { if (myDirection.isHorizontal()) {
x = Math.max(1, Math.min(myWidth - 1, myEndX)); x = Math.max(1, Math.min(myWidth - 1, myEndX));
if (cornerY == 0) { if (cornerY == 0) {
y = Math.max(1, Math.min(myHeight / 2, myEndY)); y = Math.max(1, Math.min(myHeight / 2, myEndY));
@ -88,8 +88,16 @@ class CurlAnimationProvider extends AnimationProvider {
} }
ZLView.PageIndex getPageToScrollTo() { ZLView.PageIndex getPageToScrollTo() {
return myHorizontal switch (myDirection) {
? (myStartX < myWidth / 2 ? ZLView.PageIndex.previous : ZLView.PageIndex.next) case leftToRight:
: (myStartY < myHeight / 2 ? ZLView.PageIndex.previous : ZLView.PageIndex.next); return myStartX < myWidth / 2 ? ZLView.PageIndex.previous : ZLView.PageIndex.next;
case rightToLeft:
return myStartX < myWidth / 2 ? ZLView.PageIndex.next : ZLView.PageIndex.previous;
case up:
return myStartY < myHeight / 2 ? ZLView.PageIndex.previous : ZLView.PageIndex.next;
case down:
return myStartY < myHeight / 2 ? ZLView.PageIndex.next : ZLView.PageIndex.previous;
}
return ZLView.PageIndex.current;
} }
} }

View file

@ -29,7 +29,7 @@ class ShiftAnimationProvider extends SimpleAnimationProvider {
@Override @Override
public void draw(Canvas canvas, Bitmap bgBitmap, Bitmap fgBitmap) { public void draw(Canvas canvas, Bitmap bgBitmap, Bitmap fgBitmap) {
myPaint.setColor(Color.rgb(127, 127, 127)); myPaint.setColor(Color.rgb(127, 127, 127));
if (myHorizontal) { if (myDirection.isHorizontal()) {
final int dX = myEndX - myStartX; final int dX = myEndX - myStartX;
canvas.drawBitmap(bgBitmap, dX > 0 ? dX - myWidth : dX + myWidth, 0, myPaint); canvas.drawBitmap(bgBitmap, dX > 0 ? dX - myWidth : dX + myWidth, 0, myPaint);
canvas.drawBitmap(fgBitmap, dX, 0, myPaint); canvas.drawBitmap(fgBitmap, dX, 0, myPaint);

View file

@ -29,8 +29,16 @@ abstract class SimpleAnimationProvider extends AnimationProvider {
} }
ZLView.PageIndex getPageToScrollTo() { ZLView.PageIndex getPageToScrollTo() {
return myHorizontal switch (myDirection) {
? (myStartX < myEndX ? ZLView.PageIndex.previous : ZLView.PageIndex.next) case rightToLeft:
: (myStartY < myEndY ? ZLView.PageIndex.previous : ZLView.PageIndex.next); return myStartX < myEndX ? ZLView.PageIndex.previous : ZLView.PageIndex.next;
case leftToRight:
return myStartX < myEndX ? ZLView.PageIndex.next : ZLView.PageIndex.previous;
case up:
return myStartY < myEndY ? ZLView.PageIndex.previous : ZLView.PageIndex.next;
case down:
return myStartY < myEndY ? ZLView.PageIndex.next : ZLView.PageIndex.previous;
}
return ZLView.PageIndex.current;
} }
} }

View file

@ -30,7 +30,7 @@ class SlideAnimationProvider extends SimpleAnimationProvider {
public void draw(Canvas canvas, Bitmap bgBitmap, Bitmap fgBitmap) { public void draw(Canvas canvas, Bitmap bgBitmap, Bitmap fgBitmap) {
canvas.drawBitmap(bgBitmap, 0, 0, myPaint); canvas.drawBitmap(bgBitmap, 0, 0, myPaint);
myPaint.setColor(Color.rgb(127, 127, 127)); myPaint.setColor(Color.rgb(127, 127, 127));
if (myHorizontal) { if (myDirection.isHorizontal()) {
final int dX = myEndX - myStartX; final int dX = myEndX - myStartX;
canvas.drawBitmap(fgBitmap, dX, 0, myPaint); canvas.drawBitmap(fgBitmap, dX, 0, myPaint);
if (dX > 0 && dX < myWidth) { if (dX > 0 && dX < myWidth) {

View file

@ -51,7 +51,7 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
private int myEndY; private int myEndY;
private ZLView.PageIndex myPageToScrollTo = ZLView.PageIndex.current; private ZLView.PageIndex myPageToScrollTo = ZLView.PageIndex.current;
private boolean myScrollHorizontally; private ZLView.Direction myScrollingDirection;
private float myScrollingSpeed; private float myScrollingSpeed;
@ -159,20 +159,29 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
boolean doStopScrolling = false; boolean doStopScrolling = false;
if (myScrollingState == ScrollingState.AutoScrollingForward || if (myScrollingState == ScrollingState.AutoScrollingForward ||
myScrollingState == ScrollingState.AutoScrollingBackward) { myScrollingState == ScrollingState.AutoScrollingBackward) {
if (myScrollHorizontally) { switch (myScrollingDirection) {
myEndX += (int)myScrollingSpeed; case leftToRight:
} else { myEndX -= (int)myScrollingSpeed;
myEndY += (int)myScrollingSpeed; break;
case rightToLeft:
myEndX += (int)myScrollingSpeed;
break;
case up:
myEndY += (int)myScrollingSpeed;
break;
case down:
myEndY -= (int)myScrollingSpeed;
break;
} }
final int bound; final int bound;
if (myScrollingState == ScrollingState.AutoScrollingForward) { if (myScrollingState == ScrollingState.AutoScrollingForward) {
bound = myScrollHorizontally ? getWidth() : getMainAreaHeight(); bound = myScrollingDirection.isHorizontal() ? getWidth() : getMainAreaHeight();
} else { } else {
bound = 0; bound = 0;
} }
if (myScrollingSpeed > 0) { if (myScrollingSpeed > 0) {
if (getAnimationProvider().getScrollingShift() >= bound) { if (getAnimationProvider().getScrollingShift() >= bound) {
if (myScrollHorizontally) { if (myScrollingDirection.isHorizontal()) {
myEndX = myStartX + bound; myEndX = myStartX + bound;
} else { } else {
myEndY = myStartY + bound; myEndY = myStartY + bound;
@ -181,7 +190,7 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
} }
} else { } else {
if (getAnimationProvider().getScrollingShift() <= -bound) { if (getAnimationProvider().getScrollingShift() <= -bound) {
if (myScrollHorizontally) { if (myScrollingDirection.isHorizontal()) {
myEndX = myStartX - bound; myEndX = myStartX - bound;
} else { } else {
myEndY = myStartY - bound; myEndY = myStartY - bound;
@ -211,7 +220,7 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
getAnimationProvider().setup( getAnimationProvider().setup(
myStartX, myStartY, myStartX, myStartY,
myEndX, myEndY, myEndX, myEndY,
myScrollHorizontally, myScrollingDirection,
getWidth(), getMainAreaHeight() getWidth(), getMainAreaHeight()
); );
getAnimationProvider().draw( getAnimationProvider().draw(
@ -234,13 +243,13 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
} }
} }
public void scrollManually(int startX, int startY, int endX, int endY, boolean horizontally) { public void scrollManually(int startX, int startY, int endX, int endY, ZLView.Direction direction) {
if (myMainBitmap == null) { if (myMainBitmap == null) {
return; return;
} }
myScrollingState = ScrollingState.ManualScrolling; myScrollingState = ScrollingState.ManualScrolling;
myScrollHorizontally = horizontally; myScrollingDirection = direction;
myStartX = startX; myStartX = startX;
myStartY = startY; myStartY = startY;
myEndX = endX; myEndX = endX;
@ -249,7 +258,7 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
getAnimationProvider().setup( getAnimationProvider().setup(
startX, startY, startX, startY,
endX, endY, endX, endY,
horizontally, direction,
getWidth(), getMainAreaHeight() getWidth(), getMainAreaHeight()
); );
setPageToScrollTo(getAnimationProvider().getPageToScrollTo()); setPageToScrollTo(getAnimationProvider().getPageToScrollTo());
@ -267,11 +276,11 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
postInvalidate(); postInvalidate();
} }
public void startAutoScrolling(ZLView.PageIndex pageIndex, boolean horizontally) { public void startAutoScrolling(ZLView.PageIndex pageIndex, ZLView.Direction direction) {
if (myMainBitmap == null) { if (myMainBitmap == null) {
return; return;
} }
myScrollHorizontally = horizontally; myScrollingDirection = direction;
boolean doSetup = myScrollingState == ScrollingState.NoScrolling; boolean doSetup = myScrollingState == ScrollingState.NoScrolling;
switch (pageIndex) { switch (pageIndex) {
case current: case current:
@ -301,13 +310,8 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
setPageToScrollTo(pageIndex); setPageToScrollTo(pageIndex);
break; break;
} }
System.err.println(doSetup + ":" + myScrollingState);
if (doSetup && myScrollingState != ScrollingState.NoScrolling) { if (doSetup && myScrollingState != ScrollingState.NoScrolling) {
//getAnimationProvider().setup( if (myScrollingDirection.isHorizontal()) {
// myScrollHorizontally,
// getWidth(), getMainAreaHeight()
//);
if (myScrollHorizontally) {
myStartX = myScrollingSpeed < 0 ? getWidth() : 0; myStartX = myScrollingSpeed < 0 ? getWidth() : 0;
myStartY = 0; myStartY = 0;
} else { } else {
@ -319,7 +323,7 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
getAnimationProvider().setup( getAnimationProvider().setup(
myStartX, myStartY, myStartX, myStartY,
myEndX, myEndY, myEndX, myEndY,
myScrollHorizontally, myScrollingDirection,
getWidth(), getMainAreaHeight() getWidth(), getMainAreaHeight()
); );
} }
@ -593,7 +597,7 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
} else { } else {
final int from = view.getScrollbarThumbLength(ZLView.PageIndex.current); final int from = view.getScrollbarThumbLength(ZLView.PageIndex.current);
final int to = view.getScrollbarThumbLength(myPageToScrollTo); final int to = view.getScrollbarThumbLength(myPageToScrollTo);
final int size = myScrollHorizontally ? getWidth() : getMainAreaHeight(); final int size = myScrollingDirection.isHorizontal() ? getWidth() : getMainAreaHeight();
final int shift = Math.abs(getAnimationProvider().getScrollingShift()); final int shift = Math.abs(getAnimationProvider().getScrollingShift());
return (from * (size - shift) + to * shift) / size; return (from * (size - shift) + to * shift) / size;
} }
@ -609,7 +613,7 @@ public class ZLAndroidWidget extends View implements View.OnLongClickListener {
} else { } else {
final int from = view.getScrollbarThumbPosition(ZLView.PageIndex.current); final int from = view.getScrollbarThumbPosition(ZLView.PageIndex.current);
final int to = view.getScrollbarThumbPosition(myPageToScrollTo); final int to = view.getScrollbarThumbPosition(myPageToScrollTo);
final int size = myScrollHorizontally ? getWidth() : getMainAreaHeight(); final int size = myScrollingDirection.isHorizontal() ? getWidth() : getMainAreaHeight();
final int shift = Math.abs(getAnimationProvider().getScrollingShift()); final int shift = Math.abs(getAnimationProvider().getScrollingShift());
return (from * (size - shift) + to * shift) / size; return (from * (size - shift) + to * shift) / size;
} }