Revampola! Moved the code that handles viewport offset into the animated

panel; restructured things so everyone deals with "view" coordinates
rather than screen coordinates and the animated panel takes care of doing
the final translation before rendering.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1065 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2002-02-21 06:01:29 +00:00
parent 424f2da631
commit 137e9e9ea8
4 changed files with 90 additions and 80 deletions
@@ -1,5 +1,5 @@
// //
// $Id: AnimatedPanel.java,v 1.16 2002/02/21 00:20:22 mdb Exp $ // $Id: AnimatedPanel.java,v 1.17 2002/02/21 06:01:28 mdb Exp $
package com.threerings.media.animation; package com.threerings.media.animation;
@@ -86,7 +86,9 @@ public class AnimatedPanel extends Canvas implements AnimatedView
/** /**
* Instructs the view to begin scrolling with the specified velocities * Instructs the view to begin scrolling with the specified velocities
* in milliseconds per pixel. A setting of zero indicates that * in milliseconds per pixel. A setting of zero indicates that
* scrolling should be deactivated in that direction. * scrolling should be deactivated in that direction. Negative values
* mean that the view should be scrolled one pixel in the opposite
* direction in the positive number of milliseconds.
*/ */
public void setScrolling (int msppx, int msppy) public void setScrolling (int msppx, int msppy)
{ {
@@ -123,7 +125,11 @@ public class AnimatedPanel extends Canvas implements AnimatedView
{ {
// we don't paint directly any more, we pass the dirty rect to the // we don't paint directly any more, we pass the dirty rect to the
// animation manager to queue up along with our next repaint // animation manager to queue up along with our next repaint
_animmgr.addDirtyRect(g.getClipBounds()); Rectangle dirty = g.getClipBounds();
// we need to adjust the dirty rect by our viewport offset before
// passing it to the animation manager
dirty.translate(_tx, _ty);
_animmgr.addDirtyRect(dirty);
} }
// documentation inherited // documentation inherited
@@ -141,6 +147,18 @@ public class AnimatedPanel extends Canvas implements AnimatedView
// if we change size, clear out our old back buffer // if we change size, clear out our old back buffer
_backimg = null; _backimg = null;
// figure out our viewport offsets
Dimension size = getSize(), vsize = getViewSize();
_tx = (vsize.width - size.width)/2;
_ty = (vsize.height - size.height)/2;
}
// documentation inherited
public void invalidateRect (Rectangle invalidRect)
{
// pass it on to the animation manager
_animmgr.addDirtyRect(invalidRect);
} }
// documentation inherited // documentation inherited
@@ -191,14 +209,22 @@ public class AnimatedPanel extends Canvas implements AnimatedView
// and add invalid rectangles for the exposed areas // and add invalid rectangles for the exposed areas
if (dx > 0) { if (dx > 0) {
invalidRects.add(new Rectangle(width - dx, 0, dx, height)); Rectangle dirty = new Rectangle(width - dx, 0, dx, height);
dirty.translate(_tx, _ty);
invalidRects.add(dirty);
} else if (dx < 0) { } else if (dx < 0) {
invalidRects.add(new Rectangle(0, 0, -dx, height)); Rectangle dirty = new Rectangle(0, 0, -dx, height);
dirty.translate(_tx, _ty);
invalidRects.add(dirty);
} }
if (dy > 0) { if (dy > 0) {
invalidRects.add(new Rectangle(0, height - dy, width, dy)); Rectangle dirty = new Rectangle(0, height - dy, width, dy);
dirty.translate(_tx, _ty);
invalidRects.add(dirty);
} else if (dy < 0) { } else if (dy < 0) {
invalidRects.add(new Rectangle(0, 0, width, -dy)); Rectangle dirty = new Rectangle(0, 0, width, -dy);
dirty.translate(_tx, _ty);
invalidRects.add(dirty);
} }
// make sure we're actually scrolling before telling people // make sure we're actually scrolling before telling people
@@ -213,7 +239,7 @@ public class AnimatedPanel extends Canvas implements AnimatedView
// let our derived classes do whatever they need to do to // let our derived classes do whatever they need to do to
// prepare to be scrolled // prepare to be scrolled
viewWillScroll(dx, dy, now); viewWillScroll(dx, dy, now, invalidRects);
} }
} }
@@ -248,7 +274,7 @@ public class AnimatedPanel extends Canvas implements AnimatedView
// business rather than just the dirty parts // business rather than just the dirty parts
if (valres != VolatileImage.IMAGE_OK) { if (valres != VolatileImage.IMAGE_OK) {
invalidRects.clear(); invalidRects.clear();
invalidRects.add(new Rectangle(0, 0, width, height)); invalidRects.add(new Rectangle(_tx, _ty, width, height));
Log.info("Lost back buffer, redrawing."); Log.info("Lost back buffer, redrawing.");
} else if (dx != 0 || dy != 0) { } else if (dx != 0 || dy != 0) {
@@ -256,9 +282,15 @@ public class AnimatedPanel extends Canvas implements AnimatedView
g.copyArea(0, 0, width, height, -dx, -dy); g.copyArea(0, 0, width, height, -dx, -dy);
} }
// translate into happy space
g.translate(-_tx, -_ty);
// now do our actual rendering // now do our actual rendering
render((Graphics2D)g, invalidRects); render((Graphics2D)g, invalidRects);
// translate back out of happy space
g.translate(_tx, _ty);
} finally { } finally {
g.dispose(); g.dispose();
} }
@@ -279,6 +311,9 @@ public class AnimatedPanel extends Canvas implements AnimatedView
int isize = invalidRects.size(); int isize = invalidRects.size();
for (int i = 0; i < isize; i++) { for (int i = 0; i < isize; i++) {
Rectangle rect = (Rectangle)invalidRects.get(i); Rectangle rect = (Rectangle)invalidRects.get(i);
// we have to translate out of view coordinates
// before doing the actual copy
rect.translate(-_tx, -_ty);
g.setClip(rect); g.setClip(rect);
g.drawImage(_backimg, 0, 0, null); g.drawImage(_backimg, 0, 0, null);
} }
@@ -306,16 +341,33 @@ public class AnimatedPanel extends Canvas implements AnimatedView
* y direction. * y direction.
* @param now the current time, provided because we have it and * @param now the current time, provided because we have it and
* scrolling views are likely to want to use it in calculating stuff. * scrolling views are likely to want to use it in calculating stuff.
* @param invalidRects the list of invalid rectangles which will be
* redrawn; rectangles can be added to this list if necessary.
*/ */
protected void viewWillScroll (int dx, int dy, long now) protected void viewWillScroll (int dx, int dy, long now, List invalidRects)
{ {
// nothing to do here // nothing to do here
} }
/**
* Derived classes that wish to operate in a coordinate system based
* on a view size that is larger or smaller than the viewport size
* (the actual dimensions of the animated panel) can override this
* method and return the desired size of the view. The animated panel
* will take this size into account and translate into the view
* coordinate system before calling {@link #render}.
*/
protected Dimension getViewSize ()
{
return getSize();
}
/** /**
* Requests that the supplied list of invalid rectangles be redrawn in * Requests that the supplied list of invalid rectangles be redrawn in
* the supplied graphics context. Sub-classes should override this * the supplied graphics context. The rectangles will be in the view
* method to do the actual rendering for their display. * coordinate system (which may differ from screen coordinates, see
* {@link #getViewSize}. Sub-classes should override this method to do
* the actual rendering for their display.
*/ */
protected void render (Graphics2D gfx, List invalidRects) protected void render (Graphics2D gfx, List invalidRects)
{ {
@@ -340,6 +392,9 @@ public class AnimatedPanel extends Canvas implements AnimatedView
/** The image used to render off-screen. */ /** The image used to render off-screen. */
protected VolatileImage _backimg; protected VolatileImage _backimg;
/** Our viewport offsets. */
protected int _tx, _ty;
/** The scrolling velocity in milliseconds per pixel. */ /** The scrolling velocity in milliseconds per pixel. */
protected int _msppx, _msppy; protected int _msppx, _msppy;
@@ -1,5 +1,5 @@
// //
// $Id: AnimatedView.java,v 1.3 2002/02/19 01:23:56 mdb Exp $ // $Id: AnimatedView.java,v 1.4 2002/02/21 06:01:29 mdb Exp $
package com.threerings.media.animation; package com.threerings.media.animation;
@@ -14,6 +14,14 @@ import java.util.List;
*/ */
public interface AnimatedView public interface AnimatedView
{ {
/**
* Requests that the specified rectangle (in view coordinates, which
* need not account for scrolling offsets or viewport offsets) be
* rendered invalid. The animated view should massage the location of
* the invalid rectangle and pass it on to the animation manager.
*/
public void invalidateRect (Rectangle invalidRect);
/** /**
* Requests that the animated view paint itself immediately (that it * Requests that the animated view paint itself immediately (that it
* complete the painting process before returning from this function). * complete the painting process before returning from this function).
@@ -1,5 +1,5 @@
// //
// $Id: SpriteManager.java,v 1.24 2002/02/19 19:55:14 mdb Exp $ // $Id: SpriteManager.java,v 1.25 2002/02/21 06:01:29 mdb Exp $
package com.threerings.media.sprite; package com.threerings.media.sprite;
@@ -35,21 +35,6 @@ public class SpriteManager
_dirty = new ArrayList(); _dirty = new ArrayList();
} }
/**
* Specifies the viewport offset of the view into which the sprites
* are being rendered. This is an annoying hack, but the sprite
* manager needs to create dirty rectangles and communicate them to
* the animation manager who deals in untranslated coordinates,
* whereas the sprites remain in translated coordinates; but only the
* view itself knows what the translation should be, so it has to tell
* the sprite manager about it using this method.
*/
public void setViewportOffset (int tx, int ty)
{
_tx = tx;
_ty = ty;
}
/** /**
* Lets the sprite manager know that the view is about to scroll by * Lets the sprite manager know that the view is about to scroll by
* the specified offsets. It can update the positions of its sprites * the specified offsets. It can update the positions of its sprites
@@ -76,9 +61,6 @@ public class SpriteManager
} }
dirty.add(ex, ey); dirty.add(ex, ey);
// translate the rectangle according to our viewport offset
dirty.translate(-_tx, -_ty);
// append it to the list // append it to the list
invalidRects.add(dirty); invalidRects.add(dirty);
} }
@@ -92,7 +74,6 @@ public class SpriteManager
public void addDirtyRect (Rectangle rect) public void addDirtyRect (Rectangle rect)
{ {
// translate the rectangle according to our viewport offset // translate the rectangle according to our viewport offset
rect.translate(-_tx, -_ty);
_dirty.add(rect); _dirty.add(rect);
} }
@@ -360,9 +341,6 @@ public class SpriteManager
/** The list of pending sprite notifications. */ /** The list of pending sprite notifications. */
protected ArrayList _notify; protected ArrayList _notify;
/** Our viewport offset. */
protected int _tx, _ty;
protected static class SpriteComparator implements Comparator protected static class SpriteComparator implements Comparator
{ {
public int compare (Object o1, Object o2) public int compare (Object o1, Object o2)
@@ -1,5 +1,5 @@
// //
// $Id: SceneViewPanel.java,v 1.35 2002/02/21 00:20:22 mdb Exp $ // $Id: SceneViewPanel.java,v 1.36 2002/02/21 06:01:29 mdb Exp $
package com.threerings.miso.scene; package com.threerings.miso.scene;
@@ -112,29 +112,23 @@ public class SceneViewPanel extends AnimatedPanel
_spritemgr.addSprite(sprite); _spritemgr.addSprite(sprite);
} }
// documentation inherited
public void validate ()
{
super.validate();
// figure out our translations
Dimension size = getSize();
_tx = (_viewmodel.bounds.width - size.width)/2;
_ty = (_viewmodel.bounds.height - size.height)/2;
// tell the sprite manager about the offsets so that it can
// communicate valid dirty rectangles to the animation manager
_spritemgr.setViewportOffset(_tx, _ty);
}
/** /**
* If we're scrolling, we need to pass the word on to our scene view. * If we're scrolling, we need to pass the word on to our scene view.
*/ */
protected void viewWillScroll (int dx, int dy, long now) protected void viewWillScroll (int dx, int dy, long now, List invalidRects)
{ {
_view.viewWillScroll(dx, dy); _view.viewWillScroll(dx, dy);
} }
/**
* We want to pretend like we're in a view that fits our whole model
* so we return the model bounds as our view size.
*/
protected Dimension getViewSize ()
{
return _viewmodel.bounds.getSize();
}
/** /**
* We overload this to translate mouse events into the proper * We overload this to translate mouse events into the proper
* coordinates before they are dispatched to any of the mouse * coordinates before they are dispatched to any of the mouse
@@ -160,32 +154,11 @@ public class SceneViewPanel extends AnimatedPanel
// documentation inherited // documentation inherited
protected void render (Graphics2D gfx, List invalidRects) protected void render (Graphics2D gfx, List invalidRects)
{ {
// we have to translate all of the invalid rects into our
// translated coordinate space before passing them on to the
// underlying view code
int isize = invalidRects.size();
for (int i = 0; i < isize; i++) {
((Rectangle)invalidRects.get(i)).translate(_tx, _ty);
}
// translate into happy space
gfx.translate(-_tx, -_ty);
// render the view // render the view
_view.paint(gfx, invalidRects); _view.paint(gfx, invalidRects);
// give derived classes a chance to render on top of the view // give derived classes a chance to render on top of the view
renderOnView(gfx, invalidRects); renderOnView(gfx, invalidRects);
// translate back out of happy space
gfx.translate(_tx, _ty);
// now we translate them back out (because the animated panel
// needs to use them as they were); we translate in and out rather
// than copy them to save on object creation overhead
for (int i = 0; i < isize; i++) {
((Rectangle)invalidRects.get(i)).translate(-_tx, -_ty);
}
} }
/** /**
@@ -204,9 +177,8 @@ public class SceneViewPanel extends AnimatedPanel
*/ */
public Dimension getPreferredSize () public Dimension getPreferredSize ()
{ {
Dimension psize = (_viewmodel == null) ? return (_viewmodel == null) ? super.getPreferredSize() :
super.getPreferredSize() : _viewmodel.bounds.getSize(); _viewmodel.bounds.getSize();
return psize;
} }
// documentation inherited // documentation inherited
@@ -221,7 +193,4 @@ public class SceneViewPanel extends AnimatedPanel
/** The scene view we're managing. */ /** The scene view we're managing. */
protected SceneView _view; protected SceneView _view;
/** Our rendering translation. */
protected int _tx, _ty;
} }