<?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>Easy Flex &#187; Flex 4</title>
	<atom:link href="http://evtimmy.com/tag/flex-4/feed/" rel="self" type="application/rss+xml" />
	<link>http://evtimmy.com</link>
	<description>Evtim on Flex SDK</description>
	<lastBuildDate>Mon, 12 Apr 2010 06:42:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>VerticalAlign for VGroup and HorizontalAlign for HGroup</title>
		<link>http://evtimmy.com/2010/01/verticalalign-for-vgroup-and-horizontalalign-for-hgroup/</link>
		<comments>http://evtimmy.com/2010/01/verticalalign-for-vgroup-and-horizontalalign-for-hgroup/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 02:13:49 +0000</pubDate>
		<dc:creator>Evtim</dc:creator>
				<category><![CDATA[Spark Layouts]]></category>
		<category><![CDATA[alignment]]></category>
		<category><![CDATA[Flex 4]]></category>
		<category><![CDATA[Layout]]></category>
		<category><![CDATA[Scrolling]]></category>
		<category><![CDATA[Spark]]></category>

		<guid isPermaLink="false">http://evtimmy.com/?p=239</guid>
		<description><![CDATA[ Recently I checked in the major axis alignment update to VGroup, HGroup, VerticalLayout, HorizontalLayout. It brings parity to the Flex 4 Spark layouts relative to the MX containers HBox and VBox that are well known from previous versions of Flex (and have had those alignment options for long time now). 
Now VGroup and VerticalLayout [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://evtimmy.com/wordpress/wp-content/uploads/2010/01/majoraxisalignment.swf"><img class="alignleft size-thumbnail wp-image-240" title="majoraxisalignment" src="http://evtimmy.com/wordpress/wp-content/uploads/2010/01/majoraxisalignment-150x150.png" alt="majoraxisalignment" width="150" height="150" /></a> Recently I checked in the major axis alignment update to <em>VGroup</em>, <em>HGroup</em>, <em>VerticalLayout</em>, <em>HorizontalLayout</em>. It brings parity to the Flex 4 Spark layouts relative to the MX containers <em>HBox</em> and <em>VBox </em>that are well known from previous versions of Flex (and have had those alignment options for long time now). <span id="more-239"></span></p>
<p>Now <em>VGroup </em>and <em>VerticalLayout </em>have a &#8220;<em>verticalAlign</em>&#8221; property with the three supported values of &#8220;<em>top</em>&#8220;, &#8220;<em>middle</em>&#8220;, &#8220;<em>bottom</em>&#8220;.  Accordingly <em>HGroup </em>and <em>HorizontalLayout </em>get a &#8220;<em>horizontalAlign</em>&#8221; property with the supported values of &#8220;<em>left</em>&#8220;, &#8220;<em>center</em>&#8221; and &#8220;<em>right</em>&#8220;.  The alignment modes work for the layouts with and without virtualization enabled.<br />
An important difference to note is that when &#8220;<em>clipAndEnableScrolling</em>&#8221; is set to &#8220;<em>true</em>&#8221; and the containers are scrolling (the content is bigger than the container), the Spark layouts ensure that the entire content can be scrolled in view by internally reverting to the &#8220;<em>top</em>&#8220;/&#8221;<em>left</em>&#8221; alignment respectively.</p>
<p>When playing with the attached sample, you&#8217;ll notice difference between the clipping and the non-clipping containers when scrolling is in effect (drag the slider left to force smaller height for the containers).</p>
<p>The sample was compiled with SDK version 4.0.0.13553, source: <a href="http://evtimmy.com/wordpress/wp-content/uploads/2010/01/majoraxisalignment.mxml">MajorAxisAlignment.mxml</a></p>
]]></content:encoded>
			<wfw:commentRss>http://evtimmy.com/2010/01/verticalalign-for-vgroup-and-horizontalalign-for-hgroup/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>FlowLayout Part 2 &#8211; Gap, VerticalAlign and Scrolling</title>
		<link>http://evtimmy.com/2009/06/flowlayout-part-2-gap-verticalalign-and-scrolling/</link>
		<comments>http://evtimmy.com/2009/06/flowlayout-part-2-gap-verticalalign-and-scrolling/#comments</comments>
		<pubDate>Thu, 11 Jun 2009 06:53:39 +0000</pubDate>
		<dc:creator>Evtim</dc:creator>
				<category><![CDATA[Custom Layout]]></category>
		<category><![CDATA[Flex 4]]></category>
		<category><![CDATA[Layout]]></category>
		<category><![CDATA[Scrolling]]></category>
		<category><![CDATA[Spark]]></category>

		<guid isPermaLink="false">http://evtimmy.com/?p=80</guid>
		<description><![CDATA[In part one here, the FlowLayout had really basic functionality.  I decided to demonstrate how to add layout properties to the custom Spark layouts and how to enable scrolling.]]></description>
			<content:encoded><![CDATA[<p>In part one <a href="http://evtimmy.com/?p=3">here</a>, the FlowLayout had really basic functionality.  I decided to demonstrate how to add layout properties to the custom Spark layouts and how to enable scrolling.</p>
<p><span id="more-80"></span>Let me show off the end result first:</p>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_0" width="400" height="300" align="center">
      <param name="movie" value="http://evtimmy.com/wordpress/wp-content/uploads/2009/06/flowtest-props-and-scrolling2.swf" />
      <param name="align" value="center" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://evtimmy.com/wordpress/wp-content/uploads/2009/06/flowtest-props-and-scrolling2.swf" width="400" height="300" align="center">
      <!--<![endif]-->
        <p>The Flash plugin is required to view this object.</p>
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p>And the source code is available <a href="http://evtimmy.com/wordpress/wp-content/uploads/2009/10/flowtestpropsandscrolling2.zip">here</a>.</p>
<p><em>Update: The source has been updated to compile with Flash Builder / Flex SDK beta 2. I also added a horizontalAlign property, just because I can =)</em></p>
<h3>Adding a Layout Property</h3>
<p>I want to implement two properties &#8211; a horizontalGap for between the elements within a row, and verticalAlign which will control how the elements within the row are aligned. For the latter, I&#8217;ll have &#8220;top&#8221;, &#8220;middle&#8221; and &#8220;bottom&#8221; as options, and of course I&#8217;ll have to beef up the FlowLayout to support elements with various heights.</p>
<p>I&#8217;ll add the properties directly to the FlowLayout class. When the value changes, I need to invalidate the container which will cause the layout to run its updateDisplayList() and at that point I&#8217;ll take into account the new values:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;">    <span style="color: #009900; font-style: italic;">//---------------------------------------------------------------</span>
    <span style="color: #009900; font-style: italic;">//  horizontalGap</span>
    <span style="color: #009900; font-style: italic;">//---------------------------------------------------------------</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _horizontalGap<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">10</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> <span style="color: #0033ff; font-weight: bold;">set</span> horizontalGap<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>
        _horizontalGap = <span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
        <span style="color: #009900; font-style: italic;">// We must invalidate the layout</span>
        <span style="color: #6699cc; font-weight: bold;">var</span> layoutTarget<span style="color: #000066; font-weight: bold;">:</span>GroupBase = <span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>layoutTarget<span style="color: #000000;">&#41;</span>
            layoutTarget<span style="color: #000066; font-weight: bold;">.</span>invalidateDisplayList<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: #009900; font-style: italic;">//---------------------------------------------------------------</span>
    <span style="color: #009900; font-style: italic;">//  verticalAlign</span>
    <span style="color: #009900; font-style: italic;">//---------------------------------------------------------------</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _verticalAlign<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;bottom&quot;</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> <span style="color: #0033ff; font-weight: bold;">set</span> verticalAlign<span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</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>
        _verticalAlign = <span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
        <span style="color: #009900; font-style: italic;">// We must invalidate the layout</span>
        <span style="color: #6699cc; font-weight: bold;">var</span> layoutTarget<span style="color: #000066; font-weight: bold;">:</span>GroupBase = <span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>layoutTarget<span style="color: #000000;">&#41;</span>
            layoutTarget<span style="color: #000066; font-weight: bold;">.</span>invalidateDisplayList<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></pre></td></tr></table></div>

<p>Then I will add a drop-down and a slider in the flowTest.mxml to configure the the properties like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
</pre></td><td class="code"><pre class="mxml" style="font-family:monospace;">    <span style="color: #000000;"><span style="color: #808080; font-style: italic;">&lt;!-- The slider to control the horizontal gap --&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:HSlider</span> id=<span style="color: #ff0000;">&quot;hGapSlider&quot;</span> minimum=<span style="color: #ff0000;">&quot;0&quot;</span> maximum=<span style="color: #ff0000;">&quot;50&quot;</span></span>
<span style="color: #000000;">                         value=<span style="color: #ff0000;">&quot;10&quot;</span> liveDragging=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #808080; font-style: italic;">&lt;!-- The drop-down to select vertical alignment --&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:DropDownList</span> id=<span style="color: #ff0000;">&quot;vAlign&quot;</span> requiresSelection=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:ArrayCollection</span><span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:String</span><span style="color: #7400FF;">&gt;</span></span>bottom<span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:String</span><span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:String</span><span style="color: #7400FF;">&gt;</span></span>middle<span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:String</span><span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:String</span><span style="color: #7400FF;">&gt;</span></span>top<span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:String</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:ArrayCollection</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:DropDownList</span><span style="color: #7400FF;">&gt;</span></span>                         
&nbsp;
    <span style="color: #000000;"><span style="color: #808080; font-style: italic;">&lt;!-- A Spark List --&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:List</span> id=<span style="color: #ff0000;">&quot;list1&quot;</span> width=<span style="color: #ff0000;">&quot;{widthSlider.value}&quot;</span></span>
<span style="color: #000000;">            selectedIndex=<span style="color: #ff0000;">&quot;7&quot;</span></span>
<span style="color: #000000;">            dataProvider=<span style="color: #ff0000;">&quot;{new ArrayCollection(</span>
<span style="color: #000000;">            'The quick fox jumped over the lazy sleepy\n\dog'.split(' '))}&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
        <span style="color: #000000;"><span style="color: #808080; font-style: italic;">&lt;!-- Configure the layout to be the FlowLayout --&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:layout</span><span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;my:FlowLayout</span><span style="color: #cc66cc;">1</span> verticalAlign=<span style="color: #ff0000;">&quot;{vAlign.selectedItem}&quot;</span></span>
<span style="color: #000000;">                            horizontalGap=<span style="color: #ff0000;">&quot;{hGapSlider.value}&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:layout</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:List</span><span style="color: #7400FF;">&gt;</span></span></pre></td></tr></table></div>

<p>Then the harder part &#8211; actually implementing the support for these properties in the FlowLayout&#8217;s updateDisplayList() method.  Since I&#8217;ll need to know the row height before I can decide how to position the elements, I&#8217;ll have to replace my original loop with a little more complicated code, something along the lines of:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;">        <span style="color: #009900; font-style: italic;">// loop through the elements</span>
        <span style="color: #009900; font-style: italic;">// while we can start a new row</span>
        <span style="color: #6699cc; font-weight: bold;">var</span> rowStart<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: #0033ff; font-weight: bold;">while</span> <span style="color: #000000;">&#40;</span>rowStart <span style="color: #000066; font-weight: bold;">&lt;</span> count<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #009900; font-style: italic;">// The row always contains the start element</span>
            element = layoutTarget<span style="color: #000066; font-weight: bold;">.</span>getElementAt<span style="color: #000000;">&#40;</span>rowStart<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #6699cc; font-weight: bold;">var</span> rowWidth<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = element<span style="color: #000066; font-weight: bold;">.</span>getPreferredBoundsWidth<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #6699cc; font-weight: bold;">var</span> rowHeight<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> =  element<span style="color: #000066; font-weight: bold;">.</span>getPreferredBoundsHeight<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;">// Find the end of the current row</span>
            <span style="color: #6699cc; font-weight: bold;">var</span> rowEnd<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = rowStart<span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #0033ff; font-weight: bold;">while</span> <span style="color: #000000;">&#40;</span>rowEnd <span style="color: #000066; font-weight: bold;">+</span> 1 <span style="color: #000066; font-weight: bold;">&lt;</span> count<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                element = layoutTarget<span style="color: #000066; font-weight: bold;">.</span>getElementAt<span style="color: #000000;">&#40;</span>rowEnd <span style="color: #000066; font-weight: bold;">+</span> 1<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
                <span style="color: #009900; font-style: italic;">// Since we haven't resized the element just yet, get its preferred size</span>
                elementWidth = element<span style="color: #000066; font-weight: bold;">.</span>getPreferredBoundsWidth<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
                elementHeight = element<span style="color: #000066; font-weight: bold;">.</span>getPreferredBoundsHeight<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;">// Can we add one more element to this row?</span>
                <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>rowWidth <span style="color: #000066; font-weight: bold;">+</span> hGap <span style="color: #000066; font-weight: bold;">+</span> elementWidth <span style="color: #000066; font-weight: bold;">&gt;</span> containerWidth<span style="color: #000000;">&#41;</span>
                    <span style="color: #0033ff; font-weight: bold;">break</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
                rowWidth <span style="color: #000066; font-weight: bold;">+</span>= hGap <span style="color: #000066; font-weight: bold;">+</span> elementWidth<span style="color: #000066; font-weight: bold;">;</span>
                rowHeight = <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">max</span><span style="color: #000000;">&#40;</span>rowHeight<span style="color: #000066; font-weight: bold;">,</span> elementHeight<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
                rowEnd<span style="color: #000066; font-weight: bold;">++;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #009900; font-style: italic;">// Layout all the elements within the row</span>
            <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> = rowStart<span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">&lt;</span>= rowEnd<span style="color: #000066; font-weight: bold;">;</span> i<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                element = layoutTarget<span style="color: #000066; font-weight: bold;">.</span>getElementAt<span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
                <span style="color: #000066; font-weight: bold;">...</span>
                <span style="color: #009900; font-style: italic;">// Position the element</span>
                element<span style="color: #000066; font-weight: bold;">.</span>setLayoutBoundsPosition<span style="color: #000000;">&#40;</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span> elementY<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;">// Next row will start with the first element after the current row's end</span>
            rowStart = rowEnd <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
            <span style="color: #009900; font-style: italic;">// Update the position to the beginning of the row</span>
            <span style="color: #000066; font-weight: bold;">...</span>
        <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<h3>Enabling Scrolling</h3>
<p>Now I&#8217;m almost done, but I want the FlowLayout to be scrollable. In theory this would mean that I need do the following steps:</p>
<ol>
<li> Add scroll-bars.</li>
<li>Hook up the scroll-bars with the layout&#8217;s horizontalScrollPosition and verticalScrollPosition properties.</li>
<li>Calculate the ranges for the scroll-bars and keep them in sync whenever the values change.</li>
</ol>
<p>In practice, I can save a lot of work by using a ready stock Spark component &#8211; the Scroller. Without going into too much details, the Scroller can wrap a container and automatically put up scroll-bars, keeping everything in sync with the container&#8217;s layout.  And since in my flowTest.mxml I&#8217;m using a List, the Scroller is already in there!!! It&#8217;s a part of the List default skin <img src='http://evtimmy.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />   Now all that&#8217;s left to do for me is actually calculate the ranges for the scroll bars. This is basically the total width and height of the scrollable area of the container a.k.a. the &#8220;content size&#8221;.</p>
<p>Calculating the content size is easy enough &#8211; I&#8217;ll just find the maximum extents of all the children while I&#8217;m resizing and arranging them in the FlowLayout&#8217;s updateDisplayList(). At the very end of that method I&#8217;ll set the updated content size like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>144
145
146
147
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;">        <span style="color: #009900; font-style: italic;">// Set the content size which determines the scrolling limits</span>
        <span style="color: #009900; font-style: italic;">// and is used by the Scroller to calculate whether to show up</span>
        <span style="color: #009900; font-style: italic;">// the scrollbars when the the scroll policy is set to &quot;auto&quot;</span>
        layoutTarget<span style="color: #000066; font-weight: bold;">.</span>setContentSize<span style="color: #000000;">&#40;</span>maxRowWidth<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></td></tr></table></div>

<p>That&#8217;s it!</p>
]]></content:encoded>
			<wfw:commentRss>http://evtimmy.com/2009/06/flowlayout-part-2-gap-verticalalign-and-scrolling/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>FlowLayout &#8211; a Spark Custom Layout Example</title>
		<link>http://evtimmy.com/2009/06/flowlayout-a-spark-custom-layout-example/</link>
		<comments>http://evtimmy.com/2009/06/flowlayout-a-spark-custom-layout-example/#comments</comments>
		<pubDate>Mon, 01 Jun 2009 08:10:49 +0000</pubDate>
		<dc:creator>Evtim</dc:creator>
				<category><![CDATA[Custom Layout]]></category>
		<category><![CDATA[Flex 4]]></category>
		<category><![CDATA[Layout]]></category>

		<guid isPermaLink="false">http://evtimmy.com/wordpress/?p=3</guid>
		<description><![CDATA[I&#8217;ve been working on the Flex SDK team for more than a year now, primarily playing with the Spark layouts.  One of the new Spark features is the &#8220;assignable layouts&#8221;.  The stock layouts have been separated into their own classes and the containers can be assigned different layouts at run-time.  This modularization allows anyone to [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on the Flex SDK team for more than a year now, primarily playing with the Spark layouts.  One of the new Spark features is the &#8220;assignable layouts&#8221;.  The stock layouts have been separated into their own classes and the containers can be assigned different layouts at run-time.  This modularization allows anyone to create their own cool custom layouts and use them with any of the Spark containers.</p>
<p><span id="more-3"></span></p>
<p>I want to show how easy it is to create a custom Spark layout.  I&#8217;ve chosen a flow layout example where all the elements are arranged horizontally, wrapping to the next row when the width limit of the container is reached.  For simplicity I&#8217;m going to assume that all elements are of equal height.</p>
<p><strong><em><strong><em>Update: Check out part 2 of the article <a title="here" href="http://evtimmy.com/2009/06/flowlayout-part-2-gap-verticalalign-and-scrolling/">here</a>.</em></strong></em></strong></p>
<h3>Step 1 &#8211; Simple List Application with a Stock TileLayout</h3>
<p>For starter, let&#8217;s get a simple application going with a Spark List with a few items. I&#8217;m going to add a slider that controls the width of the List. By default the List&#8217;s layout is the VerticalLayout, but I&#8217;m going to switch it to the stock TileLayout just to contrast it with the FlowLayout that I&#8217;m going to put together further below.</p>
<p style="text-align: center;">
    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_1" width="400" height="300" align="center">
      <param name="movie" value="http://evtimmy.com/wordpress/wp-content/uploads/2009/06/simple-tile.swf" />
      <param name="align" value="center" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://evtimmy.com/wordpress/wp-content/uploads/2009/06/simple-tile.swf" width="400" height="300" align="center">
      <!--<![endif]-->
        <p>The Flash plugin is required to view this object.</p>
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
</p>
<p>And the source code:</p>
<div id="attachment_16" class="wp-caption aligncenter" style="width: 610px"><img class="size-full wp-image-16" title="a-simple-flow-layout-step11" src="http://evtimmy.com/wordpress/wp-content/uploads/2009/05/a-simple-flow-layout-step11.png" alt="List with TileLayout" width="600" height="550" /><p class="wp-caption-text">FlowTest.mxml</p></div>
<h3>Step 2 &#8211; A Custom Layout Skeleton Class</h3>
<p>Next step is creating the skeleton for the custom layout. I&#8217;ll create a new AS class FlowLayout.as, and since it is a custom layout, it must subclass LayoutBase.  The LayoutBase provides a lot of default functionality, but the most important methods I need to override are updateDisplayList() and measure().</p>
<ul>
<li>updateDisplayList() is called by the container to size and arrange the container&#8217;s elements.</li>
<li>measure() is called to calculate the default size of the container based on the container&#8217;s elements. I&#8217;m not going to override measure(), as the List skin already has default size.</li>
</ul>
<div id="attachment_22" class="wp-caption aligncenter" style="width: 610px"><img class="size-full wp-image-22" title="a-simple-flow-layout-step2" src="http://evtimmy.com/wordpress/wp-content/uploads/2009/06/a-simple-flow-layout-step2.png" alt="FlowLayout.as" width="600" height="240" /><p class="wp-caption-text">FlowLayout.as</p></div>
<h3>Step 3 &#8211; Make Sure It Does Something</h3>
<p>Now I&#8217;m going to iterate through all of the container&#8217;s elements.  I&#8217;ll access the container through the layout&#8217;s &#8220;target&#8221; property, which is of type GroupBase.  To work with the elements I&#8217;ll be using the ILayoutElement interface, specifically the following APIs:</p>
<ul>
<li>setLayoutBoundsSize(width, height) &#8211; where I can also pass in NaN leaving the element to size itself to its preferred size.</li>
<li>getLayoutBoundsWidth(), getLayoutBoundsHeight() &#8211; to get the current bounds of the element.</li>
<li>setLayoutBoundsPosition(x, y) &#8211; to position the element.  Coordinates are relative to the container&#8217;s origin.</li>
</ul>
<p>Before delving into too much coding, let&#8217;s just size every element to it&#8217;s preferred size, stagger them vertically by 10 pixels each and setup the List to use the the new FlowLayout.</p>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_2" width="400" height="300" align="center">
      <param name="movie" value="http://evtimmy.com/wordpress/wp-content/uploads/2009/06/flowtest-flow-layout-basic.swf" />
      <param name="align" value="center" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://evtimmy.com/wordpress/wp-content/uploads/2009/06/flowtest-flow-layout-basic.swf" width="400" height="300" align="center">
      <!--<![endif]-->
        <p>The Flash plugin is required to view this object.</p>
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p>And the source code:</p>
<div id="attachment_24" class="wp-caption aligncenter" style="width: 610px"><img class="size-full wp-image-24" title="a-simple-flow-layout-step3" src="http://evtimmy.com/wordpress/wp-content/uploads/2009/06/a-simple-flow-layout-step3.png" alt="a-simple-flow-layout-step3" width="600" height="530" /><p class="wp-caption-text">FlowLayout.as</p></div>
<p style="text-align: center;">
<div id="attachment_25" class="wp-caption aligncenter" style="width: 610px"><img class="size-full wp-image-25" title="a-simple-flow-layout-step31" src="http://evtimmy.com/wordpress/wp-content/uploads/2009/06/a-simple-flow-layout-step31.png" alt="The update to the flowText.mxml" width="600" height="170" /><p class="wp-caption-text">The update to the flowText.mxml</p></div>
<h3>Final Step &#8211; Finish the FlowLayout</h3>
<p>And now let&#8217;s finish the FlowLayout &#8211; I want to arrange elements left to right, space them by 10 pixels, and move the elements down when there&#8217;s not enough space to fit in the containerWidth.  With a couple of lines I get the final result:</p>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_3" width="400" height="300" align="center">
      <param name="movie" value="http://evtimmy.com/wordpress/wp-content/uploads/2009/06/flowtest-final.swf" />
      <param name="align" value="center" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://evtimmy.com/wordpress/wp-content/uploads/2009/06/flowtest-final.swf" width="400" height="300" align="center">
      <!--<![endif]-->
        <p>The Flash plugin is required to view this object.</p>
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p>And the final code looks like this:</p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<div id="attachment_29" class="wp-caption aligncenter" style="width: 610px"><img class="size-full wp-image-29" title="a-simple-flow-layout-step4" src="http://evtimmy.com/wordpress/wp-content/uploads/2009/06/a-simple-flow-layout-step4.png" alt="Final source code of the FlowLayout.as" width="600" height="810" /><p class="wp-caption-text">Final source code of the FlowLayout.as</p></div>
<p>Hope you enjoyed it!</p>
<p>Download the source files <a href="http://evtimmy.com/wordpress/wp-content/uploads/2009/06/flowlayout.zip">here.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://evtimmy.com/2009/06/flowlayout-a-spark-custom-layout-example/feed/</wfw:commentRss>
		<slash:comments>35</slash:comments>
		</item>
	</channel>
</rss>
