Pages

Tuesday, August 16, 2011

App: Project Artificial Intelligence (P.A.I.)

This is a basic chatbot program, that has been launched to evolve itself with time. It is basically made to chat via mobile-sms. While this app uses the same core to communicate on web.

Technical details:
  • Body: the flex 4 has been used to create the user interface for front-end.
  • Brain: the php 5 has been used as server-side language for back-end.
  • Memory: the xml has been used as database.
Application - PAI:

Thursday, August 4, 2011

AS3: Basic navigation class

Here is basic code for making a navigation class. The navigation here in is based on a counter. By taking the reference of a counter the class can be extended to achieve any type of navigation.

CODE
 package   
 {  
  /**  
   * @file CNavigator.as  
   * @author Abhishek Kumar  
   */  
  public class CNavigator   
  {  
   public var _Proceed:Function;  
   private var counter:int;  
   private var minRange:int;  
   private var maxRange:int;  
   public function CNavigator(min:int, max:int)  
   {  
    trace('CNavigator -> constructor');  
    minRange = min;  
    maxRange = max;  
    counter = 0;  
   }  
   public function navLeft():void  
   {  
    trace('CNavigator -> navLeft');  
    if (counter > minRange)  
    {  
     counter--;  
     _Proceed(counter);  
    }  
   }  
   public function navRight():void  
   {  
    trace('CNavigator -> navRight');  
    if (counter < maxRange)  
    {   
     counter++;  
     _Proceed(counter);  
    }   
   }   
  }   
 }   

USAGE
 import CNavigator;

 var oNav:CNavigator = new CNavigator(0, 5);
 oNav._Proceed = trace;

 oNav.navLeft();
 oNav.navLeft();

 oNav.navRight();
 oNav.navRight();
 oNav.navRight();
 oNav.navRight();

 oNav.navLeft();
 oNav.navLeft();
 oNav.navLeft();

Wednesday, August 3, 2011

AS3: How to load child swf in parent swf at run-time

Here is a class to load single or multiple external/child swf files in the main/parent swf at run-time.

CODE
 package   
 {  
  import flash.display.Sprite;  
  import flash.display.Loader;  
  import flash.display.MovieClip;  
  import flash.net.URLRequest;  
  import flash.events.Event;  
  import flash.events.IEventDispatcher;  
  import flash.events.ProgressEvent;  
  import flash.events.SecurityErrorEvent;  
  import flash.events.HTTPStatusEvent;  
  import flash.events.IOErrorEvent;  
  /**  
   * @file CSwfLoader.as  
   * @author Abhishek Kumar  
   */  
  public class CSwfLoader   
  {  
   public var _Proceed:Function;  
   private var swfLoader:Loader;  
   private var swfRequest:URLRequest;  
   private var swfPathList:Array;  
   private var swfList:Array = [];  
   private var swfTemp:MovieClip;  
   private var swfCount:int; 
 
   public function CSwfLoader()   
   {  
    swfLoader = new Loader();  
    swfRequest = new URLRequest();     
   }  

   public function startLoading(swfPaths:Array):void   
   {  
    swfPathList = swfPaths;  
    loadSWF(swfPathList[0]);  
   }  

   private function loadSWF(path:String):void   
   {  
    initiateListeners(swfLoader.contentLoaderInfo);  
    swfRequest.url = path;  
    swfLoader.load(swfRequest);  
   }  

   private function initiateListeners (dispatcher:IEventDispatcher):void  
   {  
    trace ("CSwfLoader -> initiateListeners: ");  
    dispatcher.addEventListener (Event.COMPLETE, completeHandler);  
    dispatcher.addEventListener (Event.OPEN, openHandler);  
    dispatcher.addEventListener (ProgressEvent.PROGRESS, progressHandler);  
    dispatcher.addEventListener (SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);  
    dispatcher.addEventListener (HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);  
    dispatcher.addEventListener (IOErrorEvent.IO_ERROR, ioErrorHandler);  
   }  

   private function destroyListeners (dispatcher:IEventDispatcher):void  
   {  
    trace ("CSwfLoader -> destroyListeners: ");  
    dispatcher.removeEventListener (Event.COMPLETE, completeHandler);  
    dispatcher.removeEventListener (Event.OPEN, openHandler);  
    dispatcher.removeEventListener (ProgressEvent.PROGRESS, progressHandler);  
    dispatcher.removeEventListener (SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);  
    dispatcher.removeEventListener (HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);  
    dispatcher.removeEventListener (IOErrorEvent.IO_ERROR, ioErrorHandler);  
   }  

   private function completeHandler (event:Event):void  
   {  
    trace ("CSwfLoader -> completeHandler: " + event);  
    destroyListeners(swfLoader.contentLoaderInfo);  
    swfTemp = event.target.content as MovieClip;  
    swfTemp.customID = swfCount;  
    swfList.push(swfTemp);  
    if (swfCount < swfPathList.length - 1)  
    {   
     swfCount++;  
     initiateListeners(swfLoader.contentLoaderInfo);  
     loadSWF(swfPathList[swfCount]);  
    }  
    else  
    {   
     swfLoader.unload();  
     swfLoader = null;   
     _Proceed (swfList);   
    }  
   }   

   private function openHandler (event:Event):void   
   {   
    trace ("CSwfLoader -> openHandler: " + event);  
   }  

   private function progressHandler (event:ProgressEvent):void  
   {  
    trace ("CSwfLoader -> progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);  
   }  

   private function securityErrorHandler (event:SecurityErrorEvent):void  
   {  
    trace ("CSwfLoader -> securityErrorHandler: " + event);  
   }  

   private function httpStatusHandler (event:HTTPStatusEvent):void  
   {  
    trace ("CSwfLoader -> httpStatusHandler: " + event);  
   }  

   private function ioErrorHandler (event:IOErrorEvent):void  
   {  
    trace ("CSwfLoader -> ioErrorHandler: " + event);  
   }  
  }  
 }  


USAGE

 package   
 {  
  import flash.display.MovieClip;  
  import flash.events.Event;  
  import flash.events.MouseEvent;  
  /**  
   * @file Main.as  
   * @author Abhishek Kumar  
   */  
  public class Main extends MovieClip  
  {  
   private var _swfPathArr:Array = [  
    '../asset/swf_1.swf',  
    '../asset/swf_2.swf',  
    '../asset/swf_3.swf',  
    '../asset/swf_4.swf',  
    '../asset/swf_5.swf',  
    '../asset/swf_6.swf'  
   ];  

   private var _swfClipsArr:Array = [];
  
   public function Main()   
   {  
    var oSwfLoader:CSwfLoader = new CSwfLoader();  
    oSwfLoader._Proceed = onCompletePreloading;  
    oSwfLoader.startLoading(_swfPathArr);  
   }  

   private function onCompletePreloading(list:Array):void   
   {  
    _swfClipsArr = list;  

    btn1_mc.buttonMode = true;  
    btn2_mc.buttonMode = true;  
    btn3_mc.buttonMode = true;  
    btn4_mc.buttonMode = true;  
    btn5_mc.buttonMode = true;  
    btn6_mc.buttonMode = true;  

    btn1_mc.addEventListener(MouseEvent.CLICK, setContent);  
    btn2_mc.addEventListener(MouseEvent.CLICK, setContent);  
    btn3_mc.addEventListener(MouseEvent.CLICK, setContent);  
    btn4_mc.addEventListener(MouseEvent.CLICK, setContent);  
    btn5_mc.addEventListener(MouseEvent.CLICK, setContent);  
    btn6_mc.addEventListener(MouseEvent.CLICK, setContent);  
   }  

   private function setContent(event:MouseEvent):void   
   {  
    var _swfToAdd:MovieClip;  
    switch(event.target.name)   
    {  
     case "btn1_mc": _swfToAdd = _swfClipsArr[0] as MovieClip; break;  
     case "btn2_mc": _swfToAdd = _swfClipsArr[1] as MovieClip; break;  
     case "btn3_mc": _swfToAdd = _swfClipsArr[2] as MovieClip; break;  
     case "btn4_mc": _swfToAdd = _swfClipsArr[3] as MovieClip; break;  
     case "btn5_mc": _swfToAdd = _swfClipsArr[4] as MovieClip; break;  
     case "btn6_mc": _swfToAdd = _swfClipsArr[5] as MovieClip; break;  
    }  
    contentContainer.removeChildAt(contentContainer.numChildren-1);  
    contentContainer.addChild(_swfToAdd);
  
    //Ex: How to access internal movieclips of loaded swf  
    _swfToAdd.title_mc.visible = false;  
    trace(_swfToAdd.customID);  
   }  
  }  
 }  


AS3: How to load XML in swf at run-time ... Alternate version

We can load xml file into swf at run-time by using following class. Here is an alternate version of xml loading class that I'd presented in my previous post [AS3: How to load XML in swf at run-time].

CODE
package
{
 import flash.display.Sprite;
 import flash.net.URLLoader;
 import flash.net.URLRequest;
 import flash.events.Event;
 import flash.events.IEventDispatcher;
 import flash.events.ProgressEvent;
 import flash.events.SecurityErrorEvent;
 import flash.events.HTTPStatusEvent;
 import flash.events.IOErrorEvent;

 /**
  * @file CXmlLoader.as
  * @author Abhishek Kumar
  */
 public class CXmlLoader extends Sprite
 {
  public var _Proceed:Function;

  public function CXmlLoader ()
  {
    trace ("CXmlLoader -> constructor");
  }

  public function retrieve (iXml:String):void
  {
    var loader:URLLoader = new URLLoader();
    configureListeners (loader);

    var request:URLRequest = new URLRequest(iXml);
    try
    {
      loader.load (request);
    }
    catch (error:Error)
    {
      trace ("CXmlLoader -> retrieve: " + error.message);
    }
  }

  private function configureListeners (dispatcher:IEventDispatcher):void
  {
    dispatcher.addEventListener (Event.COMPLETE, completeHandler);
    dispatcher.addEventListener (Event.OPEN, openHandler);
    dispatcher.addEventListener (ProgressEvent.PROGRESS, progressHandler);
    dispatcher.addEventListener (SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
    dispatcher.addEventListener (HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
    dispatcher.addEventListener (IOErrorEvent.IO_ERROR, ioErrorHandler);
  }

  private function completeHandler (event:Event):void
  {
    trace ("CXmlLoader -> completeHandler: " + event);
    _Proceed (event.target.data);
  }

  private function openHandler (event:Event):void
  {
    trace ("CXmlLoader -> openHandler: " + event);
  }

  private function progressHandler (event:ProgressEvent):void
  {
    trace ("CXmlLoader -> progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
  }

  private function securityErrorHandler (event:SecurityErrorEvent):void
  {
    trace ("CXmlLoader -> securityErrorHandler: " + event);
  }

  private function httpStatusHandler (event:HTTPStatusEvent):void
  {
    trace ("CXmlLoader -> httpStatusHandler: " + event);
  }

  private function ioErrorHandler (event:IOErrorEvent):void
  {
    trace ("CXmlLoader -> ioErrorHandler: " + event);
  }
 }
}

Tuesday, August 2, 2011

Batch File - Bulk File Remover

Have you ever faced a problem, where you need delete a particular type of files from your directory. Well I faced that while creating a zip file of a directory. The directory was downloaded from Visual Source Safe (VSS), and people who had worked with it knows that it puts an extra file that is named vssver.scc in each and every folders and sub-folders of the directory. As it was really cumbersome method to open and delete each and every folders. So I've developed a batch file for doing this.

Code:

attrib /s -r *.scc
del /s *.scc

Steps to create the batch file:
  1. Create a file with filename: del_scc.bat
  2. Open it in notepad.
  3. Copy and paste the above code in it.
  4. Save the file.
  5. Move the file to the required location, i.e., the directory from which you want to remove .scc files.
  6. Run the file by double-clicking on it.
Understand the code:
  • The first line will change the property of all the files whose extension is .scc by converting it from read-only to writable file.
  • The second line will delete all the files whose extension is .scc from the current folder and their sub-folders.
Scope of this code:
The code can be extended to include any type of files. I'd recently used to delete .fla files from the scorm course package directory.

App: Gil_TextEditor

I was working on an e-learning project for Gujarat government. The whole content of the project was in Gujarati language. Since nobody in our team had Gujarati background, so it'd become hard to understand or modify the content. The content team, those who were creating the xml files from the requirement documents, were facing lots of time & difficulty while Gujarati content creation process. So I'd developed this tool for making the content developer's life easier.

Firstly, I'd analysed the problem, which were:
  • Manual content creation for XML takes a lot of time.
  • Symbols & Gujarati-text is being manually encoded that is error prone and time taking.
  • Manual encoding requires debugging many times to ensure correctness of encoded text.

Then, I looked for the worth of doing this, which were:
  • to speed-up the XML development  and debugging 
  • to increase accuracy
  • to ease the encoding of symbols

Finally, I had to convince my supervisor that how it fits with the business initiative and target:
  • by speeding-up the process of development & delivery
  • by cutting-back debugging cycle

At last, I got approval when I showed them the following calculation:
Total time saved = manual creation timecreation time using tool = 963.6 hours

Here, is that tool which made a big difference:


LinkProject Charter - Gil_TextEditor.pdf

Modus-operandi:
  1. Copy and paste the gujrati translated text in the white box (editing area).
  2. Click on the white box once to view it's gujrati equivalent in grey box (display area).
  3. Select whole text in the white box.
  4. Click on the Process button to encode all the special characters ('<' and '>') that creates conflict in shell.
  5. Now edit the portion as required:
    • To change the portion in english - Select the portion of text and click on English button.
    • To change the portion in symbol - Select the portion of text and click on Symbol button.
    • To change the portion in sub/superscript - Select the portion of text and click on Sub/Super button repectively.
    • To change the portion into bold/italic/underline - Select the portion of text and click on Bold/Italic/Underline button respectively.
Resources:

The 5 fonts embedded in this tool are given below:
  1. ArialMT for English characters
  2. LMG-Arun for Gujarati characters
  3. Symbol for symbols
  4. GG Subscript for subscripts
  5. GG Superscript for superscripts

Monday, August 1, 2011

AS3: A class to control 'About ...' movieclip

It is well known fact that, each and every software or program has atleast About <software_name> dialogue box. Similarly if an application is made-up in flash, it should have an About ... dialogue-box.

I usually prefer to show it over top of the application as a message box with a faded or dark background.
To achieve this in flash, there is an About ... movieclip. It should be of size that covers the whole stage.

There are 2 movieclips inside About ... movieclip:
  1. shows message related to About ... content
  2. fades or darkens the are behind About ... content

By default this About ... movieclip will remain hidden. And on certain predefined mouse-click it'll show up. But after that as soon as user will click on the dark background area, the About ... will disappear.

So this functionality requires only on movieclip inside the About ... movieclip to be linked with mouse-click event, which is the bas or background movieclip.

CODE
 package   
 {  
  import flash.display.MovieClip;   
  import flash.events.Event;  
  import flash.events.MouseEvent;  
  /**  
   * @file CAbout.as  
   * @author Abhishek Kumar  
   */  
  public class CAbout  
  {  
   private var Container:MovieClip = null;  
   public function CAbout(iReference:MovieClip)  
   {  
    Container = iReference;  
    initializeAssets();  
   }  
   private function initializeAssets():void  
   {  
    hide();  
   }  
   public function initializeEvents():void  
   {  
    Container.base.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);  
   }  
   public function destroyEvents():void  
   {  
    Container.base.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);  
   }  
   private function onMouseUp(me:MouseEvent):void  
   {  
    hide();  
    destroyEvents();  
   }  
   public function hide():void  
   {  
    Container.visible = false;  
   }  
   public function show():void  
   {  
    Container.visible = true;  
    initializeEvents();  
   }  
  }  
 }  

AS3: A class to control button behavior & states

Here is a CButtonControl.as class that'll control the behavior of a movieclip (made in flash) and make it work like a button.

The movieclip should have atleast 4 frames with labels as:
  1. up
  2. over
  3. down
  4. disable

These labelled frames will work as different states of the button.

CODE
 package   
 {  
  import flash.display.MovieClip;   
  import flash.events.Event;  
  import flash.events.MouseEvent;  
  /**  
   * @file CButtonControl.as  
   * @author Abhishek Kumar  
   */  
  public class CButtonControl  
  {  
   private var Container:MovieClip = null;  
   public var proceed:Function;  
   private var objekt:Object = {};  
   public function CButtonControl(iReference:MovieClip)   
   {  
    Container = iReference;  
    initializeAssets();  
   }  
   private function initializeAssets():void  
   {  
    objekt.id = -1;  
    Container.visible = true;  
    Container.mouseChildren = false;  
    Container.gotoAndStop('disable');  
   }  
   public function initializeEvents():void  
   {  
    Container.buttonMode = true;  
    Container.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);  
    Container.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);  
    Container.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);  
    Container.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);  
   }  
   public function destroyEvents():void  
   {  
    Container.buttonMode = false;  
    Container.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);  
    Container.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);  
    Container.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);  
    Container.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);  
    //Container.removeEventListener(MouseEvent.ROLL_OUT, onMouseOut);  
   }  
   private function onMouseDown(me:MouseEvent):void  
   {  
    Container.gotoAndStop('down');  
   }  
   private function onMouseUp(me:MouseEvent):void  
   {  
    Container.gotoAndStop('up');  
    (objekt.id == -1)? proceed() : proceed(objekt);  
   }  
   private function onMouseOver(me:MouseEvent):void  
   {  
    Container.gotoAndStop('over');  
   }  
   private function onMouseOut(me:MouseEvent):void  
   {  
    Container.gotoAndStop('up');  
   }  
   public function setLabel(iLabel:String, isHtml:Boolean=false):void  
   {  
    if(isHtml)  
     Container.caption.htmlText = iLabel;  
    else  
     Container.caption.text = iLabel;  
   }  
   public function embedFonts(bool:Boolean):void  
   {  
    Container.caption.embedFonts = bool;  
   }  
   public function hide():void  
   {  
    Container.visible = false;  
   }  
   public function show():void  
   {  
    Container.visible = true;  
   }  
   public function enable():void  
   {  
    initializeEvents();  
    Container.gotoAndStop('up');  
   }  
   public function disable():void  
   {  
    destroyEvents();  
    Container.gotoAndStop('disable');  
   }  
   public function set id(id:int):void  
   {  
    objekt.id = id;  
   }  
   public function get id():int  
   {  
    return objekt.id;  
   }  
  }  
 }