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:
@@ -56,6 +56,12 @@ import com.threerings.media.sprite.ButtonSprite;
|
|||||||
import com.threerings.media.sprite.Sprite;
|
import com.threerings.media.sprite.Sprite;
|
||||||
import com.threerings.media.sprite.SpriteManager;
|
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
|
* Provides a useful extensible framework for rendering animated displays
|
||||||
* that use sprites and animations. Sprites and animations can be added to
|
* that use sprites and animations. Sprites and animations can be added to
|
||||||
@@ -185,13 +191,13 @@ public class MediaPanel extends JComponent
|
|||||||
{
|
{
|
||||||
_spritemgr.addSprite(sprite);
|
_spritemgr.addSprite(sprite);
|
||||||
|
|
||||||
if (sprite instanceof ButtonSprite &&
|
if (((sprite instanceof ActionSprite) ||
|
||||||
_buttonSpriteCount++ == 0) {
|
(sprite instanceof HoverSprite)) && (_actionSpriteCount++ == 0)) {
|
||||||
if (_buttonHandler == null) {
|
if (_actionHandler == null) {
|
||||||
_buttonHandler = new ButtonHandler();
|
_actionHandler = new ActionSpriteHandler();
|
||||||
}
|
}
|
||||||
addMouseListener(_buttonHandler);
|
addMouseListener(_actionHandler);
|
||||||
addMouseMotionListener(_buttonHandler);
|
addMouseMotionListener(_actionHandler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,11 +216,10 @@ public class MediaPanel extends JComponent
|
|||||||
{
|
{
|
||||||
_spritemgr.removeSprite(sprite);
|
_spritemgr.removeSprite(sprite);
|
||||||
|
|
||||||
if (sprite instanceof ButtonSprite &&
|
if (((sprite instanceof ActionSprite) ||
|
||||||
--_buttonSpriteCount == 0)
|
(sprite instanceof HoverSprite)) && (--_actionSpriteCount == 0)) {
|
||||||
{
|
removeMouseListener(_actionHandler);
|
||||||
removeMouseListener(_buttonHandler);
|
removeMouseMotionListener(_actionHandler);
|
||||||
removeMouseMotionListener(_buttonHandler);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -224,6 +229,12 @@ public class MediaPanel extends JComponent
|
|||||||
public void clearSprites ()
|
public void clearSprites ()
|
||||||
{
|
{
|
||||||
_spritemgr.clearMedia();
|
_spritemgr.clearMedia();
|
||||||
|
|
||||||
|
if (_actionHandler != null) {
|
||||||
|
removeMouseListener(_actionHandler);
|
||||||
|
removeMouseMotionListener(_actionHandler);
|
||||||
|
_actionSpriteCount = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -607,51 +618,100 @@ public class MediaPanel extends JComponent
|
|||||||
/** Used to keep metrics. */
|
/** Used to keep metrics. */
|
||||||
protected float _dirtyPerTick;
|
protected float _dirtyPerTick;
|
||||||
|
|
||||||
/** Handles button manipulation. */
|
/** Handles ActionSprite/HoverSprite/ArmingSprite manipulation. */
|
||||||
protected class ButtonHandler extends MouseInputAdapter
|
protected class ActionSpriteHandler extends MouseInputAdapter
|
||||||
{
|
{
|
||||||
|
// documentation inherited
|
||||||
public void mousePressed (MouseEvent me)
|
public void mousePressed (MouseEvent me)
|
||||||
{
|
{
|
||||||
Sprite highestHit = _spritemgr.getHighestHitSprite(
|
if (_activeSprite == null) {
|
||||||
me.getX(), me.getY());
|
// see if we can find one
|
||||||
|
Sprite s = getHit(me);
|
||||||
if (highestHit instanceof ButtonSprite &&
|
if (s instanceof ActionSprite) {
|
||||||
((ButtonSprite)highestHit).isEnabled()) {
|
_activeSprite = s;
|
||||||
_activeButton = (ButtonSprite)highestHit;
|
}
|
||||||
_activeButton.setPressed(true);
|
}
|
||||||
|
|
||||||
|
if (_activeSprite instanceof ArmingSprite) {
|
||||||
|
((ArmingSprite) _activeSprite).setArmed(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// documentation inherited
|
||||||
public void mouseReleased (MouseEvent me)
|
public void mouseReleased (MouseEvent me)
|
||||||
{
|
{
|
||||||
if (_activeButton != null) {
|
if ((_activeSprite instanceof ActionSprite) &&
|
||||||
CommandEvent ce = new CommandEvent(MediaPanel.this,
|
_activeSprite.hitTest(me.getX(), me.getY())) {
|
||||||
_activeButton.getActionCommand(),
|
if (_activeSprite instanceof CommandSprite) {
|
||||||
_activeButton.getCommandArgument(),
|
CommandSprite cs = (CommandSprite) _activeSprite;
|
||||||
me.getWhen(), me.getModifiers());
|
Controller.postAction(MediaPanel.this,
|
||||||
Controller.postAction(ce);
|
cs.getActionCommand(), cs.getCommandArgument());
|
||||||
_activeButton.setPressed(false);
|
|
||||||
_activeButton = null;
|
} else {
|
||||||
|
Controller.postAction(MediaPanel.this,
|
||||||
|
((ActionSprite) _activeSprite).getActionCommand());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!(_activeSprite instanceof HoverSprite)) {
|
||||||
|
_activeSprite = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
mouseMoved(me);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// documentation inherited
|
||||||
public void mouseDragged (MouseEvent me)
|
public void mouseDragged (MouseEvent me)
|
||||||
{
|
{
|
||||||
if (_activeButton != null &&
|
if (_activeSprite instanceof ArmingSprite) {
|
||||||
!_activeButton.contains(me.getX(), me.getY())) {
|
((ArmingSprite) _activeSprite).setArmed(
|
||||||
_activeButton.setPressed(false);
|
_activeSprite.hitTest(me.getX(), me.getY()));
|
||||||
_activeButton = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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. */
|
/** The action sprite handler, or null for none. */
|
||||||
protected ButtonHandler _buttonHandler;
|
protected ActionSpriteHandler _actionHandler;
|
||||||
|
|
||||||
/** The number of button sprites being managed. */
|
/** The number of action/hover sprites being managed. */
|
||||||
protected int _buttonSpriteCount;
|
protected int _actionSpriteCount;
|
||||||
|
|
||||||
// used to render performance metrics
|
// used to render performance metrics
|
||||||
protected String _perfStatus = "";
|
protected String _perfStatus = "";
|
||||||
|
|||||||
@@ -29,10 +29,15 @@ import java.awt.Shape;
|
|||||||
import com.samskivert.swing.Label;
|
import com.samskivert.swing.Label;
|
||||||
import com.samskivert.swing.util.SwingUtil;
|
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.
|
* A sprite that acts as a button.
|
||||||
*/
|
*/
|
||||||
public class ButtonSprite extends Sprite
|
public class ButtonSprite extends Sprite
|
||||||
|
implements CommandSprite, ArmingSprite, DisableableSprite
|
||||||
{
|
{
|
||||||
/** The normal, square button style. */
|
/** The normal, square button style. */
|
||||||
public static final int NORMAL = 0;
|
public static final int NORMAL = 0;
|
||||||
@@ -188,9 +193,7 @@ public class ButtonSprite extends Sprite
|
|||||||
_actionCommand = actionCommand;
|
_actionCommand = actionCommand;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// documentation inherited from interface CommandSprite
|
||||||
* Returns the action command generated by this button.
|
|
||||||
*/
|
|
||||||
public String getActionCommand ()
|
public String getActionCommand ()
|
||||||
{
|
{
|
||||||
return _actionCommand;
|
return _actionCommand;
|
||||||
@@ -204,9 +207,7 @@ public class ButtonSprite extends Sprite
|
|||||||
_commandArgument = commandArgument;
|
_commandArgument = commandArgument;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// documentation inherited from interface CommandSprite
|
||||||
* Returns the command argument generated by this button.
|
|
||||||
*/
|
|
||||||
public Object getCommandArgument ()
|
public Object getCommandArgument ()
|
||||||
{
|
{
|
||||||
return _commandArgument;
|
return _commandArgument;
|
||||||
@@ -223,19 +224,14 @@ public class ButtonSprite extends Sprite
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// documentation inherited from interface DisableableSprite
|
||||||
* Checks whether or not this button is enabled.
|
|
||||||
*/
|
|
||||||
public boolean isEnabled ()
|
public boolean isEnabled ()
|
||||||
{
|
{
|
||||||
return _enabled;
|
return _enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// documentation inherited from interface ArmingSprite
|
||||||
* Sets whether or not this button appears pressed
|
public void setArmed (boolean pressed)
|
||||||
* (does not fire an event).
|
|
||||||
*/
|
|
||||||
public void setPressed (boolean pressed)
|
|
||||||
{
|
{
|
||||||
if (_pressed != pressed) {
|
if (_pressed != pressed) {
|
||||||
_pressed = pressed;
|
_pressed = pressed;
|
||||||
@@ -246,7 +242,7 @@ public class ButtonSprite extends Sprite
|
|||||||
/**
|
/**
|
||||||
* Checks whether or not this button appears pressed.
|
* Checks whether or not this button appears pressed.
|
||||||
*/
|
*/
|
||||||
public boolean isPressed ()
|
public boolean isArmed ()
|
||||||
{
|
{
|
||||||
return _pressed;
|
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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user