Follow-up: Score view
Follow-up: Score view
- Subject: Follow-up: Score view
- From: Steve Klingsporn <email@hidden>
- Date: Thu, 7 Nov 2002 05:35:02 -0600
I was using 4 NSTextFields, two pairs overlapping each other, their
bounds offset up and left by 1 pixel. There were a couple of problems
in using this approach:
- The code to update the scores (posted earlier) is nasty.
- Keeping the views synced with each other didn't always work out
nicely.
- NSTextField is not thread-safe, so it's icky-bad to set the text,
color, etc. from a background thread (app crashes hard).
- Using NSEvents was a bad call, because updating didn't happen when
updates were needed.
I wrote my own custom NSView, and am sharing it here for those
interested.
-Steve
import com.apple.cocoa.foundation.*;
import com.apple.cocoa.application.*;
/**
A custom view class that displays two scores
**/
public class AtaxxScoreView
extends NSView
{
/* The colors for drawing the scoreboard */
public static final NSColor FOREGROUND_COLOR =
NSColor.colorWithCalibratedRGB(.20f, .20f, .20f, 1.0f);
public static final NSColor BACKGROUND_COLOR = NSColor.whiteColor();
public static final NSColor FOREGROUND_UP_COLOR =
NSColor.blackColor();
public static final NSColor BACKGROUND_UP_COLOR =
NSColor.grayColor();
/* The (four) attributed score strings ((back, front) * 2) */
private NSMutableAttributedString mScoreStrings[] = null;
/**
Constructor
@param frame the frame rectangle
**/
public AtaxxScoreView(NSRect frame)
{
super(frame);
}
/**
Tells the window server that we are not
opaque.
@returns false, as we are transparent.
**/
public boolean isOpaque()
{
return false;
}
/**
Draws the view
@param rect the update rectangle
**/
public void drawRect(NSRect rect)
{
if (mScoreStrings != null)
{
NSMutableRect stencil = new NSMutableRect(bounds());
stencil.insetRect(0.5f, 1.0f);
stencil.setOrigin(new NSPoint(1.0f, 0.0f));
NSGraphics.drawAttributedString(mScoreStrings[0], stencil);
NSGraphics.drawAttributedString(mScoreStrings[2], stencil);
stencil.setOrigin(new NSPoint(0.0f, 1.0f));
NSGraphics.drawAttributedString(mScoreStrings[1], stencil);
NSGraphics.drawAttributedString(mScoreStrings[3], stencil);
}
}
/**
Creates the 4 attributed score strings, based on
the current score strings and player that is up.
@param player1 the string for the first player
@param player2 the string for the second player
@param up the player that is up (hilighted)
(0 = none, 1 = player 1, 2 = player 2)
**/
public void update(String player1, String player2, int up)
{
/* For convenience in looping through the strings */
String strings[] = { player1, player1, player2, player2 };
/* Create the string array, if need be */
if (mScoreStrings == null)
mScoreStrings = new NSMutableAttributedString[4];
for (int i = 0; i < 4; i += 2)
{
/* Create the pair of attributed strings
(foreground and background) */
mScoreStrings[i] =
new NSMutableAttributedString(strings[i]);
mScoreStrings[i + 1] =
new NSMutableAttributedString(strings[i + 1]);
/* Create a range for the string pair */
NSRange range = new NSRange(0, strings[i].length());
/* Set the text alignment */
NSMutableParagraphStyle paragraphStyle =
new NSMutableParagraphStyle();
paragraphStyle.setAlignment((i < 2) ?
NSText.LeftTextAlignment :
NSText.RightTextAlignment);
mScoreStrings[i].addAttributeInRange(
NSAttributedString.ParagraphStyleAttributeName,
paragraphStyle, range);
mScoreStrings[i + 1].addAttributeInRange(
NSAttributedString.ParagraphStyleAttributeName,
paragraphStyle, range);
/* Set the font to system font size 10 (loc?) */
NSFont font = NSFont.systemFontOfSize(10);
mScoreStrings[i].addAttributeInRange(
NSAttributedString.FontAttributeName,
font, range);
mScoreStrings[i + 1].addAttributeInRange(
NSAttributedString.FontAttributeName,
font, range);
/* Set the foreground and background colors */
NSColor foregroundColor = FOREGROUND_COLOR;
NSColor backgroundColor = BACKGROUND_COLOR;
if ((i < 2 && up == 1) ||
(i > 1 && up == 2))
{
foregroundColor = FOREGROUND_UP_COLOR;
backgroundColor = BACKGROUND_UP_COLOR;
}
mScoreStrings[i].addAttributeInRange(
NSAttributedString.ForegroundColorAttributeName,
backgroundColor, range);
mScoreStrings[i + 1].addAttributeInRange(
NSAttributedString.ForegroundColorAttributeName,
foregroundColor, range);
/* Fix up the attributes, whatever that does */
mScoreStrings[i].fixAttributesInRange(range);
mScoreStrings[i + 1].fixAttributesInRange(range);
/* Redraw */
display();
}
}
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.