New topic: make canvas item clickable
<http://forums.realsoftware.com/viewtopic.php?t=30472> Page 1 of 1 [ 4 posts ] Previous topic | Next topic Author Message pbart Post subject: make canvas item clickablePosted: Wed Oct 14, 2009 12:36 am Joined: Sat Oct 10, 2009 6:40 am Posts: 4 I am adding some text from an editfield to a canvas using the following code applied to the action of a push button: Code:Canvas1.Graphics.DrawString(EditField1.Text,50,50) My question is, how do I make this item selectable with the mouse so that I can apply drag actions to it. I have done this with the whole canvas, but can't figure out how to only get selected items on the canvas. Thanks in advance Paul Top Phil M Post subject: Re: make canvas item clickablePosted: Wed Oct 14, 2009 1:33 am Joined: Fri Sep 30, 2005 12:18 pm Posts: 100 Tricky tricky tricky. Not easy to do, as it calls for a lot of measurements. Get to know the Graphics.Text... and Graphics.String... type properties because you are gonna basically use them all. It would be best to use the same type of system the REALbasic uses for its string functions, and so this example will do something like the TextArea for text selection. Basically, you: 1. Capture a MouseDown and calculate which CHARACTER POSITION the mouse is closest to (rounding as necessary), setting the SelStart to that char index (where first char in your string is 1), and SelLength = 0. Return True 2. In the MouseDrag, you convert the current mouse position to a second CHARACTER POSITION. When you compare this second char position to the SelStart, that gets your SelLength (which COULD be negative). 3. In addition to calculating the position, you have to draw the selection to your canvas, but ONLY if the SelStart or SelLength values have changed (to avoid flicker and massive CPU drain on text selections). You have to calculate the TextAscent for the chars selected and draw the selected char box behind the text. Be careful of two things... the chars you have selected might not be the same font / size as the other chars in the line... you should draw the selection box height to be the max for the line to be visually correct. Secondly, multiple lines selected mean multiple "selection" boxes to draw. To draw the selection boxes, you need to separate each split the TEXT into 3 parts. Anything before the normalized SelStart is in first group, anything after normalized SelStart + SelLength in the last group, and then the actual selection in the middle group. For single line text, your selection box starts at the g.StringWidth( selectionPartFirst ) and is g.StringWidth( selectionPartMiddle ) pixels long. Multiple lines are tricker because you have to get selectionPartFirst as a whole for the yOffset box start, and then only the last line in that to get the xOffset (same for the end of the selection). 4. In the MouseUp event you normalize the SelStart and SelLength when SelLength is negative. That way your code always has a positive length to worry about (except your selection box drawing routines which will have to work in negative lengths, but you can normalize the values there as local variables). The math gets lots more complex if you have multiple lines or if you support Stylized Text (multiple fonts / sizes). If this doesn't give you a basic understanding, please let me know. Top pbart Post subject: Re: make canvas item clickablePosted: Wed Oct 14, 2009 1:39 am Joined: Sat Oct 10, 2009 6:40 am Posts: 4 Thanks for your reply Phil, I this gives me something to go on. Top timhare Post subject: Re: make canvas item clickablePosted: Wed Oct 14, 2009 1:42 am Joined: Fri Jan 06, 2006 3:21 pm Posts: 6555 Location: Portland, OR USA Also, have a look at the DragPics example project that came with RB. That may give you some ideas for how to deal with smaller areas inside a larger canvas. Tim Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 4 posts ] -- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml [email protected]
