Quick test of FCSS
Sunday, February 14th, 2010 | Author: dehash
Spent an hour testing the cool Flash AS3 FCSS library. Seems fine to set any public property so it can trigger tweens, image loads, filters etc as in this example
There is a good FCSS video tutorial here
Trees image from here released under CC license
Code below all in one file for now as it was just a quick test. Aside from FCSS it also uses three other AS3 libraries – Tweener and MinimalComps and BumpSlide
You can view the swf here
- /**
- * v0.1 code by dehash.com 2010
- * Quick Test of FCSS
- * Released as open source under the BSD License
- * http://www.opensource.org/licenses/bsd-license.php
- */
- package {
- // http://code.google.com/p/minimalcomps/
- import com.bit101.components.*;
- // http://github.com/theflashbum/fcss
- import com.flashartofwar.fcss.applicators.StyleApplicator;
- import com.flashartofwar.fcss.managers.SingletonManager;
- import com.flashartofwar.fcss.styles.IStyle;
- import com.flashartofwar.fcss.stylesheets.*;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.MouseEvent;
- // trees image from http://www.flickr.com/photos/cybot586/4356730349/
- public class Main extends Sprite
- {
- private var css:XML = <css><![CDATA[
- #boxStyle {
- x: 50;
- y: 10;
- alpha: 0.95;
- width: 300;
- height: 250;
- color: 0x1C2331;
- tweenYto: 333;
- dropShadowDistance: 20;
- loadImage: 4356730349_2cfcbdff4d.jpg;
- }
- ]]>
- </css>;
- private var styleSheetCollection:IStyleSheetCollection =
- SingletonManager.getClassReference(StyleSheetCollection) as IStyleSheetCollection;
- private var txt:Text;
- private var box:BoxExtended;
- public function Main():void
- {
- if (stage) init();
- else addEventListener(Event.ADDED_TO_STAGE, init);
- }
- private function init(e:Event = null):void
- {
- removeEventListener(Event.ADDED_TO_STAGE, init);
- // entry point
- addControls();
- addBox();
- }
- private function addControls():void {
- var window:Window = new Window(this, 400, 50, "Box CSS Window");
- window.width = 320;
- window.height = 460;
- var vBox:VBox = new VBox(window.content);
- txt = new Text(vBox, 10, 20);
- txt.text = css.toString();
- txt.width = 300;
- txt.height = 400;
- new PushButton(vBox, 180 , 0, "Apply CSS to Box", parseButtonHandler);
- }
- private function parseButtonHandler(e:MouseEvent):void {
- var styleSheet:IStyleSheet = new FStyleSheet( );
- styleSheet.parseCSS(txt.text.toString());
- styleSheetCollection.removeStyleSheet("boxStyleSheet");
- styleSheetCollection.addStyleSheet(styleSheet, "boxStyleSheet");
- var boxStyle:IStyle = styleSheetCollection.getStyle("#boxStyle");
- var styleApplicator:StyleApplicator = new StyleApplicator();
- styleApplicator.applyStyle(box,boxStyle);
- }
- private function addBox():void {
- box = new BoxExtended();
- addChild(box);
- }
- }
- }
- // http://code.google.com/p/tweener/
- import caurina.transitions.Tweener;
- import flash.filters.DropShadowFilter;
- // http://code.google.com/p/bumpslide/
- import com.bumpslide.events.UIEvent;
- import com.bumpslide.ui.Box;
- import com.bumpslide.ui.Image;
- class BoxExtended extends Box {
- private var image:Image = new Image(null, Image.SCALE_CROP, -1, -1, imageLoaded);
- private var _tweenYto:Number;
- private var _loadImage:String;
- private var _dropShadowDistance:Number;
- public function BoxExtended() {
- super(0xccccff,100,100,20,20);
- }
- public function set tweenYto(value:Number):void {
- _tweenYto = value;
- Tweener.addTween(this, { y:_tweenYto, time:2.5, delay:0.5, transition:"easeInOutExpo" } );
- }
- public function set loadImage(value:String):void {
- _loadImage = "/demoimages/"+ value.split("/").pop();
- if (_loadImage == "null") {
- tileBitmap = null;
- }else {
- image.url = _loadImage;
- }
- }
- public function set dropShadowDistance(value:Number):void {
- _dropShadowDistance = value;
- filters = [new DropShadowFilter(_dropShadowDistance)];
- }
- private function imageLoaded(e:UIEvent):void {
- tileBitmap = image.imageBitmap.bitmapData;
- }
- }



This is great, glad to see you understand some of the power F*CSS has to offer outside of TextFields. Can’t wait to see what you come up with next!
Useful library you have made there so thanks. Thinking of what is the best way to integrate it into RobotLegs. Some sort of automation would be good – so that the mediators auto apply the appropriate style to their view component. Looks a good plan to me. I will be keeping a look out for more F*CSS stuff.
Something I am working on now related to different approaches to control RobotLegs and FCSS integration – one of the less common approaches is as follows:
Context:
mediatorMap.mapView(TextField, TextFieldMediator);…
Mediator:
override public function preRegister():void {trace("TextFieldMediator>preRegister")
view.border = true;
super.preRegister();
}
override public function onRegister():void {
trace("TextFieldMediator>onRegister")
}
Then hook TextFieldMediator up to FCSS and…
Hooking up a custom component to be styled using FCSS via its mediator is quite straightforward so there are plenty of other ways to css style a textfield. Doing it with built in classes like the above could be useful though – probably not an approach to over use depending on resources/performance needs.