From 190ebe86ff0bee86d1a62e13909705f8e36f75cb Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 10 Oct 2024 02:59:27 +0200 Subject: [PATCH] drag works --- src/datatypes.h | 5 ++ src/main.cpp | 221 ++++++++++-------------------------------------- src/node.h | 4 +- src/signal.h | 2 +- 4 files changed, 54 insertions(+), 178 deletions(-) diff --git a/src/datatypes.h b/src/datatypes.h index b1b5e5d..86dfcaa 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -24,6 +24,11 @@ class Vec2D{ //return acos(dot(v)); }; + T angleDeg(Vec2D &v){ + + return angle(v) * 180 / M_PI; + }; + T dot(Vec2D &v){ return (x*v.x) + (y*v.y); }; diff --git a/src/main.cpp b/src/main.cpp index 9fce337..a19eb31 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,12 +11,15 @@ #include "libremidi/message.hpp" #include "libremidi/observer_configuration.hpp" +#include #include #include #include +#include #include #include +#include #include #include #include @@ -68,6 +71,8 @@ int main(){ midi_out.open_port(midi_out_ports[2]); + Vec2D drag_origin; + sf::Clock deltaClock; while(window.isOpen()){ @@ -79,15 +84,38 @@ int main(){ } } - window.clear(); + sf::Vector2i mp = sf::Mouse::getPosition(window); + Vec2D mouse_pos = {(float)mp.x, (float)mp.y}; + bool mouse_toggled = false; + mouse_pressed = sf::Mouse::isButtonPressed(sf::Mouse::Button::Left); + + if(!mouse_pressed && mouse_clicked) mouse_clicked = false; + if(mouse_pressed && !mouse_clicked){ + mouse_clicked = true; + mouse_toggled = true; + drag_start = mouse_pos; + } + + window.clear(sf::Color(46,46,46)); + + bool hovering = false; + for( auto& n : nodes){ + if((n.position - mouse_pos).length() <= n.radius && !hovering) { + n.hover_move = true; + hovering = true; + } else { + n.hover_move = false; + } + if(mouse_clicked && n.hover_move){ n.clicked = true; } + if(mouse_toggled) drag_origin = n.position; if(mouse_pressed && n.clicked){ - n.position = mouse_pos; + n.position = mouse_pos - (drag_start - drag_origin); } if(!mouse_pressed) n.clicked = false; n.update(); @@ -106,184 +134,25 @@ int main(){ n.draw(&window); } + if(!hovering && mouse_clicked) { + Vec2D ref = {1.f,0.f}; + Vec2D lineVec = mouse_pos - drag_start; + float width = lineVec.length(); + float angle = ref.angleDeg(lineVec); + + sf::RectangleShape line; + line.setSize(sf::Vector2f(width, 2)); + line.setPosition(drag_start.x, drag_start.y); + line.setRotation(angle); + line.setFillColor(sf::Color(100,0,0)); + window.draw(line); + } //window.draw(); window.display(); } } -/***/ -/*SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv){*/ -/**/ -/* if (not SDL_Init(SDL_INIT_VIDEO)) {*/ -/* SDL_Log("Unable to initialize SDL: %s", SDL_GetError());*/ -/* return SDL_APP_FAILURE;*/ -/* }*/ -/**/ -/* SDL_Window* window = SDL_CreateWindow("Window", 1920, 1080, SDL_WINDOW_OPENGL|SDL_WINDOW_FULLSCREEN);*/ -/* if (not window){*/ -/* SDL_Log("Unable to create window: %s", SDL_GetError());*/ -/* return SDL_APP_FAILURE;*/ -/* }*/ -/**/ -/* gl = SDL_GL_CreateContext(window);*/ -/**/ -/* SDL_ShowWindow(window);*/ -/**/ -/* int window_width;*/ -/* int window_height;*/ -/* SDL_GetWindowSize(window, &window_width, &window_height);*/ -/**/ -/**/ -/*// cairo_device_t* device = cairo_glx_device_create();*/ -/**/ -/* sdl_cairo_surface = SDL_CreateSurface(renderer_width, renderer_height, SDL_PIXELFORMAT_RGBA32);*/ -/**/ -/* int stride = cairo_format_stride_for_width(CAIRO_FORMAT_RGB24, sdl_cairo_surface->w);*/ -/* cr_surface = cairo_image_surface_create_for_data((unsigned char*) sdl_cairo_surface->pixels, CAIRO_FORMAT_RGB24, sdl_cairo_surface->w, sdl_cairo_surface->h, stride);*/ -/**/ -/* cr = cairo_create(cr_surface);*/ -/* SDL_Log("%d", cairo_surface_status(cr_surface));*/ -/**/ -/* int cairo_x_multiplier = renderer_width / window_width;*/ -/* int cairo_y_multiplier = renderer_height / window_height;*/ -/**/ -/* cairo_surface_set_device_scale(cr_surface, cairo_x_multiplier, cairo_y_multiplier);*/ -/**/ -/* for(const libremidi::input_port& port : obs.get_input_ports()){*/ -/* midi_in_ports.push_back(port);*/ -/* }*/ -/**/ -/* for(const libremidi::output_port& port : obs.get_output_ports()){*/ -/* midi_out_ports.push_back(port);*/ -/* }*/ -/**/ -/* for(int i = 0; i < 10; i++){*/ -/* nodes.emplace(nodes.end(), Node(i*10.0f, i*10.0f, 15.0f));*/ -/* }*/ -/**/ -/* for(int i = 0; i < 10; i++){*/ -/* signals.emplace(signals.end(), Signal(i*10.0f, i*10.0f, 5.0f));*/ -/* }*/ -/**/ -/* IMGUI_CHECKVERSION();*/ -/* ImGui::CreateContext();*/ -/* io = ImGui::GetIO(); (void)io;*/ -/**/ -/* ImGui::StyleColorsDark();*/ -/* //ImGui::StyleColorsLight();*/ -/**/ -/* // Setup Platform/Renderer backends*/ -/* ImGui_ImplSDL3_InitForSDLRenderer(window, renderer);*/ -/* ImGui_ImplSDLRenderer3_Init(renderer);*/ -/**/ -/* *appstate = new AppContext{*/ -/* window,*/ -/* renderer,*/ -/* (float)window_width,*/ -/* (float)window_height*/ -/* };*/ -/**/ -/* return SDL_APP_CONTINUE;*/ -/*}*/ -/**/ -/*SDL_AppResult SDL_AppIterate(void *appstate){*/ -/* auto* app = (AppContext*)appstate;*/ -/**/ -/* SDL_SetRenderDrawColor(app->renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);*/ -/* SDL_RenderClear(app->renderer); */ -/**/ -/* // ---------- cairo*/ -/**/ -/**/ -/* cairo_set_source_rgba(cr, 0, 0, 0, 1.0);*/ -/* cairo_rectangle(cr, 0, 0, app->width, app->height);*/ -/* cairo_fill(cr);*/ -/**/ -/* for( auto& n : nodes){*/ -/**/ -/* if(mouse_clicked && n.hover_move){*/ -/* n.clicked = true;*/ -/* }*/ -/* if(mouse_pressed && n.clicked){*/ -/* n.position = mouse_pos;*/ -/* }*/ -/* if(!mouse_pressed) n.clicked = false;*/ -/* n.update();*/ -/* n.draw(cr, mouse_pos);*/ -/* }*/ -/**/ -/* for( auto& n : signals){*/ -/* n.update();*/ -/* n.draw(cr);*/ -/* }*/ -/**/ -/* SDL_Texture *texture = SDL_CreateTextureFromSurface(app->renderer, sdl_cairo_surface);*/ -/**/ -/* // ---------- ImGUI*/ -/**/ - /*ImGui_ImplSDLRenderer3_NewFrame();*/ - /*ImGui_ImplSDL3_NewFrame();*/ - /*ImGui::NewFrame();*/ - /**/ - /*ImGui::Begin("MIDI Config");*/ - /*ImGui::SameLine();*/ - /**/ - /*for(auto port : midi_out_ports){*/ - /* if(ImGui::Button(port.port_name.c_str())){*/ - /* midi_out.open_port(port);*/ - /* midi_out.send_message(144, 100, 40);*/ - /* };*/ - /*}*/ - /**/ - /*ImGui::Text("%f %f", mouse_pos.x, mouse_pos.y);*/ - /**/ - /*ImGui::End();*/ - /*ImGui::Render();*/ -/**/ -/**/ -/* // ---------- Render*/ -/**/ -/* SDL_RenderTexture(app->renderer, texture, NULL, NULL);*/ -/*// ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), app->renderer); */ -/**/ -/* SDL_RenderPresent(app->renderer);*/ -/**/ -/*// auto time = SDL_GetTicks();*/ -/* SDL_Delay(5);*/ -/* return SDL_APP_CONTINUE;*/ -/*}*/ -/**/ -/*SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event){*/ -/* //auto* app = (AppContext*)appstate;*/ -/* mouse_clicked = false;*/ -/**/ -/* const SDL_Event *ev = event;*/ -/* ImGui_ImplSDL3_ProcessEvent(ev);*/ -/**/ -/* if (event->type == SDL_EVENT_QUIT) {*/ -/* return SDL_APP_SUCCESS;*/ -/* }*/ -/**/ -/* if(event->type == SDL_EVENT_MOUSE_MOTION){*/ -/* mouse_pos = {event->motion.x, event->motion.y};*/ -/* }*/ -/**/ -/* if(event->type == SDL_EVENT_MOUSE_BUTTON_DOWN){*/ -/* if(event->button.button == SDL_BUTTON_LEFT) {*/ -/* if(!mouse_pressed) {*/ -/* drag_start = mouse_pos;*/ -/* mouse_clicked = true;*/ -/* }*/ -/* mouse_pressed = true;*/ -/* }*/ -/* }*/ -/**/ -/* if(event->type == SDL_EVENT_MOUSE_BUTTON_UP){*/ -/* if(event->button.button == SDL_BUTTON_LEFT) mouse_pressed = false;*/ -/* }*/ -/**/ -/* return SDL_APP_CONTINUE;*/ -/*}*/ -/**/ + + diff --git a/src/node.h b/src/node.h index 19bd1ac..ac24bee 100644 --- a/src/node.h +++ b/src/node.h @@ -27,7 +27,9 @@ class Node : public Movable{ void draw(sf::RenderWindow* window) { sf::CircleShape circle(radius); - circle.setFillColor(sf::Color::Green); + circle.setOrigin(radius/2.f, radius/2.f); + if(!hover_move) circle.setFillColor(sf::Color(180, 210, 115)); + if(hover_move) circle.setFillColor(sf::Color(108, 153, 187)); circle.setPosition(position.x, position.y); window->draw(circle); } diff --git a/src/signal.h b/src/signal.h index 0256c6e..f44c6bd 100644 --- a/src/signal.h +++ b/src/signal.h @@ -23,7 +23,7 @@ class Signal : public Movable{ void draw(sf::RenderWindow *window) { sf::CircleShape circle(radius); - circle.setFillColor(sf::Color()); + circle.setFillColor(sf::Color(232, 125, 62)); circle.setPosition(position.x, position.y); // circle.getPoint(); // circle.getPointCount()