<?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>Flexfactor.it</title>
	<atom:link href="http://www.flexfactor.it/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.flexfactor.it</link>
	<description>because developing is a dna matter</description>
	<lastBuildDate>Wed, 15 Apr 2009 12:00:22 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Drag and Drop of rotated objects</title>
		<link>http://www.flexfactor.it/?p=9</link>
		<comments>http://www.flexfactor.it/?p=9#comments</comments>
		<pubDate>Mon, 06 Apr 2009 21:53:36 +0000</pubDate>
		<dc:creator>Emanuele Spinella</dc:creator>
				<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://www.flexfactor.it/?p=9</guid>
		<description><![CDATA[Adding drag and drop capabilities to a display object is quite simple: you have to listen for a MOUSE_DOWN event on that object and call the startDrag method.
&#60; mx:Box id=&#34;cnt&#34; width=&#34;400&#34; height=&#34;400&#34; x=&#34;200&#34; y=&#34;100&#34;
        backgroundColor=&#34;#555555&#34;&#62;
	&#60; mx:Canvas x=&#34;65&#34; y=&#34;37&#34; width=&#34;194&#34; height=&#34;94&#34;
		backgroundColor=&#34;#EF1414&#34; id=&#34;img&#34;
        [...]]]></description>
			<content:encoded><![CDATA[<p>Adding drag and drop capabilities to a display object is quite simple: you have to listen for a MOUSE_DOWN event on that object and call the startDrag method.</p>
<pre class="actionscript">&lt; mx:Box id=<span style="color: #ff0000;">&quot;cnt&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;400&quot;</span> <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;400&quot;</span> x=<span style="color: #ff0000;">&quot;200&quot;</span> y=<span style="color: #ff0000;">&quot;100&quot;</span>
        <span style="color: #0066CC;">backgroundColor</span>=<span style="color: #ff0000;">&quot;#555555&quot;</span>&gt;
	&lt; mx:Canvas x=<span style="color: #ff0000;">&quot;65&quot;</span> y=<span style="color: #ff0000;">&quot;37&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;194&quot;</span> <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;94&quot;</span>
		<span style="color: #0066CC;">backgroundColor</span>=<span style="color: #ff0000;">&quot;#EF1414&quot;</span> id=<span style="color: #ff0000;">&quot;img&quot;</span>
                mouseDown=<span style="color: #ff0000;">&quot;startMove(event)&quot;</span>
		mouseUp=<span style="color: #ff0000;">&quot;stopMove(event)&quot;</span>&gt;
	&lt; /mx:Canvas&gt;
&lt; /mx:Box&gt;</pre>
<p>In the example above, there is a Box that acts as a container for a Canvas. The mouseDown event is then listened by the startMove method.</p>
<pre class="actionscript"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> startMove<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
       img.<span style="color: #0066CC;">startDrag</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p>The startDrag method without parameters lets you drag the object wherever you want.<br />
To release the object, listen for the MOUSE_UP event and call the stopDrag method. The stopMove listener do this.</p>
<pre class="actionscript"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> stopMove<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
      img.<span style="color: #0066CC;">stopDrag</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p>If you want to drag only within a certain area, for example the cnt box, you need to define a bounding rectangle as a parameter of the startDrag method.<br />
Let's modify the startMove method:</p>
<pre class="actionscript"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> startMove<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// Bounding rectangle</span>
	<span style="color: #000000; font-weight: bold;">var</span> boundingRect:Rectangle = <span style="color: #000000; font-weight: bold;">new</span> Rectangle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">// Starting point in the container coordinates system</span>
    	boundingRect.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">0</span>;
	boundingRect.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">0</span>;
&nbsp;
    	<span style="color: #808080; font-style: italic;">// Allowed horizontal and vertical displacemnt</span>
	boundingRect.<span style="color: #0066CC;">width</span> = cnt.<span style="color: #0066CC;">width</span> - img.<span style="color: #0066CC;">width</span>;
	boundingRect.<span style="color: #0066CC;">height</span> = cnt.<span style="color: #0066CC;">height</span> - img.<span style="color: #0066CC;">height</span>;
&nbsp;
    	<span style="color: #808080; font-style: italic;">// Start drag</span>
	img.<span style="color: #0066CC;">startDrag</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span>,boundingRect<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p>It's simple, isn't it?<br />
Now suppose to rotate the img object, specifying a rotation parameter.</p>
<pre class="actionscript">&lt; mx:Box id=<span style="color: #ff0000;">&quot;cnt&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;400&quot;</span> <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;400&quot;</span> x=<span style="color: #ff0000;">&quot;200&quot;</span> y=<span style="color: #ff0000;">&quot;100&quot;</span>
	<span style="color: #0066CC;">backgroundColor</span>=<span style="color: #ff0000;">&quot;#555555&quot;</span>&gt;
	&lt; mx:Canvas x=<span style="color: #ff0000;">&quot;65&quot;</span> y=<span style="color: #ff0000;">&quot;37&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;194&quot;</span> <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;94&quot;</span>
		<span style="color: #0066CC;">backgroundColor</span>=<span style="color: #ff0000;">&quot;#EF1414&quot;</span> id=<span style="color: #ff0000;">&quot;img&quot;</span>
		rotation=<span style="color: #ff0000;">&quot;0&quot;</span>
		mouseDown=<span style="color: #ff0000;">&quot;startMove(event)&quot;</span>
		mouseUp=<span style="color: #ff0000;">&quot;stopMove(event)&quot;</span>&gt;
	&lt; /mx:Canvas&gt;
&lt; /mx:Box&gt;</pre>
<p>It seems that something doesn't work!<br />
The rotation is only a visual effect, but for calculations about the bounds, the object is considered as unrotated!<br />
Click <a href="http://www.flexfactor.it/samples/Drag and Drop of rotated objects/badBounds/main.html" target="_blank">here</a> to see the result (the rotation is set to 45 degrees).</p>
<p>Unfortunately we need to keep in consideration the specified rotation while setting up the bounding rectangle.<br />
We can use the getBounds method to obtain the rectangle whose sides are tangent to the img vertices.</p>
<pre class="actionscript"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> startMove<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> rect:Rectangle = img.<span style="color: #0066CC;">getBounds</span><span style="color: #66cc66;">&#40;</span>cnt<span style="color: #66cc66;">&#41;</span>;
        ...
<span style="color: #66cc66;">&#125;</span></pre>
<p>Basing on that rectangle we can calculate the right starting point, vertical and horizontal displacements for the draggable area.</p>
<pre class="actionscript"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> startMove<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> rect:Rectangle = img.<span style="color: #0066CC;">getBounds</span><span style="color: #66cc66;">&#40;</span>cnt<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// Bounding rectangle</span>
		<span style="color: #000000; font-weight: bold;">var</span> boundingRect:Rectangle = <span style="color: #000000; font-weight: bold;">new</span> Rectangle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// Starting point in the container coordinates system</span>
		boundingRect.<span style="color: #006600;">x</span> = <span style="color: #66cc66;">&#40;</span>img.<span style="color: #006600;">x</span>-rect.<span style="color: #006600;">x</span><span style="color: #66cc66;">&#41;</span>;
		boundingRect.<span style="color: #006600;">y</span> = <span style="color: #66cc66;">&#40;</span>img.<span style="color: #006600;">y</span>-rect.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span>;
		boundingRect.<span style="color: #0066CC;">width</span> = <span style="color: #66cc66;">&#40;</span>cnt.<span style="color: #006600;">width</span>-rect.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span>;
		boundingRect.<span style="color: #0066CC;">height</span> = <span style="color: #66cc66;">&#40;</span>cnt.<span style="color: #006600;">height</span>-rect.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// Start drag</span>
	        img.<span style="color: #0066CC;">startDrag</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span>,boundingRect<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p>Ok! It works!<br />
If the rotation is 0 (zero) boundingRect.x and boundingRect.y are both 0 (zero), while rect.width and rect.height coincide with img.width and img.height. Now this is only a particular case!</p>
<p>Click <a href="http://www.flexfactor.it/samples/Drag and Drop of rotated objects/finalSample/main.html" target="_blank">here</a> for the complete example.</p>
<p><a href="http://www.actionscript.it/showPagedContent.cfm?id=1688">Italian version</a> on Actionscript.it</p>
]]></content:encoded>
			<wfw:commentRss>http://www.flexfactor.it/?feed=rss2&amp;p=9</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My first post</title>
		<link>http://www.flexfactor.it/?p=3</link>
		<comments>http://www.flexfactor.it/?p=3#comments</comments>
		<pubDate>Wed, 25 Feb 2009 23:54:09 +0000</pubDate>
		<dc:creator>Emanuele Spinella</dc:creator>
				<category><![CDATA[Miscellanea]]></category>

		<guid isPermaLink="false">http://www.flexfactor.it/?p=3</guid>
		<description><![CDATA[Hello Everybody,
this is my first post, I hope that this blog could be useful for all the Flex/Flash developers.
]]></description>
			<content:encoded><![CDATA[<p>Hello Everybody,<br />
this is my first post, I hope that this blog could be useful for all the Flex/Flash developers.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.flexfactor.it/?feed=rss2&amp;p=3</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
