From d05147fa6918f7d430c5bcd123d4e553b5d45433 Mon Sep 17 00:00:00 2001 From: IXtreme Date: Wed, 1 Jan 2025 21:48:48 -0500 Subject: [PATCH] Add cool record spin feature --- src/main.cpp | 174 +++++++++++++++++++++++++-------------- src/mpris_connection.cpp | 43 ++++++++++ src/mpris_connection.hpp | 16 ++++ 3 files changed, 169 insertions(+), 64 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 17aba02..1155155 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,11 +20,11 @@ #include "stdio.h" +#include #include #include #include #include -#include extern "C" { #include "testc.h" } @@ -126,9 +126,9 @@ std::vector GetMprisServices() { } void DrawPlay(Vector2 Pos, float scale, Color color) { - Vector2 backtop = (Vector2) { .x=-0.29f, .y=-0.40f }; - Vector2 backbot = (Vector2) { .x=-0.29f, .y=0.40f}; - Vector2 front = (Vector2) {.x = 0.5f, .y=0}; + Vector2 backtop = (Vector2){.x = -0.29f, .y = -0.40f}; + Vector2 backbot = (Vector2){.x = -0.29f, .y = 0.40f}; + Vector2 front = (Vector2){.x = 0.5f, .y = 0}; backtop = Vector2Scale(backtop, scale); backbot = Vector2Scale(backbot, scale); @@ -142,9 +142,9 @@ void DrawPlay(Vector2 Pos, float scale, Color color) { } void DrawNext(Vector2 Pos, float scale, Color color) { - Vector2 backtop = (Vector2) { .x=-0.35f, .y=-0.35f }; - Vector2 backbot = (Vector2) { .x=-0.35f, .y=0.35f}; - Vector2 front = (Vector2) {.x = 0.28f, .y=0}; + Vector2 backtop = (Vector2){.x = -0.35f, .y = -0.35f}; + Vector2 backbot = (Vector2){.x = -0.35f, .y = 0.35f}; + Vector2 front = (Vector2){.x = 0.28f, .y = 0}; backtop = Vector2Scale(backtop, scale); backbot = Vector2Scale(backbot, scale); @@ -156,26 +156,22 @@ void DrawNext(Vector2 Pos, float scale, Color color) { DrawTriangle(backtop, backbot, front, color); - Vector2 topleft = (Vector2) {.x = -0.29f, .y = -0.4f }; + Vector2 topleft = (Vector2){.x = -0.29f, .y = -0.4f}; - - - Vector2 bar2topleft = Vector2Add(topleft, (Vector2) {0.706f*0.6f, .y=0}); + Vector2 bar2topleft = Vector2Add(topleft, (Vector2){0.706f * 0.6f, .y = 0}); bar2topleft = Vector2Scale(bar2topleft, scale); bar2topleft = Vector2Add(bar2topleft, Pos); - Vector2 size = (Vector2) {.x=0.706f *0.2f, .y = 0.8f }; + Vector2 size = (Vector2){.x = 0.706f * 0.2f, .y = 0.8f}; size = Vector2Scale(size, scale); - DrawRectangleV(bar2topleft, size, color); } - void DrawBack(Vector2 Pos, float scale, Color color) { - Vector2 backtop = (Vector2) { .x=0.35f, .y=-0.35f }; - Vector2 backbot = (Vector2) { .x=0.35f, .y=0.35f}; - Vector2 front = (Vector2) {.x = -0.28f, .y=0}; + Vector2 backtop = (Vector2){.x = 0.35f, .y = -0.35f}; + Vector2 backbot = (Vector2){.x = 0.35f, .y = 0.35f}; + Vector2 front = (Vector2){.x = -0.28f, .y = 0}; backtop = Vector2Scale(backtop, scale); backbot = Vector2Scale(backbot, scale); @@ -185,27 +181,25 @@ void DrawBack(Vector2 Pos, float scale, Color color) { backbot = Vector2Add(backbot, Pos); front = Vector2Add(front, Pos); - //DrawTriangle(backtop, backbot, front, color); + // DrawTriangle(backtop, backbot, front, color); DrawTriangle(front, backbot, backtop, color); - Vector2 topleft = (Vector2) {.x = -0.29f, .y = -0.4f }; - + Vector2 topleft = (Vector2){.x = -0.29f, .y = -0.4f}; Vector2 bar1topleft = Vector2Scale(topleft, scale); bar1topleft = Vector2Add(bar1topleft, Pos); - Vector2 size = (Vector2) {.x=0.706f *0.2f, .y = 0.8f }; + Vector2 size = (Vector2){.x = 0.706f * 0.2f, .y = 0.8f}; size = Vector2Scale(size, scale); - DrawRectangleV(bar1topleft, size, color); } void DrawStop(Vector2 Pos, float scale, Color color) { - Vector2 topleft = (Vector2) {.x = -0.353f, .y = -0.353f }; - Vector2 botright = (Vector2) {.x = 0.353f, .y = 0.353f }; + Vector2 topleft = (Vector2){.x = -0.353f, .y = -0.353f}; + Vector2 botright = (Vector2){.x = 0.353f, .y = 0.353f}; topleft = Vector2Scale(topleft, scale); - botright = Vector2Scale(botright, scale*2); + botright = Vector2Scale(botright, scale * 2); topleft = Vector2Add(topleft, Pos); @@ -213,17 +207,16 @@ void DrawStop(Vector2 Pos, float scale, Color color) { } void DrawPause(Vector2 Pos, float scale, Color color) { - Vector2 topleft = (Vector2) {.x = -0.353f, .y = -0.353f }; - + Vector2 topleft = (Vector2){.x = -0.353f, .y = -0.353f}; Vector2 bar1topleft = Vector2Scale(topleft, scale); bar1topleft = Vector2Add(bar1topleft, Pos); - Vector2 bar2topleft = Vector2Add(topleft, (Vector2) {0.706f*0.6f, .y=0}); + Vector2 bar2topleft = Vector2Add(topleft, (Vector2){0.706f * 0.6f, .y = 0}); bar2topleft = Vector2Scale(bar2topleft, scale); bar2topleft = Vector2Add(bar2topleft, Pos); - Vector2 size = (Vector2) {.x=0.706f *0.4f, .y = 0.706f }; + Vector2 size = (Vector2){.x = 0.706f * 0.4f, .y = 0.706f}; size = Vector2Scale(size, scale); topleft = Vector2Add(topleft, Pos); @@ -233,9 +226,19 @@ void DrawPause(Vector2 Pos, float scale, Color color) { } float ScaleToFit(Vector2 src, Vector2 dst) { - float ratio = std::min(dst.x/src.x, dst.y/src.y); + float ratio = std::min(dst.x / src.x, dst.y / src.y); return ratio; - //return Vector2Scale(src, ratio); + // return Vector2Scale(src, ratio); +} + +void GenCircleOverlay(RenderTexture tex) { + BeginTextureMode(tex); + ClearBackground(BLACK); + BeginBlendMode(BLEND_SUBTRACT_COLORS); + DrawCircle(GetScreenWidth()/2, GetScreenHeight()/2, std::min(GetScreenWidth()/2, GetScreenHeight()/2), (Color{0,0,0,0})); + EndBlendMode(); + EndTextureMode(); + } int main(int argc, char *argv[]) { @@ -265,11 +268,22 @@ int main(int argc, char *argv[]) { auto current_player = MprisPlayer("org.mpris.MediaPlayer2.playerctld"); double lasttime = 0.0; + bool discmode = false; + DiscObject disc(0); // Main game loop - while (!WindowShouldClose() && - run) // Detect window close button or ESC key, or a quit from the menu + bool goodstate = false; + + RenderTexture recordoverlay = LoadRenderTexture(screenWidth, screenHeight); + GenCircleOverlay(recordoverlay); + + while (!WindowShouldClose() && run) // Detect window close button or ESC key, or a quit from the menu { - if (GetTime() > lasttime+3 ) { + if (IsWindowResized()) { + UnloadRenderTexture(recordoverlay); + recordoverlay = LoadRenderTexture(GetScreenWidth(), GetScreenHeight()); + GenCircleOverlay(recordoverlay); + } + if (GetTime() > lasttime + 3) { lasttime = GetTime(); current_player.Refresh(); } @@ -277,61 +291,88 @@ int main(int argc, char *argv[]) { BeginDrawing(); ClearBackground(BLACK); - Vector2 winwidth = (Vector2) {(float) GetScreenWidth(), (float) GetScreenHeight()}; - Vector2 imagesize = (Vector2) {(float) current_player.tex.tex.width, (float) current_player.tex.tex.height}; + Vector2 winwidth = (Vector2){(float)GetScreenWidth(), (float)GetScreenHeight()}; + Vector2 imagesize = (Vector2){(float)current_player.tex.tex.width, (float)current_player.tex.tex.height}; Vector2 scaledsize = Vector2Scale(imagesize, ScaleToFit(imagesize, winwidth)); Vector2 fpos; - fpos.x = (winwidth.x *0.5) - (scaledsize.x * 0.5f); - fpos.y = (winwidth.y *0.5) - (scaledsize.y * 0.5f); - DrawTextureEx(current_player.tex.tex, fpos, 0, ScaleToFit(imagesize, winwidth), WHITE); + fpos.x = (winwidth.x * 0.5) - (scaledsize.x * 0.5f); + fpos.y = (winwidth.y * 0.5) - (scaledsize.y * 0.5f); + // DrawTextureEx(current_player.tex.tex, fpos, 0, ScaleToFit(imagesize, winwidth), WHITE); + Rectangle src = (Rectangle){0, 0, (float)current_player.tex.tex.width, (float)current_player.tex.tex.height}; + Rectangle dst = (Rectangle){fpos.x + scaledsize.x / 2, fpos.y + scaledsize.y / 2, scaledsize.x, scaledsize.y}; + DrawTexturePro(current_player.tex.tex, src, dst, (Vector2){dst.width / 2, dst.height / 2}, disc.pos, WHITE); - - if ( IsWindowFocused() ) { + if (IsWindowFocused()) { const float buttonsize = 0.1; - Vector2 screen = (Vector2) {(float) GetScreenWidth(), (float) GetScreenHeight()}; + Vector2 screen = (Vector2){(float)GetScreenWidth(), (float)GetScreenHeight()}; Vector2 centerline; centerline.x = screen.x / 2; centerline.y = screen.y * 0.8; Color transblack = BLACK; transblack.a = (200); - DrawCircleV(centerline, 0.05*GetScreenWidth(), transblack); + DrawCircleV(centerline, 0.05 * GetScreenWidth(), transblack); if (current_player.playstate == "Playing") { - DrawPause(centerline, 0.1*GetScreenWidth(), WHITE); + DrawPause(centerline, 0.1 * GetScreenWidth(), WHITE); } else { - DrawPlay(centerline, 0.1*GetScreenWidth(), WHITE); + DrawPlay(centerline, 0.1 * GetScreenWidth(), WHITE); } - Vector2 next = Vector2Add(centerline, (Vector2) {screen.x * 0.2f, 0.0f}); - DrawCircleV(next, 0.05*GetScreenWidth(), transblack); - DrawNext(next, 0.1*GetScreenWidth(), WHITE); + Vector2 next = Vector2Add(centerline, (Vector2){screen.x * 0.2f, 0.0f}); + DrawCircleV(next, 0.05 * GetScreenWidth(), transblack); + DrawNext(next, 0.1 * GetScreenWidth(), WHITE); - Vector2 back = Vector2Add(centerline, (Vector2) {screen.x * -0.2f, 0.0f}); - DrawCircleV(back, 0.05*GetScreenWidth(), transblack); - DrawBack(back, 0.1*GetScreenWidth(), WHITE); + Vector2 back = Vector2Add(centerline, (Vector2){screen.x * -0.2f, 0.0f}); + DrawCircleV(back, 0.05 * GetScreenWidth(), transblack); + DrawBack(back, 0.1 * GetScreenWidth(), WHITE); - if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && CheckCollisionPointCircle(GetMousePosition(), centerline, 0.05*GetScreenWidth()) ) { + if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && CheckCollisionPointCircle(GetMousePosition(), centerline, 0.05 * GetScreenWidth())) { current_player.PausePlay(); current_player.playstate = current_player.playstate == "Playing" ? "Paused" : "Playing"; - } - - if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && CheckCollisionPointCircle(GetMousePosition(), next, 0.05*GetScreenWidth()) ) { + } else if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && CheckCollisionPointCircle(GetMousePosition(), next, 0.05 * GetScreenWidth())) { current_player.Next(); current_player.playstate = current_player.playstate == "Playing" ? "Paused" : "Playing"; - } - - if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && CheckCollisionPointCircle(GetMousePosition(), back, 0.05*GetScreenWidth()) ) { + } else if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && CheckCollisionPointCircle(GetMousePosition(), back, 0.05 * GetScreenWidth())) { current_player.Prev(); current_player.playstate = current_player.playstate == "Playing" ? "Paused" : "Playing"; + } else if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) { + float initmouserot; + Vector2 initmousepos; + if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { + initmouserot = disc.pos; + initmousepos = GetMousePosition(); + initmousepos = Vector2Add(initmousepos, (Vector2){-screen.x / 2, -screen.y / 2}); + goodstate = true; + } + if (goodstate) { + Vector2 mouse = GetMousePosition(); + Vector2 mousecentered = Vector2Add(mouse, (Vector2){-screen.x / 2, -screen.y / 2}); + disc.velo = 0; + disc.UpdatePos(initmouserot + (((180.0f * atan2(mousecentered.y, mousecentered.x)) / M_PI) - ((180.0f * atan2(initmousepos.y, initmousepos.x)) / M_PI)), GetFrameTime()); + } + } + } + if (!goodstate) { + disc.UpdatePos(GetFrameTime()); + // Ease from velo to target + if (current_player.playstate == "Playing") { + disc.target = 100; + } else { + disc.target = 0; + } + disc.accel = disc.target - disc.velo; + } + if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) { + goodstate = false; } - + if (disc.active) { + DrawTexture(recordoverlay.texture, 0, 0 , WHITE); } - if (IsKeyPressed(KEY_SLASH) || IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) { showIMgui = !showIMgui; } @@ -364,7 +405,9 @@ int main(int argc, char *argv[]) { if (ImGui::BeginMenu("Window")) { if (ImGui::MenuItem("Demo Window", nullptr, showDemoWindow)) showDemoWindow = !showDemoWindow; - + if (ImGui::MenuItem("Record Spin !!", nullptr, disc.active)) { + disc.Activate(); + } ImGui::EndMenu(); } ImGui::EndMainMenuBar(); @@ -375,7 +418,6 @@ int main(int argc, char *argv[]) { if (ImGui::Begin("Player Control")) { ImGui::Text("%s", current_player.GetIdentity().c_str()); - if (ImGui::Button("Prev")) { current_player.Prev(); } @@ -392,8 +434,12 @@ int main(int argc, char *argv[]) { current_player.Refresh(); current_player.UpdateTexture(); } - //rlImGuiImage(¤t_player.tex.tex); - rlImGuiImageSize(¤t_player.tex.tex,300,300); + + // ImGui::SliderFloat("erm", &rotation, -180, 180, "Rotation"); + // rlImGuiImage(¤t_player.tex.tex); + ImGui::Separator(); + ImGui::LabelText("Disc Info", "P: %f, V: %f, A: %f", disc.pos, disc.velo, disc.accel); + rlImGuiImageSize(¤t_player.tex.tex, 300, 300); } ImGui::End(); diff --git a/src/mpris_connection.cpp b/src/mpris_connection.cpp index 3b09de4..a99b3d1 100644 --- a/src/mpris_connection.cpp +++ b/src/mpris_connection.cpp @@ -177,6 +177,49 @@ void MprisPlayer::UpdateTexture() { } } +DiscObject::DiscObject(float ia) { + pos = 0; + prevouspos = 0; + velo = 0; + accel = 0; + target = ia; + active = true; +} + +void DiscObject::Activate() { + active = !active; + pos = 0; + prevouspos = 0; + velo = 0; + accel = 0; + target = 0; +} + + + +void DiscObject::UpdatePos(float dtime) { + if (active) { + prevouspos = pos; + pos = pos + velo * dtime; + velo = velo + accel * dtime; + accel = target; + } +} + +void DiscObject::UpdatePos(float new_pos, float dtime) { + if (active) { + prevouspos = pos; + pos = new_pos; + velo = (pos - prevouspos) / dtime; + accel = target; + } +} + + +void DiscObject::AddAccel(float ac) { + accel = accel + ac; +} + RayTexture::RayTexture() { auto rtex = LoadRenderTexture(300,300); BeginTextureMode(rtex); diff --git a/src/mpris_connection.hpp b/src/mpris_connection.hpp index 18a4b6b..c48e43f 100644 --- a/src/mpris_connection.hpp +++ b/src/mpris_connection.hpp @@ -22,6 +22,22 @@ class RayTexture { ~RayTexture(); }; +class DiscObject { +public: + float prevouspos; + float pos; + float velo; + float accel; + float target; + bool active; + DiscObject(float ia); + void UpdatePos(float dtime); + void UpdatePos(float pos, float dtime); + void AddAccel(float ac); + void Activate(); +}; + + class MprisPlayer { public: std::unique_ptr bus_connection;