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

How's this! And will I be able to use the same function for any kinda widget, like radio buttons or text fields?

1int button::is_clicked(void)
2{
3 if(d->flags & D_DISABLED)
4 {
5 draw_frame = DRAW_FRAME_DISABLED
6 }
7 
8 else if(d->flags & D_SELECTED)
9 {
10 draw_frame = DRAW_FRAME_DOWN;
11 play_sound = PLAY_SOUND_DOWN;
12 
13 return TRUE;
14 }
15 
16 else if(d->flags & D_GOTMOUSE)
17 {
18 draw_frame = DRAW_FRAME_ON;
19 play_sound = PLAY_SOUND_ON;
20 }
21 
22 else if(d->flags & D_GOTFOCUS)
23 {
24 // ??
25 }
26 
27 else
28 {
29 draw_frame = DRAW_FRAME_OFF;
30 }
31 
32 return FALSE;
33 
34 // what the hell is this
35 drawskinnedrect(wnd, &buttons.rect[box], d->x, d->y, d->x + d->w, d->y + d->h);
36}

Steve Terry
Member #1,989
March 2002
avatar

... is that valid code???

___________________________________
[ 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

/bawl.. What am I doing wrong?

Steve Terry
Member #1,989
March 2002
avatar

Ok what the hell is your button class? It keeps changing but it still makes no sense. You basically copied my code I gave you and pasted it into your is_clicked method but the code I gave you was for my GUI as a demonstration on how you should handle things. It was not to be taken literally. Your button has an is_clicked method but the code you have inside it has nothing to do with it being clicked.. it's more of a draw method and yet you still use play_sound inside of it which also makes no sense. You keep hanging onto your class, we're just trying to show you how you can make pretty buttons using the Allegro built in GUI. You can pretty much take the Allegro GUI and extend the procs to make pretty nice looking GUI elements. NAS does pretty much that and it extends all the procs to have more functionality. It's up to you how you want to your buttons but the thread is labeled Allegro GUI so I assumed you wanted to use the Allegro GUI in which case your class is irrelevant and your code will need to be rewritten.

___________________________________
[ 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

This is just getting depressing.. All I wanted was something to handle the different states for my widgets so I wouldn't have to keep rewriting the (near) same logic in each widget class, while keeping my own graphics and not using libraries other then Allegro to do it. I thought the Allegro GUI could do that, well it seems like it could. My class hasn't changed yet, I just copied the code given to me and rewrote it to the way I thought it works, then reposted it to see if I was even on the right track. :P

This is what I'm using now:

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

Steve Terry
Member #1,989
March 2002
avatar

Ok you can use your button class but it's not that flexible, I mean you can't use your button for anything else than playing a sound because that's the variable it's setting. You couldn't, for example, use your button to change the color of a box or something. You could also use the Allegro GUI which has the functionality you need but it's a bit ugly but I showed you ways to make it less ugly. Unfortunately to use the Allegro GUI you would have to rewrite your code. That said I would stick with what you have at the moment, you just need to break out your methods a bit and possibly use callbacks or overloaded methods to drive your play_sound variable.

___________________________________
[ 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 all I want it to do, just draw and play a sound, or return TRUE if I want to use the function outside the class for other things. Before, everyone kept saying that I needed some kinda event handler for all my widgets and since then I've been trying to do that without much luck.

I suppose I could just try to write things like WIDGET_FOCUSED, WIDGET_PRESSED, etc myself.

ImLeftFooted
Member #3,935
October 2003
avatar

A GUI is a pretty advanced and complicated project. Maybe make your user interface a bit simpler this time around and improve with your next game?

julian_boolean
Member #8,201
January 2007

Because I'd just rather start learning it now while I'm at it since I already know how the code I'm using works throughout.

And yeah it can be complicated, depending on how versatile you want it to be. I want it to be pretty specific though, for the most part.

Steve Terry
Member #1,989
March 2002
avatar

Hang on I'll whip up a simple widget and button class... I was doing so but accidentally hit the wrong keyboard button and lost my friggin post >:E

___________________________________
[ 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

:) Thanks a bunch, hehe I've done that a lot before, which is why I started just writing it in a text file instead of the actual post, then just copy and paste it over.

Steve Terry
Member #1,989
March 2002
avatar

1#define W_SELECTED 1
2#define W_DISABLED 2
3#define W_GOTMOUSE 4
4 
5class widget
6{
7 public:
8 int x, y, w, h; // dimensions
9 unsigned int flags; // flags
10 void widget(int x, int y, int w, int h); // default constructor
11 virtual void ~widget(); // virtual destructor
12 bool mouse_over(); // true if mouse_over
13 void enable(); // enables a widget
14 void disable(); // disables a widget
15 virtual void init(); // method to do some initialization
16 virtual void draw(BITMAP *b); // method to draw
17 virtual void idle(); // method to do stuff during idle
18 virutal bool onKeyPressed(int key); // method to react to a key pressed (returns true if accepted key press)
19};
20 
21void widget::widget(int x, int y, int w, int h)
22{
23 this->x = x;
24 thix->y = y;
25 this->w = w;
26 this->h = h;
27 this->flags = 0;
28 init();
29}
30 
31bool widget::mouse_over()
32{
33 return flags & W_GOTMOUSE;
34}
35 
36void widget::idle()
37{
38 if(mouse_x >= x && mouse_x <= x + w && mouse_y >= y && mouse_y <= y + h)
39 flags |= W_GOTMOUSE;
40 else
41 flags &= ~W_GOTMOUSE;
42}
43 
44void widget::enable()
45{
46 flags &= ~W_DISABLED;
47}
48 
49void widget::disable()
50{
51 flags |= W_DISABLED;
52}
53 
54class button : public widget
55{
56 public:
57 void button(int x, int y, int w, int h); // default constructor
58 bool clicked(); // clicked method
59 virtual void ~button(); // virtual destructor
60 virtual void onButtonClickedCallback(); // clicked callback
61};
62 
63void button::button(int x, int y, int w, int h)
64{
65 widget::widget(x, y, w, h);
66}
67 
68void button::idle()
69{
70 // Do some idle stuff
71}
72 
73void button::draw(BITMAP *b)
74{
75 int draw_frame;
76 if(flags & W_DISABLED)
77 {
78 draw_frame = DRAW_FRAME_DISABLED;
79 }
80 else
81 {
82 if(widget::mouse_over())
83 {
84 if(!mouse_b & 1)
85 draw_frame = DRAW_FRAME_HIGHLIGHTED;
86 else if(mouse_b & 1)
87 draw_frame = DRAW_FRAME_PRESSED;
88 }
89 else
90 draw_frame = DRAW_FRAME_OFF;
91 }
92 // draw the button using draw_frame
93}
94 
95bool button::clicked()
96{
97 if(flags & D_DISABLED)
98 return false;
99 if(widget::mouse_over())
100 {
101 if(mouse_b & 1)
102 {
103 onButtonClickedCallback();
104 return true;
105 }
106 }
107 return false;
108}
109 
110class mySoundButton : button
111{
112 private:
113 SOUND *my_sound;
114 public:
115 void mySoundButton(int x, int y, int w, int h);
116 virtual void ~mySoundButton();
117}
118 
119void mySoundButton::mySoundButton()
120{
121 widget::widget(x, y, w, h);
122 my_sound = Load_Sound("my_sound.wav");
123}
124 
125void mySoundButton::~mySoundButton()
126{
127 if(my_sound != NULL)
128 {
129 Unload_Sound(my_sound);
130 my_sound = NULL;
131 }
132}
133 
134void mySoundButton::onButtonClickedCallback()
135{
136 ASSERT(my_sound);
137 Play_Sound(my_sound);
138}

I made three classes, one is the base widget class. The widget class is what every other GUI object is derived from. The next class is a base button class, all it is concerned about is doing a few methods essential to a button. The third class is a class derived from button which overloads a few methods and utilizes the callback to play a sound when the button is pressed. Please use this code only as a template, I have not compiled it and it probably won't work correctly but it should give you a direction. If you notice all methods do what their names imply and nothing more. They each do a small part. This code is not optimized either but as I said it's just to give a general idea.

[edit]
Also note that this widget class doesn't use messages but instead virtual methods to do the drawing, idling, and whatnot. You can have a message handler as part of the base widget I just did it the lazy way :P
[/edit]

___________________________________
[ 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

Looks very promising to me, going to look it over before bed (stupid work in the morning) :P

Thanks a million for all your help, and everyone elses help (again) hehe tomorrow whenever I get home I'll try to fool around with it for awhile and will certainly give a heads up if I run into any problems, but I understand this a lot more then the Allergo gui stuff. ;)

Steve Terry
Member #1,989
March 2002
avatar

Why would you do that? You are making confusing methods again. set_enabled sets a variable wheras enable acts on that variable? Why not just have a method called set_enable(bool enable) that takes true or false and enables or disables the widget there. No need for an extra method that is seemingly pointless.

[edit]
aww you erased your post :P
[/edit]

___________________________________
[ 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

Sorry hehe erased it after looking it over for a couple seconds. Thanks again though. ;)

Steve Terry
Member #1,989
March 2002
avatar

Sorry I made some minor tweaks to the widget and button classes because I could ;D

___________________________________
[ 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

Hmm.. The button is being displayed properly, but it doesn't seem to be doing the "clicked" logic. Also, what does |= and &= mean?

Johan Halmén
Member #1,550
September 2001

I've tried to read this thread some times, but can't figure out what you want to do. Do you want to create your own gui system? Or do you just want to have a button in your game? If the former, I guess it's a bit hard, if you still learn about &= and |=. If the latter, Allegro gui should do the job.

a = a & b;
is same as
a &= b;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

julian_boolean
Member #8,201
January 2007

I just wanted an event/message handler using only Allegro. What is the difference between & and && anyway?

Steve Terry
Member #1,989
March 2002
avatar

&& is an AND comparison operator whereas & is a binary AND operator. For instance you would say if(someval == 2 && someotherval > 0) to only trigger the if statement only if both statements are true. Binary operations are completely different:

1#define THIS 1
2#define THAT 2
3#define OTHER 4
4char someval = 0;
5someval |= THIS; // someval = 0x01 - sets bit 1
6someval |= THAT; // someval = 0x03 - sets bit 2
7someval |= OTHER; // someval = 0x07 - sets bit 3
8someval &= ~THAT; // someval = 0x05 - unsets bit 2
9someval ^= THAT; // someval = 0x07 - toggles bit 2
10if(someval & THAT) // true if bit 2 is set
11{
12 // do something
13}
14if(someval & (THIS | THAT)) // true if bit 1 and bit 2 are set
15{
16 // do something
17}

I'm using the operations to toggle bits in the flags variable. Which by posting this I realize a flaw in my W_* enumeration. You need each state to be a power of two. The changes would be:

#define W_DISABLED   1
#define W_GOTMOUSE   2
#define W_SELECTED   4

___________________________________
[ 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

Still no luck. :/ No idea why it's not working for me, doesn't seem to like those W_'s.

Steve Terry
Member #1,989
March 2002
avatar

Allegro's defines are the same to mine, no reason it wouldn't work. Again I never said the code would work right off the bat, it's just there as an example so you can understand how things work. How are you using the class? Just stating it doesn't work doesn't tell me you are doing something wrong elsewhere.

___________________________________
[ 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

I just took functions from the code you did and stuck it in my own classes, modifying them slightly.

1 void widget::logic()
2 {
3 if(flag &W_DISABLED)
4 {
5 draw_frame = DRAW_FRAME_DISABLED
6 }
7 
8 else
9 {
10 if(flag &W_GOTMOUSE)
11 {
12 if(!mouse_b & 1)
13 {
14 draw_frame = DRAW_FRAME_ON;
15 }
16 
17 else if(mouse_b & 1)
18 {
19 play(); // plays a sound when widget is pressed
20 draw_frame = DRAW_FRAME_DOWN;
21 }
22 }
23 
24 else
25 {
26 draw_frame = DRAW_FRAME_OFF;
27 }
28 }
29 }
30 
31 void widget::draw(BITMAP *dbuff)
32 {
33 if(draw_frame == DRAW_FRAME_ON)
34 {
35 if(on_bitmap != NULL)
36 {
37 acquire_screen();
38 stretch_sprite(dbuff, on_bitmap, x_pos, y_pos, w_pos, h_pos);
39 release_screen();
40 }
41 }
42
43 if(draw_frame == DRAW_FRAME_OFF)
44 {
45 if(off_bitmap != NULL)
46 {
47 acquire_screen();
48 stretch_sprite(dbuff, off_bitmap, x_pos, y_pos, w_pos, h_pos);
49 release_screen();
50 }
51 }
52
53 if(draw_frame == DRAW_FRAME_DOWN)
54 {
55 if(down_bitmap != NULL)
56 {
57 acquire_screen();
58 stretch_sprite(dbuff, down_bitmap, x_pos, y_pos, w_pos, h_pos);
59 release_screen();
60 }
61 }
62 
63 if(draw_frame == DRAW_FRAME_DISABLED)
64 {
65 if(disable_bitmap != NULL)
66 {
67 acquire_screen();
68 stretch_sprite(dbuff, disabled_bitmap, x_pos, y_pos, w_pos, h_pos);
69 release_screen();
70 }
71 }
72 }
73 
74//=================================================================
75 
76 bool button::is_clicked()
77 {
78 if(flag &W_DISABLED)
79 {
80 return false;
81 }
82 
83 if(flag &W_GOTMOUSE)
84 {
85 if(mouse_over()) // i want this to be if(flag &W_PRESSED)
86 {
87 return true;
88 }
89 }
90 
91 return false;
92 }

Steve Terry
Member #1,989
March 2002
avatar

I don't know... you just aren't getting the picture here. I'm not sure if I can even help you. Take a closer look at my code instead of copying and pasting and get a good grasp of the concepts first. I give up.

___________________________________
[ 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

Will do.. I'm sorry for bugging so much, going to just work with what you did until it works for me! Thank you. :)

 1   2   3 


Go to: