Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » allegro gui

This thread is locked; no one can reply to it. rss feed Print
 1   2   3 
allegro gui
julian_boolean
Member #8,201
January 2007

Can anyone tell me if this is even near right?

1int button::logic(int msg)
2{
3 if(is_mouse_over)
4 {
5 switch(msg)
6 {
7 case MSG_LPRESS:
8 {
9 
10 if(mouse_b & 1 && mouse_status != NOT_ON_FOCUS)
11 {
12 mouse_status = MOUSE_STATUS_DOWN;
13 draw_frame = DRAW_FRAME_DOWN;
14 play_sound = PLAY_SOUND_DOWN;
15 }
16 }break;
17
18 case MSG_LRELEASE:
19 {
20 if(!(mouse_b & 1) && mouse_status == MOUSE_STATUS_DOWN)
21 {
22 mouse_status = MOUSE_STATUS_UP;
23 draw_frame = DRAW_FRAME_ON;
24 play_sound = PLAY_SOUND_ON;
25
26 return TRUE;
27 }
28 }break;
29 }
30 }
31}

Edit:

I'm guessing I'll also need to throw MSG_IDLE in there, somewhere.

CGamesPlay
Member #2,559
July 2002
avatar

Yeah, for the most part. You don't need to check mouse_b in MSG_LRELEASE, because you wouldn't have gotten the message otherwise.

Also, make sure you get both a MSG_LPRESS and a MSG_LRELEASE. Otherwise, I can hold down the mouse button over nothing, drag to over the button, and release the mouse button, and the button would feel it had been pressed.

[append]
I am too tired. I see that you already do that ::)

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

julian_boolean
Member #8,201
January 2007

How about this:

1int button::logic(int msg)
2{
3 switch(msg)
4 {
5 if(is_mouse_over)
6 {
7 case MSG_LPRESS:
8 {
9 if(mouse_b & 1 && mouse_status != NOT_ON_FOCUS)
10 {
11 mouse_status = MOUSE_STATUS_DOWN;
12 draw_frame = DRAW_FRAME_DOWN;
13 play_sound = PLAY_SOUND_DOWN;
14 }
15 }break;
16
17 case MSG_LRELEASE:
18 {
19 if(mouse_status == NOT_ON_FOCUS)
20 {
21 mouse_status = MOUSE_STATUS_UP;
22 draw_frame = DRAW_FRAME_ON;
23 play_sound = PLAY_SOUND_ON;
24 }
25
26 else if(mouse_status == MOUSE_STATUS_DOWN)
27 {
28 mouse_status = MOUSE_STATUS_UP;
29 draw_frame = DRAW_FRAME_ON;
30 play_sound = PLAY_SOUND_ON;
31
32 return TRUE;
33 }
34 }break;
35 }
36
37 case MSG_IDLE:
38 {
39 mouse_status = NOT_ON_FOCUS;
40 draw_frame = DRAW_FRAME_OFF;
41 }
42 }
43}

And what did you mean by "Also, make sure you get both a MSG_LPRESS and a MSG_LRELEASE"?.. I still don't understand how I'm suppose to use this function, like this (exit->logic(MSG_LPRESS))?

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

How about this:

Good, do the same for the MSG_LPRESS... and remove that crazy, invalid if statement.

Quote:

I still don't understand how I'm suppose to use this function, like this (exit->logic(MSG_LPRESS))?

Err, you pass it to an Allegro dialog in the proc field.
DIALOG

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

julian_boolean
Member #8,201
January 2007

Proc field eh. And which crazy invalid if statement? ;)

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

and remove that crazy, invalid if statement.

If you mean the one I think you mean, its not invalid. It is crazy though ;) just move it outside the switch...

You can mix statements in with a switch iirc. its used with loop unrolling lots.

edit, maybe I'm wrong, since Duff's Device seems to start after the first label.. Meh.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

julian_boolean
Member #8,201
January 2007

The if(is_mouse_over) you mean?

CGamesPlay
Member #2,559
July 2002
avatar

Yes. Besides, you won't get a MSG_LPRESS if the mouse is over a different control.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

julian_boolean
Member #8,201
January 2007

If I move it outside the switch how can I draw the button graphics when the user isn't touching it?

CGamesPlay
Member #2,559
July 2002
avatar

I think you should remove the if statement entirely. Why would you get a MSG_LPRESS if the mouse wasn't over the object?

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

julian_boolean
Member #8,201
January 2007

Well I just don't understand how it knows. So more need of the "mouse_status" stuff?

CGamesPlay
Member #2,559
July 2002
avatar

Allegro only calls your update function with MSG_LPRESS when the current control receives a click.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

julian_boolean
Member #8,201
January 2007

Yes but how does it know what the user is clicking on, is there some kinda bounds checking already made?

CGamesPlay
Member #2,559
July 2002
avatar

Yes, the Allegro GUI automatically handles that. It wouldn't be very useful if it didn't lessen your work load, would it? ;)

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

julian_boolean
Member #8,201
January 2007

That's sweet :D so then it SHOULD look something like this?

1int button::logic(int msg)
2{
3 switch(msg)
4 {
5 case MSG_LPRESS:
6 draw_frame = DRAW_FRAME_DOWN;
7 play_sound = PLAY_SOUND_DOWN;
8 break;
9
10 case MSG_LRELEASE:
11 draw_frame = DRAW_FRAME_ON;
12 play_sound = PLAY_SOUND_ON;
13 break;
14
15 case MSG_IDLE:
16 draw_frame = DRAW_FRAME_OFF;
17 break;
18 }
19}

Why does this look too simple/good to be true?

CGamesPlay
Member #2,559
July 2002
avatar

Not quite. MSG_IDLE will happen regardless of the mouse button's state. So that means that for 1 frame your button will have the DRAW_FRAME_DOWN image, then it will switch back to DRAW_FRAME_OFF. You need to check that !(mouse_b & 1) in the MSG_IDLE handler :)

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Steve Terry
Member #1,989
March 2002
avatar

coding doesn't have to be complicated, most great programs are written elegantly such that it is coherent and easy to understand even if you didn't code it. One step further would be to register a callback in your button so that when you click you set your variables. This makes it more flexible and not hard coded to your sound player. Then you can reuse your button for anything you need. It's really very simple as:

(*(button_callback))(params);

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

julian_boolean
Member #8,201
January 2007

That's not so bad. Will I have to make different messange handlers for different objects or can I just have one.. To rule them all.

Not really sure what you mean about the callback thing..

Dustin Dettmer
Member #3,935
October 2003
avatar

1class Foo {
2public:
3 typedef (*ButtonCallback)();
4 
5 ButtonCallback callback;
6 
7 void foo() { callback(); }
8}
9 
10bool shit = false;
11 
12void awShit()
13{
14 shit = true;
15}
16 
17main()
18{
19 Foo f;
20 f.callback = awShit;
21 f.foo();
22 shit == true
23}

julian_boolean
Member #8,201
January 2007

Thanks :D hmm.. Could someone whip up a quick example of how this should look using the dialog handler? I get the impression I'm probably not going to need as many classes.

Steve Terry
Member #1,989
March 2002
avatar

Another way to handle callbacks with C++ is to have a virtual method inside your main button widget that is called on button press. This can be overloaded in a new button class which is derived from the main button widget. Then inside the overloaded method you do what you need to do.

Quote:

bool shit;

;D that's funny man.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

nonnus29
Member #2,606
August 2002
avatar

julian_boolean
Member #8,201
January 2007

I was actually reading that awhile ago. So I'll need something like this?

// Copied from a random example

DIALOG the_dialog[] =
{
   /* (dialog proc)     (x)   (y)   (w)   (h) (fg)(bg) (key) (flags)     (d1) (d2)    (dp)                   (dp2) (dp3) */
   { d_clear_proc,        0,   0,    0,    0,   0,  0,    0,      0,       0,   0,    NULL,                   NULL, NULL  },
   { d_text_proc,         0,  10,    0,    0,   0,  0,    0,      0,       0,   0,    (void*)"enter number",  NULL, NULL  },
   { d_edit_proc,       160,  10,  160,    8,   0,  0,    0, D_EXIT,      64,   0,    (void*)the_string,      NULL, NULL  },
   { d_button_proc,     328,   6,  160,   16,   0,  0,    0, D_EXIT,       0,   0,    (void*)"OK",            NULL, NULL  },
   { d_yield_proc,        0,   0,    0,    0,   0,  0,    0,      0,       0,   0,    NULL,                   NULL, NULL  },
   { NULL,                0,   0,    0,    0,   0,  0,    0,      0,       0,   0,    NULL,                   NULL, NULL  }
};

I still don't understand how this works exactly, and how it will work with the graphics I want to use..

Dustin Dettmer
Member #3,935
October 2003
avatar

You'll have to make your own widgets if you want to use custom graphics. This is a lot of work. If you're looking to take the next step and use custom graphics I would recommend the agup library. While we're at I might as well throw in a plug for the unfinished XUI gui scripting project.

julian_boolean
Member #8,201
January 2007

I already have my own widget classes made. I just wanted to find a cleaner way to handle them without using someone elses library. Is there any way to handle my classes with the Allegro gui or do I HAVE to scrap it and use their ugly buttons?

 1   2   3 


Go to: