Added a bevy of interfaces that Sprites can implement to be recognized

by the MediaPanel as sprites that interact in some standard way with the
mouse.
Modified existing ButtonSprite to work this new way, and it now implements
CommandSprite, ArmingSprite, and DisableableSprite. One method name changed.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3362 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2005-02-22 04:14:44 +00:00
parent 097730e84d
commit 7ec0eeca56
7 changed files with 190 additions and 54 deletions
+99 -39
View File
@@ -56,6 +56,12 @@ import com.threerings.media.sprite.ButtonSprite;
import com.threerings.media.sprite.Sprite;
import com.threerings.media.sprite.SpriteManager;
import com.threerings.media.sprite.action.ActionSprite;
import com.threerings.media.sprite.action.ArmingSprite;
import com.threerings.media.sprite.action.CommandSprite;
import com.threerings.media.sprite.action.DisableableSprite;
import com.threerings.media.sprite.action.HoverSprite;
/**
* Provides a useful extensible framework for rendering animated displays
* that use sprites and animations. Sprites and animations can be added to
@@ -185,13 +191,13 @@ public class MediaPanel extends JComponent
{
_spritemgr.addSprite(sprite);
if (sprite instanceof ButtonSprite &&
_buttonSpriteCount++ == 0) {
if (_buttonHandler == null) {
_buttonHandler = new ButtonHandler();
if (((sprite instanceof ActionSprite) ||
(sprite instanceof HoverSprite)) && (_actionSpriteCount++ == 0)) {
if (_actionHandler == null) {
_actionHandler = new ActionSpriteHandler();
}
addMouseListener(_buttonHandler);
addMouseMotionListener(_buttonHandler);
addMouseListener(_actionHandler);
addMouseMotionListener(_actionHandler);
}
}
@@ -210,11 +216,10 @@ public class MediaPanel extends JComponent
{
_spritemgr.removeSprite(sprite);
if (sprite instanceof ButtonSprite &&
--_buttonSpriteCount == 0)
{
removeMouseListener(_buttonHandler);
removeMouseMotionListener(_buttonHandler);
if (((sprite instanceof ActionSprite) ||
(sprite instanceof HoverSprite)) && (--_actionSpriteCount == 0)) {
removeMouseListener(_actionHandler);
removeMouseMotionListener(_actionHandler);
}
}
@@ -224,6 +229,12 @@ public class MediaPanel extends JComponent
public void clearSprites ()
{
_spritemgr.clearMedia();
if (_actionHandler != null) {
removeMouseListener(_actionHandler);
removeMouseMotionListener(_actionHandler);
_actionSpriteCount = 0;
}
}
/**
@@ -607,51 +618,100 @@ public class MediaPanel extends JComponent
/** Used to keep metrics. */
protected float _dirtyPerTick;
/** Handles button manipulation. */
protected class ButtonHandler extends MouseInputAdapter
/** Handles ActionSprite/HoverSprite/ArmingSprite manipulation. */
protected class ActionSpriteHandler extends MouseInputAdapter
{
// documentation inherited
public void mousePressed (MouseEvent me)
{
Sprite highestHit = _spritemgr.getHighestHitSprite(
me.getX(), me.getY());
if (highestHit instanceof ButtonSprite &&
((ButtonSprite)highestHit).isEnabled()) {
_activeButton = (ButtonSprite)highestHit;
_activeButton.setPressed(true);
if (_activeSprite == null) {
// see if we can find one
Sprite s = getHit(me);
if (s instanceof ActionSprite) {
_activeSprite = s;
}
}
if (_activeSprite instanceof ArmingSprite) {
((ArmingSprite) _activeSprite).setArmed(true);
}
}
// documentation inherited
public void mouseReleased (MouseEvent me)
{
if (_activeButton != null) {
CommandEvent ce = new CommandEvent(MediaPanel.this,
_activeButton.getActionCommand(),
_activeButton.getCommandArgument(),
me.getWhen(), me.getModifiers());
Controller.postAction(ce);
_activeButton.setPressed(false);
_activeButton = null;
if ((_activeSprite instanceof ActionSprite) &&
_activeSprite.hitTest(me.getX(), me.getY())) {
if (_activeSprite instanceof CommandSprite) {
CommandSprite cs = (CommandSprite) _activeSprite;
Controller.postAction(MediaPanel.this,
cs.getActionCommand(), cs.getCommandArgument());
} else {
Controller.postAction(MediaPanel.this,
((ActionSprite) _activeSprite).getActionCommand());
}
}
if (!(_activeSprite instanceof HoverSprite)) {
_activeSprite = null;
}
mouseMoved(me);
}
// documentation inherited
public void mouseDragged (MouseEvent me)
{
if (_activeButton != null &&
!_activeButton.contains(me.getX(), me.getY())) {
_activeButton.setPressed(false);
_activeButton = null;
if (_activeSprite instanceof ArmingSprite) {
((ArmingSprite) _activeSprite).setArmed(
_activeSprite.hitTest(me.getX(), me.getY()));
}
}
protected ButtonSprite _activeButton;
// documentation inherited
public void mouseMoved (MouseEvent me)
{
if (_activeSprite instanceof HoverSprite) {
if (!_activeSprite.hitTest(me.getX(), me.getY())) {
((HoverSprite) _activeSprite).setHovered(false);
_activeSprite = null;
}
}
if (_activeSprite == null) {
Sprite s = getHit(me);
if (s instanceof HoverSprite) {
((HoverSprite) s).setHovered(true);
_activeSprite = s;
}
}
}
/**
* Utility method, get the highest sprite, return it
* if it's enabled.
*/
protected Sprite getHit (MouseEvent me)
{
Sprite s = _spritemgr.getHighestHitSprite(me.getX(), me.getY());
if (!(s instanceof DisableableSprite) ||
((DisableableSprite) s).isEnabled()) {
return s;
} else {
return null;
}
}
/** The active hover sprite, or action sprite. */
protected Sprite _activeSprite;
}
/** The active button handler, or null for none. */
protected ButtonHandler _buttonHandler;
/** The action sprite handler, or null for none. */
protected ActionSpriteHandler _actionHandler;
/** The number of button sprites being managed. */
protected int _buttonSpriteCount;
/** The number of action/hover sprites being managed. */
protected int _actionSpriteCount;
// used to render performance metrics
protected String _perfStatus = "";
@@ -29,10 +29,15 @@ import java.awt.Shape;
import com.samskivert.swing.Label;
import com.samskivert.swing.util.SwingUtil;
import com.threerings.media.sprite.action.ArmingSprite;
import com.threerings.media.sprite.action.CommandSprite;
import com.threerings.media.sprite.action.DisableableSprite;
/**
* A sprite that acts as a button.
*/
public class ButtonSprite extends Sprite
implements CommandSprite, ArmingSprite, DisableableSprite
{
/** The normal, square button style. */
public static final int NORMAL = 0;
@@ -188,9 +193,7 @@ public class ButtonSprite extends Sprite
_actionCommand = actionCommand;
}
/**
* Returns the action command generated by this button.
*/
// documentation inherited from interface CommandSprite
public String getActionCommand ()
{
return _actionCommand;
@@ -204,9 +207,7 @@ public class ButtonSprite extends Sprite
_commandArgument = commandArgument;
}
/**
* Returns the command argument generated by this button.
*/
// documentation inherited from interface CommandSprite
public Object getCommandArgument ()
{
return _commandArgument;
@@ -223,19 +224,14 @@ public class ButtonSprite extends Sprite
}
}
/**
* Checks whether or not this button is enabled.
*/
// documentation inherited from interface DisableableSprite
public boolean isEnabled ()
{
return _enabled;
}
/**
* Sets whether or not this button appears pressed
* (does not fire an event).
*/
public void setPressed (boolean pressed)
// documentation inherited from interface ArmingSprite
public void setArmed (boolean pressed)
{
if (_pressed != pressed) {
_pressed = pressed;
@@ -246,7 +242,7 @@ public class ButtonSprite extends Sprite
/**
* Checks whether or not this button appears pressed.
*/
public boolean isPressed ()
public boolean isArmed ()
{
return _pressed;
}
@@ -0,0 +1,16 @@
//
// $Id$
package com.threerings.media.sprite.action;
/**
* An Action sprite is a sprite that may be pressed to generate an
* ActionEvent that will be posted to the Controller hierarchy.
*/
public interface ActionSprite
{
/**
* @return the action command to submit if this sprite is clicked.
*/
public String getActionCommand ();
}
@@ -0,0 +1,16 @@
//
// $Id$
package com.threerings.media.sprite.action;
/**
* An ActionSprite that wishes to be notified of events when it is armed
* or not.
*/
public interface ArmingSprite extends ActionSprite
{
/**
* Render this sprite such that is is drawn "armed".
*/
public void setArmed (boolean armed);
}
@@ -0,0 +1,16 @@
//
// $Id$
package com.threerings.media.sprite.action;
/**
* Extends CommandSprite to be a sprite that posts CommandEvents to
* the Controller hierarchy.
*/
public interface CommandSprite extends ActionSprite
{
/**
* @return the argument to the action command.
*/
public Object getCommandArgument();
}
@@ -0,0 +1,16 @@
//
// $Id$
package com.threerings.media.sprite.action;
/**
* Indicates a Sprite that may or may not be enabled to receive
* action / hover / arming notifications.
*/
public interface DisableableSprite
{
/**
* @return true if this sprite is currently enabled.
*/
public boolean isEnabled ();
}
@@ -0,0 +1,16 @@
//
// $Id$
package com.threerings.media.sprite.action;
/**
* An interface indicating that a sprite wishes to be notified when
* the mouse hovers over it.
*/
public interface HoverSprite
{
/**
* Set the current hover state.
*/
public void setHovered (boolean hovered);
}