<?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</title>
	<atom:link href="http://guyinthechair.com/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.2</generator>
		<item>
		<title>tinytlf Explorer App</title>
		<link>http://guyinthechair.com/2011/10/tinytlf-explorer-app/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=tinytlf-explorer-app</link>
		<comments>http://guyinthechair.com/2011/10/tinytlf-explorer-app/#comments</comments>
		<pubDate>Fri, 21 Oct 2011 07:33:18 +0000</pubDate>
		<dc:creator>Paul Taylor</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[community]]></category>
		<category><![CDATA[tinytlf]]></category>
		<category><![CDATA[tinytlf demo app]]></category>

		<guid isPermaLink="false">http://guyinthechair.com/?p=781</guid>
		<description><![CDATA[I&#8217;ve written a little feature explorer app to showcase some of tinytlf&#8217;s latest features. You can play around with it below, or you can launch the demo app into a new window. You can manipulate the renderer by editing CSS or switching which HTML document is rendered. I threw in a circular paragraph renderer just [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve written a little feature explorer app to showcase some of tinytlf&#8217;s latest features. You can play around with it below, or you can <a href="http://guyinthechair.com/flash/tinytlf/2.0/explorer/TinyTLFDemo.html" target="_blank">launch the demo app into a new window.</a></p>
<p>You can manipulate the renderer by editing CSS or switching which HTML document is rendered. I threw in a circular paragraph renderer just for fun. I also included a time indicator to show how long Flash takes to parse each HTML source. Even though Flash takes forever to serialize Strings to XML, this shows that tinytlf renders it almost instantaneously every time. I&#8217;ll talk about this more in my next post.</p>
<p>This is now the official tinytlf demo, so I&#8217;ll be updating it as I add features in the future. To demonstrate tinytlf&#8217;s capabilities beyond sanitized HTML input, I&#8217;ve included examples from <a href="http://idlewords.com/" target="_blank">Idle Words</a> and the HTML version of <a href="https://plus.google.com/u/0/113556260101951952093/about" target="_blank">Christian Cantrell&#8217;s</a> eBook, <a href="http://www.livingdigitally.net/books/farmer_one/farmer_one.html" target="_blank">Farmer One</a>.</p>
<p><a href="https://github.com/guyinthechair/tinytlf" target="_blank">Watch or fork the project on Github.</a></p>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_0" width="600" height="500">
      <param name="movie" value="http://guyinthechair.com/flash/tinytlf/2.0/explorer/TinyTLFDemo.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://guyinthechair.com/flash/tinytlf/2.0/explorer/TinyTLFDemo.swf" width="600" height="500">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

]]></content:encoded>
			<wfw:commentRss>http://guyinthechair.com/2011/10/tinytlf-explorer-app/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Hello tinytlf 2.0 (beta)</title>
		<link>http://guyinthechair.com/2011/09/hello-tinytlf-2-0-beta/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=hello-tinytlf-2-0-beta</link>
		<comments>http://guyinthechair.com/2011/09/hello-tinytlf-2-0-beta/#comments</comments>
		<pubDate>Tue, 13 Sep 2011 19:32:29 +0000</pubDate>
		<dc:creator>Paul Taylor</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[tinytlf]]></category>

		<guid isPermaLink="false">http://guyinthechair.com/?p=765</guid>
		<description><![CDATA[I recently embarked on the journey towards tinytlf v2.0, a shiny new version replete with glossy sheen, twinkling diamond star, and cash-machine *jing* sound effect. First, I gotta apologize for pushing v1 out into the world. It was crap. I&#8217;ve changed almost everything in v2, making it smaller, faster, and more feature rich. Crazy you [...]]]></description>
			<content:encoded><![CDATA[<p>I recently embarked on the journey towards <a href="https://github.com/guyinthechair/tinytlf" title="tinytlf 2.0" target="_blank">tinytlf v2.0</a>, a shiny new version replete with glossy sheen, twinkling diamond star, and cash-machine *jing* sound effect.</p>
<p>First, I gotta apologize for pushing v1 out into the world. It was crap. I&#8217;ve changed almost everything in v2, making it smaller, faster, and more feature rich. Crazy you say? Here&#8217;s a few of the additions and changes that make version 2 smarter than version 1:</p>
<ul>
<li>Using <a href="https://github.com/guyinthechair/tinytlf/blob/2e5e8e0a2f1c52340b7ef045a1bd74c124d55886/framework/core/src/org/tinytlf/TextEngineInjector.as" title="TextEngineInjector.as" target="_blank">Dependency Injection</a> instead of the Facade pattern</li>
<li>Consolidating all the maps into a <a href="https://github.com/guyinthechair/tinytlf/blob/6ada64138f3414c88dc1f09fa5628b0e479e9e09/framework/core/src/org/tinytlf/FactoryMap.as#L11" title="FactoryMap.as" target="_blank">single implementation</a></li>
<li>Integrating <a href="https://github.com/guyinthechair/tinytlf/blob/6ada64138f3414c88dc1f09fa5628b0e479e9e09/framework/core/src/org/tinytlf/html/DOMNode.as" title="DOMNode.as" target="_blank">HTML</a> and <a href="https://github.com/guyinthechair/tinytlf/blob/6ada64138f3414c88dc1f09fa5628b0e479e9e09/framework/core/src/org/tinytlf/html/CSS.as" title="CSS.as" target="_blank">CSS</a> into the core</li>
<li>Rewriting the <a href="https://github.com/guyinthechair/tinytlf/blob/0aef6b275c128b8aabfaca037bf79e62011344d4/framework/core/src/org/tinytlf/layout/sector/TextPane.as" title="TextPane.as" target="_blank">paragraph</a> and <a href="https://github.com/guyinthechair/tinytlf/blob/0aef6b275c128b8aabfaca037bf79e62011344d4/framework/core/src/org/tinytlf/layout/sector/TextSector.as" title="TextSector.as" target="_blank">line</a> renderer that handles all <a href="http://unicode.org/notes/tn22/RobustVerticalLayout.pdf" title="Robust Vertical Layout" target="_blank">variations of layout [PDF]</a>, floats, and virtualization (I&#8217;m proud of my work in this area)</li>
<li>Alongside the renderer, adding a <a href="https://github.com/guyinthechair/tinytlf/blob/e25f650e4974bcb492e011b325510c4e4e03706b/framework/core/src/org/tinytlf/html/ParagraphTSF.as" title="ParagraphTSF.as" target="_blank">parser for content blocks</a>, which is a better abstraction for implementing percent sizing, margins, padding, lists, tables, and floats than the way I did it in v1</li>
<li>A rewrite of the interaction model, implementing gestures using <a href="https://github.com/guyinthechair/raix" title="Reactive And Interactive eXtensions" target="_blank">raix</a>, a port of Microsoft&#8217;s incredible <a href="http://msdn.microsoft.com/en-us/data/gg577609" title="Reactive eXtensions" target="_blank">Rx.NET</a> framework. (events are fun again!)</li>
<li>Because of the addition of raix, I can do away with the Flash Text Engine&#8217;s <a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/engine/TextLineMirrorRegion.html" title="TextLineMirrorRegion" target="_blank">EventMirrors</a>. I&#8217;ve implemented a (faster, smaller) <a href="https://github.com/guyinthechair/tinytlf/blob/2e5e8e0a2f1c52340b7ef045a1bd74c124d55886/framework/core/src/org/tinytlf/interaction/EventMirror.as" title="EventMirror.as" target="_blank">replacement</a>. You can see this action in <a href="https://github.com/guyinthechair/tinytlf/blob/2e5e8e0a2f1c52340b7ef045a1bd74c124d55886/framework/core/src/org/tinytlf/interaction/AnchorMirror.as" title="AnchorMirror.as" target="_blank">AnchorMirror.as</a></li>
</ul>
<h2>Dependency Injection</h2>
<p>Instead of wiring everything up through the combination of a Facade and maps, I&#8217;ve switched to <a href="https://github.com/tschneidereit/SwiftSuspenders" title="SwiftSuspenders" target="_blank">SwiftSuspenders</a> for Dependency Injection. Using a formal IoC framework makes tinytlf infinitely more extensible, rids the framework of ceremony for the sake of it, and gives us sweet [Inject] syntax to boot. And SwiftSuspenders is only 15K; fucking awesome.</p>
<h2>Maps maps maps</h2>
<p>I&#8217;ve written a generic mapping class, implemented a few interfaces for the sake of IoC mappings (and to reduce map collisions), and stuck them all in the IoC container. This reduces file size and cyclomatic complexity, as each map implementation is the same. That&#8217;s not to say you can&#8217;t extend it; feel free to <a href="https://github.com/guyinthechair/tinytlf/blob/2e5e8e0a2f1c52340b7ef045a1bd74c124d55886/framework/core/src/org/tinytlf/interaction/EventMirrorMap.as#L6" title="EventMirrorMap.as" target="_blank">extend the map</a> and <a href="https://github.com/guyinthechair/tinytlf/blob/2e5e8e0a2f1c52340b7ef045a1bd74c124d55886/framework/core/src/org/tinytlf/TextEngineInjector.as#L31" title="TextEngineInjector.as" target="_blank">replace it in the IoC container</a> if required.</p>
<h2>HTML and CSS</h2>
<p>The only way anybody is going to work with text is with markup and CSS. So I wrote a small CSS parser to inject and lookup styles, and the model is a DOM built from XML.</p>
<h2>New Rendering Algorithm</h2>
<p>I completely rewrote the render and layout algorithms. The new algorithm is referentially transparent, with zero side effects (aside from a bit of TextLine caching for performance&#8217; sake).</p>
<p>What does that mean to you? There should be no weird bugs or race conditions caused by variables you can&#8217;t replicate in testing.</p>
<p>The previous rendering algorithm was shit. It&#8217;s the primary reason I never implemented editing. Virtualization was hacked in. It suffered from race conditions and required property synchronization, due to an architecture that maintained state and only worked if started in the correct configuration.</p>
<p>I&#8217;ve rewritten the layout algorithm from the ground up, solved all these problems, and exposed more points of extension.</p>
<p>If you want to see the speed of the new layout algorithm, <a href="http://guyinthechair.com/flash/tinytlf/2.0/hello_post/lipsum.html" title="Lipsum demo" target="_blank">scroll through this</a> as fast as you can (looks cool with OS X Lion&#8217;s inertial scrolling). I already know of many places it should be improved, but it&#8217;s a hell of a lot better than what it was!</p>
<h2>New interactions via Reactive eXtensions</h2>
<p>Reactive Programming is already a wave breaking over the .NET (and maybe JS?) world, but unfortunately ActionScript developers have been largely left behind by this revolution in interactive programming, and have suffered for it.</p>
<p>Big thanks to <a href="http://blog.richardszalay.com/" title="Richard Szalay" target="_blank">Richard Szalay</a> for <a href="https://github.com/richardszalay/raix/wiki/" title="raix" target="_blank">raix</a>, his port of Rx.NET to AS3.</p>
<p>I wrote a class called <a href="https://github.com/guyinthechair/tinytlf/blob/2e5e8e0a2f1c52340b7ef045a1bd74c124d55886/framework/core/src/org/tinytlf/interaction/Observables.as" title="Observables.as" target="_blank">Observables</a> which creates and manages IObservable streams for various interaction<br />
events, some of which (like <a href="https://github.com/guyinthechair/tinytlf/blob/2e5e8e0a2f1c52340b7ef045a1bd74c124d55886/framework/core/src/org/tinytlf/interaction/Observables.as#L279" title="drag gesture" target="_blank">drag</a>, <a href="https://github.com/guyinthechair/tinytlf/blob/2e5e8e0a2f1c52340b7ef045a1bd74c124d55886/framework/core/src/org/tinytlf/interaction/Observables.as#L217" title="double down gesture" target="_blank">doubleDown</a>, <a href="https://github.com/guyinthechair/tinytlf/blob/2e5e8e0a2f1c52340b7ef045a1bd74c124d55886/framework/core/src/org/tinytlf/interaction/Observables.as#L289" title="double drag gesture" target="_blank">doubleDrag</a>, <a href="https://github.com/guyinthechair/tinytlf/blob/2e5e8e0a2f1c52340b7ef045a1bd74c124d55886/framework/core/src/org/tinytlf/interaction/Observables.as#L238" title="triple down gesture" target="_blank">tripleDown</a>, and <a href="https://github.com/guyinthechair/tinytlf/blob/2e5e8e0a2f1c52340b7ef045a1bd74c124d55886/framework/core/src/org/tinytlf/interaction/Observables.as#L299" title="triple drag gesture" target="_blank">tripleDrag</a>) are compositions of event streams.</p>
<p>Observables.as also allows you to register IEventDispatchers to be included in master IObservable streams. A developer can subscribe to the master streams if he&#8217;s interested in receiving events from all IEventDispatchers registered with Observables.</p>
<h2>HTML Demo</h2>
<p>For my demos, I&#8217;ve chosen to work with the unaltered HTML from a few posts on <a href="http://idlewords.com/" title="Idle Words" target="_blank">idlewords.com</a>, specifically <a href="http://idlewords.com/2011/08/why_arabic_is_terrific.htm" title="Why Arabic is Terrific" target="_blank">Why Arabic is Terrific</a>, and the <a href="http://idlewords.com/2010/07/" title="Idle Words July 2010" target="_blank">July 2010 index page</a>. The text has many features I wish to support: malformed XHTML, CSS, inline styles, tables, lists, anchors, bidirectional text, and floats. Here&#8217;s the demo <a href="http://guyinthechair.com/flash/tinytlf/2.0/hello_post/srcview/index.html" title="demo source" target="_blank">source</a>, <a href="http://guyinthechair.com/flash/tinytlf/2.0/hello_post/assets/html/idlewords.txt" title="idlewords.txt" target="_blank">HTML</a>, and <a href="http://guyinthechair.com/flash/tinytlf/2.0/hello_post/assets/css/arabic.css" title="arabic.css" target="_blank">CSS</a>. <a href="http://guyinthechair.com/flash/tinytlf/2.0/hello_post/Main.html" title="Main.html" target="_blank">Click here</a> to open the SWF in a fullscreen window.</p>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_1" width="600" height="500">
      <param name="movie" value="http://guyinthechair.com/flash/tinytlf/2.0/hello_post/Main.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://guyinthechair.com/flash/tinytlf/2.0/hello_post/Main.swf" width="600" height="500">
      <!--<![endif]-->
        Main.swf
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<h2>Where does that leave things?</h2>
<p>I wrote this post to show people that the framework is back in active development. There are already many places I know where performance can be improved. I don&#8217;t have a TextField component yet, decorations aren&#8217;t done, and most HTML tags (and their layout configurations) haven&#8217;t been implemented. Editing is back on the roadmap, but not done either.</p>
<p>Like always, feel free to <a href="mailto:guyinthechair@gmail.com">email me</a> with questions or feature requests, and I&#8217;m always monitoring <a href="https://github.com/guyinthechair/tinytlf" title="guyinthechair's tinytlf" target="_blank">github</a> for forks and pull requests. Happy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://guyinthechair.com/2011/09/hello-tinytlf-2-0-beta/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Design-by-Contract in RobotLegs 1.4</title>
		<link>http://guyinthechair.com/2011/07/design-by-contract-in-robotlegs-1-4/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=design-by-contract-in-robotlegs-1-4</link>
		<comments>http://guyinthechair.com/2011/07/design-by-contract-in-robotlegs-1-4/#comments</comments>
		<pubDate>Tue, 05 Jul 2011 20:12:08 +0000</pubDate>
		<dc:creator>Paul Taylor</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[bitching]]></category>
		<category><![CDATA[community]]></category>
		<category><![CDATA[covariance]]></category>
		<category><![CDATA[invariance]]></category>
		<category><![CDATA[robotlegs]]></category>
		<category><![CDATA[robotlegs-variance-utility]]></category>

		<guid isPermaLink="false">http://guyinthechair.com/?p=754</guid>
		<description><![CDATA[If you talked to me at a conference sometime in the last nine months, chances are I ranted loudly about type-variant auto-mediation. At the time, I meant an implementation I wrote in PureMVC because I assumed robotlegs already supported covariant auto-mediation. It doesn&#8217;t. Explanation to follow. Flash and the City Before I begin, this was [...]]]></description>
			<content:encoded><![CDATA[<p>If you talked to me at a conference sometime in the last nine months, chances are I ranted loudly about <a href="https://github.com/dnalot/robotlegs-utilities-variance">type-variant auto-mediation</a>. At the time, I meant an implementation I wrote in PureMVC because I assumed <a href="http://www.robotlegs.org/" title="robotlegs-as3" target="_blank">robotlegs</a> already supported covariant auto-mediation. It doesn&#8217;t. Explanation to follow.</p>
<h2>Flash and the City</h2>
<p>Before I begin, this was the topic of my <a href="http://fatc.co/" title="Flash and the City" target="_blank">Flash and the City</a> talk this year. You can find the slides <a href="http://www.slideshare.net/pauletaylor/design-by-contract-in-robotlegs-as3" title="Design by Contract in robotlegs AS3" target="_blank">here</a> if you want.</p>
<h2>Auto Mediation</h2>
<p>Robotlegs has an interface called <a href="http://api.robotlegs.org/org/robotlegs/core/IMediatorMap.html" title="IMediatorMap interface" target="_blank">IMediatorMap</a>, which maps UIComponent types to Mediator types. Robotlegs watches the display list, and when instances of any mapped components are added to (or removed from) the stage, it will create (or destroy) an instance of the mapped Mediator for it.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">mediatorMap<span style="color: #000066; font-weight: bold;">.</span>mapView<span style="color: #000000;">&#40;</span>MyViewType<span style="color: #000066; font-weight: bold;">,</span> MyViewMediator<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>Each time you create and add a new instance of MyViewType to the stage, a corresponding MyViewMediator instance will be created.</p>
<h2>Problems</h2>
<p>The problem with MediatorMap is two-fold:</p>
<ol>
<li>It enforces an arbitrary restriction that each View instance can only have one Mediator instance. This is dangerous, because it blurs the line between view logic and business logic. Does this functionality belong in the UIComponent or the Mediator? Developers end up writing Views and Mediators with poor separation of concerns, and inevitably overflow View logic into their Mediators. Mediators aren&#8217;t seen as reusable bits of business logic, they&#8217;re seen as an extension to the UI component.
</li>
<li>MediatorMap is a <a href="http://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)" title="Covariance and Contravariance" target="_blank">type invariant</a> mapping. The map creates a Mediator only if the view component is an exact instance of a mapped type.
<p>For example, if you register the <code>MyViewComponentMediator</code> type for <code>MyViewComponent</code>, subclasses of <code>MyViewComponent</code> (say, <code>MyViewComponentSubclass</code>) don&#8217;t get an instance of <code>MyViewComponentMediator</code> automatically created for them.</p>
<p>If <code>MyViewComponent</code> encapsulates enough useful functionality to supplement it with its own Mediator, doesn&#8217;t <code>MyViewComponentSubclass</code> still implement (through inheritance) and require the same functionality from the MediatorMap? Sure, the subclass may override certain functions, but that&#8217;s just polymorphism at work.
</li>
</ol>
<h2>Covariant Auto Mediation</h2>
<p>The idea behind <a href="http://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)" title="Covariance and Contravariance" target="_blank">covariant</a> mediation is that a Mediator is created if a component <em>extends</em> or <em>implements</em> a mapped view component type.</p>
<p>Here&#8217;s the practical difference between an invariant check and a covariant check:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900; font-style: italic;">//Invariant check:</span>
<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>myViewComponent<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">constructor</span> == MyViewComponent<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
    <span style="color: #009900; font-style: italic;">//Do something</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">//Covariant check:</span>
<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>myViewComponent <span style="color: #0033ff; font-weight: bold;">is</span> MyViewComponent<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
    <span style="color: #009900; font-style: italic;">//Do something else</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>If the instance of <code>myViewComponent</code> is actually a subclass of the MyViewComponent type, the first check will fail, but the second check will pass.</p>
<h2>Extends or Implements</h2>
<p>Extending classes for functionality is all well and good, but it&#8217;s not as flexible or as clean as implementing an interface. For starters, AS3 allows you only one parent class, so you can&#8217;t compose much functionality through inheritance before running into the <a href="http://en.wikipedia.org/wiki/Diamond_problem" title="The Diamond Problem -- why multiple inheritance is problematic" target="_blank">diamond problem</a>.</p>
<p>Before I go any further, yes, this means you can map a Mediator for all instances or subclasses of <code>Sprite</code> or <code>UIComponent</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900; font-style: italic;">// An instance of SpriteMediator will be created each time an instance</span>
<span style="color: #009900; font-style: italic;">// or subclass of Sprite is added to the display list. Crazy, huh?</span>
<span style="color: #004993;">map</span><span style="color: #000066; font-weight: bold;">.</span>mapMediator<span style="color: #000000;">&#40;</span><span style="color: #004993;">Sprite</span><span style="color: #000066; font-weight: bold;">,</span> SpriteMediator<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>Naturally this introduces performance problems, so everything from the &#8220;flash.*&#8221; and &#8220;mx.*&#8221; packages are filtered out by default.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #004993;">map</span><span style="color: #000066; font-weight: bold;">.</span>registerPackageFilter<span style="color: #000000;">&#40;</span><span style="color: #990000;">'flash.*'</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #004993;">map</span><span style="color: #000066; font-weight: bold;">.</span>registerPackageFilter<span style="color: #000000;">&#40;</span><span style="color: #990000;">'mx.*'</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>And you can add your own package filter definitions (like if you&#8217;ve got a particle emitter and want to skip inspection of each particle):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #004993;">map</span><span style="color: #000066; font-weight: bold;">.</span>registerPackageFilter<span style="color: #000000;">&#40;</span><span style="color: #990000;">'com.myApp.particles.*'</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>Anyway, covariantly auto-mediating base classes is alright I guess, but who wants to maintain a complex inheritance tree? Not me. <strong>The real sexy magic happens when you start mapping Interfaces for auto-mediation.</strong></p>
<p>Interfaces are covariantly checked:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>myComponent<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">constructor</span> == IMyInterface<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//will always be false</span>
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>myComponent <span style="color: #0033ff; font-weight: bold;">is</span> IMyInterface<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//can evaluate to true</span></pre></div></div>

<h2>Demo Time</h2>
<p>This post has been highly theoretical so far, and if you let me continue extolling the virtues of designing interfaces to apply behaviors to components via covariant mediation it will only devolve into something more abstract and academic.</p>
<p>So instead, I&#8217;ll show off some code to do it for me. Here&#8217;s the demo app I created for my FATC talk.</p>
<p>This demo has three progressively more impressive examples of common problems solved in very few LOC using covariant mediation:</p>
<ol>
<li>An example of handling system-wide error events.</li>
<li>An example of managing selected screen state.</li>
<li>An example DataGrid where each cell is representative of a unique and open streaming channel from the server.</li>
</ol>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_2" width="595" height="400">
      <param name="movie" value="http://guyinthechair.com/wp-content/flex/covariant_mediation/FATC.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://guyinthechair.com/wp-content/flex/covariant_mediation/FATC.swf" width="595" height="400">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<h2>Rundown</h2>
<p>I&#8217;ll run through just the meaty parts of this demo. The source for the whole thing is <a href="http://guyinthechair.com/wp-content/flex/covariant_mediation/srcview/index.html" target="_blank">here</a>.</p>
<h3>App.mxml and IoC Mappings</h3>
<p>Here&#8217;s the important part of the Application MXML file:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">override <span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> createChildren<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #009900; font-style: italic;">// Normally these IoC mappings are done inside the Context.</span>
	<span style="color: #009900; font-style: italic;">// Do them here because we have access to our children.</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> context<span style="color: #000066; font-weight: bold;">:</span>FATC_Context = <span style="color: #0033ff; font-weight: bold;">new</span> FATC_Context<span style="color: #000000;">&#40;</span>systemManager <span style="color: #0033ff; font-weight: bold;">as</span> <span style="color: #004993;">DisplayObjectContainer</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> <span style="color: #004993;">map</span><span style="color: #000066; font-weight: bold;">:</span>IVariantMediatorMap = context<span style="color: #000066; font-weight: bold;">.</span>variantMap<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #009900; font-style: italic;">// Map implementations of ISystemErrorUI to have</span>
	<span style="color: #009900; font-style: italic;">// SystemErrorUIMediators registered for them automatically</span>
	<span style="color: #004993;">map</span><span style="color: #000066; font-weight: bold;">.</span>mapMediator<span style="color: #000000;">&#40;</span>ISystemErrorUI<span style="color: #000066; font-weight: bold;">,</span> SystemErrorUIMediator<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">// Map implementations of IScreen to have ScreenMediators</span>
	<span style="color: #009900; font-style: italic;">// registered for them automatically</span>
	<span style="color: #004993;">map</span><span style="color: #000066; font-weight: bold;">.</span>mapMediator<span style="color: #000000;">&#40;</span>IScreen<span style="color: #000066; font-weight: bold;">,</span> ScreenMediator<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">// Map implementations of IStreamingServiceUI to have</span>
	<span style="color: #009900; font-style: italic;">// StreamingServiceUIMediator registered for them automatically</span>
	<span style="color: #004993;">map</span><span style="color: #000066; font-weight: bold;">.</span>mapMediator<span style="color: #000000;">&#40;</span>IStreamingServiceUI<span style="color: #000066; font-weight: bold;">,</span> StreamingServiceUIMediator<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #6699cc; font-weight: bold;">var</span> injector<span style="color: #000066; font-weight: bold;">:</span>IInjector = context<span style="color: #000066; font-weight: bold;">.</span>theInjector<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">// Tell the injector to create and inject a new instance of</span>
	<span style="color: #009900; font-style: italic;">// StreamingService each time it sees a dependency on IStreamingService</span>
	injector<span style="color: #000066; font-weight: bold;">.</span>mapClass<span style="color: #000000;">&#40;</span>IStreamingService<span style="color: #000066; font-weight: bold;">,</span> StreamingService<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000066; font-weight: bold;">.</span>createChildren<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #009900; font-style: italic;">// Register the mediators for the Screens class immediately.</span>
	<span style="color: #009900; font-style: italic;">// (normally the registration is deferred to the next frame).</span>
	<span style="color: #004993;">map</span><span style="color: #000066; font-weight: bold;">.</span>registerMediators<span style="color: #000000;">&#40;</span>screens<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h3>ISystemErrorUI and the <a href="http://guyinthechair.com/wp-content/flex/covariant_mediation/srcview/source/ptaylor/bdd/errors/SystemErrorUIMediator.as.html" target="_blank">SystemErrorUIMediator</a></h3>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> interface ISystemErrorUI
<span style="color: #000000;">&#123;</span>
	<span style="color: #339966; font-weight: bold;">function</span> handleError<span style="color: #000000;">&#40;</span><span style="color: #004993;">error</span><span style="color: #000066; font-weight: bold;">:</span>SystemErrorEvent<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> errorAcknowledged<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span>ISignal<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Components that are interested in being notified when errors occur can implement the <code>ISystemErrorUI</code> interface. Whenever an error happens in the Application, it&#8217;s the responsibility of the errored operation to dispatch a SystemErrorEvent, either bubbling on the display list, or on robotlegs&#8217; shared <code>eventBus</code>. Each instance of SystemErrorUIMediator will notify its ISystemErrorUI instance.</p>
<h3>IScreen and the <a href="http://guyinthechair.com/wp-content/flex/covariant_mediation/srcview/source/ptaylor/bdd/screens/ScreenMediator.as.html" target="_blank">ScreenMediator</a></h3>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> interface IScreen
<span style="color: #000000;">&#123;</span>
	<span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">set</span> selectedScreen<span style="color: #000000;">&#40;</span>screen<span style="color: #000066; font-weight: bold;">:</span>IScreen<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> screenChangedSignal<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span>ScreenChangedSignal<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Implementations of IScreen will be notified when the <code>selectedScreen</code> changes. Implementations of IScreen can cause the screen to change by dispatching their <code>screenChangedSignal</code> member.</p>
<p>Whether the implementor of IScreen truly wishes to change the <code>selectedScreen</code>, or whether it&#8217;s simply interested when the <code>selectedScreen</code> is changed, the ScreenMediator doesn&#8217;t care. In the demo, three of the four implementations of <code>IScreen</code> respond uniquely when their <code>selectedScreen</code> setter is called:</p>
<ol>
<li>The <code><a href="http://guyinthechair.com/wp-content/flex/covariant_mediation/srcview/source/ptaylor/bdd/screens/components/Screens.mxml.html" target="_blank">Screens</a></code> class is a ViewStack. When its <code>selectedScreen</code> setter is called, it checks first whether the <code>IScreen</code> instance is a child of itself. If not, <code>Screens</code> adds it as a child. <code>Screens</code> ultimately changes its <code>selectedChild</code> property to the new <code>IScreen</code>.</li>
<li>The <code><a href="http://guyinthechair.com/wp-content/flex/covariant_mediation/srcview/source/ptaylor/bdd/errors/components/ErrorNotificationView.mxml.html" target="_blank">ErrorNotificationView</a></code> class is the bar at the top that drops down when an error occurs. If the bar is showing and the <code>selectedScreen</code> setter is called, the bar hides itself.</li>
<li>The <code><a href="http://guyinthechair.com/wp-content/flex/covariant_mediation/srcview/source/ptaylor/bdd/grid/components/StreamingServiceItemRenderer.as.html" target="_blank">StreamingServiceItemRenderer</a></code> class is the item renderer for the DataGrid. When the <code>selectedScreen</code> changes to and from the <code>GridScreen</code> instance, <code>StreamingServiceItemRenderer</code> starts or stops the incoming data stream. In this way, we can shut down any data sources for screens that aren&#8217;t currently visible.</li>
</ol>
<h3>IStreamingServiceUI and the <a href="http://guyinthechair.com/wp-content/flex/covariant_mediation/srcview/source/ptaylor/bdd/grid/StreamingServiceUIMediator.as.html" target="_blank">StreamingServiceUIMediator</a></h3>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> interface IStreamingServiceUI
<span style="color: #000000;">&#123;</span>
	<span style="color: #339966; font-weight: bold;">function</span> setStreamData<span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">:</span>StreamData<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> updateStreamInfo<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span>ChangeStreamInfoSignal<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>There&#8217;s only one implementation of <code>IStreamingServiceUI</code>: <code><a href="http://guyinthechair.com/wp-content/flex/covariant_mediation/srcview/source/ptaylor/bdd/grid/components/StreamingServiceItemRenderer.as.html" target="_blank">StreamingServiceItemRenderer</a></code>. <code>StreamingServiceItemRenderer</code> is the item renderer for each cell in the DataGrid.</p>
<p><code>StreamingServiceItemRenderer</code> takes advantage of the fact that the DataGrid reuses its itemRenderers. When the user scrolls, the DataGrid sets in a new value for <code>data</code>. When that happens, the itemRenderer dispatches its <code>updateStreamInfo</code> member Signal.</p>
<p>When the UI dispatches its <code>updateStreamInfo</code> Signal, <code>StreamingServiceUIMediator</code> tells the service instance the new channel name. Then, the service closes its current channel, and opens a stream with the new channel name.</p>
<p>Because the DataGrid reuses its itemRenderers, and the itemRenderers dispatch changes in its data values to its Mediator, the Mediator can reuse its service instance, which manages opening and closing streams. </p>
<p><strong>This means we only keep open connections for visible cells.</strong></p>
<p>Pretty cool, huh?</p>
]]></content:encoded>
			<wfw:commentRss>http://guyinthechair.com/2011/07/design-by-contract-in-robotlegs-1-4/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>FTE Layout Tricks with BreakOpportunity.ALL</title>
		<link>http://guyinthechair.com/2011/06/fte-layout-tricks-with-breakopportunity-all/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=fte-layout-tricks-with-breakopportunity-all</link>
		<comments>http://guyinthechair.com/2011/06/fte-layout-tricks-with-breakopportunity-all/#comments</comments>
		<pubDate>Fri, 03 Jun 2011 05:48:58 +0000</pubDate>
		<dc:creator>Paul Taylor</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[breakOpportunity]]></category>
		<category><![CDATA[Flash Text Engine]]></category>
		<category><![CDATA[FTE]]></category>
		<category><![CDATA[text layout]]></category>

		<guid isPermaLink="false">http://guyinthechair.com/?p=703</guid>
		<description><![CDATA[This should be a relatively short post, since it&#8217;s late and I&#8217;ve been drinking. I&#8217;m going to focus on just one property from ElementFormat: breakOpportunity. Cunning Line Breaks BreakOpportunity is relatively straightforward. It directs the Flash Text Engine of when to break new TextLines. It&#8217;s different from the standard unicode line breaking control character, the [...]]]></description>
			<content:encoded><![CDATA[<p>This should be a relatively short post, since it&#8217;s late and I&#8217;ve been drinking. I&#8217;m going to focus on just one property from <a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/engine/ElementFormat.html">ElementFormat</a>: <a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/engine/ElementFormat.html#breakOpportunity">breakOpportunity</a>.</p>
<h2>Cunning Line Breaks</h2>
<p><a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/engine/BreakOpportunity.html">BreakOpportunity</a> is relatively straightforward. It directs the Flash Text Engine of when to break new TextLines. It&#8217;s different from the standard <a href="http://en.wikipedia.org/wiki/Newline">unicode line breaking control character</a>, the break-if-you-wanna <a href="http://en.wikipedia.org/wiki/Zero-width_space">Zero-width space character</a>, and it isn&#8217;t the <a href="http://en.wikipedia.org/wiki/Soft_hyphen">soft hyphen</a> used in auto-hyphenation engines.</p>
<p>BreakOpportunity is subtler. It gives you control over line breaks without inserting control characters into your content.</p>
<h2>Documentation</h2>
<p>Here&#8217;s the documentation of <code>breakOpportunity</code>. I&#8217;m pasting it here to illustrate that this doesn&#8217;t tell the whole story.</p>
<table>
<tbody>
<tr>
<th>String value</th>
<th>Description</th>
</tr>
<tr>
<td><code>BreakOpportunity.ALL</code></td>
<td>All characters in the range are treated as line break opportunities, meaning that a line break will occur after each character. Useful for creating effects like text on a path.</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<th>Subclass</th>
<th>Effect of setting property</th>
</tr>
<tr>
<td><code>GraphicElement</code></td>
<td>Has no effect.</td>
</tr>
<tr>
<td><code>GroupElement</code></td>
<td>Determines the break opportunity between adjacent text elements in the group. If the elementFormat of the group is <code>null</code>, the format of the first of the adjacent elements is used.</td>
</tr>
<tr>
<td><code>TextElement</code></td>
<td>Determines the break opportunity between the characters in the text element.</td>
</tr>
</tbody>
</table>
<p><br/></p>
<h2>Standard Line Breaking</h2>
<p>I&#8217;ve posted this before, but here&#8217;s a refresher. Breaking lines in the Flash Text Engine couldn&#8217;t be easier:</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;">content</span><span style="color: #000066; font-weight: bold;">:</span>ContentElement = <span style="color: #0033ff; font-weight: bold;">new</span> TextElement<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Some example text to be broken into TextLine objects by a TextBlock instance.&quot;</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: #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> tb<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: #004993;">content</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 = tb<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: #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: #0033ff; font-weight: bold;">while</span><span style="color: #000000;">&#40;</span>line <span style="color: #000066; font-weight: bold;">!</span>= <span style="color: #0033ff; font-weight: bold;">null</span><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;">ascent</span><span style="color: #000066; font-weight: bold;">;</span>
    line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #004993;">y</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;">descent</span><span style="color: #000066; font-weight: bold;">;</span>
    line = tb<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>

<p>
    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_3" width="200" height="36">
      <param name="movie" value="http://guyinthechair.com/wp-content/uploads/2011/06/FTEDemo_simple.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://guyinthechair.com/wp-content/uploads/2011/06/FTEDemo_simple.swf" width="200" height="36">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
<br />
<br/></p>
<h2>Tricks</h2>
<p>But what happens if we modify the breakOpportunity of the ElementFormat?</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> ef<span style="color: #000066; font-weight: bold;">:</span>ElementFormat = <span style="color: #0033ff; font-weight: bold;">new</span> ElementFormat<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
ef<span style="color: #000066; font-weight: bold;">.</span>breakOpportunity = BreakOpportunity<span style="color: #000066; font-weight: bold;">.</span>ALL<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">content</span><span style="color: #000066; font-weight: bold;">:</span>ContentElement = <span style="color: #0033ff; font-weight: bold;">new</span> TextElement<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Some example text to be broken into TextLine objects by a TextBlock instance.&quot;</span><span style="color: #000066; font-weight: bold;">,</span> ef<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> tb<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: #004993;">content</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 = tb<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: #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: #0033ff; font-weight: bold;">while</span><span style="color: #000000;">&#40;</span>line <span style="color: #000066; font-weight: bold;">!</span>= <span style="color: #0033ff; font-weight: bold;">null</span><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;">ascent</span><span style="color: #000066; font-weight: bold;">;</span>
    line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #004993;">y</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;">descent</span><span style="color: #000066; font-weight: bold;">;</span>
    line = tb<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>

<p><br/><br />

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_4" width="200" height="920">
      <param name="movie" value="http://guyinthechair.com/wp-content/uploads/2011/06/FTEDemo16.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://guyinthechair.com/wp-content/uploads/2011/06/FTEDemo16.swf" width="200" height="920">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
<br />
<br/><br />
As you can see, when the ElementFormat for a TextElement has its breakOpportunity set to <code>BreakOpportunity.ALL</code>, the TextBlock breaks a new TextLine instance after each character. Crazy right?</p>
<p>When I figured this out about 9 months ago, my next thought was to test how GroupElement responds to BreakOpportunity:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> ef<span style="color: #000066; font-weight: bold;">:</span>ElementFormat = <span style="color: #0033ff; font-weight: bold;">new</span> ElementFormat<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
ef<span style="color: #000066; font-weight: bold;">.</span>breakOpportunity = BreakOpportunity<span style="color: #000066; font-weight: bold;">.</span>ALL<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> group<span style="color: #000066; font-weight: bold;">:</span>GroupElement = <span style="color: #0033ff; font-weight: bold;">new</span> GroupElement<span style="color: #000000;">&#40;</span>
    <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">&lt;</span>ContentElement<span style="color: #000066; font-weight: bold;">&gt;</span><span style="color: #000000;">&#91;</span>
    <span style="color: #0033ff; font-weight: bold;">new</span> TextElement<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Some example text &quot;</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: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span>
    <span style="color: #0033ff; font-weight: bold;">new</span> TextElement<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;to be broken into &quot;</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: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span>
    <span style="color: #0033ff; font-weight: bold;">new</span> TextElement<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;TextLine objects by &quot;</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: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span>
    <span style="color: #0033ff; font-weight: bold;">new</span> TextElement<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;a TextBlock instance.&quot;</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: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span>
    <span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">,</span> ef<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> tb<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>group<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 = tb<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: #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: #0033ff; font-weight: bold;">while</span><span style="color: #000000;">&#40;</span>line <span style="color: #000066; font-weight: bold;">!</span>= <span style="color: #0033ff; font-weight: bold;">null</span><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;">ascent</span><span style="color: #000066; font-weight: bold;">;</span>
    line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #004993;">y</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;">descent</span><span style="color: #000066; font-weight: bold;">;</span>
    line = tb<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>

<p>
    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_5" width="100" height="46">
      <param name="movie" value="http://guyinthechair.com/wp-content/uploads/2011/06/FTEDemo17.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://guyinthechair.com/wp-content/uploads/2011/06/FTEDemo17.swf" width="100" height="46">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
<br />
<br/><br />
Sweet. So it does what the documentation says it&#8217;ll do, break between adjacent TextElements. But is it only TextElements? What about adjacent GraphicElements?</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> ef<span style="color: #000066; font-weight: bold;">:</span>ElementFormat = <span style="color: #0033ff; font-weight: bold;">new</span> ElementFormat<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
ef<span style="color: #000066; font-weight: bold;">.</span>breakOpportunity = BreakOpportunity<span style="color: #000066; font-weight: bold;">.</span>ALL<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> group<span style="color: #000066; font-weight: bold;">:</span>GroupElement = <span style="color: #0033ff; font-weight: bold;">new</span> GroupElement<span style="color: #000000;">&#40;</span>
    <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">&lt;</span>ContentElement<span style="color: #000066; font-weight: bold;">&gt;</span><span style="color: #000000;">&#91;</span>
        <span style="color: #0033ff; font-weight: bold;">new</span> TextElement<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Some example text &quot;</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: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span>
        <span style="color: #0033ff; font-weight: bold;">new</span> GraphicElement<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> GraphicRect<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;">20</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">20</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: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span>
        <span style="color: #0033ff; font-weight: bold;">new</span> TextElement<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;with GraphicElements &quot;</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: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span>
        <span style="color: #0033ff; font-weight: bold;">new</span> GraphicElement<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> GraphicRect<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;">20</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">20</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: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span>
        <span style="color: #0033ff; font-weight: bold;">new</span> TextElement<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;to be broken into &quot;</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: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span>
        <span style="color: #0033ff; font-weight: bold;">new</span> TextElement<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;TextLine objects by &quot;</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: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span>
        <span style="color: #0033ff; font-weight: bold;">new</span> TextElement<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;a TextBlock instance.&quot;</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: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span>
    <span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">,</span> ef<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> tb<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>group<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 = tb<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: #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: #0033ff; font-weight: bold;">while</span><span style="color: #000000;">&#40;</span>line <span style="color: #000066; font-weight: bold;">!</span>= <span style="color: #0033ff; font-weight: bold;">null</span><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>totalHeight<span style="color: #000066; font-weight: bold;">;</span>
    line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">;</span>
    line = tb<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>

<p>
    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_6" width="102" height="100">
      <param name="movie" value="http://guyinthechair.com/wp-content/uploads/2011/06/FTEDemo18.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://guyinthechair.com/wp-content/uploads/2011/06/FTEDemo18.swf" width="102" height="100">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
<br />
<br/><br />
Ah ha! So it doesn&#8217;t only work for TextElements, it works for any siblings enclosed in a GroupElement. Good to know. Can you imagine the implications of this? (hint, this is important for rendering floats using the FTE!)</p>
]]></content:encoded>
			<wfw:commentRss>http://guyinthechair.com/2011/06/fte-layout-tricks-with-breakopportunity-all/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A Better Describe Type Cache</title>
		<link>http://guyinthechair.com/2011/02/a-better-describetypecache/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=a-better-describetypecache</link>
		<comments>http://guyinthechair.com/2011/02/a-better-describetypecache/#comments</comments>
		<pubDate>Wed, 09 Feb 2011 01:01:01 +0000</pubDate>
		<dc:creator>Paul Taylor</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[community]]></category>
		<category><![CDATA[describeType]]></category>
		<category><![CDATA[reflection]]></category>

		<guid isPermaLink="false">http://guyinthechair.com/?p=672</guid>
		<description><![CDATA[I don&#8217;t always work with the Flex Framework, so over and over I&#8217;ve written the following: package my.project.package.utils &#123; public class Type &#123; private static const cache:Dictionary = new Dictionary&#40;false&#41;; public static function describe&#40;value:Object&#41;:XML&#123;&#125;; &#125; &#125; The describe function caches the results of a call to describeType() based on the type of Class passed in [...]]]></description>
			<content:encoded><![CDATA[<p>I don&#8217;t always work with the Flex Framework, so over and over I&#8217;ve written the following:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> my<span style="color: #000066; font-weight: bold;">.</span>project<span style="color: #000066; font-weight: bold;">.</span><span style="color: #9900cc; font-weight: bold;">package</span><span style="color: #000066; font-weight: bold;">.</span>utils
<span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Type
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0033ff; font-weight: bold;">private</span> static const cache<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Dictionary</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Dictionary</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #0033ff; font-weight: bold;">public</span> static <span style="color: #339966; font-weight: bold;">function</span> describe<span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">XML</span><span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The describe function caches the results of a call to describeType() based on the type of Class passed in as the Value. But it&#8217;s super ugly! All over the place I&#8217;ve had to reference a static class: &#8220;Type.describe(myObject);&#8221; <strong>lame</strong>.</p>
<p>Why can&#8217;t the results of describeType() just be cached in the player? Maybe it is now and I just don&#8217;t know it? If so, please tell me!</p>
<p>So <a href="https://github.com/guyinthechair/as3-gestures/blob/master/src/org/tinytlf/utils/reflect.as">here&#8217;s the solution</a> if you&#8217;d rather have a global function than a static method call:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> org<span style="color: #000066; font-weight: bold;">.</span>tinytlf<span style="color: #000066; font-weight: bold;">.</span>utils
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.utils</span><span style="color: #000066; font-weight: bold;">.*;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> reflect<span style="color: #000000;">&#40;</span><span style="color: #004993;">type</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000066; font-weight: bold;">,</span> refreshCache<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><span style="color: #004993;">XML</span>
	<span style="color: #000000;">&#123;</span>
		const typeCache<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Dictionary</span> = ReflectionCache<span style="color: #000066; font-weight: bold;">.</span>cache<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">!</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">type</span> <span style="color: #0033ff; font-weight: bold;">is</span> <span style="color: #004993;">Class</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">type</span> <span style="color: #0033ff; font-weight: bold;">is</span> <span style="color: #004993;">Proxy</span><span style="color: #000000;">&#41;</span>
				<span style="color: #004993;">type</span> = <span style="color: #004993;">getDefinitionByName</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">getQualifiedClassName</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">type</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #0033ff; font-weight: bold;">as</span> <span style="color: #004993;">Class</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">type</span> <span style="color: #0033ff; font-weight: bold;">is</span> <span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span>
				<span style="color: #004993;">type</span> = <span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #0033ff; font-weight: bold;">else</span>
				<span style="color: #004993;">type</span> = <span style="color: #004993;">type</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">constructor</span> <span style="color: #0033ff; font-weight: bold;">as</span> <span style="color: #004993;">Class</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>refreshCache <span style="color: #000066; font-weight: bold;">||</span> typeCache<span style="color: #000000;">&#91;</span><span style="color: #004993;">type</span><span style="color: #000000;">&#93;</span> == <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span>
			typeCache<span style="color: #000000;">&#91;</span><span style="color: #004993;">type</span><span style="color: #000000;">&#93;</span> = <span style="color: #004993;">flash.utils</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">describeType</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">type</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">return</span> typeCache<span style="color: #000000;">&#91;</span><span style="color: #004993;">type</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.utils</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Dictionary</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">internal</span> <span style="color: #9900cc; font-weight: bold;">class</span> ReflectionCache
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> static const cache<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Dictionary</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Dictionary</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>An internal class! Yipee! Static, but internalized. Until now I didn&#8217;t think you could store a static stateful reference for global functions to use. </p>
<p>Now you can go around all over the place calling &#8220;reflect(myObject);&#8221;, enjoying the convenience of describeType() and the benefits of using a static method.</p>
]]></content:encoded>
			<wfw:commentRss>http://guyinthechair.com/2011/02/a-better-describetypecache/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>From Flash to Objective-C Part 1: Syntax</title>
		<link>http://guyinthechair.com/2011/02/flash-to-objective-c-syntax-part-1/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=flash-to-objective-c-syntax-part-1</link>
		<comments>http://guyinthechair.com/2011/02/flash-to-objective-c-syntax-part-1/#comments</comments>
		<pubDate>Tue, 08 Feb 2011 17:49:52 +0000</pubDate>
		<dc:creator>Paul Taylor</dc:creator>
				<category><![CDATA[community]]></category>
		<category><![CDATA[ActionScript3]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Syntax]]></category>

		<guid isPermaLink="false">http://guyinthechair.com/?p=630</guid>
		<description><![CDATA[Face it: Apple&#8217;s done a great job with the iOS UI. Consider this an axiom for the rest of the post. I&#8217;m not going to try and convince you if you disagree, and I&#8217;m not going to rehash what boils down to a religious debate. Their HIG is clutch. But programming for it is crazy! [...]]]></description>
			<content:encoded><![CDATA[<p>Face it: Apple&#8217;s done a great job with the iOS UI. Consider this an axiom for the rest of the post. I&#8217;m not going to try and convince you if you disagree, and I&#8217;m not going to rehash what boils down to a religious debate. Their <a href="http://www.google.com/url?sa=t&#038;source=web&#038;cd=1&#038;ved=0CBMQFjAA&#038;url=http%3A%2F%2Fdeveloper.apple.com%2Flibrary%2Fmac%2Fdocumentation%2FUserExperience%2FConceptual%2FAppleHIGuidelines%2FXHIGIntro%2FXHIGIntro.html&#038;ei=4SBDTem-AoWcgQeit8XuAQ&#038;usg=AFQjCNEcUV4xiwK2PW9wgW1N_o63IiGNow" title="Apple Human Interface Guidelines">HIG</a> is clutch.</p>
<p>But programming for it is crazy! Have you seen this language? What is this shit?<br />
<img src="http://guyinthechair.com/wp-content/uploads/2011/01/obj-c_code_snippet.png" alt="Are the designers of Objective-C insane??" title="obj-c_code_snippet" width="600" height="202" class="aligncenter wp-image-631" /></p>
<h1>Relax</h1>
<p>This post is Objective-C syntax for the average Flash developer. It isn&#8217;t that scary. The colors are just different than you&#8217;re used to. And you&#8217;ll have to know or learn C, but don&#8217;t worry if you&#8217;re unfamiliar, I have faith you&#8217;ll pick it up quick enough. Because you&#8217;re creative and curious and you like learning new things dammit.</p>
<h1>Syntax</h1>
<p>So why haven&#8217;t you jumped into iOS? For me, it was the syntax. So now I&#8217;m going to compare the syntax of the languages from an <a href="http://jessewarden.com/">AS3 dev&#8217;s</a> point of view. In the end, I hope you won&#8217;t feel as intimidated as I did by this weirdly beautiful language.</p>
<h1>Disclaimer</h1>
<p>I&#8217;m still learning this stuff too. If I&#8217;m wrong on anything, please post it in the comments. I&#8217;m trying to disseminate useful information, things that took me a bit of studying to figure out, in the hopes that it won&#8217;t take you as long.</p>
<h2>Primitives</h2>
<p>There are a few keyword differences:</p>
<h4>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">AS3		Objective<span style="color: #000066; font-weight: bold;">-</span>C
<span style="color: #0033ff; font-weight: bold;">true</span>		YES
<span style="color: #0033ff; font-weight: bold;">false</span>		NO
<span style="color: #0033ff; font-weight: bold;">null</span>		nil
<span style="color: #0033ff; font-weight: bold;">this</span>		self</pre></div></div>

</h4>
<h2>Variables</h2>
<p>Variable declaration and assignment:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900; font-style: italic;">// AS3</span>
<span style="color: #6699cc; font-weight: bold;">var</span> myInteger<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Objective-C</span>
<span style="color: #a61390;">int</span> myInteger <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>;</pre></div></div>

<p>You can see, this isn&#8217;t any different from C++, Java, C#, etc. The difference from AS3 is the lack of the required <strong>var</strong> or <strong>const</strong> keyword, and the type is specified <strong>before</strong> the variable.</p>
<h3>Memory Allocation</h3>
<p>Something we don&#8217;t typically think about in AS3: stack vs. heap allocation. </p>
<p>The Stack is a LIFO (last in, first out) pool of memory blocks that are allocated/deallocated very quickly. Each time you call a function, a block of memory is allocated for the function&#8217;s scope. When you return from the function, the block of memory is released back to the stack. This means stack-managed memory blocks are reused very frequently, therefore is mostly allocated from RAM or even the CPU&#8217;s cache, making it extremely fast.</p>
<p>The Heap is more complex, and allocates and deallocates memory based on usage patterns. I am not remotely familiar with how Objective-C allocates memory from The Heap, but in general, allocating/deallocating memory from the heap takes longer than from the stack.</p>
<p><a href="http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap">Here&#8217;s a better explanation of the difference between the stack and the heap.</a> This stuff is just generally good to know.</p>
<h3>Values</h3>
<p>Values are primitive types, allocated on the stack, and passed by value, or copied. That is, if you pass the variable as an argument to a function, the variable&#8217;s <strong>value</strong> is copied then passed in.</p>
<h4>Structs</h4>
<p>A struct (short for <strong>&#8220;structure&#8221;</strong>), is a type that stores multiple values. AS3 doesn&#8217;t have structs, but we typically mimic the behavior of structs with <strong>Value Objects</strong> (VOs). But when structs are passed, they are passed by value, not reference. When you pass a struct (such as CGRect in Objective-C), the struct is essentially copied and fed into the target method.</p>
<h3>Pointers</h3>
<p>Pointers are a special data type that points to a location in memory allocated from The Heap. When a variable is typed to a pointer, accessing that variable will redirect you to the value stored at the pointer&#8217;s memory address. Pointers typically point to object instances, partly because objects have state and it won&#8217;t do to pass objects by value.</p>
<p>More about pointers at Wikipedia <a href="http://en.wikipedia.org/wiki/Pointer_(computing)">here</a>.</p>
<p>The Objective-C syntax to designate pointers is to add a star (<strong>*</strong>) between the type and the variable name:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">UIButton <span style="color: #002200;">*</span>myButton <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIButton alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>This means var <code>myButton</code> is a pointer that points to a <code>UIButton</code> instance somewhere in memory.</p>
<h2>Methods</h2>
<p>Objective-C has no concept of <span style="text-decoration:line-through;">access modifiers or</span> namespaces. For the purpose of this writeup, I&#8217;ll only use public methods. For familiarity&#8217;s sake, I&#8217;m using the <strong>addChild</strong> method signature of the <strong>DisplayObjectContainer</strong> class in my examples. Also, this post isn&#8217;t meant to introduce any Objective-C paradigms, obviously Cocoa doesn&#8217;t do DisplayObjects and event listeners like Flash does.</p>
<h3>Anatomy</h3>
<p>In AS3, functions are declared in this pattern:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #004993;">namespace</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">name</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">parameters</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span>Type<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">child</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">DisplayObject</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">DisplayObject</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>In Objective-C, you have the exact same options, just moved around a bit:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>Type<span style="color: #002200;">&#41;</span>name<span style="color: #002200;">:</span> parameters;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>DisplayObject <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>addChild<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>DisplayObject <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>child;</pre></div></div>

<p>So in comparison:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">child</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">DisplayObject</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">DisplayObject</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>DisplayObject <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>addChild<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>DisplayObject <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>child;</pre></div></div>

<p>Say you&#8217;re writing the addChild method signature of the DisplayObjectContainer class:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">public function addChild<span style="color: #002200;">&#40;</span>child<span style="color: #002200;">:</span>DisplayObject<span style="color: #002200;">&#41;</span><span style="color: #002200;">:</span>DisplayObject;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>DisplayObject <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>add<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span>DisplayObject <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>child</pre></div></div>

<h4>Multiple Parameters</h4>
<p>In AS3, you want more parameters, you separate them by commas:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">type</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000066; font-weight: bold;">,</span> listener<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Function</span><span style="color: #000066; font-weight: bold;">,</span> useCapture<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: #000066; font-weight: bold;">,</span> priority<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> useWeakReference<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><span style="color: #0033ff; font-weight: bold;">void</span></pre></div></div>

<p>In Objective-C, you name them:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>addEventListener<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>type listener<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">SEL</span><span style="color: #002200;">&#41;</span>listener useCapture<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>useCapture priority<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>priority useWeakReference<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>useWeakReference</pre></div></div>

<p>This is just to show 1-1 parity with AS3. Because the order and name of the parameters is what makes up the method name, you could also write this method something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>addEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>type withListener<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">SEL</span><span style="color: #002200;">&#41;</span>callback usingCapture<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>phase withPriority<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>priority andWeakReference<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>weakReference</pre></div></div>

<p>This way, the method reads almost like a sentence: <strong>&#8220;add event type String, with listener SEL, using capture phase BOOL, with priority int, and weak reference BOOL.&#8221;</strong></p>
<h4>Static Functions</h4>
<p>In AS3, you declare a static function with the <strong>static</strong> keyword:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> instanceFunction<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">public</span> static <span style="color: #339966; font-weight: bold;">function</span> staticFunction<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>In Objective-C, you put a + in front of the method declaration:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>instanceFunction;
<span style="color: #002200;">+</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>staticFunction;</pre></div></div>

<h3>Calling Methods</h3>
<p>In AS3, if you&#8217;re calling a method, it&#8217;s straightforward like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">mySprite<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>myChild<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>Here&#8217;s the same thing in Objective-C:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>mySprite addChild<span style="color: #002200;">:</span>myChild<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>And the two lines side-by-side:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">mySprite.addChild<span style="color: #002200;">&#40;</span>myChild<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#91;</span>mySprite addChild<span style="color: #002200;">:</span>myChild<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Here&#8217;s calling our pretend addEventListener function from before:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//Calling our fictitious addEventListener method:</span>
<span style="color: #002200;">&#91;</span>dispatcher addEvent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;mouseDown&quot;</span> withListener<span style="color: #002200;">:^</span><span style="color: #002200;">&#123;</span><span style="color: #11740a; font-style: italic;">/* do something */</span><span style="color: #002200;">&#125;</span> usingCapture<span style="color: #002200;">:</span>no withPriority<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span> andWeakReference<span style="color: #002200;">:</span>no<span style="color: #002200;">&#93;</span>;</pre></div></div>

<h2>Classes</h2>
<p>In Objective-C, each class is composed of <strong>two</strong> files. One is called the <strong>header</strong> file (suffix <strong>.h</strong>), the other is the <strong>implementation</strong> file (suffix <strong>.m</strong>). This is a C convention, since C doesn&#8217;t have proper classes.</p>
<h3>Constructors</h3>
<p>ActionScript has one constructor, it&#8217;s a function named after the Class it&#8217;s constructing. There are language reasons for this, primarily because deep down, AS3 isn&#8217;t that different from JavaScript, and each class is really just one big closure. But that&#8217;s a topic for another time.</p>
<p>Objective-C has what are called &#8220;designated initializers.&#8221; A designated initializer is pretty much the same idea as a constructor. A designated initializer is just a function that returns <strong>self</strong>, the Object&#8217;s reference to itself (AS3&#8242;s equivalent of <strong>this</strong>). Since designated initializers don&#8217;t have to be named after the class, there can be many, each taking different initialization values. This is essentially constructor overloading.</p>
<p>So where in AS3, you&#8217;d write:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> MyClass<span style="color: #000000;">&#40;</span>required<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span><span style="color: #000066; font-weight: bold;">,</span> optional1<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> optional2<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>You could write the following in Objective-C:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>init; <span style="color: #11740a; font-style: italic;">//no required options, can leave this out if you wish</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>init<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>required;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>init<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">:</span>required withOption<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>option1;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>init<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">:</span>required withOption<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>option1 andAnother<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>option2;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>init<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">:</span>required orJust<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>option2;</pre></div></div>

<p>By convention, initializers start with some variation of the word &#8220;init.&#8221;</p>
<h3>Headers</h3>
<p>A header file is loosely analogous to an <strong>Interface</strong> in ActionScript 3. The header file declares all the publicly available functions and getters/setters available on the object. The header also declares what our Class inherits from, and the interfaces our Class implements. For this reason, what ActionScript actually calls an Interface, Objective-C calls a Protocol.</p>
<p>In short, all the API is declared in header files.</p>
<h4>Implementation</h4>
<p>The second part of a Class is the implementation. The implementation is the actual method and property implementation of the header file for the Class.</p>
<h4>Examples</h4>
<p><strong>ExampleClass.as:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com<span style="color: #000066; font-weight: bold;">.</span>ptaylor<span style="color: #000066; font-weight: bold;">.</span>blog <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ExampleClass <span style="color: #0033ff; font-weight: bold;">extends</span> <span style="color: #004993;">Object</span> implements IMyInterface<span style="color: #000066; font-weight: bold;">,</span> IOtherInterface <span style="color: #000000;">&#123;</span>
        <span style="color: #009900; font-style: italic;">//Constructor</span>
        <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> ExampleClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #009900; font-style: italic;">//do stuff</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p><strong>ExampleClass.h:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;UIKit/UIKit.h&gt;</span>
<span style="color: #a61390;">@interface</span> ExampleClass <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> &lt;IMyInterface, IOtherInterface&gt;
<span style="color: #002200;">&#123;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The <strong>@interface</strong> declaration tells Objective-C that this is a header file, followed by the header&#8217;s name, <strong>ExampleClass</strong>. The following colon (<strong>:</strong>) declares what type this Class will extend from. Protocol (AS3 Interfaces) implementation declaration happens after the superclass declaration, inside the <strong>&lt; &gt;</strong> block.</p>
<p><strong>ExampleClass.m:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;ExampleClass.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> ExampleClass
&nbsp;
<span style="color: #11740a; font-style: italic;">//constructor</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>init<span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">//do stuff</span>
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The <strong>@implementation</strong> declaration tells Objective-C what header this file is implementing.</p>
<p>That&#8217;s it. That&#8217;s all I&#8217;ve got. I&#8217;ve skimmed over and given you the fifty-thousand foot overview of the Objective-C continent. As always, post some shit in the comments if I&#8217;m wrong or you liked this or whatever.</p>
]]></content:encoded>
			<wfw:commentRss>http://guyinthechair.com/2011/02/flash-to-objective-c-syntax-part-1/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Fun With Comments</title>
		<link>http://guyinthechair.com/2011/01/fun-with-comments/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=fun-with-comments</link>
		<comments>http://guyinthechair.com/2011/01/fun-with-comments/#comments</comments>
		<pubDate>Sun, 23 Jan 2011 23:36:41 +0000</pubDate>
		<dc:creator>Paul Taylor</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[community]]></category>

		<guid isPermaLink="false">http://guyinthechair.com/?p=627</guid>
		<description><![CDATA[Found this comment from a commit in November 2010. Had to repost it here. In case anybody else does this too, know you&#8217;re not alone. At least the code this describes passes the unit tests. //// // Everything after this point is basically magic. It was written during // one coffee fueled weekend, and to [...]]]></description>
			<content:encoded><![CDATA[<p>Found this comment from a commit in <a href="https://github.com/guyinthechair/tinytlf/blob/880733788f62dbc2f09a6724ed883b7e40365b47/tinytlf-gestures/src/org/tinytlf/interaction/gestures/Gesture.as#L54">November 2010</a>. Had to repost it here. In case anybody else does this too, know you&#8217;re not alone. At least the code this describes passes the unit tests.</p>
<blockquote>
<p>		////<br />
		// Everything after this point is basically magic. It was written during<br />
		// one coffee fueled weekend, and to be honest, I don&#8217;t remember much of<br />
		// it. That night is just a blur in my memory. Modify at your own peril.<br />
		//<br />
		// Here be dragons.<br />
		// (I&#8217;ve always felt that any serious library needs this comment at<br />
		// least once).<br />
		////
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://guyinthechair.com/2011/01/fun-with-comments/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Tinytlf Updates and new Experiments</title>
		<link>http://guyinthechair.com/2010/11/tinytlf-updates-and-new-experiments/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=tinytlf-updates-and-new-experiments</link>
		<comments>http://guyinthechair.com/2010/11/tinytlf-updates-and-new-experiments/#comments</comments>
		<pubDate>Mon, 15 Nov 2010 17:50:15 +0000</pubDate>
		<dc:creator>Paul Taylor</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[community]]></category>
		<category><![CDATA[tinytlf]]></category>

		<guid isPermaLink="false">http://guyinthechair.com/?p=601</guid>
		<description><![CDATA[Sorry for the dearth of tinytlf posts lately, I&#8217;ve been head-down coding tons of new stuff. If you don&#8217;t believe me, check out the git repository history. I&#8217;m working on an official tinytlf demo explorer app, ala minimalcomps but it&#8217;s going to let you do lots of cool stuff. In the meantime, I&#8217;ll post a [...]]]></description>
			<content:encoded><![CDATA[<p>Sorry for the dearth of tinytlf posts lately, I&#8217;ve been head-down coding tons of new stuff. If you don&#8217;t believe me, check out the <a href="https://github.com/guyinthechair/tinytlf/commits/master">git repository history</a>.</p>
<p>I&#8217;m working on an official tinytlf demo explorer app, ala <a href="http://minimalcomps.com/">minimalcomps</a> but it&#8217;s going to let you do lots of cool stuff. In the meantime, I&#8217;ll post a few choice demos to this post, kind of show off what I&#8217;ve been working on.</p>
<h2>Mobile TextField</h2>
<p>First up, huge updates to gestures, behaviors, and decorations. So much that I&#8217;ve been able to come up with this demo, primarily meant for Android devices, but still usable on the desktop. Highlight in this TextField:<br />

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_7" width="480`" height="854">
      <param name="movie" value="http://guyinthechair.com/flash/tinytlf/demos/Android.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://guyinthechair.com/flash/tinytlf/demos/Android.swf" width="480`" height="854">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
 </p>
<h2>Better HTML support</h2>
<p>Here&#8217;s some awesome HTML demos that <a href="http://twitter.com/tanya">Tanya Gray</a> has put together&#8230;<br />

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_8" width="600" height="600">
      <param name="movie" value="http://guyinthechair.com/flash/tinytlf/demos/KittehFlow.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://guyinthechair.com/flash/tinytlf/demos/KittehFlow.swf" width="600" height="600">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
<br />

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_9" width="600" height="600">
      <param name="movie" value="http://guyinthechair.com/flash/tinytlf/demos/KittehHistoryFlow.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://guyinthechair.com/flash/tinytlf/demos/KittehHistoryFlow.swf" width="600" height="600">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
 </p>
<p>and&#8230; maybe Divs and Tables?<br />

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_10" width="600" height="600">
      <param name="movie" value="http://guyinthechair.com/flash/tinytlf/demos/TinyTLFDemo.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://guyinthechair.com/flash/tinytlf/demos/TinyTLFDemo.swf" width="600" height="600">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
 </p>
<h2>Advanced Constraint Layouts</h2>
<p>Floats aren&#8217;t just for HTML. Floats take advantage of a more generalized layout algorithm, which flows text around <em>constraints</em>. Floats are just a simple (Rectangle) constraint. You can implement the <em>ITextConstraint</em> interface with your own constraint implementation.</p>
<p>Here&#8217;s a few demos that show off the layout algorithm. Special thanks to <a href="http://algorithmist.wordpress.com/" target="_blank" title="The Algorithmist">Jim Armstrong (the Algorithmist)</a> for the spline math.<br />

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_11" width="600" height="600">
      <param name="movie" value="http://guyinthechair.com/flash/tinytlf/demos/Spline.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://guyinthechair.com/flash/tinytlf/demos/Spline.swf" width="600" height="600">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
<br />

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_12" width="600" height="600">
      <param name="movie" value="http://guyinthechair.com/flash/tinytlf/demos/SplineScrollerDemo.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://guyinthechair.com/flash/tinytlf/demos/SplineScrollerDemo.swf" width="600" height="600">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
 </p>
<h2>Experimental, Just for Fun</h2>
<p>A gmail-style link editing behavior!<br />

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_13" width="600" height="400">
      <param name="movie" value="http://guyinthechair.com/flash/tinytlf/demos/Ticonderoga.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://guyinthechair.com/flash/tinytlf/demos/Ticonderoga.swf" width="600" height="400">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
 </p>
<p>So you can see I haven&#8217;t been idle! It&#8217;s just a lot of work. All the code for these demos is <a href="http://guyinthechair.com/flash/tinytlf/demos/srcview/source/">here</a>. Fork the framework at <a href="https://github.com/guyinthechair/tinytlf">github</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://guyinthechair.com/2010/11/tinytlf-updates-and-new-experiments/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>The FTE Part 4: Advanced Layout Techniques</title>
		<link>http://guyinthechair.com/2010/10/the-fte-part-4-advanced-layout-techniques/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=the-fte-part-4-advanced-layout-techniques</link>
		<comments>http://guyinthechair.com/2010/10/the-fte-part-4-advanced-layout-techniques/#comments</comments>
		<pubDate>Sun, 10 Oct 2010 11:07:51 +0000</pubDate>
		<dc:creator>Paul Taylor</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[misc]]></category>
		<category><![CDATA[Flash Text Engine]]></category>
		<category><![CDATA[FTE]]></category>
		<category><![CDATA[text layout]]></category>

		<guid isPermaLink="false">http://guyinthechair.com/?p=583</guid>
		<description><![CDATA[This is part four in an ongoing series about the Flash Text Engine. You can see the previous entries here. You should know, 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 TLF is built on. In Flash [...]]]></description>
			<content:encoded><![CDATA[<p>This is part four in an ongoing series about the <a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/engine/package-detail.html" title="FTE" target="_blank">Flash Text Engine</a>. You can see the previous entries <a href="http://guyinthechair.com/tag/fte/" target="_blank">here</a>.</p>
<p style="padding-left: 15px; padding-right: 15px; color: #999999;">You should know, 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/FlashPlatform/reference/actionscript/3/flash/text/engine/package-detail.html" target="_blank">flash.text.engine</a> package.</p>
<h2>Advanced Text Rendering</h2>
<p>The FTE may be low level, but that affords developers like you and me serious control over the presentation of our text fields. The FTE renders glyphs into a series of TextLines, but forces us to handle the sizing and layout of the lines. Today, I&#8217;ll cover how to efficiently create and position lines. For simplicity&#8217;s sake, I&#8217;ll assume the TextLines&#8217; widths extend to the edges of the Container they are rendered into.</p>
<p>In this post I&#8217;ll cover:</p>
<ul>
<li>text flow across containers</li>
<li>container resizing</li>
<li>TextLine positioning, invalidation, and reuse</li>
</ul>
<h3>Caveats</h3>
<p>In order to keep this post relatively short, I&#8217;ve simplified the algorithms here down to the basic principles. This is a general description of the layout algorithms in tinytlf, though they are getting more complex by the day.</p>
<h2>Example</h2>
<p>Here&#8217;s the final product of what I&#8217;m going to walk through. <strong>Click on the text to resize the columns</strong>:</p>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_14" width="600" height="600" align="center">
      <param name="movie" value="http://guyinthechair.com/wp-content/uploads/2010/10/FTE_Resizing.swf" />
      <param name="align" value="center" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://guyinthechair.com/wp-content/uploads/2010/10/FTE_Resizing.swf" width="600" height="600" align="center">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<h3>Defining the Problem</h3>
<p>From a layout perspective, we have a number of paragraphs that need to be rendered into a list of DisplayObjectContainers. When we run out of DisplayObjectContainers, we&#8217;re going to call it quits. What if we run out of lines before we&#8217;re done rendering containers? Good! It&#8217;s easy to quit when you don&#8217;t have any content left to render.</p>
<p>So what we need is a layout algorithm that will break TextLines from a Vector of TextBlocks across a list of DisplayObjectContainers (DOCs).</p>
<h3>Text Layout</h3>
<p>In order to coordinate the layout between paragraphs and containers, I&#8217;ve defined a controller I call <a href="http://guyinthechair.com/wp-content/uploads/2010/10/srcview/source/org/tinytlf/layout/TextLayout.as.html" title="Simplified TextLayout">TextLayout</a>. TextLayout&#8217;s job is to run through the list of TextBlocks, rendering as many lines as possible into the available DOCs. When the DOC is full, TextLayout moves to the next container. If there is no next container, TextLayout breaks out of the rendering algorithm. Similarly with TextBlocks, if we run out paragraphs to render, TextLayout breaks out of the layout algorithm.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #3f5fbf;">/**
 * Renders as many lines from the list of TextBlocks into the specified
 * conatiners as possible.
 */</span>
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">render</span><span style="color: #000000;">&#40;</span>blocks<span style="color: #000066; font-weight: bold;">:</span>Vector<span style="color: #000066; font-weight: bold;">.&lt;</span>TextBlock<span style="color: #000066; font-weight: bold;">&gt;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">!</span>containers <span style="color: #000066; font-weight: bold;">||</span> <span style="color: #000066; font-weight: bold;">!</span>containers<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span> <span style="color: #000066; font-weight: bold;">||</span> <span style="color: #000066; font-weight: bold;">!</span>blocks <span style="color: #000066; font-weight: bold;">||</span> <span style="color: #000066; font-weight: bold;">!</span>blocks<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000000;">&#41;</span>
		<span style="color: #0033ff; font-weight: bold;">return</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	containers<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">forEach</span><span style="color: #000000;">&#40;</span><span style="color: #339966; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">c</span><span style="color: #000066; font-weight: bold;">:</span>TextContainer<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000066; font-weight: bold;">...</span>args<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
		<span style="color: #004993;">c</span><span style="color: #000066; font-weight: bold;">.</span>preLayout<span style="color: #000000;">&#40;</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;">&#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 = blocks<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</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> container<span style="color: #000066; font-weight: bold;">:</span>TextContainer = containers<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">while</span> <span style="color: #000000;">&#40;</span>block <span style="color: #000066; font-weight: bold;">&amp;&amp;</span> container<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		container = renderBlockAcrossContainers<span style="color: #000000;">&#40;</span>block<span style="color: #000066; font-weight: bold;">,</span> container<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		block = <span style="color: #000066; font-weight: bold;">++</span>i <span style="color: #000066; font-weight: bold;">&lt;</span> blocks<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span> <span style="color: #000066; font-weight: bold;">?</span> blocks<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h3>TextContainer</h3>
<p><a href="http://guyinthechair.com/wp-content/uploads/2010/10/srcview/source/org/tinytlf/layout/TextContainer.as.html">TextContainer</a> is a single DOC that renders TextLines. It is a layout controller that determines the positions and sizes of the TextLines inside himself.</p>
<p>TextContainer maintains a very important relationship with TextLayout&#8217;s rendering algorithm. TextLayout repeatedly calls the <strong>TextContainer.layout()</strong> method, passing in a TextBlock to render lines from, and optionally, the previous line that was rendered from the block. It is TextContainer&#8217;s responsibility to render as many lines from the TextBlock into itself until either, 1. the TextBlock has no lines left to render, or 2. the TextContainer is full and has no more room for additional TextLines.</p>
<p><strong>TextContainer.layout()</strong> should return the last line rendered from the TextBlock. Null is a valid value to return. If a (non-null) TextLine was returned, TextLayout assumes the TextContainer is full, finished rendering lines, and moves to the next TextContainer, keeping the same TextBlock. If TextContainer returns <strong>null</strong>, TextLayout assumes there&#8217;s still room in the TextContainer for lines, and TextLayout passes the next TextBlock to the TextContainer.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> layout<span style="color: #000000;">&#40;</span>block<span style="color: #000066; font-weight: bold;">:</span>TextBlock<span style="color: #000066; font-weight: bold;">,</span> previousLine<span style="color: #000066; font-weight: bold;">:</span>TextLine<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span>TextLine
<span style="color: #000000;">&#123;</span>
	_y = measuredHeight<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #6699cc; font-weight: bold;">var</span> line<span style="color: #000066; font-weight: bold;">:</span>TextLine = createTextLine<span style="color: #000000;">&#40;</span>block<span style="color: #000066; font-weight: bold;">,</span> previousLine<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<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>
		lines<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>line<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #004993;">position</span><span style="color: #000000;">&#40;</span>line<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #009900; font-style: italic;">//If there's no room, return the last line broken.</span>
		<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>checkConstraints<span style="color: #000000;">&#40;</span>line<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> line<span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		line = createTextLine<span style="color: #000000;">&#40;</span>block<span style="color: #000066; font-weight: bold;">,</span> line<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #009900; font-style: italic;">//This will be null here.</span>
	<span style="color: #0033ff; font-weight: bold;">return</span> line<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Line layout is straightforward. I&#8217;m sure you know what the <strong>position()</strong> and <strong>checkConstraints()</strong> methods do, and if not, the source is available <a href="http://guyinthechair.com/wp-content/uploads/2010/10/srcview/source/org/tinytlf/layout/TextContainer.as.html">here</a>.</p>
<p>This is good enough to work for the first layout pass, but resizing introduces a bit more complexity.</p>
<h2>TextLineValidity</h2>
<p>We can take advantage of an FTE TextBlock and TextLine feature to get resizing.</p>
<p>When the ContentElement &#8220;model&#8221; is updated, the TextLine &#8220;views&#8221; should updated to reflect the changes. But we as FTE users have no concept of the data behind each individual TextLines, so if we updated the screen, we&#8217;d have to start from the first line and re-create each one. This is extremely inefficient, especially if the change only reflected in one actual line update.</p>
<p>Luckily, the TextBlock can track and resolve such changes for you. Whenever anything in the TextBlock&#8217;s <strong>content</strong> member changes, the TextBlock marks relevant lines &#8220;invalid&#8221; (<a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/engine/TextLineValidity.html#INVALID">TextLineValidity.INVALID</a>). The TextBlock exposes a pretty handy property called <strong>firstInvalidLine</strong>, which points to the first line in the TextBlock that needs updating. Look mom, no searching!</p>
<p>You can check the status of individual lines simply by reading the <strong>TextLine.validity</strong> property. Luckily, validity is also writeable, and the TextBlock respects our decision if we choose to mark certain lines &#8220;invalid&#8221; ourselves. I guess we know best!</p>
<h2>Resizing</h2>
<p>When a TextContainer is resized, we need to know about it and invalidate our TextLine children.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> explicitWidth<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #004993;">NaN</span><span style="color: #000066; font-weight: bold;">;</span>
override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> <span style="color: #004993;">width</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">return</span> explicitWidth<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">set</span> <span style="color: #004993;">width</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span> == explicitWidth<span style="color: #000000;">&#41;</span> <span style="color: #0033ff; font-weight: bold;">return</span><span style="color: #000066; font-weight: bold;">;</span>
	explicitWidth = <span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">;</span>
	invalidateLines<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> invalidateLines<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	lines<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">forEach</span><span style="color: #000000;">&#40;</span><span style="color: #339966; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span>l<span style="color: #000066; font-weight: bold;">:</span>TextLine<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000066; font-weight: bold;">...</span>args<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
		l<span style="color: #000066; font-weight: bold;">.</span>validity = TextLineValidity<span style="color: #000066; font-weight: bold;">.</span>INVALID<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</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>Line Reuse</h3>
<p>During the first layout pass, we use the <a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/engine/TextBlock.html#createTextLine()">TextBlock.createTextLine</a> method exclusively. But TextLines are expensive to create, so Flash Player 10.1 introduced the <a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/engine/TextBlock.html#recreateTextLine()">TextBlock.recreateTextLine</a> method. Allowing us to recreate TextLines means that the Flash Player can re-jigger the TextLine&#8217;s contents internally, without the overhead of creating a new TextLine instance.</p>
<p>During a resize operation, lines will be either created or destroyed. If you set the width smaller, the TextField will be forced to render new TextLines. If you set the width wider, the TextField can remove TextLines at the end. In the first case, we&#8217;ll have to make extra calls to <strong>TextBlock.createTextLine</strong>. In the second case, we can remove the TextLines from the display list and remove all references. In this case, the lines have been <strong>orphaned</strong>.</p>
<p>But with the introduction of <strong>recreateTextLine</strong>, it&#8217;s optimal to cache orphaned lines, at least for a little while. It&#8217;s possible that we will resize the TextField smaller again, which will require the creation of new lines. But if we&#8217;ve previously created lines, why not reuse them instead of creating new ones? Good thinking you.</p>
<p>However, this changes the meaning of the <strong>line</strong> argument in the <strong>TextContainer.layout</strong> method. Now, the line can either be:</p>
<ul>
<li>A valid and successfully broken TextLine, which should be used as the <strong>previousLine</strong> argument to create or re-create a TextLine.</li>
<li>An invalid TextLine that needs to be re-created. This should only happen in the case that the line is the <strong>TextBlock.firstLine</strong> value, because there is no previously broken valid TextLine. Since we don&#8217;t want to wipe out our orphan cache looking for a line, we can detect this case and recreate the line.</li>
<ul>
<p>This introduces just a bit more complexity, but luckily we can encapsulate it in the <strong>TextContainer.createTextLine</strong> method.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #3f5fbf;">/**
 * Creates or recreates a given TextLine.
 */</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> createTextLine<span style="color: #000000;">&#40;</span>block<span style="color: #000066; font-weight: bold;">:</span>TextBlock<span style="color: #000066; font-weight: bold;">,</span> line<span style="color: #000066; font-weight: bold;">:</span>TextLine<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span>TextLine
<span style="color: #000000;">&#123;</span>
	removeOrphanedLines<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>line<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>line<span style="color: #000066; font-weight: bold;">.</span>validity === TextLineValidity<span style="color: #000066; font-weight: bold;">.</span>INVALID<span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> block<span style="color: #000066; font-weight: bold;">.</span>recreateTextLine<span style="color: #000000;">&#40;</span>line<span style="color: #000066; font-weight: bold;">,</span> <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: #000000; font-weight:bold;">0.0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>orphans<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> orphan<span style="color: #000066; font-weight: bold;">:</span>TextLine = getFirstOrphan<span style="color: #000000;">&#40;</span>line<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>orphan<span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">return</span> block<span style="color: #000066; font-weight: bold;">.</span>recreateTextLine<span style="color: #000000;">&#40;</span>orphan<span style="color: #000066; font-weight: bold;">,</span> line<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: #000000; font-weight:bold;">0.0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">true</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>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">return</span> 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;">width</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0.0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> removeOrphanedLines<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> line<span style="color: #000066; font-weight: bold;">:</span>TextLine<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> n<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = lines<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">&lt;</span> n<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000066; font-weight: bold;">++</span>i<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		line = lines<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>line<span style="color: #000066; font-weight: bold;">.</span>validity === TextLineValidity<span style="color: #000066; font-weight: bold;">.</span>VALID<span style="color: #000000;">&#41;</span>
			<span style="color: #0033ff; font-weight: bold;">continue</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">contains</span><span style="color: #000000;">&#40;</span>line<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #004993;">removeChild</span><span style="color: #000000;">&#40;</span>line<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		lines<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">splice</span><span style="color: #000000;">&#40;</span>i<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		orphans<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>line<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		n = lines<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">//Static so it's shared between instances of TextContainer</span>
<span style="color: #0033ff; font-weight: bold;">private</span> static const orphans<span style="color: #000066; font-weight: bold;">:</span>Vector<span style="color: #000066; font-weight: bold;">.&lt;</span>TextLine<span style="color: #000066; font-weight: bold;">&gt;</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">&lt;</span>TextLine<span style="color: #000066; font-weight: bold;">&gt;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #3f5fbf;">/**
 * Returns the first invalid orphan that also isn't the input line.
 */</span>
<span style="color: #0033ff; font-weight: bold;">private</span> static <span style="color: #339966; font-weight: bold;">function</span> getFirstOrphan<span style="color: #000000;">&#40;</span>exceptForMe<span style="color: #000066; font-weight: bold;">:</span>TextLine<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span>TextLine
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>orphans<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span> == <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>
		<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #6699cc; font-weight: bold;">var</span> orphan<span style="color: #000066; font-weight: bold;">:</span>TextLine = orphans<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">pop</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: #0033ff; font-weight: bold;">while</span><span style="color: #000000;">&#40;</span>orphan == exceptForMe<span style="color: #000000;">&#41;</span>
		orphan = orphans<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">pop</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: #0033ff; font-weight: bold;">while</span><span style="color: #000000;">&#40;</span>orphan <span style="color: #000066; font-weight: bold;">&amp;&amp;</span> orphan<span style="color: #000066; font-weight: bold;">.</span>validity == TextLineValidity<span style="color: #000066; font-weight: bold;">.</span>VALID<span style="color: #000000;">&#41;</span>
		orphan = orphans<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">pop</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: #0033ff; font-weight: bold;">return</span> orphan<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Because we can possibly recreate TextLines without calling <strong>getFirstOrphan</strong>, sometimes a TextLine in the orphan list is recreated but not removed from the list of possible orphans. The two checks in this method ensure that we don&#8217;t hit this case.</p>
<p>And there you have it! That&#8217;s the basic gist of TextLine rendering, resizing, and reuse across multiple DisplayObjectContainers. You can see all the source for the demo <a href="http://guyinthechair.com/wp-content/uploads/2010/10/srcview/index.html">here</a>, and be sure to check out <a href="http://github.com/guyinthechair/tinytlf">tinytlf</a>, the small text layout framework I&#8217;m writing. I described the basic line rendering algorithm in tinytlf (with enhancements of course). Though I&#8217;m currently working on a more iterative version. If I&#8217;m successful, it&#8217;ll be the topic of a future blog post. Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://guyinthechair.com/2010/10/the-fte-part-4-advanced-layout-techniques/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Tinytlf at 360&#124;Flex DC and 360&#124;MAX</title>
		<link>http://guyinthechair.com/2010/09/tinytlf-at-360flex-dc-and-360max/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=tinytlf-at-360flex-dc-and-360max</link>
		<comments>http://guyinthechair.com/2010/09/tinytlf-at-360flex-dc-and-360max/#comments</comments>
		<pubDate>Sun, 12 Sep 2010 03:54:57 +0000</pubDate>
		<dc:creator>Paul Taylor</dc:creator>
				<category><![CDATA[community]]></category>

		<guid isPermaLink="false">http://guyinthechair.com/?p=574</guid>
		<description><![CDATA[I haven&#8217;t been blogging as often these last two weeks because I&#8217;m spending a lot of time preparing for my 360&#124;Flex and 360&#124;MAX presentations. I&#8217;ve been developing some kickass features and demos that I have yet to discuss publicly, so if you&#8217;re in the mood to be wowed, show up to my presentations. 360&#124;Flex DC [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.360flex.com/" target="_blank" title="360 Flex Conference"><img src="http://guyinthechair.com/wp-content/uploads/2010/09/speakerbadge.png" alt="360|Flex Speaker" title="speakerbadge" width="200" height="211" class="aligncenter size-full wp-image-575" /></a></p>
<p>I haven&#8217;t been blogging as often these last two weeks because I&#8217;m spending a lot of time preparing for my <a href="http://www.360flex.com/" target="_blank" title="360 Flex Conference">360|Flex</a> and <a href="http://www.360flex.com/about/360max/" target="_blank" title="360|MAX Unconference">360|MAX</a> presentations. I&#8217;ve been developing some kickass features and demos that I have yet to discuss publicly, so if you&#8217;re in the mood to be wowed, show up to my presentations.</p>
<h2><a href="http://www.360flex.com/" target="_blank" title="360 Flex Conference">360|Flex DC</a></h2>
<p>My presentation is the first morning, right after the keynote address (go ahead, ask me if I&#8217;m nervous about following <a href="http://dougmccune.com/blog/" target="_blank" title="badass">Doug McCune</a>?). The title is &#8220;<strong>Advanced Text Rendering with the Flash Text Engine and tinytlf</strong>.&#8221; I&#8217;ll present a general overview of the Flash Text Engine and introduce tinytlf. I&#8217;ll try and cover the low level FTE APIs as best I can in the time, but I need to move quickly into tinytlf. I only have about 70 minutes, and I hope to leave 5-10 minutes at the end for questions. I&#8217;ll be announcing tinytlf 1.0, as well as demoing some yet-to-be-announced features. It&#8217;ll be killer, I promise!</p>
<h2><a href="http://www.360flex.com/about/360max/" target="_blank" title="360|MAX Unconference">360|MAX</a></h2>
<p>The title for my 360|MAX presentation is &#8220;<strong>Tinytlf and Multi-Everything</strong>.&#8221; This presentation will begin with an overview of tinytlf, but will primarily focus on the features that enable and promote multi-screen, multi-platform, and multi-touch development. Topics covered will be the performance, interactivity, and configurability for different platforms. If you&#8217;re interested in a multi-platform text controls, this will be presentation to see.</p>
<p>These two conferences are <em>the</em> places to be for developers or companies who have invested in the Flash Platform. You&#8217;ve done your due diligence by choosing Flash, now keep up with the platform &#8217;cause the times, they are a-changin&#8217;. Register for 360|Flex <a href="http://www.eventbrite.com/event/680816340/360flexhelpsell/2222560777">here</a>, and for Adobe MAX <a href="https://max.adobe.com/register/">here</a>. </p>
<p>I&#8217;m very excited to present tinytlf to the world. If you see me at either event, hit me up! I&#8217;m more than happy to answer questions, etc. See you there!</p>
]]></content:encoded>
			<wfw:commentRss>http://guyinthechair.com/2010/09/tinytlf-at-360flex-dc-and-360max/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

