2024-11-15 01:49:10 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "draggable.h"
|
2024-11-16 00:29:01 +01:00
|
|
|
#include "raylib.h"
|
2024-11-15 01:49:10 +01:00
|
|
|
#include "raymath.h"
|
|
|
|
|
#include "state.h"
|
|
|
|
|
namespace VISEQ::APP::CANVAS {
|
|
|
|
|
class Canvas : public BASE::Draggable {
|
|
|
|
|
public:
|
|
|
|
|
Canvas() {}
|
|
|
|
|
|
|
|
|
|
Camera2D camera;
|
|
|
|
|
Vec2Df camera_delta;
|
2024-11-16 00:29:01 +01:00
|
|
|
Vec2Df select_start;
|
2024-11-15 01:49:10 +01:00
|
|
|
|
2024-11-16 00:29:01 +01:00
|
|
|
virtual bool hovering() override {
|
|
|
|
|
return true;
|
2024-11-15 01:49:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void drag() override {
|
|
|
|
|
if(state->mouse_right.down) {
|
|
|
|
|
camera.target = Vector2Add(camera.target, camera_delta);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void drawStart(){
|
|
|
|
|
BeginMode2D(camera);
|
2024-11-17 02:40:31 +01:00
|
|
|
/*state->hovering = false;*/
|
|
|
|
|
/*if(hovering()) state->istate = InteractionState::HOVER_CANVAS;*/
|
2024-11-15 01:49:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void drawEnd(){
|
|
|
|
|
EndMode2D();
|
|
|
|
|
}
|
2024-11-16 00:29:01 +01:00
|
|
|
|
2024-11-18 01:57:10 +01:00
|
|
|
void selectNodes(){
|
2024-11-16 00:29:01 +01:00
|
|
|
if(state->mouse_left.clicked){
|
2024-11-17 02:40:31 +01:00
|
|
|
state->selection = (Rectangle){0,0,0,0};
|
2024-11-16 00:29:01 +01:00
|
|
|
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};
|
|
|
|
|
}
|
2024-11-17 02:40:31 +01:00
|
|
|
|
|
|
|
|
if(state->mouse_left.released){
|
|
|
|
|
state->istate = InteractionState::HOVER_CANVAS;
|
|
|
|
|
}
|
2024-11-16 00:29:01 +01:00
|
|
|
}
|
2024-11-15 01:49:10 +01:00
|
|
|
|
2024-11-17 02:40:31 +01:00
|
|
|
virtual void update() override {
|
2024-11-15 01:49:10 +01:00
|
|
|
camera_delta = state->mouse_delta * (-1.0f/camera.zoom);
|
|
|
|
|
if(hovering()) drag();
|
2024-11-18 01:57:10 +01:00
|
|
|
if(state->istate == InteractionState::HOVER_CANVAS || state->istate == InteractionState::DRAW_SELECTION) selectNodes();
|
2024-11-15 01:49:10 +01:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-17 02:40:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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});
|
2024-11-15 01:49:10 +01:00
|
|
|
|
|
|
|
|
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});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|