rectangle graphic on the fly
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
rectangle graphic on the fly
On my current project, I need for my end users to be able to draw a box within an image area, much like the rectangle tool does in runrev. Is there any way to create graphics on the fly? I have not seen any commands for doing so.
This thread has some basic info about how to set the points of a graphic and its style. http://forums.runrev.com/phpBB2/viewtopic.php?t=1898
-
- VIP Livecode Opensource Backer
- Posts: 3999
- Joined: Sun Jan 07, 2007 9:12 pm
- Location: Bochum, Germany
magice,
tryWhen you put this code into the script of a card it will draw the rectangle anywhere on the card. If you put it into the script of an image it will only draw within the limits of that image.
If you put it into a card script you have to watch out for controls (e.g. buttons) that dont trap the mouseDown. They misbehave. Just add a "on mouseUp/end mouseUp to the script of those controls.
regards
Bernd
try
Code: Select all
local tStartLoc
on mouseDown pMouseBtnNum
if there is a graphic "myGraphic" then delete graphic "myGraphic"
reset templategraphic
set the style of the templategraphic to "rectangle" -- or "oval" or "roundrect"
set the linesize of the templategraphic to 1 -- adjust
-- set the foregroundpattern of the templategraphic to 208034 -- block to have straight line w/o pattern
set the opaque of the templategraphic to false
create invisible graphic
set the name of last graphic to "myGraphic"
put the clickloc into tStartLoc
send "makeMyGraphic" to me in 2 milliseconds
end mouseDown
on makeMyGraphic
if the mouse is up then exit makeMyGraphic
put the mouseloc into tLoc
if item 1 of tStartLoc < item 1 of tLoc then
put item 1 of tStartLoc into tLeft
put item 1 of tLoc into tRight
else
put item 1 of tStartLoc into tRight
put item 1 of tLoc into tLeft
end if
if item 2 of tStartloc > item 2 of tLoc then
put item 2 of tStartloc into tBottom
put item 2 of tLoc into tTop
else
put item 2 of tStartloc into tTop
put item 2 of tLoc into tBottom
end if
if not (tTop < the top of me or tLeft < the left of me or tBottom > the bottom of me or tRight > the right of me) then
put tLeft & "," & tTop & "," & tRight & "," & tBottom into myRect
set the rect of graphic "myGraphic" to myRect
set the visible of graphic "myGraphic" to true
end if
send "makeMyGraphic" to me in 10 milliseconds
end makeMyGraphic
If you put it into a card script you have to watch out for controls (e.g. buttons) that dont trap the mouseDown. They misbehave. Just add a "on mouseUp/end mouseUp to the script of those controls.
regards
Bernd
Brilliant! Thank you bn. This is exactly what I need for my "learning" app.bn wrote:magice,
tryWhen you put this code into the script of a card it will draw the rectangle anywhere on the card. If you put it into the script of an image it will only draw within the limits of that image.Code: Select all
local tStartLoc on mouseDown pMouseBtnNum if there is a graphic "myGraphic" then delete graphic "myGraphic" reset templategraphic set the style of the templategraphic to "rectangle" -- or "oval" or "roundrect" set the linesize of the templategraphic to 1 -- adjust -- set the foregroundpattern of the templategraphic to 208034 -- block to have straight line w/o pattern set the opaque of the templategraphic to false create invisible graphic set the name of last graphic to "myGraphic" put the clickloc into tStartLoc send "makeMyGraphic" to me in 2 milliseconds end mouseDown on makeMyGraphic if the mouse is up then exit makeMyGraphic put the mouseloc into tLoc if item 1 of tStartLoc < item 1 of tLoc then put item 1 of tStartLoc into tLeft put item 1 of tLoc into tRight else put item 1 of tStartLoc into tRight put item 1 of tLoc into tLeft end if if item 2 of tStartloc > item 2 of tLoc then put item 2 of tStartloc into tBottom put item 2 of tLoc into tTop else put item 2 of tStartloc into tTop put item 2 of tLoc into tBottom end if if not (tTop < the top of me or tLeft < the left of me or tBottom > the bottom of me or tRight > the right of me) then put tLeft & "," & tTop & "," & tRight & "," & tBottom into myRect set the rect of graphic "myGraphic" to myRect set the visible of graphic "myGraphic" to true end if send "makeMyGraphic" to me in 10 milliseconds end makeMyGraphic
If you put it into a card script you have to watch out for controls (e.g. buttons) that dont trap the mouseDown. They misbehave. Just add a "on mouseUp/end mouseUp to the script of those controls.
regards
Bernd
Dondi.
-
- VIP Livecode Opensource Backer
- Posts: 931
- Joined: Thu Nov 13, 2008 6:48 am
- Location: London, UK
Re:
Thanks Bernd, I just used this example from Mar 2009 because I needed to add a background rectangle to a card that was already fully populated (header bar, nav bar, datagrid). I suppose I could have tried to place the rectangle image on top and then send to back (next time) but it was easier to just create the object in code than to move and rearrange the existing objects.
Cheers,
Mark
macOS 12.6.5 (Monterey), Xcode 14.2, LC 10.0.0, iOS 15.6.1
Targets: Mac, iOS
Targets: Mac, iOS