Brought up to date and made easier to subclass.

git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@54 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
Ray Greenwell
2006-08-24 00:14:15 +00:00
parent eed4d67b9e
commit b118e302a3
+102 -62
View File
@@ -1,5 +1,6 @@
package com.threerings.ezgame { package com.threerings.ezgame {
import flash.display.DisplayObject;
import flash.display.Sprite; import flash.display.Sprite;
import flash.text.StyleSheet; import flash.text.StyleSheet;
@@ -19,26 +20,90 @@ import flash.text.TextFieldAutoSize;
* free to copy/modify or extend this class. * free to copy/modify or extend this class.
*/ */
public class PlayersDisplay extends Sprite public class PlayersDisplay extends Sprite
implements Game implements Game, StateChangedListener
{ {
// implementation of Game method // implementation of Game method
public function setGameObject (gameObj :EZGame) :void public function setGameObject (gameObj :EZGame) :void
{ {
_gameObj = gameObj; _gameObj = gameObj;
_gameObj.addEventListener(StateChangedEvent.TURN_CHANGED, recheckTurn);
_gameObj.addEventListener(StateChangedEvent.GAME_ENDED, recheckTurn);
setupPlayers(); configureInterface();
}
// from StateChangedListener
public function stateChanged (event :StateChangedEvent) :void
{
displayCurrentTurn();
} }
/** /**
* Set up the player labels and configure the look of the entire UI. * Set up the player labels and configure the look of the entire UI.
*/ */
protected function setupPlayers () :void protected function configureInterface () :void
{ {
var y :Number = BORDER; var border :int = getBorderSpacing();
var pad :int = getInternalSpacing();
var y :Number = border;
var maxWidth :Number = 0;
var label :TextField;
var icon :DisplayObject;
// create a label at the top, above the player names // create a label at the top, above the player names
var label :TextField = new TextField(); label = createHeader();
if (label != null) {
label.x = border;
label.y = y;
addChild(label);
y += label.textHeight + pad;
maxWidth = label.textWidth;
}
// create a label for each player
var playerIndex :int = 0;
for each (var name :String in _gameObj.getPlayerNames()) {
label = createPlayerLabel(playerIndex, name);
icon = createPlayerIcon(playerIndex, name);
var iconW :int = 0;
var iconH :int = 0;
if (icon != null) {
iconW = icon.width + pad;
iconH = icon.height;
icon.x = border;
icon.y = y;
addChild(icon);
}
label.x = border + iconW;
label.y = y;
addChild(label);
y += Math.max(label.textHeight, iconH) + pad;
maxWidth = Math.max(maxWidth, iconW + label.textWidth);
_playerLabels.push(label);
playerIndex++;
}
// make all the player labels the same width
// (looks nice when highlighted)
for each (label in _playerLabels) {
label.autoSize = TextFieldAutoSize.NONE;
label.width = maxWidth - (label.x - border);
}
// y has a pad at the end, we want border instead
y += border - pad;
// draw a blue rectangle around everything
graphics.clear();
graphics.lineStyle(1, 0x0000FF);
graphics.drawRect(0, 0, maxWidth + (border * 2), y);
displayCurrentTurn();
}
protected function createHeader () :TextField
{
var label :TextField = createHeader();
// damn stylesheet doesn't seem to actually -work- // damn stylesheet doesn't seem to actually -work-
// var style :StyleSheet = new StyleSheet(); // var style :StyleSheet = new StyleSheet();
// style.fontWeight = "bold"; // style.fontWeight = "bold";
@@ -49,43 +114,40 @@ public class PlayersDisplay extends Sprite
label.autoSize = TextFieldAutoSize.LEFT; label.autoSize = TextFieldAutoSize.LEFT;
label.selectable = false; label.selectable = false;
label.text = "Players"; label.text = "Players";
label.x = BORDER; return label;
label.y = y; }
addChild(label);
y += label.textHeight;
var maxWidth :Number = label.textWidth; /**
* Create a TextArea that will be used to display player names.
*/
protected function createPlayerLabel (idx :int, name :String) :TextField
{
var label :TextField = new TextField();
label.autoSize = TextFieldAutoSize.LEFT;
label.background = true;
label.selectable = false;
label.text = name;
return label;
}
// create a label for each player protected function createPlayerIcon (idx :int, name :String) :DisplayObject
for each (var name :String in _gameObj.getPlayerNames()) { {
y += PAD; return null;
label = new TextField(); }
label.autoSize = TextFieldAutoSize.LEFT;
label.background = true;
label.selectable = false;
label.text = name;
label.x = BORDER;
label.y = y;
addChild(label);
y += label.textHeight;
maxWidth = Math.max(maxWidth, label.textWidth);
_playerLabels.push(label); protected function getBackground (isTurn :Boolean) :uint
} {
return isTurn ? 0xFF9999 : 0xFFFFFF;
}
// make all the player labels the same width protected function getBorderSpacing () :int
// (looks nice when highlighted) {
for each (label in _playerLabels) { return 6;
label.autoSize = TextFieldAutoSize.NONE; }
label.width = maxWidth;
}
// draw a blue rectangle around everything protected function getInternalSpacing () :int
graphics.clear(); {
graphics.lineStyle(1, 0x0000FF); return 2;
graphics.drawRect(0, 0, maxWidth + (BORDER * 2), y + BORDER);
displayCurrentTurn();
} }
/** /**
@@ -97,36 +159,14 @@ public class PlayersDisplay extends Sprite
var idx :int = _gameObj.isInPlay() ? _gameObj.getTurnHolderIndex() : -1; var idx :int = _gameObj.isInPlay() ? _gameObj.getTurnHolderIndex() : -1;
for (var ii :int = 0; ii < _playerLabels.length; ii++) { for (var ii :int = 0; ii < _playerLabels.length; ii++) {
var label :TextField = (_playerLabels[ii] as TextField); var label :TextField = (_playerLabels[ii] as TextField);
label.backgroundColor = (ii == idx) ? TURN_BACKGROUND label.backgroundColor = getBackground(ii == idx);
: NORMAL_BACKGROUND;
} }
} }
/**
* Registered to receive TURN_CHANGED and GAME_ENDED events
* from the GameObject.
*/
protected function recheckTurn (event :StateChangedEvent) :void
{
displayCurrentTurn();
}
/** Our game object. */ /** Our game object. */
protected var _gameObj :EZGame; protected var _gameObj :EZGame;
/** An array of labels, one for each player name. */ /** An array of labels, one for each player name. */
protected var _playerLabels :Array = []; protected var _playerLabels :Array = [];
/** The background color for players that doesn't have the turn. */
protected static const NORMAL_BACKGROUND :uint = 0xFFFFFF;
/** The background color for players that do have the turn. */
protected static const TURN_BACKGROUND :uint = 0xFF9999;
/** The number of pixels to border around all the names. */
protected static const BORDER :int = 6;
/** The additional padding inserted between names. */
protected static const PAD :int = 2;
} }
} }