Dragging and selecting works. Dragging the selection not yet.

This commit is contained in:
Sebastian
2024-11-25 00:53:10 +01:00
parent f04838bffa
commit 9c0aac8618
9 changed files with 76 additions and 53 deletions
+9 -3
View File
@@ -3,8 +3,10 @@
#include "clipboard.h" #include "clipboard.h"
#include "datatypes.h" #include "datatypes.h"
#include "graph-manager.h" #include "graph-manager.h"
#include "interaction-event.h"
#include "midi-node.h" #include "midi-node.h"
#include "mouse.h" #include "mouse.h"
#include "moveable.h"
#include "node.h" #include "node.h"
#include "raylib.h" #include "raylib.h"
#include "signal-manager.h" #include "signal-manager.h"
@@ -61,16 +63,18 @@ namespace VISEQ::APP {
auto e = event_queue.front(); auto e = event_queue.front();
std::cout << e.action << " " << e.item->z_index << std::endl; std::cout << e.action << " " << e.item->z_index << std::endl;
if(e.item->getType() == GraphItemType::NODE || e.item->getType() == GraphItemType::CONNECTOR) { if(e.item->getType() == GraphItemType::NODE || 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();
goto exit_loop;
break; break;
} }
case VISEQ::APP::Interaction::DRAG: case VISEQ::APP::Interaction::DRAG:
{ {
e.item->drag(); e.item->drag();
goto exit_loop;
break; break;
} }
case VISEQ::APP::Interaction::PUTDOWN: case VISEQ::APP::Interaction::PUTDOWN:
@@ -78,11 +82,13 @@ namespace VISEQ::APP {
e.item->putDown(); e.item->putDown();
break; break;
} }
default:
} }
} }
event_queue.pop_front();
event_queue.pop();
} }
exit_loop:
event_queue.clear();
} }
void update(){ void update(){
+30 -27
View File
@@ -6,9 +6,10 @@
#include "draggable.h" #include "draggable.h"
#include "raylib.h" #include "raylib.h"
#include "raymath.h" #include "raymath.h"
#include <memory>
namespace VISEQ::APP::CANVAS { namespace VISEQ::APP::CANVAS {
class Canvas : public BASE::Draggable { class Canvas : public BASE::Draggable, public std::enable_shared_from_this<Canvas> {
public: public:
Canvas() {} Canvas() {}
@@ -21,11 +22,23 @@ namespace VISEQ::APP::CANVAS {
} }
virtual void drag() override { virtual void drag() override {
if(state->mouse_right.down) { 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};
camera.target = Vector2Add(camera.target, camera_delta); 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};
DrawRectangleLines(state->selection.x, state->selection.y, state->selection.width, state->selection.height, (Color){150,150,150,150});
} }
virtual void pickUp() override {
BASE::Draggable::pickUp();
state->selection = (Rectangle){0,0,0,0};
select_start = SCREEN_MOUSE;
}
virtual void putDown() override {
BASE::Draggable::putDown();
state->selection = (Rectangle){0,0,0,0};
}
virtual void drawStart(){ virtual void drawStart(){
BeginMode2D(camera); BeginMode2D(camera);
} }
@@ -37,30 +50,21 @@ namespace VISEQ::APP::CANVAS {
GRAPH::GraphItemType getType() override { GRAPH::GraphItemType getType() override {
return GRAPH::GraphItemType::CANVAS; return GRAPH::GraphItemType::CANVAS;
} }
void makeSelection(){ std::shared_ptr<Canvas> getPtr(){
if(MOUSE_LEFT.clicked){ return shared_from_this();
state->selection = (Rectangle){0,0,0,0};
select_start = SCREEN_MOUSE;
STATE = IState::DRAW_SELECTION;
}
if(MOUSE_LEFT.down){
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};
state->selection = (Rectangle){select1.x, select1.y, select2.x, select2.y};
}
if(MOUSE_LEFT.released){
STATE = IState::HOVER_CANVAS;
state->selection = (Rectangle){0,0,0,0};
}
} }
virtual void update() override { virtual void update() override {
camera_delta = state->mouse_delta * (-1.0f/camera.zoom); camera_delta = state->mouse_delta * (-1.0f/camera.zoom);
if(hovering()) drag();
if(STATE == IState::HOVER_CANVAS || STATE == IState::DRAW_SELECTION) makeSelection(); if(state->mouse_right.down) {
camera.target = Vector2Add(camera.target, camera_delta);
}
if(hovering() && clicked()) state->pushEvent({getPtr(), APP::Interaction::PICKUP});
if(pickedUp()) state->pushEvent({getPtr(), APP::Interaction::DRAG});
if(picked_up && released()) state->pushEvent({getPtr(), APP::Interaction::PUTDOWN});
float wheel = GetMouseWheelMove(); float wheel = GetMouseWheelMove();
if(wheel != 0){ if(wheel != 0){
@@ -74,8 +78,7 @@ namespace VISEQ::APP::CANVAS {
} }
} }
virtual void draw(){ virtual void draw() {
if(STATE == IState::DRAW_SELECTION) DrawRectangleLines(state->selection.x, state->selection.y, state->selection.width, state->selection.height, (Color){150,150,150,150});
for(int x = -SETTINGS.grid*100; x < SETTINGS.grid*100; x+=SETTINGS.grid){ for(int x = -SETTINGS.grid*100; x < SETTINGS.grid*100; x+=SETTINGS.grid){
for(int y = -SETTINGS.grid*100; y < SETTINGS.grid*100; y+=SETTINGS.grid){ for(int y = -SETTINGS.grid*100; y < SETTINGS.grid*100; y+=SETTINGS.grid){
DrawPixel(x, y, (Color){130,130,130,200}); DrawPixel(x, y, (Color){130,130,130,200});
+3 -2
View File
@@ -38,8 +38,9 @@ namespace VISEQ::BASE {
} }
virtual void drag() override { virtual void drag() override {
state->selection_origin = position;
if(KEYS_PRESSED.ctrl){ if(KEYS_PRESSED.ctrl){
selection_offset = position - state->hover_position; selection_offset = position - state->selection_origin;
} }
if(KEYS_DOWN.ctrl){ if(KEYS_DOWN.ctrl){
position = MOD_MOUSE + selection_offset; position = MOD_MOUSE + selection_offset;
@@ -53,7 +54,7 @@ namespace VISEQ::BASE {
} }
virtual void select() override { virtual void select() override {
if(STATE == IState::DRAW_SELECTION) in_selection_rect = CheckCollisionPointRec(position, state->selection); in_selection_rect = CheckCollisionPointRec(position, state->selection);
if(MOUSE_LEFT.released) selected = (KEYS_DOWN.shift && selected) || in_selection_rect; if(MOUSE_LEFT.released) selected = (KEYS_DOWN.shift && selected) || in_selection_rect;
} }
+1 -1
View File
@@ -376,7 +376,7 @@ namespace VISEQ::GRAPH {
void drawNodes(Vec2Df mouse_pos, Camera2D camera){ void drawNodes(Vec2Df mouse_pos, Camera2D camera){
for(auto n : nodes){ for(auto n : nodes){
if(KEYS_PRESSED.ctrl && n->hovering()) state->hover_position = n->position; if(KEYS_PRESSED.ctrl && n->hovering()) state->selection_origin = n->position;
} }
BASE::Interactable::z_hovering = -1; BASE::Interactable::z_hovering = -1;
for(auto n : nodes){ for(auto n : nodes){
+4 -3
View File
@@ -1,16 +1,17 @@
#pragma once #pragma once
#include "interactable.h" #include "interactable.h"
#include <deque>
#include <memory> #include <memory>
#include <queue>
namespace VISEQ::APP{ namespace VISEQ::APP{
enum Interaction {HOVER, CLICK, HOLD, RELEASE, PICKUP, DRAG, PUTDOWN}; enum Interaction {NONE, HOVER, CLICK, HOLD, RELEASE, PICKUP, DRAG, PUTDOWN, SELECTED};
struct InteractionEvent{ struct InteractionEvent{
std::shared_ptr<BASE::Interactable> item; std::shared_ptr<BASE::Interactable> item;
Interaction action; Interaction action;
}; };
inline static std::queue<VISEQ::APP::InteractionEvent> event_queue; inline static std::deque<VISEQ::APP::InteractionEvent> event_queue;
} }
+13 -13
View File
@@ -31,7 +31,7 @@
using json = nlohmann::json; using json = nlohmann::json;
VISEQ::APP::CANVAS::Canvas canvas; std::shared_ptr<VISEQ::APP::CANVAS::Canvas> canvas = std::make_shared<VISEQ::APP::CANVAS::Canvas>();
VISEQ::APP::AppManager app; VISEQ::APP::AppManager app;
std::shared_ptr<VISEQ::APP::State> state = VISEQ::APP::State::get(); std::shared_ptr<VISEQ::APP::State> state = VISEQ::APP::State::get();
@@ -74,10 +74,10 @@ int main(){
SetTargetFPS(state->settings.fps); SetTargetFPS(state->settings.fps);
canvas.camera.target = (Vector2){ state->settings.width/2.0f, state->settings.height/2.0f }; canvas->camera.target = (Vector2){ state->settings.width/2.0f, state->settings.height/2.0f };
canvas.camera.offset = (Vector2){ state->settings.width/2.0f, state->settings.height/2.0f }; canvas->camera.offset = (Vector2){ state->settings.width/2.0f, state->settings.height/2.0f };
canvas.camera.rotation = 0.0f; canvas->camera.rotation = 0.0f;
canvas.camera.zoom = 1.0f; canvas->camera.zoom = 1.0f;
if(std::filesystem::exists("state.json")){ if(std::filesystem::exists("state.json")){
std::ifstream f("state.json"); std::ifstream f("state.json");
@@ -176,9 +176,9 @@ int main(){
BeginDrawing(); BeginDrawing();
ClearBackground((Color){46,46,46}); ClearBackground((Color){46,46,46});
canvas.drawStart(); canvas->drawStart();
state->update(canvas.camera); state->update(canvas->camera);
/*else if(IsKeyDown(KEY_LEFT_SHIFT)){*/ /*else if(IsKeyDown(KEY_LEFT_SHIFT)){*/
/* float steps = round((dragNode->neighbors.front()->position.dist(mouse_pos)) / state->settings.grid) * state->settings.grid;*/ /* float steps = round((dragNode->neighbors.front()->position.dist(mouse_pos)) / state->settings.grid) * state->settings.grid;*/
@@ -290,7 +290,7 @@ int main(){
/* drag_distance = 0;*/ /* drag_distance = 0;*/
/* }*/ /* }*/
/* if(mouse_right_down) {*/ /* if(mouse_right_down) {*/
/* canvas.camera.target = Vector2Add(canvas.camera.target, delta);*/ /* canvas->camera.target = Vector2Add(canvas->camera.target, delta);*/
/* drag_distance += delta.length();*/ /* drag_distance += delta.length();*/
/* }*/ /* }*/
/* if(mouse_right_released && drag_distance < 5) {*/ /* if(mouse_right_released && drag_distance < 5) {*/
@@ -490,14 +490,14 @@ int main(){
/* }*/ /* }*/
/*};*/ /*};*/
canvas.update(); canvas->update();
app.update(); app.update();
canvas.draw(); canvas->draw();
app.draw(SCREEN_MOUSE, canvas.camera); app.draw(SCREEN_MOUSE, canvas->camera);
canvas.drawEnd(); canvas->drawEnd();
app.drawUI(SCREEN_MOUSE, canvas.camera); app.drawUI(SCREEN_MOUSE, canvas->camera);
EndDrawing(); EndDrawing();
} }
+1 -1
View File
@@ -99,7 +99,7 @@ namespace VISEQ::GRAPH::NODES {
break;} break;}
}; };
if(midi_note < 0 ) midi_note = VISEQ::MIDI::Notes.size()-1; if(midi_note < 0 ) midi_note = VISEQ::MIDI::Notes.size()-1;
if(midi_note >= VISEQ::MIDI::Notes.size()) midi_note = 0; if(midi_note >= (int)VISEQ::MIDI::Notes.size()) midi_note = 0;
if(midi_cc < 0 ) midi_cc = 127; if(midi_cc < 0 ) midi_cc = 127;
if(midi_cc > 127 ) midi_cc = 0; if(midi_cc > 127 ) midi_cc = 0;
+3 -1
View File
@@ -68,10 +68,12 @@ namespace VISEQ::GRAPH::NODES {
} }
void update() override{ void update() override{
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});
if(selected) state->pushEvent({getPtr(), APP::Interaction::SELECTED});
if(state->selection.width > 0) select();
int free_connectors = 0; int free_connectors = 0;
for(auto c : connectors){ for(auto c : connectors){
+12 -2
View File
@@ -1,6 +1,7 @@
#pragma once #pragma once
#include "interaction-event.h" #include "interaction-event.h"
#include "nlohmann/json.hpp"
#include "raylib.h" #include "raylib.h"
#include "vmath.h" #include "vmath.h"
#include <memory> #include <memory>
@@ -78,12 +79,21 @@ namespace VISEQ::APP{
Vec2Df mouse_delta; Vec2Df mouse_delta;
Vec2Df hover_position; Vec2Df selection_origin;
void pushEvent(InteractionEvent e){ void pushEvent(InteractionEvent e){
event_queue.push(e); event_queue.push_back(e);
} }
InteractionEvent pullEvent(){
if(!event_queue.empty()){
InteractionEvent e = event_queue.front();
event_queue.pop_front();
return e;
}
return {nullptr, Interaction::NONE};
}
void update(Camera2D camera){ void update(Camera2D camera){
if(istate != old_istate) std::cout << state_string[istate] << std::endl; if(istate != old_istate) std::cout << state_string[istate] << std::endl;