From 657c23702cbb48f3107390c1d4a2bf2357049d1b Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 4 May 2026 18:40:00 +0200 Subject: [PATCH] moved event_queue to state singleton --- .clangd | 3 ++ Makefile | 1 - selectable.cpp | 1 + src/canvas.cpp | 2 +- src/clipboard.cpp | 16 ++++++ src/clipboard.h | 13 ++--- src/connector.h | 2 +- src/draggable.cpp | 41 +++++++++++++++ src/draggable.h | 47 ++++-------------- src/interactable.cpp | 17 +++++++ src/interactable.h | 12 ++--- src/interaction-event.h | 1 - src/interaction-manager.cpp | 18 ++++--- src/main.cpp | 1 - src/node-handle.cpp | 98 ++++++++++++++++++++++++++++++++++++ src/node-handle.h | 99 ++++++------------------------------- src/node.cpp | 1 - src/selectable.cpp | 7 +++ src/selectable.h | 4 +- src/state.cpp | 5 +- src/state.h | 2 + 21 files changed, 233 insertions(+), 158 deletions(-) create mode 100644 .clangd create mode 100644 selectable.cpp create mode 100644 src/clipboard.cpp create mode 100644 src/draggable.cpp create mode 100644 src/interactable.cpp create mode 100644 src/node-handle.cpp create mode 100644 src/selectable.cpp diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..06baf60 --- /dev/null +++ b/.clangd @@ -0,0 +1,3 @@ +CompileFlags: + Add: [-I/home/sebastian/code/viseq/include, -std=c++23] + diff --git a/Makefile b/Makefile index c770a86..5ad4c7b 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,6 @@ LDFLAGS := -Llib -L/usr/lib -L/usr/local/lib SRCS := $(wildcard src/*.cpp) OBJECTS := $(patsubst src/%.cpp,build/%.o, $(SRCS)) - viseq: $(OBJECTS) $(CXX) $(OBJECTS) -o $@ $(LDFLAGS) $(LDLIBS) diff --git a/selectable.cpp b/selectable.cpp new file mode 100644 index 0000000..48cedf1 --- /dev/null +++ b/selectable.cpp @@ -0,0 +1 @@ +#include "selectable.h diff --git a/src/canvas.cpp b/src/canvas.cpp index 5638aa0..c0f08b5 100644 --- a/src/canvas.cpp +++ b/src/canvas.cpp @@ -1,7 +1,7 @@ #include "canvas.h" #include "datatypes.h" -#include "interaction-event.h" +//#include "interaction-event.h" #include "state.h" #include "draggable.h" #include "raylib.h" diff --git a/src/clipboard.cpp b/src/clipboard.cpp new file mode 100644 index 0000000..ab16089 --- /dev/null +++ b/src/clipboard.cpp @@ -0,0 +1,16 @@ +#include "clipboard.h" + +namespace VISEQ::APP { + GRAPH::NODES::VEC_SPTR_Node Clipboard::pasteNodes(){ + return clipboard.back(); + } + + void Clipboard::copyNodes(GRAPH::NODES::VEC_SPTR_Node selected_nodes){ + if(clipboard.size() == 20) clipboard.pop_back(); + clipboard.emplace(clipboard.begin()); + for(auto n : selected_nodes){ + clipboard.front().push_back(n); + } + } + +} diff --git a/src/clipboard.h b/src/clipboard.h index e345567..98a61e6 100644 --- a/src/clipboard.h +++ b/src/clipboard.h @@ -1,6 +1,7 @@ #pragma once #include "node.h" +#include namespace VISEQ::APP { class Clipboard { @@ -9,17 +10,9 @@ namespace VISEQ::APP { std::vector clipboard; - GRAPH::NODES::VEC_SPTR_Node pasteNodes(){ - return clipboard.back(); - } + GRAPH::NODES::VEC_SPTR_Node pasteNodes(); - void copyNodes(GRAPH::NODES::VEC_SPTR_Node selected_nodes){ - if(clipboard.size() == 20) clipboard.pop_back(); - clipboard.emplace(clipboard.begin()); - for(auto n : selected_nodes){ - clipboard.front().push_back(n); - } - } + void copyNodes(GRAPH::NODES::VEC_SPTR_Node selected_nodes); }; } diff --git a/src/connector.h b/src/connector.h index da77833..8323a58 100644 --- a/src/connector.h +++ b/src/connector.h @@ -3,7 +3,7 @@ #include "connectable.h" #include "datatypes.h" #include "graph-item.h" -#include "interaction-event.h" +//#include "interaction-event.h" #include "raymath.h" #include "state.h" #include "vmath.h" diff --git a/src/draggable.cpp b/src/draggable.cpp new file mode 100644 index 0000000..2e12ef3 --- /dev/null +++ b/src/draggable.cpp @@ -0,0 +1,41 @@ +#include "draggable.h" + +namespace VISEQ::BASE { + bool Draggable::entered() { + return !is_hovering && hovering(); + } + + bool Draggable::exited() { + return is_hovering && !hovering(); + } + + bool Draggable::pickedUp() { + return held() && picked_up; + } + + void Draggable::pickUp() { + picked_up = true; + drag_offset = position - SCREEN_MOUSE; + } + + void Draggable::pickUp(Vec2Df selection_origin) { + picked_up = true; + drag_offset = position - selection_origin; + } + + void Draggable::drag(Vec2Df new_position) { + position = new_position; + } + + void Draggable::putDown() { + picked_up = false; + } + + bool Draggable::select() { + return CheckCollisionPointRec(position, state->selection); + } + + void Draggable::deselect() { + selected = false; + } +} diff --git a/src/draggable.h b/src/draggable.h index 896c41c..f8d7e66 100644 --- a/src/draggable.h +++ b/src/draggable.h @@ -20,43 +20,14 @@ namespace VISEQ::BASE { bool picked_up = false; - virtual bool entered() override { - return !is_hovering && hovering(); - } - - virtual bool exited() override { - return is_hovering && !hovering(); - } - - virtual bool pickedUp() { - return held() && picked_up; - } - - virtual void pickUp() override { - picked_up = true; - drag_offset = position - SCREEN_MOUSE; - } - - virtual void pickUp(Vec2Df selection_origin) override { - picked_up = true; - drag_offset = position - selection_origin; - } - - virtual void drag(Vec2Df new_position) override { - position = new_position; - } - - virtual void putDown() override { - picked_up = false; - } - - virtual bool select() override { - return CheckCollisionPointRec(position, state->selection); - } - - virtual void deselect() override { - selected = false; - } - + virtual bool entered() override; + virtual bool exited() override; + virtual bool pickedUp(); + virtual void pickUp() override; + virtual void pickUp(Vec2Df selection_origin) override; + virtual void drag(Vec2Df new_position) override; + virtual void putDown() override; + virtual bool select() override; + virtual void deselect() override; }; } diff --git a/src/interactable.cpp b/src/interactable.cpp new file mode 100644 index 0000000..7d16976 --- /dev/null +++ b/src/interactable.cpp @@ -0,0 +1,17 @@ +#include "interactable.h" + +namespace VISEQ::BASE { + + bool Interactable::clicked() { + return hovering() && IsMouseButtonPressed(MOUSE_BUTTON_LEFT); + } + + bool Interactable::held() { + return IsMouseButtonDown(MOUSE_BUTTON_LEFT); + } + + bool Interactable::released() { + return IsMouseButtonReleased(MOUSE_BUTTON_LEFT); + } + +} diff --git a/src/interactable.h b/src/interactable.h index dbc8a53..51f7b75 100644 --- a/src/interactable.h +++ b/src/interactable.h @@ -24,17 +24,11 @@ namespace VISEQ::BASE { virtual bool entered() {return false;}; virtual bool exited() {return false;}; - virtual bool clicked() { - return hovering() && IsMouseButtonPressed(MOUSE_BUTTON_LEFT); - } + virtual bool clicked(); - virtual bool held() { - return IsMouseButtonDown(MOUSE_BUTTON_LEFT); - } + virtual bool held(); - virtual bool released() { - return IsMouseButtonReleased(MOUSE_BUTTON_LEFT); - } + virtual bool released(); virtual GRAPH::GraphItemType getType() = 0; virtual void pickUp() = 0; diff --git a/src/interaction-event.h b/src/interaction-event.h index 5431f20..ff7b933 100644 --- a/src/interaction-event.h +++ b/src/interaction-event.h @@ -17,5 +17,4 @@ namespace VISEQ::APP{ return lhs.item == rhs.item; } - inline static std::deque event_queue; } diff --git a/src/interaction-manager.cpp b/src/interaction-manager.cpp index 1c9b6c8..f9c15b0 100644 --- a/src/interaction-manager.cpp +++ b/src/interaction-manager.cpp @@ -3,7 +3,6 @@ #include "clipboard.h" #include "connector.h" #include "datatypes.h" -#include "interaction-event.h" #include "node.h" #include "raylib.h" #include "state.h" @@ -133,12 +132,13 @@ namespace VISEQ::INTERACTION{ bool connect_once = false; state->mouse_z = canvas->z_index; + std::cout << state->event_queue.size() << " "; - for(auto e : APP::event_queue){ + for(auto e : state->event_queue){ if(!connect_once && e.action == APP::Interaction::CONNECT){ APP::InteractionEvent partner; bool found = false; - for(auto ei : APP::event_queue){ + for(auto ei : state->event_queue){ if(ei.action == APP::Interaction::HOVER && ei.item->getType() == GraphItemType::CONNECTOR) { std::cout << "YEAH" << std::endl; partner = ei; @@ -166,8 +166,12 @@ namespace VISEQ::INTERACTION{ } } - while(APP::event_queue.size() > 0 && !exit){ - auto e = APP::event_queue.front(); + while(state->event_queue.size() > 0 && !exit){ + auto e = state->event_queue.front(); + std::cout << e.action; + if(e.action == APP::Interaction::CLICK){ + gm->addToSelection(); + } if(e.action == APP::Interaction::RIGHTCLICK){ gm->addToSelection(); state->context_menu = !state->context_menu; @@ -248,8 +252,8 @@ namespace VISEQ::INTERACTION{ } } - APP::event_queue.pop_front(); + state->event_queue.pop_front(); } - APP::event_queue.clear(); + state->event_queue.clear(); } } diff --git a/src/main.cpp b/src/main.cpp index faf141a..38c4084 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,7 +16,6 @@ #include #include - VISEQ::APP::AppManager app; std::shared_ptr state = VISEQ::APP::State::get(); diff --git a/src/node-handle.cpp b/src/node-handle.cpp new file mode 100644 index 0000000..74dd471 --- /dev/null +++ b/src/node-handle.cpp @@ -0,0 +1,98 @@ +#include "node-handle.h" + +namespace VISEQ::GRAPH::NODES { + template + void NodeHandle::init(){ + position = parentPosition.orbitAtAngle(orbit, 0) - parentPosition; + } + + template + float NodeHandle::distance(){ + return (parentPosition + position).dist(parentPosition); + } + + template + void NodeHandle::drag(Vec2Df mouse_pos) { + float range = (maxValue - minValue) + 1; + float stepping = 360.0f / range; + + if(KEYS_DOWN.shift) stepping = stepping * steps; + + float angle = Vector2Angle((SCREEN_MOUSE - parentPosition), Vector2(0,1)) * RAD2DEG - 180; // -180 because round(180 / 33.75) * 33.75 is not 0 + + if(KEYS_PRESSED.ctrl) angle_start = angle; + if(KEYS_DOWN.ctrl) angle = angle_start + (angle - angle_start) / 10; + + float stepped_angle = (round(angle / stepping) * stepping + 180) * DEG2RAD; // see above. -180 + 180 to get 0 right round(0 / 33.75) * 33.75 is still 0 + position = parentPosition.orbitAtAngle(orbit, stepped_angle) - parentPosition; + } + + template + void NodeHandle::lock(){ + visible = false; + locked = true; + } + + template + void NodeHandle::unlock(){ + visible = true; + locked = false; + } + + template + bool NodeHandle::hovering() { + return CheckCollisionPointCircle(SCREEN_MOUSE, position + parentPosition, radius); + } + + template + void NodeHandle::update(Vec2Df pos) { + if(!locked && visible){ + if(hovering()) state->pushEvent({getPtr(), APP::Interaction::HOVER}); + 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}); + + parentPosition = pos; + if(!locked && visible) updateAssignedValue(); + + minP = parentPosition.orbitAlongVector((parentPosition + position), minDistance); + maxP = parentPosition.orbitAlongVector((parentPosition + position), maxDistance); + } + + template + void NodeHandle::draw() { + if(!locked && visible){ + bool show = hovering() || picked_up; + Color dcol = ColorBrightness(col, -0.5f); + Color dyncol = show ? col : dcol; + Color dGrey = ColorBrightness(VS_COLOR_GREY, -0.2f); + Color dynGrey = show ? VS_COLOR_GREY : dGrey; + + if(steps > 0){ + for(int i = 0; i < steps; i++){ + Vec2Df pos = parentPosition.orbitAtAngle(orbit, DEG2RAD * i * (360.0f/steps)); + Vec2Df pos1 = parentPosition.orbitAtAngle(orbit - 3.0f, DEG2RAD * i * (360.0f/steps)); + DrawLineV(pos, pos1, dynGrey); + } + } + + float r = (parentPosition + position).dist(parentPosition); + DrawRing(parentPosition, r - 5, r + 5, 0, 360, 90, ColorAlpha(dyncol, 0.1)); + DrawRing(parentPosition, r - 5, r + 5, -90, angle_g - 90, 90, ColorAlpha(dyncol, 0.3)); + DrawCircleV(position + parentPosition, radius, dyncol); + //VISEQ::HELPER::DrawSuperText(std::format("{}", assignedValueRef), position + parentPosition, position + parentPosition + Vec2Df(50,0), 1, 0, 8, dyncol); + + /*if(show) {*/ + /* std::string text = std::format("{}", assignedValueRef);*/ + /* DrawCenteredText(text, parentPosition, 12, ColorBrightness(dyncol, 0.75f));*/ + /*}*/ + } + } + + template + bool NodeHandle::select() { + return false; + } + +} diff --git a/src/node-handle.h b/src/node-handle.h index 21b1d7f..2c57750 100644 --- a/src/node-handle.h +++ b/src/node-handle.h @@ -70,44 +70,13 @@ namespace VISEQ::GRAPH::NODES{ bool locked = true; bool visible = false; - void init(){ - position = parentPosition.orbitAtAngle(orbit, 0) - parentPosition; - } + void init(); void assignValue(T &value){ assignedValueRef = value; } - - float distance(){ - return (parentPosition + position).dist(parentPosition); - } - - virtual void drag(Vec2Df mouse_pos) override { - float range = (maxValue - minValue) + 1; - float stepping = 360.0f / range; - if(KEYS_DOWN.shift) stepping = stepping * steps; - - float angle = Vector2Angle((SCREEN_MOUSE - parentPosition), Vector2(0,1)) * RAD2DEG - 180; // -180 because round(180 / 33.75) * 33.75 is not 0 - - if(KEYS_PRESSED.ctrl) angle_start = angle; - if(KEYS_DOWN.ctrl) angle = angle_start + (angle - angle_start) / 10; - - float stepped_angle = (round(angle / stepping) * stepping + 180) * DEG2RAD; // see above. -180 + 180 to get 0 right round(0 / 33.75) * 33.75 is still 0 - position = parentPosition.orbitAtAngle(orbit, stepped_angle) - parentPosition; - } - - void lock(){ - visible = false; - locked = true; - } - - void unlock(){ - visible = true; - locked = false; - } - - void assignValue(T &value, float min, float max, float minDistance, float maxDistance){ + void assignValue(T &value, float min, float max, float minDistance, float maxDistance){ assignedValueRef = value; } @@ -124,58 +93,22 @@ namespace VISEQ::GRAPH::NODES{ } assignedValueRef = value; } + + float distance(); - virtual bool hovering() override { - return CheckCollisionPointCircle(SCREEN_MOUSE, position + parentPosition, radius); - } - - virtual void update(Vec2Df pos) override { - if(!locked && visible){ - if(hovering()) state->pushEvent({getPtr(), APP::Interaction::HOVER}); - 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}); - - parentPosition = pos; - if(!locked && visible) updateAssignedValue(); - - minP = parentPosition.orbitAlongVector((parentPosition + position), minDistance); - maxP = parentPosition.orbitAlongVector((parentPosition + position), maxDistance); - } - - virtual void draw() override { - if(!locked && visible){ - bool show = hovering() || picked_up; - Color dcol = ColorBrightness(col, -0.5f); - Color dyncol = show ? col : dcol; - Color dGrey = ColorBrightness(VS_COLOR_GREY, -0.2f); - Color dynGrey = show ? VS_COLOR_GREY : dGrey; - - if(steps > 0){ - for(int i = 0; i < steps; i++){ - Vec2Df pos = parentPosition.orbitAtAngle(orbit, DEG2RAD * i * (360.0f/steps)); - Vec2Df pos1 = parentPosition.orbitAtAngle(orbit - 3.0f, DEG2RAD * i * (360.0f/steps)); - DrawLineV(pos, pos1, dynGrey); - } - } - - float r = (parentPosition + position).dist(parentPosition); - DrawRing(parentPosition, r - 5, r + 5, 0, 360, 90, ColorAlpha(dyncol, 0.1)); - DrawRing(parentPosition, r - 5, r + 5, -90, angle_g - 90, 90, ColorAlpha(dyncol, 0.3)); - DrawCircleV(position + parentPosition, radius, dyncol); - //VISEQ::HELPER::DrawSuperText(std::format("{}", assignedValueRef), position + parentPosition, position + parentPosition + Vec2Df(50,0), 1, 0, 8, dyncol); - - /*if(show) {*/ - /* std::string text = std::format("{}", assignedValueRef);*/ - /* DrawCenteredText(text, parentPosition, 12, ColorBrightness(dyncol, 0.75f));*/ - /*}*/ - } - } + virtual void drag(Vec2Df mouse_pos) override; - virtual bool select() override { - return false; - } + void lock(); + + void unlock(); + + virtual bool hovering() override; + + virtual void update(Vec2Df pos) override; + + virtual void draw() override; + + virtual bool select() override; NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(NodeHandle, position, name); }; diff --git a/src/node.cpp b/src/node.cpp index 6735099..54b686a 100644 --- a/src/node.cpp +++ b/src/node.cpp @@ -5,7 +5,6 @@ #include "connector.h" #include "datatypes.h" -#include "interaction-event.h" #include "state.h" #include "vmath.h" diff --git a/src/selectable.cpp b/src/selectable.cpp new file mode 100644 index 0000000..9ed3150 --- /dev/null +++ b/src/selectable.cpp @@ -0,0 +1,7 @@ +#include "selectable.h" + +namespace VISEQ::BASE { + bool Selectable::clicked() { + return (selected || hovering()) && IsMouseButtonPressed(MOUSE_BUTTON_LEFT); + } +} diff --git a/src/selectable.h b/src/selectable.h index 391d344..9213a48 100644 --- a/src/selectable.h +++ b/src/selectable.h @@ -16,9 +16,7 @@ namespace VISEQ::BASE { bool erase = false; Vec2Df selection_offset; - virtual bool clicked() override { - return (selected || hovering()) && IsMouseButtonPressed(MOUSE_BUTTON_LEFT); - } + virtual bool clicked() override; virtual bool select() = 0; virtual void deselect() = 0; diff --git a/src/state.cpp b/src/state.cpp index 6a09051..63599f9 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -8,11 +8,12 @@ namespace VISEQ::APP{ void State::pushEvent(InteractionEvent e){ - std::cout << "PUSHED" << std::endl; + std::cout << "push event" << std::endl; event_queue.push_back(e); } - InteractionEvent pullEvent(){ + InteractionEvent State::pullEvent(){ + std::cout << "pull event" << std::endl; if(!event_queue.empty()){ InteractionEvent e = event_queue.front(); event_queue.pop_front(); diff --git a/src/state.h b/src/state.h index a331dd4..49b71b2 100644 --- a/src/state.h +++ b/src/state.h @@ -93,6 +93,8 @@ namespace VISEQ::APP{ inline static std::shared_ptr instance; static std::shared_ptr get(); + + std::deque event_queue; }; }