La tarjeta d'ajuda ja entra i ix amb animacions suavitzades

This commit is contained in:
2024-10-02 18:03:20 +02:00
parent 9b3e549876
commit 5ebc58dd01
2 changed files with 31 additions and 10 deletions

View File

@@ -6,7 +6,9 @@
#include "texture.h"
#include "lang.h"
#include "param.h"
#include "dbgtxt.h"
#include <memory> // Para std::unique_ptr
#include "utils.h"
// [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado
OnScreenHelp *OnScreenHelp::onScreenHelp = nullptr;
@@ -51,7 +53,7 @@ OnScreenHelp *OnScreenHelp::get()
}
// Constructor
OnScreenHelp::OnScreenHelp() : state(OnScreenHelpStatus::hidden)
OnScreenHelp::OnScreenHelp() : state(OnScreenHelpStatus::hidden), index(0)
{
setSize();
@@ -59,6 +61,7 @@ OnScreenHelp::OnScreenHelp() : state(OnScreenHelpStatus::hidden)
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
fillTexture();
precalculatePath(hiddenPos, showingPos, 60);
}
// Destructor
@@ -157,7 +160,7 @@ void OnScreenHelp::toggleState()
state = OnScreenHelpStatus::exitting;
}
if (state == OnScreenHelpStatus::hidden || state == OnScreenHelpStatus::exitting)
else if (state == OnScreenHelpStatus::hidden || state == OnScreenHelpStatus::exitting)
{
state = OnScreenHelpStatus::entering;
}
@@ -193,17 +196,17 @@ void OnScreenHelp::updatePosition()
switch (state)
{
case OnScreenHelpStatus::hidden:
dest.x = hiddenPos;
index = 0;
break;
case OnScreenHelpStatus::showing:
dest.x = showingPos;
index = path.size() - 1;
break;
case OnScreenHelpStatus::exitting:
if (dest.x > hiddenPos)
if (index > 0)
{
dest.x -= 8;
index--;
}
else
{
@@ -212,9 +215,9 @@ void OnScreenHelp::updatePosition()
break;
case OnScreenHelpStatus::entering:
if (dest.x < showingPos)
if (index < (int)path.size() - 1)
{
dest.x += 8;
index++;
}
else
{
@@ -226,6 +229,18 @@ void OnScreenHelp::updatePosition()
break;
}
dest.x = std::max(hiddenPos, dest.x);
dest.x = std::min(showingPos, dest.x);
dest.x = path[index];
}
// Rellena los puntos por donde pasa la animación
void OnScreenHelp::precalculatePath(double start, double end, int steps)
{
path.reserve(steps);
for (int i = 0; i <= steps; ++i)
{
double t = static_cast<double>(i) / steps;
double value = start + (end - start) * easeInOutSine(t);
path.push_back(static_cast<int>(value));
}
}

View File

@@ -2,6 +2,7 @@
#include <SDL2/SDL.h>
#include "text.h"
#include <vector>
class Screen;
class Asset;
class Input;
@@ -28,6 +29,8 @@ private:
int hiddenPos, showingPos; // Las dos posiciones donde colocar la textura
OnScreenHelpStatus state; // Estado del objeto
std::vector<int> path; // Puntos por donde pasa la animación de la tarjeta
int index; // Indice para recorrer la animación
// [SINGLETON] Ahora el constructor y el destructor son privados, para no poder crear objetos screen desde fuera
@@ -52,6 +55,9 @@ private:
// Actualiza la posición
void updatePosition();
// Rellena los puntos por donde pasa la animación
void precalculatePath(double start, double end, int steps);
public:
// [SINGLETON] Crearemos el objeto screen con esta función estática
static void init();