Cleaned up a few things related to render origin. If a derived class wants
the render origin to be other than (_x, _y) it can update (_rxoff, _ryoff) when it is appropriate to do so, but we don't really need an overridden method to do that. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1500 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -1,8 +1,12 @@
|
|||||||
//
|
//
|
||||||
// $Id: ImageSprite.java,v 1.7 2002/06/05 23:38:18 ray Exp $
|
// $Id: ImageSprite.java,v 1.8 2002/06/19 23:30:06 mdb Exp $
|
||||||
|
|
||||||
package com.threerings.media.sprite;
|
package com.threerings.media.sprite;
|
||||||
|
|
||||||
|
import java.awt.AlphaComposite;
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.Composite;
|
||||||
|
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
import java.awt.Image;
|
import java.awt.Image;
|
||||||
import java.awt.Rectangle;
|
import java.awt.Rectangle;
|
||||||
@@ -83,7 +87,7 @@ public class ImageSprite extends Sprite
|
|||||||
super.init(spritemgr);
|
super.init(spritemgr);
|
||||||
|
|
||||||
// now that we have our spritemanager, we can initialize our frames
|
// now that we have our spritemanager, we can initialize our frames
|
||||||
initFrames();
|
setFrameIndex(0, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -154,29 +158,38 @@ public class ImageSprite extends Sprite
|
|||||||
|
|
||||||
// set and init our frames
|
// set and init our frames
|
||||||
_frames = frames;
|
_frames = frames;
|
||||||
initFrames();
|
setFrameIndex(0, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize our frames.
|
* Instructs the sprite to display the specified frame index.
|
||||||
*/
|
*/
|
||||||
protected void initFrames ()
|
protected void setFrameIndex (int frameIdx, boolean forceUpdate)
|
||||||
{
|
{
|
||||||
|
// make sure we're displaying a valid frame
|
||||||
|
frameIdx = (frameIdx % _frames.getFrameCount());
|
||||||
|
|
||||||
|
// if this is the same frame we're already displaying and we're
|
||||||
|
// not being forced to update, we can stop now
|
||||||
|
if (frameIdx == _frameIdx && !forceUpdate) {
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
_frameIdx = frameIdx;
|
||||||
|
}
|
||||||
|
|
||||||
// start with our old bounds
|
// start with our old bounds
|
||||||
Rectangle dirty = new Rectangle(_bounds);
|
Rectangle dirty = new Rectangle(_bounds);
|
||||||
|
|
||||||
_frameIdx %= _frames.getFrameCount();
|
|
||||||
|
|
||||||
// determine our drawing offsets and rendered rectangle size
|
// determine our drawing offsets and rendered rectangle size
|
||||||
accomodateFrame(_frames.getWidth(_frameIdx),
|
accomodateFrame(_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);
|
||||||
|
|
||||||
updateRenderOffset();
|
|
||||||
updateRenderOrigin();
|
|
||||||
|
|
||||||
// give the dirty rectangle to the region manager
|
// give the dirty rectangle to the region manager
|
||||||
if (_spritemgr != null) {
|
if (_spritemgr != null) {
|
||||||
_spritemgr.getRegionManager().addDirtyRegion(dirty);
|
_spritemgr.getRegionManager().addDirtyRegion(dirty);
|
||||||
@@ -200,7 +213,16 @@ public class ImageSprite extends Sprite
|
|||||||
public void paint (Graphics2D gfx)
|
public void paint (Graphics2D gfx)
|
||||||
{
|
{
|
||||||
if (_frames != null) {
|
if (_frames != null) {
|
||||||
|
// // DEBUG: fill our background with an alpha'd rectangle
|
||||||
|
// Composite ocomp = gfx.getComposite();
|
||||||
|
// gfx.setComposite(ALPHA_BOUNDS);
|
||||||
|
// gfx.setColor(Color.blue);
|
||||||
|
// gfx.fill(_bounds);
|
||||||
|
// gfx.setComposite(ocomp);
|
||||||
|
|
||||||
|
// render our frame
|
||||||
_frames.paintFrame(gfx, _frameIdx, _bounds.x, _bounds.y);
|
_frames.paintFrame(gfx, _frameIdx, _bounds.x, _bounds.y);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
super.paint(gfx);
|
super.paint(gfx);
|
||||||
}
|
}
|
||||||
@@ -241,12 +263,9 @@ public class ImageSprite extends Sprite
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// only update the sprite if our frame index changed
|
// update our frame (which will do nothing if this is the same as
|
||||||
if (nfidx != _frameIdx) {
|
// our existing frame index)
|
||||||
_frameIdx = nfidx;
|
setFrameIndex(nfidx, false);
|
||||||
// dirty our rectangle since we've altered our display image
|
|
||||||
invalidate();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// documentation inherited
|
// documentation inherited
|
||||||
@@ -267,4 +286,8 @@ public class ImageSprite extends Sprite
|
|||||||
|
|
||||||
/** For how many milliseconds to display an animation frame. */
|
/** For how many milliseconds to display an animation frame. */
|
||||||
protected long _frameDelay;
|
protected long _frameDelay;
|
||||||
|
|
||||||
|
// /** DEBUG: The alpha level used when rendering our bounds. */
|
||||||
|
// protected static final Composite ALPHA_BOUNDS =
|
||||||
|
// AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.2f);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
//
|
//
|
||||||
// $Id: Sprite.java,v 1.47 2002/06/18 22:25:33 mdb Exp $
|
// $Id: Sprite.java,v 1.48 2002/06/19 23:30:06 mdb Exp $
|
||||||
|
|
||||||
package com.threerings.media.sprite;
|
package com.threerings.media.sprite;
|
||||||
|
|
||||||
@@ -53,8 +53,6 @@ public abstract class Sprite
|
|||||||
protected void init (SpriteManager spritemgr)
|
protected void init (SpriteManager spritemgr)
|
||||||
{
|
{
|
||||||
_spritemgr = spritemgr;
|
_spritemgr = spritemgr;
|
||||||
|
|
||||||
updateRenderOrigin();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -353,16 +351,6 @@ public abstract class Sprite
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates the sprite's render offset which is used to determine
|
|
||||||
* where to place the top-left corner of the render bounds.
|
|
||||||
*/
|
|
||||||
protected void updateRenderOffset ()
|
|
||||||
{
|
|
||||||
_rxoff = 0;
|
|
||||||
_ryoff = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the coordinates at which the sprite image is drawn to
|
* Update the coordinates at which the sprite image is drawn to
|
||||||
* reflect the sprite's current position.
|
* reflect the sprite's current position.
|
||||||
@@ -431,6 +419,8 @@ public abstract class Sprite
|
|||||||
{
|
{
|
||||||
buf.append("x=").append(_x);
|
buf.append("x=").append(_x);
|
||||||
buf.append(", y=").append(_y);
|
buf.append(", y=").append(_y);
|
||||||
|
buf.append(", rxoff=").append(_rxoff);
|
||||||
|
buf.append(", ryoff=").append(_ryoff);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The sprite manager. */
|
/** The sprite manager. */
|
||||||
@@ -439,7 +429,9 @@ public abstract class Sprite
|
|||||||
/** The location of the sprite in pixel coordinates. */
|
/** The location of the sprite in pixel coordinates. */
|
||||||
protected int _x, _y;
|
protected int _x, _y;
|
||||||
|
|
||||||
/** The offsets from our location to our rendered origin. */
|
/** The offsets from our location to our rendered origin. Derived
|
||||||
|
* classes may wish to set these values if the sprite's upper left
|
||||||
|
* corner should not be coincident with its location. */
|
||||||
protected int _rxoff, _ryoff;
|
protected int _rxoff, _ryoff;
|
||||||
|
|
||||||
/** Our rendered bounds in pixel coordinates. */
|
/** Our rendered bounds in pixel coordinates. */
|
||||||
|
|||||||
Reference in New Issue
Block a user