Moving a line...

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

cmhjon
Posts: 175
Joined: Tue Aug 04, 2015 11:55 am

Moving a line...

Post by cmhjon » Tue May 23, 2023 9:15 pm

Hi everyone,

I want to be able to move a line (while NOT in edit mode) simply by clicking and holding the mouse and dragging it to a desired position. I have implemented a script to do this which was easy enough:

Code: Select all

on mouseStillDown
     set the loc of me to the mouseLoc
end mouseStillDown
The question I have is that since the line thickness is only one pixel thick which makes it a tad more difficult to move requiring the user to correctly position the cursor over the line in order to move it, is there a way to somehow add "padding" around the line to make it easier to click on and move?

Thank you,
Jon :-)

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Moving a line...

Post by stam » Wed May 24, 2023 12:39 am

It was a revelation to me that you can actually just use pointer tool even in standalone apps (just set the cantSelect of the interface elements you don’t want the user to move to true), making moving/resizing easier, so that’s something to consider, but possibly doesn’t fulfil your use case.

As to moving lines - that’s a pain. I don’t know if grouping the line may help, as it adds a 4 px margin - maybe move your code into the line’s group?
I know some just don’t use lines at all and just use a rect with real narrow width or height, so again that may help…

cmhjon
Posts: 175
Joined: Tue Aug 04, 2015 11:55 am

Re: Moving a line...

Post by cmhjon » Wed May 24, 2023 1:14 am

Hi Stam,

I tried creating a group and adding the line to it. As you said, it had a 4px margin. I also added the aforementioned script to the group element but it won't move. I tried making the group opaque but that didn't help.

Any ideas?

Thanks,
Jon :-)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Moving a line...

Post by dunbarx » Wed May 24, 2023 3:07 am

Hi.

This is either homework for you or homework for me. I vote for you.

On a new card make a vertical graphic. Vertical, because we are going to demonstrate a principle. The homework comes later, when this principle has to be made more universal.

In the card script:

Code: Select all

on mouseMove
   if abs(item 1 of the mouseLoc - the left of grc 1) < 15 and the mouse is down
   then set the loc of grc 1 to the mouseLoc
end mouseMove
Now click and hold near the graphic and move the cursor.

Homework. Get going.

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Moving a line...

Post by dunbarx » Wed May 24, 2023 3:14 am

Your time is up. No homework, I see. Lazy kids...

OK. This is an easy one, but may be too "broad". I give myself a C+.

Code: Select all

on mouseMove
   if the mouseLoc is within the rect of grc 1 and the mouse is down
   then set the loc of grc 1 to the mouseLoc
end mouseMove
Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Moving a line...

Post by richmond62 » Wed May 24, 2023 11:33 am

SShot 2023-05-24 at 13.31.09.png
-
While the line is only 2 pixels thick, I have set the width of the group to 20 pixels, and 'stained' it red for demonstration purposes only.

A moronically simple solution that works.
-
Just grab it and 'run'. 8)
Attachments
Line Mover.livecode.zip
Stack.
(10.82 KiB) Downloaded 55 times

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Moving a line...

Post by dunbarx » Wed May 24, 2023 2:39 pm

Richmond.

I do not particularly like adding another "layer" on top of the control of interest.

It works perfectly, of course.

@Jon. Lots of ways to do things in LC, as you now know. But what do you think? And where is your homework?

Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Moving a line...

Post by richmond62 » Wed May 24, 2023 2:43 pm

Craig,

What is your objection to another 'layer'?

Come to think of things, you could 'grab' the line, although it would only be suitable for people who don't have arthritic fingers.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Moving a line...

Post by dunbarx » Wed May 24, 2023 3:11 pm

Richmond.

The main issue is that an "overlayer" blocks messages from being invoked at the "real" control. This can be kludged, but, well, has to be kludged.

It adds another, er, layer of complexity. It seems right to me to address the problem with the actual control.

Craig

cmhjon
Posts: 175
Joined: Tue Aug 04, 2015 11:55 am

Re: Moving a line...

Post by cmhjon » Wed May 24, 2023 5:14 pm

Hi Richmond,

I downloaded your stack. When I put the cursor in the red area, the object will not move. I have to the put the hotspot of the cursor right on the line to get it to move. Am I missing something?

Thanks,
Jon

P.S. was my reply my homework? ;-)
richmond62 wrote:
Wed May 24, 2023 11:33 am
SShot 2023-05-24 at 13.31.09.png
-
While the line is only 2 pixels thick, I have set the width of the group to 20 pixels, and 'stained' it red for demonstration purposes only.

A moronically simple solution that works.
-
Just grab it and 'run'. 8)

cmhjon
Posts: 175
Joined: Tue Aug 04, 2015 11:55 am

Re: Moving a line...

Post by cmhjon » Wed May 24, 2023 5:23 pm

Hi Craig!

I tried your script and it worked! As there could be more than one line added, will need to figure out how to get the script to account for the number of graphics already present so that only the one that is clicked moves and not any others.

Thank you so much!
Jon :)
dunbarx wrote:
Wed May 24, 2023 3:07 am
Hi.

This is either homework for you or homework for me. I vote for you.

On a new card make a vertical graphic. Vertical, because we are going to demonstrate a principle. The homework comes later, when this principle has to be made more universal.

In the card script:

Code: Select all

on mouseMove
   if abs(item 1 of the mouseLoc - the left of grc 1) < 15 and the mouse is down
   then set the loc of grc 1 to the mouseLoc
end mouseMove
Now click and hold near the graphic and move the cursor.

Homework. Get going.

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Moving a line...

Post by dunbarx » Wed May 24, 2023 5:36 pm

Hi.

So now it matters how sloppy you will tolerate the distance between the graphic and the cursor. It now may matter how many other graphics there are and how close they might be to each other.

May I assume that these graphics will have varying angles?

Craig

jiml
Posts: 336
Joined: Sat Dec 09, 2006 1:27 am
Location: Los Angeles

Re: Moving a line...

Post by jiml » Wed May 24, 2023 6:06 pm

This might help. If you have multiple lines use a behavior script.
Put this in a button's script:

Code: Select all

local myTool

on mouseenter
   put the tool into myTool
   if myTool is "browse tool" then
      select me
      choose pointer
   end if
end mouseenter

on mouseLeave
   if myTool = "browse tool" then
      select empty
      choose browse
   end if
end mouseLeave
Then set the behavior of each line to that button.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Moving a line...

Post by dunbarx » Wed May 24, 2023 6:22 pm

Try this stack.

Let me know when you find out the one little thing that may well become homework.

Craig
MoveGraphicsAround.livecode.zip
(1.16 KiB) Downloaded 58 times

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

Re: Moving a line...

Post by bn » Wed May 24, 2023 7:37 pm

dunbarx wrote:
Wed May 24, 2023 6:22 pm
Let me know when you find out the one little thing that may well become homework.
Craig,

here is my assignment. It also solves the problems with lines since it uses the rect of a graphic.
For horizontal lines and vertial lines you could group those and it will increase the "hot" area for mouseDown to make it easer to grab the line.

Using your stack and applying this code to the card script:

Code: Select all

local sGrcToMove
local sDiffX, sDiffY

on mouseMove pX, pY
   if sGrcToMove <> "" and the mouse is down then 
      set the loc of grc sGrcToMove to pX + sDiffX, pY + sDiffY
   end if
end mouseMove

on mouseDown
   put the mouseLoc into tMLoc
   put empty into sGrcToMove
   
   repeat with i = 1 to the number of graphics
      if the owner of grc i begins with "group" then
         put true into tGrouped
         put the rect of the owner of grc i into tOwnerRect
      else 
         put false into tGrouped
         put empty into tOwnerRect
      end if
     
      if (tMLoc is within the rect of grc i) or (tGrouped and  tMLoc is within tOwnerRect) then
         put the name of grc i into sGrcToMove
         exit repeat
      end if
   end repeat
   
   if sGrcToMove is empty then exit mouseDown
   put the loc of grc sGrcToMove into tGLoc
   put item 1 of tGLoc - item 1 of tMLoc into sDiffX
   put item 2 of tGLoc - item 2 of tMLoc into sDiffY
end mouseDown

on mouseUp
   put empty into sGrcToMove
end mouseUp
Kind regards
Bernd

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”