BACKGROUD: So I am creating an application with ImGui and as it stands I have created several files that describe the functionality of each window and then include those cpp files headers to my app main.cpp, I then call a displaywindowXX function for each window in order to populate them simpler to the ImGui demo window. But I have grouped them into a UI namespace however if say I hit a button on the menu bar and I want it to call functions specific to window x and class a what is a good/standard design pattern for accomplishing this sort of basic communication/method calling.
Below or the 3 approaches I can see working in a crude way, let me know if these are bad good or if there are better alternatives.
// mainWindow.cpp ---------------
include "window1.h"
include "window2.h"
// etc
int main()
{
// glfw window create code
//create ImGui::Frame ....
UI::showWindow1();
UI::showWindow2();
}
// window1.cpp ------------------
namespace UI
{
void showWindow1() // just generates and populates the window1
{
ImGui::Begin("window1");
//do some stuff etc
ImGui::End();
}
void createNewName(){ // do some code stuff }
//the rest of window1's methods
// windo2.cpp ------------------
namespace UI
{
void showWindow2()
{
ImGui::Begin("window2)";
static int csClicked = 0;
if (ImGui::Button("newName"))
{
UI::createNewName(); //calling method from other windows file via namespace
csClicked++;
}
}
method 2
// window1.cpp ------------
class window1 : public window
{
window1()
{
ImGui::Begin("window1"); //here is where I am starting to question this method
// will the constructor get call over and over again
ImGui::End();
} ;
~window1() { };
};
// main.cpp --------------
include "window1.h"
include "window2.h"
int main() {
// glfw window code
// ImGui frame and dockspace code
// create window1 instance which creates the ImGui window in the constructor
window1* win1 = new window1;
window2* win2 = new window2;
/* now how could I pass the pointer to win2? do I need an event handler type of setup or
maybe I was thinking a vector list that holds the pointers of the window instances
and then just pass the list to each window after initialization so they each have a
local copy of the pointer list? */
method 3 using singletons
Create each window as a singleton and simply get the instance and call its specific functions/methods .