Sunday, March 8, 2015

EventManager and CustomEvent using AS3.0

Most of the time we face challenges in managing events while developing AS3.0 applications. If we could create an EventManager class, it would help us manage and control all event at a single place. I have written this utility class to handle all events at a single place.

EventManager.as


package utils {
 import flash.events.EventDispatcher;
 import flash.events.Event;
 /*
 * class - EventManager
 * Synopsis
 * Central event dispatching class (singleton)
 * All classes that need to dispatch or listen to events should use the services of this class.
 * @author nitinsinha1@gmail.com
 */
 public class EventManager extends EventDispatcher {
   /**
  * Reference to this instance.
  */
  private static var _instance:EventManager;
  
  public function EventManager() {
   if (_instance) {
    throw new Error("Singleton pattern can only be accessed through Singleton.getInstance()");
   }
  }
  
  /**
  * Static accessor method.
  * @return A unique instance of this class.
  */ 
  
  public static function getInstance():EventManager {
   if (!_instance) {
    _instance = new EventManager()
   }
   return _instance;
  }
 }
}





Using EventManager.as


/*
  * evt - EventManager instance
  */ 
  private var evt:EventManager;

/*
   * evt - EventManager instance
   */
         evt=EventManager.getInstance();
         evt.addEventListener("dispatchedEvent", onEventReady);
evt.dispatchEvent(new CustomEvent("dispatchedEvent",false,false));
evt.dispatchEvent(new CustomEvent("dispatchedEvent",false,false,,));


private function dispatchedEvent(ce:CustomEvent){
   trace("//value param1 >>> ",ce.arg[0]);
   trace("//value param2 >>> ",ce.arg[2]);
  }



CustomEvent.as - class to dispatch events with parameters


package utils {
 import flash.events.Event;
 /*
 * class - CustomEvent
 * Synopsis
 * This class used to dispatch the user defined event with unlimited parameters
 * @author nitinsinha1@gmail.com
 */
 public class CustomEvent extends Event {
  public var arg:*;
 /*
 * Function - CustomEvent
 * Synopsis
 * This constructor function used to initialize the class function/variables
 * Parameters
 * type - Custom event type as String
 * bubbles - Boolean (true/false)
 * cancelable - Boolean (true/false)
 * a:* - Arguments (any type)
 */
  public function CustomEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false, ... a:*) {
   super(type, bubbles, cancelable);
   arg = a;
  }
 /*
 * Function - clone
 * Synopsis
 * This function used to clone the class objectfor reference
 */
  
 // Override clone
 override public function clone():Event{
   return new CustomEvent(type, bubbles, cancelable, arg);
  };  
 }
}


Wednesday, March 4, 2015

JSFL to update text type(input/static/dynamic) and font face of Textfield


// This code modified by Nitin, copyright 2015.
// Modified code found at: https://delfeld.wordpress.com/2009/12/01/jsfl-textfield-fix/
// Original code found at: http://deleteaso.com/fix-textfields-jsfl/

fl.outputPanel.clear();

var doChangeStaticToDynamic = true;
var updateFontName = "Arial"
var outStr = "Fixing textfields for proper output.\nNOTE: This will only affect objects held in the library (or instances of them).\n\n";
var testDoc;

// cycle through all open documents:
for (doc in fl.documents)
{
    outStr += "Document: " + fl.documents[doc].name + "\n";
    
    // set TextField defaults in this doc:
    scanLibrary(fl.documents[doc].library);
}

fl.trace( outStr );

// -------------------------------------------------------

// auxillary function:
function getIsWholeInt(n)
{
    var s = String(n);
    if(s.indexOf(".") != -1)
    {
        return false;
    }
    else
    {
        return true;
    }
}

// auxillary function:
function round(n)
{
    var s = String(n);
    var a = s.split(".");
    var num = parseInt(a[0]);
    var dec = parseInt(a[1].substr(0, 1));
    if(dec >= 5)
    {
        num++;
    }
    return num;
}

/**
 * Scans the supplied flash library for linked classes and makes sure the textfields are set up properly
 * @param   library     A flash library to scan.
 */
function scanLibrary(library)
{
    // outStr += "Scanning textfields";
    
    // loop variables:
    var item, timeline, layerCnt, frameCnt, elems, elemCnt, matX, tx, matY, ty;
    
    var items = library.items;
    var replaceCount = 0;
    
    // for each item in the library:
    for( var i = 0; i < items.length; i++ )
    {
        item = items[i];
        
        //outStr += "item: " + item.itemType + "\n"
        
        // dig into any MCs:
        if (item.itemType == "movie clip" || item.itemType == "graphic" )
        {
            // store current timeline:
            timeline = item.timeline;
            
            // store total number of layers:
            layerCnt = timeline.layerCount;
            
            // set up editing mode:
            library.selectItem(item);
            library.editItem();
            
            // cycle through all layers within this timeline:
            while(layerCnt--)
            {
                // store total frames
                frameCnt = timeline.layers[layerCnt].frameCount;
                
                // cycle through all frames within this layer:
                while(frameCnt--)
                {
                    // store elements array:
                    elems = timeline.layers[layerCnt].frames[frameCnt].elements;
                    
                    // store total number of elements in this frame:
                    elemCnt = elems.length;
                    
                    // cycle through all elements within this frame:
                    while(elemCnt--)
                    {
                        // process any text type element:
                        if( elems[elemCnt].elementType == "text" )
                        {
                            outStr += "\t Found a textfield: "
                            + ((elems[elemCnt].name == "") ? "{no name}" : elems[elemCnt].name);
                            
       elems[elemCnt].setTextAttr("face", updateFontName);
       
                            //Change the static TextFields to dynamic
                            if( elems[elemCnt].textType == "static" )
                            {
                                if( !doChangeStaticToDynamic)
                                {
                                    outStr += ".  Ignoring this static TextField.\n";
                                    //skip this iteration (element)
                                    continue;
                                }
                                else
                                {
                                    outStr += ". (Switching this static TextField type to dynamic.)\n";
                                    // change text type to dynamic:
                                    elems[elemCnt].textType = "dynamic";
                                }
                            }
                            
                            outStr += "\t Text type: " + elems[elemCnt].textType + "\n";
                            
                            if(elems[elemCnt].textType == "dynamic")
                            {
                                //Remove the ability to be selectable
                                elems[elemCnt].selectable = false;
                            }
                            
                            // This section applies to all dynamic or input TextFields
                            
                            // Remove the auto kern attribute
                            elems[elemCnt].setTextAttr('autoKern', false);
                            
                            // Embed fonts UpperCase, LowerCase, Numerals, Punctuation
                            // This can be fine tuned to get a smaller font set.
                            elems[elemCnt].embedRanges = "1|2|3|4";
                            
                            // Pop onto whole pixel
                            // not sure that two assignments are needed:
                            matX = elems[elemCnt].matrix;
                            tx = matX.tx;
                            if(!getIsWholeInt(tx))
                            {
                                matX.tx = round(tx);
                                elems[elemCnt].matrix = matX;
                            }
                            // Can this be combined with the 'x' section?
                            matY = elems[elemCnt].matrix;
                            ty = matY.ty;
                            if(!getIsWholeInt(ty))
                            {
                                matY.ty = round(ty);
                                elems[elemCnt].matrix = matY;
                            }
                        }
                    }
                }
            }
        }
    }
}

Using FormBuilder, Validators & FormGroup in Angular 2 to post data to Node server (node.js)

This blog explains usage of  FormBuilder , Validators , FormGroup classes in Angular 2 projects. In this post we will create HTML form a...