Skip to content

Commit

Permalink
Add right click signal to TreeView
Browse files Browse the repository at this point in the history
  • Loading branch information
Miv99 committed Feb 15, 2020
1 parent 5ee202f commit 15ef22f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/TGUI/Signal.hpp
Expand Up @@ -207,6 +207,7 @@ namespace tgui
static constexpr const char* const DoubleClicked = "DoubleClicked"; ///< A leaf node was double clicked. Optional parameter: selected node
static constexpr const char* const Expanded = "Expanded"; ///< A branch node was expanded in the tree view. Optional parameter: expanded node
static constexpr const char* const Collapsed = "Collapsed"; ///< A branch node was collapsed in the tree view. Optional parameter: collapsed node
static constexpr const char* const RightClicked = "RightClicked"; ///< A node was right clicked. Optional parameter: node below mouse
};

using BitmapButton = Button;
Expand Down
9 changes: 7 additions & 2 deletions include/TGUI/Widgets/TreeView.hpp
Expand Up @@ -291,7 +291,12 @@ namespace tgui
/// @internal
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void leftMouseReleased(Vector2f pos) override;


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @internal
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void rightMousePressed(Vector2f pos) override;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @internal
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -459,7 +464,7 @@ namespace tgui
SignalItemHierarchy onDoubleClick = {"DoubleClicked"}; ///< A leaf node was double clicked. Optional parameter: selected node
SignalItemHierarchy onExpand = {"Expanded"}; ///< A branch node was expanded in the tree view. Optional parameter: expanded node
SignalItemHierarchy onCollapse = {"Collapsed"}; ///< A branch node was collapsed in the tree view. Optional parameter: collapsed node

SignalItemHierarchy onRightClick = {"RightClicked"}; ///< A node was right clicked. Optional parameter: node below mouse

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected:
Expand Down
1 change: 1 addition & 0 deletions src/TGUI/Signal.cpp
Expand Up @@ -122,6 +122,7 @@ namespace tgui
constexpr const char* const TreeView::DoubleClicked;
constexpr const char* const TreeView::Expanded;
constexpr const char* const TreeView::Collapsed;
constexpr const char* const TreeView::RightClicked;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
36 changes: 36 additions & 0 deletions src/TGUI/Widgets/TreeView.cpp
Expand Up @@ -259,6 +259,7 @@ namespace tgui
onDoubleClick {other.onDoubleClick},
onExpand {other.onExpand},
onCollapse {other.onCollapse},
onRightClick {other.onRightClick},
m_selectedItem {other.m_selectedItem},
m_hoveredItem {other.m_hoveredItem},
m_itemHeight {other.m_itemHeight},
Expand Down Expand Up @@ -307,6 +308,7 @@ namespace tgui
std::swap(onDoubleClick, temp.onDoubleClick);
std::swap(onExpand, temp.onExpand);
std::swap(onCollapse, temp.onCollapse);
std::swap(onRightClick, temp.onRightClick);
std::swap(m_nodes, temp.m_nodes);
std::swap(m_visibleNodes, temp.m_visibleNodes);
std::swap(m_selectedItem, temp.m_selectedItem);
Expand Down Expand Up @@ -758,6 +760,38 @@ namespace tgui
m_horizontalScrollbar->leftMouseReleased(childPos);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void TreeView::rightMousePressed(Vector2f pos)
{
pos -= getPosition();

m_mouseDown = true;

float maxItemWidth = getInnerSize().x - m_paddingCached.getLeft() - m_paddingCached.getRight();
if (m_verticalScrollbar->isShown())
maxItemWidth -= m_verticalScrollbar->getSize().x;

if (FloatRect{m_bordersCached.getLeft() + m_paddingCached.getLeft(), m_bordersCached.getTop() + m_paddingCached.getTop(),
maxItemWidth, getInnerSize().y - m_paddingCached.getTop() - m_paddingCached.getBottom()}.contains(pos))
{
pos.y -= m_bordersCached.getTop() + m_paddingCached.getTop();
int selectedItem = static_cast<int>(((pos.y - (m_itemHeight - (m_verticalScrollbar->getValue() % m_itemHeight))) / m_itemHeight) + (m_verticalScrollbar->getValue() / m_itemHeight) + 1);
if (selectedItem < static_cast<int>(m_visibleNodes.size()))
{
std::vector<sf::String> hierarchy;
auto* node = m_visibleNodes[selectedItem].get();
while (node)
{
hierarchy.insert(hierarchy.begin(), node->text.getString());
node = node->parent;
}

onRightClick.emit(this, hierarchy.back(), hierarchy);
}
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void TreeView::mouseMoved(Vector2f pos)
Expand Down Expand Up @@ -854,6 +888,8 @@ namespace tgui
return onExpand;
else if (signalName == toLower(onCollapse.getName()))
return onCollapse;
else if (signalName == toLower(onRightClick.getName()))
return onRightClick;
else
return Widget::getSignal(std::move(signalName));
}
Expand Down

0 comments on commit 15ef22f

Please sign in to comment.