Files
coffee_crisis_arcade_edition/source/pause_manager.h
2025-08-10 13:17:36 +02:00

129 lines
4.0 KiB
C++

#pragma once
#include <cstdint>
#include <functional>
#include <string>
// Clase dedicada para manejar el sistema de pausa
class PauseManager {
public:
// Enum encapsulado dentro de la clase
enum class Source : uint8_t {
NONE = 0,
PLAYER = 1 << 0, // 0001
SERVICE_MENU = 1 << 1, // 0010
FOCUS_LOST = 1 << 2 // 0100
};
// Operadores para trabajar con las banderas (funciones friend)
friend Source operator|(Source a, Source b) {
return static_cast<Source>(static_cast<uint8_t>(a) | static_cast<uint8_t>(b));
}
friend Source operator&(Source a, Source b) {
return static_cast<Source>(static_cast<uint8_t>(a) & static_cast<uint8_t>(b));
}
friend Source operator~(Source a) {
return static_cast<Source>(~static_cast<uint8_t>(a));
}
friend Source& operator|=(Source& a, Source b) {
return a = a | b;
}
friend Source& operator&=(Source& a, Source b) {
return a = a & b;
}
private:
Source flags_ = Source::NONE;
std::function<void(bool)> onPauseChangedCallback_;
bool hasFlag(Source flag) const {
return (flags_ & flag) != Source::NONE;
}
void notifyPauseChanged() {
if (onPauseChangedCallback_) {
onPauseChangedCallback_(isPaused());
}
}
public:
// Constructor con callback opcional
explicit PauseManager(std::function<void(bool)> callback = nullptr)
: onPauseChangedCallback_(callback) {}
// Establece/quita una fuente de pausa específica
void setFlag(Source source, bool enable) {
bool wasPaused = isPaused();
if (enable) {
flags_ |= source;
} else {
flags_ &= ~source;
}
// Solo notifica si cambió el estado general de pausa
if (wasPaused != isPaused()) {
notifyPauseChanged();
}
}
// Métodos específicos para cada fuente
void setPlayerPause(bool enable) { setFlag(Source::PLAYER, enable); }
void setServiceMenuPause(bool enable) { setFlag(Source::SERVICE_MENU, enable); }
void setFocusLossPause(bool enable) { setFlag(Source::FOCUS_LOST, enable); }
// Toggle para el jugador (más común)
void togglePlayerPause() { setPlayerPause(!isPlayerPaused()); }
// Getters
bool isPaused() const { return flags_ != Source::NONE; }
bool isPlayerPaused() const { return hasFlag(Source::PLAYER); }
bool isServiceMenuPaused() const { return hasFlag(Source::SERVICE_MENU); }
bool isFocusLossPaused() const { return hasFlag(Source::FOCUS_LOST); }
// Obtiene las banderas actuales
Source getFlags() const { return flags_; }
// Limpia todas las pausas (útil para reset)
void clearAll() {
if (isPaused()) {
flags_ = Source::NONE;
notifyPauseChanged();
}
}
// Método para debug
std::string getStatusString() const {
if (flags_ == Source::NONE) return "Active";
std::string result = "Paused by: ";
bool first = true;
if (hasFlag(Source::PLAYER)) {
if (!first) result += ", ";
result += "Player";
first = false;
}
if (hasFlag(Source::SERVICE_MENU)) {
if (!first) result += ", ";
result += "ServiceMenu";
first = false;
}
if (hasFlag(Source::FOCUS_LOST)) {
if (!first) result += ", ";
result += "FocusLoss";
first = false;
}
return result;
}
// Permite cambiar el callback en runtime
void setCallback(std::function<void(bool)> callback) {
onPauseChangedCallback_ = callback;
}
};