Jump to content

  • Log In with Google      Sign In   
  • Create Account





GUIObject: Button

Posted by JD_Rushing, 16 October 2013 · 351 views

GUI
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 <string>
#include "OpenGLContext.hpp"
#include "Command.hpp"
#include "AValue.hpp"
#include "Vector2i.hpp"
#include "Texture.hpp"
#include "FontBMP.hpp"
#include <vector>
#include <stdarg.h>
#include <stdio.h>
#include "Buffer.hpp"
#include "Mouse.hpp"


#ifndef GUIOBJECT_H
#define GUIOBJECT_H
class GUIObject
{
public:
    GUIObject()
    {
    };


    std::vector<Command> command; //outgoing command
    std::vector<Command> 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<Texture> &textures, FontBMP &aFont, bool textured, OpenGLContext *openglContext)
    {
        Draw(0, 0, textures, aFont, textured, openglContext);
        return;
    };
    virtual void Draw(int sx, int sy, std::vector<Texture> &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::vector<bool>keys, std::vector<BufferLoc> buffer)
    {
        return -2;
    };
    virtual AValue PropertyGet(std::string theProp)
    {
        AValue newValue;
        newValue.theString = (char*)"none";
        return newValue;
    };
    virtual std::vector<AValue> PropertyGetVector(std::string theProp)
    {
        std::vector<AValue> 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;
    }
};
#endif

Now 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 <string>
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<Texture> &textures, FontBMP &aFont, bool textured, OpenGLContext *openglContext);
    void Draw(int sx, int sy, std::vector<Texture> &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::vector<bool>keys, std::vector<BufferLoc> buffer);
    AValue PropertyGet(std::string theProp);
    std::vector<AValue> 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();
};
#endif

Notice 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*> 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.




December 2014 »

S M T W T F S
 123456
78910111213
141516171819 20
21222324252627
28293031   

Recent Entries

Recent Comments

PARTNERS