Mouse interactions on Text Layout Framework InlineGraphicElements in EditMode
Thursday, March 26th, 2009One slightly frustrating (but totally logical) feature of using editmode in TLF is that inserted InlineGraphicElements lose their "interactivity". Instead, they are treated the same as text characters, that is, the user can select them as if they were selecting text:

There was a comment In my previous entry about the possibility of mouse interaction with inserted graphics.
I have a way to do this, a bit hackish, but it does the job. Every added graphic needs to be stored in an array. When a user clicks, the code loops through all the graphics and checks if one of them happens to be under the mouse. This is achieved using the trusty old localToGlobal() method:
-
protected function handleMouseDown(e:MouseEvent):void{
-
var hitrect:Rectangle;
-
var bm:Bitmap;
-
for(var i:uint=0;i<_images.length;i++){
-
bm=Bitmap(_images[i]);
-
var ltg:Point=bm.localToGlobal(new Point(0,0));
-
hitrect=new Rectangle(ltg.x,ltg.y,bm.width,bm.height);
-
if(hitrect.contains(mouseX,mouseY)){
-
mx.controls.Alert.show("so, I see you have clicked on an image eh?");
-
break;
-
}
-
}
-
}
This is very useful if you want to edit the graphic somehow, for instance, in the RTE that I'm currently working on I use this for "table support". In the TextFlow, my tables are represented as bitmaps, when clicked, the actual DataGrid is opened and ready to edit. When finished, a new BitmapData is grabbed and the InlineGraphic in the TextFlow is updated.
I have found one "bug" (or a feature?!), if an image is taller than (somewhere around 1000 pixels), the image is no longer properly "wrapped", and actually starts to cover some text.
It's also possible to listen to MouseMove, and set some rollover/out states to the graphics. You can see a demo here and download the source mxml file here.

