refactored from main to canvas.h - basic functionality working
This commit is contained in:
+1
-1
@@ -131,7 +131,7 @@ namespace VISEQ::APP {
|
|||||||
sm.drawSignals(mouse_pos, camera);
|
sm.drawSignals(mouse_pos, camera);
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawUI(Vec2Df *mouse_pos, Camera2D *camera){
|
void drawUI(Vec2Df mouse_pos, Camera2D camera){
|
||||||
mutex.lock();
|
mutex.lock();
|
||||||
ui.draw(mouse_pos, camera);
|
ui.draw(mouse_pos, camera);
|
||||||
mutex.unlock();
|
mutex.unlock();
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "draggable.h"
|
||||||
|
#include "raymath.h"
|
||||||
|
#include "state.h"
|
||||||
|
namespace VISEQ::APP::CANVAS {
|
||||||
|
class Canvas : public BASE::Draggable {
|
||||||
|
public:
|
||||||
|
Canvas() {}
|
||||||
|
|
||||||
|
Camera2D camera;
|
||||||
|
Vec2Df camera_delta;
|
||||||
|
|
||||||
|
virtual bool hovering() override {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void drag() override {
|
||||||
|
if(state->mouse_right.down) {
|
||||||
|
camera.target = Vector2Add(camera.target, camera_delta);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void drawStart(){
|
||||||
|
BeginMode2D(camera);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void drawEnd(){
|
||||||
|
EndMode2D();
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void draw(){
|
||||||
|
camera_delta = state->mouse_delta * (-1.0f/camera.zoom);
|
||||||
|
if(hovering()) drag();
|
||||||
|
|
||||||
|
float wheel = GetMouseWheelMove();
|
||||||
|
if(wheel != 0){
|
||||||
|
if(!IsKeyDown(KEY_LEFT_SHIFT) && !IsKeyDown(KEY_LEFT_CONTROL) && !IsKeyDown(KEY_LEFT_ALT)){
|
||||||
|
camera.offset = state->raw_mouse;
|
||||||
|
camera.target = state->screen_mouse;
|
||||||
|
float scaleFactor = 1.0f + (0.25f*fabsf(wheel));
|
||||||
|
if (wheel < 0) scaleFactor = 1.0f/scaleFactor;
|
||||||
|
camera.zoom = Clamp(camera.zoom*scaleFactor, 0.125f, 64.0f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int x = -state->settings.grid*100; x < state->settings.grid*100; x+=state->settings.grid){
|
||||||
|
for(int y = -state->settings.grid*100; y < state->settings.grid*100; y+=state->settings.grid){
|
||||||
|
DrawPixel(x, y, (Color){130,130,130,200});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
+5
-1
@@ -25,7 +25,11 @@ namespace VISEQ::BASE {
|
|||||||
}
|
}
|
||||||
|
|
||||||
virtual void drag() {
|
virtual void drag() {
|
||||||
position = state->mod_mouse + drag_offset;
|
if(state->keys_down.ctrl){
|
||||||
|
position = state->mod_mouse;
|
||||||
|
} else {
|
||||||
|
position = state->mod_mouse + drag_offset;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void putDown(){
|
virtual void putDown(){
|
||||||
|
|||||||
+13
-58
@@ -5,6 +5,7 @@
|
|||||||
* 2024 Sebastian Kujas
|
* 2024 Sebastian Kujas
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
#include "canvas.h"
|
||||||
#include "state.h"
|
#include "state.h"
|
||||||
#include "datatypes.h"
|
#include "datatypes.h"
|
||||||
#include "graph-manager.h"
|
#include "graph-manager.h"
|
||||||
@@ -30,6 +31,7 @@
|
|||||||
|
|
||||||
using json = nlohmann::json;
|
using json = nlohmann::json;
|
||||||
|
|
||||||
|
VISEQ::APP::CANVAS::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();
|
||||||
@@ -38,8 +40,6 @@ Vec2Df mouse_pos;
|
|||||||
Vec2Df drag_start;
|
Vec2Df drag_start;
|
||||||
//Vec2Df global_mouse;
|
//Vec2Df global_mouse;
|
||||||
|
|
||||||
Camera2D camera = { {0} };
|
|
||||||
|
|
||||||
bool mouse_clicked = false;
|
bool mouse_clicked = false;
|
||||||
bool mouse_touched = false;
|
bool mouse_touched = false;
|
||||||
bool mouse_dragging = false;
|
bool mouse_dragging = false;
|
||||||
@@ -74,10 +74,10 @@ int main(){
|
|||||||
|
|
||||||
SetTargetFPS(state->settings.fps);
|
SetTargetFPS(state->settings.fps);
|
||||||
|
|
||||||
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 };
|
||||||
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 };
|
||||||
camera.rotation = 0.0f;
|
canvas.camera.rotation = 0.0f;
|
||||||
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");
|
||||||
@@ -172,39 +172,14 @@ int main(){
|
|||||||
|
|
||||||
VEC_SPTR_Node copied_nodes;
|
VEC_SPTR_Node copied_nodes;
|
||||||
|
|
||||||
Vec2Df modified_mouse;
|
|
||||||
|
|
||||||
while(!WindowShouldClose()){
|
while(!WindowShouldClose()){
|
||||||
|
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
ClearBackground((Color){46,46,46});
|
ClearBackground((Color){46,46,46});
|
||||||
BeginMode2D(camera);
|
canvas.drawStart();
|
||||||
|
|
||||||
state->settings.speed = (state->settings.grid * 16.0f) * (state->settings.bpm / 120.f);
|
state->update(canvas.camera);
|
||||||
|
|
||||||
Vector2 mp = GetScreenToWorld2D(GetMousePosition(), camera);
|
|
||||||
Vec2Df mouse_pos = {(float)mp.x, (float)mp.y};
|
|
||||||
Vec2Df cam_pos = {camera.offset.x, camera.offset.y};
|
|
||||||
|
|
||||||
bool mouse_down = IsMouseButtonDown(MOUSE_BUTTON_LEFT);
|
|
||||||
bool mouse_clicked = IsMouseButtonPressed(MOUSE_BUTTON_LEFT);
|
|
||||||
bool mouse_released = IsMouseButtonReleased(MOUSE_BUTTON_LEFT);
|
|
||||||
|
|
||||||
bool mouse_right_down = IsMouseButtonDown(MOUSE_BUTTON_RIGHT);
|
|
||||||
bool mouse_right_clicked = IsMouseButtonPressed(MOUSE_BUTTON_RIGHT);
|
|
||||||
bool mouse_right_released = IsMouseButtonReleased(MOUSE_BUTTON_RIGHT);
|
|
||||||
|
|
||||||
Vec2Df mouse_delta = {GetMouseDelta().x, GetMouseDelta().y};
|
|
||||||
Vec2Df delta = mouse_delta * (-1.0f/camera.zoom);
|
|
||||||
|
|
||||||
state->screen_mouse = mouse_pos;
|
|
||||||
state->raw_mouse = {GetMousePosition().x, GetMousePosition().y};
|
|
||||||
state->mod_mouse = mouse_pos;
|
|
||||||
|
|
||||||
if(IsKeyDown(KEY_LEFT_CONTROL)) {
|
|
||||||
state->mod_mouse.x = round(mouse_pos.x / state->settings.grid) * state->settings.grid;
|
|
||||||
state->mod_mouse.y = round(mouse_pos.y / state->settings.grid) * state->settings.grid;
|
|
||||||
}
|
|
||||||
/*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;*/
|
||||||
/* if(dragNode->neighbors.size() > 1){*/
|
/* if(dragNode->neighbors.size() > 1){*/
|
||||||
@@ -315,7 +290,7 @@ int main(){
|
|||||||
/* drag_distance = 0;*/
|
/* drag_distance = 0;*/
|
||||||
/* }*/
|
/* }*/
|
||||||
/* if(mouse_right_down) {*/
|
/* if(mouse_right_down) {*/
|
||||||
/* camera.target = Vector2Add(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) {*/
|
||||||
@@ -514,32 +489,12 @@ int main(){
|
|||||||
/* break;*/
|
/* break;*/
|
||||||
/* }*/
|
/* }*/
|
||||||
/*};*/
|
/*};*/
|
||||||
if(mouse_right_down) {
|
|
||||||
camera.target = Vector2Add(camera.target, delta);
|
|
||||||
}
|
|
||||||
|
|
||||||
float wheel = GetMouseWheelMove();
|
canvas.draw();
|
||||||
if(wheel != 0){
|
app.draw(state->screen_mouse, canvas.camera);
|
||||||
if(!IsKeyDown(KEY_LEFT_SHIFT) && !IsKeyDown(KEY_LEFT_CONTROL) && !IsKeyDown(KEY_LEFT_ALT)){
|
canvas.drawEnd();
|
||||||
camera.offset = GetMousePosition();
|
|
||||||
camera.target = mouse_pos;
|
|
||||||
float scaleFactor = 1.0f + (0.25f*fabsf(wheel));
|
|
||||||
if (wheel < 0) scaleFactor = 1.0f/scaleFactor;
|
|
||||||
camera.zoom = Clamp(camera.zoom*scaleFactor, 0.125f, 64.0f);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for(int x = -state->settings.grid*100; x < state->settings.grid*100; x+=state->settings.grid){
|
app.drawUI(state->screen_mouse, canvas.camera);
|
||||||
for(int y = -state->settings.grid*100; y < state->settings.grid*100; y+=state->settings.grid){
|
|
||||||
DrawPixel(x, y, (Color){130,130,130,200});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
app.draw(mouse_pos, camera);
|
|
||||||
|
|
||||||
EndMode2D();
|
|
||||||
|
|
||||||
app.drawUI(&mouse_pos, &camera);
|
|
||||||
|
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
}
|
}
|
||||||
|
|||||||
+62
@@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "raylib.h"
|
||||||
#include "vmath.h"
|
#include "vmath.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
@@ -18,6 +19,18 @@ namespace VISEQ::APP{
|
|||||||
|
|
||||||
enum InteractionState {HOVER_CANVAS, HOVER_NODE, HOVER_CONNECTOR, ADD_NODES_MENU, DRAG_NODE, CONNECT_NODE, DRAW_SELECTION, SCALE_SELECTION, DELETE_SELECTED_NODES, COPY_SELECTED_NODES, PASTE_SELECTED_NODES};
|
enum InteractionState {HOVER_CANVAS, HOVER_NODE, HOVER_CONNECTOR, ADD_NODES_MENU, DRAG_NODE, CONNECT_NODE, DRAW_SELECTION, SCALE_SELECTION, DELETE_SELECTED_NODES, COPY_SELECTED_NODES, PASTE_SELECTED_NODES};
|
||||||
|
|
||||||
|
struct MouseState{
|
||||||
|
bool down = false;
|
||||||
|
bool clicked = false;
|
||||||
|
bool released = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ModifierKeys{
|
||||||
|
bool ctrl = false;
|
||||||
|
bool shift = false;
|
||||||
|
bool alt = false;
|
||||||
|
};
|
||||||
|
|
||||||
class State{
|
class State{
|
||||||
public:
|
public:
|
||||||
State(){}
|
State(){}
|
||||||
@@ -34,6 +47,55 @@ namespace VISEQ::APP{
|
|||||||
|
|
||||||
int node_mode = 0;
|
int node_mode = 0;
|
||||||
|
|
||||||
|
MouseState mouse_left;
|
||||||
|
MouseState mouse_right;
|
||||||
|
MouseState mouse_middle;
|
||||||
|
|
||||||
|
ModifierKeys keys_down;
|
||||||
|
ModifierKeys keys_pressed;
|
||||||
|
ModifierKeys keys_released;
|
||||||
|
|
||||||
|
|
||||||
|
Vec2Df mouse_delta;
|
||||||
|
|
||||||
|
void update(Camera2D camera){
|
||||||
|
mouse_delta = {GetMouseDelta().x, GetMouseDelta().y};
|
||||||
|
|
||||||
|
settings.speed = (settings.grid * 16.0f) * (settings.bpm / 120.f);
|
||||||
|
|
||||||
|
Vector2 mp = GetScreenToWorld2D(GetMousePosition(), camera);
|
||||||
|
Vec2Df mouse_pos = {(float)mp.x, (float)mp.y};
|
||||||
|
|
||||||
|
mouse_left.down = IsMouseButtonDown(MOUSE_BUTTON_LEFT);
|
||||||
|
mouse_left.clicked = IsMouseButtonPressed(MOUSE_BUTTON_LEFT);
|
||||||
|
mouse_left.released = IsMouseButtonReleased(MOUSE_BUTTON_LEFT);
|
||||||
|
|
||||||
|
mouse_right.down = IsMouseButtonDown(MOUSE_BUTTON_RIGHT);
|
||||||
|
mouse_right.clicked = IsMouseButtonPressed(MOUSE_BUTTON_RIGHT);
|
||||||
|
mouse_right.released = IsMouseButtonReleased(MOUSE_BUTTON_RIGHT);
|
||||||
|
|
||||||
|
keys_down.ctrl = IsKeyDown(KEY_LEFT_CONTROL) || IsKeyDown(KEY_RIGHT_CONTROL);
|
||||||
|
keys_down.shift = IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT);
|
||||||
|
keys_down.alt = IsKeyDown(KEY_LEFT_ALT) || IsKeyDown(KEY_RIGHT_ALT);
|
||||||
|
|
||||||
|
keys_pressed.ctrl = IsKeyPressed(KEY_LEFT_CONTROL) || IsKeyPressed(KEY_RIGHT_CONTROL);
|
||||||
|
keys_pressed.shift = IsKeyPressed(KEY_LEFT_SHIFT) || IsKeyPressed(KEY_RIGHT_SHIFT);
|
||||||
|
keys_pressed.alt = IsKeyPressed(KEY_LEFT_ALT) || IsKeyPressed(KEY_RIGHT_ALT);
|
||||||
|
|
||||||
|
keys_released.ctrl = IsKeyReleased(KEY_LEFT_CONTROL) || IsKeyReleased(KEY_RIGHT_CONTROL);
|
||||||
|
keys_released.shift = IsKeyReleased(KEY_LEFT_SHIFT) || IsKeyReleased(KEY_RIGHT_SHIFT);
|
||||||
|
keys_released.alt = IsKeyReleased(KEY_LEFT_ALT) || IsKeyReleased(KEY_RIGHT_ALT);
|
||||||
|
|
||||||
|
screen_mouse = mouse_pos;
|
||||||
|
raw_mouse = {GetMousePosition().x, GetMousePosition().y};
|
||||||
|
mod_mouse = mouse_pos;
|
||||||
|
|
||||||
|
if(keys_down.ctrl) {
|
||||||
|
mod_mouse.x = round(mouse_pos.x / settings.grid) * settings.grid;
|
||||||
|
mod_mouse.y = round(mouse_pos.y / settings.grid) * settings.grid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
inline static std::shared_ptr<State> instance;
|
inline static std::shared_ptr<State> instance;
|
||||||
static std::shared_ptr<State> get() {
|
static std::shared_ptr<State> get() {
|
||||||
if(!instance){
|
if(!instance){
|
||||||
|
|||||||
@@ -105,9 +105,9 @@ namespace VISEQ::APP::UI{
|
|||||||
DrawText(text.c_str(), position.x-offset/2.0f, position.y-(float)size/2.0f+1, size, color );
|
DrawText(text.c_str(), position.x-offset/2.0f, position.y-(float)size/2.0f+1, size, color );
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawPerNodeConfig(Camera2D *camera){
|
void drawPerNodeConfig(Camera2D camera){
|
||||||
for(auto& n : gm->nodes | std::views::filter([](std::shared_ptr<Node> n){ return n->config; })){
|
for(auto& n : gm->nodes | std::views::filter([](std::shared_ptr<Node> n){ return n->config; })){
|
||||||
Vector2 np = GetWorldToScreen2D((Vector2){n->position.x, n->position.y}, *camera);
|
Vector2 np = GetWorldToScreen2D((Vector2){n->position.x, n->position.y}, camera);
|
||||||
|
|
||||||
n->config = !GuiWindowBox((Rectangle){np.x + 10, np.y + 10, 240, 440}, "config");
|
n->config = !GuiWindowBox((Rectangle){np.x + 10, np.y + 10, 240, 440}, "config");
|
||||||
|
|
||||||
@@ -195,7 +195,7 @@ namespace VISEQ::APP::UI{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw(Vec2Df *mouse_pos, Camera2D *camera){
|
void draw(Vec2Df mouse_pos, Camera2D camera){
|
||||||
drawSideBar();
|
drawSideBar();
|
||||||
drawPerNodeConfig(camera);
|
drawPerNodeConfig(camera);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user