Skip to content

imgui glfw3 GLEQ eventhandler #3034

@AnimatorJeroen

Description

@AnimatorJeroen

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;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions