I’ve hacked together a scroll dynamic with a 30 pixel width and a scroll speed proportional to the distance from the viewport edge. It allows you to change between slow and fast scroll with a very natural feel. It could be better, I think the scroll speed needs to be proportional to the log of the distance to the edge.
I’m having a hell of a time getting the cursor to change though!
public static final int PREFERRED_EDGE_SCROLL_DELAY = 200;
public static final String PREFERRED_EDGE_DELAY = “PreferredEdgeDelay”; //$NON-NLS-1$
public static final int SCROLL_ZONE = 30;
/** The horizontal component of the autoscrolling vector, -1, 0, or 1. /
protected int sx;
/* The vertical component of the autoscrolling vector, -1, 0, or 1. */
protected int sy;
protected int dx, dy;
/**
- Begin autoscrolling the map if the given point is within the given
- distance from a viewport edge.
-
-
@param evtPt
-
@param dist
*/
public void scrollAtEdge(Point evtPt, int dist) {
final Rectangle vrect = scroll.getViewport().getViewRect();
final int px = evtPt.x - vrect.x;
final int py = evtPt.y - vrect.y;
// determine scroll vector
sx = 0;
if (px < dist && px >= 0) {
sx = -1;
dx = dist - px;
}
else if (px < vrect.width && px >= vrect.width - dist) {
sx = 1;
dx = dist - (vrect.width - px);
}
//sx = ? -1 :
// (px < vrect.width && px >= vrect.width - dist ? 1 : 0);
sy = 0;
if (py < dist && py >= 0) {
sy = -1;
dy = dist - py;
}
else if (py < vrect.height && py >= vrect.height - dist) {
sy = 1;
dy = dist - (vrect.height - py);
}
// Slow it down.
dx = dx / 2;
dy = dy / 2;
//sy = py < dist && py >= 0 ? -1 :
// (py < vrect.height && py >= vrect.height - dist ? 1 : 0);
// start autoscrolling if we have a nonzero scroll vector
if (sx != 0 || sy != 0) {
if (!scroller.isRunning()) {
scroller.setStartDelay((Integer)
GameModule.getGameModule().getPrefs().getValue(PREFERRED_EDGE_DELAY));
scroller.start();
}
}
else {
if (scroller.isRunning()) scroller.stop();
}
}
/** The animator which controls autoscrolling. */
protected Animator scroller = new Animator(Animator.INFINITE,
new TimingTargetAdapter() {
private int t;
@Override
public void timingEvent(float fraction) {
// Follow the parabola x^2
//final int delta = 2*(t++);
//scroll(sxdelta, sydelta);
scroll(sxdx, sydy);
// Check whether we have hit an edge
final Rectangle vrect = scroll.getViewport().getViewRect();
if ((sx == -1 && vrect.x == 0) ||
(sx == 1 && vrect.x + vrect.width >= theMap.getWidth())) sx = 0;
if ((sy == -1 && vrect.y == 0) ||
(sy == 1 && vrect.y + vrect.height >= theMap.getHeight())) sy = 0;
// Stop if the scroll vector is zero
if (sx == 0 && sy == 0) scroller.stop();
}
@Override
public void begin() { t = 1; }
}
);
Messages mailing list
Messages@forums.vassalengine.org
forums.vassalengine.org/mailman/ … engine.org
Post generated using Mail2Forum (mail2forum.com)