<?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>Ad hoc Geek &#187; draggable</title>
	<atom:link href="http://www.adhocgeek.com/tag/draggable/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.adhocgeek.com</link>
	<description>Approaching geekery in an ad hoc and improvisational manner.</description>
	<lastBuildDate>Fri, 30 Sep 2011 09:43:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>A simple Flex drag handle.</title>
		<link>http://www.adhocgeek.com/2009/06/a-simple-flex-drag-handle/</link>
		<comments>http://www.adhocgeek.com/2009/06/a-simple-flex-drag-handle/#comments</comments>
		<pubDate>Sat, 27 Jun 2009 19:18:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Geek]]></category>
		<category><![CDATA[draggable]]></category>
		<category><![CDATA[flex]]></category>

		<guid isPermaLink="false">http://www.adhocgeek.com/?p=73</guid>
		<description><![CDATA[I had a need for the user to be able to manually drag and re-organise some windows on the screen. This is a fairly common problem I think, but I couldn&#8217;t find any simple examples that didn&#8217;t involve messing around with the DragManager. I don&#8217;t really need that kind of flexibility, I&#8217;m quite happy for [...]]]></description>
			<content:encoded><![CDATA[<p>I had a need for the user to be able to manually drag and re-organise some windows on the screen. This is a fairly common problem I think, but I couldn&#8217;t find any simple examples that didn&#8217;t involve messing around with the DragManager. I don&#8217;t really need that kind of flexibility, I&#8217;m quite happy for the draggable items to be constrained within a single container.</p>
<p>Each of my draggable components is going to be a container itself, so I want a UIComponent I can add to the display list which will just automagically deal with moving the parent around when we click and drag. i.e. in MXML I want to write something like this :</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;Application </span>
<span style="color: #000000;">    xmlns=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> </span>
<span style="color: #000000;">    xmlns:local=<span style="color: #ff0000;">&quot;*&quot;</span></span>
<span style="color: #000000;">    layout=<span style="color: #ff0000;">&quot;absolute&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;">&lt;VBox<span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;local:DragHandle</span> height=<span style="color: #ff0000;">&quot;20&quot;</span> width=<span style="color: #ff0000;">&quot;100%&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #808080; font-style: italic;">&lt;!-- More components go here --&gt;</span></span>
    <span style="color: #000000;">&lt;/VBox<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;">&lt;/Application<span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>The intent being that this should enable me to drag that VBox anywhere within the main application window without adding any further code to either the VBox or the application.</p>
<p>First things first, I need to inherit from UIComponent and add a simple sprite to act as the visible handle :</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Graphics</span>;
    <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
    <span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">core</span>.<span style="color: #006600;">UIComponent</span>;
    <span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">events</span>.<span style="color: #006600;">ResizeEvent</span>;
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DragHandle <span style="color: #0066CC;">extends</span> UIComponent
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _rect:Sprite;
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> DragHandle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            _rect = <span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            addChild<span style="color: #66cc66;">&#40;</span>_rect<span style="color: #66cc66;">&#41;</span>;
&nbsp;
            addEventListener<span style="color: #66cc66;">&#40;</span>ResizeEvent.<span style="color: #006600;">RESIZE</span>, draw<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _fillColor:<span style="color: #0066CC;">Number</span> = 0xcccccc;
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> fillColor<span style="color: #66cc66;">&#40;</span>value:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
        <span style="color: #66cc66;">&#123;</span>
            _fillColor = value;
            invalidateProperties<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        protected override <span style="color: #000000; font-weight: bold;">function</span> commitProperties<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">super</span>.<span style="color: #006600;">commitProperties</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
            draw<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> draw<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:ResizeEvent<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> g:Graphics = _rect.<span style="color: #006600;">graphics</span>;
            g.<span style="color: #0066CC;">clear</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            g.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>_fillColor, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
            g.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #0066CC;">width</span>, <span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#41;</span>;
            g.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>		
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This just draws a rectangle and positions it at (0,0) within the UIComponent. We handle the ResizeEvent so that the sprite stays the same size as the UIComponent container.</p>
<p><small>You may wonder why I&#8217;m adding a Sprite to the UIComponent and not just using the underlying Sprite of the UIComponent class. Well, for reasons which are currently unclear to me, I can&#8217;t seem to get UIComponent to fire the MouseDown event. Perhaps I&#8217;ll come back to this and investigate later &#8211; for now I&#8217;ll use the way I know that works. I don&#8217;t think there&#8217;s anything wrong with approaching a project like this. After all, it&#8217;s generally more important to get something working than it is to write something elegant. I could find, after a few hours of investigation that for some arcane reason I have to do this anyway, so, for now, I&#8217;ll let it stand.</small></p>
<p>Next we need to handle the Sprite&#8217;s MouseDown event, so we declare an event handler in our DragHandle constructor and then add this function definition :</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> handleMouseDown<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>			
    systemManager.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_MOVE</span>, doMove, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
    systemManager.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_UP</span>, endMove, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    _window = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">parent</span> as UIComponent;
&nbsp;
    <span style="color: #808080; font-style: italic;">//We bring the window to the top of the parents display list</span>
    <span style="color: #808080; font-style: italic;">//so that it passes in front of all other display objects.</span>
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">_parent</span>:DisplayObjectContainer = _window.<span style="color: #006600;">parent</span>; 
    <span style="color: #0066CC;">_parent</span>.<span style="color: #006600;">setChildIndex</span><span style="color: #66cc66;">&#40;</span>_window, <span style="color: #0066CC;">_parent</span>.<span style="color: #006600;">numChildren</span> - <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    _originalPosition = <span style="color: #000000; font-weight: bold;">new</span> Point<span style="color: #66cc66;">&#40;</span>_window.<span style="color: #006600;">x</span>, _window.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span>;  
    _mouseDownPosition = <span style="color: #000000; font-weight: bold;">new</span> Point<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">stageX</span>, <span style="color: #0066CC;">e</span>.<span style="color: #006600;">stageY</span><span style="color: #66cc66;">&#41;</span>;  
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This adds two event handlers for MOUSE_MOVE and MOUSE_UP. The former will actually move the handle container (what we&#8217;re calling _window, here) and the latter will disconnect both of these event handlers, ending the drag process :</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> doMove<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">e</span>.<span style="color: #006600;">stopImmediatePropagation</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">var</span> positionToMove:Point = <span style="color: #000000; font-weight: bold;">new</span> Point<span style="color: #66cc66;">&#40;</span>
		_originalPosition.<span style="color: #006600;">x</span> + <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">stageX</span> - _mouseDownPosition.<span style="color: #006600;">x</span><span style="color: #66cc66;">&#41;</span>,
		_originalPosition.<span style="color: #006600;">y</span> + <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">stageY</span> - _mouseDownPosition.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>positionToMove.<span style="color: #006600;">x</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> positionToMove.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">0</span>;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>positionToMove.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> positionToMove.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">0</span>;
&nbsp;
	_window.<span style="color: #006600;">move</span><span style="color: #66cc66;">&#40;</span>positionToMove.<span style="color: #006600;">x</span>, positionToMove.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> endMove<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">e</span>.<span style="color: #006600;">stopImmediatePropagation</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	systemManager.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_MOVE</span>, doMove, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
	systemManager.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_UP</span>, endMove, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>And that&#8217;s pretty much it. You can see a demo (and source code) <a href="/flash/SimpleDragHandle/DragTest.swf">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adhocgeek.com/2009/06/a-simple-flex-drag-handle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

