Skip to content

Commit

Permalink
refactor(GraphView): extract struct Selection.h 2/2
Browse files Browse the repository at this point in the history
  • Loading branch information
berdal84 committed Nov 29, 2024
1 parent 3cfb476 commit e9dac64
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 157 deletions.
2 changes: 1 addition & 1 deletion src/ndbl/gui/Action.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace ndbl
using Action_ToggleFolding = Action<Event_ToggleFolding>;
using Action_SelectNext = Action<Event_SelectNext>;
using Action_ToggleIsolate = Action<Event_ToggleIsolationFlags>;
using Action_SelectionChange = Action<Event_SelectionChange>;
using Action_SelectionChange = Action<Event_GraphViewSelectionChanged>;
using Action_MoveGraph = Action<Event_MoveSelection>;

// 2) Advanced actions (custom events)
Expand Down
6 changes: 3 additions & 3 deletions src/ndbl/gui/Event.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ namespace ndbl

struct EventPayload_SelectionChange
{
Selection new_selection;
Selection old_selection;
GraphView* graph_view;
// Selection old_selection; was unused
};
using Event_SelectionChange = tools::Event<EventID_SELECTION_CHANGE, EventPayload_SelectionChange>;
using Event_GraphViewSelectionChanged = tools::Event<EventID_SELECTION_CHANGE, EventPayload_SelectionChange>;

struct EventPayload_CreateNode
{
Expand Down
5 changes: 5 additions & 0 deletions src/ndbl/gui/FileView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ void FileView::draw(float dt)
// 1) Draw History Bar
// 2) Draw Text and Graph Editors

clear_overlay();
Condition condition_flags = m_file->graph().view()->selection().empty()
? Condition_ENABLE_IF_HAS_NO_SELECTION
: Condition_ENABLE_IF_HAS_SELECTION;
refresh_overlay( condition_flags );

// 1)
if (ImGui::IsMouseReleased(0))
Expand Down
98 changes: 41 additions & 57 deletions src/ndbl/gui/GraphView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,7 @@ bool GraphView::draw(float dt)

// Animate style
ImGuiEx::WireStyle style = default_wire_style;
if (is_selected(node_view_out) ||
is_selected(node_view_in))
if ( m_selection.contains( node_view_out ) || m_selection.contains( node_view_in ) )
{
style.color.w *= wave(0.5f, 1.f, (float) App::get_time(), 10.f);
}
Expand Down Expand Up @@ -625,46 +624,15 @@ void GraphView::frame_nodes(FrameMode mode )

case FRAME_SELECTION_ONLY:
{
if ( !m_selected.node.empty())
frame_views(m_selected.node, CENTER);
if ( !m_selection.node().empty())
frame_views(m_selection.node(), CENTER);
break;
}
default:
VERIFY(false, "unhandled case!");
}
}

void GraphView::set_selected(const Selection& views, SelectionMode mode )
{
Selection curr_selection = m_selected;
if ( mode == SelectionMode_REPLACE )
{
m_selected.remove_all();
for(auto& each : curr_selection.node )
each->set_selected(false);
}

m_selected.add( views ) ;

EventPayload_SelectionChange event{m_selected, curr_selection };
get_event_manager()->dispatch<Event_SelectionChange>(event);
}

const Selection& GraphView::selected() const
{
return m_selected;
}

bool GraphView::is_selected(NodeView* view) const
{
return m_selected.has(view);
}

bool GraphView::selection_empty() const
{
return m_selected.empty();
}

void GraphView::_on_graph_change()
{
m_physics_dirty = true;
Expand Down Expand Up @@ -729,9 +697,9 @@ void GraphView::draw_create_node_context_menu(CreateNodeCtxMenu& menu, SlotView*

void GraphView::drag_state_enter()
{
for ( NodeView* view : m_selected.node )
for ( NodeView* view : m_selection.node() )
view->set_pinned();
for ( ScopeView* view : m_selected.scope )
for ( ScopeView* view : m_selection.scope() )
view->set_pinned();
}

Expand All @@ -750,7 +718,7 @@ void GraphView::drag_state_tick()

default:
{
for (NodeView* view : m_selected.node )
for (NodeView* view : m_selection.node() )
view->spatial_node().translate( delta );
break;
}
Expand Down Expand Up @@ -833,7 +801,10 @@ void GraphView::cursor_state_tick()
views.push_back(view);
}
// Replace selection
set_selected(views);
m_selection.clear();
for(NodeView* view : views)
m_selection.append(view ) ;
get_event_manager()->dispatch<Event_GraphViewSelectionChanged>({this});
}

ImGui::Separator();
Expand Down Expand Up @@ -895,12 +866,6 @@ void GraphView::cursor_state_tick()
return;
}

// TODO: handle remove!
// Add/Remove/Replace selection
SelectionMode selection_flags = SelectionMode_REPLACE;
if (ImGui::IsKeyDown(ImGuiKey_LeftCtrl) || ImGui::IsKeyDown(ImGuiKey_RightCtrl))
selection_flags = SelectionMode_ADD;

switch (m_hovered.type)
{
case ViewItemType_SLOT:
Expand All @@ -920,14 +885,15 @@ void GraphView::cursor_state_tick()

case ViewItemType_NODE:
{
bool select_hovered = false;
if ( ImGui::IsMouseClicked(1) )
{
m_focused = m_hovered;
ImGui::OpenPopup(CONTEXT_POPUP);
}
else if (ImGui::IsMouseReleased(0) )
{
set_selected(m_hovered.nodeview, selection_flags);
select_hovered = true;
m_focused = m_hovered;
}
else if (ImGui::IsMouseDoubleClicked(0))
Expand All @@ -937,11 +903,19 @@ void GraphView::cursor_state_tick()
}
else if (ImGui::IsMouseDragging(0))
{
select_hovered = true;
m_focused = m_hovered;
if (!m_hovered.nodeview->selected())
set_selected(m_hovered.nodeview);
m_state_machine.change_state(DRAG_STATE);
}

if ( select_hovered )
{
const bool ctrl_pressed = ImGui::IsKeyDown(ImGuiKey_LeftCtrl) || ImGui::IsKeyDown(ImGuiKey_RightCtrl);
if ( !ctrl_pressed ) // replace when Ctrl is pressed
m_selection.clear();
m_selection.append( m_hovered.nodeview ) ;
get_event_manager()->dispatch<Event_GraphViewSelectionChanged>({this});
}
break;
}

Expand All @@ -961,9 +935,10 @@ void GraphView::cursor_state_tick()

case ViewItemType_SCOPE:
{
bool select_hovered = false;
if (ImGui::IsMouseReleased(0) )
{
set_selected(m_hovered.scopeview, selection_flags);
select_hovered = true;
m_focused = m_hovered;
}
else if (ImGui::IsMouseClicked(1))
Expand All @@ -974,9 +949,18 @@ void GraphView::cursor_state_tick()
else if ( ImGui::IsMouseDragging(0) )
{
m_focused = m_hovered;
set_selected(m_hovered.scopeview);
select_hovered = true;
m_state_machine.change_state(DRAG_STATE);
}

if ( select_hovered )
{
const bool ctrl_pressed = ImGui::IsKeyDown(ImGuiKey_LeftCtrl) || ImGui::IsKeyDown(ImGuiKey_RightCtrl);
if ( !ctrl_pressed ) // replace when Ctrl is pressed
m_selection.clear();
m_selection.append( m_hovered.scopeview ) ;
get_event_manager()->dispatch<Event_GraphViewSelectionChanged>({this});
}
break;
}

Expand All @@ -985,7 +969,7 @@ void GraphView::cursor_state_tick()
if ( ImGui::IsWindowHovered(ImGuiFocusedFlags_ChildWindows) )
{
if (ImGui::IsMouseClicked(0))
set_selected({}, SelectionMode_REPLACE); // Deselect All (Click on the background)
m_selection.clear(); // Deselect All (Click on the background)
else if (ImGui::IsMouseReleased(0))
m_focused = {};
else if (ImGui::IsMouseClicked(1))
Expand Down Expand Up @@ -1098,17 +1082,17 @@ void GraphView::roi_state_tick()

if (ImGui::IsMouseReleased(0))
{
const bool ctrl_pressed = ImGui::IsKeyDown(ImGuiKey_LeftCtrl) || ImGui::IsKeyDown(ImGuiKey_RightCtrl);
if ( !ctrl_pressed )
m_selection.clear();

// Select the views included in the ROI
std::vector<NodeView*> nodeview_in_roi;
for ( Node* node : graph()->nodes() )
if ( auto v = node->get_component<NodeView>() )
if (Rect::contains(roi, v->get_rect()))
nodeview_in_roi.emplace_back(v);
m_selection.append(v);

bool control_pressed = ImGui::IsKeyDown(ImGuiKey_LeftCtrl) ||
ImGui::IsKeyDown(ImGuiKey_RightCtrl);
SelectionMode flags = control_pressed ? SelectionMode_ADD : SelectionMode_REPLACE;
set_selected(nodeview_in_roi, flags);
get_event_manager()->dispatch<Event_GraphViewSelectionChanged>({this});

m_state_machine.exit_state();
}
Expand Down
9 changes: 4 additions & 5 deletions src/ndbl/gui/GraphView.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <map>
#include <string>
#include <functional>
#include <vector>

#include "tools/gui/ViewState.h" // base class
#include "tools/core/reflection/reflection"
Expand Down Expand Up @@ -50,11 +51,10 @@ namespace ndbl
bool draw(float dt);
void add_action_to_node_menu(Action_CreateNode* _action);
void frame_nodes(FrameMode mode );
bool selection_empty() const;
void reset(); // unfold and frame the whole graph
bool has_an_active_tool() const;
void set_selected(const Selection&, SelectionMode = SelectionMode_REPLACE);
const Selection& selected() const;
Selection& selection() { return m_selection; }
const Selection& selection() const { return m_selection; }
void reset_all_properties();
Graph* graph() const;
void add_child(NodeView*);
Expand All @@ -65,7 +65,7 @@ namespace ndbl
CreateNodeCtxMenu m_create_node_menu;
ViewItem m_hovered;
ViewItem m_focused;
Selection m_selected;
Selection m_selection;
tools::ViewState m_view_state;
Graph* m_graph;
bool m_physics_dirty = false;
Expand All @@ -75,7 +75,6 @@ namespace ndbl
void _update(float dt, u16_t iterations);
void _update(float dt);
void _on_graph_change();
bool is_selected(NodeView*) const;
void frame_views(const std::vector<NodeView*>&, const Vec2& pivot );
void draw_create_node_context_menu(CreateNodeCtxMenu& menu, SlotView* dragged_slotview = nullptr );
void create_constraints__align_top_recursively(const std::vector<Node*>& unfiltered_follower, ndbl::Node *leader);
Expand Down
52 changes: 20 additions & 32 deletions src/ndbl/gui/Nodable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,18 +190,6 @@ void Nodable::update()
break;
}

case Event_SelectionChange::id:
{
if (m_current_file == nullptr )
break;
auto _event = reinterpret_cast<Event_SelectionChange*>( event );

Condition_ condition = _event->data.new_selection.empty() ? Condition_ENABLE_IF_HAS_NO_SELECTION
: Condition_ENABLE_IF_HAS_SELECTION;
m_current_file->view.clear_overlay();
m_current_file->view.refresh_overlay(condition );
break;
}
case EventID_FILE_OPENED:
{
ASSERT(m_current_file != nullptr );
Expand All @@ -212,45 +200,44 @@ void Nodable::update()
case Event_DeleteNode::id:
{
if ( graph_view )
for(NodeView* view : graph_view->selected().node)
for(NodeView* view : graph_view->selection().node() )
graph_view->graph()->destroy_next_frame(view->node());
break;
}

case Event_ArrangeNode::id:
{
if ( graph_view )
for(NodeView* view : graph_view->selected().node)
for(NodeView* view : graph_view->selection().node() )
view->arrange_recursively();
break;
}

case Event_SelectNext::id:
{
if (graph_view && !graph_view->selected().empty())
if (graph_view && !graph_view->selection().empty())
{
std::vector<NodeView*> successors;
if (!graph_view->selected().empty())
for(NodeView* view : graph_view->selected().node )
graph_view->selection().clear();
if (!graph_view->selection().empty())
for(NodeView* view : graph_view->selection().node() )
for (NodeView* successor : Utils::get_components<NodeView>( view->node()->flow_outputs() ) )
successors.push_back( successor );

graph_view->set_selected(successors, SelectionMode_REPLACE);
graph_view->selection().append( successor );
}

break;
}

case Event_ToggleFolding::id:
{
if ( graph_view && !graph_view->selection_empty() )
for(NodeView* view : graph_view->selected().node)
{
auto _event = reinterpret_cast<Event_ToggleFolding*>(event);
_event->data.mode == RECURSIVELY ? view->expand_toggle_rec()
: view->expand_toggle();
}
if ( graph_view )
break;

for(NodeView* view : graph_view->selection().node())
{
auto _event = reinterpret_cast<Event_ToggleFolding*>(event);
_event->data.mode == RECURSIVELY ? view->expand_toggle_rec()
: view->expand_toggle();
}
break;
}

Expand Down Expand Up @@ -389,7 +376,8 @@ void Nodable::update()
if ( auto view = new_node->get_component<NodeView>() )
{
view->spatial_node().set_position(_event->data.desired_screen_pos, WORLD_SPACE);
graph->view()->set_selected({view});
graph->view()->selection().clear();
graph->view()->selection().append(view);
}
break;
}
Expand Down Expand Up @@ -546,16 +534,16 @@ void Nodable::step_over_program()

if (!m_interpreter->is_there_a_next_instr() )
{
graph_view->set_selected({}, SelectionMode_REPLACE);
graph_view->selection().clear();
return;
}

const Node* next_node = m_interpreter->get_next_node();
if ( !next_node )
return;

NodeView* view = next_node->get_component<NodeView>();
graph_view->set_selected({view});
graph_view->selection().clear();
graph_view->selection().append( next_node->get_component<NodeView>() );
}

void Nodable::stop_program()
Expand Down
Loading

0 comments on commit e9dac64

Please sign in to comment.