From f5e5d273e762453cdcba7586d83e52406d719aaf Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Thu, 28 Apr 2005 23:17:29 +0000 Subject: [PATCH] Moved ScoreAnimation to parlor/media so that games may make use of it. Removed "puzzle fonts" from the narya library (lived in PuzzleBoardView). Left convenience methods in PuzzleBoardView for creation ScoreAnimations, as we like convenience. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3527 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../media}/ScoreAnimation.java | 32 ++++- .../puzzle/client/PuzzleBoardView.java | 114 ++---------------- .../puzzle/drop/client/DropBoardView.java | 54 ++------- 3 files changed, 49 insertions(+), 151 deletions(-) rename src/java/com/threerings/{puzzle/client => parlor/media}/ScoreAnimation.java (68%) diff --git a/src/java/com/threerings/puzzle/client/ScoreAnimation.java b/src/java/com/threerings/parlor/media/ScoreAnimation.java similarity index 68% rename from src/java/com/threerings/puzzle/client/ScoreAnimation.java rename to src/java/com/threerings/parlor/media/ScoreAnimation.java index 34697250c..de2ca8fa4 100644 --- a/src/java/com/threerings/puzzle/client/ScoreAnimation.java +++ b/src/java/com/threerings/parlor/media/ScoreAnimation.java @@ -1,5 +1,5 @@ // -// $Id$ +// $Id: ScoreAnimation.java 3479 2005-04-13 19:06:33Z mdb $ // // Narya library - tools for developing networked games // Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved @@ -19,13 +19,22 @@ // 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.puzzle.client; +package com.threerings.parlor.media; + +import java.awt.Color; +import java.awt.Font; import com.samskivert.swing.Label; + +import com.threerings.media.MediaPanel; import com.threerings.media.animation.FloatingTextAnimation; public class ScoreAnimation extends FloatingTextAnimation { + { // initializer, run automatically with every constructor + setRenderOrder(Integer.MAX_VALUE); + } + /** * Constructs a score animation for the given score value centered at * the given coordinates. @@ -55,4 +64,23 @@ public class ScoreAnimation extends FloatingTextAnimation { super(label, sx, sy, destx, desty, floatPeriod); } + + /** + * Create and configure a Label suitable for a ScoreAnimation with + * all the most common options. + */ + public static Label createLabel (String score, Color c, Font font, + MediaPanel host) + { + Label label = new Label(score); + label.setTargetWidth(host.getWidth()); + label.setStyle(Label.OUTLINE); + label.setTextColor(c); + label.setAlternateColor(Color.BLACK); + label.setFont(font); + label.setAlignment(Label.CENTER); + label.layout(host); + + return label; + } } diff --git a/src/java/com/threerings/puzzle/client/PuzzleBoardView.java b/src/java/com/threerings/puzzle/client/PuzzleBoardView.java index 3174895bf..7e1132c83 100644 --- a/src/java/com/threerings/puzzle/client/PuzzleBoardView.java +++ b/src/java/com/threerings/puzzle/client/PuzzleBoardView.java @@ -33,7 +33,6 @@ import javax.swing.UIManager; import com.samskivert.swing.Label; import com.samskivert.swing.util.SwingUtil; -import com.samskivert.util.RunAnywhere; import com.samskivert.util.StringUtil; import com.threerings.media.VirtualMediaPanel; @@ -42,6 +41,8 @@ import com.threerings.media.animation.AnimationAdapter; import com.threerings.media.image.Mirage; import com.threerings.media.sprite.Sprite; +import com.threerings.parlor.media.ScoreAnimation; + import com.threerings.puzzle.Log; import com.threerings.puzzle.data.Board; import com.threerings.puzzle.data.PuzzleCodes; @@ -103,6 +104,8 @@ public abstract class PuzzleBoardView extends VirtualMediaPanel /** * Set whether this puzzle is paused or not. + * If paused, a label will be displayed with the component's font, + * which may be set with setFont(). */ public void setPaused (boolean paused) { @@ -110,9 +113,10 @@ public abstract class PuzzleBoardView extends VirtualMediaPanel String pmsg = _pctrl.getPauseString(); pmsg = _ctx.getMessageManager().getBundle( PuzzleCodes.PUZZLE_MESSAGE_BUNDLE).xlate(pmsg); + // create a label using our component's standard font _pauseLabel = new Label(pmsg, Label.BOLD | Label.OUTLINE, Color.WHITE, Color.BLACK, - _fonts[getPauseFont()]); + getFont()); _pauseLabel.setTargetWidth(_bounds.width); _pauseLabel.layout(this); } else { @@ -259,28 +263,15 @@ public abstract class PuzzleBoardView extends VirtualMediaPanel * * @param score the score text to display. * @param color the color of the text. - * @param fontSize the size of the text; a value between 0 and {@link - * #getPuzzleFontCount} - 1. + * @param font the font whith which to create the score animation. * @param x the x-position at which the score is to be placed. * @param y the y-position at which the score is to be placed. */ public ScoreAnimation createScoreAnimation ( - String score, Color color, int fontSize, int x, int y) + String score, Color color, Font font, int x, int y) { - // create and configure the label - Label label = new Label(score); - label.setTargetWidth(_bounds.width); - label.setStyle(Label.OUTLINE); - label.setTextColor(color); - label.setAlternateColor(Color.black); - label.setFont(getPuzzleFont(fontSize)); - label.setAlignment(Label.CENTER); - label.layout(this); - - // create the score animation - ScoreAnimation anim = createScoreAnimation(label, x, y); - anim.setRenderOrder(getScoreRenderOrder()); - return anim; + return createScoreAnimation( + ScoreAnimation.createLabel(score, color, font, this), x, y); } /** @@ -293,59 +284,6 @@ public abstract class PuzzleBoardView extends VirtualMediaPanel return new ScoreAnimation(label, x, y); } - /** - * Returns the render order to be assigned to score animations by - * default. Derived classes may wish to override this method to - * change the default render order. - * - * @see #createScoreAnimation - */ - protected int getScoreRenderOrder () - { - return 1; - } - - /** - * Returns the puzzle font to be used for the specified score. - */ - public int getFont (String why, int score, int maxScore) - { - int fontSize = (int)(score*getPuzzleFontCount()/maxScore); - fontSize = Math.min(FONT_SIZES.length-1, fontSize); -// Log.info("Font for " + why + " (" + score + " of " + maxScore + -// ") => " + fontSize + "."); - return fontSize; - } - - /** - * Returns the puzzle font to use for the pause message. - */ - public int getPauseFont () - { - return FONT_SIZES.length - 2; - } - - /** - * Returns the number of different puzzle font sizes so that those who - * care to choose a font size out of the range of possible sizes can - * do so gracefully. - */ - public int getPuzzleFontCount () - { - return FONT_SIZES.length; - } - - /** - * Returns the puzzle font of the specified size. - * - * @param size the desired font size; a value between 0 and {@link - * #getPuzzleFontCount} - 1. - */ - public static Font getPuzzleFont (int size) - { - return _fonts[size]; - } - /** * Positions the supplied animation so as to avoid any active * animations previously registered with this method, and adds the @@ -483,9 +421,6 @@ public abstract class PuzzleBoardView extends VirtualMediaPanel } }; - /** The puzzle fonts. */ - protected static Font[] _fonts; - /** Temporary action debugging. */ protected static boolean DEBUG_ACTION = false; @@ -496,33 +431,4 @@ public abstract class PuzzleBoardView extends VirtualMediaPanel /** The default vertical distance to float score animations. */ protected static final int DEFAULT_SCORE_DISTANCE = 30; - - /** The mid-sized font used as the default size for score - * animations. */ - protected static final int MEDIUM_FONT_SIZE = 1; - - /** The puzzle font sizes. */ - protected static final double[] FONT_SIZES = { - 24d, 30d, 36d, 42d, 52d, 68d }; - - static { - // create the puzzle fonts - Font ofont = UIManager.getFont("Label.serifFont"); - if (ofont == null) { - ofont = new Font("Helvetica", Font.PLAIN, 16); - } - ofont = ofont.deriveFont((float)FONT_SIZES[0]); - _fonts = new Font[FONT_SIZES.length]; - for (int ii = 0; ii < _fonts.length; ii++) { - double scale = FONT_SIZES[ii]/FONT_SIZES[0]; - // MacOS X Java currently doesn't dig scaling fonts with - // affine transformations - if (RunAnywhere.isMacOS()) { - _fonts[ii] = ofont.deriveFont((float)FONT_SIZES[ii]); - } else { - _fonts[ii] = ofont.deriveFont( - AffineTransform.getScaleInstance(scale * 1.1d, scale)); - } - } - } } diff --git a/src/java/com/threerings/puzzle/drop/client/DropBoardView.java b/src/java/com/threerings/puzzle/drop/client/DropBoardView.java index 14f01eb2d..40b7afe4d 100644 --- a/src/java/com/threerings/puzzle/drop/client/DropBoardView.java +++ b/src/java/com/threerings/puzzle/drop/client/DropBoardView.java @@ -23,6 +23,7 @@ package com.threerings.puzzle.drop.client; import java.awt.Color; import java.awt.Dimension; +import java.awt.Font; import java.awt.Graphics2D; import java.awt.Point; import java.awt.Rectangle; @@ -36,9 +37,10 @@ import com.threerings.media.sprite.Sprite; import com.threerings.media.util.LinePath; import com.threerings.media.util.Path; +import com.threerings.parlor.media.ScoreAnimation; + import com.threerings.puzzle.Log; import com.threerings.puzzle.client.PuzzleBoardView; -import com.threerings.puzzle.client.ScoreAnimation; import com.threerings.puzzle.data.Board; import com.threerings.puzzle.data.PuzzleConfig; import com.threerings.puzzle.util.PuzzleContext; @@ -424,20 +426,6 @@ public abstract class DropBoardView extends PuzzleBoardView _remgr.invalidateRegion(x, y, wid, hei); } - /** - * Creates and returns an animation which makes use of a label sprite - * that is assigned a path that floats it a short distance up the - * view, with the label initially centered within the view. - * - * @param score the score text to display. - * @param color the color of the text. - */ - public ScoreAnimation createScoreAnimation (String score, Color color) - { - return createScoreAnimation( - score, color, MEDIUM_FONT_SIZE, 0, _bhei - 1, _bwid, _bhei); - } - /** * Creates and returns an animation showing the specified score * floating up the view, with the label initially centered within the @@ -445,14 +433,13 @@ public abstract class DropBoardView extends PuzzleBoardView * * @param score the score text to display. * @param color the color of the text. - * @param fontSize the size of the text; a value between 0 and {@link - * #FONT_SIZES}.length - 1. + * @param font the font. */ public ScoreAnimation createScoreAnimation ( - String score, Color color, int fontSize) + String score, Color color, Font font) { return createScoreAnimation( - score, color, fontSize, 0, _bhei - 1, _bwid, _bhei); + score, color, font, 0, _bhei - 1, _bwid, _bhei); } /** @@ -461,30 +448,7 @@ public abstract class DropBoardView extends PuzzleBoardView * * @param score the score text to display. * @param color the color of the text. - * @param x the left coordinate in board coordinates of the rectangle - * within which the score is to be centered. - * @param y the bottom coordinate in board coordinates of the - * rectangle within which the score is to be centered. - * @param width the width in board coordinates of the rectangle within - * which the score is to be centered. - * @param height the height in board coordinates of the rectangle - * within which the score is to be centered. - */ - public ScoreAnimation createScoreAnimation ( - String score, Color color, int x, int y, int width, int height) - { - return createScoreAnimation( - score, color, MEDIUM_FONT_SIZE, x, y, width, height); - } - - /** - * Creates and returns an animation showing the specified score - * floating up the view. - * - * @param score the score text to display. - * @param color the color of the text. - * @param fontSize the size of the text; a value between 0 and {@link - * #FONT_SIZES}.length - 1. + * @param font the font to use. * @param x the left coordinate in board coordinates of the rectangle * within which the score is to be centered. * @param y the bottom coordinate in board coordinates of the @@ -495,12 +459,12 @@ public abstract class DropBoardView extends PuzzleBoardView * within which the score is to be centered. */ public ScoreAnimation createScoreAnimation (String score, Color color, - int fontSize, int x, int y, + Font font, int x, int y, int width, int height) { // create the score animation ScoreAnimation anim = - createScoreAnimation(score, color, fontSize, x, y); + createScoreAnimation(score, color, font, x, y); // position the label within the specified rectangle Dimension lsize = anim.getLabel().getSize();