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

Added drop event #282

Merged
merged 4 commits into from
Sep 21, 2021
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
26 changes: 26 additions & 0 deletions include/ignition/gui/GuiEvents.hh
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,32 @@ namespace ignition
/// \brief Private data pointer
IGN_UTILS_IMPL_PTR(dataPtr)
};

/// \brief Event called to clone a resource, given its name as a string.
class IGNITION_GUI_VISIBLE DropOnScene : public QEvent
{
/// \brief Constructor
/// \param[in] _drop Dropped string.
/// \param[in] _dropMouse x and y coordinate of mouse position.
public: explicit DropOnScene(
const std::string &_dropText,
const ignition::math::Vector2i &_dropMouse);

/// \brief Get the text of the dropped thing on the scene
/// \return The name of the dropped thing on the scene
public: const std::string &DropText() const;

/// \brief Get X and Y position
/// \return Get X and Y position
public: const ignition::math::Vector2i &Mouse() const;

/// \brief Unique type for this event.
static const QEvent::Type kType = QEvent::Type(QEvent::MaxUser - 15);

/// \internal
/// \brief Private data pointer
IGN_UTILS_IMPL_PTR(dataPtr)
};
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions src/GuiEvents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ class ignition::gui::events::SpawnCloneFromName::Implementation
public: std::string name;
};

class ignition::gui::events::DropOnScene::Implementation
{
/// \brief The name of the dropped thing
public: std::string dropText;

/// \brief X and Y position of the mouse
public: ignition::math::Vector2i mouse;
};

using namespace ignition;
using namespace gui;
using namespace events;
Expand Down Expand Up @@ -314,3 +323,24 @@ const std::string &SpawnCloneFromName::Name() const
{
return this->dataPtr->name;
}

/////////////////////////////////////////////////
DropOnScene::DropOnScene(
const std::string &_dropText, const ignition::math::Vector2i &_dropMouse)
: QEvent(kType), dataPtr(utils::MakeImpl<Implementation>())
{
this->dataPtr->dropText = _dropText;
this->dataPtr->mouse = _dropMouse;
}

/////////////////////////////////////////////////
const std::string &DropOnScene::DropText() const
{
return this->dataPtr->dropText;
}

/////////////////////////////////////////////////
const ignition::math::Vector2i &DropOnScene::Mouse() const
{
return this->dataPtr->mouse;
}
10 changes: 10 additions & 0 deletions src/GuiEvents_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,13 @@ TEST(GuiEventsTest, SpawnCloneFromName)
EXPECT_LT(QEvent::User, toCloneName.type());
EXPECT_EQ("thingToClone", toCloneName.Name());
}

/////////////////////////////////////////////////
TEST(GuiEventsTest, DropOnScene)
{
events::DropOnScene dropOnScene("text", ignition::math::Vector2i(3, 100));

EXPECT_LT(QEvent::User, dropOnScene.type());
EXPECT_EQ(ignition::math::Vector2i(3, 100), dropOnScene.Mouse());
EXPECT_EQ("text", dropOnScene.DropText());
}
46 changes: 46 additions & 0 deletions src/plugins/minimal_scene/MinimalScene.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class ignition::gui::plugins::IgnRenderer::Implementation
/// \brief Flag to indicate if hover event is dirty
public: bool hoverDirty{false};

/// \brief Flag to indicate if drop event is dirty
public: bool dropDirty{false};

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

Expand All @@ -77,6 +80,12 @@ class ignition::gui::plugins::IgnRenderer::Implementation
/// \brief The currently hovered mouse position in screen coordinates
public: math::Vector2i mouseHoverPos{math::Vector2i::Zero};

/// \brief The currently drop mouse position in screen coordinates
public: math::Vector2i mouseDropPos{math::Vector2i::Zero};

/// \brief The dropped text in the scene
public: std::string dropText{""};

/// \brief Ray query for mouse clicks
public: rendering::RayQueryPtr rayQuery{nullptr};

Expand Down Expand Up @@ -312,6 +321,7 @@ void IgnRenderer::HandleMouseEvent()
this->BroadcastRightClick();
this->BroadcastKeyPress();
this->BroadcastKeyRelease();
this->BroadcastDrop();
this->HandleMouseViewControl();
this->dataPtr->mouseDirty = false;
}
Expand Down Expand Up @@ -354,6 +364,17 @@ void IgnRenderer::HandleKeyRelease(const common::KeyEvent &_e)
this->dataPtr->mouseEvent.SetAlt(this->dataPtr->keyEvent.Alt());
}

/////////////////////////////////////////////////
void IgnRenderer::BroadcastDrop()
{
if (!this->dataPtr->dropDirty)
return;
events::DropOnScene dropOnSceneEvent(
this->dataPtr->dropText, this->dataPtr->mouseDropPos);
App()->sendEvent(App()->findChild<MainWindow *>(), &dropOnSceneEvent);
this->dataPtr->dropDirty = false;
}

/////////////////////////////////////////////////
void IgnRenderer::BroadcastHoverPos()
{
Expand Down Expand Up @@ -516,6 +537,16 @@ void IgnRenderer::NewHoverEvent(const math::Vector2i &_hoverPos)
this->dataPtr->hoverDirty = true;
}

/////////////////////////////////////////////////
void IgnRenderer::NewDropEvent(const std::string &_dropText,
const math::Vector2i &_dropPos)
{
std::lock_guard<std::mutex> lock(this->dataPtr->mutex);
this->dataPtr->dropText = _dropText;
this->dataPtr->mouseDropPos = _dropPos;
this->dataPtr->dropDirty = true;
}

/////////////////////////////////////////////////
void IgnRenderer::NewMouseEvent(const common::MouseEvent &_e)
{
Expand Down Expand Up @@ -1000,6 +1031,14 @@ void RenderWindowItem::OnHovered(const ignition::math::Vector2i &_hoverPos)
this->dataPtr->renderThread->ignRenderer.NewHoverEvent(_hoverPos);
}

/////////////////////////////////////////////////
void RenderWindowItem::OnDropped(const QString &_drop,
const ignition::math::Vector2i &_dropPos)
{
this->dataPtr->renderThread->ignRenderer.NewDropEvent(
_drop.toStdString(), _dropPos);
}

/////////////////////////////////////////////////
void RenderWindowItem::mousePressEvent(QMouseEvent *_e)
{
Expand Down Expand Up @@ -1087,6 +1126,13 @@ void MinimalScene::OnHovered(int _mouseX, int _mouseY)
renderWindow->OnHovered({_mouseX, _mouseY});
}

/////////////////////////////////////////////////
void MinimalScene::OnDropped(const QString &_drop, int _mouseX, int _mouseY)
{
auto renderWindow = this->PluginItem()->findChild<RenderWindowItem *>();
renderWindow->OnDropped(_drop, {_mouseX, _mouseY});
}

/////////////////////////////////////////////////
void MinimalScene::OnFocusWindow()
{
Expand Down
22 changes: 22 additions & 0 deletions src/plugins/minimal_scene/MinimalScene.hh
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ namespace plugins
/// focus the window for mouse/key events
public slots: void OnFocusWindow();

/// \brief Callback when receives a drop event.
/// \param[in] _drop Dropped string.
/// \param[in] _mouseX x coordinate of mouse position.
/// \param[in] _mouseY y coordinate of mouse position.
public slots: void OnDropped(const QString &_drop,
int _mouseX, int _mouseY);

// Documentation inherited
public: virtual void LoadConfig(const tinyxml2::XMLElement *_pluginElem)
override;
Expand Down Expand Up @@ -109,6 +116,12 @@ namespace plugins
/// \param[in] _hoverPos Mouse hover screen position
public: void NewHoverEvent(const math::Vector2i &_hoverPos);

/// \brief New hover event triggered.
/// \param[in] _dropText Text dropped on the scene
/// \param[in] _dropPos Mouse drop screen position
public: void NewDropEvent(const std::string &_dropText,
const math::Vector2i &_dropPos);

/// \brief Handle key press event for snapping
/// \param[in] _e The key event to process.
public: void HandleKeyPress(const common::KeyEvent &_e);
Expand All @@ -135,6 +148,9 @@ namespace plugins
/// \brief Broadcasts a key release event within the scene
private: void BroadcastKeyRelease();

/// \brief Broadcasts a drop event within the scene
private: void BroadcastDrop();

/// \brief Broadcasts a key press event within the scene
private: void BroadcastKeyPress();

Expand Down Expand Up @@ -292,6 +308,12 @@ namespace plugins
/// the render window.
public: void OnHovered(const ignition::math::Vector2i &_hoverPos);

/// \brief Callback when receives a drop event.
/// \param[in] _drop Dropped string.
/// \param[in] _dropPos x coordinate of mouse position.
public: void OnDropped(const QString &_drop,
const ignition::math::Vector2i &_dropPos);

/// \brief Set if sky is enabled
/// \param[in] _sky True to enable the sky, false otherwise.
public: void SetSkyEnabled(const bool &_sky);
Expand Down
8 changes: 8 additions & 0 deletions src/plugins/minimal_scene/MinimalScene.qml
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,12 @@ Rectangle {
width = Qt.binding(function() {return parent.parent.width})
height = Qt.binding(function() {return parent.parent.height})
}

DropArea {
anchors.fill: renderWindow

onDropped: {
MinimalScene.OnDropped(drop.text, drag.x, drag.y)
}
}
}