The Flash 8 Player beta has been released, and looking at the features it is without a doubt a major leap in the player’s capabilities. The api has not been officially released (and thus may change), however it still is possible to create files that use these new features with Xamlon Web edition. Internally we've been testing this for a while, but only since the latest public release is it possible with the beta. 

2000 mile overview, to do this all you really need to do is change the exported version to 8, and export correct text. However the Xamlon swfs are heavily optimized for size and speed (we don't use strings for identifiers for example), and all types, including native player classes are type safe and include intellisense info. This is all accomplished by creating stub files that use attributes to indicate export mappings and contain intellisense info. We have partly mapped in the Flash 8 api already, but if you are interested in fooling around with this (even to map your own swfs to .Net) this is how it works... For this example we'll map the new DropShadow filter from Flash 8, and turn on bitmap caching. Have a look here to see the final flash 8 file (requires Flash 8 player!), and right beside it is the Visual Studio project if you'd like to follow along.

Required Tools

First thing you will need is the Flash 8 beta player. This can be found (along with uninstallers) at:
http://www.macromedia.com/software/flashplayer/public_beta/

And of course if you don’t have it already, the Xamlon Web product (*in beta, free download), which allows you to create swf files using C# or VB:
http://www.xamlon.com/software/xamlonpro/flash/

Setting the Version

The two essentials with creating Flash Player 8 files are
1) Make sure the swf version is set to 8, and
2) Expose the Flash 8 api to C#.

Setting the version is very straight forward. Xamlon Web projects can have an optional config.xaml file, where you can do things like set the size and frame rate, embed fonts and sounds, and most importantly here, set the swf version. It is a xaml file, however in this case isn’t much different than basic xml:

<?Mapping XmlNamespace="swf"
      ClrNamespace="Xamlon.Swf.XamlTags"
      Assembly="BCL"?>
<SwfEnvironment xmlns="swf"
  Width="800"
  Height="700"
  FrameRate="12"
  Version="8"
  Background="White">
</SwfEnvironment>

This is saved as config.xaml and your build command needs to add /xaml:config.xaml to have these settings picked up (there is a sample command line included in the bin/Debug directory of the sample download). Notice the version has been set to 8.


Mapping the Flash Player 8 API

To access flash 8 api elements, they must first be made accessible to your C# code. This is done with mapping attributes, which create ‘stub’ files you can code against. The first attribute we’ll deal with is [Implement(bool)], which indicates whether a class needs to be written to the swf or not. Because these classes are already in the Flash player, we don’t want to rewrite them, so we will set all stub classes to [Implement(false)]. This attribute goes directly on the class.

The second attribute we need to use, allows us to specify the text identifier that should be used for a given element. This allows using identifiers that are more consistant with .Net syntax, ‘X’ rather than ‘_x’ or ‘CacheAsBitmap’ rather than the camel cased ‘cacheAsBitmap’. More importantly, the Xamlon generated swfs do not actually use words at all for identifiers at all, so the MapTo attribute indicates a text identifier should be used (rather than a numeric slot). Here is a simple mapping example:

[MapTo("cacheAsBitmap", false)]
public bool CacheAsBitmap;

Notice how the value is type safe, so mc.CacheAsBitmap=true; will work, however mc.CacheAsBitmap=77; will fail to compile. This is mapped as a field, however if that value needs to be read only it would be better to map it as a property without a setter. Adding xml comments will expose those to VisualStudio’s intellisense as well, so this is well worth doing (much handier than a trip to the help files, esp when the help files don’t exist yet!)

CacheAsBitmap is in the Movieclip class (in Flash 8), so we need to subclass our existing MovieClip stub class (found in the SwfNative.dll) and add whatever new properties we like. Two are added for this demo – notice how Filters is an array of BitmapFilters… something we’ll have to implement as that is new to Flash 9 as well.

using SwfNative;
namespace SwfNative8
{
 [Implement(false)] // prevents this from ending up in the swf 
 public class MovieClip8 : MovieClip
 {
  [MapTo("cacheAsBitmap", false)]
  public bool CacheAsBitmap;

  [MapTo("filters", false)]
  public BitmapFilter[] Filters;
 }
}

 
Mapping the DropShadowFilter Class

Mapping the DropShadowFilter works much the same way, so well just cover a few new bits of information here, and then probably it is easiest to just look over the code. First, DropShadow is a BitmapFilter, the base class for all filters (including blur, glow, bevel, convolution etc.). This is done just as you normally would in C#, making a class and subclasses. The base class can implement any properties or methods common to all subclasses, in this case the Quality setting is common to all Filters, so it can go in the BitmapFilter class.

One nice thing about C# is enums, and the Quality property is a good example of where they are useful. In the Flash Player API the three quality settings are 1, 2, and 3 (low medium and high if that isn’t obvious – and it isn’t). This kind of thing can be hard to remember – for example you need to remember that these start at one and not zero. What we can do is simply use an enum instead, and while that will still become 1, 2 or 3 in your generated swf, you no longer need to worry about its actual value, as your code will use the enum FilterQuality.High (much easier to read as well!). There is no special mapping needed when using enums to map to Flash Player API values, though be sure to indicate the actual values as well as the names:

 /// <summary>
 /// Sets the filter quality on bitmap filters.
 /// </summary>

 public enum FilterQuality
 {
  Low  = 1,
  Medium = 2,
  High  = 3,
 }

The last unusual mapping has to do with the new API types now using namespaces. A quick and easy way to map this is to just give the full path in the constructor (as well as any static methods. So for example DropShadowFilter is in the ‘flash.filters’ namespace, so the constructor mapping looks like:

[MapTo("flash.filters.DropShadowFilter", true)]
public DropShadowFilter()
{
}

Note that all your stub classes like this are never implemented, so you do not need any code inside methods or constructors (other that to satisfy the return value -- return null; or return 0; will satisfy most cases there).

As you can guess, these stub classes can be used to map to any existing objects, so for example they can also be used to access code of your own written in actionscript if you like
 
Code Example

You can now access these stub classes from your own code, here is an example of how that would look. Feel free to play around with the sample or add to SwfNative8.

DropShadowFilter dsf = new DropShadowFilter();
dsf.Color = 0x080066;
dsf.Alpha = 0.5;
dsf.Distance = 15;
dsf.BlurX = 6;
dsf.BlurY = 6;
dsf.Angle = 35;
dsf.Knockout = false;
dsf.Quality = FilterQuality.High;
dsf.Type = "outer"; // "inner" "all"
dsf.Inner = false;

mc.Filters = new BitmapFilter[]{dsf};

There is a run.bat file in the bin/debug folder that shows one way to set up your command line, or if you have Visual Studio you can use the Xamlon templates there as well.

To map other Flash Player 8 types, you will need to look online for information pertaining to the Flash 8 API, though it will still probably require a bit of jiggling and guessing. Here are some places that may help you get started:

http://osflash.org/doku.php?id=flashcoders:undocumented:flash8
http://www.develotec.com/flash8api.txt
http://script.com.ua/dev/materials.php?id=11

And of course if you have any questions, discoveries, or demos you are dying to show someone, please join us at the Xamlon forms!
http://www.xamlon.com/forums/messages.aspx?ForumID=14


 

posted on Sunday, September 04, 2005 4:01 PM
Feedback
No comments posted yet.

Blog Stats

  • Posts - 121
  • Stories - 1
  • Comments - 1441
  • Trackbacks - 47

.Net Blogs

01101 Blogs

Flash Blogs

Graphics

People