Saturday, September 1, 2007

as3mathlib (formerly WIS math libraries)

I've just imported the WIS mathematics library -- an excellent collection of mathematics routines -- onto Google Code. (You'll find the Actionscript 2 version of the library at its original site)

This library carries a BSD-ish license and includes support for

  • Geometric Objects and Intersection calculations
  • Integral and Differential equation calculations
  • Bezier, Quadric, Polynomial, Complex, Vector and Matrix calculations
  • Symbolic expression parsing

I'm converting the library to Actionscript 3 from Actionscript 2 as time and necessity allow. (That's converting as in getting it to work, and converting as in getting it to be object/pattern oriented). Right now it builds without errors and only a few warnings, but I haven't applied any of the unit tests or checked it for correctness or compatibility.

If you see the value of updating this well-thought out collection of functions, please get in touch and I will add you as a developer. The code is quite modular: it will be straigforward to take modest chunks and get them working independently. I wrote the original author and maintainer, who responded "By all means, continue in the evolution/integration of my library to support AS3" -- but please let me know of any other efforts to update this code, or if a similar or superior math library exists, so that I don't waste my time :).

Email me [flip at the mrflip with the dot and the com] or comment on this post if you'd like to pitch in!

Labels: , , , , , , , , , , , , , , , ,

Friday, August 24, 2007

Patches to the AS3 Cookbook Code

The Actionscript 3 Cookbook is a very helpful reference, and the example code that came with it has many good examples. Unfortunately there's a modicum of bitrot in the code: compiler warnings and errors when compiling under strict mode.

Here is a patch against the version of the code I downloaded on 2007-08-24:

files/AS3CB-patch-2007-08-24.diff

Apply it thusly.

Labels: , , , , , ,

Monday, August 20, 2007

Flex Demo: Matrix Math (and an error in the Actionscript docs)

I'm working on something that uses (an algorithm similar to) texture mapping, for which I want to precalculate the .invert() of a whole bunch of .transform.matrix objects. I'll post something about that in the next coupla days. Meanwhile, I found something perplexing in the Actionscript documentation but the possibility exists that I am just a dope so please point out an error in my reasoning. As you may know, you can represent any arbitrary combination of 2-D scalings, skews, rotations and translations using standard matrix operations. The Actionscript docs for the Matrix class mention this in passing, but has elements .b and .c switched: it should be
right: [ [a,c,tx] [b,d,ty] [0,0,1] ] and not wrong: [ [a,c,tx] [c,d,ty] [0,0,1] ].
I whipped up a MatrixMathDemo in flex to demonstrate the issue: and a writeup on Mathematical matrices and the actionscript Matrix transformations [PDF]. The 2d, 3d, 4th tabs compare the Matrix methods concat(), invert() and (deltaP/p)ointTransform() respectively with an explicit calculation of the corresponding Matrix operation: they show that in fact the documentation has b and c switched. (The code is free to reuse or modify (but give credit) in case that's useful.) Some references:

Labels: , , , , , , , , , , , , ,

Wednesday, July 25, 2007

Emacs modes for Flex

  • Emacs modes for Flex:
    • XML: nXML-mode for Emacs from James Clark Using Emacs for XML documents
    • Actionscript: actionscript-mode.el for editing actionscript files in emacs.
    • At least right now it seems you want this xml mode and this actionscript-mode.el.
    • Then, add
      (setq auto-mode-alist (append (list
       '("\\.as\\'"   . actionscript-mode)
       '("\\.\\(xml\\|xsl\\|rng\\|xhtml\\|mxml\\)\\'" . nxml-mode)
       ;; add more modes here
       ) auto-mode-alist))
      
      ;;
      ;; ------------------ Magic for XML Mode ----------------
      ;;
      
      (setq nxml-mode-hook
          '(lambda ()
       (setq tab-width        2
             indent-tabs-mode nil)
             (set-variable 'nxml-child-indent     2)
             (set-variable 'nxml-attribute-indent 2)
             ))
      
    • You can use M-x customize-group RET nxml-highlighting-faces RET to fix your colors the way you like 'em.
  • Setting up asdoc to work within Flex Builder:
    • First, install ant support (Ant is an offshoot of apache and is like Makefile only more betterer.)
    • Then set up a build.xml in your docs/ directory to build the documentation set.
    • I had to modify mine a bit: I added <property name="Templates.dir" location="${FlexSDK.dir}/asdoc/templates/"/> <arg line='-templates-path ${Templates.dir}'/>
    • I also linked the flex home to a no-funny-characters dir:
          ln -s "/Applications/Applications/Adobe Flex Builder 2" /work/ProgramStores/Flex
         cd /work/ProgramStores/Flex
         ln -s "Flex SDK 2" FlexSDK
      Then I exported the location for the asdoc file:
          export FLEX_HOME=/work/ProgramStores/Flex/FlexSDK
      or else I got the error message:
          Exception in thread "main" java.lang.NoClassDefFoundError: Flex

Labels: , , , , , , ,

Tuesday, July 24, 2007

Adobe Flex and Custom Namespace / manifest.xml

Create a file "manifest.xml" and add the following:
<?xml version="1.0"?>
<?componentpackage>
<?!--
URI http://vizsage.com/vzg
namespace gg
package com. vizsage
-->
<component id="widget1" class="com.vizsage.controls.widget1"/>
<component id="widget2" class="com. vizsage.controls.widget2"/>
<?/componentpackage>
Notes:
  • The class part should give the full path (with / turned into .) to the corresponding .as files.
  • You don't need one <component/> for each .as file, just one for each component.
  • The comment part, like most comments (and many goggles), does nothing: all you need is a <component/> for each widget.
  • You don't have to follow the tld.domain.clevername.widgetname format, but it's what all the cool kids are doing. Just make sure the dotted path matches your files' path.
  • The dotted path and the namespace URL don't have anything to do with each other.
  • In fact, the namespace URL is completely made up: it doesn't have to exist; the compiler doesn't look for it; hell, adobe's URL doesn't even exist. It's just a tag for uniquely identifying a namespace. All that matters is that the namespace in your compiler flags and your mxml files match up.

If you use Flex Builder, go into the library project's properties, into the "Library Compiler" field -- add the namespace and manifest.xml into the respective fields. If you use the standalone package, you'll have to add an option for the Component Compiler

-include-namespaces="http://vizsage.com/vzg"  -namespace "http://vizsage.com/vzg" manifest.xml

  • You need to include the namespace and define it
  • The -namespace flag takes two arguments (a namespace and a manifest.xml)
  • The URI here has to match the ns:URL in your .mxml file.
Now your .mxml files (which can be anywhere, and not in that project) start off like
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:gg="http://vizsage.com/vzg"
layout="absolute" width="100%" height="100%"
viewSourceURL="srcview/index.html">
<?!-- ... your mxml file ... -->
  • Make damn sure the xmlns URI matches what you used before. I spent 30 minutes figuring out that http://www.vizsage.com/vzg and http://vizsage.com/vzg weren't the same thing.
  • In Flex Builder 2, you need to get your project's properties, go into "Flex Build Path", then the "Library Path" pane, and "Add SWC" (the one you built with your custom components).
  • For the command-line tools, add a flag
    -library-path+=/abs/olute/path/to/library.swc
    Make sure that's a += there.
  • Either way, applications (as opposed to libraries) don't need any compiler flags or manifest.xml nothing. The library uniquely identifies itself within a namespace, and provides files in the right .com.foo.bar hierarchy. When your .mxml file (asserts a namespace) and (includes the file) everything turns out right.
For more about namespaces see here, with one caveat: I think you're better off using the -load-config+= trick (to just tack on your changes) than hacking stuff into the main flex-config.xml file.

Labels: , , , , , , , ,

How to use exuberant CTAGS with ActionScript and Flex

How to fix CTAGS to work with ActionScript, from the vim-taglist project:
  • ActionScript Add the following lines to the $HOME/.ctags or $HOME/ctags.conf file:
    --langdef=actionscript
    --langmap=actionscript:.as
    --regex-actionscript=/^[ \t]*[(private| public|static) ( \t)]*function[ \t]+([A-Za-z0-9_]+)[ \t]*\(/\1/f, function, functions/
    --regex-actionscript=/^[ \t]*[(public) ( \t)]*function[ \t]+(set|get) [ \t]+([A-Za-z0-9_]+)[ \t]*\(/\1 \2/p,property, properties/
    --regex-actionscript=/^[ \t]*[(private| public|static) ( \t)]*var[ \t]+([A-Za-z0-9_]+)[ \t]*/\1/v,variable, variables/
    --regex-actionscript=/.*\.prototype \.([A-Za-z0-9 ]+)=([ \t]?)function( [ \t]?)*\(/\1/ f,function, functions/
    --regex-actionscript=/^[ \t]*class[ \t]+([A-Za-z0-9_]+)[ \t]*/\1/c,class, classes/
    
    Add the following lines to the ~/.vimrc or $HOME\_vimrc file:
    " actionscript language
    let tlist_actionscript_settings = 'actionscript;c:class;f:method;p:property;v:variable'
    
I actually just add these lines to my maintenance Makefile:
CTAGLANGS = --langdef=actionscript \
--langmap=actionscript:.as \
--regex-actionscript='/^[ \t]*[(private| public|static) ( \t)]*function[\t]+([A-Za-z0-9_]+)[ \t]*\(/\1/f, function, functions/' \
--regex-actionscript='/^[ \t]*[(public) ( \t)]*function[ \t]+(set|get) [ \t]+([A-Za-z0-9_]+)[ \t]*\(/\1 \2/p,property, properties/' \
--regex-actionscript='/^[ \t]*[(private| public|static) ( \t)]*var[  \t]+([A-Za-z0-9_]+)[\t]*/\1/v,variable, variables/' \
--regex-actionscript='/.*\.prototype \.([A-Za-z0-9 ]+)=([ \t]?)function( [  \t]?)*\(/\1/f,function, functions/' \
--regex-actionscript='/^[ \t]*class[ \t]+([A-Za-z0-9_]+)[ \t]*/\1/c,class, classes/'

.PHONY: ctags
ctags:
-rm -f TAGS
find . -name "*.as" -or -name "*.mxml" | ctags -eL - $(CTAGLANGS)
Take off the -e (ctags -L - $(CTAGLANGS)) if you're one of those vi users (I'm being polite because, after all, it's from them comes this tip)

Labels: , , , , , , , ,

Wednesday, June 20, 2007

Resources for learning Flex

Hints I gathered for learning Flex:

Labels: , , , , ,