|
Do you want to allow users to move objects around in your finished project, or simply want to be able to adjust the position of things at runtime? Sure, you can easily move things around while in the Project Editor authoring environment (or SuperEdit, or the third-party Tangerine editing environment), but sometimes it is more useful to be able to move things while the project is running.
The trick lies in SuperCard's Grab command, which allows you to simply grab and move an object by clicking on it and dragging it around.
The Grab command is very easy to use. Try putting the following script into an object, then run the project and try moving it around:
on mouseDown grab me end mouseDown
It really is as simple as that! Of course, you can do a lot more than just this. For instance, if you don't want to have to put the script into every object, but you still want to be able to move things around when you click them, you can put the following script into the card script - or the background, window or project script. (This exploits the 'message path' concept where a message such as mouseDown travels up the path from object to card to window to project, looking for a matching script handler.) Put the following script into the card script of your test project, and try moving any object around.
on mouseDown grab the target end mouseDown
By using the target rather than the self-referential me SuperCard understands that you mean whatever happens to be the target of the current mouseDown event... in other words, what you just clicked on.
If you want to restrict this to a specific set of objects you could try adding in some kind of filter to the above mouseDown handler which only performs the grab line if you've clicked on an appropriate object. For instance, if you want to just allow grabbing of card graphics rather than fields or buttons, you can use the "kind" property to see what kind of object has been clicked...
on mouseDown if the kind of the target is "graphic" then grab the target end mouseDown
Or, should you wish to restrict grabbing to items with certain names...
on mouseDown if the short name of the target contains "grabbable" then grab the target end if end mouseDown
Note that getting the short name gets literally just the name, not card graphic "grabbable box" as requesting the name would return, or card graphic "grabbable box" of card ID 101 of window ID 100 of project "SuperCard Universe:project archive:drag test" as requesting the long name would return.
And note that by checking to see if the short name contains rather than if the short name is, you can use any object which has whatever you check for as part or all of it's name.
If you want to make an object only grabbable once, you could then set the name of the target to something which excludes it the next time, either at the end of the mouseDown handler or (for finer control over individual objects) in a separate mouseUp handler in the object itself. You could check the location before doing anything. The following script removes the object's name if the left edge is more than 200 pixels from the left of the window edge when it is released:
on mouseUp if the left of me > 200 then set the name of me to empty end mouseUp
(Of course, you can use the right of me, the top of me, the bottom of me...)
And finally, you can restrict the "grab" move to a particular area - such as the bounding box of another object, or a specified set of coordinates.
on mouseDown grab me within "10,10,200,200" end mouseDown
on mouseDown grab me within the rectangle of card graphic "box" end mouseDown
All this deals with just moving objects around using the grab command. You can also get feedback on where you're dragging the object as you go by adding with message to the grab line, and adding a mouseStillDown handler to intercept the mouseStillDown events generated by this as you drag the object about:
on mouseDown grab me with message within the rectangle of card graphic "box" end mouseDown
on mouseStillDown put the location of me into card field "info" end mouseStillDown
Note that you have to put the with message part before the within part of the line, or SuperCard doesn't see it. |