Grab me constrained to horizontal

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

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Grab me constrained to horizontal

Post by bogs » Tue Dec 26, 2017 11:05 pm

jacque wrote:
Tue Dec 26, 2017 9:09 pm
Your working code is the recommended way to do it. Even though it may appear "complex" it avoids a lot of issues, particularly tracking and checking the status of the mouse, which is time-consuming.
Not that I doubt any part of this statement, but for my own understanding, doesn't any version of this require tracking and checking the status of the mouse?

After all, updating the variable for the location during the mouseMove event, isn't that what it is doing, tracking and checking the status of the mouse? I'm confused.

I like his working code better as well, I just wanted to see if I could figure out the least amount of lines to produce a reasonable result :wink:
Image

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7215
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Grab me constrained to horizontal

Post by jacque » Tue Dec 26, 2017 11:37 pm

MouseMove sends the current coordinates as parameters, so the engine has already done the work much faster than we can. Checking the coordinates within a line of script is more expensive, as is checking any property of the mouse state. In your example:

Code: Select all

on mouseStillDown
   if the mouseLoc is within the rect of graphic "Rectangle" then
      set the left of me to item 1 of the mouseLoc -5
   end if
 end mouseStillDown
This gets the mouse location for an intersection test, then gets the location a second time when calculating the final position. So, two mouse checks plus another call for an intersection check. Using mouseMove requires no calls for the mouse state.

I'm not saying we shouldn't ever ask for the mouse state; "expensive" is relative and the delay is tiny. But since there will be lots and lots of mouseStillDown messages sent at a very rapid rate, delays can add up and cause a backup in the queue. It is more efficient, whenever possible, to use the data the engine provides than to call for it in script. One of the best pieces of advice I ever heard from another developer was, "let the engine do it."

That said, using mouseStillDown is still more efficient than a repeat loop which would be completely blocking.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Grab me constrained to horizontal

Post by richmond62 » Tue Dec 26, 2017 11:55 pm

Code: Select all

on mouseDown
   if the mouseLoc is within the rect of me then
      set the left of me to item 1 of the mouseLoc -5
   end if
end mouseDown

on mouseStillDown
   if the mouseLoc is within the rect of graphic "Rectangle" then
      set the left of me to item 1 of the mouseLoc -5
   end if
 end mouseStillDown
Works on the basis that the thing that is being dragged is 10 pixels wide.

This doesn't care what size the thing that is being dragged is:

Code: Select all

on mouseDown
   put item 1 of the mouseLoc into MISHKA
   put item 2 of the loc of me into UPP
   set the loc of me to MISHKA,UPP
end mouseDown

on mouseStillDown
   put item 1 of the mouseLoc into MISHKA
   put item 2 of the loc of me into UPP
   set the loc of me to MISHKA,UPP
end mouseStillDown
I don't see any particular reasons for the conditional bits.

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Grab me constrained to horizontal

Post by bogs » Wed Dec 27, 2017 1:32 am

jacque wrote:
Tue Dec 26, 2017 11:37 pm
MouseMove sends the current coordinates as parameters, so the engine has already done the work much faster than we can. Checking the coordinates within a line of script is more expensive...
<sic>
... gets the mouse location for an intersection test, then gets the location a second time when calculating the final position. So, two mouse checks plus another call for an intersection check. Using mouseMove requires no calls for the mouse state.
<sic>
That said, using mouseStillDown is still more efficient than a repeat loop which would be completely blocking.
Now that is good to know, I hadn't come across it previously, and didn't think about it while coding it up. It does explain a few things though now that I think on it.
richmond62 wrote:
Tue Dec 26, 2017 11:55 pm
Works on the basis that the thing that is being dragged is 10 pixels wide.

This doesn't care what size the thing that is being dragged is
Yup, just came across this particular information today while exploring some of the presents you sent me :wink:

In previous languages, I'd more likely have coded that "the width of me /2", eliminating the size measuring part of it.

The test for the mouse position was solely to find out if it was inside of the button in the first case, as opposed to somewhere generally in the area or on the card. IF I were just trying to move the button to where the mouse was, it would have been a single mouse down event, but the items would have been the same.

With the additional condition Da_Elf put of only moving right, I would have changed the if/then to simply move the button if the mousedown had been in the rect of the graphic farther right than the right of the button.

What you have there looks pretty slick though :)
Image

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

Re: Grab me constrained to horizontal

Post by Da_Elf » Thu Dec 28, 2017 6:01 pm

all was going well however since i had my slidable objects inside of a vertical scrolling group and was sliding to the right the coordinate system the object was working with was all screwed up due to the group

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

Re: Grab me constrained to horizontal

Post by richmond62 » Thu Dec 28, 2017 6:02 pm

How about posting a screenshot?

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

Re: Grab me constrained to horizontal

Post by Da_Elf » Fri Dec 29, 2017 9:44 pm

tried to reproduce it and it didnt mess up. I have it so that when it is moved if its not moved enough it jumps back to original position. However in some of my tries it almost seemed like coordinates messed up because even though everything was set at 450 in x position when things went to "jump back to original" all items in the group went to -3000something in x
but then i went and re-set everything up and it seems to work now

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”