com.threerings.media.sprite
Class Sprite

java.lang.Object
  extended by com.threerings.media.AbstractMedia
      extended by com.threerings.media.sprite.Sprite
All Implemented Interfaces:
Pathable, DirectionCodes, Shape
Direct Known Subclasses:
AnimationSprite, ButtonSprite, ImageSprite, LabelSprite

public abstract class Sprite
extends AbstractMedia
implements DirectionCodes, Pathable

The sprite class represents a single movable object in an animated view. A sprite has a position and orientation within the view, and can be moved along a path.


Nested Class Summary
protected static class Sprite.CancelledOp
          Used to dispatch PathObserver.pathCancelled(com.threerings.media.sprite.Sprite, com.threerings.media.util.Path).
protected static class Sprite.CompletedOp
          Used to dispatch PathObserver.pathCompleted(com.threerings.media.sprite.Sprite, com.threerings.media.util.Path, long).
 
Field Summary
protected  int _orient
          The orientation of this sprite.
protected  int _ox
          The location of the sprite's origin in pixel coordinates.
protected  int _oxoff
          The offsets from our upper left coordinate to our origin (or hot spot).
protected  int _oy
          The location of the sprite's origin in pixel coordinates.
protected  int _oyoff
          The offsets from our upper left coordinate to our origin (or hot spot).
protected  Path _path
          When moving, the path the sprite is traversing.
protected  long _pathStamp
          The timestamp at which we started along our path.
 
Fields inherited from class com.threerings.media.AbstractMedia
_bounds, _firstTick, _mgr, _observers, _renderOrder, HUD_LAYER
 
Fields inherited from interface com.threerings.util.DirectionCodes
CARDINAL_DIRECTIONS, CCW, CW, DIRECTION_COUNT, DOWN, EAST, EASTNORTHEAST, EASTSOUTHEAST, FINE_DIRECTION_COUNT, HORIZONTAL, LEFT, NONE, NORTH, NORTHEAST, NORTHNORTHEAST, NORTHNORTHWEST, NORTHWEST, RIGHT, SOUTH, SOUTHEAST, SOUTHSOUTHEAST, SOUTHSOUTHWEST, SOUTHWEST, UP, VERTICAL, WEST, WESTNORTHWEST, WESTSOUTHWEST
 
Constructor Summary
Sprite()
          Constructs a sprite with an initially invalid location.
Sprite(int width, int height)
          Constructs a sprite with the supplied dimensions.
 
Method Summary
 void addSpriteObserver(Object obs)
          Add a sprite observer to observe this sprite's events.
 void cancelMove()
          Cancels any path that the sprite may currently be moving along.
 boolean contains(int x, int y)
          Returns true if the sprite's bounds contain the specified point, false if not.
 void fastForward(long timeDelta)
          Called when the appropriate media manager has been paused for some length of time and is then unpaused.
 int getHeight()
          Returns the sprite's height in pixels.
 int getOrientation()
          Returns the sprite's orientation as one of the eight cardinal directions: DirectionCodes.NORTH, DirectionCodes.NORTHEAST, etc.
 Path getPath()
          Returns the path being followed by this sprite or null if the sprite is not following a path.
 int getWidth()
          Returns the sprite's width in pixels.
 int getX()
          Returns the sprite's x position in screen coordinates.
 int getXOffset()
          Returns the offset to the sprite's origin from the upper-left of the sprite's image.
 int getY()
          Returns the sprite's y position in screen coordinates.
 int getYOffset()
          Returns the offset to the sprite's origin from the upper-left of the sprite's image.
 boolean hitTest(int x, int y)
          Returns true if the sprite's bounds contain the specified point, false if not.
 boolean inside(Shape shape)
          Returns whether the sprite is inside the given shape in pixel coordinates.
 boolean intersects(Shape shape)
          Returns whether the sprite's drawn rectangle intersects the given shape in pixel coordinates.
 boolean isMoving()
          Returns true if this sprite is currently following a path, false if it is not.
 void move(Path path)
          Set the sprite's active path and start moving it along its merry way.
 void paint(Graphics2D gfx)
          Called by the appropriate manager to request that the media render itself with the given graphics context.
 void paintPath(Graphics2D gfx)
          Paint the sprite's path, if any, to the specified graphics context.
 void pathBeginning()
          Called by the active path when it begins.
 void pathCompleted(long timestamp)
          Called by the active path when it has completed.
 void removeSpriteObserver(Object obs)
          Remove a sprite observer.
 void setLocation(int x, int y)
          Set the location.
 void setOrientation(int orient)
          Sprites have an orientation in one of the eight cardinal directions: DirectionCodes.NORTH, DirectionCodes.NORTHEAST, etc.
protected  void shutdown()
          Called by the media manager after the media is removed from service.
 void tick(long tickStamp)
          Called periodically by this media's manager to give it a chance to do its thing.
protected  boolean tickPath(long tickStamp)
          Ticks any path assigned to this sprite.
protected  void toString(StringBuilder buf)
          This should be overridden by derived classes (which should be sure to call super.toString()) to append the derived class specific information to the string buffer.
protected  void updateRenderOrigin()
          Update the coordinates at which the sprite image is drawn to reflect the sprite's current position.
 void viewLocationDidChange(int dx, int dy)
          Called by the AbstractMediaManager when we are in a VirtualMediaPanel that just scrolled.
 
Methods inherited from class com.threerings.media.AbstractMedia
addObserver, contains, contains, contains, contains, getBounds, getBounds2D, getPathIterator, getPathIterator, getRenderOrder, init, init, intersects, intersects, invalidate, invalidateAfterChange, naturalCompareTo, queueNotification, removeObserver, renderCompareTo, setRenderOrder, toString, willStart
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.threerings.media.util.Pathable
getBounds
 

Field Detail

_ox

protected int _ox
The location of the sprite's origin in pixel coordinates. If the 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 _oxoff and _oyoff.


_oy

protected int _oy
The location of the sprite's origin in pixel coordinates. If the 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 _oxoff and _oyoff.


_oxoff

protected int _oxoff
The offsets from our upper left coordinate to our origin (or hot spot). Derived classes will need to update these values if the sprite's origin is not coincident with the upper left coordinate of its bounds.


_oyoff

protected int _oyoff
The offsets from our upper left coordinate to our origin (or hot spot). Derived classes will need to update these values if the sprite's origin is not coincident with the upper left coordinate of its bounds.


_orient

protected int _orient
The orientation of this sprite.


_path

protected Path _path
When moving, the path the sprite is traversing.


_pathStamp

protected long _pathStamp
The timestamp at which we started along our path.

Constructor Detail

Sprite

public Sprite()
Constructs a sprite with an initially invalid location. Because sprite derived classes generally want to get in on the business when a sprite's location is set, it is not safe to do so in the constructor because their derived methods will be called before their constructor has been called. Thus a sprite should be fully constructed and then its location should be set.


Sprite

public Sprite(int width,
              int height)
Constructs a sprite with the supplied dimensions. Because sprite derived classes generally want to get in on the business when a sprite's location is set, it is not safe to do so in the constructor because their derived methods will be called before their constructor has been called. Thus a sprite should be fully constructed and then its location should be set.

Method Detail

getX

public int getX()
Returns the sprite's x position in screen coordinates. This is the x coordinate of the sprite's origin, not the upper left of its bounds.

Specified by:
getX in interface Pathable

getY

public int getY()
Returns the sprite's y position in screen coordinates. This is the y coordinate of the sprite's origin, not the upper left of its bounds.

Specified by:
getY in interface Pathable

getXOffset

public int getXOffset()
Returns the offset to the sprite's origin from the upper-left of the sprite's image.


getYOffset

public int getYOffset()
Returns the offset to the sprite's origin from the upper-left of the sprite's image.


getWidth

public int getWidth()
Returns the sprite's width in pixels.


getHeight

public int getHeight()
Returns the sprite's height in pixels.


setOrientation

public void setOrientation(int orient)
Sprites have an orientation in one of the eight cardinal directions: DirectionCodes.NORTH, DirectionCodes.NORTHEAST, etc. Derived classes can choose to override this member function and select a different set of images based on their orientation, or they can ignore the orientation information.

Specified by:
setOrientation in interface Pathable
See Also:
DirectionCodes

getOrientation

public int getOrientation()
Returns the sprite's orientation as one of the eight cardinal directions: DirectionCodes.NORTH, DirectionCodes.NORTHEAST, etc.

Specified by:
getOrientation in interface Pathable
See Also:
DirectionCodes

setLocation

public void setLocation(int x,
                        int y)
Description copied from class: AbstractMedia
Set the location.

Specified by:
setLocation in interface Pathable
Overrides:
setLocation in class AbstractMedia

paint

public void paint(Graphics2D gfx)
Description copied from class: AbstractMedia
Called by the appropriate manager to request that the media render itself with the given graphics context. The media may wish to inspect the clipping region that has been set on the graphics context to render itself more efficiently. This method will only be called after it has been established that this media's bounds intersect the clipping region.

Specified by:
paint in class AbstractMedia

paintPath

public void paintPath(Graphics2D gfx)
Paint the sprite's path, if any, to the specified graphics context.

Parameters:
gfx - the graphics context.

contains

public boolean contains(int x,
                        int y)
Returns true if the sprite's bounds contain the specified point, false if not.


hitTest

public boolean hitTest(int x,
                       int y)
Returns true if the sprite's bounds contain the specified point, false if not.


inside

public boolean inside(Shape shape)
Returns whether the sprite is inside the given shape in pixel coordinates.


intersects

public boolean intersects(Shape shape)
Returns whether the sprite's drawn rectangle intersects the given shape in pixel coordinates.


isMoving

public boolean isMoving()
Returns true if this sprite is currently following a path, false if it is not.


move

public void move(Path path)
Set the sprite's active path and start moving it along its merry way. If the sprite is already moving along a previous path the old path will be lost and the new path will begin to be traversed.

Parameters:
path - the path to follow.

cancelMove

public void cancelMove()
Cancels any path that the sprite may currently be moving along.


getPath

public Path getPath()
Returns the path being followed by this sprite or null if the sprite is not following a path.


pathBeginning

public void pathBeginning()
Called by the active path when it begins.

Specified by:
pathBeginning in interface Pathable

pathCompleted

public void pathCompleted(long timestamp)
Called by the active path when it has completed.

Specified by:
pathCompleted in interface Pathable

tick

public void tick(long tickStamp)
Description copied from class: AbstractMedia
Called periodically by this media's manager to give it a chance to do its thing.

Specified by:
tick in class AbstractMedia
Parameters:
tickStamp - a time stamp associated with this tick. Note: this is not obtained from a call to System.currentTimeMillis() and cannot be compared to timestamps obtained there from.

tickPath

protected boolean tickPath(long tickStamp)
Ticks any path assigned to this sprite.

Returns:
true if the path relocated the sprite as a result of this tick, false if it remained in the same position.

fastForward

public void fastForward(long timeDelta)
Description copied from class: AbstractMedia
Called when the appropriate media manager has been paused for some length of time and is then unpaused. Media should adjust any time stamps that are maintained internally forward by the delta so that time maintains the illusion of flowing smoothly forward.

Overrides:
fastForward in class AbstractMedia

updateRenderOrigin

protected void updateRenderOrigin()
Update the coordinates at which the sprite image is drawn to reflect the sprite's current position.


addSpriteObserver

public void addSpriteObserver(Object obs)
Add a sprite observer to observe this sprite's events.

Parameters:
obs - the sprite observer.

removeSpriteObserver

public void removeSpriteObserver(Object obs)
Remove a sprite observer.


viewLocationDidChange

public void viewLocationDidChange(int dx,
                                  int dy)
Description copied from class: AbstractMedia
Called by the AbstractMediaManager when we are in a VirtualMediaPanel that just scrolled.

Overrides:
viewLocationDidChange in class AbstractMedia

shutdown

protected void shutdown()
Description copied from class: AbstractMedia
Called by the media manager after the media is removed from service. Derived classes may override this method, but should be sure to call super.shutdown().

Overrides:
shutdown in class AbstractMedia

toString

protected void toString(StringBuilder buf)
Description copied from class: AbstractMedia
This should be overridden by derived classes (which should be sure to call super.toString()) to append the derived class specific information to the string buffer.

Overrides:
toString in class AbstractMedia