Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scaling imgui widget sizes with the window size #8124

Open
xRogl opened this issue Nov 2, 2024 · 0 comments
Open

Scaling imgui widget sizes with the window size #8124

xRogl opened this issue Nov 2, 2024 · 0 comments

Comments

@xRogl
Copy link

xRogl commented Nov 2, 2024

Version/Branch of Dear ImGui:

Version 1.XX, Branch: XXX (master/docking/etc.)

Back-ends:

imgui_impl_XXX.cpp + imgui_impl_XXX.cpp

Compiler, OS:

Windows 10 + MSVC 2022

Full config/build information:

No response

Details:

I am using imgui with SFML and im wondering if there is any automated way to set all the sizes and positions in reference to the window size. In sfml for instance when you setup the view and have any sprites rendering on the screen, after you resize the window, the sprites also resize accordingly. In imgui it seems that the widget sizes stay static no matter the window size. I am trying to avoid the problem where the ui appears different in size and positions on different screen resolutions, for example when i setup my widgets on 1920x1080, then all the widgets would appear smaller in 4k resolution. Is there any better way to do it than i did it here with the scaling factor? Its kinda painful to manually multiply everything by the scaling factor when you have a lot of widgets (i left the code that you can copy and run if you have imgui linked with sfml. If you dont have thos linked then just skip to the TLDR part marked with the comments)

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

#include <sfml/Graphics.hpp>
#include "imgui.h"
#include "imgui-SFML.h"


int main()
{
	sf::VideoMode desktop = sf::VideoMode::getDesktopMode();
	sf::ContextSettings settings;
	settings.antialiasingLevel = 8;
	sf::RenderWindow windowME(sf::VideoMode(desktop.width / 2, desktop.height / 2), "Map Editor Launcher", sf::Style::Default, settings);
	sf::View ui;

	const float targetWidth = 960.0f;
	const float targetHeight = 540.0f;

	ui = sf::View(sf::FloatRect(0, 0, 960, 540));
	
	sf::Vector2f scalingFactor;

	sf::Clock deltaClock;
	
	ImGui::SFML::Init(windowME);

	char buf[256] = "";
	
	windowME.setActive();
	while (windowME.isOpen() == true)
	{
		scalingFactor = sf::Vector2f(windowME.getSize().x / targetWidth, windowME.getSize().y / targetHeight);
		sf::Event e;
		while (windowME.pollEvent(e) == true)
		{
			ImGui::SFML::ProcessEvent(e);
			if (e.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
			{
				windowME.close();
			}
		}

		if (windowME.hasFocus() == true)
		{
			ImGui::SFML::Update(windowME, deltaClock.restart());
			ImGui::SetNextWindowPos(ImVec2(0, 0), ImGuiCond_Always);
			ImGui::SetNextWindowSize(ImVec2(windowWidth, windowHeight));
			ImGui::Begin("##InvisibleWindow", NULL, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar);
			
			//TLDR (scaling factor's value above the event loop)-----------------------------
			ImGui::SetNextItemWidth(100.0f * scalingFactor.x);
			ImGui::SetCursorPos(ImVec2(25 * scalingFactor.x, 70 * scalingFactor.y));
                        //-------------------------------------------------------------------------------
			ImGui::InputText("Enter Text", buf, IM_ARRAYSIZE(buf));
			ImGui::End();
			
						windowME.clear(sf::Color(200, 200, 200, 255));

			windowME.setView(ui);

			//drawing stuff here
			
			ImGui::SFML::Render(windowME);

			windowME.display();
		}
	}
	ImGui::SFML::Shutdown();
}
@xRogl xRogl changed the title Scaling imgui widgets sizes with the window size Scaling imgui widget sizes with the window size Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant