3

I need to do some emulation of some old DOS or mainframe terminals in Flex. Something like the image below for example.

alt text

The different coloured text is easy enough, but the ability to do different background colours, such as the yellow background is beyond the capabilities of the standard Flash text.

I may also need to be able to enter text at certain places and scroll text up the "terminal". Any idea how I'd attack this? Or better still, any existing code/components for this sort of thing?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Evan
  • 18,183
  • 8
  • 41
  • 48

2 Answers2

2

Use TextField.getCharBoundaries to get a rectangle of the first and last characters in the areas where you want a background. From these rectangles you can construct a rectangle that spans the whole area. Use this to draw the background in a Shape placed behind the text field, or in the parent of the text field.

Update you asked for an example, here is how to get a rectangle from a range of characters:

var firstCharBounds : Rectangle = textField.getCharBoundaries(firstCharIndex);
var lastCharBounds  : Rectangle = textField.getCharBoundaries(lastCharIndex);

var rangeBounds : Rectangle = new Rectangle();

rangeBounds.topLeft = firstCharBounds.topLeft;
rangeBounds.bottomRight = lastCharBounds.bottomRight;

If you want to find a rectangle for a whole line you can do this instead:

var charBounds : Rectangle = textField.getCharBoundaries(textField.getLineOffset(lineNumber));

var lineBounds : Rectangle = new Rectangle(0, charBounds.y, textField.width, firstCharBounds.height);

When you have the bounds of the text range you want to paint a background for, you can do this in the updateDisplayList method of the parent of the text field (assuming the text field is positioned at [0, 0] and has white text, and that textRangesWithYellowBackground is an array of rectangles that represent the text ranges that should have yellow backgrounds):

graphics.clear();

// this draws the black background
graphics.beginFill(0x000000);
graphics.drawRect(0, 0, textField.width, textField.height);
graphics.endFill();

// this draws yellow text backgrounds
for each ( var r : Rectangle in textRangesWithYellowBackground )
    graphics.beginFill(0xFFFF00);
    graphics.drawRect(r.x, r.y, r.width, r.height);
    graphics.endFill();
}
Theo
  • 131,503
  • 21
  • 160
  • 205
  • Oh sure, show me up by giving more detailed information. ;-D Upvoted! – Adam Davis Sep 13 '08 at 15:12
  • I don't suppose you could add a fully working mxml example file that does this? I'm having trouble getting my head fully around it... – Evan Sep 15 '08 at 00:55
  • I don't mean to be unhelpful, but no. I think that the code I've shown is what you need to solve the problem. The rest is just plumbing. – Theo Sep 15 '08 at 07:51
1

The font is fixed width and height, so making a background bitmap dynamically isn't difficult, and is probably the quickest and easiest solution. In fact, if you size it correctly there will only be one stretched pixel per character.

Color the pixel (or pixels) according to the background of the character.

-Adam

Adam Davis
  • 91,931
  • 60
  • 264
  • 330