Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / [Release] Button Handler

Author
Message
tehg00n
15
Years of Service
User Offline
Joined: 4th Jan 2009
Location: Ohio
Posted: 15th Jan 2009 21:52 Edited at: 16th Jan 2009 00:52
Ok, I know GUI isn't the funnest thing on the planet. But here is a nice OOP way to handle buttons for your login screen, or even in game GUI.

So here we go. First your going to need to create GUI.cpp and GUI.h. Paste the following code into them.

GUI.h



GUI.cpp




Now that we have the code taken care of, lets learn how to use it! First we need to make a button object. So in our Main.cpp, lets call the following code:



But lets say we want to have multiple buttons at the same time. No problem:



There you have it! I also included the ability to check for hover, click and also a little function to delete your button. Feel free to add to this and use it any way you wish. Just credit me please. Also if you add more functionality or improve this code, post your changes here so I can update this post. Also, if I made a mistake please let me know. Constructive criticism is welcome!
Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 15th Jan 2009 22:17
Why not generate IDs yourself? That's the main point of a wrapper...
AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 15th Jan 2009 23:47
Looks useful. Just a couple things...

1. You should have functions to check if the buttons was just clicked, if it was held, and if it was released. Most buttons do not take any action until the mouse button is released.

2. As Zuka said you should generate image IDs within your function; you shouldn't make the user supply one.

3. Consider making an option to make medialess buttons, that way the user can have buttons of all different sizes with different text without having dozens of images.

4. Your button will never actually draw, because you're only calling dbSprite() initially when it's created. dbSprite() must be called each loop. You should have an Update() function to handle this.

5. You should have a function to set the position of the button, in case the user wants to reposition it.

That's about all I can find atm. Nice job though.

Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 15th Jan 2009 23:49
No, you only call dbSprite() again to update it's position directly, Andrew.
tehg00n
15
Years of Service
User Offline
Joined: 4th Jan 2009
Location: Ohio
Posted: 16th Jan 2009 01:10
Yes thank you, these are all good ideas. However, I don't want the ID's to be supplied. This way you can add effects and ect to the button without having to count all your buttons just to get the ID if the sprite and image. Its not true OOP but it is a solution to create buttons easily.

I will however add functionality for repositioning and even others like changing the image, hiding and more. Thanks for the suggestions!

Login to post a reply

Server time is: 2024-09-30 15:35:39
Your offset time is: 2024-09-30 15:35:39