|
SYS-CON.TV Webcasts
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Top Links You Must Click On
Flash Skinning the V2 ProgressBar Component, Part 1
Flash Guru Colin Moock Shares His Expertise on Skinning and Customizing V2 components
By: Colin Moock
May. 18, 2005 10:00 AM
This article explains how to completely change the graphical appearance of (i.e., "to skin") the V2 ProgressBar component. Before we begin, a warning: the techniques described in this article apply to the ProgressBar and to many other V2 components, but not to all. Some compo-nents require advanced skinning techniques not discussed in this article. For example, the Button component's skin, perhaps surprisingly, is primarily code-based. Skinning a Button component requires a thorough understanding of the Button skin code. Likewise, the specific steps required to skin components made up of multiple subcomponents, such as the DataGrid or the List, are not covered in this article. This article will help you get started with skinning and will give you the skills to skin many V2 components, but for some components you should expect to do further research (see Further Study at the end of this article for suggested resources). The Basics: Styles Versus Skins And Figure 2 shows what the ProgressBar component looks like with the "themeColor", "fontFamily" and "fontSize" styles set. Here's the code used to style the ProgressBar shown in Figure 2:
pBar.setStyle("themeColor", 0xFF0000);
pBar.setStyle("fontFamily", "Times New Roman");
pBar.setStyle("fontSize", 16);
In Figure 2, the ProgressBar's color is now red instead of green and its text is set in Times New Roman instead of Verdana, but the basic graphical shapes of the ProgressBar are the same as they were in Figure 1. By setting a component's styles, we can gain some control over the appearance of the component, but when we need to change more than just colors and fonts we must skin the component. Skinning a component means replacing the component's graphics entirely, producing a completely new appearance for the component. For example, if we were creating an adventure game we might want to skin our game's ProgressBar to make it look like Figure 3. Later we'll examine how the above "adventure game" ProgressBar was made. But before we do, it's worth pointing out that not everything about the appearance of a component can be changed, even with skins. The internal display system of each component determines what aspects of the component's appearance can be changed. For example, the variable-width "load bar" section of the ProgressBar component shown in Figures 1, 2, and 3 is produced by stretching a thin graphic between the left and right side of the component. Because the graphic is stretched, it must be a simple, abstract shape not, say, a repeating image such as the apple shown in Figure 4. Filling a region with a repeated graphic like the apple in Figure 4 is known as "tiling." Tiled skins are not natively supported by the V2 ProgressBar. Hence, modifying a ProgressBar to look like the one shown in Figure 4 would require custom code and in-depth knowledge of the ProgressBar's own skinning implementation. What, Exactly, Is a "Skin"? Specifically, for a Flash V2 component, a "skin" is a collection of movie clips that contain the component's graphics. Each movie clip represents either an entire state of the component (e.g., enabled, disabled, pressed, etc) or a subsection of a particular state or states. For example, a skin movie clip might contain an entire disabled button face or just the arrow icon for a scrollbar. The skin is broken into multiple sections so that it can be assembled and resized at runtime in response to either user input or application logic. Each movie clip in a skin has a specific Linkage Identifier that is used by the component to attach the movie clip to the Flash movie stage. Once on stage, the movie clips are positioned and scaled to produce the graphical appearance of the component. Figure 5 shows the skin movie clips for the ProgressBar component. The Linkage Identifier for each movie clip is shown in black text. Among the various skin movie clips shown in Figure 5, those whose IDs begin with "ProgTrack" are the background of the ProgressBar while those whose IDs begin with "ProgBar" are the foreground of the ProgressBar the part that stretches horizontally to indicate load progress. Both the "track" (background) and the "bar" (foreground) are made up of three separate sections: a left and right edge the "caps" at the ends of the bar and a middle section that stretches between the caps. When the ProgressBar is resized, the caps remain the same size and are placed at either end of the bar, while the middle section is scaled horizontally to fill the space between the caps. Finally, the ProgIndBar is a special graphic that animates between the left and right caps if the duration of the load operation being represented is unknown. The ProgIndBar movie clip is only used when the ProgressBar instance's indeterminate property is set to true. Changing the Skin of a Single Instance
The first option requires more work, but lets us skin a component on a per-instance basis. The second option is easier to implement, but forces every instance of the component being skinned to use the custom skin. Let's take a look at both options, starting with changing the Linkage Identifiers a component uses when attaching its skin movie clips. The V2 components store the Linkage Identifiers for their skin movie clips in so-called "skin properties". For example, the Linkage Identifiers for the ProgressBar component (as shown in Figure 5) are stored in the following properties: ProgressBar.progTrackLeftName, ProgressBar.progTrackMiddleName, ProgressBar.progTrackRightName, ProgressBar.progBarLeftName, ProgressBar.progBarMiddleName, ProgressBar.progBarRightName, ProgressBar.progBarIndName. We can change which movie clips a component instance uses for its skins by setting its skin properties. Let's see how this works by making a progress bar with a black square for its track's left cap instead of the default rounded grey left cap. Admittedly, this is a boring skin but it demonstrates the general principle. We'll make a more interesting skin later. Follow these steps:
The preceding steps describe how to modify the skin of a component instance placed on stage manually in the Flash MX 2004 authoring tool. We changed the skin of the component by setting the progTrackLeftName property within the component's "initialize" event. But if we had created the instance with code at runtime, we would not be able to use onClipEvent() as we did in Step 9. Instead, we would use the createClassObject() method's initObj parameter to set the skin property on the new component instance. The following code does just that; it creates a ProgressBar instance at runtime with createClassObject() and uses the initObj parameter to set the progTrackLeftName property to "SquareSymbol". The result is exactly the same as that shown in Figure 7.
import mx.controls.ProgressBar;
this.createClassObject(ProgressBar, "pBar", 0,
{progTrackLeftName:"SquareSymbol"});
Where to Find a Component's Skin Properties First, we can look in the Flash MX 2004 Help files. The so-called "ellipsis" release of the Flash authoring tool (Version 7.2) contains updated Help files that include the skin properties for all V2 components. For each component, the skin properties are listed in the following location:
Within that file we can find the following skin property definitions:
var progTrackLeftName:String = "ProgTrackLeft";
var progTrackMiddleName:String = "ProgTrackMiddle";
var progTrackRightName:String = "ProgTrackRight";
var progBarLeftName:String = "ProgBarLeft";
var progBarMiddleName:String = "ProgBarMiddle";
var progBarRightName:String = "ProgBarRight";
var progIndBarName:String = "ProgIndBar";
However, locating the skin properties in the source code for a component's class can be difficult because the code is often not thoroughly commented and the skin property names do not follow a single naming convention. Some skin property names end with the word "Skin", as in "Button.trueUpSkin"; some skin property names end with the word "Icon", as in "RadioButton.falseOverIcon", while still others follow no convention at all, as in "NumericStepper.upArrowDisabled". Hence, the easiest way to determine the skin properties for a class is through the Flash MX 2004 Version 7.2 updated documentation. No matter how you find a component's skin properties, once you know them you can skin the component by setting those properties to the Linkage Identifiers of your own custom skin movie clips. Creating Skins That Can Be Colored with Styles
A detailed look at styleable custom skins is outside the scope of this article, but Figure 8 should give you a sense of what's possible. For more information on associating skins with color styles, see the following document in the Flash MX 2004 Version 7.2 updated Help: Using components > Customizing components > About skinning components > Linking skin color to styles. Changing the Skin of All Instances in a Document
We'll examine the first two techniques in this article; for information on subclassing component classes see the resources under Further Study. Here's an example of the first technique, setting the progTrackLeftName property on the ProgressBar class's prototype:
import mx.controls.ProgressBar;
ProgressBar.prototype.progTrackLeftName = "SquareSymbol";
That code tells the ProgressBar class to use the SquareSymbol movie clip when creating any ProgressBar component's left track cap. If the above code appears on frame 1, then all ProgressBar instances placed on frame 2 or later will be affected. That said, it is still possible to customize the left track cap for any single instance. To customize a single instance, we follow the exact steps we studied earlier in the SquareSymbol example--using onClipEvent(initialize) (for author-time instances) or createClassObject()'s initObj parameter (for runtime instances) to set the instance's progTrackLeftName property to the desired movie clip. For example, to apply a triangular cap to a single ProgressBar instance we would use the following code (assuming we had also created a triangle movie clip and given it a Linkage Identifier of "TriangleSymbol"):
onClipEvent (initialize) {
this.progTrackLeftName = "TriangleSymbol"; }
Now let's consider the second technique for changing the left track cap of all ProgressBar instances in a document using the default Linkage Identifier for our custom left track cap movie clip. We'll start by determining the default Linkage Identifier for the skin movie clip we want to customize, and then we'll assign that Linkage Identifier to our own custom skin movie clip, overriding the component's default skin. Let's return to the document we created earlier and follow these steps to see how it works:
Overriding a component's skin movie clip is an attractive way to skin components, especially for designers, because it involves no code whatsoever. However, for a complicated component, creating skin movie clips and assigning Linkage Identifiers can be time consuming. Fortunately, we can use so-called "themes" to reduce the labour required to override the skin of every component instance in a document. Themes
Here's how it works. Each theme has an entire set of skin movie clips for every available V2 component. To skin a component in a new document, we simply copy the component's skin movie clips from one of the supplied themes into the new document, then alter the graphics as desired. PRESTO! Our new skin will automatically be applied to all component instances in the new document. Let's use this "copy-from-theme" approach to build the adventure-game ProgressBar example shown earlier in Figure 3. But as we build the example, remind yourself that this technique works only with components that use graphics-based skins. As we learned earlier, components that are nested or use code-based skins require advanced techniques that are not covered here, but can be found in the Flash MX 2004 Version 7.2 help. The Adventure-Game ProgressBar Example Before we start, here's a warning to heed when using ProgressBar with Loader: when a ProgressBar's mode property is set to "event" (the default) it won't broadcast complete() or progress() events. If you want to register for events from a ProgressBar make sure its mode is set to "polled" or "manual". If you are using the ProgressBar in "event" mode then you should register for events with the ProgressBar's source rather than with the ProgressBar itself. Bearing that warning in mind, follow these steps to create a ProgressBar linked to a Loader component:
// Make the load bar red.
pBar.setStyle("themeColor", 0xFF0000);
// Tell the ProgressBar to show progress for the Loader.
// (The source property specifies the object for which
// load progress will be displayed.) pBar.source = loader;
// Load an image into the Loader.
loader.contentPath = "http://marsrovers.jpl.nasa.gov/gallery/press/spirit/"
+ "20050113a/site_A89_CY_navcam_360_cyl-A365R1.jpg";
Our ProgressBar is now fully functional. Let's get skinning. First, we need to copy the ProgressBar skin assets from the HaloTheme.fla file to our document's Library. Follow these steps:
In the Elements folder (shown in Figure 10), you should recognize the following symbols as the ProgressBar skin movie clips (shown earlier in Figure 5): ProgBarLeft, ProgBarMiddle, ProgBarRight, ProgIndBar, ProgTrackLeft, ProgTrackMiddle, and ProgTrackRight. Those symbols are the movie clips that you would normally redesign when skinning the ProgressBar. In addition to the skin movie clips from Figure 5, the Elements folder also contains a series of symbols whose names end with "ThemeColor": ProgBarCapThemeColor, ProgBarIndMiddleThemeColor, ProgBarIndThemeColor, and ProgBarMiddleThemeColor. Those movie clips contain the raw shapes for the caps and middles of the ProgressBar's track (background), load bar (foreground), and indeterminate bar (animated bar that indicates an unknown load progress). The raw shapes are contained by the "ThemeColor" movie clips so that they can be associated with a style (exactly like we saw earlier under "Creating skins that can be colored with styles"). Each "ThemeColor" movie clip contains the following single line of code, which causes its color to change when the "themeColor" style is set:
mx.skins.ColoredSkinElement.setColorStyle(this, "themeColor");
Finally, the ProgressBar Assets folder itself contains a single movie clip symbol named ProgressBarAssets. That symbol simply forces the ProgressBar skins to export with the movie. For our purposes, we're only interested in skinning the track (background) of the ProgressBar, so we'll only need to change the following movie clips: ProgTrackLeft, ProgTrackMiddle, and ProgTrackRight. In addition to changing those movie clips, we'll also create three new movie clips to contain our track's highlight shapes: ProgTrackLeftHighlight, ProgTrackMiddleHighlight, and ProgTrackRightHighlight. We place the highlight shapes in movie clips so that we can associate them with the "themeColor" style (i.e., so that they change color whenever the "themeColor" style is set). The highlight movie clips are nested inside the track movie clips. Figure 11 shows the completed skin movie clips for our ProgressBar skin. Notice that the ProgTrackMiddle movie clip has a transparent (i.e., 0% alpha) shape just above the ProgTrackMiddleHighlight movie clip. The transparent shape forces the track's middle section to line up with the left and right caps. Transparent shapes are often used to force proper alignment in graphical component skins. Now follow these steps to apply the adventure-game style skin to the ProgressBar in skinnedV2ProgressBar.fla.
Further Study Reader Feedback: Page 1 of 1
Enterprise Open Source Magazine Latest Stories . . .
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||