-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extrae ControladorClicks y RealizadorBase TODO: estructura
- Loading branch information
1 parent
6f027cd
commit fb2ba4e
Showing
5 changed files
with
151 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#include "controlador_clicks.h" | ||
#include "comandos.h" | ||
#include "estado_nivel.h" | ||
#include "globales.h" | ||
#include "realizador_base.h" | ||
#include "vista/botones_app.h" | ||
|
||
std::optional<Comando> ControladorClicks::genera_comando( | ||
const std::function<bool(const BotonConTexto &boton)> &pulsado, // | ||
const BotonesApp &botones, // | ||
const FaseNivel fase_actual // | ||
) { | ||
// Fijos | ||
if (pulsado(botones.generales.salir)) { | ||
return Comando::Salir{}; | ||
} else if (pulsado(botones.generales.reiniciar)) { | ||
return Comando::Reiniciar{}; | ||
} else if (pulsado(botones.generales.alternar_grid)) { | ||
return Comando::AlternarGrid{}; | ||
} | ||
// Dependientes de la fase | ||
switch (fase_actual) { | ||
case FaseNivel::MostrandoInstrucciones: | ||
if (pulsado(botones.empezar)) { | ||
return Comando::Empezar{}; | ||
} | ||
break; | ||
case FaseNivel::Activa: | ||
for (const auto &[tp, boton] : botones.encargar) { | ||
if (pulsado(boton)) { | ||
return Comando::Encargar{tp}; | ||
} | ||
} | ||
for (const auto &[tp, boton] : botones.despachar) { | ||
if (pulsado(boton)) { | ||
return Comando::Despachar{tp}; | ||
} | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
return std::nullopt; | ||
} | ||
|
||
#define SWITCH(variante) \ | ||
using T = std::decay_t<decltype(variante)>; \ | ||
if (false) { /* Para inicializar los bloques if else */ \ | ||
} | ||
#define CASE(comando, accion) \ | ||
else if constexpr (std::is_same_v<T, Comando::comando>) { \ | ||
return accion; \ | ||
} | ||
/* Aplica un comando y devuelve la nueva fase, si correspondiera cambiar */ | ||
std::optional<FaseNivel> ControladorClicks::aplica_comando( // | ||
RealizadorBase &realizador, // | ||
const Comando &comando // | ||
|
||
) { | ||
return std::visit( | ||
[&realizador](auto &&variante) -> std::optional<FaseNivel> { | ||
SWITCH(variante) | ||
CASE(Empezar, realizador.empezar()) | ||
CASE(Salir, FaseNivel::Saliendo) | ||
CASE(Reiniciar, FaseNivel::Reiniciando) | ||
CASE(AlternarGrid, realizador.alternar_grid()) | ||
CASE(Encargar, realizador.encargar_pizza(variante.tp)) | ||
CASE(Despachar, realizador.despachar_pizza(variante.tp)) | ||
return std::nullopt; | ||
}, | ||
comando.variante | ||
); | ||
} | ||
#undef SWITCH | ||
#undef CASE | ||
|
||
std::optional<FaseNivel> ControladorClicks::procesa_click( | ||
Globales &globales, // | ||
const BotonesApp &botones, // | ||
const Estado &estado, // | ||
RealizadorBase &realizador, // | ||
const sf::Vector2i &mouse_pos // | ||
|
||
) { | ||
const auto pulsado = [&globales, &mouse_pos](const BotonConTexto &boton) { | ||
return globales.detecta_colision(boton, mouse_pos); | ||
}; | ||
std::optional<Comando> comando = genera_comando( // | ||
pulsado, botones, estado.fase_actual | ||
); | ||
if (!comando) { | ||
return std::nullopt; | ||
} | ||
return aplica_comando(realizador, comando.value()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#pragma once | ||
|
||
#include <SFML/Graphics.hpp> | ||
#include <functional> | ||
#include <optional> | ||
|
||
struct BotonConTexto; | ||
struct BotonesApp; | ||
struct Estado; | ||
struct Globales; | ||
class RealizadorBase; | ||
class Comando; | ||
enum class FaseNivel; | ||
|
||
struct ControladorClicks { | ||
private: | ||
std::optional<Comando> ControladorClicks::genera_comando( | ||
const std::function<bool(const BotonConTexto &boton)> &pulsado, // | ||
const BotonesApp &, // | ||
const FaseNivel fase_actual // | ||
); | ||
std::optional<FaseNivel> aplica_comando( | ||
RealizadorBase &, // | ||
const Comando & // | ||
); | ||
|
||
public: | ||
std::optional<FaseNivel> procesa_click( | ||
Globales &, // | ||
const BotonesApp &, // | ||
const Estado &, // | ||
RealizadorBase &, // | ||
const sf::Vector2i &mouse_pos // | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#pragma once | ||
|
||
#include <optional> | ||
|
||
enum class FaseNivel; | ||
|
||
class RealizadorBase { | ||
public: | ||
using NuevaFase = std::optional<FaseNivel>; | ||
|
||
virtual ~RealizadorBase() = default; // Destructor virtual | ||
|
||
// Métodos virtuales puros | ||
virtual NuevaFase encargar_pizza(const modelo::TipoPizza tp) = 0; | ||
virtual NuevaFase despachar_pizza(const modelo::TipoPizza tp) = 0; | ||
virtual NuevaFase alternar_grid() = 0; | ||
virtual NuevaFase empezar() = 0; | ||
}; |