Button with image and text

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

Post Reply
ibe
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 61
Joined: Sun Oct 04, 2009 12:15 pm

Button with image and text

Post by ibe » Fri Dec 03, 2010 1:41 pm

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.

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Button with image and text

Post by Klaus » Fri Dec 03, 2010 2:06 pm

Hi ibe,

you also want to handle "on mouseRelease", which is the missing part :D


Best

Klaus

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Re: Button with image and text

Post by Janschenkel » Fri Dec 03, 2010 2:30 pm

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.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

Regulae
Posts: 136
Joined: Tue Oct 20, 2009 6:05 am

Re: Button with image and text

Post by Regulae » Fri Dec 03, 2010 2:53 pm

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

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Button with image and text

Post by Klaus » Fri Dec 03, 2010 3:05 pm

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? :D
(Just kidding, no offences!)


Best

Klaus

ibe
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 61
Joined: Sun Oct 04, 2009 12:15 pm

Re: Button with image and text

Post by ibe » Fri Dec 03, 2010 3:21 pm

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

Regulae
Posts: 136
Joined: Tue Oct 20, 2009 6:05 am

Re: Button with image and text

Post by Regulae » Fri Dec 03, 2010 3:24 pm

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

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Button with image and text

Post by Klaus » Fri Dec 03, 2010 4:01 pm

Hi all,

I plead guilty for not having read ibes posting carefully enough!
I wear sackcloth and ashes! :oops:


Best

Klaus

Post Reply