rectangle graphic on the fly

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

rectangle graphic on the fly

Post by magice » Mon Mar 30, 2009 11:27 pm

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.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Tue Mar 31, 2009 8:26 am

Sure, Magice, check the create command in the docs.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Tue Mar 31, 2009 9:25 am

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

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Post by bn » Tue Mar 31, 2009 2:25 pm

magice,
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
When 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

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Post by magice » Tue Mar 31, 2009 5:45 pm

you are all awesome!!

Dondi
Posts: 18
Joined: Fri Apr 10, 2009 2:40 am

Post by Dondi » Tue Jun 23, 2009 4:09 am

bn wrote:magice,
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
When 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
Brilliant! Thank you bn. This is exactly what I need for my "learning" app.

Dondi.

marksmithhfx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 931
Joined: Thu Nov 13, 2008 6:48 am
Location: London, UK

Re:

Post by marksmithhfx » Mon Nov 09, 2020 5:18 pm

bn wrote:
Tue Mar 31, 2009 2:25 pm
When 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.
regards
Bernd
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

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”