Changed the hourglass to animate a multiframe image for the sand rather
than move a wee dot. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3521 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -15,6 +15,7 @@ import com.samskivert.util.Interval;
|
|||||||
import com.samskivert.util.ResultListener;
|
import com.samskivert.util.ResultListener;
|
||||||
|
|
||||||
import com.threerings.media.image.Mirage;
|
import com.threerings.media.image.Mirage;
|
||||||
|
import com.threerings.media.util.MultiFrameImage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Displays an hourglass with the sand level representing the amount of
|
* Displays an hourglass with the sand level representing the amount of
|
||||||
@@ -27,10 +28,11 @@ public class HourglassView extends TimerView
|
|||||||
*/
|
*/
|
||||||
public HourglassView (FrameManager fmgr, JComponent host, int x, int y,
|
public HourglassView (FrameManager fmgr, JComponent host, int x, int y,
|
||||||
Mirage glassImage, Mirage topImage, Rectangle topRect,
|
Mirage glassImage, Mirage topImage, Rectangle topRect,
|
||||||
Mirage botImage, Rectangle botRect, Color sandColor)
|
Mirage botImage, Rectangle botRect,
|
||||||
|
MultiFrameImage sandTrickle)
|
||||||
{
|
{
|
||||||
this(fmgr, host, x, y, glassImage, topImage, topRect, new Point(0, 0),
|
this(fmgr, host, x, y, glassImage, topImage, topRect, new Point(0, 0),
|
||||||
botImage, botRect, new Point(0, 0), sandColor);
|
botImage, botRect, new Point(0, 0), sandTrickle);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -39,7 +41,8 @@ public class HourglassView extends TimerView
|
|||||||
public HourglassView (
|
public HourglassView (
|
||||||
FrameManager fmgr, JComponent host, int x, int y, Mirage glassImage,
|
FrameManager fmgr, JComponent host, int x, int y, Mirage glassImage,
|
||||||
Mirage topImage, Rectangle topRect, Point topOff,
|
Mirage topImage, Rectangle topRect, Point topOff,
|
||||||
Mirage botImage, Rectangle botRect, Point botOff, Color sandColor)
|
Mirage botImage, Rectangle botRect, Point botOff,
|
||||||
|
MultiFrameImage sandTrickle)
|
||||||
{
|
{
|
||||||
super(fmgr, host, new Rectangle(x, y, glassImage.getWidth(),
|
super(fmgr, host, new Rectangle(x, y, glassImage.getWidth(),
|
||||||
glassImage.getHeight()));
|
glassImage.getHeight()));
|
||||||
@@ -49,38 +52,41 @@ public class HourglassView extends TimerView
|
|||||||
_topOff = topOff;
|
_topOff = topOff;
|
||||||
_botRect = botRect;
|
_botRect = botRect;
|
||||||
_botOff = botOff;
|
_botOff = botOff;
|
||||||
_sandColor = sandColor;
|
|
||||||
|
|
||||||
// Save hourglass images
|
// Save hourglass images
|
||||||
_hourglass = glassImage;
|
_hourglass = glassImage;
|
||||||
_sandTop = topImage;
|
_sandTop = topImage;
|
||||||
_sandBottom = botImage;
|
_sandBottom = botImage;
|
||||||
|
_sandTrickle = sandTrickle;
|
||||||
|
|
||||||
// Initialize the falling grain of sand
|
// Initialize the falling grain of sand
|
||||||
_sandY = _topRect.y + _topRect.height;
|
_sandY = 0;
|
||||||
_sandTime = 0;
|
|
||||||
|
|
||||||
// Percentage changes smaller than one pixel in the hourglass
|
// Percentage changes smaller than one pixel in the hourglass
|
||||||
// itself definitely won't be noticed
|
// itself definitely won't be noticed
|
||||||
_changeThreshold = 1.0f / _bounds.height;
|
_changeThreshold = 1.0f / _bounds.height;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// documentation inherited
|
||||||
|
public void changeComplete (float complete)
|
||||||
|
{
|
||||||
|
super.changeComplete(complete);
|
||||||
|
setSandTrickleY();
|
||||||
|
}
|
||||||
|
|
||||||
// documentation inherited
|
// documentation inherited
|
||||||
public void tick (long tickStamp)
|
public void tick (long tickStamp)
|
||||||
{
|
{
|
||||||
// Let the parent hand its stuff
|
// Let the parent handle its stuff
|
||||||
super.tick(tickStamp);
|
super.tick(tickStamp);
|
||||||
|
|
||||||
// Check if the falling sand dot should move
|
// check if the sand should be updated
|
||||||
if (_sandTime + SAND_RATE < tickStamp)
|
if (tickStamp > _sandStamp) {
|
||||||
{
|
|
||||||
// Move the sand grain, possibly wrapping around
|
|
||||||
if (++_sandY > getSandBottomTop(_renderComplete)) {
|
|
||||||
_sandY = _topRect.y + _topRect.height;
|
|
||||||
}
|
|
||||||
|
|
||||||
// This is when the grain was last updated
|
// update the sand frame
|
||||||
_sandTime = tickStamp;
|
setSandTrickleY();
|
||||||
|
_sandFrame = (_sandFrame + 1) % _sandTrickle.getFrameCount();
|
||||||
|
_sandStamp = tickStamp + SAND_RATE;
|
||||||
|
|
||||||
// Make sure the timer gets repainted
|
// Make sure the timer gets repainted
|
||||||
invalidate();
|
invalidate();
|
||||||
@@ -103,6 +109,16 @@ public class HourglassView extends TimerView
|
|||||||
gfx.clipRect(_topRect.x, top, _topRect.width,
|
gfx.clipRect(_topRect.x, top, _topRect.width,
|
||||||
_topRect.height - (top-_topRect.y));
|
_topRect.height - (top-_topRect.y));
|
||||||
_sandTop.paint(gfx, _topOff.x, _topOff.y);
|
_sandTop.paint(gfx, _topOff.x, _topOff.y);
|
||||||
|
|
||||||
|
// paint the current sand frame
|
||||||
|
gfx.setClip(oclip);
|
||||||
|
int sandtop = _topRect.y + _topRect.height;
|
||||||
|
if (_sandY < _botRect.height) {
|
||||||
|
gfx.clipRect(_botRect.x, sandtop, _botRect.width, _sandY);
|
||||||
|
}
|
||||||
|
_sandTrickle.paintFrame(gfx, _sandFrame,
|
||||||
|
_botRect.x + (_botRect.width - _sandTrickle.getWidth(_sandFrame))/2,
|
||||||
|
sandtop);
|
||||||
gfx.setClip(oclip);
|
gfx.setClip(oclip);
|
||||||
|
|
||||||
// Paint the current bottom sand level
|
// Paint the current bottom sand level
|
||||||
@@ -112,13 +128,18 @@ public class HourglassView extends TimerView
|
|||||||
_sandBottom.paint(gfx, _botOff.x, _botOff.y);
|
_sandBottom.paint(gfx, _botOff.x, _botOff.y);
|
||||||
gfx.setClip(oclip);
|
gfx.setClip(oclip);
|
||||||
|
|
||||||
// Paint the sand trickle
|
// untranslate
|
||||||
gfx.setColor(_sandColor);
|
|
||||||
gfx.fillRect(_hourglass.getWidth() / 2, _sandY, 1, 1);
|
|
||||||
|
|
||||||
gfx.translate(-_bounds.x, -_bounds.y);
|
gfx.translate(-_bounds.x, -_bounds.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the y position of the trickle.
|
||||||
|
*/
|
||||||
|
protected void setSandTrickleY ()
|
||||||
|
{
|
||||||
|
_sandY = (int) (_botRect.height * Math.min(1f, (_complete / .025)));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the current top coordinate of the bottom sand pile.
|
* Returns the current top coordinate of the bottom sand pile.
|
||||||
*/
|
*/
|
||||||
@@ -134,19 +155,21 @@ public class HourglassView extends TimerView
|
|||||||
/** Offsets at which to render the sand images. */
|
/** Offsets at which to render the sand images. */
|
||||||
protected Point _topOff, _botOff;
|
protected Point _topOff, _botOff;
|
||||||
|
|
||||||
/** The y-coordinate of the current sand bit trickling down. */
|
|
||||||
protected int _sandY;
|
|
||||||
|
|
||||||
/** The color of the rendered sand. */
|
|
||||||
protected Color _sandColor;
|
|
||||||
|
|
||||||
/** Our images. */
|
/** Our images. */
|
||||||
protected Mirage _hourglass, _sandTop, _sandBottom;
|
protected Mirage _hourglass, _sandTop, _sandBottom;
|
||||||
|
|
||||||
/** The last time the sand pixel moved. */
|
/** The sand trickle. */
|
||||||
protected long _sandTime = 0;
|
protected MultiFrameImage _sandTrickle;
|
||||||
|
|
||||||
/** How many milliseconds it takes the animated sand drop to move
|
/** The last time the sand updated moved. */
|
||||||
* one pixel. */
|
protected long _sandStamp;
|
||||||
protected static final long SAND_RATE = 100;
|
|
||||||
|
/** The next sand frame to be painted. */
|
||||||
|
protected int _sandFrame;
|
||||||
|
|
||||||
|
/** The position of the top of the sand. */
|
||||||
|
protected int _sandY;
|
||||||
|
|
||||||
|
/** How often the sand frame updates. */
|
||||||
|
protected static final long SAND_RATE = 80;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user