Cleaned up some sprite business: better documented various things, changed

local member names to reflect their actual functionality, flipped the sign
of the origin offset so that it's an offset rather than a negative offset.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1512 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2002-06-20 21:42:53 +00:00
parent 29cd8ec051
commit 1f80cf9805
3 changed files with 49 additions and 40 deletions
@@ -1,5 +1,5 @@
// //
// $Id: CharacterSprite.java,v 1.34 2002/06/19 23:31:57 mdb Exp $ // $Id: CharacterSprite.java,v 1.35 2002/06/20 21:42:53 mdb Exp $
package com.threerings.cast; package com.threerings.cast;
@@ -146,13 +146,17 @@ public class CharacterSprite extends ImageSprite
// } // }
// documentation inherited // documentation inherited
protected void accomodateFrame (int width, int height) protected void accomodateFrame (int frameIdx, int width, int height)
{ {
super.accomodateFrame(width, height); // this will update our width and height
super.accomodateFrame(frameIdx, width, height);
// we need to update the render offset for this frame // we now need to update the render offset for this frame
_rxoff = -_aframes.getXOrigin(_orient, _frameIdx); _oxoff = _aframes.getXOrigin(_orient, frameIdx);
_ryoff = -_aframes.getYOrigin(_orient, _frameIdx); _oyoff = _aframes.getYOrigin(_orient, frameIdx);
// and cause those changes to be reflected in our bounds
updateRenderOrigin();
} }
// documentation inherited // documentation inherited
@@ -1,5 +1,5 @@
// //
// $Id: ImageSprite.java,v 1.8 2002/06/19 23:30:06 mdb Exp $ // $Id: ImageSprite.java,v 1.9 2002/06/20 21:42:53 mdb Exp $
package com.threerings.media.sprite; package com.threerings.media.sprite;
@@ -181,12 +181,9 @@ public class ImageSprite extends Sprite
Rectangle dirty = new Rectangle(_bounds); Rectangle dirty = new Rectangle(_bounds);
// determine our drawing offsets and rendered rectangle size // determine our drawing offsets and rendered rectangle size
accomodateFrame(_frames.getWidth(_frameIdx), accomodateFrame(_frameIdx, _frames.getWidth(_frameIdx),
_frames.getHeight(_frameIdx)); _frames.getHeight(_frameIdx));
// account for any new render offsets
updateRenderOrigin();
// add our new bounds // add our new bounds
dirty.add(_bounds); dirty.add(_bounds);
@@ -197,13 +194,17 @@ public class ImageSprite extends Sprite
} }
/** /**
* Must adjust the bounds to accomodate the new image. Called when a * Must adjust the bounds to accomodate the our new frame. This
* new image has been set for this image sprite. If a derived class is * includes changing the width and height to reflect the size of the
* going to expand the bounds beyond the bounds of the image frame, it * new frame and also updating the render origin (if necessary) and
* will need to override this method and adjust bounds accordingly for * calling {@link #updateRenderOrigin} to reflect those changes in the
* the new frame (which can be null). * sprite's bounds.
*
* @param frameIdx the index of our new frame.
* @param width the width of the new frame.
* @param height the height of the new frame.
*/ */
protected void accomodateFrame (int width, int height) protected void accomodateFrame (int frameIdx, int width, int height)
{ {
_bounds.width = width; _bounds.width = width;
_bounds.height = height; _bounds.height = height;
@@ -1,5 +1,5 @@
// //
// $Id: Sprite.java,v 1.48 2002/06/19 23:30:06 mdb Exp $ // $Id: Sprite.java,v 1.49 2002/06/20 21:42:53 mdb Exp $
package com.threerings.media.sprite; package com.threerings.media.sprite;
@@ -40,8 +40,8 @@ public abstract class Sprite
*/ */
public Sprite (int x, int y) public Sprite (int x, int y)
{ {
_x = x; _ox = x;
_y = y; _oy = y;
} }
/** /**
@@ -72,7 +72,7 @@ public abstract class Sprite
*/ */
public int getX () public int getX ()
{ {
return _x; return _ox;
} }
/** /**
@@ -82,7 +82,7 @@ public abstract class Sprite
*/ */
public int getY () public int getY ()
{ {
return _y; return _oy;
} }
/** /**
@@ -165,11 +165,11 @@ public abstract class Sprite
Rectangle dirty = new Rectangle(_bounds); Rectangle dirty = new Rectangle(_bounds);
// move ourselves // move ourselves
_x = x; _ox = x;
_y = y; _oy = y;
// we need to update our draw position which is based on the // we need to update our draw position which is based on the size
// size of our current bounds // of our current bounds
updateRenderOrigin(); updateRenderOrigin();
// grow the dirty rectangle to incorporate our new bounds and pass // grow the dirty rectangle to incorporate our new bounds and pass
@@ -233,7 +233,7 @@ public abstract class Sprite
*/ */
public boolean inside (Shape shape) public boolean inside (Shape shape)
{ {
return shape.contains(_x, _y); return shape.contains(_ox, _oy);
} }
/** /**
@@ -357,9 +357,9 @@ public abstract class Sprite
*/ */
protected void updateRenderOrigin () protected void updateRenderOrigin ()
{ {
// our render origin may differ from our location // our bounds origin may differ from the sprite's origin
_bounds.x = _x + _rxoff; _bounds.x = _ox - _oxoff;
_bounds.y = _y + _ryoff; _bounds.y = _oy - _oyoff;
} }
/** /**
@@ -417,22 +417,26 @@ public abstract class Sprite
*/ */
protected void toString (StringBuffer buf) protected void toString (StringBuffer buf)
{ {
buf.append("x=").append(_x); buf.append("ox=").append(_ox);
buf.append(", y=").append(_y); buf.append(", oy=").append(_oy);
buf.append(", rxoff=").append(_rxoff); buf.append(", oxoff=").append(_oxoff);
buf.append(", ryoff=").append(_ryoff); buf.append(", oyoff=").append(_oyoff);
} }
/** The sprite manager. */ /** The sprite manager. */
protected SpriteManager _spritemgr; protected SpriteManager _spritemgr;
/** The location of the sprite in pixel coordinates. */ /** The location of the sprite's origin in pixel coordinates. If the
protected int _x, _y; * sprite positions itself via a hotspot that is not the upper left
* coordinate of the sprite's bounds, the offset to the hotspot should
* be maintained in {@link #_oxoff} and {@link #_oyoff}. */
protected int _ox, _oy;
/** The offsets from our location to our rendered origin. Derived /** The offsets from our upper left coordinate to our origin (or hot
* classes may wish to set these values if the sprite's upper left * spot). Derived classes will need to update these values if the
* corner should not be coincident with its location. */ * sprite's origin is not coincident with the upper left coordinate of
protected int _rxoff, _ryoff; * its bounds. */
protected int _oxoff, _oyoff;
/** Our rendered bounds in pixel coordinates. */ /** Our rendered bounds in pixel coordinates. */
protected Rectangle _bounds = new Rectangle(); protected Rectangle _bounds = new Rectangle();