DAGLI ESPERTI DI COMTASTE

di Liviu Stoica

the ObjectHandles library with a smoother resizing movement
A very common UI element found in many applications are those little square handles around an on-screen object that allow you to move & resize it. To do this you can use the great ObjectHandles library.

ObjectHandles (http://osflash.org/projects/objecthandles) is a library to allow a user to move and resize UI elements in a Flex based application.
It provides drag handles around controls that users can manipulate with their mouse. Any Flex component can be placed within an ObjectHandles container to quickly allow you to add functionality.

It behaves as a container object where you can put any other flex components and use its resizeable functionalities.

Features of the ObjectHandles library:

- Move & Resize Functionality for any flex component

- Ability to limit movement and/or resizing in both vertical and horizontal directions

- Ability to limit movement and/or resizing along an anchor

- Simple 1-file (.swc) install

- Mouse cursors change when over different handles

- Graphical handle support

- Aspect ratio support

- Initial rotatation support has been added

ObjectHandles is a very cool library for your project when you want to implement moving & resizing any kind of flex object.

Example usage:
In the example below you’ll see two buttons in a vbox that can be moved together. This is the mxml to achieve that:

  1. <oh:ObjectHandles x=”10” y=”158” width=”107” height=”108”
  2. minHeight=”30” minWidth=”30”>
  3. <mx:VBox borderStyle=”none” top=”0” left=”0” right=”0” bottom=”0”>
  4. <mx:Button width=”100%” height=”50%” />
  5. <mx:Button width=”100%” height=”50%” />
  6. </mx:VBox>
  7. </oh:ObjectHandles>

Or if you want an Actionscript example:
Here’s an example to dynamically load an image into an ObjectHandle and allow resize movements:

  1. <protected function init() : void
  2. {
  3. var oh:ObjectHandles = new ObjectHandles();
  4. oh.height = 50;
  5. oh.width = 50;
  6.  
  7. var image:Image = new Image();
  8. image.source = “snowflake.png”;
  9. image.percentHeight = 100;
  10. image.percentWidth = 100;
  11. image.maintainAspectRatio = false;
  12.  
  13. oh.addChild(image);
  14. oh.allowRotate = false;
  15.  
  16. genericExamples.addChild(oh); // replace genericExamples with a canvas in your application
  17. }

Improving the experience of the ObjectHandles library
When you drag or resize an object from right to left or from up to down everything works great.
But if you to resize the object back (from right to left) you’ll see that the inside object will cause a strange flickering. To prevent this we can use the “Flex Move Effect” and “Flex Resize Effect” instide of change programmaticaly width and height and x and y.

How it actually works:

Inside the ObjectHandles.as Actionscript 3 class, you can find the protected function onMouseMove(event:MouseEvent) : void.
This is the function that moves and resizes UI elements in a Flex based application. Find the following line inside the ObjectHandles.as:

  1. if( wasMoved || wasResized )
  2. {
  3. applyConstraints(desiredPos,desiredSize);
  4. x = desiredPos.x;
  5. y = desiredPos.y
  6. width = desiredSize.x;
  7. height = desiredSize.y;
  8. }

The applyConstraints() method simply applies restrictions to your objects like xAnchor, minium size, etc. The other four lines simply set the x,y coordinates and the width and height properties of the Flex object.
Now let’s change this four lines to prevent the strange flicker bug that affects the object :

  1. if( wasMoved || wasResized )
  2. {
  3. applyConstraints(desiredPos,desiredSize);
  4.  
  5. if(moveEffect.isPlaying){
  6. moveEffect.end();
  7. }
  8. moveEffect = new Move();
  9. moveEffect.target = this;
  10. moveEffect.duration = 1;
  11. moveEffect.xTo = desiredPos.x;
  12. moveEffect.yTo = desiredPos.y;
  13. moveEffect.play();
  14.  
  15. if(resizeEffect.isPlaying){
  16. resizeEffect.end();
  17. }
  18. resizeEffect = new Resize();
  19. resizeEffect.target = this;
  20. resizeEffect.duration = 1;
  21. resizeEffect.heightTo = desiredSize.y;
  22. resizeEffect.widthTo = desiredSize.x;
  23. resizeEffect.play();
  24. }

It replaces the brutal property changing of the widget with a smoothness move and resize effect provided by Flex.

In the next post we will see how to create a snap to grid and a snap to line effect.

________________________________________________________________

Liviu Stoica
l.stoica@comtaste.com
Apassionato e affascinato dalla tecnologia con competenze in Flex, Livecycle Data Services, Java e PHP per la realizzazione di applicazioni enterprise. Ha lavorato su diversi progetti, molti dei quali legati al settore finanziario e medico-chirurgico. Fa parte del team di Comtaste all’interno del quale sviluppa Rich Internet Application.