Advertisement

Better option than using a static bool?

Started by September 16, 2024 01:37 AM
8 comments, last by taby 17 hours, 54 minutes ago

I am trying to keep track of which ImGui tab is selected. Whenever the screens tab is selected, it retrieves the data from SQLite. The problem is, there's like 6 tabs, and keeping track of all those bools would be cumbersome. Is there a different method that's better?


		ImGui_ImplOpenGL3_NewFrame();
		ImGui_ImplSDL2_NewFrame(window);
		ImGui::NewFrame();

		ImGui::Begin("Debug");


		static bool screens_tab_was_selected = false;
		static vector<screen> vs;

		if (ImGui::BeginTabBar("##tabbar"), ImGuiTabBarFlags_::ImGuiTabBarFlags_NoTooltip)
		{
			ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_None;

			if (ImGui::BeginTabBar("MyTabBar", tab_bar_flags))
			{
				if (ImGui::BeginTabItem("Screens"))
				{
					if (screens_tab_was_selected == false)
					{
						vs = retrieve_screens("test.db");

						screens_tab_was_selected = true;

						cout << "retrieving screens" << endl;
					}

					vector<char*> vcharp(vs.size(), NULL);

					for (size_t i = 0; i < vs.size(); i++)
						vcharp[i] = const_cast<char*>(vs[i].nickname.c_str());
	
					static int selected = 0;
					if (ImGui::Combo("My Combo", &selected, &vcharp[0], vcharp.size()))
					{
						//MessageBoxA(NULL, vcharp[selected], "", MB_OK);
					}


					ImGui::EndTabItem();
				}
				if (ImGui::BeginTabItem("Characters"))
				{
					screens_tab_was_selected = false;

					ImGui::Text("Characters tab");
					ImGui::EndTabItem();
				}
				if (ImGui::BeginTabItem("Portal Pairs"))
				{
					screens_tab_was_selected = false;
					ImGui::Text("Portal pairs tab");
					ImGui::EndTabItem();
				}
				if (ImGui::BeginTabItem("Cinematics"))
				{
					screens_tab_was_selected = false;
					ImGui::Text("Cinematics");
					ImGui::EndTabItem();
				}
				if (ImGui::BeginTabItem("Global booleans"))
				{
					screens_tab_was_selected = false;
					ImGui::Text("Global booleans");
					ImGui::EndTabItem();
				}
				ImGui::EndTabBar();
			}
		}

		ImGui::End();





	//	ImGui::ShowDemoWindow();





		ImGui::Render();
		ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

taby said:
keeping track of all those bools would be cumbersome. Is there a different method that's better?

If you need to track all the state, you loose the convenience of an immediate mode GUI in any case.

Personally i use some kind if configuration files to store settings on disk, and i have automated GUI generation from its data.
Settign it up looks liek this:

		void MakeConfig () // called from constructor of editor application class
		{
			config = Configuration("EditorApplication", "configs\\", 0, true);//false);//

			config.BeginScope ("Startup:", 0);
				config.SetParamI ("background threads", 15);
				config.SetParamString ("prefabDirectory", "c:\\dev\\data\\prefabs\\");
				config.SetParamString ("load staticMesh0", "sponza8k", 0, true);
				config.SetParamString ("load staticMesh1", "Armadillo_input", 0, true);
				config.SetParamString ("load staticMesh2", "sphere", 0, true);
				config.SetParamString ("load staticMesh3", "botijo", 0, true);
				config.SetParamFN ("activeRegion.minmax[0]", std::array<float,3>{-100.f, -100.0f, -100.0f}.data(), 3);
				config.SetParamFN ("activeRegion.minmax[1]", std::array<float,3>{100.f, 100.f, 100.f}.data(), 3);
				
				config.SetParamB ("start background processing at startup", 0);
				config.SetParamB ("procedural fluid scene at startup", 1);
				config.SetParamB ("fluid from file at startup", 1);
				config.SetParamB ("fluid to scene at startup", 0);
				config.SetParamB ("hm from file at startup", 0);

			config.EndScope ();

			

			config.LoadFromFile("", 0); // loads former data from disk. data is saved automatically in Configuration deconstructor
		}

This pushes the data with given default to std::vector, but then eventually loads from disk if already existing.

To show the GUI and getting the data i do this in another place:

void MainLoop (ApplicationFramework::Application &application, const matrix &cameraMatrix)
{
	ImGui::Begin("EditorApplication::MainLoop()");

	config.GUI(true); // automatically creates all the GUI elements for the data, using the data itself instead static variables
			

	activeRegion.minmax[0] = vec( // example to get some of the data
				config.GetParamF("activeRegion.minmax[0]", -600.f, 0),
				config.GetParamF("activeRegion.minmax[0]", -600.f, 1),
				config.GetParamF("activeRegion.minmax[0]", -600.f, 2));
}

That's probably as convenient as it can get. It's not more code than writing IMGUI calls where needed.

But because i need to define the data in advance during some initialization, it still feels more cumbersome than using the static variables in place.

So in practice, usually i still begin with static variables, and only if stuff becomes way too much i finally replace it with a configuration file. I know i should do this from the start, but i tend to refuse initially.
Probably the reason is that i always need to look at older code to have an example on how to setup the data. E.g. the syntax to make drop down boxes, or setting default tweaking step sizes, etc. is all compact one liners but hard to remember.
So i do copy paste from older code to have examples for those detail things.

If you do something similar, maybe make sure intellisense is good enough to use it, and no further knowledge is needed.

Also, ofc. this is limited regarding complex GUI design. I have support to group stuff using treeView for example, but i could not to tabs this way.

Advertisement

I ended up using an enum.

You're going to have so many variables, temporary or global or static, to run your system inevitably, so just make sure you keep them neatly somewhere logical, you can fit them into a structure is neat but makes them take longer to type out. Having less is more, eventually you kill your name space!

@taby To keep track of which ImGui tab is selected without using multiple boolean flags, you can use a more efficient approach. Here are a few better methods:

Using Tab Index
Using ImGui::TabItemButton()
Using ImGui::GetCurrentTabId()

Thank you for the comments, y’all.

Advertisement

Is there any way to programatically select a certain row in the combo box?

I guess this should work?

static int selected = 0;
if (program_rules) selected = program_desire;
ImGui::Combo("", &selected, ...);

Yes sir, that worked like a charm! Thanks again. Sorry that it was so obvious a fix.

Advertisement