89 lines
3.0 KiB
C++
89 lines
3.0 KiB
C++
#include "canvas.h"
|
|
|
|
#include "datatypes.h"
|
|
#include "interaction-event.h"
|
|
#include "state.h"
|
|
#include "draggable.h"
|
|
#include "raylib.h"
|
|
#include "raymath.h"
|
|
#include "vmath.h"
|
|
#include <memory>
|
|
|
|
namespace VISEQ::APP::CANVAS {
|
|
bool Canvas::hovering() {
|
|
return state->mouse_z == z_index;
|
|
}
|
|
|
|
void Canvas::drag(Vec2Df position) {
|
|
Vec2Df select1 = {SCREEN_MOUSE.x > select_start.x ? select_start.x : SCREEN_MOUSE.x, SCREEN_MOUSE.y > select_start.y ? select_start.y : SCREEN_MOUSE.y};
|
|
Vec2Df select2 = {SCREEN_MOUSE.x > select_start.x ? SCREEN_MOUSE.x - select_start.x : select_start.x - SCREEN_MOUSE.x, SCREEN_MOUSE.y > select_start.y ? SCREEN_MOUSE.y - select_start.y : select_start.y - SCREEN_MOUSE.y};
|
|
state->selection = (Rectangle){select1.x, select1.y, select2.x, select2.y};
|
|
}
|
|
|
|
void Canvas::pickUp() {
|
|
std::cout << "canvas pickUp" << std::endl;
|
|
BASE::Draggable::pickUp();
|
|
select_start = SCREEN_MOUSE;
|
|
state->pushEvent({getPtr(), Interaction::DESELECT});
|
|
}
|
|
|
|
void Canvas::putDown() {
|
|
std::cout << "canvas putDown" << std::endl;
|
|
BASE::Draggable::putDown();
|
|
state->pushEvent({getPtr(), Interaction::SELECT});
|
|
}
|
|
|
|
void Canvas::clearSelectionRect(){
|
|
state->selection = (Rectangle){0,0,0,0};
|
|
}
|
|
|
|
void Canvas::drawStart(){
|
|
BeginMode2D(camera);
|
|
}
|
|
|
|
void Canvas::drawEnd(){
|
|
EndMode2D();
|
|
}
|
|
|
|
GRAPH::GraphItemType Canvas::getType() {
|
|
return GRAPH::GraphItemType::CANVAS;
|
|
}
|
|
|
|
std::shared_ptr<Canvas> Canvas::getPtr(){
|
|
return shared_from_this();
|
|
}
|
|
|
|
void Canvas::update() {
|
|
camera_delta = state->mouse_delta * (-1.0f/camera.zoom);
|
|
|
|
if(state->mouse_right.down) {
|
|
camera.target = Vector2Add(camera.target, camera_delta);
|
|
}
|
|
|
|
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});
|
|
|
|
float wheel = GetMouseWheelMove();
|
|
if(wheel != 0){
|
|
if(!KEYS_DOWN.shift && !KEYS_DOWN.ctrl && !KEYS_DOWN.alt){
|
|
camera.offset = state->raw_mouse;
|
|
camera.target = 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);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Canvas::draw() {
|
|
for(int x = -SETTINGS.grid*100; x < SETTINGS.grid*100; x+=SETTINGS.grid){
|
|
for(int y = -SETTINGS.grid*100; y < SETTINGS.grid*100; y+=SETTINGS.grid){
|
|
DrawPixel(x, y, (Color){130,130,130,200});
|
|
}
|
|
}
|
|
if(state->selection.width > 0 || state->selection.height > 0) DrawRectangleLines(state->selection.x, state->selection.y, state->selection.width, state->selection.height, (Color){150,150,150,150});
|
|
|
|
}
|
|
}
|