resizable rectangle

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

resizable rectangle

Post by Da_Elf » Fri Apr 01, 2016 11:44 pm

I want to have a grc "rectangle" that can be resized interactively by the user. I know how when im on the inside to use grab me to move and drop the object but i want that when the user goes over the edges he can resize it

doable?
to fake it for now im trying to setup mouseenter areas inside the edge of my rectangle to change the cursor icon then resize by a long script id rather not use. however im using

Code: Select all

on mouseLeave
   set cursor to cross
   lock cursor
end mouseLeave
ive got a crossedarrows image which i replace the cross cursor with that however the center of the cross becomes the top left of the image. i want it to be the center of the image

Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

Re: resizable rectangle

Post by Da_Elf » Sat Apr 02, 2016 1:12 am

Ok here is the long code. works very well but i would like to know if there is a shorter way

Code: Select all

local resize
local anchorpos
local theanchor
local mouseStart

on mouseDown
   put the mouseloc into mouseStart
   put findmouse() into theanchor
   if findmouse() is "topleft" then
      put the topleft of me into anchorpos
   else if findmouse() is "bottomleft" then
      put the bottomleft of me into anchorpos
   else if findmouse() is "topright" then
      put the topright of me into anchorpos
   else if findmouse() is "bottomright" then
      put the bottomright of me into anchorpos
   end if
   put true into resize
   --grab me
end mouseDown

on mouseUp
   put empty into mouseStart
   put false into resize
end mouseUp

on mouseLeave
   put empty into mouseStart
   put false into resize
end mouseLeave

on mouseMove
   if resize then
      set itemDelimiter to comma
      put the width of me into tWidth
      put the height of me into tHeight
      put item 1 of mouseStart into startx
      put item 2 of mouseStart into starty
      put item 1 of the mouseloc into thisx
      put item 2 of the mouseloc into thisy
      put thisx - startx into newx
      put thisy - starty into newy
      put tWidth + newx into nWidth
      put tHeight + newy into nHeight
      if (nWidth > 30) and (nHeight > 30) then
         set the width of me to nWidth
         set the height of me to nHeight
         if theanchor is "topleft" then
            set the topleft of me to anchorpos
         else if theanchor is "bottomleft" then
            set the bottomleft of me to anchorpos
         else if theanchor is "topright" then
            set the topright of me to anchorpos
         else if theanchor is "bottomright" then
            set the bottomright of me to anchorpos
         else
           put empty into mouseStart
           put false into resize
         end if
      end if
      put the mouseloc into mouseStart
   end if
end mouseMove

function findmouse
   set itemDelimiter to comma
   put item 1 of the location of me into xloc
   put item 2 of the location of me into yloc
   put item 1 of the mouseloc into mxloc
   put item 2 of the mouseloc into myloc
   if mxloc >= xloc then
      put left into xAnchor
   else
      put right into xAnchor
   end if
   if myloc >= yloc then
      put top into yAnchor
   else
      put bottom into yAnchor
   end if
   return yAnchor&xAnchor
end findmouse

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10330
Joined: Wed May 06, 2009 2:28 pm

Re: resizable rectangle

Post by dunbarx » Sat Apr 02, 2016 1:28 am

Hi.

I did not look at your code, but maybe check out something I did years ago. In a button "liveResize" script:

Code: Select all

local tCorner,tRect

on mouseMove x,y
   if the mouse is down then
      switch tCorner
         case "topRight"
            set the rect of btn "liveresize"  to  item 1 of tRect & "," & y & "," & x & "," & item 4 of tRect
            break
         case  "topLeft"
            set the rect of btn "liveresize"  to x & "," & y & "," & item 3 of tRect & "," & item 4 of tRect
            break
         case "botLeft"
            set the rect of btn "liveresize"  to  x & "," & item 2 of tRect & "," & item 3 of  tRect & "," & y
            break
         case "botRight"
            set the rect of btn "liveresize"  to  item 1 of  tRect  & "," & item 2 of tRect & "," & x & "," & y
            break
      end switch
      set the loc of  btn "handle" to x & "," & y 
   end if
end mouseMove

on mouseDown
put "XXX"
   put the rect of btn "liveresize"  into tRect
   put whichCorner(the mouseLoc,tRect) into tCorner
end mouseDown

function whichCorner tMouseLoc,tRect
   switch 
      case abs(item 1 of tMouseLoc - item 3 of tRect) < 25 and abs(item 2 of tMouseLoc - item 2 of tRect) < 25
         show btn "handle"
         return "topRight"
         break
      case abs(item 1 of tMouseLoc - item 1 of tRect) < 25 and abs(item 2 of tMouseLoc - item 2 of tRect) < 25
        show btn "handle"
         return "topLeft"
         break
      case abs(item 1 of tMouseLoc - item 1 of tRect) < 25 and abs(item 2 of tMouseLoc - item 4 of tRect) < 25
         show btn "handle"
         return "botLeft"
         break
      case abs(item 1 of tMouseLoc - item 3 of tRect) < 25 and abs(item 2 of tMouseLoc - item 4 of tRect) < 25
         show btn "handle"
         return "botRight"
         break
      case var
         return ""
   end switch
end whichCorner

on mouseUp
   put ""
   put "" into tCorner
      --put the rect of btn "liveresize" into tRect
   hide btn "handle"
end mouseUp
Craig Newman

Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

Re: resizable rectangle

Post by Da_Elf » Sat Apr 02, 2016 2:28 am

i realised my code only worked if grabbing the bottom right. ive fixed that and expanded on this by a lot will post the livecode script here when finished.

this is for my POS system to be able to create a table layout for a restaurant without overlapping tables or tables out of bounds
test.zip
(2.99 KiB) Downloaded 241 times

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: resizable rectangle

Post by jacque » Sat Apr 02, 2016 4:13 pm

You can change the active point of the cursor with the "hotspot" function.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply