Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

GUIObject: Button

Sign in to follow this  


Now please correct me if I say something wrong. I am not trying to create a tutorial, but rather explain my process.

Now all my GUI (Graphical User Interface) objects inherit from GUIObject.hpp

#include #include "OpenGLContext.hpp"#include "Command.hpp"#include "AValue.hpp"#include "Vector2i.hpp"#include "Texture.hpp"#include "FontBMP.hpp"#include #include #include #include "Buffer.hpp"#include "Mouse.hpp"#ifndef GUIOBJECT_H#define GUIOBJECT_Hclass GUIObject{public: GUIObject() { }; std::vector command; //outgoing command std::vector commands; //command to copy to outgoing std::string name; std::string type; int width; int height; Vector2i position; Vector2i prevPos; bool enabled; //Is this button clickable; bool visible; Vector2i mousePosition; int debug; std::string debugS; bool created; bool grabbed; virtual void Standard() { return; }; virtual void CreateVAO() { return; } virtual void Destroy() { return; } virtual void Draw(std::vector &textures, FontBMP &aFont, bool textured, OpenGLContext *openglContext) { Draw(0, 0, textures, aFont, textured, openglContext); return; }; virtual void Draw(int sx, int sy, std::vector &textures, FontBMP &aFont, bool textured, OpenGLContext *openglContext) { return; }; virtual void DrawPick(int sx, int sy, Color4f thePick, OpenGLContext *openglContext) { return; } virtual void SetPosition(int x, int y, int Awidth, int Aheight) { position.x = x; position.y = y; width = Awidth; height = Aheight; } virtual void SetPosition(int x, int y) { SetPosition(x, y, width, height); return; } virtual bool InBounds(Vector2i pos) { return InBounds(pos.x, pos.y); } virtual bool InBounds(int x, int y) { if (x > position.x && x < position.x + width) { if (y > position.y && y < position.y + height) { return true; } } return false; } virtual bool InBounds(Vector2i pos, Vector2i upperLeft, Vector2i size) { if (pos.x > upperLeft.x && pos.x < upperLeft.x + size.x) { if (pos.y > upperLeft.y && pos.y < upperLeft.y + size.y) { return true; } } return false; } virtual bool ClickUp(Mouse& mouse) { return true; }; virtual bool ClickHeld(Mouse& mouse) { return true; }; virtual bool ClickDown(Mouse& mouse) { return true; }; virtual int KeyboardInput(std::vectorkeys, std::vector buffer) { return -2; }; virtual AValue PropertyGet(std::string theProp) { AValue newValue; newValue.theString = (char*)"none"; return newValue; }; virtual std::vector PropertyGetVector(std::string theProp) { std::vector newValue; newValue.clear(); return newValue; }; virtual void PropertySet(std::string theProp, std::string theFormat, ...) { return; }; virtual void PropertySet(std::string theProp, AValue &theValue) { return; } virtual void Commands(Command &aCommand) { return; } virtual AValue CommandR(Command &aCommand) { return AValue(); } virtual void LoseFocus() { return; }};#endifNow my Button class will Inherit the GUIObject class. This allows Button to immediately have all the variables and functions that the GUIObject has. Plus, it can override any virtual class to make one with the same name and parameters, but different code inside it.
Now if I wanted to, I could make a function abstract versus virtual. This would make any class that inherits this class forced to override the function and make its own.

#ifndef Button_h#define Button_h#include "GUIObject.hpp"#include "Quad.hpp"#include class Button: public GUIObject{ public: Quad back; std::string text; Button() { } Button(const Button& other) { if (this != &other) { Destroy(); back = other.back; } } void Standard(); void CreateVAO(); void Destroy(); void Draw(std::vector &textures, FontBMP &aFont, bool textured, OpenGLContext *openglContext); void Draw(int sx, int sy, std::vector &textures, FontBMP &aFont, bool textured, OpenGLContext *openglContext); void DrawPick(int sx, int sy, Color4f thePick, OpenGLContext *openglContext); void SetPosition(int x, int y, int Awidth, int Aheight); void SetPosition(int x, int y); bool InBounds(int x, int y); bool ClickUp(Mouse& mouse); bool ClickHeld(Mouse& mouse); bool ClickDown(Mouse& mouse); int KeyboardInput(std::vectorkeys, std::vector buffer); AValue PropertyGet(std::string theProp); std::vector PropertyGetVector(std::string theProp); void PropertySet(std::string theProp, std::string theFormat, ...); void PropertySet(std::string theProp, AValue &theValue); void Commands(Command &aCommand); AValue CommandR(Command &aCommand); void LoseFocus();};#endifNotice some of the functions are the same as GUIObject, just without the virtual part. These functions can be called even when stored in a GUIObject pointer, and will call the overridden function versus the GUIObject original function.

ComponentGUI has a vector 'std::vector guiObject' that stores a pointer to all the GUIObject created.
It has a function to Add a GUIObject:

int ComponentGui::AddControl(char *theName, char *theType, GUIObject *theObject){ int count = AddControlVar(); guiObject[count] = theObject; guiObject[count]->type = theType; guiObject[count]->name = theName; controlOrder.push_back(ControlOrder(count, theName)); theObject = NULL; return count;}Now I want to add a button so I could call this function like this:
componentGui.AddControl("Button1", "Button", new Button());Even though the parameter asked for two char* and a GUIObject pointer, I gave it two char* and a pointer to a Button. Since Button inherited GUIObject, the Button pointer can be used in place of a GUIObject pointer. This can work vice versa, but you have the type cast the GUIObject back to Button.
Button* button = (Button*)guiObject[0];Now since guiObject[0] now contains a GUIObject, I can call one of its functions.
componentGui.guiObject[0]->Standard();componentGui.guiObject[0]->Draw(0, 0, textures, aFont, textured, &openglContext);This code calls Standard(), basically sets all the parameters of the object to starting/standard.
Then the next code draws it.
Now since button has overridden Standard and Draw, the buttons version of the functions will be called.

I do want to point out, you cannot call any function or use any parameter from Button that is not contained in GUIObject while it is thought of as a GUIobject.
'Button *button = new Button()' could call any of the functions from GUIObject or Button.
'GUIObject *guiObject = new Button()' could only call the functions from GUIObject. Now any overridden functions would be called instead, but they must have same name and parameters.
Sign in to follow this  


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!