Archive for November, 2008

After months working on my own framework and successfully using in small to large projects, I’m happy to give you access to SomaUI and Soma.

As a quick description, Soma is a MVC (Model-View-Controller) framework and SomaUI is a tool to generate the base of a Flash site in AS3, based on Soma (available for windows, mac and linux).

First of all, I’d like to thanks Juan. He has shown very early his interest in Soma and has helped me to get started on the java part: SomaUI, it really was a great help. I’d like to thanks Luis from Less Rain, he has given me invaluable advice on the flash part: Soma, and I’m sure it will continue. We had great nice and long conversations. I’d also like to thanks CVSDude for the help and support.

Soma

An important thing to understand is, I’m not entering in the “framework fight” of the best one and the best OOP practice. Soma is not at all a competitor to PureMVC, which is not even restricted to a language, or any other framework. Soma is a result of a personal need, no more no less, and I built it my way. I wanted it quick, easy, invisible and as much as I could, well-written. I wish to share it hoping it will help some of you.

I like to call Soma an invisible framework, as you can fully use the MVC way even used in its internal building, or completely avoid it and build your site your way. The framework is built to centralize events and managers that are handling common actions in a flash site, like loading, content manager, page manager, deep-linking, transitions, and so on. The framework will handle most of those redundant and annoying development and give a body (or a skeleton) to work with.

Soma is very XML-based and use a reference to a XML site definition almost all the time. The structure, content, assets and assets behaviors are stored in this XML site definition. You can use Soma for any “page-based” site. Soma is only managers and “empty layers” meaning you can use it with any kind of design.

Soma can be used in a lot of different ways, so I won’t go further for now. I’ll write other detailed tutorials about Soma and its XML.

SomaUI

SomaUI is a software available for Windows, Mac and Linux. Its main purpose is to generate the base of an AS3 site, ready to be used for further development, for Flash Player 9 or 10 and for Flash or Flex SDK developer.

To use SomaUI, you’ll need at least a Flex SDK version 3.1 (even if you are a Flash developer), you can download it here. You’ll also need at least a java runtime environment version 5 (JRE 1.5). The JRE is usually already available on your computer but if it is not the case, you can download or update it here.

SomaUI makes you able to create a project, choose specific settings such as name, package, flash player version, and so on. You will write the xml definition of your site or load a template and finally, export it (source files, files needed to deploy it on a server and Flash files if you set the option “Flash Developer”).

To avoid annoyance with security sandbox, I suggest you export your site in a local server, you can use Wamp or Xampp for WIndows, Mamp for Mac and I suppose Apache is already installed in most of the Linux distribution.

You’ll be able to generate a good base to work with in seconds if you use a template…
A getting-started is available in html or pdf.

Important: SomaUI is a beta software, SomaUI does not scan or scaffold your project. For testing purpose, if you export again your project in the same folder, any file will be overwritten without warning. The scan/scaffold is a feature that will come in a further release.

Get the source

You can find the last versions on the SomaUI page in the downloads section. A soma demo site with more functionalities is available on the SVN if you want to see further before I write tutorials.

Have a try and give me your feedback, the Soma forums are available here.

Vote in HexoSearch

Comments 4 Comments »

It seems the problem explained in this post and this post has been solved in the Flex SDK 3.2 release.

A good thing then :)

Vote in HexoSearch

Comments No Comments »

I had some XML E4X filters and I couldn't understand why they were working everywhere (included Gumbo) but in the Flash CS4 IDE.

I've finally isolated the problem…

I’ve got a simple XML like this:

XML:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <items>
  3.   <item id="item1" type="item1_type" />
  4.   <item id="item2" type="item2_type">
  5.     <item id="item3" type="item3_type" />
  6.   </item>
  7. </items>

Basically my filter is parsing all the attributes "id" of the nodes "item", to return me the attribute "type", sounds simple.

Here is my filter:

Actionscript:
  1. xml..*.(name() == "item" && @id == "item1").@type

So, I nicely put this filter in a function, and then it stopped working in Flash CS4. When I tried to get the type of the id "item1" it worked but none of the id "item2" and "item3" worked, the filter wasn’t able to find the node. Also, when I removed the item3, which is a child of the item2, I could get the type of my item2… how weird, anyway…

My mistake, which wasn’t really a mistake by the way, was to use the word "item" as a parameter of my function. It should work as the parameter has nothing to do with the XML but it seems that Flash CS4 messed up with something and got lost.

So, this didn’t work:

Actionscript:
  1. getItemTypeNotWorking("item2");
  2. function getItemTypeNotWorking(item:String):void {
  3. trace("test1 = ", xml..*.(name() == "item" &amp;&amp; @id == item).@type);
  4. }

But this worked:

Actionscript:
  1. getItemTypeWorking("item2");
  2. function getItemTypeWorking(itemID:String):void {
  3. trace("test2 = ", xml..*.(name() == "item" &amp;&amp; @id == itemID).@type);
  4. }

As you can see only the parameter is different (item and itemID).

Using a parameter that is the same word as a node name will make the filter not working as intended, only in Flash IDE CS4.

See the source by clicking here.

Vote in HexoSearch

Comments 1 Comment »

For a special purpose I had to extend a Singleton, to make my system fully flexible. I found complicated solutions that wasn't fully working as intended.

To use PureMVC, you have to extend a Singleton (Facade), which is the entry point of the framework. This is working only if you don't use the Facade class (the super Singleton) before using the subclass, otherwise you get an error, as the super singleton has already been instantiated.

Well this might be a simple solution but not working in my case.

First of all, they are a lot of solutions to correctly "lock" a Singleton, because in AS3, private constructor doesn't exist. Some of them have turn around, that you can solve with a bit more code, but the solution I prefer is instantiate the Singleton out of the constructor and method:

Actionscript:
  1. private static var _instance:Singleton = new Singleton();

So, if I use the super singleton with a Singleton.getInstance(), and then use the subclass with a SingletonExtended.getInstance(), this will throw an error as when the SingletonExtended is called, the Singleton is called as well and has already been instantiated.

The solution I found is compare the super class name with a static class name, basically you can't use the keyword "this" when you declare a variable, but using "super" will return the same result (well that's odd but it is what I saw):

Actionscript:
  1. private static var _className:String = getQualifiedClassName(super);

And then compare it with the super classname in the Singleton constructor:

Actionscript:
  1. if (_instance != null && getQualifiedSuperclassName(this) != _className) throw new Error

if I trace(_className + " - " + getQualifiedSuperclassName(this)) in the Singleton constructor:

- when I use the Singleton class, I get: "null - Object"
- when I use the SingletonExtended class, I get: "Singleton - Singleton", meaning the constructor has been called from a subclass.

Until now, it is working pretty well, I can use:

Actionscript:
  1. Singleton.getInstance().singletonValue
  2. SingletonExtended.getInstance().singletonValue
  3. SingletonExtended.getInstance().singletonExtendedValue

But we're not completely done. We can access to our two Singleton... but wait, "two Singleton", is that possible?

In fact, it is completely possible and it is even a problem. Let's say I have a undefined value in Singleton, like:

Actionscript:
  1. private var _myValueNotInitialized:String;

And then you have a public method that is setting a value to this variable:

Actionscript:
  1. public function initializeMyValue():void {
  2.     _myValueNotInitialized = "I am initialized";
  3. }

You can now use:

Actionscript:
  1. Singleton.getInstance().initializeMyValue();
  2. or
  3. SingletonExtended.getInstance().initializeMyValue();

But here is the problem: you still have 2 instances. One in Singleton and the other in SingletonExtended. If you use:
Singleton.getInstance().initializeMyValue();
and trace:
SingletonExtended.getInstance().myValueNotInitialized
you will get null, which is normal.

This doesn't make a lot of sense for a Singleton, does it?

The solution I found is updating the Singleton instance with the SingletonExtended one.

In the Singleton, I'm now able to update the instance:

Actionscript:
  1. public function updateInstance(instance:Singleton):void {
  2.     if (instance != null && instance is Singleton) _instance = instance;
  3.     else throw new Error("Incorrect Singleton update");
  4. }

And in the subclass SingletonExtended, I change the getInstance method:

Actionscript:
  1. public static function getInstance():SingletonExtended {
  2.     Singleton.getInstance().updateInstance(_instance);
  3.     return _instance;
  4. }

This is doing the job I needed, a fully Extended Singleton with no errors accessing the super or he subclass.

I tried to break it, but if it is possible, I didn't find the hole. And if you have a better solution, I'd be very happy to see it.

You can download the source by clicking here, or see the classes below.

The main class:

Actionscript:
  1. package {
  2.    
  3.     import flash.display.Sprite;
  4.    
  5.     /**
  6.      * <b>Author:</b> Romuald Quantin - <a href="http://www.soundstep.com/" target="_blank">www.soundstep.com</a><br />
  7.      * <b>Class version:</b> 1.0<br />
  8.      * <b>Actionscript version:</b> 3.0<br />
  9.      * <b>Date:</b> 11-2008<br />
  10.      */
  11.     
  12.     public class Main extends Sprite {
  13.        
  14.         //------------------------------------
  15.         // private properties
  16.         //------------------------------------
  17.        
  18.         //------------------------------------
  19.         // public properties
  20.         //------------------------------------
  21.        
  22.         //------------------------------------
  23.         // constructor
  24.         //------------------------------------
  25.        
  26.         public function Main() {
  27.            
  28.             trace("--- public properties ---");
  29.             trace(Singleton.getInstance().singletonValue + ">>> from Singleton");
  30.             trace(SingletonExtended.getInstance().singletonValue + ">>> from SingletonExtended");
  31.             trace(SingletonExtended.getInstance().singletonExtendedValue + ">>> from SingletonExtended");
  32.             trace(Singleton.getInstance().singletonValue + ">>> from Singleton");
  33.            
  34.             trace("--- access to public methods ---");
  35.             trace(Singleton.getInstance().publicSingletonMethod() + ">>> from Singleton");
  36.             trace(SingletonExtended.getInstance().publicSingletonMethod() + ">>> from SingletonExtended");
  37.             trace(SingletonExtended.getInstance().publicSingletonExtendedMethod() + ">>> from SingletonExtended");
  38.            
  39.             trace("--- access to private methods ---");
  40.             trace(Singleton.getInstance().accessPrivateSingletonMethod() + ">>> from Singleton");
  41.             trace(SingletonExtended.getInstance().accessPrivateSingletonMethod() + ">>> from SingletonExtended");
  42.             trace(SingletonExtended.getInstance().accessPrivateSingletonExtendedMethod() + ">>> from SingletonExtended");
  43.            
  44.             trace("--- access to protected and overriden methods ---");
  45.             trace(Singleton.getInstance().accessProtectedSingletonMethod() + ">>> from Singleton");
  46.             trace(SingletonExtended.getInstance().accessProtectedSingletonMethod() + ">>> from SingletonExtended");
  47.             trace(SingletonExtended.getInstance().accessOverridenSingletonExtendedMethod() + ">>> from SingletonExtended");
  48.            
  49.             trace("--- value not initialized ---");
  50.             trace("before = ", Singleton.getInstance().myValueNotInitialized);
  51.             Singleton.getInstance().initializeMyValue(); // or SingletonExtended.getInstance().initializeMyValue();
  52.             trace("after = ", Singleton.getInstance().myValueNotInitialized);
  53.            
  54.             // this will generate errors
  55.             // as you can't instantiate twice a Singleton or a sublass of Singleton
  56.            
  57.             //var s1:Singleton = new Singleton();
  58.             //var s2:SingletonExtended = new SingletonExtended();
  59.            
  60.         }
  61.        
  62.         //
  63.         // PRIVATE, PROTECTED
  64.         //________________________________________________________________________________________________
  65.        
  66.         //
  67.         // PUBLIC
  68.         //________________________________________________________________________________________________
  69.        
  70.     }
  71. }

The Singleton class:

Actionscript:
  1. package {
  2.  
  3.     import flash.utils.getQualifiedClassName;
  4.     import flash.utils.getQualifiedSuperclassName;
  5.  
  6.     /**
  7.      * <b>Author:</b> Romuald Quantin - <a href="http://www.soundstep.com/" target="_blank">www.soundstep.com</a><br />
  8.      * <b>Class version:</b> 1.0<br />
  9.      * <b>Actionscript version:</b> 3.0<br />
  10.      * <b>Date:</b> 11-2008<br />
  11.      */
  12.     
  13.     public class Singleton {
  14.        
  15.         //------------------------------------
  16.         // private properties
  17.         //------------------------------------
  18.        
  19.         private static var _instance:Singleton = new Singleton();
  20.         private static var _className:String = getQualifiedClassName(super);
  21.        
  22.         private var _myValueNotInitialized:String;
  23.        
  24.         //------------------------------------
  25.         // public properties
  26.         //------------------------------------
  27.        
  28.         public var singletonValue:String = "Singleton value";
  29.        
  30.         //------------------------------------
  31.         // constructor
  32.         //------------------------------------
  33.        
  34.         public function Singleton() {
  35.             if (_instance != null && getQualifiedSuperclassName(this) != _className) throw new Error("Singleton is obviously... Singleton.");
  36.         }
  37.        
  38.         //
  39.         // PRIVATE, PROTECTED
  40.         //________________________________________________________________________________________________
  41.        
  42.         private function privateSingletonMethod():String {
  43.             return "privateSingletonMethod";
  44.         }
  45.        
  46.         protected function protectedSingletonMethod():String {
  47.             return "protectedSingletonMethod";
  48.         }
  49.        
  50.         //
  51.         // PUBLIC
  52.         //________________________________________________________________________________________________
  53.        
  54.         public function initializeMyValue():void {
  55.             _myValueNotInitialized = "I am initialized";
  56.         }
  57.        
  58.         public function updateInstance(instance:Singleton):void {
  59.             if (instance != null && instance is Singleton) _instance = instance;
  60.             else throw new Error("Incorrect Singleton update");
  61.         }
  62.  
  63.         public static function getInstance():Singleton {
  64.             return _instance;
  65.         }
  66.        
  67.         public function publicSingletonMethod():String {
  68.             return "publicSingletonMethod value";
  69.         }
  70.        
  71.         public function accessPrivateSingletonMethod():String {
  72.             return privateSingletonMethod();
  73.         }
  74.        
  75.         public function accessProtectedSingletonMethod():String {
  76.             return protectedSingletonMethod();
  77.         }
  78.        
  79.         public function get myValueNotInitialized():String {
  80.             return _myValueNotInitialized;
  81.         }
  82.     }
  83. }

The SingletonExtended class:

Actionscript:
  1. package {
  2.  
  3.     /**
  4.      * <b>Author:</b> Romuald Quantin - <a href="http://www.soundstep.com/" target="_blank">www.soundstep.com</a><br />
  5.      * <b>Class version:</b> 1.0<br />
  6.      * <b>Actionscript version:</b> 3.0<br />
  7.      * <b>Date:</b> 11-2008<br />
  8.      */
  9.     
  10.     public class SingletonExtended extends Singleton {
  11.        
  12.         //------------------------------------
  13.         // private properties
  14.         //------------------------------------
  15.        
  16.         private static var _instance:SingletonExtended = new SingletonExtended();
  17.        
  18.         //------------------------------------
  19.         // public properties
  20.         //------------------------------------
  21.        
  22.         public var singletonExtendedValue:String = "SingletonExtended value";
  23.        
  24.         //------------------------------------
  25.         // constructor
  26.         //------------------------------------
  27.        
  28.         public function SingletonExtended() {
  29.             if (_instance != null) throw new Error("SingletonExtended is obviously also... Singleton.");
  30.         }
  31.        
  32.         //
  33.         // PRIVATE, PROTECTED
  34.         //________________________________________________________________________________________________
  35.        
  36.         private function privateSingletonExtendedMethod():String {
  37.             return "privateSingletonExtendedMethod";
  38.         }
  39.        
  40.         override protected function protectedSingletonMethod():String {
  41.             return "protectedSingletonMethod overriden in SingletonExtended, value in super.protectedSingletonMethod(): " + super.protectedSingletonMethod();
  42.         }
  43.        
  44.         //
  45.         // PUBLIC
  46.         //________________________________________________________________________________________________
  47.        
  48.         public static function getInstance():SingletonExtended {
  49.             Singleton.getInstance().updateInstance(_instance);
  50.             return _instance;
  51.         }
  52.        
  53.         public function publicSingletonExtendedMethod():String {
  54.             return "publicSingletonExtendedMethod value";
  55.         }
  56.        
  57.         public function accessPrivateSingletonExtendedMethod():String {
  58.             return privateSingletonExtendedMethod();
  59.         }
  60.        
  61.         public function accessOverridenSingletonExtendedMethod():String {
  62.             return protectedSingletonMethod();
  63.         }
  64.    
  65.     }
  66. }

Vote in HexoSearch

Comments 4 Comments »