Archived

This topic is now archived and is closed to further replies.

Dean1988

Mouse-Over a Bitmap...

Recommended Posts

Hey People. It''s me...again hehe. In Allegro i''m trying to make a mouse-over work on a Bitmap, the mouse-over is working fine using this code..
if (mouse_x < 230 && mouse_x > test.x && mouse_y < 190  && mouse_y > test.x && hover == 0)
{

clear_bitmap(screen);
		draw(test.btn_hover, test.x, test.y);
		hover = 1;
		break;
	
}
BUT, What i''m not sure about is, when the mouse ISNT hovering the bitmap, i want it to change back to it''s original bitmap. I''m not sure about this, so i ask for your help, please. Thanks. Dena.

Share this post


Link to post
Share on other sites
wouldn't you just do the exact opposite?

like:

mouse_x > 230 && mouse_X < test.x

etc.

then change the pic back to the original

-btw, how do you make a code box like that? i'm new

The New N00b

[edited by - True Edge on July 25, 2003 8:19:00 PM]

Share this post


Link to post
Share on other sites
tried that, didnt work ><

to use the code box use this.
[ source]
[ /source]



[edited by - dean1988 on July 25, 2003 8:29:21 PM]

[edited by - dean1988 on July 25, 2003 8:29:46 PM]

Share this post


Link to post
Share on other sites
Welcome to the wonderful world of "Programming is not easy".

Have a variable like;

int ButtonState;

Then it can have values;

#define HIGHLIGHT 0
#define UP 1
#define DOWN 2

Then you can have a statement like

if(mouse over the button) ButtonState = HIGHLIGHT
if(mouse over button and clicked) ButtonState = DOWN
else ButtonState = UP

And THEN draw the button -- You could even have the buttonstate be an index into a BITMAP array so you could do something like

blit(screen, button[ButtonState],.....);

Share this post


Link to post
Share on other sites
Using an else statement in which you set the bitmap back to the original should work. For example,

if (mouse_x==whatever && mouse_y==whatever)
{
//change current bitmap
}
else
{
//mouse is not over the bitmap, so change it back

}



"Skepticism.... that great rot of the intellect." - V.H.
Bah, what does HE know?


Albekerky Software

Share this post


Link to post
Share on other sites
Couldn't you just do and else?


if (mouse_x < 230 && mouse_x > test.x && mouse_y < 190 && mouse_y > test.x && hover == 0)
{
clear_bitmap(screen);
draw(test.btn_hover, test.x, test.y);
hover = 1;
break;
}
else
{
clear_bitmap(screen);
draw(test.btn_default, test.x, test.y);
hover = 0;
break;
}


EDIT:
I just noticed this.
Shouldn't:
if (mouse_x < 230 && mouse_x > test.x && mouse_y < 190 && mouse_y > test.x && hover == 0)

Be:
if (mouse_x < 230 && mouse_x > test.x && mouse_y < 190 && mouse_y > test.y && hover == 0)

The test.x should be test.y?

-UltimaX-

"You wished for a white christmas... Now go shovel your wishes!"

[edited by - UltimaX on July 25, 2003 8:37:01 PM]

Share this post


Link to post
Share on other sites
If else doesn''t work, then you have an error in your routine to check whether the mouse is over the bitmap, because the else part is executed when the if statement is false.
Did you fix the error UltimaX has stated??


if ((mouse_x > 230 || mouse_x < test.x) && (mouse_y > 190 || mouse_y < test.y) && hover == 1)


should work to tell if the mouse cursor is outside the bitmap.

Share this post


Link to post
Share on other sites
Hey,

Yup i fixed that error.

And i just tried what you said...

this is the code..


if (mouse_x < 230 && mouse_x > test.x && mouse_y < 190 && mouse_y > test.y && hover == 0)
{
clear_bitmap(screen);
draw(test.btn_hover, test.x, test.y);
hover = 1;

}
else if ((mouse_x > 230 || mouse_x < test.x) && (mouse_y > 190 || mouse_y < test.y) && hover == 1)
{
clear_bitmap(screen);
draw(test.btn_norm, test.x, test.y);
hover = 0;

}


But, it works..ish...It''ll change the bitmap, BUT sometimes it will sometimes it wont :S

Any ideas if it''s the code or my comp?

Thanks.

Dean.

Share this post


Link to post
Share on other sites
Ok I'm going to try and visual this.


TestX/TestY
---------- -
| | |
| | |Height = TestY + 190
| | |
---------- -
|__________|
Width = TestX + 230

   
if((mouse_x > test.x) && (mouse_x < test.x + 230) &&
(mouse_y > test.y) && (mouse_y < test.y + 190) && hover == 0)
{
clear_bitmap(screen);
draw(test.btn_hover, test.x, test.y);
hover = 1;
break;
}
else
{
clear_bitmap(screen);
draw(test.btn_norm, test.x, test.y);
hover = 0;
break;
}


This should work.

EDIT: Forgot about the hover. (Trying to help you and I had a bug )

BTW: You don't need the hover variable. The if statement decides that. If it finds the mouse position is within the buttons bound, it will show the hover image. If not, it will show the default button image. If you want a callback function when the button is pressed, do something like this:


void OnButtonPressCallback(int& ButtonState)
{
if(ButtonState)
{
//DoButtonPress();

}
}
//

void UpdateButton(void (*OnButtonPress)(int&))
{
if((mouse_x > test.x) && (mouse_x < test.x + 230) &&
(mouse_y > test.y) && (mouse_y < test.y + 190))
{
clear_bitmap(screen);
draw(test.btn_hover, test.x, test.y);
OnButtonPress(1);
break;
}
else
{
clear_bitmap(screen);
draw(test.btn_norm, test.x, test.y);
OnButtonPress(0);
break;
}
}


Then to use it just call:

UpdateButton(OnButtonPressCallback);


-UltimaX-

"You wished for a white christmas... Now go shovel your wishes!"

[edited by - UltimaX on July 26, 2003 1:34:46 PM]

Share this post


Link to post
Share on other sites
I looked at it, and I don''t understand. The examples we gave you should work like a miracle.

Post the actual mouse/checking code.
What are you using for your mouseX and MouseY? Are you calling something like:


POINT CursorPosition;
GetCursorPos(&CursorPosition);
ScreenToClient(hWnd, &CursorPosition);

mouse_x = CursorPosition.x;
mouse_y = CursorPosition.y;


Maybe your custom mouse cursor strays from the actual coords?

-UltimaX-

"You wished for a white christmas... Now go shovel your wishes!"

Share this post


Link to post
Share on other sites
I''m not using any Custome Mouse Cursors.

In the Allegro source code, there is mouse_y and mouse_x.
And it says that they hold the x and y co-ords of the mouse, so i went along with that.

and here''s the source, for ease


http://www.geocities.com/spikeo_uk/source.zip

Bet it''s something i''m doing wrong hehe.

Thanks.

Dean.

Share this post


Link to post
Share on other sites
I don't have allegro so I can't test it, but I can give you a few pointers.

1) Take out the else if(*) , it's not needed. Just use else
2) Try the last example I gave. (The if/else one, not the mouse one)

When you check the far boundries, it should be:
Far right button edge = PositionX + Width;
Far bottom button edge = PositionY + Height;

So a boundry rect would look like:
RECT ButtonRect;
ButtonRect.left = test.x;
ButtonRect.top = test.y;
ButtonRect.right = test.x + 230; //--(ButtonWidth)
ButtonRect.bottom = test.y + 190; //--(ButtonHeight)

If you keep the rect in mind, you should have no problem.

-UltimaX-

[edited by - UltimaX on July 26, 2003 2:00:27 PM]

Share this post


Link to post
Share on other sites