refactored from main to canvas.h - basic functionality working

This commit is contained in:
Sebastian
2024-11-15 01:49:10 +01:00
parent ab34081dbb
commit 9cccbab703
6 changed files with 141 additions and 64 deletions
+14 -59
View File
@@ -5,6 +5,7 @@
* 2024 Sebastian Kujas
*
*/
#include "canvas.h"
#include "state.h"
#include "datatypes.h"
#include "graph-manager.h"
@@ -30,6 +31,7 @@
using json = nlohmann::json;
VISEQ::APP::CANVAS::Canvas canvas;
VISEQ::APP::AppManager app;
std::shared_ptr<VISEQ::APP::State> state = VISEQ::APP::State::get();
@@ -38,8 +40,6 @@ Vec2Df mouse_pos;
Vec2Df drag_start;
//Vec2Df global_mouse;
Camera2D camera = { {0} };
bool mouse_clicked = false;
bool mouse_touched = false;
bool mouse_dragging = false;
@@ -74,10 +74,10 @@ int main(){
SetTargetFPS(state->settings.fps);
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 };
camera.rotation = 0.0f;
camera.zoom = 1.0f;
canvas.camera.target = (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 };
canvas.camera.rotation = 0.0f;
canvas.camera.zoom = 1.0f;
if(std::filesystem::exists("state.json")){
std::ifstream f("state.json");
@@ -171,40 +171,15 @@ int main(){
float drag_distance = 0;
VEC_SPTR_Node copied_nodes;
Vec2Df modified_mouse;
while(!WindowShouldClose()){
BeginDrawing();
ClearBackground((Color){46,46,46});
BeginMode2D(camera);
canvas.drawStart();
state->settings.speed = (state->settings.grid * 16.0f) * (state->settings.bpm / 120.f);
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);
state->update(canvas.camera);
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)){*/
/* float steps = round((dragNode->neighbors.front()->position.dist(mouse_pos)) / state->settings.grid) * state->settings.grid;*/
/* if(dragNode->neighbors.size() > 1){*/
@@ -315,7 +290,7 @@ int main(){
/* drag_distance = 0;*/
/* }*/
/* if(mouse_right_down) {*/
/* camera.target = Vector2Add(camera.target, delta);*/
/* canvas.camera.target = Vector2Add(canvas.camera.target, delta);*/
/* drag_distance += delta.length();*/
/* }*/
/* if(mouse_right_released && drag_distance < 5) {*/
@@ -514,32 +489,12 @@ int main(){
/* break;*/
/* }*/
/*};*/
if(mouse_right_down) {
camera.target = Vector2Add(camera.target, delta);
}
float wheel = GetMouseWheelMove();
if(wheel != 0){
if(!IsKeyDown(KEY_LEFT_SHIFT) && !IsKeyDown(KEY_LEFT_CONTROL) && !IsKeyDown(KEY_LEFT_ALT)){
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);
}
}
canvas.draw();
app.draw(state->screen_mouse, canvas.camera);
canvas.drawEnd();
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});
}
}
app.draw(mouse_pos, camera);
EndMode2D();
app.drawUI(&mouse_pos, &camera);
app.drawUI(state->screen_mouse, canvas.camera);
EndDrawing();
}