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

IMGUI touch listener #6552

Closed
plakapenka opened this issue Jun 26, 2023 · 7 comments
Closed

IMGUI touch listener #6552

plakapenka opened this issue Jun 26, 2023 · 7 comments

Comments

@plakapenka
Copy link

plakapenka commented Jun 26, 2023

imgui ver - last. (1.89.6)

i am using imgui in ndk android (touch control). how do i properly add clicks to imgui? I have my own function that catches OnTouchEvent clicks with Type push, pop, move

I am currently doing this:
in push io.MouseDown[0] = true;
in pop io.MouseDown[0] = false;

everywhere

io.MousePos.x = x;
io.MousePos.y = y;

But sometimes my buttons just don't click

Sorry, I think this thread already exists, but I couldn't find it.

full code touch

bool CGUI::OnTouchEvent(int type, bool multi, int x, int y)
{
	ImGuiIO& io = ImGui::GetIO();

	io.MousePos.x = x;
	io.MousePos.y = y;

	switch(type)
	{
		case TOUCH_PUSH:
		{
			io.MouseDown[0] = true;
			break;
		}

		case TOUCH_POP:
		{
			io.MouseDown[0] = false;
			break;
		}

		case TOUCH_MOVE:
		{
			break;
		}
	}
	return true;
}

here is my code with the button, maybe the problem is in it

void CGiftNotify::Render() {
    if(!m_bIsShow)
        return;

    if(!m_pGiftBoxTex) {
        m_pGiftBoxTex = CUtil::LoadTextureFromDB("gui", "gift_box");
    }
    ImVec2 windowSize = ImVec2(900, 400);
    ImVec2 windowPos = CGUI::GetCenterScreen(windowSize);

    ImGui::SetNextWindowSize(windowSize);
    ImGui::SetNextWindowPos(windowPos);

    ImGui::Begin("gift_give", &m_bIsShow, ImGuiWindowFlags_NoDecoration);
    {
        auto pDrawList = ImGui::GetWindowDrawList();

        // background
        ImColor colorTop(IM_COL32(21, 8, 11, 170));
        ImColor colorBottom(IM_COL32(4, 120, 126, 170));
        pDrawList->AddRectFilledMultiColor(windowPos,
                                                            ImVec2(windowPos.x + windowSize.x,
                                                                   windowPos.y + windowSize.y),
                                                            colorBottom, colorBottom,
                                                            colorTop, colorTop,
                                                            10,
                                                            ImDrawCornerFlags_All
        );

        // texture
        ImVec2 imgSize = ImVec2(210, 210);
        ImVec2 imgPos = ImVec2( windowPos.x + 20, windowPos.y + 80);
        pDrawList->AddImage((ImTextureID)m_pGiftBoxTex->raster, imgPos, ImVec2(imgPos.x + imgSize.x, imgPos.y + imgSize.y));

        // button
        ImVec2 buttSize = ImVec2(200, 80);
        ImVec2 buttPos = CGUI::GetCenterScreen(buttSize);
        buttPos.y = windowPos.y + windowSize.y - buttSize.y - 30;

        ImGui::SetCursorPos(buttPos);
        ImGui::SetCursorPos(ImVec2(200, 200));

        if (ImGui::Button("Забрать", buttSize)) {
            Log("забрать!");
            m_bIsShow = false;

            RwTextureDestroy(m_pGiftBoxTex);
            m_pGiftBoxTex = nullptr;
        }

//        ImVec2 textPos = ImVec2(imgPos.x + imgSize.x + 50, imgPos.y + 70);
//
//        ImGui::SetCursorPos(textPos);
//        CGUI::RainbowText(CGiftNotify::m_pGiftName, 35);
//
//        textPos.y += 40;
//        //ImVec2 text2pos = ImVec2(ImGui::GetCursorScreenPos().x, ImGui::GetCursorScreenPos().y + 45);
//        pDrawList->AddText(nullptr, 20, textPos, ImColor(0xc9c7c7ff), "Используйте '/roulette'");
    }
    ImGui::End();

}
@ocornut
Copy link
Owner

ocornut commented Jun 26, 2023

You should use io.AddMouseButtonEvent() and AddMousePosEvent() etc. see #4921

You can also use or refer to our backend imgui_impl_android.cpp.

@plakapenka
Copy link
Author

plakapenka commented Jun 26, 2023

the button still stops responding after showing the window 2-3 times. native imgi button. the code has not changed.
event is added ( io.AddMouseButtonEvent(ImGuiMouseButton_Left, true); ), im logging

@plakapenka
Copy link
Author

plakapenka commented Jun 26, 2023

What can be wrong?

I tried to put static text and remove elements to exclude their influence.

It's strange that button works 1 time, and then it doesn't ( ~ sometimes. )

those. the first time I show the window. the button works well and I can close it.

The second time when I show the window, it doesn't close on the button (condition doesn't work. I'm logging) in 80% of cases. The window cannot be closed. Clicking in any places, any number of clicks does not activate the button in any way.

I also tried to add
io.AddMouseSourceEvent(ImGuiMouseSource_TouchScreen);

@plakapenka
Copy link
Author

plakapenka commented Jun 26, 2023

upd.
removing the flag but ImGuiWindowFlags_NoDecoration, I realized that the window loses focus and cannot be returned

@plakapenka
Copy link
Author

@ocornut

also after changing the simple

io.MousePos.x = x;
io.MousePos.y = y;

to

io.AddMousePosEvent((float)x, (float)y);

according to your advice, I often get crash

image

@plakapenka
Copy link
Author

plakapenka commented Jun 26, 2023

@ocornut
the error was that I had a window on top of my window. I didn't think about it because it was rendered later, but I didn't take into account that it took focus on itself because of the click and didn't see it visually because of the transparency of the window. Sorry.

But the problem with the collapse is relevant. I don’t understand what I could do wrong there, because you just need to pass 2 numbers

bool CGUI::OnTouchEvent(int type, bool multi, int x, int y)
{
	if(!CKeyBoard::OnTouchEvent(type, multi, x, y)) return false;

	ImGuiIO& io = ImGui::GetIO();

	io.AddMousePosEvent((float)x, (float)y);
	switch(type)
	{
		case TOUCH_PUSH:
		{
            io.AddMouseButtonEvent(ImGuiMouseButton_Left, true);
			break;
		}

		case TOUCH_POP:
		{
			io.AddMouseButtonEvent(ImGuiMouseButton_Left, false);
			break;
		}
	}
	return true;
}

@plakapenka
Copy link
Author

plakapenka commented Jun 26, 2023

@ocornut
I think it has something to do with alignment. because the error sounds like Fatal signal 7 (SIGBUS), code 1 (BUS_ADRALN). But not always. I have -fpack-struct=1 in my keys. This also causes problems with ImGui::Scrollbar. These problems do not occur when -fpack-struct=1 is removed. I think it's worth specifying explicit alignment in imgui, although I'm not 100% sure that the problem is not with me.

i use imgi in ndk, armv7 abi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants