Archive for the ‘programming’ Category

ComboBox text formatting in AS3

Tuesday, April 15th, 2008

After struggling with it for a while, I’ve finally found the way to set embedFonts and a specific textFormat for all text in a Flash CS3 ComboBox component.

First of all, it is wise to create a subclass of fl.controls.ComboBox to represent your custom comboBox. In this class you can then set some styles in the constructor as follows:

package com.rockabit.utils.as3
{
import fl.controls.ComboBox;
import fl.controls.List;

import flash.text.TextFormat;

/**
* Customized ComboBox component. Graphic changes have been made inside the component itself,
* in the library, changes in textual display are made here.
*/
public class CustomComboBox extends ComboBox
{
private var textFormat:TextFormat;

public function CustomComboBox()
{
super();

textFormat = new TextFormat(”Arial”, 10, 0xffffff);

//textField is the top text, in the closed combobox
textField.setStyle(”embedFonts”, true);
textField.setStyle(”textFormat”, textFormat);

//dropdown is the list that shows when you open the combobox
dropdown.setStyle(”cellRenderer”, CustomCellRenderer);
}

override protected function drawLayout():void
{
super.drawLayout();
textField.y = 0;
}
}
}

So in the constructor, I create a TextFormat instance and assign that to the textField variable. I then set the cellRenderer style of the dropdown list, which is a class that describes how each item in the list is formatted. For that I use another custom class, which extends fl.controls.listClasses.CellRenderer:

package com.rockabit.utils.as3
{
import fl.controls.listClasses.CellRenderer;

import flash.text.TextFormat;

/**
* Custom cellRenderer for comboBox list items.
*/
public class CustomCellRenderer extends CellRenderer
{

public function CustomCellRenderer()
{
super();
this.setStyle(”embedFonts”, true);
this.setStyle(”textFormat”, new TextFormat(”Arial”, 10, 0×000000));
}

}

}

All this class does is extend the default CellRenderer class and set some styles in the constructor.

Using these two simple classes, you can easily make sure that all your fonts are embedded in the comboBox component, and give them custom text formatting.

I’ve made a small example .fla file. You’ll notice that I added the Tahoma font to the library with a linkage class, which I instantiate in the TextFormat constructor parameter to get the correct fontName. Because I set the library font to bitmap text andset embedFonts to true in code, you get a nice crisp font display in your combobox.

Edit: I have now also added functionality to the CustomComboBox class to set vertical positioning of the main textfield. Right now this is hardcoded, but that could be changed to listen to a custom style.

Download source:
http://www.rockabit.com/downloads/as3/combobox_example.zip

Navigatable ArrayCollection for Flex

Tuesday, March 18th, 2008

I’ve written a NavigatableList class to provide the already handy ArrayCollection class with a simple cursor-like navigation option. You can call list.navigateTo() and provide for instance list.next or list.previous. You also specify the class to which items should belong and you can search for an item with list.findItem(property, value). And finally, you can clone it with list.clone().

Download it here

EDIT: the clone() method didn’t work correctly for some reason. I’ve removed it for now.
EDIT: added an equals() method which compares this list to another list. You  must specify the  name of a unique property which can be used to check if two items are the same.

AS 3.0 layout manager

Monday, February 25th, 2008

I’ve created a custom layout manager in AS 3.0 which provides some of the automatic scaling and positioning that Flex provides.

You can register objects and specify a size to maintain relative to the stage size, alignment, padding and set a function that is called when the screen is resized.

This is a first version and it may (and most likely will) change in the future.

Download: LayoutManager.as and LayoutTarget.as

Processing - Arduino message queue

Tuesday, September 18th, 2007

When I was working on the software for my graduation project, I found I needed a way for Processing and Arduino to communicate safely, so that no messages would be overwritten, lost or sent in the wrong order. To this end I wrote a messaging system, the code for which can be downloaded here: http://www.rockabit.com/downloads/message queueing.zip

It basically works like this: processing sends numerical messages to the arduino I/O-board over a serial connection. To ensure safe messaging, every message is preceded by another message that specifies the type of message that follows. Arduino then first receives this type identifier, knows what type of message should follow, and then receives that message and sends back a confirmation to let processing know that the message was received. Only when a message is confirmed will processing send the next message in line. Also, if a message is not received for 2 seconds, it is resent, and if resending has no effect then it is placed at the back of the queue to be tried again later on