From 28273acb8b04e92d3505d7f8eeabdeef90f6dc5a Mon Sep 17 00:00:00 2001 From: LeCrapouille Date: Tue, 16 Apr 2024 14:16:40 +0200 Subject: [PATCH] WIP cleaning mouse --- src/Editor/DearImGui/Editor.cpp | 65 +++++++++++++++------------------ src/Editor/DearImGui/Editor.hpp | 21 ++++++----- 2 files changed, 40 insertions(+), 46 deletions(-) diff --git a/src/Editor/DearImGui/Editor.cpp b/src/Editor/DearImGui/Editor.cpp index 40f1b5c..ac35112 100644 --- a/src/Editor/DearImGui/Editor.cpp +++ b/src/Editor/DearImGui/Editor.cpp @@ -1205,7 +1205,7 @@ bool Editor::PetriView::isMouseReleased(ImGuiMouseButton& key) } //-------------------------------------------------------------------------- -bool Editor::PetriView::isMouseClicked(ImGuiMouseButton& key, bool& dragging) +bool Editor::PetriView::isMouseClicked(ImGuiMouseButton& key) { ImGuiIO &io = ImGui::GetIO(); if ((io.MousePos.x >= m_canvas.corners[0].x) && @@ -1216,34 +1216,40 @@ bool Editor::PetriView::isMouseClicked(ImGuiMouseButton& key, bool& dragging) if (ImGui::IsMouseClicked(ImGuiMouseButton_Middle)) { key = ImGuiMouseButton_Middle; - if (m_editor.getNode(m_mouse.position) != nullptr) - m_mouse.disable_dragging = true; - dragging = false; return true; } if (ImGui::IsMouseClicked(ImGuiMouseButton_Left)) { key = ImGuiMouseButton_Left; - dragging = false; return true; } if (ImGui::IsMouseClicked(ImGuiMouseButton_Right)) { key = ImGuiMouseButton_Right; - dragging = false; return true; } + } + + return false; +} + +//-------------------------------------------------------------------------- +bool Editor::PetriView::isMouseDraggingView(ImGuiMouseButton const& button) +{ + if (button != ImGuiMouseButton_Middle) + return false; + + // We are already displacingg node + //if (m_mouse.selection.size() != 0u) + // return false; + Node* node = m_editor.getNode(m_mouse.position); + if (node == nullptr) + { const float mouse_threshold_for_pan = grid.menu ? -1.0f : 0.0f; - if (!m_mouse.disable_dragging) + if (ImGui::IsMouseDragging(ImGuiMouseButton_Middle, mouse_threshold_for_pan)) { - if (ImGui::IsMouseDragging(ImGuiMouseButton_Middle, - mouse_threshold_for_pan)) - { - key = ImGuiMouseButton_Middle; - dragging = true; - return true; - } + return true; } } @@ -1253,7 +1259,6 @@ bool Editor::PetriView::isMouseClicked(ImGuiMouseButton& key, bool& dragging) //-------------------------------------------------------------------------- void Editor::PetriView::handleArcOrigin() { - // TODO m_marked_arcs.clear(); m_mouse.selection.clear(); // Get a place or a transition from the mouse cursor @@ -1265,7 +1270,7 @@ void Editor::PetriView::handleArcOrigin() { // We do not yet know the type of the destination node so create // intermediate information. - m_mouse.click_position = m_mouse.position; + m_mouse.clicked_at = m_mouse.position; m_mouse.arc_from_unknown_node = true; } } @@ -1295,8 +1300,6 @@ void Editor::PetriView::handleMoveNode() //-------------------------------------------------------------------------- void Editor::PetriView::handleAddNode(ImGuiMouseButton button) { - // TODO m_marked_arcs.clear(); - if (!m_editor.m_simulation.running) { // Add a new Place or a new Transition only if a node is not already @@ -1393,12 +1396,12 @@ void Editor::PetriView::handleArcDestination() if (m_mouse.to->type == Node::Type::Place) { m_mouse.from = &m_editor.m_net.addTransition( - m_mouse.click_position.x, m_mouse.click_position.y); + m_mouse.clicked_at.x, m_mouse.clicked_at.y); } else { m_mouse.from = &m_editor.m_net.addPlace( - m_mouse.click_position.x, m_mouse.click_position.y); + m_mouse.clicked_at.x, m_mouse.clicked_at.y); } action->after(m_editor.m_net); m_editor.m_history.add(std::move(action)); @@ -1476,21 +1479,11 @@ void Editor::PetriView::onHandleInput() ImGuiMouseButton button; if (ImGui::IsItemActive() && ImGui::IsItemHovered()) { - if (isMouseClicked(button, m_mouse.is_dragging)) + if (isMouseClicked(button)) { - // The 'M' key was pressed. - // Reset the state but do not add new node! - if (m_mouse.selection.size() != 0u) - { - m_mouse.from = m_mouse.to = nullptr; - m_mouse.selection.clear(); - if (button == ImGuiMouseButton_Middle) - { - return; - } - } + // TODO m_marked_arcs.clear(); - if (m_mouse.is_dragging) + if (isMouseDraggingView(button)) { ImGuiIO& io = ImGui::GetIO(); m_canvas.scrolling.x += io.MouseDelta.x; @@ -1509,7 +1502,7 @@ void Editor::PetriView::onHandleInput() if (isMouseReleased(button)) { - m_mouse.is_dragging = m_mouse.disable_dragging = false; + m_mouse.is_dragging_view = false; // The 'M' key was pressed for moving selected nodes. // Reset the state but do not add new node! @@ -1635,10 +1628,10 @@ void Editor::PetriView::drawPetriNet(Net& net, Simulation& simulation) // Show the arc we are creating drawArc(m_canvas.draw_list, m_mouse.from, m_mouse.to, - m_mouse.arc_from_unknown_node ? &m_mouse.click_position : nullptr, + m_mouse.arc_from_unknown_node ? &m_mouse.clicked_at : nullptr, origin, m_mouse.position); - // Draw critical cycle + // FIXME Draw critical cycle //for (auto& a: m_marked_arcs) // draw(*a, 255); diff --git a/src/Editor/DearImGui/Editor.hpp b/src/Editor/DearImGui/Editor.hpp index 108caf9..8616cb4 100644 --- a/src/Editor/DearImGui/Editor.hpp +++ b/src/Editor/DearImGui/Editor.hpp @@ -150,8 +150,9 @@ class Editor: public PetriNetEditor, public Application private: - bool isMouseClicked(ImGuiMouseButton& key, bool& dragging); + bool isMouseClicked(ImGuiMouseButton& key); bool isMouseReleased(ImGuiMouseButton& key); + bool isMouseDraggingView(ImGuiMouseButton const& key); void handleAddNode(ImGuiMouseButton button); void handleArcOrigin(); void handleMoveNode(); @@ -182,25 +183,25 @@ class Editor: public PetriNetEditor, public Application // ******************************************************************** //! \brief // ******************************************************************** - class MouseSelection + class MouseState { public: - //! \brief Mouse cursor position. + //! \brief Memorize the mouse cursor position when the user has moved it. ImVec2 position; - //! \brief - bool is_dragging = false; - bool disable_dragging = false; // FIXME + //! \brief Memorize the mouse cursor position when the user has clicked. + ImVec2 clicked_at; + //! \brief The user is dragging the view + bool is_dragging_view = false; //! \brief Selected origin node (place or transition) by the user when //! adding an arc. Node* from = nullptr; + // + bool arc_from_unknown_node = false; // FIXME //! \brief Selected destination node (place or transition) by the user when //! adding an arc. Node* to = nullptr; //! \brief The user has select a node to be displaced. std::vector selection; - // Ugly stuffs needed when trying to determine which node the user wants to - // create. - ImVec2 click_position; bool arc_from_unknown_node = false; } m_mouse; }; // class PetriView @@ -236,7 +237,7 @@ class Editor: public PetriNetEditor, public Application Net m_after; }; -private: +private: // FIXME m_marked_arcs (critical cycles ...) //! \brief Heper instance to find files like Linux $PATH environment variable. //! Used for example for loading font files.