#pragma once #include "draggable.h" #include "raylib.h" #include "raymath.h" #include "state.h" namespace VISEQ::APP::CANVAS { class Canvas : public BASE::Draggable { public: Canvas() {} Camera2D camera; Vec2Df camera_delta; Vec2Df select_start; 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); /*state->hovering = false;*/ /*if(hovering()) state->istate = InteractionState::HOVER_CANVAS;*/ } virtual void drawEnd(){ EndMode2D(); } void selectNodes(){ if(state->mouse_left.clicked){ state->selection = (Rectangle){0,0,0,0}; select_start = state->screen_mouse; state->istate = InteractionState::DRAW_SELECTION; } if(state->mouse_left.down){ Vec2Df select1 = {state->screen_mouse.x > select_start.x ? select_start.x : state->screen_mouse.x, state->screen_mouse.y > select_start.y ? select_start.y : state->screen_mouse.y}; Vec2Df select2 = {state->screen_mouse.x > select_start.x ? state->screen_mouse.x - select_start.x : select_start.x - state->screen_mouse.x, state->screen_mouse.y > select_start.y ? state->screen_mouse.y - select_start.y : select_start.y - state->screen_mouse.y}; state->selection = (Rectangle){select1.x, select1.y, select2.x, select2.y}; } if(state->mouse_left.released){ state->istate = InteractionState::HOVER_CANVAS; } } virtual void update() override { camera_delta = state->mouse_delta * (-1.0f/camera.zoom); if(hovering()) drag(); if(state->istate == InteractionState::HOVER_CANVAS || state->istate == InteractionState::DRAW_SELECTION) selectNodes(); 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); } } } virtual void draw(){ if(state->istate == InteractionState::DRAW_SELECTION) DrawRectangleLines(state->selection.x, state->selection.y, state->selection.width, state->selection.height, (Color){150,150,150,150}); 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}); } } } }; }