highest safe dose of levitralevitra takecompare levitra cialis viagrageneric levitra onlinecheap herbal vardenafillevitra online uklevitra increases sperm countwomen\s vardenafillevitra samplelevitra slide kitmail order levitralevitra prescription onlinesoft levitraPurchase Levitra pillthe buy levitrageneric levitra canadaproduct team levitravardenafil 90 pills Buylevitra sale 32free sample prescription for levitralevitra trial offermedication levitranon prescription vardenafillevitra online shop in uklevitra medicareinformation levitralevitra on line saleslevitra japanqry vardenafilSale vardenafil 20 pillscheap herbal levitraidentify vardenafillevitra alternative lavitraflomax levitraGeneric Levitra 20 mg Salelevitra couponus vardenafilbuy levitra 10mglevitra commercial modelsforum levitraOnline vardenafil Courier shipping10 mg vs 20 mg levitralevitra dosage directionstoo much levitrawhere to puchase vardenafil onlinecheap levitra on linecialis levitra vardenafilhow levitra workqueen levitravardenafil 180 pills orderlevitra beogradlevitra birth control pillslevitra orallow cost alternatives and vardenafilLevitra 120 pills BuyBuy vardenafil Free Airmail shippingordering levitra onlinecialis levitra viagra comparebuy vardenafil online australiaorder levitra cheap pricevardenafil saleanyone tried generic levitravardenafil softabsbuy levitra viagra onlinelevitra user reviewneed for increasing amounts of levitralevitra online purchasewhat is vardenafilvardenafil rrp australia costvardenafil fast shippingvardenafil no prescriptionlevitra online gamblingviva levitrawomen taking levitradiscount vardenafil onlinelevitra sampleslowest price levitra generic onlinelevitra penslevitra and flomaxpurchase levitra 60 pillslevitra side effectorder Levitra 10 mgalternative to levitraby levitra priceBuy vardenafil Courier shippinguk alternative vardenafillevitra sarasota floridaprice on levitralevitra or viagra which is betterlevitra from canadalevitra ad reviewtry levitra for freeacheter du levitrawholesale vardenafillevitra where to buycomparison levitra cialistoo much vardenafilcomparison of levitra and viagradosage for levitrageneric mexican levitrahow to buy levitragenuine vardenafilcialis levitra viagra which is bettervardenafil onlinerelative costs of cialis levitraget vardenafil drug onlinelevitra vs viagra vs cialisorder levitra 30 pillssample of vardenafildiscount cialis levitra online canadanatural equivelant ingredient in levitraorder levitravardenafil 20 pills onlinecialis vardenafilreviews of viagra cialis and levitraeuropean vardenafilcanada in levitraPurchase Generic vardenafil 20 mglevitra 100mglevitra propafenoneinfo levitrauk levitra saleswoman\s levitraOnline Levitra 120 pillspurchase vardenafil 10 mggeneric levitra tab 20mgvardenafil australiacompare viagra to levitravardenafil 6 free samplesvardenafil canadian drugslevitra generikaOnline Levitra 20 mgwhat does vardenafil dovardenafil uterine thicknesslevitra hourslevitra cost of salesGeneric Levitra 10 mg ordervardenafil lengthvardenafil sideviagra cyalis levitra comparedlevitra pictureslevitra for salevardenafil online cheapLevitra 20 pills BuyLevitra Free Airmail shipping onlinebuy levitra online atgeneric levitra manufacturermaximum dosage levitravardenafil Courier shipping SaleSale vardenafil 10 pillswhat is better levitra or viagradifference between cialis levitra viagralevitracialis compare levitramedical drug levitravardenafil patent levitraprice levitralevitra tablevardenafil vs cialisvardenafil 180 pills Buyeffects of levitraget levitra drug onlinedreampharmaceuticals from levitranew erectile drug vardenafilover the counter levitrabuy levitra australiangeneric vardenafil indiacialis dysfunction erectile levitralevitra side effects visiondiscount levitra pillsOnline vardenafil Free Insurancecanadian pharmacy levitralevitra low costlevitra reviewsPurchase Generic Levitranew erectile drug levitravardenafil usevardenafil pricinglevitra herb alternativeOrder Generic vardenafil 20 mgvardenafil max complaintsfree vardenafil sampleslevitra or viagraSale Levitra 10 mglevitra lowest pricecheap vardenafil siproblems with levitralevitra suppliers in the ukwatermelon levitralevitra to buy new zealandmedicine levitralevitra 10 mg tabletbuy levitra online dream pharmaceuticalsoft levitra mastercardlevitra ingredientsvardenafil perscription onlinegeneric levitra cheapcanadian vardenafillevitra and price listvardenafil hcl 20 mgbayer levitra onlinefree trial of vardenafilbuy levitra delived fed exrecreational levitra usekey buy levitra onlinelevitrabuy cheap levitra onlineGeneric vardenafil 10 mg onlineorder cheap levitra faslevitra by bayervardenafil 30 pills onlineship free vardenafil sampleregalis vardenafilbuy levitra usvardenafil super activeLevitra 180 pills Buydrug levitra newvardenafil purchasevardenafil 30 pills orderprofessional levitra627 discount levitra online 903levitra and premature ejaculationpfizer vardenafilcialis levitra or viagralevitra twice a daysnorting levitraorder 50mg levitrabuy Levitra 10mgLevitra pills Buybuy cheap levitra onlinelevitra vs cialis reviewbrand name levitratake levitralevitra pill pricevardenafil optionslevitra cialis for womenhow to get levitravardenafil alternativespharmacy vardenafilbuy levitra ukvardenafil hcl 20mg tab side effectsbuy levitra in ukgeneric levitra professionallevitra efectosgeneric levitra effectiveorder vardenafil 10 mg2buy levitra online viagralevitra worksflomax and levitraSale Levitra Airmail shippingclinical data levitrageneric levitra soft tab fasti have levitrarx levitravardenafil 120 pillsbuy vardenafil meds onlinevardenafil online no prescriptionis vardenafil professional realcheap vardenafil walmartuk levitrabuy levitra in canadavardenafil from indiafree sample pack of levitramature vardenafilhow long is levitra effectivelevitra price comparisonlevitra storyvardenafil effekter biverkningarvardenafil for teenscheapest levitra in ukfda on levitralevitra fedexgeneric vardenafil mexicosample levitralevitra best resultslevitra substitutelevitra generic soft tabsafe maximum levitra dosagelevitra online cheapLevitra pills onlinevardenafil free sampleslevitra dysfunction erectilelevitra rxlevitra from mexicovardenafil drug prescriptionvardenafil discountsOrder vardenafil Free Insurancelevitra precautionsbuy buy cheap cheap levitra levitralevitra v s levitravardenafil sex dominationlevitra success storieslevitra for womanvardenafil 20 mg paypallevitra sverigecheap levitra overnightOrder Levitra Airmail shippingbuy vardenafil 10 pillscounterfeit levitrabuy levitra 30 pillsvardenafil suppliers in the ukvardenafil effective timeLevitra Courier shipping orderorder levitra from canadabuy drug satellite tv levitralevitra 20order vardenafillevitra wholesale onlinelevitra fast overnight delivery usa onlyuser review levitracialis and levitra generic brandgeneric levitralevitracialis generic levitra viagralevitra online usdoes levitra look likebuy levitra lowest pricescompare viagra levitralevitra on linesample of levitraviagra cialis levitra candian pharmacy safecheap vardenafil nzprice of vardenafilbuy online vardenafilnatural levitrageneric levitra pillsnatural equivalent ingredient in vardenafilordering vardenafillevitra free pillsbuy vardenafil in uklevitra order2buy generic levitralevitra pay with paypalvardenafil in uk onlineglaxo levitravardenafil and commercialvardenafil 50 mglevitra with lisinoprilwhat does levitra dobuy levitra medicationlevitra couchcan a woman take levitravardenafil 10mgreal levitralevitra offervardenafil 10 pills Buylowest prices for levitracanada vardenafilfree levitra pillscheapest online cost for levitravardenafil generic pricevardenafil gelvardenafil mexicovardenafil ukon levitralevitra discussiongeneric name of levitrabuy vardenafil 30 pillslevitra price walmartlevitra time effectiveLevitra Free Airmail shipping Salevardenafil wholesale onlinelevitra andacheter levitra francecheap vardenafil 20 mgwoman in levitra commercialpill identification levitrageneric discount levitralevitra 20 mg dosagelevitra interactionsoriginal levitrapurchase vardenafil 10mgbuy levitra online in 24 hoursvardenafil for sale without a prescriptionlevitrabuy levitra onlineLevitra 10 mg onlineviagra medication prescription levitra cialis propecialevitra anwendungviagra cialis levitra vpxlcan woman take levitrabuy levitra upsLevitra 90 pills BuySale vardenafil pillovernight vardenafilvardenafil reviewslevitra drugswatermellon vardenafilbuy levitra without prescriptionquery lowest levitra price onlinebuying levitra onlinelevitra generic priceusing levitralevitra for freegeneric viagra levitra generic cialis pillsvardenafil testimonialGeneric Levitra 20 mg ordervardenafil on linevardenafil Free Airmail shipping onlinehow long will levitra lastq buy levitraorder discount levitraviagra levitra cialis offerslevitra and bayercheap pharmacy viagra cialis levitralevitra vs viagra reviewswomans levitraburing levitra onlinelevitra softbuy cheap levitralevitra drug prescriptionbuy levitra in englandvardenafil Salevardenafil for daily useactress in levitra commercialbuy levitra with pay palcheap vardenafil overnightindia vardenafilvardenafil pill orderbuy levitra online 35008 buylevitra pill cuttervardenafil sizevardenafil uk cost pillvardenafil 60 pills onlinegeneric soft tabs vardenafilkey buy levitra cheaplevitra cheap buy onlinelow price vardenafilcheap vardenafil tabletslevitra onlinevardenafil 20 mg orderblindness cialis levitravardenafil levitra onlinelevitra faqsplitting levitravardenafil in ukvardenafil lowest priceprozac interactions with viagra cialis levitrakeywords cialis levitra sales viagrahow quick does levitra workwhat is levitra productPurchase vardenafil Free Airmail shippingLevitra 20 mgcomparison of cialis levitra and viagraovernight shipping of professional vardenafilbenefits of levitralevitra vs viagra which is better2buy cheap levitra onlinelevitra prices american pharmaciesvardenafil online overnightdiscount levitra cialis viagrabuy cheap levitra xanax xenicalorder levitra onlinecheapest vardenafil substitute vardenafilbuy vardenafil cialasbuy cheap generic levitra pharmacy onlinelevitra soft tablevitra third quarter 2004 saleslevitra bestellenvardenafil fedexviva levitra commercialLevitra Courier shipping Salecheapest vardenafil professionalLevitra 10 pills Buycheap vardenafil canadaOnline Levitra Free Airmail shippingfull information levitradiscounted vardenafillevitra substitutesLevitra 20 mg Buylevitra 20 mg bayerbuy vardenafil delived fed exforeign cialis levitra pillswomen vardenafilvardenafil overnight shippingcan women use levitravardenafil Buyvardenafil propafenonelevitra provardenafil impotence drug eli lilly cobuy levitra viagralevitra web siteship free levitra sampleorder levitra without prescriptionfree levitra onlinesuper levitralevitra in ukcompare viagra 26 levitra2cheap levitra onlineOrder vardenafilvardenafil Courier shipping orderbuy vardenafilvardenafil storieslevitra successdiscount levitra purchasecheapest levitra substitute sildenafillevitra cialis viagra comparebuy vardenafil online in ukLevitra 20 mg onlineordering levitraconnecticut levitra vardenafillevitra productsLevitra Free Airmail shipping BuyGeneric vardenafil 10 mg Sale
intense creativity » Tutorial - Creating Custom Components (Visual Controls) in pure AS3 - Part 1

Tutorial - Creating Custom Components (Visual Controls) in pure AS3 - Part 1

Hi Everyone,

Looking back at the last 4-5 years I spent developing Flash widgets & applications (again, I do NOT participate in marketing efforts.. so no micro-sites or bogus games for me) I realize that one of the core abilities of a good Flash application developer is his ability to develop components from scratch. The better you get at this, the more valuable you become. At first you try to develop simple components: buttons, radio buttons, check boxes, thumbnail viewers; then move on to more difficult ones: accordeons, tooltips, tabbed panels, tree controls.

Imagine, whatever your client asks you, you say: "Yes I can do". This ability is very precious, it takes years of efforts to develop properly but once you have acquired it, there is no limit to what you can do. W/o this ability, you are limited to what you can integrate from libraries or packages. You can compare this to the difference between a composer and a DJ. The DJ can only mix tracks, but the composer can actually make up any music he wants!

Last summer I had a big challenge while working for Adobe: rebuild the DataGrid to enable it to show multiple levels of data, with animated filtering. It works, however is hasn't been integrated inside of the Verizon Wireless Media Store yet, maybe one day!

Since that challenge, my perspective changed; in fact the experience at Abode proved very instructive. I have to thank Paul, Josh, Dave, Tommy, and Bjorn for this.

In this article I will describe the foundation of most of the custom components I build, a simple class called BaseControl. The ideas behind this class come from a Tutorial I first read in 2004 from Joey Lott, which I implemented in AS2 and then migrated to AS3.

This class can be thought as a trimmed down version of UIComponent inside of the Flex Architecture because it doesn't carry the weight of styles, callLater (and since I don't really like flex, I cannot remember what else BaseControl lacks).

The class contains functions to streamline creation (1), help manage disposition (2), and help you track some mouse activity (3).

Ah, important note, I use Sprite as my base class. Why? We need a class that is derived from DisplayObjectContainer, and there is no need for a timeline, therefore Sprite is ideal.

1. Streamlining Creation

When creating a component, there are a few steps you will go through. First step is passing all the initializing parameters to the component. You need to do this before the creation process starts. These parameters can be varied, can go from styling, data source, dimensions, mode, state, etc... In Flex thanks to the use of "callLater" they can offer an empty constructor for the controls, and then you pass the parameters by setting attributes on the instantiated class. Once the current "thread" of execution is done, the control is initialized and created.

In this example, I will not use "callLater". You can refer to a post I did earlier on how to implement "callLater" inside of your classes if you wish to take another route.

In this example, we will pass all initalization parameters to the constructor of the component. This constructor needs to adapt to the subclasses, some parameters might be optional, therefore I find it easier for this function to "blindly" apply the params passed to it. Here is the code for the constructor:

Actionscript:
  1. public function BaseControl(initObj:Object)
  2. {         
  3.     for(var i in initObj)
  4.     {
  5.         this[i] = initObj[i];
  6.     }
  7.            
  8.     init();
  9.     createChilds();
  10.     initEvents();
  11.     arrange()
  12.     redraw();
  13. }

In this function, I first go through all the properties of the object that was passed in the constructor, called initObj, and apply each of those properties to the component itself. Again, this is a blind process, to support all variations found in subclasses. Then the following sequence is called:

  • init -- used to initialize the variables, such as arrays, state information, data sources and others non-visual properties of the component
  • createChilds -- used to create all the visual elements contained inside of the component, such as background shape, canvas for drawing, buttons, etc... This function is often broken down in smaller bits.
  • initEvents -- used to initialize the event listeners on the visual elements just created.
  • arrange -- used to layout the visual elements contained in the component
  • redraw -- used to draw the content of the component, when required. It does fill in the component with a transparent layer by default, officially occupying the space and enabling it to capture mouse events that happen over it.

A. init()

The init function, atleast its default behaviour makes sure the component isn't scaled (you never want to scale components themselves!), makes sure the width and height are positive, and disabled tab interaction.

Actionscript:
  1. protected function init():void
  2. {
  3.     if(_width <= 0)   
  4.         _width = width;
  5.     if(_height <= 0)
  6.         _height = height;
  7.                
  8.     scaleX = 1;
  9.     scaleY = 1;
  10.     tabEnabled = false;
  11. }

Note that here we use _width instead of width because sometimes a component can occupy more width than it should (through masked content for example). Hence we want to change this behavior by overriding the width and height getters this way:

Actionscript:
  1. public override function get width():Number
  2. {
  3.     return _width;
  4. }
  5.        
  6. public override function get height():Number
  7. {
  8.     return _height;
  9. }

This way, you have control over the reported size of your component, no matter what flash actually thinks the dimensions should be. This is very handy.

As a continuation of this logic, I implement a function called setSize which lets me control the size of the control after it's instantiation; like so:

Actionscript:
  1. public function setSize(nW:Number, nH:Number):void
  2. {
  3.     scaleX = 1;
  4.     scaleY = 1;
  5.     _width = nW;
  6.     _height = nH;
  7.     arrange();
  8.     redraw();
  9.     dispatchEvent(new ControlEvent(ControlEvent.RESIZED, {control:this, width:width, height:height}));
  10. }

The function ensures that scaleX and scaleY are both set to 1. Again scaling is not a good practice on the component itself, you can scale some of it's children if needed, but NOT the component itself! Then the "new width" and "new height" are set, arrange() is called to lay out the children appropriately, followed by a call to redraw() to draw the content again, and finally an event is dispatched to inform anybody interested that the component has been resized.

B. createChilds()

In this case, since BaseControl really is an Abstract Class (I wish ActionScript would support them properly..), we are not creating any children. All was have is the function signature which will be overridden in subclasses.

Actionscript:
  1. protected function createChilds():void
  2. {         
  3. }

C. initEvents()

Ah, here there is a bit of logic, used mainly to replace the good old "onReleaseOutside" which has disappeared from in AS3. What needs to be done is as follows:

  • track when the component is added to stage
  • when it is, add a MouseEvent.MOUSE_UP listener on the Stage
  • track when the mouseButton goes down on the component itself
  • if the mouseButton goes up on the stage (outside of the component) and had gone down on the component, this is the equivalent of onReleaseOutside

No matter where the mouse button goes up inside of Flash, the Stage will always dispatch it. This enables us to simulate the onReleaseOutside event..

First, we need a protected property on BaseControl, a flag

Actionscript:
  1. protected var _mouseDown:Boolean = false;

Next the implementation of initEvents() and all functions related to it..

Actionscript:
  1. protected function initEvents():void
  2. {
  3.     addEventListener(MouseEvent.MOUSE_DOWN, doMouseDown);
  4.     addEventListener(MouseEvent.MOUSE_UP, doMouseUp);
  5.     addEventListener(Event.ADDED_TO_STAGE, doAddedToStage);
  6. }
  7.        
  8. protected function doAddedToStage(evt:Event):void
  9. {
  10.     stage.addEventListener(MouseEvent.MOUSE_UP, doParentRelease);
  11.     removeEventListener(Event.ADDED_TO_STAGE, doAddedToStage);
  12. }
  13.  
  14. protected function doMouseDown(evt:MouseEvent):void
  15. {
  16.     _mouseDown = true;
  17. }
  18.  
  19. protected function doMouseUp(evt:MouseEvent):Boolean
  20. {
  21.     if(_mouseDown)
  22.     {
  23.         _mouseDown = false;
  24.         return true;
  25.     }
  26.     else
  27.     {
  28.         return false;
  29.     }
  30. }
  31.  
  32. public function doParentRelease(evt:MouseEvent):void
  33. {
  34.     for(var i:Number = 0; i <this.numChildren; i++)
  35.     {
  36.         var child:DisplayObject = getChildAt(i);
  37.         if(child is BaseControl)
  38.             (child as BaseControl).doParentRelease(evt);
  39.     }
  40.            
  41.     doMouseUp(evt);
  42. }
  43.        
  44. public function mouseInside():Boolean
  45. {
  46.     return (mouseX>= 0 && mouseX <= _width && mouseY>= 0 && mouseY <= _height);
  47. }

That was quite a bit of code... I will explain it shortly! work calls.... :)

Take care,

Martin

5 Responses to “Tutorial - Creating Custom Components (Visual Controls) in pure AS3 - Part 1”

  1. Daily News About Work : A few links about Work - Friday, 29 May 2009 11:08 says:

    [...] 05.29Tutorial - Creating Custom Components (Visual Controls) in pure AS3 - Part 1 [...]

  2. Video | Enjolt.com | Innovate for Success says:

    [...] have a web based tool to help a little. You could paste parts of actionscript documentation 05.29Tutorial - Creating Custom Components (Visual Controls) in pure AS3 - Part 1 - blog.martinlegris.com 05/29/2009 Hi Everyone,Looking back at the last 4-5 years I spent [...]

  3. thomas says:

    you should try myLib components, are very powerfull in skinning and functionnalities :
    http://mylib.samystudio.net/

  4. Tommy Baggett says:

    Thanks for showing the love! It was great working with you at Adobe. I hope we can do it again soon.

  5. intense creativity » Tutorial - Creating a reusable Slider control says:

    [...] established, lets get down to business. I will follow the methodology I started describing in this post (I promise to finish it sometime in the future). Just to recap.. At creation time the following [...]

Leave a Reply