I am working on my own GUI framework using C++ and OpenGL. My experience in both is small as this is my first project using them.
In the framework I have created a window, which renders the design of the window, and then any objects that have been added to the window as a child.
I then create the window using a new class called MainMenu that inherits from my window class, I then add a new button to the MainMenu
Using:
button->SetOnClick(&MainMenu::button_Click, this);
I then add a pointer to the function, as well as the instance of the class to the buttons 'SetOnClick' method - Therefore if that button is clicked a method within the MainMenu will run.
This works great at the moment, however my issue now is that I want to add a new window called Settings, which will also inherit from the Window class
Therefore I need to make the function pointer in my button class work for any number of classes (all of type Window)
Here is the code that is currently working - but for only the MainMenu (Please note that I have stripped out a lot of methods that do not relate to the question to make it easier to read)
Window.h:
class Window
{
private:
void Initialize(WindowManager* manager, int width, int height);
public:
Window(WindowManager* manager);
Window(WindowManager* manager, int width, int height);
~Window(void);
void Render();
bool MouseMove(int x, int y);
bool MouseLBDown(int x, int y);
Children* Child() { return m_children; };
MainMenu.h:
class MainMenu : public ObjWindow
{
private:
void Initialize();
public:
MainMenu(WindowManager* manager, int width, int height) : Window(manager, width, height)
{
Initialize();
};
MainMenu(WindowManager* manager) : Window(manager)
{
Initialize();
};
~MainMenu(void);
void button_Click();
MainMenu.cpp:
void MainMenu::Initialize()
{
Button* button = new Button(Vector2D(100, -400), 100, 50);
Child()->Add(button);
button->SetText("Click Me!");
button->SetOnClick(&MainMenu::button_Click, this);
}
void MainMenu::button_Click()
{
//Do some button code
}
Button.h:
class Button
{
typedef void (MainMenu::*Function)();
Function m_onClickFunction;
MainMenu* m_window;
public:
Button(Vector2D position, int width, int height);
~Button(void);
void SetText(std::string);
std::string GetText();
void Render();
bool MouseMove(int x, int y);
bool MouseLBDown(int x, int y);
void SetOnClick(Function, MainMenu*);
Button.cpp:
Button::Button(Vector2D position, int width, int height)
{
m_onClickFunction = NULL;
}
Button::~Button(void)
{
}
bool Button::MouseMove(int x, int y)
{
return true;
}
bool Button::MouseLBDown(int x, int y)
{
if (m_body->Intercepts(x,y))
{
if (m_onClickFunction != NULL)
{
(m_window->*m_onClickFunction)();
}
}
return true;
}
void Button::SetOnClick(Function function, MainMenu* window)
{
m_onClickFunction = function;
m_window = window;
}
From researching around the internet it seems that I need to use a template so that I can handle the class as a type, I have tried fitting this in to my code, but so far I nothing has worked.
Please note that I am also new to asking question on stackoverflow, so if I am missing anything important please let me know