PDA

View Full Version : Map mouse coordinates to Textarea index


wGrand
10-06-2008, 10:45 PM
I have a textarea and I need to identify the position (or index) in the text over which the mouse cursor is located during a drag / drop event:


<mx:Script>
<![CDATA[
private function doDrag(event:MouseEvent):void
{
...
}

private function dropHandler(event:DragEvent):void
{
var box:Box = Box( event.dragInitiator );
var Y:Number = event.localY;
box.x = event.localX;
box.y = event.localY;

//Here's where I need help, I want something like this:
var index:int = textarea.getIndexFromPosition(event.mousePos ...)
}

]]>
</mx:Script>

<mx:Canvas dragDrop="dropHandler(event)">
<mx:TextArea id="textarea" drag>
<mx:text>Aliquam aliquet, est a ullamcorper condimentum, tellus nulla fingilla elit, a iaculis nulla turpis sed wisi</mx:text>
</mx:TextArea>
<mx:Box id="ticker" x="100" y="200" width="3" height="24" mouseDown="doDrag(event)"/>
</mx:Canvas>


Any ideas?

drkstr
10-06-2008, 11:40 PM
If you were to subclass the TextArea, you can reference the protected property 'textField'. With it, you can use textField.getCharIndexAtPoint(x,y) to get the character index.


Best Regards,
~Aaron

wGrand
10-07-2008, 04:17 PM
Thank you for your reply! I extended the TextArea class, but how do I reference the protected textField property?

drkstr
10-07-2008, 05:36 PM
this.textField


Best Regards,
~Aaron

wGrand
10-07-2008, 05:55 PM
Right, I figured it out. I also created a public function getTextField():IUITextField { return this.textField; } to make it visible outside to other classes.