Auto-resizing is a common feature in contemporary programming languages, but not Flex. It is uncommon. After surveying, I have to admit that I can't find it. So, I decide to write it by my own.
The auto-resizing is based on the mouse position and dragging operation. The dragging operation can be observed by mouse down or drag start. The mouse position can be observed by the mouse move event. But we won't be notified while the mouse is moved out side of canvas. Another way to be aware of mouse can be done by reading mouseX and mouseY in enter frame event, even if the mouse is moved out of canvas.
The example program will be designed as:
1. using a boolean flag to record mouse is down
2. resizing the inner canvas while the mouse is dragging the right-bottom corner
3. check if auto-resizing while entering frame
The following are the code:
<mx:canvas id="outer" enterFrame="handleEnterFrame(outer.mouseX, outer.mouseY)" mouseUp="handleMouseUp(inner.mouseX, inner.mouseY)" mouseDown="handleMouseDown(inner.mouseX, inner.mouseY)" mouseMove="handleMouseMove(inner.mouseX, inner.mouseY)" x="114" y="56" width="334" height="271" borderStyle="solid" borderColor="#FFFFFF" borderThickness="5">
<mx:canvas id="expander" x="0" y="0" width="{inner.width+ 20}" height="{inner.height + 20}">
<mx:canvas id="inner" x="10" y="10" width="200" height="200" borderStyle="solid" borderColor="#33444F" borderThickness="2">
</mx:canvas>
</mx:canvas>
</mx:canvas>
The above codes declare three canvases, outer, expander, and inner. The outer is the bound while holds the resizing canvas. The expander is one of the resized canvas who preserves 30 pixels offset between inner and outer. The inner canvas is the one we want to resize.
private function handleMouseDown(x:Number, y:Number):void
{
if(x > (inner.width - 20) && y > (inner.height - 20))
{
m_MouseDown = true;
m_MouseOffsetX = x;
m_MouseOffsetY = y;
}
}
These codes handles mouse down event of outer canvas, but the x and y arguments are mouseX and mouseY of inner canvas. This method marks mouse down while a user presses right-bottom corner.
private function handleMouseUp(x:Number, y:Number):void
{
m_MouseOffsetX = 0;
m_MouseOffsetY = 0;
m_MouseDown = false;
}
These codes handles mouse up event of outer canvas. It marks mouse not down.
private function handleEnterFrame(x:Number, y:Number):void
{
if(m_MouseDown){
if(x > outer.width - 30 && x <>
{
inner.width += 1;
m_MouseOffsetX = inner.mouseX;
outer.horizontalScrollPosition+=2;
trace("Frame Increase X");
}
if(y > outer.height - 30 && y <>
{
inner.height += 1;
m_MouseOffsetY = inner.mouseY;
outer.verticalScrollPosition+=2;
trace("Frame Increase Y");
}
}
}
These codes actually resizing inner canvas. While user presses the right-bottom corner of inner canvas and moves the mouse to the offset between inner canvas and outer canvas, it auto-resizes. This method increase width or height of inner canvas by one and change the scroll bar position to the end.
The following is the view of the above code: