Add swipe to like and dislike
This commit is contained in:
parent
6a38b6df53
commit
73a9f70467
18
inc/App.hpp
18
inc/App.hpp
@ -17,12 +17,24 @@ private:
|
|||||||
|
|
||||||
int screenWidth, screenHeight;
|
int screenWidth, screenHeight;
|
||||||
Canvas canvas;
|
Canvas canvas;
|
||||||
RenderTexture2D canvasTexure = {0};
|
|
||||||
|
|
||||||
Rectangle dest;
|
int pos = 0;
|
||||||
|
std::array<RenderTexture2D, 2> 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 likeBox;
|
||||||
Rectangle disLikeBox;
|
Rectangle disLikeBox;
|
||||||
|
|
||||||
Unit unit;
|
std::array<Unit, 2> unit = {0};
|
||||||
|
|
||||||
DnaManager manager;
|
DnaManager manager;
|
||||||
};
|
};
|
||||||
|
@ -7,9 +7,9 @@
|
|||||||
|
|
||||||
enum Liked
|
enum Liked
|
||||||
{
|
{
|
||||||
|
tbd,
|
||||||
yes,
|
yes,
|
||||||
no,
|
no
|
||||||
tbd
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Unit
|
struct Unit
|
||||||
|
123
src/App.cpp
123
src/App.cpp
@ -7,18 +7,40 @@
|
|||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
#include <raymath.h>
|
#include <raymath.h>
|
||||||
|
|
||||||
|
#define TOP 1 - pos
|
||||||
|
#define BOTTOM pos
|
||||||
|
|
||||||
void App::init(int screenWidth, int screenHeight)
|
void App::init(int screenWidth, int screenHeight)
|
||||||
{
|
{
|
||||||
this->screenWidth = screenWidth;
|
this->screenWidth = screenWidth;
|
||||||
this->screenHeight = screenHeight;
|
this->screenHeight = screenHeight;
|
||||||
this->canvas.init(screenWidth);
|
this->canvas.init(screenWidth);
|
||||||
|
|
||||||
canvasTexure = LoadRenderTexture(screenWidth, screenWidth);
|
for (size_t i = 0; i < canvasTexure.size(); i++)
|
||||||
|
{
|
||||||
|
canvasTexure[i] = LoadRenderTexture(screenWidth, screenWidth);
|
||||||
|
}
|
||||||
|
|
||||||
manager.init();
|
manager.init();
|
||||||
upTex(Liked::tbd);
|
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;
|
float posY = (screenHeight - screenWidth) / 2.0f;
|
||||||
|
|
||||||
|
destB = {0, posY, (float)screenWidth, (float)screenWidth};
|
||||||
|
destA = destB;
|
||||||
|
|
||||||
float recPosX = screenWidth * 0.2f;
|
float recPosX = screenWidth * 0.2f;
|
||||||
dest = {0, posY, (float)screenWidth, (float)screenWidth};
|
|
||||||
disLikeBox = {0, posY, (float)recPosX, (float)screenWidth};
|
disLikeBox = {0, posY, (float)recPosX, (float)screenWidth};
|
||||||
likeBox = {screenWidth - recPosX, 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)
|
if (liked != Liked::tbd)
|
||||||
{
|
{
|
||||||
unit.liked = liked;
|
unit[TOP].liked = liked;
|
||||||
manager.like(unit);
|
manager.like(unit[TOP]);
|
||||||
}
|
}
|
||||||
unit = manager.next();
|
unit[TOP] = manager.next();
|
||||||
if (unit.dna != nullptr)
|
if (unit[TOP].dna != nullptr)
|
||||||
{
|
{
|
||||||
canvas.newGen(canvasTexure, unit.dna);
|
canvas.newGen(canvasTexure[TOP], unit[TOP].dna);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
BeginTextureMode(canvasTexure);
|
BeginTextureMode(canvasTexure[TOP]);
|
||||||
ClearBackground(WHITE);
|
ClearBackground(BLACK);
|
||||||
DrawText("NEXT GEN", 10, 10, 20, BLACK);
|
DrawText("NEXT GEN", 10, 10, 20, WHITE);
|
||||||
EndTextureMode();
|
EndTextureMode();
|
||||||
}
|
}
|
||||||
|
|
||||||
void App::update()
|
void App::update()
|
||||||
{
|
{
|
||||||
bool isDone = canvas.tick(canvasTexure);
|
bool isDone = canvas.tick(canvasTexure[BOTTOM]);
|
||||||
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && isDone)
|
Vector2 mousePosition = GetMousePosition();
|
||||||
|
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
|
||||||
{
|
{
|
||||||
Vector2 mouse = GetMousePosition();
|
mouseStart = mousePosition;
|
||||||
if (CheckCollisionPointRec(mouse, disLikeBox))
|
validHit = CheckCollisionPointRec(mouseStart, destA);
|
||||||
{
|
len = Vector2Distance(mouseStart, {destB.x, destB.y});
|
||||||
upTex(Liked::no);
|
ofset = std::atan2(destB.x - mouseStart.x, destB.y - mouseStart.y);
|
||||||
}
|
}
|
||||||
if (CheckCollisionPointRec(mouse, likeBox))
|
|
||||||
|
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT) && validHit)
|
||||||
{
|
{
|
||||||
upTex(Liked::yes);
|
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))
|
||||||
|
{
|
||||||
|
topLiked = Liked::no;
|
||||||
}
|
}
|
||||||
|
else if (CheckCollisionPointRec(mousePosition, likeBox))
|
||||||
|
{
|
||||||
|
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);
|
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};
|
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);
|
DrawTexturePro(canvasTexure[BOTTOM].texture, source, destB, origin, 0.0f, WHITE);
|
||||||
DrawText(text, dest.x + 10, dest.y - 30, 20, BLACK);
|
|
||||||
|
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()
|
void App::deinit()
|
||||||
{
|
{
|
||||||
UnloadRenderTexture(canvasTexure);
|
for (size_t i = 0; i < canvasTexure.size(); i++)
|
||||||
|
{
|
||||||
|
UnloadRenderTexture(canvasTexure[i]);
|
||||||
|
}
|
||||||
canvas.deinit();
|
canvas.deinit();
|
||||||
manager.deinit();
|
manager.deinit();
|
||||||
}
|
}
|
@ -114,6 +114,11 @@ void DnaManager::saveVec()
|
|||||||
|
|
||||||
Unit DnaManager::next()
|
Unit DnaManager::next()
|
||||||
{
|
{
|
||||||
|
if (queued >= NUM_PER_GEN)
|
||||||
|
{
|
||||||
|
return {nullptr, Liked::tbd, -1};
|
||||||
|
}
|
||||||
|
|
||||||
Dna *ret = &vector[queued];
|
Dna *ret = &vector[queued];
|
||||||
int index = queued++;
|
int index = queued++;
|
||||||
return {ret, Liked::tbd, index};
|
return {ret, Liked::tbd, index};
|
||||||
@ -129,7 +134,7 @@ void DnaManager::like(Unit unit)
|
|||||||
|
|
||||||
if (found == -1)
|
if (found == -1)
|
||||||
{
|
{
|
||||||
TraceLog(LOG_ERROR, "NOT FOUND UNIT");
|
// RUN OUT OF GEN WAITING FOR NEW GEN
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (unit.liked == Liked::yes)
|
if (unit.liked == Liked::yes)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user