Dragging and selecting works. Copy / Paste does not copy connectors yet.

This commit is contained in:
Sebastian
2024-11-28 01:08:04 +01:00
parent 813958002b
commit 492971ed4f
9 changed files with 70 additions and 42 deletions
+32 -17
View File
@@ -12,6 +12,7 @@
#include "mouse.h" #include "mouse.h"
#include "node.h" #include "node.h"
#include "raylib.h" #include "raylib.h"
#include "selectable.h"
#include "signal-manager.h" #include "signal-manager.h"
#include "state.h" #include "state.h"
#include "ui.h" #include "ui.h"
@@ -163,41 +164,55 @@ namespace VISEQ::APP {
void doInteraction(){ void doInteraction(){
bool exit = false; bool exit = false;
bool once = false;
state->mouse_z = canvas->z_index;
while(event_queue.size() > 0 && !exit){ while(event_queue.size() > 0 && !exit){
auto e = event_queue.front(); auto e = event_queue.front();
std::cout << e.item->getType() << " " << e.action << std::endl; std::cout << e.item->getType() << " " << e.action << std::endl;
if(e.action == Interaction::HOVER){
state->mouse_z = e.item->z_index;
std::cout << state->mouse_z << std::endl;
}
if(e.item->getType() == GraphItemType::CANVAS) { if(e.item->getType() == GraphItemType::CANVAS) {
if(e.action == Interaction::SELECT) { if(e.action == Interaction::SELECT) {
gm.select(); gm.select();
std::cout << gm.selected_nodes.size() << std::endl; std::cout << "app canvas select" << std::endl;
canvas->clearSelectionRect(); canvas->clearSelectionRect();
} }
if(e.action == Interaction::DESELECT) gm.deselect(); if(e.action == Interaction::DESELECT) {
std::cout << "app canvas deselect" << std::endl;
gm.deselect();
}
} }
if(e.item->getType() == GraphItemType::NODE){ if(!once && e.item->getType() == GraphItemType::NODE){
auto node = std::dynamic_pointer_cast<Node>(e.item);
if(e.action == Interaction::PICKUP) { if(e.action == Interaction::PICKUP) {
e.item->pickUp(); node->pickUp();
if(std::dynamic_pointer_cast<Node>(e.item)->selected){ if(node->selected){
for(auto s : gm.selected_nodes){ for(auto s : gm.selected_nodes){
s->pickUp(std::dynamic_pointer_cast<Node>(e.item)->position); s->pickUp(node->position);
} }
} }
exit = true; once = true;
} }
if(e.action == Interaction::DRAG) { if(e.action == Interaction::DRAG) {
e.item->drag(); if(KEYS_DOWN.ctrl) node->drag(MOD_MOUSE);
if(std::dynamic_pointer_cast<Node>(e.item)->selected){ else node->drag(MOD_MOUSE + node->drag_offset);
if(node->selected){
for(auto s : gm.selected_nodes){ for(auto s : gm.selected_nodes){
s->drag(); s->drag(MOD_MOUSE + s->drag_offset);
} }
} }
exit = true; once = true;
} }
if(e.action == Interaction::PUTDOWN) { if(e.action == Interaction::PUTDOWN) {
e.item->putDown(); node->putDown();
if(std::dynamic_pointer_cast<Node>(e.item)->selected){ if(node->selected){
for(auto s : gm.selected_nodes){ for(auto s : gm.selected_nodes){
s->putDown(); s->putDown();
} }
@@ -206,18 +221,18 @@ namespace VISEQ::APP {
} }
if(exit) break; if(exit) break;
if(e.item->getType() == GraphItemType::CONNECTOR || e.item->getType() == GraphItemType::CANVAS) { if(!once && (e.item->getType() == GraphItemType::CONNECTOR || e.item->getType() == GraphItemType::CANVAS)) {
switch(e.action){ switch(e.action){
case VISEQ::APP::Interaction::PICKUP: case VISEQ::APP::Interaction::PICKUP:
{ {
e.item->pickUp(); e.item->pickUp();
exit = true; once = true;
break; break;
} }
case VISEQ::APP::Interaction::DRAG: case VISEQ::APP::Interaction::DRAG:
{ {
e.item->drag(); e.item->drag(MOD_MOUSE);
exit = true; once = true;
break; break;
} }
case VISEQ::APP::Interaction::PUTDOWN: case VISEQ::APP::Interaction::PUTDOWN:
+5 -2
View File
@@ -7,6 +7,7 @@
#include "draggable.h" #include "draggable.h"
#include "raylib.h" #include "raylib.h"
#include "raymath.h" #include "raymath.h"
#include "vmath.h"
#include <memory> #include <memory>
namespace VISEQ::APP::CANVAS { namespace VISEQ::APP::CANVAS {
@@ -19,22 +20,24 @@ namespace VISEQ::APP::CANVAS {
Vec2Df select_start; Vec2Df select_start;
virtual bool hovering() override { virtual bool hovering() override {
return true; return state->mouse_z == z_index;
} }
virtual void drag() override { virtual void drag(Vec2Df position) override {
Vec2Df select1 = {SCREEN_MOUSE.x > select_start.x ? select_start.x : SCREEN_MOUSE.x, SCREEN_MOUSE.y > select_start.y ? select_start.y : SCREEN_MOUSE.y}; Vec2Df select1 = {SCREEN_MOUSE.x > select_start.x ? select_start.x : SCREEN_MOUSE.x, SCREEN_MOUSE.y > select_start.y ? select_start.y : SCREEN_MOUSE.y};
Vec2Df select2 = {SCREEN_MOUSE.x > select_start.x ? SCREEN_MOUSE.x - select_start.x : select_start.x - SCREEN_MOUSE.x, SCREEN_MOUSE.y > select_start.y ? SCREEN_MOUSE.y - select_start.y : select_start.y - SCREEN_MOUSE.y}; Vec2Df select2 = {SCREEN_MOUSE.x > select_start.x ? SCREEN_MOUSE.x - select_start.x : select_start.x - SCREEN_MOUSE.x, SCREEN_MOUSE.y > select_start.y ? SCREEN_MOUSE.y - select_start.y : select_start.y - SCREEN_MOUSE.y};
state->selection = (Rectangle){select1.x, select1.y, select2.x, select2.y}; state->selection = (Rectangle){select1.x, select1.y, select2.x, select2.y};
} }
virtual void pickUp() override { virtual void pickUp() override {
std::cout << "canvas pickUp" << std::endl;
BASE::Draggable::pickUp(); BASE::Draggable::pickUp();
select_start = SCREEN_MOUSE; select_start = SCREEN_MOUSE;
state->pushEvent({getPtr(), Interaction::DESELECT}); state->pushEvent({getPtr(), Interaction::DESELECT});
} }
virtual void putDown() override { virtual void putDown() override {
std::cout << "canvas putDown" << std::endl;
BASE::Draggable::putDown(); BASE::Draggable::putDown();
state->pushEvent({getPtr(), Interaction::SELECT}); state->pushEvent({getPtr(), Interaction::SELECT});
} }
+3 -3
View File
@@ -1,7 +1,7 @@
#pragma once #pragma once
#include "draggable.h" #include "draggable.h"
#include "state.h" #include "vmath.h"
#include <memory> #include <memory>
namespace VISEQ::BASE { namespace VISEQ::BASE {
@@ -19,8 +19,8 @@ namespace VISEQ::BASE {
connectables.emplace_back(this); connectables.emplace_back(this);
} }
virtual void drag() override { virtual void drag(Vec2Df new_position) override {
DrawLineV(position, MOD_MOUSE, (Color){229, 181, 103, 255}); DrawLineV(position, new_position, (Color){229, 181, 103, 255});
} }
void putDown() override { void putDown() override {
+3
View File
@@ -3,10 +3,12 @@
#include "connectable.h" #include "connectable.h"
#include "datatypes.h" #include "datatypes.h"
#include "graph-item.h" #include "graph-item.h"
#include "interaction-event.h"
#include "moveable.h" #include "moveable.h"
#include "raymath.h" #include "raymath.h"
#include "state.h" #include "state.h"
#include "vmath.h" #include "vmath.h"
#include <fcntl.h>
namespace VISEQ::GRAPH { namespace VISEQ::GRAPH {
class Connector : public BASE::Connectable, public GraphItem{ class Connector : public BASE::Connectable, public GraphItem{
@@ -50,6 +52,7 @@ namespace VISEQ::GRAPH {
} }
void update() override { void update() override {
if(hovering()) state->pushEvent({getPtr(), APP::Interaction::HOVER});
if(hovering() && clicked()) state->pushEvent({getPtr(), APP::Interaction::PICKUP}); if(hovering() && clicked()) state->pushEvent({getPtr(), APP::Interaction::PICKUP});
if(pickedUp()) state->pushEvent({getPtr(), APP::Interaction::DRAG}); if(pickedUp()) state->pushEvent({getPtr(), APP::Interaction::DRAG});
if(picked_up && released()) state->pushEvent({getPtr(), APP::Interaction::PUTDOWN}); if(picked_up && released()) state->pushEvent({getPtr(), APP::Interaction::PUTDOWN});
+6 -16
View File
@@ -33,25 +33,17 @@ namespace VISEQ::BASE {
} }
virtual void pickUp() override { virtual void pickUp() override {
picked_up = true; picked_up = true;
drag_offset = position - SCREEN_MOUSE; drag_offset = position - SCREEN_MOUSE;
} }
virtual void pickUp(Vec2Df selection_origin) override { virtual void pickUp(Vec2Df selection_origin) override {
picked_up = true; picked_up = true;
drag_offset = position - selection_origin; drag_offset = position - selection_origin;
} }
virtual void drag() override { virtual void drag(Vec2Df new_position) override {
// state->selection_origin = position; position = new_position;
/*if(KEYS_PRESSED.ctrl){*/
/* selection_offset = position - state->selection_origin;*/
/*}*/
/*if(KEYS_DOWN.ctrl){*/
/* position = MOD_MOUSE + selection_offset;*/
/*} else {*/
position = MOD_MOUSE + drag_offset;
/*}*/
} }
virtual void putDown() override { virtual void putDown() override {
@@ -59,8 +51,6 @@ namespace VISEQ::BASE {
} }
virtual bool select() override { virtual bool select() override {
/*in_selection_rect = */
/*if(MOUSE_LEFT.released) selected = (KEYS_DOWN.shift && selected) || in_selection_rect;*/
return CheckCollisionPointRec(position, state->selection); return CheckCollisionPointRec(position, state->selection);
} }
+4
View File
@@ -65,6 +65,7 @@ namespace VISEQ::GRAPH {
} }
void select(){ void select(){
std::cout << "select" << std::endl;
auto temp_nodes = nodes | RANGE_FILTER([](SPTR_Node n){return n->select();}) | std::ranges::to<VEC_SPTR_Node>(); auto temp_nodes = nodes | RANGE_FILTER([](SPTR_Node n){return n->select();}) | std::ranges::to<VEC_SPTR_Node>();
if(!KEYS_DOWN.shift) selected_nodes.clear(); if(!KEYS_DOWN.shift) selected_nodes.clear();
selected_nodes.insert(selected_nodes.end(), temp_nodes.begin(), temp_nodes.end()); selected_nodes.insert(selected_nodes.end(), temp_nodes.begin(), temp_nodes.end());
@@ -72,6 +73,7 @@ namespace VISEQ::GRAPH {
} }
void deselect(){ void deselect(){
std::cout << "deselect" << std::endl;
for(auto s : selected_nodes){ for(auto s : selected_nodes){
s->selected = false; s->selected = false;
} }
@@ -269,6 +271,8 @@ namespace VISEQ::GRAPH {
new_node->copied = false; new_node->copied = false;
new_node->selected = true; new_node->selected = true;
//TODO copy connectors
for(auto c : copy_connections){ for(auto c : copy_connections){
if(c->start == old_node) { if(c->start == old_node) {
c->start = new_node; c->start = new_node;
+1 -1
View File
@@ -39,7 +39,7 @@ namespace VISEQ::BASE {
virtual GRAPH::GraphItemType getType() = 0; virtual GRAPH::GraphItemType getType() = 0;
virtual void pickUp() = 0; virtual void pickUp() = 0;
virtual void pickUp(Vec2Df position) = 0; virtual void pickUp(Vec2Df position) = 0;
virtual void drag() = 0; virtual void drag(Vec2Df position) = 0;
virtual void putDown() = 0; virtual void putDown() = 0;
}; };
+11
View File
@@ -26,6 +26,16 @@ namespace VISEQ::GRAPH::NODES {
Node(float x, float y) : Draggable(Vec2Df(x, y)) {} Node(float x, float y) : Draggable(Vec2Df(x, y)) {}
Node(Vec2Df p) : Draggable(p) {} Node(Vec2Df p) : Draggable(p) {}
/*Node(const Node& n){*/
/* distributionMode = n.distributionMode;*/
/* radius = n.radius;*/
/* config = n.config;*/
/* position = n.position;*/
/* for(auto c : n.connectors){*/
/* connectors.emplace_back(std::make_shared<Connector>(*c));*/
/* }*/
/*}*/
NodeType nodeType; NodeType nodeType;
std::vector<std::shared_ptr<Node>> neighbors; std::vector<std::shared_ptr<Node>> neighbors;
@@ -68,6 +78,7 @@ namespace VISEQ::GRAPH::NODES {
} }
void update() override{ void update() override{
if(hovering()) state->pushEvent({getPtr(), APP::Interaction::HOVER});
if(hovering() && clicked()) state->pushEvent({getPtr(), APP::Interaction::PICKUP}); if(hovering() && clicked()) state->pushEvent({getPtr(), APP::Interaction::PICKUP});
if(pickedUp()) state->pushEvent({getPtr(), APP::Interaction::DRAG}); if(pickedUp()) state->pushEvent({getPtr(), APP::Interaction::DRAG});
if(picked_up && released()) state->pushEvent({getPtr(), APP::Interaction::PUTDOWN}); if(picked_up && released()) state->pushEvent({getPtr(), APP::Interaction::PUTDOWN});
+2
View File
@@ -67,6 +67,8 @@ namespace VISEQ::APP{
bool hovering = false; bool hovering = false;
int mouse_z = 0;
MouseState mouse_left; MouseState mouse_left;
MouseState mouse_right; MouseState mouse_right;
MouseState mouse_middle; MouseState mouse_middle;