Page 1 of 1
Button with image and text
Posted: Fri Dec 03, 2010 1:41 pm
by ibe
I'm trying to make a simple transparent button with an image and text below it. That's the easy part. Now I want the button to display the button label in bold when a user presses the button down (works), but if the mouse is kept down and the user moves the pointer out of the button I would want the text to revert to plain and if still keeping the mouse down when re-entering the button change it back to bold. But I can't get it to change to plain when the mouse is moved outside the button.
The script for the button:
Code: Select all
on mouseDown
set the textStyle of me to bold
end mouseDown
on mouseUp
set the textStyle of me to plain
end mouseUp
on mouseEnter
set the textStyle of me to bold
end mouseEnter
on mouseLeave
set the textStyle of me to plain
end mouseLeave
I must be missing something obvious.
Re: Button with image and text
Posted: Fri Dec 03, 2010 2:06 pm
by Klaus
Hi ibe,
you also want to handle "on mouseRelease", which is the missing part
Best
Klaus
Re: Button with image and text
Posted: Fri Dec 03, 2010 2:30 pm
by Janschenkel
The problem is that 'mouseLeave' is not sent if the mouse is down. Try the following script:
Code: Select all
local sTrackingFlag
on mouseEnter
put true into sTrackingFlag
end mouseEnter
on mouseLeave
set the textStyle of me to "plain"
put false into sTrackingFlag
end mouseLeave
on mouseUp
beep
end mouseUp
on mouseMove x,y
if sTrackingFlag then
if (x,y) is within the rectangle of me then
set the textStyle of me to "bold"
else
set the textStyle of me to "plain"
end if
end if
end mouseMove
HTH,
Jan Schenkel.
Re: Button with image and text
Posted: Fri Dec 03, 2010 2:53 pm
by Regulae
Hi there,
You have to be quick on the Forum- I see Jan has already posted a more elegant solution, but I'll post mine as well for interest:
As you’ve found, if you hold the mouse button down and leave the button, no mouseLeave is sent until you release the mouse. While the mouse is held down, mouseEnter and mouseLeaves are blocked, and as I understand it, you want the textStyle of the button to respond during the “mouse is down” state. This can be achieved by sending a “test” message, which continues to call itself for as long as the mouse is down:
Code: Select all
on mouseDown
set the textStyle of me to bold
send "TestMouse" to me in 10 milliseconds
end mouseDown
on TestMouse
if the mouse is down then
if the mouseLoc is within the rect of me then
set the textStyle of me to bold
end if
if the mouseLoc is not within the rect of me then
set the textStyle of me to plain
end if
send "TestMouse" to me in 10 milliseconds
end if
end TestMouse
This might be what you are looking for.
Regards,
Michael
Re: Button with image and text
Posted: Fri Dec 03, 2010 3:05 pm
by Klaus
Hi all,
I am still strongly convinced that a simple "mouseRelease" handler is exactly what ibe is looking for!
But why keep it simply, if we can have it complicated?

(Just kidding, no offences!)
Best
Klaus
Re: Button with image and text
Posted: Fri Dec 03, 2010 3:21 pm
by ibe
Wow guys, that was fast.
Klaus, mouseRelease won't help in this case as the problem is that the user isn't releasing the mouse button, just keeping it pressed and moving in and out of the control.
Jan and Michael thanks for your code snippets. I ended up using Jan's solution with a few tweaks and now the behavior is exactly what I wanted. For anyone interested here is the final code:
Code: Select all
local sTrackingFlag
on mouseDown
put true into sTrackingFlag
set the textStyle of me to "bold"
end mouseDown
on mouseLeave
set the textStyle of me to "plain"
put false into sTrackingFlag
end mouseLeave
on mouseUp
put false into sTrackingFlag
end mouseUp
on mouseMove x,y
if sTrackingFlag then
if (x,y) is within the rectangle of me then
set the textStyle of me to "bold"
else
set the textStyle of me to "plain"
end if
end if
end mouseMove
Thanks everyone,
Ismo
Re: Button with image and text
Posted: Fri Dec 03, 2010 3:24 pm
by Regulae
Hi Klaus,
I think what Jan and I are responding to is the section in ibe’s post:
“... but if the mouse is kept down and the user moves the pointer out of the button I would want the text to revert to plain and if still keeping the mouse down when re-entering the button change it back to bold.”
So we’re thinking of the scenario of the mouse being in the button, pressed and held down, then the mouse being moved outside and then back into the button (the mouse button being held down continuously throughout all this), the question being- how to get the textStyle of the button to alternate between bold and plain as appropriate. So, as I understand it, in this particular case the mouse isn’t actually being released. It is true, however, that mouseRelease is a very useful message to keep in mind for when the mouse is released outside the control. So it all depends on exactly what ibe is wanting.
Edit- I see that ibe has already clarified matters. Indeed, this is a high-speed forum.
Regards,
Michael
Re: Button with image and text
Posted: Fri Dec 03, 2010 4:01 pm
by Klaus
Hi all,
I plead guilty for not having read ibes posting carefully enough!
I wear sackcloth and ashes!
Best
Klaus