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

Fix large / unexpected camera movements #502

Merged
merged 3 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions src/plugins/interactive_view_control/InteractiveViewControl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ class ignition::gui::plugins::InteractiveViewControlPrivate
/// \brief Flag to indicate if mouse event is dirty
public: bool mouseDirty = false;

/// \brief Flag to indicate if hover event is dirty
public: bool hoverDirty = false;

/// \brief Flag to indicate if mouse press event is dirty
public: bool mousePressDirty = false;

/// \brief True to block orbiting with the mouse.
public: bool blockOrbit = false;

Expand Down Expand Up @@ -171,10 +177,18 @@ void InteractiveViewControlPrivate::OnRender()
return;
}

if (!this->mouseDirty)
if (!this->camera)
return;

if (!this->camera)
// hover
if (this->hoverDirty)
{
if (this->refVisual)
this->refVisual->SetVisible(false);
this->hoverDirty = false;
}

if (!this->mouseDirty)
return;

std::lock_guard<std::mutex> lock(this->mutex);
Expand Down Expand Up @@ -240,8 +254,10 @@ void InteractiveViewControlPrivate::OnRender()
{
this->target = rendering::screenToScene(
this->mouseEvent.PressPos(), this->camera, this->rayQuery);

this->viewControl->SetTarget(this->target);
this->UpdateReferenceVisual();
this->mousePressDirty = false;
}
else
{
Expand Down Expand Up @@ -272,12 +288,6 @@ void InteractiveViewControlPrivate::OnRender()
this->viewControl->Zoom(amount);
this->UpdateReferenceVisual();
}
// hover
else
{
if (this->refVisual)
this->refVisual->SetVisible(false);
}
}

this->drag = 0;
Expand Down Expand Up @@ -389,12 +399,16 @@ bool InteractiveViewControl::eventFilter(QObject *_obj, QEvent *_event)
auto pressOnScene =
reinterpret_cast<ignition::gui::events::MousePressOnScene *>(_event);
this->dataPtr->mouseDirty = true;
this->dataPtr->mousePressDirty = true;

this->dataPtr->drag = math::Vector2d::Zero;
this->dataPtr->mouseEvent = pressOnScene->Mouse();
}
else if (_event->type() == events::DragOnScene::kType)
{
if (this->dataPtr->mousePressDirty)
return QObject::eventFilter(_obj, _event);

auto dragOnScene =
reinterpret_cast<ignition::gui::events::DragOnScene *>(_event);
this->dataPtr->mouseDirty = true;
Expand Down Expand Up @@ -427,10 +441,7 @@ bool InteractiveViewControl::eventFilter(QObject *_obj, QEvent *_event)
}
else if (_event->type() == gui::events::HoverOnScene::kType)
{
gui::events::HoverOnScene *_e =
static_cast<gui::events::HoverOnScene*>(_event);
this->dataPtr->mouseDirty = true;
this->dataPtr->mouseEvent = _e->Mouse();
this->dataPtr->hoverDirty = true;
}

// Standard event processing
Expand Down
49 changes: 30 additions & 19 deletions src/plugins/minimal_scene/MinimalScene.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "MinimalScene.hh"

#include <algorithm>
#include <list>
#include <map>
#include <sstream>
#include <string>
Expand Down Expand Up @@ -67,12 +68,23 @@ class ignition::gui::plugins::IgnRenderer::Implementation
/// \brief Flag to indicate if drop event is dirty
public: bool dropDirty{false};

/// \brief Mouse event
/// \brief Current mouse event
public: common::MouseEvent mouseEvent;

/// \brief A list of mouse events
public: std::list<common::MouseEvent> mouseEvents;

/// \brief Key event
public: common::KeyEvent keyEvent;

/// \brief Max number of mouse events to store in the queue.
/// These events are then propagated to other gui plugins. A queue is used
/// instead of just keeping the latest mouse event so that we can capture
/// important events like mouse presses. However we keep the queue size
/// small on purpose so that we do not flood other gui plugins with events
/// that may be outdated.
public: const unsigned int kMaxMouseEventSize = 5u;
nkoenig marked this conversation as resolved.
Show resolved Hide resolved

/// \brief Mutex to protect mouse events
public: std::mutex mutex;

Expand Down Expand Up @@ -329,14 +341,21 @@ void IgnRenderer::Render(RenderSync *_renderSync)
void IgnRenderer::HandleMouseEvent()
{
std::lock_guard<std::mutex> lock(this->dataPtr->mutex);
for (const auto &e : this->dataPtr->mouseEvents)
{
this->dataPtr->mouseEvent = e;

this->BroadcastDrag();
this->BroadcastMousePress();
this->BroadcastLeftClick();
this->BroadcastRightClick();
this->BroadcastScroll();
this->BroadcastKeyPress();
this->BroadcastKeyRelease();
}
this->dataPtr->mouseEvents.clear();

this->BroadcastHoverPos();
this->BroadcastDrag();
this->BroadcastMousePress();
this->BroadcastLeftClick();
this->BroadcastRightClick();
this->BroadcastScroll();
this->BroadcastKeyPress();
this->BroadcastKeyRelease();
this->BroadcastDrop();
this->dataPtr->mouseDirty = false;
}
Expand Down Expand Up @@ -410,8 +429,6 @@ void IgnRenderer::BroadcastDrag()

events::DragOnScene dragEvent(this->dataPtr->mouseEvent);
App()->sendEvent(App()->findChild<MainWindow *>(), &dragEvent);

this->dataPtr->mouseDirty = false;
}

/////////////////////////////////////////////////
Expand All @@ -432,8 +449,6 @@ void IgnRenderer::BroadcastLeftClick()

events::LeftClickOnScene leftClickOnSceneEvent(this->dataPtr->mouseEvent);
App()->sendEvent(App()->findChild<MainWindow *>(), &leftClickOnSceneEvent);

this->dataPtr->mouseDirty = false;
}

/////////////////////////////////////////////////
Expand All @@ -454,8 +469,6 @@ void IgnRenderer::BroadcastRightClick()

events::RightClickOnScene rightClickOnSceneEvent(this->dataPtr->mouseEvent);
App()->sendEvent(App()->findChild<MainWindow *>(), &rightClickOnSceneEvent);

this->dataPtr->mouseDirty = false;
}

/////////////////////////////////////////////////
Expand All @@ -469,8 +482,6 @@ void IgnRenderer::BroadcastMousePress()

events::MousePressOnScene event(this->dataPtr->mouseEvent);
App()->sendEvent(App()->findChild<MainWindow *>(), &event);

this->dataPtr->mouseDirty = false;
}

/////////////////////////////////////////////////
Expand All @@ -484,8 +495,6 @@ void IgnRenderer::BroadcastScroll()

events::ScrollOnScene scrollOnSceneEvent(this->dataPtr->mouseEvent);
App()->sendEvent(App()->findChild<MainWindow *>(), &scrollOnSceneEvent);

this->dataPtr->mouseDirty = false;
}

/////////////////////////////////////////////////
Expand Down Expand Up @@ -641,7 +650,9 @@ void IgnRenderer::NewDropEvent(const std::string &_dropText,
void IgnRenderer::NewMouseEvent(const common::MouseEvent &_e)
{
std::lock_guard<std::mutex> lock(this->dataPtr->mutex);
this->dataPtr->mouseEvent = _e;
if (this->dataPtr->mouseEvents.size() >= this->dataPtr->kMaxMouseEventSize)
this->dataPtr->mouseEvents.pop_front();
this->dataPtr->mouseEvents.push_back(_e);
this->dataPtr->mouseDirty = true;
}

Expand Down