Added ButtonSprite class for card game user interfaces.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3187 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Andrzej Kapolka
2004-10-29 00:41:50 +00:00
parent dd1f895190
commit 18611da53d
3 changed files with 310 additions and 29 deletions
@@ -0,0 +1,202 @@
//
// $Id: ButtonSprite.java,v 1.1 2004/10/29 00:41:50 andrzej Exp $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.parlor.card.client;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Shape;
import com.samskivert.swing.Label;
import com.threerings.media.sprite.Sprite;
import com.threerings.parlor.card.Log;
/**
* A sprite that acts as a button.
*/
public class ButtonSprite extends Sprite
{
/**
* Constructs a button sprite.
*
* @param label the label to render on the button
* @param backgroundColor the background color of the button
* @param actionCommand the button's command
* @param commandArgument the button's command argument
*/
public ButtonSprite (Label label, Color backgroundColor,
String actionCommand, Object commandArgument)
{
_label = label;
_backgroundColor = backgroundColor;
_actionCommand = actionCommand;
_commandArgument = commandArgument;
}
/**
* Returns a reference to the label displayed by this sprite.
*/
public Label getLabel ()
{
return _label;
}
/**
* Updates this sprite's bounds after a change to the label.
*/
public void updateBounds ()
{
// size the bounds to fit our label
Dimension size = _label.getSize();
_bounds.width = size.width + PADDING*2;
_bounds.height = size.height + PADDING*2;
}
/**
* Sets the background color of this button.
*/
public void setBackgroundColor (Color backgroundColor)
{
_backgroundColor = backgroundColor;
}
/**
* Returns the background color of this button.
*/
public Color getBackgroundColor ()
{
return _backgroundColor;
}
/**
* Sets the action command generated by this button.
*/
public void setActionCommand (String actionCommand)
{
_actionCommand = actionCommand;
}
/**
* Returns the action command generated by this button.
*/
public String getActionCommand ()
{
return _actionCommand;
}
/**
* Sets the command argument generated by this button.
*/
public void setCommandArgument (Object commandArgument)
{
_commandArgument = commandArgument;
}
/**
* Returns the command argument generated by this button.
*/
public Object getCommandArgument ()
{
return _commandArgument;
}
/**
* Sets whether or not this button is enabled.
*/
public void setEnabled (boolean enabled)
{
if (_enabled != enabled) {
_enabled = enabled;
invalidate();
}
}
/**
* Checks whether or not this button is enabled.
*/
public boolean isEnabled ()
{
return _enabled;
}
/**
* Sets whether or not this button appears pressed
* (does not fire an event).
*/
public void setPressed (boolean pressed)
{
if (_pressed != pressed) {
_pressed = pressed;
invalidate();
}
}
/**
* Checks whether or not this button appears pressed.
*/
public boolean isPressed ()
{
return _pressed;
}
// documentation inherited
protected void init ()
{
super.init();
updateBounds();
}
// documentation inherited
public void paint (Graphics2D gfx)
{
gfx.setColor(_enabled ? _backgroundColor : _backgroundColor.darker());
gfx.fill3DRect(_bounds.x, _bounds.y, _bounds.width,
_bounds.height, !_pressed);
_label.render(gfx, _bounds.x + (_pressed ? PADDING : PADDING - 1),
_bounds.y + (_pressed ? PADDING : PADDING - 1));
}
/** The number of pixels to add between the text and the border. */
protected static final int PADDING = 2;
/** The label associated with this sprite. */
protected Label _label;
/** The action command generated by this button. */
protected String _actionCommand;
/** The command argument generated by this button. */
protected Object _commandArgument;
/** The background color of this sprite. */
protected Color _backgroundColor;
/** Whether or not the button is currently enabled. */
protected boolean _enabled = true;
/** Whether or not the button is currently pressed. */
protected boolean _pressed;
}
@@ -0,0 +1,38 @@
//
// $Id: ButtonSpriteObserver.java,v 1.1 2004/10/29 00:41:50 andrzej Exp $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.parlor.card.client;
import com.samskivert.swing.event.CommandEvent;
/**
* Observer interface for button sprites.
*/
public interface ButtonSpriteObserver
{
/**
* Notifies the observer that the user clicked a button sprite.
*
* @param sprite the clicked sprite
* @param ce the command event associated with the click
*/
public void buttonSpriteClicked (ButtonSprite sprite, CommandEvent ce);
}
@@ -1,5 +1,5 @@
// //
// $Id: CardPanel.java,v 1.4 2004/10/15 18:20:28 andrzej Exp $ // $Id: CardPanel.java,v 1.5 2004/10/29 00:41:50 andrzej Exp $
// //
// Narya library - tools for developing networked games // Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved // Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
@@ -21,10 +21,15 @@
package com.threerings.parlor.card.client; package com.threerings.parlor.card.client;
import java.awt.*; import java.awt.event.MouseAdapter;
import java.awt.event.*; import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.*; import java.util.ArrayList;
import java.util.Iterator;
import com.samskivert.swing.Controller;
import com.samskivert.swing.event.CommandEvent;
import com.samskivert.util.ObserverList; import com.samskivert.util.ObserverList;
@@ -87,6 +92,24 @@ public abstract class CardPanel extends VirtualMediaPanel
protected MouseEvent _me; protected MouseEvent _me;
} }
/** Calls ButtonSpriteObserver.buttonSpriteClicked. */
protected static class ButtonSpriteClickedOp implements ObserverList.ObserverOp
{
public ButtonSpriteClickedOp (ButtonSprite sprite, CommandEvent ce)
{
_sprite = sprite;
_ce = ce;
}
public boolean apply (Object observer)
{
((ButtonSpriteObserver)observer).buttonSpriteClicked(_sprite, _ce);
return true;
}
protected ButtonSprite _sprite;
protected CommandEvent _ce;
}
/** /**
* Constructor. * Constructor.
@@ -106,45 +129,59 @@ public abstract class CardPanel extends VirtualMediaPanel
if (al.size() > 0) { if (al.size() > 0) {
Iterator it = al.iterator(); Iterator it = al.iterator();
int highestLayer = Integer.MIN_VALUE; int highestLayer = Integer.MIN_VALUE;
CardSprite highestSprite = null; Sprite highestSprite = null;
while (it.hasNext()) { while (it.hasNext()) {
Sprite sprite = (Sprite)it.next(); Sprite sprite = (Sprite)it.next();
if (sprite instanceof CardSprite) { if (sprite.getRenderOrder() > highestLayer) {
CardSprite cs = (CardSprite)sprite; highestLayer = sprite.getRenderOrder();
highestSprite = sprite;
if (cs.getRenderOrder() > highestLayer) {
highestLayer = cs.getRenderOrder();
highestSprite = cs;
}
} }
} }
_activeCardSprite = highestSprite; _activeSprite = highestSprite;
if (_activeCardSprite != null) { if (_activeSprite != null) {
_handleX = _activeCardSprite.getX() - me.getX(); if(_activeSprite instanceof CardSprite) {
_handleY = _activeCardSprite.getY() - me.getY(); _handleX = _activeSprite.getX() - me.getX();
_handleY = _activeSprite.getY() - me.getY();
_hasBeenDragged = false; _hasBeenDragged = false;
} else if (_activeSprite instanceof ButtonSprite) {
ButtonSprite bs = (ButtonSprite)_activeSprite;
if(bs.isEnabled()) {
bs.setPressed(true);
}
}
} }
} }
else { else {
_activeCardSprite = null; _activeSprite = null;
} }
} }
public void mouseReleased (MouseEvent me) { public void mouseReleased (MouseEvent me) {
if (_activeCardSprite != null && _hasBeenDragged) { if (_activeSprite instanceof CardSprite && _hasBeenDragged) {
_activeCardSprite.queueNotification( _activeSprite.queueNotification(
new CardSpriteDraggedOp(_activeCardSprite, me) new CardSpriteDraggedOp((CardSprite)_activeSprite, me)
); );
} else if(_activeSprite instanceof ButtonSprite) {
ButtonSprite bs = (ButtonSprite)_activeSprite;
if (bs.isEnabled()) {
CommandEvent ce = new CommandEvent(CardPanel.this, bs.getActionCommand(),
bs.getCommandArgument(), me.getWhen(), me.getModifiers());
bs.queueNotification(
new ButtonSpriteClickedOp(bs, ce));
Controller.postAction(ce);
}
bs.setPressed(false);
_activeSprite = null;
} }
} }
public void mouseClicked (MouseEvent me) { public void mouseClicked (MouseEvent me) {
if (_activeCardSprite != null) { if (_activeSprite instanceof CardSprite) {
_activeCardSprite.queueNotification( _activeSprite.queueNotification(
new CardSpriteClickedOp(_activeCardSprite, me) new CardSpriteClickedOp((CardSprite)_activeSprite, me)
); );
} }
} }
@@ -155,14 +192,18 @@ public abstract class CardPanel extends VirtualMediaPanel
MouseMotionAdapter mma = new MouseMotionAdapter() { MouseMotionAdapter mma = new MouseMotionAdapter() {
public void mouseDragged (MouseEvent me) public void mouseDragged (MouseEvent me)
{ {
if (_activeCardSprite != null && if (_activeSprite instanceof CardSprite &&
_activeCardSprite.isDraggable()) { ((CardSprite)_activeSprite).isDraggable()) {
_activeCardSprite.setLocation( _activeSprite.setLocation(
me.getX() + _handleX, me.getX() + _handleX,
me.getY() + _handleY me.getY() + _handleY
); );
_hasBeenDragged = true; _hasBeenDragged = true;
} else if (_activeSprite instanceof ButtonSprite &&
!_activeSprite.contains(me.getX(), me.getY())) {
((ButtonSprite)_activeSprite).setPressed(false);
_activeSprite = null;
} }
} }
}; };
@@ -186,8 +227,8 @@ public abstract class CardPanel extends VirtualMediaPanel
public abstract Mirage getCardImage (Card card); public abstract Mirage getCardImage (Card card);
/** The last card sprite pressed. */ /** The last sprite pressed. */
protected CardSprite _activeCardSprite; protected Sprite _activeSprite;
/** The location of the cursor in the active sprite. */ /** The location of the cursor in the active sprite. */
protected int _handleX, _handleY; protected int _handleX, _handleY;