Skip to content

Commit

Permalink
fix idling on emscripten during animation and pinch zoom
Browse files Browse the repository at this point in the history
  • Loading branch information
wkjarosz committed Dec 28, 2023
1 parent 27e3d3a commit b735eff
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ if(portable-file-dialogs_ADDED)
endif()

set(HELLOIMGUI_WITH_GLFW ON)
CPMAddPackage("gh:pthom/hello_imgui#b1a74a7d76a84c08c1fa3a97b8dc3e2c5b5b97b3")
CPMAddPackage("gh:wkjarosz/hello_imgui#745ced9d52be601097e4b7d0837ad67a0b93db32")
if(hello_imgui_ADDED)
message(STATUS "hello_imgui library added")
endif()
Expand Down
29 changes: 20 additions & 9 deletions src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,7 @@ void SampleViewer::draw_editor()
bool SampleViewer::process_event(void *e)
{
#ifdef HELLOIMGUI_USE_SDL_OPENGL3
auto &io = ImGui::GetIO();
static bool sPinch = false;
SDL_Event *event = static_cast<SDL_Event *>(e);
switch (event->type)
Expand All @@ -1012,19 +1013,29 @@ bool SampleViewer::process_event(void *e)
// case SDL_FINGERDOWN: HelloImGui::Log(HelloImGui::LogLevel::Debug, "Got an SDL_FINGERDOWN event"); break;
case SDL_MULTIGESTURE:
{
const float cPinchZoomThreshold(0.0001f);
const float cPinchScale(80.0f);
SDL_MultiGestureEvent *m = static_cast<SDL_MultiGestureEvent *>(e);
// HelloImGui::Log(
// HelloImGui::LogLevel::Debug,
// fmt::format("Got an SDL_MULTIGESTURE event; numFingers: {}; dDist: {}", m->numFingers,
// m->dDist).c_str());
if (m->numFingers == 2 && fabs(m->dDist) >= cPinchZoomThreshold)
constexpr float cPinchZoomThreshold(0.0001f);
constexpr float cPinchScale(80.0f);
if (event->mgesture.numFingers == 2 && fabs(event->mgesture.dDist) >= cPinchZoomThreshold)
{
// HelloImGui::Log(HelloImGui::LogLevel::Debug,
// fmt::format("Got an SDL_MULTIGESTURE event; numFingers: {}; dDist: {}; x: {}, y: {}",
// event->mgesture.numFingers, event->mgesture.dDist, event->mgesture.x,
// event->mgesture.y)
// .c_str());
sPinch = true;
// Zoom in/out by positive/negative mPinch distance
float zoomDelta = m->dDist * cPinchScale;
float zoomDelta = event->mgesture.dDist * cPinchScale;
m_camera[CAMERA_NEXT].zoom = std::max(0.001, m_camera[CAMERA_NEXT].zoom * pow(1.1, zoomDelta));

// add a dummy event so that idling doesn't happen
auto g = ImGui::GetCurrentContext();
ImGuiInputEvent e;
e.Type = ImGuiInputEventType_None;
e.Source = ImGuiInputSource_Mouse;
e.EventId = g->InputEventsNextEventId++;
e.MouseWheel.MouseSource = ImGuiMouseSource_TouchScreen;
g->InputEventsQueue.push_back(e);

return true;
}
}
Expand Down

0 comments on commit b735eff

Please sign in to comment.