<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>guy in the chair . com &#187; flash</title>
	<atom:link href="http://guyinthechair.com/tag/flash/feed/" rel="self" type="application/rss+xml" />
	<link>http://guyinthechair.com</link>
	<description>//the blog of Paul Taylor</description>
	<lastBuildDate>Fri, 18 Nov 2011 01:22:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>The Flash Text Engine, Part 1: Overview</title>
		<link>http://guyinthechair.com/2010/06/the-flash-text-engine-part-1/</link>
		<comments>http://guyinthechair.com/2010/06/the-flash-text-engine-part-1/#comments</comments>
		<pubDate>Thu, 03 Jun 2010 10:18:20 +0000</pubDate>
		<dc:creator>Paul Taylor</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[community]]></category>
		<category><![CDATA[advanced text rendering]]></category>
		<category><![CDATA[cff]]></category>
		<category><![CDATA[compact font format]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[Flash Text Engine]]></category>
		<category><![CDATA[FTE]]></category>
		<category><![CDATA[text layout]]></category>
		<category><![CDATA[Text Layout Framework]]></category>
		<category><![CDATA[TextField]]></category>
		<category><![CDATA[tiny tlf]]></category>
		<category><![CDATA[tinytlf]]></category>
		<category><![CDATA[TLF]]></category>

		<guid isPermaLink="false">http://guyinthechair.com/?p=250</guid>
		<description><![CDATA[This is the first post in what will be a multi-part series about the Flash Text Engine, a new low level text API in Flash Player 10. To clarify, this series isn&#8217;t about Adobe&#8217;s Text Layout Framework, which is an advanced typography and text layout framework. The Flash Text Engine is the low-level API that [...]]]></description>
			<content:encoded><![CDATA[<p>This is the first post in what will be a multi-part series about the Flash Text Engine, a new low level text API in Flash Player 10.</p>
<p style="padding-left:15px;padding-right:15px;color:#999999">To clarify, this series isn&#8217;t about <a href="http://labs.adobe.com/technologies/textlayout/">Adobe&#8217;s Text Layout Framework</a>, which is an advanced typography and text layout framework. The Flash Text Engine is the low-level API that TLF is built on. In Flash Player 10, the FTE resides in the <a href="http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/text/engine/package-detail.html">flash.text.engine</a> package.</p>
<p>The FTE is designed to render text &#8220;document style&#8221;. It&#8217;s primarily meant to replace the TextField for advanced uses, not provide a whole framework for text layout on the scale of an HTML rendering engine.</p>
<p>The FTE handles what I call <strong>flow</strong>: formatting that causes text to be pushed to the next line in a paragraph. I don&#8217;t know if that&#8217;s the official term, but it seems to fit. It does not do <strong>layout</strong>, which is things like bullet points, indentation, wrapping around images, padding, etc., nor does it handle <strong>decoration</strong>, things like underline, strikethrough, background color, selection, etc. I believe the FTE leaves these out because 1.) layout is a much more complicated and nuanced problem than flow, one that you wouldn&#8217;t necessarily want in the FP core, and 2.) decorations don&#8217;t cause <strong>reflow</strong>, or affect whether and how text is wrapped to the next line.</p>
<p>The FTE conforms to a small MVC architecture, there are about 10 core classes that provide most of the functionality, with the rest of them encapsulating constants. Something to note, every class in the FTE is final :( more on that later.</p>
<h2>The FTE Model</h2>
<p>The basis of the Flash Text Engine model is something called <a title="flash.text.engine.ContentElement" href="http://livedocs.adobe.com/flex/3/langref/flash/text/engine/ContentElement.html" target="_blank">ContentElement</a>. ContentElement is an abstract base class. You never call <code>new ContentElement()</code> (it&#8217;s similar to DisplayObject in this regard), instead you instantiate one of its 3 subclasses: <a title="flash.text.engine.TextElement" href="http://livedocs.adobe.com/flex/3/langref/flash/text/engine/TextElement.html" target="_blank">TextElement</a>, <a title="flash.text.engine.GraphicElement" href="http://livedocs.adobe.com/flex/3/langref/flash/text/engine/GraphicElement.html" target="_blank">GraphicElement</a>, or <a title="flash.text.engine.GroupElement" href="http://livedocs.adobe.com/flex/3/langref/flash/text/engine/GroupElement.html" target="_blank">GroupElement</a>. Collectively these classes describe a Tree hierarchy for text, but I want to talk a bit more about ContentElement before we get too deep into that.</p>
<h3>ContentElement</h3>
<p>Take a look at the constructor of ContentElement:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">ContentElement<span style="color: #000000;">&#40;</span>elementFormat<span style="color: #000066; font-weight: bold;">:</span>ElementFormat = <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">,</span> eventMirror<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">EventDispatcher</span> = <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">,</span> textRotation<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;rotate0&quot;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>It has two important arguments, <strong>elementFormat</strong> and <strong>eventMirror</strong> (as well as a third less important argument, unless you&#8217;re one of the crazy types who likes to rotate text). I will come back to the eventMirror later, but for now lets just talk about <a title="flash.text.engine.ElementFormat" href="http://livedocs.adobe.com/flex/3/langref/flash/text/engine/ElementFormat.html" target="_blank">ElementFormat</a>.</p>
<p>The ElementFormat class describes most of the properties that handle text flow. It has a fontDescription member, which is exactly what it sounds like. In <a title="flash.text.engine.FontDescription" href="http://livedocs.adobe.com/flex/3/langref/flash/text/engine/FontDescription.html" target="_blank">FontDescription</a> you&#8217;ve got your standard <code>fontFamily</code>, <code>fontWeight</code>, <code>fontPosture</code> (which is traditionally the fontStyle in Flash), along with how the font is supposed to be retrieved from the depths of the Flash Player (as Compact Font Format or a device font).</p>
<p>ElementFormat has properties like <code>alpha</code>, <code>color</code>, <code>baselineShift</code>, <code>kerning</code>, etc. Basically anything that can affect <strong>reflow</strong>.</p>
<p>Ok, so that describes all you need to know for now about the ElementFormat and FontDescription objects. Now onto the implementation class you&#8217;ll use.</p>
<h3>TextElement</h3>
<p>Out of the three, TextElement is the most straightforward. It simply accepts a string of text to take care of:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">TextElement<span style="color: #000000;">&#40;</span><span style="color: #004993;">text</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">,</span> elementFormat<span style="color: #000066; font-weight: bold;">:</span>ElementFormat = <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">,</span> eventMirror<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">EventDispatcher</span> = <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">,</span> textRotation<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;rotate0&quot;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>The ElementFormat you pass in is applied to the entire string of text that this TextElement owns. So if you specify an ElementFormat with a color of <span style="color: #ff0000;">red</span>, the entire string of text will render <span style="color: #ff0000;">red</span>.</p>
<h3>GraphicElement</h3>
<p>The next one to worry about is GraphicElement. He accepts <strong>any DisplayObject instance</strong> (<strong>instance!</strong>), as well as the width and height that you wish to allocate for the Graphic in the text:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">GraphicElement<span style="color: #000000;">&#40;</span>graphic<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">DisplayObject</span> = <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">,</span> elementWidth<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">15.0</span><span style="color: #000066; font-weight: bold;">,</span> elementHeight<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">15.0</span><span style="color: #000066; font-weight: bold;">,</span> elementFormat<span style="color: #000066; font-weight: bold;">:</span>ElementFormat = <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">,</span> eventMirror<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">EventDispatcher</span> = <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">,</span> textRotation<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;rotate0&quot;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>Some of the properties of the ElementFormat will apply to the GraphicElement, such as <code>alpha</code>, <code>baselineShift</code>, etc. Obviously the GraphicElement doesn&#8217;t respect font-specific settings from the ElementFormat and FontDescription objects.</p>
<h3>GroupElement</h3>
<p>Lastly there&#8217;s the GroupElement:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">GroupElement<span style="color: #000000;">&#40;</span><span style="color: #004993;">elements</span><span style="color: #000066; font-weight: bold;">:</span>Vector<span style="color: #000066; font-weight: bold;">.&lt;</span>ContentElement<span style="color: #000066; font-weight: bold;">&gt;</span> = <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">,</span> elementFormat<span style="color: #000066; font-weight: bold;">:</span>ElementFormat = <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">,</span> eventMirror<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">EventDispatcher</span> = <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">,</span> textRotation<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;rotate0&quot;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>GroupElement is <strong>critical</strong>. GroupElement is a collection of any combination of TextElements, GraphicElements, or other GroupElements. GroupElement is the Tree functionality of FTE&#8217;s model. TextElement can&#8217;t have children, it controls a single String. Likewise, GraphicElement only describes a single DisplayObject instance. GroupElements tie it all together.</p>
<p>GroupElements provide an API for doing standard Tree functions; you can retrieve, split, merge, and group children using various methods. I speak from experience when I say you won&#8217;t often mess with this unless you&#8217;re writing an editable text field. And if you are writing an editable text field, God help you (just kidding, it is hella fun).</p>
<p>OK, enough Model talk. Onwards to&#8230;</p>
<h2>The FTE View</h2>
<p>There are two (<strong>2!</strong>) classes that make up the entirety of the Flash Text Engine&#8217;s <strong><em>View</em></strong> division: <a target="_blank" title="flash.text.engine.TextLine" href="http://livedocs.adobe.com/flex/3/langref/flash/text/engine/TextLine.html">TextLine</a> and <a target="_blank" title="flash.text.engine.TextLineMirrorRegion" href="http://livedocs.adobe.com/flex/3/langref/flash/text/engine/TextLineMirrorRegion.html">TextLineMirrorRegion</a>. Right now you can forget about TextLineMirrorRegion, as that has to do with interaction, which is a complicated topic and one which I will cover in detail later. So for now, only focus on TextLine.</p>
<h3>TextLine</h3>
<p>TextLine is a DisplayObjectContainer. Yes, that means it has the <code>get/add/removeChild</code> methods (<strong>they still work!</strong>), and is also an InteractiveObject. You can listen for all the normal interaction events. However, even though it inherits from InteractiveObject, there are a few properties that you can only read, not write. Those are detailed in the <a href="http://livedocs.adobe.com/flex/3/langref/flash/text/engine/TextLine.html">documentation for TextLine</a>.</p>
<p>TextLine adds the concept of <em>atoms</em>, which are indivisible characters in a TextLine. Individual characters are atoms, as well as any graphics you have. The important thing to know here is that atoms can never be split between lines. The FTE will measure only to the atom level, no lower. </p>
<p>Atom information can be expensive to keep around&#8230; At first the TextLine only renders its text, it doesn&#8217;t know anything about the atoms it contains. However calling various methods will cause the TextLine to create its atom data. For example, if you call <code>getAtomIndexAtPoint()</code>, the TextLine must create the info about each atom so it can then calculate which atom occurs at the point you specify. This is all well and good, but be sure to call <code>flushAtomData()</code> once you&#8217;re done so the atom data will be GC&#8217;d.</p>
<p>TextLine has a reference to the previous and next lines, because TextLine is also a <a href="http://en.wikipedia.org/wiki/Doubly-linked_list">doubly-linked list</a>! How convenient! Of course, if there is no previous or next, you know you&#8217;re the first or last lines, respectively.</p>
<p>TextLine also has a <code>validity</code> status, which is whether the ContentElement that the line represents has changed since the line was rendered. Values are described in the <a href="http://livedocs.adobe.com/flex/3/langref/flash/text/engine/TextLineValidity.html">TextLineValidity</a> class.</p>
<p>One thing that TextLine definitely <strong>is not</strong>: a Sprite. No, TextLine is a DisplayObjectContainer. The most important implication from this is that TextLine has no <strong>graphics context</strong>. This means you can&#8217;t call textLine.graphics.draw. :( Oh well.</p>
<p>TextLine is a concrete class, you use it directly, but you cannot instantiate one by calling its constructor. To do that you need&#8230;</p>
<h2>The FTE Controller</h2>
<p>There is arguably <strong>one class</strong> in the FTE&#8217;s <strong><em>Controller</em></strong> division: <a target="_blank" title="flash.text.engine.TextBlock" href="http://livedocs.adobe.com/flex/3/langref/flash/text/engine/TextBlock.html">TextBlock</a>. I say <em>arguably</em> because yeah, <a target="_blank" title="flash.text.engine.TextJustifier" href="http://livedocs.adobe.com/flex/3/langref/flash/text/engine/TextJustifier.html">TextJustifier</a> and <a target="_blank" title="flash.text.engine.TabStop" href="http://livedocs.adobe.com/flex/3/langref/flash/text/engine/TabStop.html">TabStop</a> exist, but they just affect how TextBlock does its rendering, not&#8230; hm. Ok, I&#8217;ve convinced myself that they count as Controller classes too, but only <em>barely</em>.</p>
<p>But believe me, you will come to think of TextBlock as the only Controller class too.</p>
<p>The TextBlock is a fairly standard Factory pattern implementation: TextBlock&#8217;s primary job is to accept a ContentElement as input and output as many TextLines as you want, given a width. <strong>ContentElement</strong> -> <strong>TextBlock</strong> -> <strong>TextLines</strong>. Got it? Me neither.</p>
<p>Ok, so TextBlock has this method called <code>createTextLine()</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">createTextLine<span style="color: #000000;">&#40;</span>previousLine<span style="color: #000066; font-weight: bold;">:</span>TextLine = <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">width</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">1000000</span><span style="color: #000066; font-weight: bold;">,</span> lineOffset<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">0.0</span><span style="color: #000066; font-weight: bold;">,</span> fitSomething<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span>TextLine</pre></div></div>

<p>Ok so what you do is you pass in the <strong><em>previous</em></strong> line that you created, plus the width that you want the <strong><em>current</em></strong> line to be, and TextBlock will measure out a TextLine for you. Are you seeing the doubly-linked list yet?</p>
<p>If you want to create the first line from a TextBlock, you should just pass in <code>null</code> to the <code>createTextLine()</code> method; assuming the TextBlock has content in his <code>content</code> property, and that content has at least one atom (characters or graphics), passing in <code>null</code> will <em>always</em> return a TextLine. If there are no more lines to be created, TextBlock will return <code>null</code> from the call to <code>createTextLine()</code>.</p>
<p>So from this it is simple to render the lines for a TextBlock with width 200:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> line<span style="color: #000066; font-weight: bold;">:</span>TextLine = block<span style="color: #000066; font-weight: bold;">.</span>createTextLine<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">200</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">while</span><span style="color: #000000;">&#40;</span>line<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>line<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span>= line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">height</span><span style="color: #000066; font-weight: bold;">;</span>
    line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">height</span> = <span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">;</span>
    line = block<span style="color: #000066; font-weight: bold;">.</span>createTextLine<span style="color: #000000;">&#40;</span>line<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">200</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h3>Ok, I&#8217;ve detailed a lot so far, now it&#8217;s time to get to at an example.</h3>
<h3>Flash:</h3>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_0" width="450" height="32">
      <param name="movie" value="http://guyinthechair.com/wp-content/uploads/2010/06/SimpleDemo1.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://guyinthechair.com/wp-content/uploads/2010/06/SimpleDemo1.swf" width="450" height="32">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p>Here&#8217;s the code for the above simple line rendering:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Sprite</span><span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.text</span><span style="color: #000066; font-weight: bold;">.</span>engine<span style="color: #000066; font-weight: bold;">.</span>ContentElement<span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.text</span><span style="color: #000066; font-weight: bold;">.</span>engine<span style="color: #000066; font-weight: bold;">.</span>ElementFormat<span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.text</span><span style="color: #000066; font-weight: bold;">.</span>engine<span style="color: #000066; font-weight: bold;">.</span>FontDescription<span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.text</span><span style="color: #000066; font-weight: bold;">.</span>engine<span style="color: #000066; font-weight: bold;">.</span>FontPosture<span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.text</span><span style="color: #000066; font-weight: bold;">.</span>engine<span style="color: #000066; font-weight: bold;">.</span>FontWeight<span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.text</span><span style="color: #000066; font-weight: bold;">.</span>engine<span style="color: #000066; font-weight: bold;">.</span>GroupElement<span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.text</span><span style="color: #000066; font-weight: bold;">.</span>engine<span style="color: #000066; font-weight: bold;">.</span>TextBlock<span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.text</span><span style="color: #000066; font-weight: bold;">.</span>engine<span style="color: #000066; font-weight: bold;">.</span>TextElement<span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.text</span><span style="color: #000066; font-weight: bold;">.</span>engine<span style="color: #000066; font-weight: bold;">.</span>TextLine<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
  <span style="color: #000000;">&#91;</span>SWF<span style="color: #000000;">&#40;</span><span style="color: #004993;">width</span>=<span style="color: #990000;">&quot;450&quot;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">height</span>=<span style="color: #990000;">&quot;32&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> SimpleDemo1 <span style="color: #0033ff; font-weight: bold;">extends</span> <span style="color: #004993;">Sprite</span>
  <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> SimpleDemo1<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
      <span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
      <span style="color: #6699cc; font-weight: bold;">var</span> e1<span style="color: #000066; font-weight: bold;">:</span>TextElement = <span style="color: #0033ff; font-weight: bold;">new</span> TextElement<span style="color: #000000;">&#40;</span><span style="color: #990000;">'Consider, what makes a text line a '</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">new</span> ElementFormat<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> FontDescription<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">24</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
      <span style="color: #6699cc; font-weight: bold;">var</span> e2<span style="color: #000066; font-weight: bold;">:</span>TextElement = <span style="color: #0033ff; font-weight: bold;">new</span> TextElement<span style="color: #000000;">&#40;</span><span style="color: #990000;">'text line'</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">new</span> ElementFormat<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> FontDescription<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;_serif&quot;</span><span style="color: #000066; font-weight: bold;">,</span> FontWeight<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">NORMAL</span><span style="color: #000066; font-weight: bold;">,</span> FontPosture<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ITALIC</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">24</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
      <span style="color: #6699cc; font-weight: bold;">var</span> e3<span style="color: #000066; font-weight: bold;">:</span>TextElement = <span style="color: #0033ff; font-weight: bold;">new</span> TextElement<span style="color: #000000;">&#40;</span><span style="color: #990000;">'?'</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">new</span> ElementFormat<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> FontDescription<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">24</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
      <span style="color: #6699cc; font-weight: bold;">var</span> e<span style="color: #000066; font-weight: bold;">:</span>Vector<span style="color: #000066; font-weight: bold;">.</span> = <span style="color: #0033ff; font-weight: bold;">new</span> Vector<span style="color: #000066; font-weight: bold;">.</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
      e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>e1<span style="color: #000066; font-weight: bold;">,</span> e2<span style="color: #000066; font-weight: bold;">,</span> e3<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
      <span style="color: #6699cc; font-weight: bold;">var</span> block<span style="color: #000066; font-weight: bold;">:</span>TextBlock = <span style="color: #0033ff; font-weight: bold;">new</span> TextBlock<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> GroupElement<span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
      <span style="color: #6699cc; font-weight: bold;">var</span> line<span style="color: #000066; font-weight: bold;">:</span>TextLine = block<span style="color: #000066; font-weight: bold;">.</span>createTextLine<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">stageWidth</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
      <span style="color: #6699cc; font-weight: bold;">var</span> _y<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
      <span style="color: #0033ff; font-weight: bold;">while</span><span style="color: #000000;">&#40;</span>line<span style="color: #000000;">&#41;</span>
      <span style="color: #000000;">&#123;</span>
        <span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>line<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
        _y <span style="color: #000066; font-weight: bold;">+</span>= line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">height</span><span style="color: #000066; font-weight: bold;">;</span>
        line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = _y<span style="color: #000066; font-weight: bold;">;</span>
        line = block<span style="color: #000066; font-weight: bold;">.</span>createTextLine<span style="color: #000000;">&#40;</span>line<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">stageWidth</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
      <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p style="font-size: 14px;"><strong>Holy crap Batman!</strong><br />
As you can see, it required <em>3 different TextElements <strong>and</strong> a GroupElement</em> to render some freakin&#8217; italic text in the middle of a sentence. Yeah. Par for the frickin&#8217; course.</p>
<p>In part 2 I&#8217;ll get into more details about interaction, TextBlock manipulation, all of it.<br />
Till then watch this project on github: <a target="_blank" title="tinytlf on github" href="http://github.com/guyinthechair/tinytlf">tinytlf</a>. It&#8217;s due for some major updates but it&#8217;s what I&#8217;m going to start talking about soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://guyinthechair.com/2010/06/the-flash-text-engine-part-1/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The Flex Framework and Modularity: A Manifesto</title>
		<link>http://guyinthechair.com/2010/01/modularity-a-manifesto/</link>
		<comments>http://guyinthechair.com/2010/01/modularity-a-manifesto/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 08:33:46 +0000</pubDate>
		<dc:creator>Paul Taylor</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[bitching]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[LayoutManager]]></category>
		<category><![CDATA[modularity]]></category>
		<category><![CDATA[UIComponent]]></category>

		<guid isPermaLink="false">http://guyinthechair.com/?p=164</guid>
		<description><![CDATA[If you haven&#8217;t watched Greg Burch&#8217;s excellent presentation on Slider at RIAdventure, you freakin should. Seriously, stop right now and watch it. But don&#8217;t forget about this blog post. Make sure to come back here after you&#8217;re done. The Soul of Flex Ok, from this point on I am going to assume you&#8217;ve done exactly as I [...]]]></description>
			<content:encoded><![CDATA[<p><span style="font-weight: normal; font-size: 13px;">If you haven&#8217;t watched Greg Burch&#8217;s <a title="Greg Burch's RIAdventure Slider Presentation" href="http://www.screencast.com/users/jwilker/folders/RIAdventure%202009/media/03101638-66d9-4792-97de-77a8fc7c053f" target="_blank">excellent presentation on Slider</a> at RIAdventure, you freakin should. Seriously, stop right now and watch it. But don&#8217;t forget about this blog post. Make sure to come back here after you&#8217;re done.</span></p>
<h2>The Soul of Flex<br />
<span style="font-weight: normal; font-size: 13px;">Ok, from this point on I am going to assume you&#8217;ve done exactly as I told you and watched Greg Burch&#8217;s <a title="Greg Burch's RIAdventure Slider Presentation" href="http://www.screencast.com/users/jwilker/folders/RIAdventure%202009/media/03101638-66d9-4792-97de-77a8fc7c053f" target="_blank">excellent presentation on Slider</a> at RIAdventure. Around 6 minutes in, Greg starts talking about the Soul of Flex. I didn&#8217;t have much time to consider it, but two things immediately came to mind: invalidation and styles. In the video you&#8217;ll hear me mention CSS but later agree with Greg that CSS or not, styles is the true feature.</span></h2>
<p>Of course, I agree with all the others he said were also part of the Soul: MXML, databinding, states, skinning, item renderers, containers, and components. But as I watched him remove items that weren&#8217;t part of the Soul of Flex, I thought to myself:</p>
<p style="padding-left: 30px;">&#8220;Self, this list is completely subjective and based entirely on my opinion. It is extremely likely that someone else&#8217;s definition of the Soul of Flex is different than mine, and who&#8217;s to say which one is right? Is it Adobe&#8217;s job? Now that they&#8217;ve packed a framework full of features that real people use, they&#8217;re going to remove a good many features simply because the they do not fall into Adobe&#8217;s definition of what the Soul is? That&#8217;s not right. The community is too diverse for that. Flex is the Soul of Flex.&#8221;</p>
<div id="attachment_170" class="wp-caption alignleft" style="width: 160px"><a href="http://guyinthechair.com/wp-content/uploads/2010/01/soul-of-flex.png" target="_blank"><img class="size-thumbnail wp-image-170" title="Soul of Flex" src="http://guyinthechair.com/wp-content/uploads/2010/01/soul-of-flex-150x150.png" alt="The real Soul of Flex" width="150" height="150" /></a><p class="wp-caption-text">I say Flex is the Soul of Flex.</p></div>
<p>This got me thinking about Flex with regards to mobile development. At the time of this writing, every Flex 4 component has a base class that is <a title="UIComponent" href="http://opensource.adobe.com/svn/opensource/flex/sdk/trunk/frameworks/projects/framework/src/mx/core/UIComponent.as" target="_blank">13,246 lines of code</a> (<a title="CLOC parser in JS" href="http://dnalot.com/SparkDataGrids/clocFx.html" target="_blank"><strong>1412</strong> blank lines, <strong>6423</strong> comment lines, and <strong>5411</strong> actual code lines.</a>) If you&#8217;re using Flex 4 and skinning, you have two components, so multiply potentially everything the UIComponent does by 2:</p>
<ul style="list-style-position: inside;">
<li>2x the UIComponents to initialize</li>
<li>2x the UIComponents added to the inheriting style chain</li>
<li>2x the UIComponents to make validation passes on</li>
<li>2x the event listeners that are registered</li>
<li>2x the number of objects for the DragProxy (in the DragManager) to find in its getObjectsUnderPoint() implementation</li>
<li>and on and on&#8230;</li>
</ul>
<p>This means one of two things. Either the decision to abstract skins into another UIComponent was monumentally retarded, or the UIComponent is too large and tries to do to many things. Since I believe the new skins are wonderful from an architectural/modularity standpoint, I have to pick the second choice: <strong>the UIComponent is too damn big</strong>. It tries to do too damn much. Let me reiterate: <strong>I love the new Spark skins. I hate the size of the UIComponent.</strong></p>
<h2>The Question of Responsibility<br />
<span style="font-weight: normal; font-size: 13px;">Why is the UIComponent (at the time of this writing) 13,246 lines long? The answer is features. The UIComponent violates the <a title="Single Responsibility Principle" href="http://en.wikipedia.org/wiki/Single_responsibility_principle" target="_blank">Single Responsibility Principle</a>. Here&#8217;s a list of most of the features the UIComponent encapsulates:</span></h2>
<ul>
<li>Invalidation &#8211; Implements IInvalidating.
<ul>
<li>Defines invalidate properties/size/displayList functions.</li>
<li>Defines validateNow function.</li>
</ul>
</li>
<li>Validation &#8211; Implements ILayoutManagerClient.
<ul>
<li>Defines the initialized, nestLevel, processedDescriptors, and updateCompletePendingFlag properties.</li>
<li>Defines validate properties/size/displayList functions.</li>
</ul>
</li>
<li>Styles &#8211; Implements IAdvancedStyleClient, the only visual class that does.</li>
<li>States &#8211; Implements IStateClient and IStateClient2 to support the new Fx4 states syntax.</li>
<li>Tooltips &#8211; Implements IToolTipManagerClient, the only component class that does.</li>
<li>Constraint-based layout.</li>
<li>IRepeaterClient &#8211; UIComponents (and subclasses) can be created by Repeaters.</li>
<li>Databinding.</li>
<li>Related to Databinding, implements IPropertyChangeNotifier.</li>
<li>Embedded fonts.</li>
<li>Focus &#8211; Doesn&#8217;t implement IFocusManagerComponent by name, but does implement the functions. Has a reference to the FocusManager and a focusPane property.</li>
<li>Validators &#8211; Implements IValidatorListener, which allows the UIComponent to respond to ValidationResultEvents dispatched by the Validator classes.</li>
<li>Modules &#8211; Implements IFlexModule, so if created by an IFlexModuleFactory, the factory stores a reference of itself on the UIComponent.</li>
<li>Explicit/measured- max/min &#8211; widths/heights.</li>
<li>Percent widths/heights.</li>
<li>Enabled/disabled.</li>
<li>Effects.</li>
<li>Special logic for adding/removing children.</li>
<li>All the events it creates and dispatches, including
<ol>
<li>Initialization events, like preInit and creationComplete.</li>
<li>FlexEvents like show, hide, move, resize</li>
<li>StateChangeEvents.</li>
<li>DragEvents for the List classes. Not part of the public API.</li>
<li>ToolTipEvents</li>
<li>FlexMouseEvents</li>
</ol>
</li>
</ul>
<p>Man, the UIComponent does a ton of stuff. We haven&#8217;t even gotten into the measurement, layout, containment, children handling, skinning, item renderers, or graphics functionality of Flex yet!</p>
<h2>The UIComponent is the base class for&#8230; everything<br />
<span style="font-weight: normal; font-size: 13px;">At least, everything visual. Not including Flex 3 skins. It&#8217;s the base class for all non-Fx3-skin-related visual elements.</span></h2>
<p>The UIComponent has so many responsibilities because it is the base class for so many controls. You want a Label or TextArea? They&#8217;re UIComponents. You want a Button? It&#8217;s a UIComponent. You want a container of Buttons? It&#8217;s a UIComponent.</p>
<p><span style="font-weight: normal; font-size: 13px;">But there are a few things I can&#8217;t figure out about this configuration:</span></p>
<ol>
<li>Why does a container need logic in its base class that accesses embedded fonts, or creates UITextFields from embedded font contexts (I checked, UITextField is the only class passed into createInFontContext()).</li>
<li>Similarly, why does a Label require any knowledge about processedDescriptors, states, or validators? Labels can&#8217;t have children, states, or validators. In fact, Labels can&#8217;t do much except display text. Why do they need to be IFlexModule objects? I could go on, but I think you get the point.</li>
</ol>
<p>There is a trend of WTFs like this about the UIComponent that can only be explained by, &#8220;it&#8217;s <strong>the</strong> base class and we&#8217;re trying to keep our API super clean.&#8221;</p>
<h2>There is a better way: Composited Modularity<br />
<span style="font-weight: normal; font-size: 13px;">Most of the functionality is already segregated by the liberal use of interfaces&#8230; now lets actually implement it that way.</span></h2>
<p>Almost all the functionality that the UIComponent (and subclasses of UIComponent for that matter) contains can be grouped into smaller, more discreet classes. Lets call them modules. Once this is done, the UIComponent simply exists to provide a unified API to the developer and glue with which to assemble the modules. More complex controls and components are simply composed of more complex modules.</p>
<p>For example, the functionality for embedded fonts should be isolated into its own module. Then, only the Label, Text, TextArea, and maybe some other controls that require direct access to embedded fonts need to include the &#8220;EmbeddedFontsModule.&#8221; This way, the VBox doesn&#8217;t have to include functionality for accessing embedded fonts.</p>
<p>Similarly, a VBox would include special modules for adding children, measurement, and layout. See, this is easy.</p>
<p><span style="font-weight: normal; font-size: 13px;">And while we&#8217;re at it, let&#8217;s rewrite the LayoutManager.</span></p>
<h2>You heard me. The LayoutManager. It&#8217;s in my sights.<br />
<span style="font-weight: normal; font-size: 13px;">What&#8217;s so special about 3-phased validation that it has to be hard-coded into the Flex framework? Is it phase ordering? Nest-level ordering? That&#8217;s not special. That&#8217;s algorithms.</span></h2>
<p>First, for those who don&#8217;t know, the LayoutManager is what enables Flex components to do the awesome 3-phase component lifecycle that we&#8217;ve all come to know and love. When a component is invalidated for a phase (say, invalidateProperties()), he registers with the LayoutManager. The LayoutManager adds the component to the proper invalidation queue. There are 3 invalidation queues, one for each validation phase. The invalidation queues are a special PriorityQueue implementation. This is important, because there is an order to the validation process.</p>
<p>Components have a nestLevel, which is really just their position in the display list. The Application&#8217;s nestLevel is 3. The nestLevel increases from there, so the lowest component in the display tree has the highest number.</p>
<p>When a component is added to the invalidatePropertiesQueue, it is added at its nestLevel priority. When the invalidatePropertiesQueue is processed, components with the lowest nestLevel, the ones closest to 0, are processed first. This is called &#8220;top-down&#8221; processing, because it starts at the top of the display list and processes to the bottom.</p>
<p>The commit phase does top-down processing, because generally parents can commit properties on themselves and change properties on their children, which will then get committed. The measure phase is from the bottom-up (can you guess what that means? If not, put it in the comments.), because a parent will often make a decision on his size based on the sizes of his children. The update phase is another top-down queue, because now that the sizes and positions are calculated, it&#8217;s time to lay them out. This is what makes percent widths/heights, scrollbars, etc. possible.</p>
<h3>Rethinking validation<br />
<span style="font-weight: normal; font-size: 13px;">Does validation have to be hardcoded? Are the 3 phases all that are needed? How hard would it be to switch to just 2? 4? 100?</span></h3>
<p>The fact is, the developer should be able to hook into the power of phased updates for himself. Why have a manager that only validates 3 hard-coded phases, when you could have a manager that validates an unlimited number of dynamic phases?<br />
<span style="font-weight: normal; font-size: 13px;">Well sure Paul, that sounds great and all, but it&#8217;s kind of a tough problem.</span> &lt;&#8211; Shut up alternate text, I&#8217;ve got a solution.</p>
<p>Keep the idea of nestLevel. It&#8217;s clear, clean, and definite. Change the idea of 3 hard-coded phases into an unlimited number of injected phases, based on priorities. Similar to nestLevel for components, priorities are arbitrarily defined by the developer. A priority is an Array of ints. It can be as simple as &#8220;1&#8243; or as complex as &#8220;1.2.3.4.&#8221; It is only used as a basis for comparison between other priorities. For example, priority 1.2 is greater than priority 1.2.1, since it is assumed 1.2.1 is a subset of 1.2. However, 1.200 is much greater than 1.2, because 200 &gt; 2. Do priorities make sense? If the answer is no, put it in the comments.</p>
<p>The idea is that a developer can invalidate a component for a certain phase. The phase has a priority (like 1.3), and a direction (UP or DOWN). The ValidationManager keeps a Heap of Heaps, sorted by priority. If it doesn&#8217;t find a Heap at the priority specified, it creates a new Heap, adds the IValidationClient to it, sets the proper sort direction (UP or DOWN) on it, then adds the new Heap to the Heap of Heaps. If it does find a Heap, it adds the IValidationClient to the Heap that it found.</p>
<p>When it comes time to Validate (on the next enter_frame event), the ValidationManager dequeues each Heap from the Heap of Heaps. Then it dequeues each item from each individual Heap, validating it as it goes.</p>
<p>The beauty of this is that the Flex component lifecycle can be perfectly mimicked. Set the three phases to happen one after another, and you can get the same results. For APIs sake, lets add a gap between them, so a third party developer can come by later and inject his own phases between the 3 usual phases. If we set commit at 1.3, measure at 1.5, and update at 1.7, this should give us enough of a gap for developers to use.</p>
<p>Say Developer X wants to add some extra <strong>umph</strong> to his custom component, but needs this processing needs to happen in-phase and inbetween the measure and update phases. He simply needs to inject his own validation phase anywhere between measure at 1.5 and update at 1.7, lets keep it simple and say 1.6, and BAM. ValidationManager will validate his phase right after measure and right before update.</p>
<p>This allows the developers a whole new avenue of development, something that they would have had to tack onto one of Flex&#8217;s 3 hard-coded phases before.</p>
<p><span style="font-weight: normal; font-size: 13px;"> This is all words. Where are the actions. You fail.</span></p>
<h2>This is where I introduce FlashWorks<br />
<span style="font-weight: normal; font-size: 13px;">FlashWorks is a component set for Flash and FlashBuilder, designed around the concepts of a tiny core API and modularity.</span></h2>
<p>It includes the ValidationManager and validation scheme I&#8217;ve outlined here, as well as a few other things:</p>
<ul>
<li>MXML development</li>
<li>Skinning similar to Flex 4</li>
<li>Graphics primitives (extend EventDispatcher)</li>
<li>Measurement and Layout controllers with support for percent widths/heights</li>
<li>Injectable Validation phases</li>
<li>States, including support for FB4&#8242;s new States syntax</li>
<li>Styles, through a controller that interfaces with <a title="F*CSS Flash CSS parsing" href="http://fcss.flashartofwar.com/" target="_blank">Jesse Freeman&#8217;s F*CSS</a></li>
<li>Databinding</li>
<li>SystemManager that works with FB4&#8242;s code generation, so the FlashWorks movie is 2 frames, just like Flex.</li>
<li>A modular, pay-as-you-go system, so functionality is only included when it&#8217;s used. For instance, the States controller isn&#8217;t included unless States are defined on the component.</li>
</ul>
<h2>Alright, here&#8217;s the code and demos</h2>
<p><font color="#000099">[Edit]</font> I&#8217;ve since moved from this project onto <a href="http://github.com/guyinthechair/reflex">Reflex</a>. I feel it has more of a chance for success than a solo framework by me would.<br />
I&#8217;m keeping the demo and demo code here as a proof of concept:<br />
Demo: <a title="FlashWorks demo" href="http://guyinthechair.com/misc/flashworks/FlashWorks.html" target="_blank">http://guyinthechair.com/misc/flashworks/FlashWorks.html</a><br />
Source code for demo: <a title="FlashWorks Demo Source" href="http://guyinthechair.com/misc/flashworks/srcview/index.html" target="_blank">http://guyinthechair.com/misc/flashworks/srcview/index.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://guyinthechair.com/2010/01/modularity-a-manifesto/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Understanding the Flex SystemManager</title>
		<link>http://guyinthechair.com/2010/01/understanding-the-flex-systemmanager/</link>
		<comments>http://guyinthechair.com/2010/01/understanding-the-flex-systemmanager/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 22:06:50 +0000</pubDate>
		<dc:creator>Paul Taylor</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[MXML]]></category>
		<category><![CDATA[SystemManager]]></category>

		<guid isPermaLink="false">http://guyinthechair.com/?p=113</guid>
		<description><![CDATA[Before I start, let me say that there has been a lot of discussion about the SystemManager on various blogs. Deepa posted a fantastic write-up on the SystemManager back in October 07, which really got me interested in learning about it more. Working on Flex-less MXML is what finally pushed me to understand exactly why [...]]]></description>
			<content:encoded><![CDATA[<p>Before I start, let me say that there has been a lot of discussion about the SystemManager on various blogs. <a title="Deepa's fantastic blog on Flex" href="http://iamdeepa.com/blog/" target="_blank">Deepa</a> posted a fantastic <a title="SystemManager: Every Flex application's best friend" href="http://iamdeepa.com/blog/?p=11" target="_blank">write-up</a> on the SystemManager back in October 07, which really got me interested in learning about it more. Working on Flex-less MXML is what finally pushed me to understand exactly why it&#8217;s such a critical class. There&#8217;s a lot to cover about the SystemManager, from the behavior of the Flash Player to the behavior of the Flex compiler, so buckle up, cause this is a long one.</p>
<p><strong>Note 1: A recurring theme in this post is the fundamental behavior of the Flash Player. Flex would be nowhere without Flash, so if we desire true understanding of Flex, we must also understand the Flash Player.<br />
Note 2: For some of this, it is helpful to turn on the -keep-generated-actionscript flag in the FlashBuilder compiler options. To do this, go to Project &#8211;&gt; Properties &#8211;&gt; Flex Compiler. In the &#8220;Additional compiler arguments&#8221; field, type &#8220;-keep&#8221; at the end of the arguments list. FlashBuilder generates ActionScript from all your MXML, including a dynamic subclass of SystemManager. This will instruct FlashBuilder to place that code in the bin-debug/generated folder.</strong></p>
<p><span style="font-weight: normal; font-size: 14px;">To understand the SystemManager, you must first understand that&#8230;</span></p>
<h2>Every Flex Application is a 2-Frame Flash Movie</h2>
<p><img class="alignleft size-full wp-image-114" title="2 Frame Flash Movie" src="http://guyinthechair.com/wp-content/uploads/2010/01/loader_content_flash.png" alt="2 Frame Flash Movie" width="302" height="63" /> Yes, every one of them. The Flash Player is capable of downloading just the data for the first frame and streaming the rest of the movie. Therefore a trend started in the earlier days of Flash-only developing; you typically put a very small loader object on your first frame that displays the loading progress of the rest of the SWF to the user. That way the user has a visual indication that something is happening, and the movie gets preloaded to a point where the user won&#8217;t experience any playback hiccups.</p>
<h2>That is awesome&#8230; why do I care?<br />
<span style="font-weight: normal; font-size: 14px;">Good question. First, lets examine the implications of Flash&#8217;s load-and-stream feature:</span></h2>
<p>From a movie perspective if frames aren&#8217;t loaded, the movie stops until they are loaded. If the movie is constantly experiencing this, the movie&#8217;s framerate essentially becomes a function of the speed of the user&#8217;s internet connection.<br />
From a code perspective, <strong>if frames aren&#8217;t fully loaded, classes can&#8217;t be accessed</strong>. Calling <code>new MyObject()</code> will throw a runtime error, &#8220;1065: Variable MyObject is not defined.&#8221; This is because the frame that contains the definition for that class hasn&#8217;t been fully loaded.</p>
<h2>Oh I see now, so we need a small preloader on the first frame?<br />
<span style="font-weight: normal; font-size: 14px;">Exactly. All that silly Flash that we thought we left behind when we became &#8220;Enterprise Flex Developers&#8221; has come back to bite us in the ass. But never fear, the SystemManager is here.</span></h2>
<p><strong>The SystemManager is a fancy MovieClip</strong>. The SystemManager has a few duties, including:</p>
<ul>
<li>Create and initialize the pre-loader to show to the user during app load and startup.</li>
<li>Manage loading in RSLs.</li>
<li>Manage module logic. If the SystemManager is the root of the SWF, he knows he is a Flex Application. If he is not the root of the SWF, he knows he is a module, and must communicate with whichever SystemManager is the root of the SWF (see if you can spot the logical flaw of the assumption made in that sentence, there will be a quiz later). SystemManager must communicate events both ways between modules/app, such as mouse and keyboard events.</li>
<li>Manage Event.RESIZE events dispatched from the Stage.</li>
<li>Manages the embedded fonts list</li>
<li>Initializes the various manager singletons, such as ResourceManager and StylesManager.</li>
<li>Manages top-level application windows. The SystemManager maintains a <code>cursorChildren</code> list and a <code>popUpChildren</code> list, for tooltips and pop-up windows respectively.</li>
<li>Once all the code for the movie is loaded, SystemManager creates your actual Application instance and calls <code>initialize()</code>.</li>
<li>Adds the Application to the stage once the Application dispatches its FlexEvent.CREATION_COMPLETE event. This is why the Application doesn&#8217;t have a reference to the stage when its FlexEvent.CREATION_COMPLETE is dispatched, when all the other components do.</li>
</ul>
<h2>How it works<br />
<span style="font-weight: normal; font-size: 14px;">How the SystemManager does its thing. Matching colors are event listening/dispatching pairs.</span></h2>
<table border="0">
<tbody>
<tr>
<td>
<h3>Frame 1</h3>
</td>
<td>
<h3>Frame 2</h3>
</td>
</tr>
<tr>
<td width="50%">
<ul>
<li>stop()</li>
<li>listen for player <span style="color: #ff0000;">init</span> event</li>
<li><span style="color: #ff0000;">init</span> dispatched:
<ol>
<li>get root SystemManager (if we&#8217;re not the top level)</li>
<li> listen for <span style="color: #ff6600;">enterFrame event</span></li>
<li>create preloader</li>
<li>listen for <span style="color: #66FF00;">preloaderDocFrameReady</span> from preloader</li>
<li>listen for <span style="color: #999900;">complete</span> from preloader</li>
<li>load RSLs and wait</li>
<li>RSLs loaded. <span style="color: #66FF00;">preloaderDocFrameReady</span> dispatched by preloader
<ul>
<li><span style="color: #3366ff;">Timer created</span> to go run nextFrame() after 100 milliseconds</li>
</ul>
</li>
<li>Exactly 10 milliseconds after preloaderDocFrameReady dispatched, <span style="color: #999900;">complete</span> dispatched by preloader</li>
<li><span style="color: #3366ff;">Timer event</span> dispatched, nextFrame() called/<span style="color: #ff6600;">enterFrame handler</span> triggered</li>
</ol>
</li>
</ul>
</td>
<td width="50%">
<ul>
<li>Managers initialized.</li>
<li>Stage Resize listener added.</li>
<li>Application instance created.</li>
<li>listener for Application <span style="color: #ff00ff;">creationComplete</span> added</li>
<li>nextFrame()</li>
<li>Application <span style="color: #ff00ff;"> creationComplete</span> dispatched
<ol>
<li>Preloader removed</li>
<li>Application added to the stage</li>
<li>applicationComplete event dispatched</li>
</ol>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
<h2>Hold on, I&#8217;m a Flex developer. I don&#8217;t work with frames.<br />
<span style="font-weight: normal; font-size: 14px;">I don&#8217;t have access to the timeline of my SWF. Actually, you do.</span></h2>
<p>It&#8217;s through an undocumented meta tag, [Frame], which is really just a shortcut for the -frames compiler option. You can <a title="[Frame] meta tag explanation" href="http://blogs.adobe.com/rgonzalez/2006/06/modular_applications_part_2.html" target="_blank">read all about the frame meta tag</a> at Roger Gonzalez&#8217;s blog, but here&#8217;s the gist of it: If you add the [Frame] meta tag to the top of the Application class of your project, the Flex compiler will insert a frame before the frame that your Application is on. If you specify the factoryClass property on the meta tag, the Flex compiler will generate a subclass of whichever class you specify as the factory, and put that on the frame it creates. If you open up the mx.core.Application class, you&#8217;ll see the line [Frame(factoryClass="mx.managers.SystemManager")] near the top. This tells the Flex compiler to create a frame before the mx.core.Application, and attach a generated subclass of mx.managers.SystemManager.</p>
<p>The Flex compiler will generate a subclass of your factory class that implements <code>IFlexModuleFactory</code>. As long as your SystemManager implements IFlexModuleFactory, your code will compile. The Flex compiler overrides the info() method and returns information specific to your Application, such as a list of embedded fonts and the name of your Application.</p>
<p>And with this meta tag, you can override the default SystemManager implementation. As Roger says, &#8220;if the [Frame] metadata exists on the base class of your application, a subclass of the factory will be generated. If the metadata is directly on your application class, it will be honored, but no subclass will be generated; its assumed that you&#8217;ve already written the appropriate factory.&#8221;</p>
<h2>Why this is cool</h2>
<p>Most Flex developers will never even know the SystemManager exists, and they shouldn&#8217;t have to. Adobe has done a good job of masking Flash behind a more professional, Java-like framework with Flex. If you want to write an app with zero dependence on the Flex framework, but still use the fantastic FlashBuilder IDE, debugger/profiler, MXML, and binding, this is really good to know. You can write an Application based on Sprite, write your own stripped down version of the SystemManager, and then write MXML and take advantage of Flex&#8217;s bindings and generated code. Rock on.</p>
]]></content:encoded>
			<wfw:commentRss>http://guyinthechair.com/2010/01/understanding-the-flex-systemmanager/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Particle Emitter Publishing Tool</title>
		<link>http://guyinthechair.com/2009/11/particle-emitter-publishing-tool/</link>
		<comments>http://guyinthechair.com/2009/11/particle-emitter-publishing-tool/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 07:19:34 +0000</pubDate>
		<dc:creator>Paul Taylor</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[misc]]></category>
		<category><![CDATA[BitmapFilter]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Tweensy]]></category>

		<guid isPermaLink="false">http://guyinthechair.com/?p=61</guid>
		<description><![CDATA[I&#8217;m tired, so I&#8217;m going to keep this quick. The Emitter class by Tweensy is fantastic and fun to use, but it&#8217;s not easy to imagine what an effect will look like without coding up an example. That&#8217;s why I&#8217;ve put together a publishing tool for Tweensy&#8217;s particle Emitter. I&#8217;m using 15 sample particles embedded [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m tired, so I&#8217;m going to keep this quick. The <a href="http://docs.flashdynamix.com/tweensy/fx/index.html?com/flashdynamix/motion/extras/Emitter.html&amp;com/flashdynamix/motion/extras/class-list.html" target="_blank">Emitter</a> class by <a href="http://www.lostinactionscript.com/blog/index.php/2009/01/05/tweensy-goes-public/" target="_blank">Tweensy</a> is fantastic and fun to use, but it&#8217;s not easy to imagine what an effect will look like without coding up an example.</p>
<p>That&#8217;s why I&#8217;ve put together a publishing tool for Tweensy&#8217;s particle Emitter. I&#8217;m using 15 sample particles embedded in a SWF, <a href="http://guyinthechair.com/wp-content/flex/emitterfx/assets/particles/particles.swf" target="_blank">which you can download here</a>. <a href="http://guyinthechair.com/wp-content/flex/emitterfx/index.html" target="_blank">Launch the editor here</a>.</p>
<p>Some features of this editor include:</p>
<ul>
<li>The ability to save configurations to a file to be loaded again later (serializing the data to AMF and reading it in to restore state). This means your designers can fiddle with the animations and then email you the configuration.</li>
<li>A code generater for each Emitter and each BitmapFilter applied, which means you can load up the configuration the designers sent you and pretty much copy/paste the code into your project.</li>
</ul>
<p>I will update to add more effects, like the <a href="http://docs.flashdynamix.com/tweensy/fx/index.html?com/flashdynamix/motion/effects/PerlinDisplacementEffect.html&amp;com/flashdynamix/motion/effects/class-list.html" target="_blank">PerlinDisplacementEffect</a> that helps with the <a href="http://www.tweensy.org/examples/fireSmokeFX.html" target="_blank">really cool fire effects</a> Tweensy is capable of, but for now this is what I&#8217;ve got.</p>
<p>Once you add an emitter, you&#8217;ll need to set at least the emission frequency, emission randomness, and particle life to see anything happen. After that I suggest moving the X/Y coords and turning on either the Blur filter or the ColorTransform filter. Changing the start and end colors does some dramatic things, as well as fiddling with the blendMode (I suggest either Normal or Add). If you&#8217;ve got any questions or feature requests, leave them in the comments. Good luck!</p>
<p><span style="color: #339966;">Update</span>: Here&#8217;s two sample emitters to start off with: <a href="http://guyinthechair.com/wp-content/flex/emitterfx/configurations/particleEffect1.amf">Effect 1</a> and <a href="http://guyinthechair.com/wp-content/flex/emitterfx/configurations/fireworks.amf">Fireworks</a> (right click and &#8220;Save as&#8230;&#8221;, keeping the .amf extension).</p>
<p><span style="color: #339966;">Update 2</span>: Added the PerlinDisplacementEffect. Try it out!</p>
]]></content:encoded>
			<wfw:commentRss>http://guyinthechair.com/2009/11/particle-emitter-publishing-tool/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Physics and Flex: Box2DFlashAS3 meets Flex</title>
		<link>http://guyinthechair.com/2009/10/physics-and-flex-box2dflashas3-meets-flex/</link>
		<comments>http://guyinthechair.com/2009/10/physics-and-flex-box2dflashas3-meets-flex/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 09:17:51 +0000</pubDate>
		<dc:creator>Paul Taylor</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[MXML]]></category>

		<guid isPermaLink="false">http://guyinthechair.com/?p=26</guid>
		<description><![CDATA[Update: I have since abandoned this project in favor of the PushButton Engine, which wraps Box2D and provides a better graphics and component set than I could hope to achieve on my own. So far this has served as a useful academic exercise, and I will keep the code here for future reference. The Setup [...]]]></description>
			<content:encoded><![CDATA[<p><span style="color: #ffcc00;">Update</span><span style="color: #ffcc00;">:</span> I have since abandoned this project in favor of the <a title="PushButton Engine" href="http://pushbuttonengine.com/" target="_blank">PushButton Engine</a>, which wraps Box2D and provides a better graphics and component set than I could hope to achieve on my own. So far this has served as a useful academic exercise, and I will keep the code here for future reference.</p>
<h3>The Setup</h3>
<p>For those not in the know, <a title="Box2D" href="http://www.box2d.org/" target="_blank">Box2D</a> is an open source 2D physics engine for C++. There are many ports of it, and luckily there&#8217;s one for <a title="Box2D Flash AS3" href="http://box2dflash.sourceforge.net/" target="_blank">AS3 (Box2DFlashAS3)</a>. The current version (2.0.2) looks very similar to the C++ version, and might be a straight port (I&#8217;m not entirely sure). It follows the C++ syntax of class prefixes and allows public access to (what should be) internal member variables. 2.1 promises cleaned up syntax and fixed accessor types, but until then, the library can seem daunting to the uninitiated.</p>
<h3>The Problem</h3>
<p>The main problem is that Box2D doesn&#8217;t define a (reusable) way to graphically represent objects on the screen. It only simulates the interactions between the objects mathematically. This is very efficient, but not very convenient if you need to rapidly prototype a physics based application. There are <a title="Box2d Flash World Construction Kit" href="http://www.sideroller.com/wck/" target="_blank">tools</a> to <a title="b2ide Flash components" href="http://code.google.com/p/b2ide/" target="_blank">build</a> Box2D worlds for the Flash environment, but they emphasize working within the Flash IDE. I hate and loathe the Flash IDE (not AS3 projects themselves, mind you). I want something tailored for Flex. I want something that fits into the rapid application development cycle of most Flex apps, which means I want to use MXML. I want to stay up to date with the Box2D project, so when 2.1 does roll around, I won&#8217;t have to rewrite application code.</p>
<h3>The Solution</h3>
<p>Enter FlexWorld. FlexWorld is a framework for graphical representation of Box2D bodies. It is most similar to the <a title="Box2D Flash World Construction Kit" href="http://www.sideroller.com/wck/" target="_blank">World Construction Kit</a>, but is meant exclusively for Flex. You get all the benefits of Flex, including the <a href="http://livedocs.adobe.com/flex/3/langref/mx/managers/LayoutManager.html" target="_blank">LayoutManager</a>, styles, events, data binding, and MXML.</p>
<h3>The Demo</h3>
<p><a href="http://guyinthechair.com/wp-content/flex/flexWorld/demo/index.html" target="_blank">FlexWorld Demo (launches in a new window)</a></p>
<h3>How it Works</h3>
<p>FlexWorld starts with a PhysicalWorld, which is a subclass of <a title="The Container class" href="http://livedocs.adobe.com/flex/3/langref/mx/core/Container.html" target="_blank">Container</a>. PhysicalWorld has boundaries determined by its width and height. Children of the PhysicalWorld are typically subclasses of the abstract class PhysicalBody (which extends <a title="The UIComponent class" href="http://livedocs.adobe.com/flex/3/langref/mx/core/UIComponent.html" target="_blank">UIComponent</a>). Right now, only the PhysicalBox and PhysicalCircle classes are available. World initialization happens only when you say it does, and clean up is as efficient as can be. To get going, initialize the world: PhysicalWorld#createWorld. Then, you can register each PhysicalBody with the world by calling PhysicalWorld#registerBody. This is to ensure proper instantiation and update. When the world is ready to go, just call PhysicalWorld#activate, and it will start rendering.</p>
<h3>In-Phase updates</h3>
<p>PhysicalWorld renders by calling PhysicalBody#update on each PhysicalBody registered with it. This render cycle falls into the commitProperties phase of the component lifecycle (more on that <a title="About the component instantiation life cycle" href="https://www.adobe.com/livedocs/flex/201/html/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Book_Parts&amp;file=ascomponents_advanced_148_05.html" target="_blank">here</a> and <a title="About the steps for creating a component" href="https://www.adobe.com/livedocs/flex/201/html/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Book_Parts&amp;file=ascomponents_advanced_148_06.html#219779" target="_blank">here</a>). This ensures that properties are changed at the beginning of the lifecycle, just before commits will happen. If the render is meant to continue, updateDisplayList (which is at the end of the update cycle) will call UIComponent#invalidateProperties on the PhysicalWorld.</p>
<h3>Where is this going</h3>
<p>This is a very alpha release. Everything that you see works, and not much else. The shapes aren&#8217;t interactive yet, and they don&#8217;t do much drawing beyond what you see. Nothing is documented (and I&#8217;m horrible at keeping up with it), I don&#8217;t even have an SVN repository set up yet. Right now it&#8217;s primarily an interesting proof-of-concept rather than a useful tool. I&#8217;ll be developing it more for a project I&#8217;m currently on, and hopefully I&#8217;ll roll those changes in.</p>
<h3>The Source</h3>
<p>The source for the demo is <a title="FlexWorld demo source" href="http://guyinthechair.com/wp-content/flex/flexWorld/demo/srcview/index.html" target="_blank">here</a>.<br />
The source for the library (including Box2DFlashAS3 2.0.2) is <a title="FlexWorld library source" href="http://guyinthechair.com/wp-content/flex/flexWorld/source/FlexWorld.zip" target="_blank">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://guyinthechair.com/2009/10/physics-and-flex-box2dflashas3-meets-flex/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Flex Equalizer</title>
		<link>http://guyinthechair.com/2009/01/flex-equalizer/</link>
		<comments>http://guyinthechair.com/2009/01/flex-equalizer/#comments</comments>
		<pubDate>Tue, 20 Jan 2009 02:00:04 +0000</pubDate>
		<dc:creator>Paul Taylor</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[sound]]></category>

		<guid isPermaLink="false">http://guyinthechair.com/?p=13</guid>
		<description><![CDATA[This is not a new concept in Flash, but as far as I could tell, there weren&#8217;t many solutions for this in Flex. Here&#8217;s my stab at doing an equalizer. Watch out for your ears, the volume is set to 100% by default. http://guyinthechair.com/wp-content/flex/equalizer/EqualizerApplication.html Fixed I do have one small problem with it, and I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p>This is not a new concept in Flash, but as far as I could tell, there weren&#8217;t many solutions for this in Flex. Here&#8217;s my stab at doing an equalizer. Watch out for your ears, the volume is set to 100% by default.</p>
<p><a title="Equalizer" href="http://guyinthechair.com/wp-content/flex/equalizer/EqualizerApplication.html" target="_self">http://guyinthechair.com/wp-content/flex/equalizer/EqualizerApplication.html</a></p>
<p><span style="color: #00ff00;">Fixed</span><br />
I do have one small problem with it, and I&#8217;m working to figure it out&#8230; for reasons unknown to me the <span style="font-family: 'Courier New'; white-space: pre;">SoundMixer.computeSpectrum</span> function will return a null array sometimes. I have absolutely no clue why, and I&#8217;m open to any suggestions as to why. It only errors in the first 5 seconds.</p>
<p><span style="color: #ffff00;"><span style="color: #00ff00;">Update:</span></span> Figured it out. The problem was that I was relying on EnterFrame to call the render method on the equalizer. Unfortunately, EnterFrame could potentially be dispatched before the sound file was loaded from the server, therefore <span style="font-family: 'Courier New'; white-space: pre;">computeSpectrum</span> wouldn&#8217;t have any sound data to work with. This would cause <span style="font-family: 'Courier New'; white-space: pre;">computeSpectrum</span> to return me a null array.</p>
]]></content:encoded>
			<wfw:commentRss>http://guyinthechair.com/2009/01/flex-equalizer/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

