Can I get the name of an object at the clickloc?

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

M-A Kuttner
Posts: 49
Joined: Mon Apr 03, 2017 3:55 pm
Location: Nova Scotia, Canada

Can I get the name of an object at the clickloc?

Post by M-A Kuttner » Thu Apr 28, 2022 2:28 pm

Hi all. Nice to see you all at the conference and put some faces to names. My question: Is it possible to get the name of an object I click on just by clicking on it without that object having any code in it? Here's what I'm trying to do:

Code: Select all

on mouseUp
wait for the mouseclick
if the mouseclick is true then
get the backgroundcolor of the graphic at the clickloc
end if 
end mouseUp
That's the gist of it. Any ideas? Thanks in advance!
Hypertalk developer on and off since the days of SuperCard. Currently using LC to do rapid prototyping for a new kids' toy.

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

Re: Can I get the name of an object at the clickloc?

Post by stam » Thu Apr 28, 2022 2:38 pm

I think maybe the target is what you need?

ie

Code: Select all

get the backgroundColor of the target

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Can I get the name of an object at the clickloc?

Post by Klaus » Thu Apr 28, 2022 3:27 pm

What stam said!

Code: Select all

on mouseUp
  wait for the mouseclick
  if the mouseclick is true then
   ...
end mouseUp
Not sure what that may mean?
The "mouseup" message will only be sent if a "mouseclick" has in fact occured!

But you could also check for:
-> controlAtLoc(the mouseloc)
or
-> controlAtLoc(the clickloc)

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

Re: Can I get the name of an object at the clickloc?

Post by dunbarx » Thu Apr 28, 2022 4:00 pm

What all said about the "target", one of our most beloved functions.

Perhaps you are thinking about a second click when you check to see if the mouseClick is true? There is a method where it is useful. I find that the "mouseDoubleUp" message is often unreliable, and use this very old technique:

Code: Select all

on mouseUp
   wait 20
   if the mouseClick then beep 2 else beep 1
end mouseUp
You can use the mouseClick function to see if, er, the mouse was clicked.

Craig

M-A Kuttner
Posts: 49
Joined: Mon Apr 03, 2017 3:55 pm
Location: Nova Scotia, Canada

Re: Can I get the name of an object at the clickloc?

Post by M-A Kuttner » Fri Apr 29, 2022 7:55 pm

Thanks everyone! What Im trying to do is replicate a functionality similar to the eyedropper tool in Photoshop, so on mouseUp will switch the cursor from the pointer to my eyedropper cursor and then I want to wait until I roll over one of several graphics, each of which has a different backgroundcolor. When I click the second time, I want to put the backgroundcolor of the target graphic into a variable.

Right now, from what I'm seeing, the target is trapped as the thing I clicked on mouseUp, so when I click the second time (on one of my graphics), it releases the info on the target, but that info is from the the button that contained my original mouseUp instead of the graphic I'm currently clicking on.

So, is there a way to get rid of that mouseUp info and tell LC "Give me the info on the thing that's right under the cursor, right at this instant"?

Cheers,
M-A
Hypertalk developer on and off since the days of SuperCard. Currently using LC to do rapid prototyping for a new kids' toy.

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

Re: Can I get the name of an object at the clickloc?

Post by stam » Fri Apr 29, 2022 8:27 pm

Hi M-A

I think the issue is that
a) you have the script running in a button when it should be in the card. When the button is clicked you act on that in the card script. You can't have logic in the button that will manipulate subsequent mouse clicks that aren't on the button
b) you need to keep track of whether you're sampling a colour or not.

for the latter just have a boolean script variable. If the variable is false and the name of the target is your button that kicks off the sampling then set the variable to true and when you next click it will give the background color of the control it's clicked on (the 'new' target)

edited to show the card script

Code: Select all

local sIsSampling

on mouseUp
     if not sIsSampling and the short name of the target is "sample color"  then
          put true into sIsSampling
          put empty into field "colour"
          exit mouseUp
     else if sIsSampling then
          put the backgroundColor of the target into field "colour"
          put false into sIsSampling
     end if
end mouseUp
if you want to cancel the colour sampling after clicking the sample color button you can trap the escape key in the card script:

Code: Select all

on escapeKey
     # reset and stop it from sampling a colour
     put false into sIsSampling
     put empty into field "colour"
end escapeKey
I threw together a quick and very simple stack to illustrate - i hope this answers the question but do say if i've misunderstood your requirements...

regards
Stam
Attachments
sample a color.livecode.zip
(1.17 KiB) Downloaded 90 times
Last edited by stam on Fri Apr 29, 2022 8:42 pm, edited 2 times in total.

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

Re: Can I get the name of an object at the clickloc?

Post by richmond62 » Fri Apr 29, 2022 8:30 pm

Code: Select all

on mouseEnter
   set the lockCursor to true
   set the cursor to cross
end mouseEnter 

on mouseLeave
   set the cursor to empty
   set the lockCursor to false
end mouseLeave
Attachments
get object.livecode.zip
Stack.
(483 Bytes) Downloaded 67 times

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

Re: Can I get the name of an object at the clickloc?

Post by stam » Fri Apr 29, 2022 8:34 pm

The OP wants to
1. click a button to activate an 'eyedropper' color-sampling facility
2. move the mouse over graphics and when clicked a second time to get the backgroundColor of the clicked object

Changing the cursor is nice, but it doesn't answer the OP ;)
Last edited by stam on Fri Apr 29, 2022 8:46 pm, edited 1 time in total.

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

Re: Can I get the name of an object at the clickloc?

Post by richmond62 » Fri Apr 29, 2022 8:43 pm

on mouseUp will switch the cursor from the pointer to my eyedropper cursor
You have to start somewhere.

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

Re: Can I get the name of an object at the clickloc?

Post by dunbarx » Fri Apr 29, 2022 9:15 pm

No sweat.

Make a graphic and color it any way you want. Make a field. Now make a button with this in its script:

Code: Select all

on mouseUp
   set the cursor to cross
   wait until the mouseClick
   put the mouseColor into fld 1
end mouseUp
Craig

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

Re: Can I get the name of an object at the clickloc?

Post by dunbarx » Fri Apr 29, 2022 9:34 pm

I just used the cross cursor because I like it. Know that you can use lots of things as cursors. If you already have your eyedropper, just mention its cursor identifier. This could take the form of setting the cursor property to the ID of an image, for example.

Craig

M-A Kuttner
Posts: 49
Joined: Mon Apr 03, 2017 3:55 pm
Location: Nova Scotia, Canada

Re: Can I get the name of an object at the clickloc?

Post by M-A Kuttner » Fri Apr 29, 2022 11:04 pm

Fantastic! Thank you so much everyone! What a great community. Not only do I get a solution to my problem, but I also get a bunch of additional great info on how LC works and how to avoid rookie mistakes. You guys rock! :D

M-A
Hypertalk developer on and off since the days of SuperCard. Currently using LC to do rapid prototyping for a new kids' toy.

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

Re: Can I get the name of an object at the clickloc?

Post by jacque » Sat Apr 30, 2022 6:40 pm

I was going to suggest the mouseColor too, but note that you don't need to change the cursor for it to work. A single line in the mouseUp handler is all you need.

Edit: To be clear, set a custom cursor in the button that changes the tool to the eyedropper. In the card script put a mouseUp handler that checks for the cursor ID (you may have more than one type of cursor) and if it's the dropper then get the mouseColor.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Can I get the name of an object at the clickloc?

Post by dunbarx » Mon May 02, 2022 12:26 am

The hard part is knowing what sort of gadgetry LiveCode offers when one is still learning.

So a great lesson here is this:

1- You wanted to get the underlying color when the mouse was clicked. "Color"and "Mouse" seem to be pertinent. Maybe even "Click".
2- You don't know, offhand, what LC has to offer in that regard.
3- You go to the dictionary and see that there are thousands of native words.
4- You are dismayed.
5- You think to yourself, "Oh hell, let's see what comes up when I enter the word 'color' in the search field".
6- Oh my, 80 or so entries where the word "color" appears directly in the entries, and these are at the top of the list.
7- Scroll down, disheartened, scanning, expecting nothing.
8- Wait, what's this??? "MouseColor??? Let's read about that...
9- YOW!!

You might have done the same thing by starting with "mouse" and would have had to deal with a list half as long. Not as logical, though.

That is the hard way to do it. The easy way was to ask here. But now and then you will have to do the hard way. After all, we don't know everything. Then you will appreciate those thousands of entries, because you will be optimistic that LC has a word for everything, pretty much, and you just have to find it. :wink:

Craig

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

Re: Can I get the name of an object at the clickloc?

Post by stam » Mon May 02, 2022 2:56 am

Assuming the premise is:
- the user clicks a button to activate an eyedropper tool
- the user clicks again to sample a colour, then the eyedropper tool deactivates

personally i would still do it the way i posted above - that is to say i'd run this in the card script with a script local variable boolean to keep tabs on whether i'm sampling or not.
Whether you use mouseColor or backgroundColor of the target or whatever other data you want to collect is less important. 3 more words isn't even a drop in the verbose ocean of LiveCode ;)

The main reason I'd do it this way is control: keeping the script in a button that will activate the eyedropper allows no easy is no way to cancel this action for example.
As a user, i would expect software to allow me to cancel an eyedropper tool by hitting the escape button.
Tougher to do in the button script where you are waiting for another mouseClick. Very simple in a card script

Also, wouldn't

Code: Select all

wait until the mouseClick
lock the interface?

S.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”