Simple OpenPyro example with source: Image Viewer

Work on OpenPyro goes well, and I keep getting people asking me for examples of the framework in use. After receiving another request today on an OpenPyro example, I figured I’d write a quick widget and release it with the source. So check out this very simple Photo Browser app. The source can be found here. The application is simple on purpose but shows some parts I want to highlight. The widget is a List module on one side and an Image container inside another container on the other side. As you click on a list item, the source of the Image component changes. Here are some key parts of the code:

Measurement:
This of course is the main thing I worked on in OpenPyro. In the main class, the shell lays out a header and a PhotoModule component and a Spacer vertically using a VLayout object. The PhotoModule is sized by setting its percentWidth and percentHeight to 100, and the layout does the rest. Pretty sweet.
The List component within the PhotoModule class is sized similarly with a width of 200 and the ScrollPane is sized at 100% of the unused space.

List with custom renderer:
The List module implementation here is also an interesting example that shows how a List module can work with custom renderers. The code looks like:

[AS]
var list:List = new List();
var itemRendererFactory:ClassFactory = new ClassFactory(DefaultListRenderer)
itemRendererFactory.properties = { percentWidth:100,
height:30,
rollOutBackgroundPainter:new FillPainter(0x444444),
labelFormat:new TextFormat(“Arial”, 12, 0xffffff)}
list.itemRenderer = itemRendererFactory;
list.dataProvider = picData;
list.layout = new VLayout()
list.skin = new AuroraContainerSkin()
list.width = 250;

var listPainter:GradientFillPainter = new GradientFillPainter([0x222222, 0x111111])
listPainter.rotation = Math.PI/2

list.backgroundPainter = listPainter
list.percentHeight = 100;

addChild(list);

[/AS]

Automatic scrollbar creation on containers
The module on the right that shows the photo is basically just an Image control within a UIContainer. If you click on the Asterix photo, the photo is bigger than the container and the container automatically puts the scrollbars in. Its also important to realize that the only reason the photo became bigger than the containing UIContainer was because the image did not have a width,height assigned to it. In that case, all UIControls default into a child based measurement strategy and their sizes are determined by the children they contain.

Using BackgroundPainters
I love the backgroundPainter api on the basic control (UIControl). In one or two lines, you can set the entire look of a component. The code looks something like:

[AS]
var bgPainter:GradientFillPainter = new GradientFillPainter([0,0×333333]);
bgPainter.cornerRadius = 10;
bgPainter.rotation = Math.PI/2
shell.backgroundPainter = bgPainter;

[/AS]
If the shell in the above case resizes, the painter is re-triggered and repaints correctly.

FileSize
Once again the killer part of the framework is the file size. This example is about 21 K.

There is a lot of stuff here I am pretty proud of, but again, it still needs a lot of work. If you are interested in the project and are coming to MAX San Francisco this year, I would love to meet you. My twitter id is @arpit.

me

See you at Max

BarCampPhilly 2008: What a blast !

BarCamp Philly

This weekend Philadelphia was host to its first BarCamp event. For the uninitiated, BarCamp is an international network of user generated conferences รขโ‚ฌโ€ open, participatory workshop-events, whose content is provided by participants (wikipedia link here). Barcamp is described as an “unconference”, with the sessions being a lot more casual and more like conversations than presentations. Having been to a bunch of conferences before, I definitely found the organic nature of BarCamp rather refreshing. When I walked in at 8:15-ish in the morning, I had no plans of presenting anything but swept by the energy in the group, I ended up giving not one but two sessions (more on that later).

Working for Comcast, and pretty familiar with how it is most often a subject of blame than praise, the biggest surprise for me was how positively the attendees spoke of Comcast. For example, a session on ROI on social media was dominated by a discussion on the @comcastcares twitter account run by Frank Eliason from Comcast support. Always available a tweet away, Frank has been answering twitter users when they have Comcast related troubles. The conversation around how Comcast “gets it” was definitely new (kudos @comcastcares ๐Ÿ™‚ ). Comcast definitely had a strong showing at Barcamp this year, with a bunch of us attending as well as volunteering for the event. Aaron Held, the director of engineering at Comcast Interactive Media (the division I work for) gave a talk on building Comcast.net, a site that boasts being one of the top 10 portals on the web, to a packed room. Aaron and I also had a round table discussion about innovating within a corportation, and I personally got a lot out of that (I’ll post a blog post on that sometime later this week).

CIM @ BarCampPhilly2008: Innovation within

Also, continuing my agenda for a Flash-y world, I gave another talk on using Flash in an AJAX world, where I talked about where Flash had certain advantages over AJAX (besides the richer interfaces it enables). Kevin Fitzpatrick, another fellow Flash dev for CIM gave a talk on sharing your ideas early and leveraging the community to take an idea from concept to a product. Livia, another fellow Cimian (yes thats what we call ourselves now ๐Ÿ˜‰ ) from our IA group also gave a talk titled “Jedi Mind Tricks – mediating user, brand, technology & business goals through conversation” which unfortunately I missed as I forgot to check for new sessions on the schedule. Wonder if anyone took notes on that one.

After an intense day of a lot of fun sessions, the day ended at the always fantastic National Mechanics bar, which was made even more geek friendly with a hooked up Rock Band and (gasp!) an open bar! It was amazing.

Hats off to the BarCamp organizers Roz Duffy and JP Toto and the entire volunteer army. You guys did an amazing job. Here’s to a lot geekier Philadelphia :).

Oh and btw, one of the things I had to do was animate a wordle that the BarCamp peeps created. This ran on a couple of big screen displays at the conference. I am attaching it here for posterity and the source can be found here.

https://arpitonline.com/barcamp/wordle.swf

Aha, so I am not crazy! Choose Flash for portable apps. Yahoo says so.

For as long as I have been a Flash developer, I have been completely addicted to it. And its only getting better with age. But off late, a lot of people have been eager to prophesize the death of Flash (usually siting poor Flash implementations as reasons). However there are certain features (not counting the wow effect) in Flash that make it a good choice for certain implementations. For one thing Flash Player (esp greater than 9 which 98% of the users now have) is much faster in processing data than javascript, especially on older browsers. Another reason is that a self contained swf file is a lot more portable when moving from one environment to another. In today’s world of Facebook widgets, OpenSocial containers, etc etc, deploying a swf is a lot easier than packaging a suite of javascript files. Actually a lot of those containers won’t even let you deploy JS on their servers and force you either to use an html extension like FBML or use an IFrame (and again there are limitations set on where those can appear on the sites). So it was really heartening to see that officially be posted on the Yahoo Developer Networks blog by Jonathan LeBlanc, Senior Software Engineer / Technology Evangelist from Yahoo. So do give it a read and use that as ammo if you are in another one of the “Why Flash” conversations.

The post can be found here.

Also take a look at the Yahoo Open Strategy (Y!OS)’s Flash SDK here (Yahoo has only released a Flash and a PHP SDK).

Go Flash !

Mixing up OpenPyro and Facebook: FacebookTV

For the last couple of weeks, I have been working quite a bit more on OpenPyro. Writing a new UI framwork in a vaccum almost never works, so I needed an app to build using OpenPyro. I had some Facebook API code handy so I figured I’d try something with that. So here is a screencast of the app as it is right now. The application uses Facebook’s desktop api to get all the friends of a user and their favorite TV and Movies. Then I group the shows/movies together to see what the most popular of them are and sort them with the most popular ones at top. The data is rendered in an OpenPyro chart component. The TV and Movies are in two different Horizontal Chart components and sit inside an OpenPyro container called SlidePane. SlidePane extends the OpenPyro ViewStack (which is very similar to the Flex ViewStack) and automatically handles sliding between different views (each being an OpenPyro UIContainer). Clicking on an entity does a show/movie lookup from Fancast.com, and also lists who among your friends have added the show/movie as their favorite. The BarChart below is segments those friends by gender or by relationship status ( I have to say, the Facebook API is pretty rocking, and I can segment the data on a variety of axes. )

Here is the awesome-est image of it all:

FacebookTV filesize

Yup. The whole sucker is about 60K. And that includes Labels, Buttons, Lists, ComboBoxes, Layouts, Painters, Containers, etc etc.

So here is the show:

http://content.screencast.com/users/arpit_mathur/folders/Jing/media/4fdecbce-0ed3-44ea-9394-24f06dce5770/bootstrap.swf

OpenPyro is still not ready for a general release yet, but its coming along pretty sweetly.

Comments welcome.

The great AS3/AS2 debate: Is it slower to work with AS3 than AS2? My take: A bit

Mike Chambers, Principal Product Manager for developer relations from Adobe, has been pinging the Flash community about whether AS2 based development is faster than AS3 for Flash projects. Having spent a long time in both languages, I thought I would post my 2 cents on this blog. So my answer is: Yes. Of course I’ll qualify my statement by saying that 1) There are a lot of capabilities in Flash 9+ that are amazing but I want to disconnect the language (pure syntax) from what it enables and 2) This post assumes you are as comfortable with AS3 as AS2. I am not factoring in the learning curve for a new language, which happens anytime there is a drastic change.

AS3 can use some help. Let me list out some reasons why I say that:

  1. Loading XML is a complete pain
  2. Calling an external URL is AS2 was simple (although having to use delegate because of the lack of proper closures was retarded). The code was something like this:
    [AS]
    var xml:XML = new XML()
    xml.ignoreWhite = true;
    xml.onLoad = function(success){
    if(success){}
    else{}
    }
    xml.load(‘doc.xml?foo=bar’)
    [/AS]

    Very readable, plus all errors were available in one check. Lets compare that to AS3:

    [AS]
    XML.ignoreWhite = true;
    var urlLoader:URLLoader = new URLLoader()
    var urlRequest = new URLRequest(‘doc.xml’)
    var urlVariables = new URLVariables()
    urlVariables.foo = bar
    urlRequest.data = urlVariables
    loader.addEventListener(IOErrorEvent.IOError, onError)
    loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
    loader.addEventListener(Event.COMPLETE, onComplete)
    urlLoader.load(urlRequest)

    [/AS]

    How verbose is that? I am still trying to figure out why every request needs a URLRequest object, seems unnecessary. just calling urlLoader.load(“data.xml”) could automatically create a URLRequest object, if you ever want to examine a request post it being called. Another huge problem is most developers I know dont actually add the error event listeners, just cause adding it is so tiresome. So when an error occurs later, they are usually clueless to what happened (was it a code bug or did some http call out just did not work?).

    The URLRequest param for navigateToURL seems another one of those requirements that seem unnecessary.

  3. Whoops, forgot my addChild
  4. I think this is where I have lost countless hours, I would create a sprite, and set its properties, but forget to add it to the stage…and then spend too much time to figure out what did I do wrong. The only way to create a new UI element in AS2 was by referencing an already added MovieClip (remember my_mc.createEmptyMovieClip() ?). I would love to have a createAndAdd function in DisplayObjects that becomes the default way of creating new Sprites.

  5. Dynamic drawing: The graphics object
  6. Drawing in AS2 was simpler: MovieClip.lineTo(….). In AS3 it has to be on the graphics context of the DisplayObject (mySprite.graphics.lineTo() ), but why? There isnt any other context to draw on, and the graphics context isnt swappable, so why do I have to type that in?

  7. No numeric depths
  8. In AS2, you could set explicit depth numbers to elements and reorder elements under those depths. So for example, if you were writing a button control, you could set the label field up on depth 1000 and keep swapping out the children under it. In AS2, you have to track where in the display tree your element is.

  9. useHandCursor vs ButtonMode + useHandCursor
  10. I dont know what buttonMode does besides actually allow me to useHandCursor property, which I have to set anyway

  11. No onReleaseOutside
  12. This one also is weird. The way we now have to do an onReleaseOutside is via listening to a stage click event. But wait, you may not be on stage yet, so you need to check : if(stage), and also write an event listener to the addedToStage event. Verbose.

  13. addEventListener vs. onRelease
  14. onRelease (and similar “on…” functions) is not a bad default hook to have. Considering how often we write these functions, having a quick shortcut to the action was nice. Not that I am saying the event model is bad, but I end up writing a lot of addEventListeners and removeEventListener throughout the code.

I think overall, when I look at an AS3 file, it just seems longer than AS2 code used to. I think everything needs to be very explicitly set in AS3 with no smart defaults. Maybe that’ll change in AS4 now that we no longer care for the ECMA spec ;). There are a lot of improvements in AS3 as well though, like actually having private and protected namespaces, but I am only listing out my pet peeves here hoping Mike takes note :). Flash is an amazing platform, and I love what it enables me to do.

Isn’t quality code a feature ?

I have had a few conversations with some fellow developers here and there (most often with Mat) around code quality, and sometimes I get really frustrated to see what passes for “Good enough for now” code. In user interface development, its very easy to confuse all work with all visually displayable features. But when you are going to live with the code for a while or this code is going to be the foundation of later features, how can you not track code quality?

And of course there are extremes to that argument. I have worked with enough wannabe-architects who want the code to be pristine and who would much rather write their perfect piece of software no matter what the cost (in terms of time, features dropped or developer-pay) is. They should be tarred and feathered cause they weaken the valid calls for code quality.

But there has to be a middle ground here. The problem is well-written code is hard to quantify. Features are blessed with visibility but architecture isn’t. I had a conversation with a friend yesterday on how I prefer being a developer to a designer since design is so subjective, and what I might think is wonderful design may be pooh-poohed by someone else (worse, my theoretical manager), but code either works or it doesn’t and thats instant feedback.

Software architecture or “well-written code” seems to suffer from the same problem of subjectivity as design to some extent. And if you are fortunate enough to actually have a conversation around code quality (and I dont mean in the theoretical sense but you actually sit down and review what you have written), its lack of visible benefits to the product makes it one of the first things to be dropped when trying to make deadlines.

How do you quantify code quality then? As a developer I can tell you it has real returns. In my (Flash) world it sometimes translates to faster load times, less taxing of the CPU, smaller number of classes to be maintained, but its really hard to measure. How do you say the swf weighing 300k is worth the extra couple of weeks than a swf weighing 600k that was never written but would have if checks on code quality weren’t there. How do you measure against something that never existed ! Worse, how do you track that when there is no feature that maps to lighter swfs ?

And then there are the the really invisibles: dead code paths within code, extra classes that you dont use or are so badly written that they are such pain to work with that other developer on the project just writes new classes with the same functionality? How do you check such things? How do you justify investment in tools that enable high quality code ?

Heck, if you had to justify version control, how would you? Thank God some things like that dont have to be justified anymore but when will that stature be given to tools like Review-Board. Is code quality just a developer itch ?

Do you track code quality ? How ? Do you use any particular tools ? Do you have coding standards ? Please share your thoughts here, cause I am really curious.

Does good code-base just accidentally happen to get written or is there a process to get to it ?

A sneak peek at OpenPyro: A lightweight alternative to Flex

For the last few months, I have worked quite a bit with Flex and at times, the complexity of the framework really makes it hard to work with. I have mentioned my problems with Flex in an earlier post but the gist of it was:

  • A lot of our projects are widgets rather than apps that are as big as the webpage itself. The smaller the apps get in dimensions, the more unacceptable the load time becomes, so we could not create apps that compiled to swfs greater than 250k (even with framework caching).
  • Flex apps are memory intensive on their own and I really need that memory for other things like rendering high def video (which a lot of our apps do).
  • I want to build apps that are more expressive that what Flex lets me do.

Given these requirements, I have started working on an open source framework modeled after the Flex framework but with the primary goal of being really light and easy to extend. The project is actually being developed within the Flash team at Comcast Interactive Media to allow us to develop apps faster and give all the developers here a common standard to develop against. It started out as an R&D project but is now an official CIM project, so will definitely evolve in the next few months.

OpenPyro is not really complete but its at a good point where I can show it off a little bit and get some feedback. Here is what the framework does so far:

  • A bunch of pre-built components like Buttons, Sliders, Scrollbars, etc (1.0 release should have most of the components we are used to)
  • Has an explicit component lifecycle very similar to Flex with the concept of invalidation and differed UI update
  • Layout management including using percent based layouts (so you can set a dimension of a control as a percent of the container its sitting in using percentWidth or percentUnusedWidth). Also there are no layout specific containers (like H or VBox) but rather a container can be assigned any layout.
  • Runtime skinning
  • Separation the look of the controller (based on a skin file) with its behavior (like Flex 4 or OpenFlux) [work in progress]
  • Concept of Painters (not as complex as Degrafa yet but still pretty good and getting better)
  • The components are not tied to the framework. So you can just use a component like a Scrollbar or Slider in your As3 / Fla project.

The best part is even some pretty complex layouts like the image below weigh only around 23k. Imagine doing that with separate V and HBoxes.

There is also a bunch of other classes (a collections framework-esque package as well as a host of utility classes) that I will migrate to OpenPyro once the core components are at a good point. So stay tuned for that.

Of course with all its wins, there are still things in Flex that OpenPyro will not be able to do:

  • Data Binding: Since Flex does this at compile time using code generation, this has not been done.
  • XML based layout: This may happen later (and may or may not be compile time) but this is definitely a phase 2 thing.

I will post more examples in a couple of days to show some actual apps built with OpenPyro, but in the meanwhile take a look at the video below and let me know if you have any comments on the direction the framework is taking. If you want to look through the source code, the project is hosted at the github. OpenPyro will be released under the MIT license. There is also a google group for the project at http://groups.google.com/group/as3-pyro-user-group/.

https://arpitonline.com/blog/examples/openPyro/OpenPyro_intro.swf

Re: Bit-101 on putting the Flash back in Flash

I have this love/hate thing going with Flex off late (if you know me personally, this would be no surprise to you). While I do love the Flex IDE with code completion and language intelligence, the Flex apps that I see on the web are a far cry from the kind of experiences that got me into Flash. So when I heard Keith Peters, aka Bit-101, gave a talk on “Bringing the Flash back into Flash” at Flash Forward ’08, I pinged him over Twitter asking him if he could share what he presented (as I am sure others did as well). So he did and it was a good read, I strongly recommend reading it here.

Having worked a lot with both technologies, I kind of feel let down by both of them just about as much. Unlike Keith, the experience I would like to see isn’t the ostentatious experiences that made 2advanced and such studios famous. While they definitely caught the eye, the crazy zap zoom effects wear thin on the second or third visit. The experience I would like to see Flash/Flex enable is closer to the experiences that Apple seems to enable (disclaimer: I am no Apple fanboy, and find a lot of things they do completely wrong). They are simpler effects but dont make you stop doing what you are doing and wait till the animation ends. I love my IPhone and just compare the experience there to any other Flash experience on a RIA. Both Flash and Flex fail pretty bad on that front. Here is why:

Flex 2/3:

  • Poor user experience by default
  • I dont get it how the core Flex 3 experience is acceptable by Flex developers. Non scrolling lists ? C’mon ! No control over itemRenderers in List controls (can you grow one’s width and height without having it clipped off by its neighbour ? ). Hacks to get any kind of custom Tooltips ? These are few in a long list of gripes I have with the framework

  • Really hard to extend core components
  • Its really amazing how hard it is to extend any Flex component, so much so that entire frameworks have been built just to do that (read about OpenFlux here). Flex 4 promises to ease this but thats still a year away.

  • Tightly coupled framework
  • It really kills me how tightly coupled the Flex framework is. There is no way to use, for example, just the collections framework without pulling in the UI framework. Why cant dataBinding be used in pure AS3 projects ?

  • Transitions/Effects an afterthought
  • Out of the box, Flex has no effects or transitions built in. So you have to explicitly create transitions but I wish out of the box some effects were enabled.

  • File Size
  • The huge swf size of a Flex app makes it unusable for any kind of widgets or smaller dimension swfs, and Flex 4 will not change this (their roadmap states that Flex 4 files will be around the same file size as Flex 3). The size of the swf goes beyond the load time. Heavy core objects mean they are more taxing on your CPU, which explains why complex animations with more than a few UIComponent objects stutter.

Flash

  • Really poor code editor
  • The quality of the code editor is really poor in Flash, enough for me to never use it.

  • Version control nightmare
  • Any code within the timeline is a nightmare for version control so you have no idea when your project is a few months old what the changes were. Same thing with any changes to a graphical element.

  • Write everything from scratch
  • I am not a fan of the Flash components architecture, which offers less capabilities than the Flex component set. So I end up writing components from scratch.

So what I am really looking for is a framework I can use in Flex Builder that is lighter than Flex and is more expressive by default. I have been working on a project that may enable those features that I am hoping to release soon, so do stay tuned.

Quick Poll: Whats your favorite Flex feature ?

Having worked with Flex long enough there are definitely times I get really annoyed when Flex does something I feel is counter intuitive or when I have to go around a pretty complicated process to do something that I feel would be trivial in Flash. But given a limited time frame for a project, I usually default to a Flex project as opposed to a pure AS3 project just since there are so many conveniences in the framework.

I am curious how the rest of the Flex community feels now that Flex has been out there for a while and the “new-and-shiny” effect has worn off. So I would like to ask, the following question: What is your favorite feature of the Flex framework. Below is a poll widget but it may be restrictive to your answer, so feel free to comment or elaborate on what you love/hate about the framework.

Whats your favorite Flex feature ?
( polls)

Thanks all

AS3 weirdness with URLVariables for HTTP POST

Two+ hours wasted today on dealing with this. Apparently when using URLVariables to pass variables to the server using HTTP Post, using the square bracket syntax ( vars[varname] = varvalue ) sets the contentType of the URLRequest to text/html. However setting the same variables using the dot syntax (vars.varname = varvalue) sets it to text/xml. I couldn’t set the contentType to text/xml using the [] notation no matter how I tried, whether using the URLRequest.contentType property or appending a custom “Content-Type”,”text/xml” URLRequestHeader.

Hope this helps someone.