|
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 Customizing Components in Flash MX 2004
Styles and themes: powerful new tools
By: Peter Elst
Nov. 17, 2003 12:00 AM
Building on the success of components in the last release of Flash MX, Macromedia has outdone itself in rebuilding this complete architecture for ActionScript version 2.0. With this latest release we've been introduced to some new and powerful ways of customizing components. Two buzzwords here are "Styles" and "Themes" - the first is largely used for defining font, color, and spacing properties, while Themes deal with the actual skinning of component assets. I'll discuss these two features in detail, as well as touch on some more advanced topics such as inheritance and code-based skinning. Of course, these new goodies come at a price. Established Flash designers and developers will need to invest time getting themselves acquainted with the new feature set in Flash MX 2004, and those new to the software will have a lot of ground to cover. This article is a good start, as it will get you more in touch with the component customization options available and how to use them in this latest release of Macromedia Flash.
Styling Components You can compare Styles to Cascading Style Sheets, though there are subtle (and not so subtle) differences in implementation. If you look at the new CSS support for TextField objects in Flash MX 2004 you'll see that they have a lot in common with the Styles feature used on components. One of the main differences between the two is that components can't parse the W3C standard style properties and use their ActionScript equivalent; more about this later in the article. The basic principle of using Styles is very easy indeed; you only have to use two methods, "getStyle" and "setStyle", to get started. You guessed it, those methods do exactly as their names indicate: set and get style properties. Not very exciting yet, is it? Let's get to the good part…these styles can be layered and applied to various scopes, have inheriting values, and even support custom style declarations (see Image II). It's definitely getting more interesting now. These various scopes on which Styles can be applied are the following: the component instance, a component class, and the _global scope. Let's first look at how to set style properties on a single component instance.
Applying Styles to a
Component Instance
myScrollPane.setStyle("backgroundColo ","0x003366"); If you test this movie you'll see that the ScrollPane component now has a dark blue background color. When you want to retrieve a value for a particular style property on the component, simply use the getStyle method and specify the style property as the first argument. myScrollPane.getStyle("backgroundColor "); // traces out 0x003366 For a full list of all supported style properties, consult the Flash MX 2004 documentation (Help > Using Components > Customizing Components > Using Styles to Customize ... > Supported Styles). Be sure to look at the documentation for each individual component (Help > Using Components > Components Dictionary) since there might be some component-specific styles listed there. Of course, when you've got multiple components on stage you won't want to style them all individually. This is where the other scopes come in handy. The first one we'll look at is the component class.
Applying Styles to a
Component Class Flash MX 2004 stores these class style definitions in the _global. styles scope. This scope contains an object for each component, the object name of which refers to the component class name.
_global.styles.ComboBox.setStyle("back groundColor","0x003366"); If you test this movie you'll see that all ComboBox components on stage have now got a dark blue background color applied. This approach is very useful if you want to use different styles for different components. However, if you want all components on stage to subscribe to your settings, doing it this way could cause you to get finger cramps from defining the styles for every single component class you're using.
Applying Styles
to the _global Scope
_global.style.setStyle("backgroundColo r","0x003366"); If you test this movie now you'll see that all components have a nice dark blue background color. Notice that we used _global.style here, not the _global.styles scope we discussed earlier for the component class style declarations. That all looks very nice, but it's unfortunately not always a one-stop approach to styling all your components. Let's look at a problem with using the _global scope to style components:
Applying styles to component classes is all very well and good, but it might not always be a good solution. What if you want to give a ComboBox and a TextArea component a particular style and a ScrollPane and List component another one? This is where custom style declarations come into play.
Creating Custom Style
Declarations
Now how did we do that? It's not that difficult; first we create custom style objects in the _global.styles scope by instantiating them from the built-in CSSStyleDeclaration class. Having done that we now have two namespaces for applying our styles to: "myStyle1" and "myStyle2". The next thing we do is apply a different value for the backgroundColor style property to each of the custom style declarations. If you look at the last four lines you'll see that we are setting a style property named "styleName" on each of our component instances. This "styleName" property points components to the name of the style declaration in the _global.styles scope we want it to subscribe to. This example, which sets only one style property, doesn't look very productive, but as you'll likely use far more style settings in real-world projects this approach can very productive as opposed to applying all those styles manually to component instances.
Styles and Inheritance This style inheritance for components is a very useful feature when working with nested components. Even components components nested at runtime automatically get these inherited style properties applied.
Dealing with Differently
Scoped Styles on a
Component
The three steps above are the same for any style property that Flash applies to a component. First it looks to see if Styles are assigned to the component instance; if so, it disregards all other scopes. If no Styles were defined on the component instance it looks to see if the "styleName" property was set to point to a custom style declaration. If not, it moves on to the default component class style declaration in the _global.styles scope. So far, so good. Now we come to the inheriting styles part. If a particular style property is set to inherit, Flash first moves on to the parent object of the component and goes looking through those three scopes in that object. Finally, when Flash has finished looking through all parent objects without finding the style to be defined it moves on to step 4, the _global scope. Style properties that aren't set to inherit automatically move on to step 4 if no style definitions were found in any of the other three locations. 4. global style scope (_global.style) We've covered a large portion of the Styles API; if you want more information on subjects such as defining color names and setting inheritance for style properties, see the documentation on the StyleManager class (Help > Using Components > Components Dictionary > StyleManager class).
Skinning Components These compiled clips do a couple of things: they hide the component code from prying eyes, allow for easy distribution of components, and automate things like creating live previews for components. All well and good, but how do we get to the component assets? For customizing component skins Macromedia has introduced the so-called "Themes," which are basically just simple FLA documents that act as containers for all component assets.
Applying a Theme
to Your Project Now let's open the Library panel and see what's really going on in there. You'll find a whole bunch of nested folders and if you browse down through Flash UI Components 2 >Themes > MMDefault you'll find the folders that contain the assets for each of the UI components (Accordion Assets, Alert Assets, etc.). Things are getting a bit more familiar now; you can now simply edit those movieclips and make all the changes you want. When you're done with modifying the component assets, simply drag that folder from the Themes FLA to the stage of the FLA you want to apply the skins to. You can then safely delete those assets from the stage of your FLA (they are still contained the Library panel) and if you test your movie you'll see the new skins applied for the component you customized. Components can also be skinned purely by using ActionScript. A full explanation of this would go beyond the scope of this article, but essentially every component contains properties that point to the linkage identifier for the skins it uses. The Accordion, for example, has four of these properties (falseUpSkin, falseDownSkin, falseOverSkin, trueUpSkin) that each point to the linkage identifier of a skin that represents a particular state of the header bars. If, for example, we want to change the skin for the rollover state of the Accordion header bars we could create a movieclip containing our skin, give it the instance name "rolloverAccordion", for example, and use the following code: myAccordion.falseOverSkin = "rolloverAccordion"; This code assumes that our Accordion component had the instance name "myAccordion". If you test the movie and move your mouse over one of the header bars you'll see your custom skin applied. For more info on skinning the Accordion component, you might want to consult the Flash documentation (Help > Using Components > Components Dictionary > Accordion component)
Conclusion If you want to learn more about it, be sure to read through the Flash MX 2004 documentation, which does a pretty good job of introducing all these topics, and be sure to do some experimenting yourself, as that is definitely the easiest way of getting acquainted with it. If you want to see customization at work on a real-world application, take a look at "aggregator.fla", available at www.sys-con.com/mx/sourcec.cfm. Reader Feedback: Page 1 of 1
Your Feedback
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 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||