Skip to content

imgui glfw3 GLEQ eventhandler #3034

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

Closed
AnimatorJeroen opened this issue Feb 20, 2020 · 2 comments
Closed

imgui glfw3 GLEQ eventhandler #3034

AnimatorJeroen opened this issue Feb 20, 2020 · 2 comments
Labels

Comments

@AnimatorJeroen
Copy link

AnimatorJeroen commented Feb 20, 2020

Not really an issue, but I'd like to share a code snippet for people using imgui with GLEQ.

GLEQ is a simple (SDL like) event queue for GLFW3. It replaces all the callbacks of the glfw window, so keyboard-, textinput and scrollinput in ImGui will be lost.

To get my inputs back in dear ImGui, I made a "ProcessEvent" function, derived from: ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event)

Run this function in the while gleqNextEvent() loop:

bool ProcessEvent(GLEQevent* event)
{
	ImGuiIO& io = ImGui::GetIO();
	switch (event->type)
	{
		case GLEQ_SCROLLED:
		{
			if (event->scroll.x > 0) io.MouseWheelH += 1;
			if (event->scroll.x < 0) io.MouseWheelH -= 1;
			if (event->scroll.y > 0) io.MouseWheel += 1;
			if (event->scroll.y < 0) io.MouseWheel -= 1;
			return true;
		}
		case GLEQ_BUTTON_PRESSED:
		{
			if (event->mouse.button == 0) io.MouseDown[0] = true;
			if (event->mouse.button == 1) io.MouseDown[1] = true;
			if (event->mouse.button == 2) io.MouseDown[2] = true;
			return true;
		}
                case GLEQ_CODEPOINT_INPUT:
		{
			//io.AddInputCharactersUTF8(event->codepoint);
                        io.AddInputCharacter(event->codepoint);
			return true;
		}
		case GLEQ_KEY_PRESSED:
		case GLEQ_KEY_RELEASED:
		{
            //std::cout << (event->keyboard.mods) << std::endl;
			int key = event->keyboard.key;
			IM_ASSERT(key >= 0 && key < IM_ARRAYSIZE(io.KeysDown));
			io.KeysDown[key] = (event->type == GLEQ_KEY_PRESSED);
                        io.KeyShift = (event->keyboard.mods == 1);
			io.KeyCtrl = (event->keyboard.mods == 2);
			io.KeyAlt = (event->keyboard.mods == 4);
			io.KeySuper = (event->keyboard.mods == 8);
			return true;
		}
	}
	return false;
}
@ocornut
Copy link
Owner

ocornut commented Feb 23, 2020

Hello,

Thanks for posting it. At a quick glance it seems like mouse buttons are never marked as released?

About

It replaces all the callbacks of the glfw window, so keyboard-, textinput and scrollinput in ImGui will be lost

I think it would be more appropriate to just chain the callback.
If you installed GLEQ callbacks gleqTrackWindow() and then install the imgui glfw backend callbacks, the later will automatically call the GLEQ callbacks while handling its own stuff. You could also do it the other way.

@AnimatorJeroen
Copy link
Author

Ah that makes a lot of sense.
I tried it and got all the input back in ImGui.
So no more need for this function!
Thank you ocornut!

@ocornut ocornut closed this as completed Feb 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants