MouseEnter
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
MouseEnter
Hello,
I'm a complete beginner. When using the "MouseEnter" code, is it possible to specify how/where the mouse enters? For example "mouseEnterLeft", "mouseEnterTop". I tried those, but they didn't work. If the object I'm coding is a square, I would each side to have a different action. I would also like to able to specify with "mouseLeave".
Hope that makes sense.
Thank you.
I'm a complete beginner. When using the "MouseEnter" code, is it possible to specify how/where the mouse enters? For example "mouseEnterLeft", "mouseEnterTop". I tried those, but they didn't work. If the object I'm coding is a square, I would each side to have a different action. I would also like to able to specify with "mouseLeave".
Hope that makes sense.
Thank you.
Re: MouseEnter
Hi.
Ready for some real fun? When the mouse enters the perimeter of the object in question, its "mouseLoc" can be read at that instant. Make a button. Put this into the button script:
on mouseEnter
put the mouseLoc --appears in the message box
end mouseEnter
Now this is a coordinate that tells you what point on the perimeter of the object the cursor first touched that object. Now read up on the following properties: left, right, top, bottom
There are many more, and many more pertinent functions and properties that can be exploited for this sort of thing. It is much better for you to play around with these than for me (or anyone) to tell you more. But do write back when you have learned a bit about how this works.
At that time you will get more help, hints and maybe chastisement. This is good stuff.
Read the dictionary. Your first stabs at putting words into LC's mouth were spot on, albeit completely wrong. This is another good thing. It means you pretty much are in tune with what LC lives by. Oh yes, read the dictionary.
Craig Newman
Ready for some real fun? When the mouse enters the perimeter of the object in question, its "mouseLoc" can be read at that instant. Make a button. Put this into the button script:
on mouseEnter
put the mouseLoc --appears in the message box
end mouseEnter
Now this is a coordinate that tells you what point on the perimeter of the object the cursor first touched that object. Now read up on the following properties: left, right, top, bottom
There are many more, and many more pertinent functions and properties that can be exploited for this sort of thing. It is much better for you to play around with these than for me (or anyone) to tell you more. But do write back when you have learned a bit about how this works.
At that time you will get more help, hints and maybe chastisement. This is good stuff.
Read the dictionary. Your first stabs at putting words into LC's mouth were spot on, albeit completely wrong. This is another good thing. It means you pretty much are in tune with what LC lives by. Oh yes, read the dictionary.
Craig Newman
Last edited by dunbarx on Thu May 23, 2013 4:46 am, edited 1 time in total.
Re: MouseEnter
I should not be so cavalier; it is a character flaw.
Are you comfortable with coordinate notation? Have you ever programmed in any other language?
If yes to both of these, then I retract my first sentence.
Craig Newman
Are you comfortable with coordinate notation? Have you ever programmed in any other language?
If yes to both of these, then I retract my first sentence.
Craig Newman
Re: MouseEnter
Thanks for the tips Craig - I'll try them out. Never programmed in any other languages. I'll let you know how I get on.
Re: MouseEnter
OK. A small head start...
I hope you already have that button. Name it "targetButton". Try this in the card script:
on mouseMove
put the mouseLoc
end mouseMove
Move the cursor around. When you get tired of that, lose that script, and write:
on mouseMove
put item 1 of the mouseLoc && ":" && the left of button "targetButton"
end mouseMove
Move the cursor slowly to the right as you approach the left side of the button. Watch the message box. A couple of points:
think about "item 1 of the mouseLoc"
Note that the "left" of button "targetButton" does not change.
As you get closer to that left side, a plan should be forming...
Craig Newman
I hope you already have that button. Name it "targetButton". Try this in the card script:
on mouseMove
put the mouseLoc
end mouseMove
Move the cursor around. When you get tired of that, lose that script, and write:
on mouseMove
put item 1 of the mouseLoc && ":" && the left of button "targetButton"
end mouseMove
Move the cursor slowly to the right as you approach the left side of the button. Watch the message box. A couple of points:
think about "item 1 of the mouseLoc"
Note that the "left" of button "targetButton" does not change.
As you get closer to that left side, a plan should be forming...
Craig Newman
Re: MouseEnter
Let me tag team with Mr. Newman.
Just for some more info, a button is a "control" or "object", everything is one of those (images, graphics, movie player, fields).
So, while the above mentions a button, it applies equally to everything on your card. As long as it's named "targetButton".
Simon
EDIT: And I agree:
Just for some more info, a button is a "control" or "object", everything is one of those (images, graphics, movie player, fields).
So, while the above mentions a button, it applies equally to everything on your card. As long as it's named "targetButton".
Simon
EDIT: And I agree:
Is very good thinking."mouseEnterLeft", "mouseEnterTop".
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!
Re: MouseEnter
Craig,
I tried-out your code and saw the results - I want to turn that now into a practical effect. What I'm trying to do is musical. I was able to assign sounds (piano) to mouse commands - mouseDown, mouseUp, mouseEnter, mouseLeave, - 4 commands, producing 4 different notes on one button. How do I use the co-ordinates your code gives me to do a similar thing, with the mouse entering from left, top etc?
Cheers,
Mark
I tried-out your code and saw the results - I want to turn that now into a practical effect. What I'm trying to do is musical. I was able to assign sounds (piano) to mouse commands - mouseDown, mouseUp, mouseEnter, mouseLeave, - 4 commands, producing 4 different notes on one button. How do I use the co-ordinates your code gives me to do a similar thing, with the mouse entering from left, top etc?
Cheers,
Mark
Re: MouseEnter
Hi.
Let's start off right. Those are messages, not commands. Your favorite command will be "put". These things matter.
Messages are sent after some action occurs, such as the mouse being clicked, and you might see a handful of messages that are sent with that simple operation. The button goes down, it stays down for a time, it goes up. There is even a function (the mouseClick") telling you that those things happened in the past. Similarly, there are more conditions than one might think of with other seemingly benign actions.
You saw that the coordinates showed the position of the cursor (the mouseLoc) on the fly. The job requires a notification when those coordinates fit in some way to the edge in question. I will give you a script to play with. It should give you a head start on your project. Please examine and learn what is going on with it, or much of its value will be lost. It does use high beginner features, and you must read up on (and experiment with) the "switch" control structure. Your job is to rewrite this script as an "if-then" structure. If you do, you will not regret it. You need not master it all at once, but you have to plug away. I won't talk to you if I think you are shirking this task.
Still have that button? Or a more convenient object as Simon rightly discussed? Nothing wrong with a button, though:
1- Set its rect to "100,100,200,200"
2- In the object's script:
on mouseEnter
put item 1 of the mouseLoc into X
put item 2 of the mouseLoc into y
switch
case x > 100 and x < 110
put "left"
break
case y > 100 and y < 110
put "top"
break
case x < 200 and x > 190
put "right"
break
case y < 200 and y > 190
put "bottom"
break
end switch
end mouseEnter
Move the mouse into the various sides of the object. Now try to use that feedback to implement your sound thing.
Have you spotted the problem with this script? It mostly works now, but we have to fix it. This is for later.
There are many improvements possible, as it stands. For example, not requiring explicit values of the object rect and its references, an indication this is not very robust code. Also, using the message box as the main display is convenient, but is not proper for a serious project. Consider improving those as additional assignments.
Simon, give him more homework.
Craig Newman
Let's start off right. Those are messages, not commands. Your favorite command will be "put". These things matter.
Messages are sent after some action occurs, such as the mouse being clicked, and you might see a handful of messages that are sent with that simple operation. The button goes down, it stays down for a time, it goes up. There is even a function (the mouseClick") telling you that those things happened in the past. Similarly, there are more conditions than one might think of with other seemingly benign actions.
You saw that the coordinates showed the position of the cursor (the mouseLoc) on the fly. The job requires a notification when those coordinates fit in some way to the edge in question. I will give you a script to play with. It should give you a head start on your project. Please examine and learn what is going on with it, or much of its value will be lost. It does use high beginner features, and you must read up on (and experiment with) the "switch" control structure. Your job is to rewrite this script as an "if-then" structure. If you do, you will not regret it. You need not master it all at once, but you have to plug away. I won't talk to you if I think you are shirking this task.
Still have that button? Or a more convenient object as Simon rightly discussed? Nothing wrong with a button, though:
1- Set its rect to "100,100,200,200"
2- In the object's script:
on mouseEnter
put item 1 of the mouseLoc into X
put item 2 of the mouseLoc into y
switch
case x > 100 and x < 110
put "left"
break
case y > 100 and y < 110
put "top"
break
case x < 200 and x > 190
put "right"
break
case y < 200 and y > 190
put "bottom"
break
end switch
end mouseEnter
Move the mouse into the various sides of the object. Now try to use that feedback to implement your sound thing.
Have you spotted the problem with this script? It mostly works now, but we have to fix it. This is for later.
There are many improvements possible, as it stands. For example, not requiring explicit values of the object rect and its references, an indication this is not very robust code. Also, using the message box as the main display is convenient, but is not proper for a serious project. Consider improving those as additional assignments.
Simon, give him more homework.
Craig Newman
Re: MouseEnter
Thanks Craig. Much appreciated!
Re: MouseEnter
Hey Craig,
That worked great. I examined the code and got the notes playing as I wanted on each side of the button, then got several buttons in a row by copy and pasting altering the co-ordinates.
Cheers,
Mark
That worked great. I examined the code and got the notes playing as I wanted on each side of the button, then got several buttons in a row by copy and pasting altering the co-ordinates.
Cheers,
Mark
Re: MouseEnter
Good.
But you did not see the problem I warned about early on. Move the mouse into the bottom until you get "bottom", however you handled that. Now slowly move into the right side of the object, but very near the top of that side. You get "top", and that is what I wanted you to find on your own. It has to do with the ordering of the switch statement, and more fundamentally, with quick and sloppy programming. Mine.
So if this does not matter to you, in that you always enter a side near the middle of that side, then you can ignore it. But I would not be able to, and want you to think about how to fix it. My solution, which I made as soon as I saw the issue, may not be yours, and that is a good thing. But ignoring it is not.
Craig
But you did not see the problem I warned about early on. Move the mouse into the bottom until you get "bottom", however you handled that. Now slowly move into the right side of the object, but very near the top of that side. You get "top", and that is what I wanted you to find on your own. It has to do with the ordering of the switch statement, and more fundamentally, with quick and sloppy programming. Mine.
So if this does not matter to you, in that you always enter a side near the middle of that side, then you can ignore it. But I would not be able to, and want you to think about how to fix it. My solution, which I made as soon as I saw the issue, may not be yours, and that is a good thing. But ignoring it is not.
Craig
Re: MouseEnter
Hi again.
You are on your way, and making clones of the objects and altering the coordinates explicitly works fine. But I also wanted you to make the code more robust, so that it does not matter where or how large any objects are. This is slightly more advanced stuff, but still at a level which would be priceless for you to learn.
Don't leave a project just because it works. You can do that later on, when you have more experience, and know you could fix stuff if you wanted to.
Get going. Write back.
Craig
You are on your way, and making clones of the objects and altering the coordinates explicitly works fine. But I also wanted you to make the code more robust, so that it does not matter where or how large any objects are. This is slightly more advanced stuff, but still at a level which would be priceless for you to learn.
Don't leave a project just because it works. You can do that later on, when you have more experience, and know you could fix stuff if you wanted to.
Get going. Write back.
Craig
Re: MouseEnter
Hey,
What I did do was to alter some of the co-ordinates
where the original code was:
case x > 100 and x < 110
I put:
case x > 100 and x < 102
and:
case x < 200 and x > 190
I put:
case x < 200 and x > 198
Etc. I was more accurate that way.
How would you suggest making the code so the size and position of the object dosen't matter?
What I did do was to alter some of the co-ordinates
where the original code was:
case x > 100 and x < 110
I put:
case x > 100 and x < 102
and:
case x < 200 and x > 190
I put:
case x < 200 and x > 198
Etc. I was more accurate that way.
How would you suggest making the code so the size and position of the object dosen't matter?
Re: MouseEnter
Also why does it only work if you move the mouse slowly? Is there a way to make sure works when it moves quickly too?
-
- Livecode Opensource Backer
- Posts: 5
- Joined: Thu Feb 28, 2013 10:44 am
- Contact:
Re: MouseEnter
The reason is that you are looking at the end result of the movement instead of looking at where the mouse came from. One thing you could try is to put a mouseLeave handler in the card script, and remember the old mouseLoc, and then compare to that. Something like (untested):markokoh wrote:Also why does it only work if you move the mouse slowly? Is there a way to make sure works when it moves quickly too?
on mouseLeave
global gLastMouseLoc
put the mouseLoc into gLastMouseLoc
pass mouseLeave
end mouseLeave
and then in your button or field:
on mouseEnter
global gLastMouseLoc
if gLastMouseLoc is empty then
-- do what you did before, we don't have any more info
end if
put item 1 of gLastMouseLoc - item 1 of the mouseLoc into xDiff
put item 2 of gLastMouseLoc - item 2 of the mouseLoc into yDiff
-- compare horizontal and vertical difference and decide if we came from left/right/top/bottom
end mouseEnter
Though I don't know whether mouseLeave is the right one here, as I'd suppose when you move the mouse very quickly from outside the window onto the button, the card may never receive mouseEnter/mouseLeave messages. So you may have to instead make a mouseMove handler that constantly updates gLastMouseLoc *unless* the mouse is inside your field.
Cheers,
-- Uli Kusterer
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de
-- Uli Kusterer
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de