From 73a9f70467aa60a74d20ad583125e3dcd63b3794 Mon Sep 17 00:00:00 2001 From: Nikola Petrov Date: Wed, 15 Jan 2025 15:08:09 +0100 Subject: [PATCH] Add swipe to like and dislike --- inc/App.hpp | 18 +++++- inc/values/DnaManager.hpp | 4 +- src/App.cpp | 121 +++++++++++++++++++++++++++++++------- src/values/DnaManager.cpp | 7 ++- 4 files changed, 122 insertions(+), 28 deletions(-) diff --git a/inc/App.hpp b/inc/App.hpp index b242e7e..e4a9b1c 100644 --- a/inc/App.hpp +++ b/inc/App.hpp @@ -17,12 +17,24 @@ private: int screenWidth, screenHeight; Canvas canvas; - RenderTexture2D canvasTexure = {0}; - Rectangle dest; + int pos = 0; + std::array canvasTexure = {0}; + + float rotation = 0.0f; + Vector2 mouseStart; + bool validHit = false; + float len; + float ofset; + + Liked topLiked = Liked::tbd; + + Rectangle destA; + Rectangle destB; Rectangle likeBox; Rectangle disLikeBox; - Unit unit; + std::array unit = {0}; + DnaManager manager; }; diff --git a/inc/values/DnaManager.hpp b/inc/values/DnaManager.hpp index d0993cb..5639f35 100644 --- a/inc/values/DnaManager.hpp +++ b/inc/values/DnaManager.hpp @@ -7,9 +7,9 @@ enum Liked { + tbd, yes, - no, - tbd + no }; struct Unit diff --git a/src/App.cpp b/src/App.cpp index 1f160e5..06ec688 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -7,18 +7,40 @@ #include #include +#define TOP 1 - pos +#define BOTTOM pos + void App::init(int screenWidth, int screenHeight) { this->screenWidth = screenWidth; this->screenHeight = screenHeight; this->canvas.init(screenWidth); - canvasTexure = LoadRenderTexture(screenWidth, screenWidth); + for (size_t i = 0; i < canvasTexure.size(); i++) + { + canvasTexure[i] = LoadRenderTexture(screenWidth, screenWidth); + } + manager.init(); upTex(Liked::tbd); + while (!canvas.tick(canvasTexure[TOP])) + { + // wait to finish drawing + } + pos = 1 - pos; + upTex(Liked::tbd); + while (!canvas.tick(canvasTexure[TOP])) + { + // wait to finish drawing + } + pos = 1 - pos; + float posY = (screenHeight - screenWidth) / 2.0f; + + destB = {0, posY, (float)screenWidth, (float)screenWidth}; + destA = destB; + float recPosX = screenWidth * 0.2f; - dest = {0, posY, (float)screenWidth, (float)screenWidth}; disLikeBox = {0, posY, (float)recPosX, (float)screenWidth}; likeBox = {screenWidth - recPosX, posY, (float)recPosX, (float)screenWidth}; } @@ -27,36 +49,73 @@ void App::upTex(Liked liked) { if (liked != Liked::tbd) { - unit.liked = liked; - manager.like(unit); + unit[TOP].liked = liked; + manager.like(unit[TOP]); } - unit = manager.next(); - if (unit.dna != nullptr) + unit[TOP] = manager.next(); + if (unit[TOP].dna != nullptr) { - canvas.newGen(canvasTexure, unit.dna); + canvas.newGen(canvasTexure[TOP], unit[TOP].dna); return; } - BeginTextureMode(canvasTexure); - ClearBackground(WHITE); - DrawText("NEXT GEN", 10, 10, 20, BLACK); + BeginTextureMode(canvasTexure[TOP]); + ClearBackground(BLACK); + DrawText("NEXT GEN", 10, 10, 20, WHITE); EndTextureMode(); } void App::update() { - bool isDone = canvas.tick(canvasTexure); - if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && isDone) + bool isDone = canvas.tick(canvasTexure[BOTTOM]); + Vector2 mousePosition = GetMousePosition(); + if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { - Vector2 mouse = GetMousePosition(); - if (CheckCollisionPointRec(mouse, disLikeBox)) + mouseStart = mousePosition; + validHit = CheckCollisionPointRec(mouseStart, destA); + len = Vector2Distance(mouseStart, {destB.x, destB.y}); + ofset = std::atan2(destB.x - mouseStart.x, destB.y - mouseStart.y); + } + + if (IsMouseButtonDown(MOUSE_BUTTON_LEFT) && validHit) + { + float dist = mousePosition.x - mouseStart.x; + float l = dist / screenWidth; + rotation = Lerp(45.0f, -45.0f, (l + 1) / 2); + + float angle = ((rotation)*PI) / 180.0f; + angle += ofset; + Vector2 newCenter = {.x = len * std::sin(angle), .y = len * std::cos(angle)}; + + destA.x = newCenter.x + mousePosition.x; + destA.y = newCenter.y + mousePosition.y; + + if (CheckCollisionPointRec(mousePosition, disLikeBox)) { - upTex(Liked::no); + topLiked = Liked::no; } - if (CheckCollisionPointRec(mouse, likeBox)) + else if (CheckCollisionPointRec(mousePosition, likeBox)) { - upTex(Liked::yes); + topLiked = Liked::yes; } + else + { + topLiked = Liked::tbd; + } + } + + if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT) && validHit) + { + if (isDone) + { + if (topLiked != Liked::tbd) + { + upTex(topLiked); + pos = 1 - pos; // switch bottom and top + } + } + rotation = 0.0f; + destA = destB; } } @@ -64,17 +123,35 @@ void App::draw() { ClearBackground(BLUE); - Rectangle source = {0, 0, (float)canvasTexure.texture.width, (float)-canvasTexure.texture.height}; + Rectangle source = {0, 0, (float)screenWidth, (float)-screenWidth}; Vector2 origin = {0.0f, 0.0f}; - DrawTexturePro(canvasTexure.texture, source, dest, origin, 0.0f, WHITE); - const char *text = TextFormat("GEN %d: %d / %d", manager.generation, unit.index + 1, NUM_PER_GEN); - DrawText(text, dest.x + 10, dest.y - 30, 20, BLACK); + DrawTexturePro(canvasTexure[BOTTOM].texture, source, destB, origin, 0.0f, WHITE); + + DrawTexturePro(canvasTexure[TOP].texture, source, destA, origin, 360 - rotation, WHITE); + + const char *text = TextFormat("GEN %d: %d / %d", manager.generation, unit[TOP].index + 1, NUM_PER_GEN); + DrawText(text, destB.x + 10, destB.y - 30, 20, BLACK); + + switch (topLiked) + { + case Liked::yes: + DrawText("LIKED", destB.x + 10, destB.y - 50, 20, BLACK); + break; + case Liked::no: + DrawText("DISLIKE", destB.x + 10, destB.y - 50, 20, BLACK); + break; + default: + break; + } } void App::deinit() { - UnloadRenderTexture(canvasTexure); + for (size_t i = 0; i < canvasTexure.size(); i++) + { + UnloadRenderTexture(canvasTexure[i]); + } canvas.deinit(); manager.deinit(); } \ No newline at end of file diff --git a/src/values/DnaManager.cpp b/src/values/DnaManager.cpp index 0265fcc..1bd6d2a 100644 --- a/src/values/DnaManager.cpp +++ b/src/values/DnaManager.cpp @@ -114,6 +114,11 @@ void DnaManager::saveVec() Unit DnaManager::next() { + if (queued >= NUM_PER_GEN) + { + return {nullptr, Liked::tbd, -1}; + } + Dna *ret = &vector[queued]; int index = queued++; return {ret, Liked::tbd, index}; @@ -129,7 +134,7 @@ void DnaManager::like(Unit unit) if (found == -1) { - TraceLog(LOG_ERROR, "NOT FOUND UNIT"); + // RUN OUT OF GEN WAITING FOR NEW GEN return; } if (unit.liked == Liked::yes)