major refactor

This commit is contained in:
Bernardo Magri
2025-04-24 09:53:09 +01:00
parent 0cdc668629
commit a4dccdc2a3
11 changed files with 2069 additions and 474 deletions
+88 -43
View File
@@ -1,53 +1,98 @@
#pragma once
#include "glibmm/dispatcher.h"
#include "minefield.hpp"
#include <memory>
#include <gtkmm.h>
#include <glibmm.h>
#include <gdkmm.h>
#include <sigc++/sigc++.h>
#include <iomanip>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <filesystem>
#define PROJECT_NAME "minesweeper"
class MainWindow : public Gtk::Window
class MainWindow : public Gtk::ApplicationWindow
{
Gtk::Box boxV{Gtk::Orientation::VERTICAL};
Gtk::Box boxH{Gtk::Orientation::HORIZONTAL};
std::vector<std::shared_ptr<Gtk::ToggleButton>> buttons;
Gtk::Grid grid;
Gtk::HeaderBar bar;
Gtk::Button newGameButton;
Gtk::Button optionButton;
Gtk::Label flagLabel;
Gtk::Label clockLabel;
MineField field{16, 16, 1};
int m_elapsedTime;
bool newGame;
std::shared_ptr<Gdk::Texture> m_textureBomb;
std::shared_ptr<Gdk::Texture> m_textureFlag;
std::shared_ptr<Gdk::Texture> m_textureFlagBomb;
void updateCell(int x, int y);
void openBombs();
void updateFlagsLabel(int flags);
void updateClockLabel();
void handleClockSig(size_t);
void gameWon();
void gameOver();
sigc::connection clockSignalConn;
Glib::Dispatcher m_clockDispatch;
// void OpenNearCells(int index);
// void Explode();xo
// bool AllCellsOpened();
public:
MainWindow();
// void OnNewButtonClick();
void OnCellClick(int x, int y);
void OnCellRightClick(int n_press, double n_x, double n_y, int index);
// void ShowGameWonAnimation();
// void ApplyStyles();
// bool UpdateClockLabel();
MainWindow();
virtual ~MainWindow() = default;
private:
// UI containers
Gtk::Box m_boxMain{Gtk::Orientation::VERTICAL};
Gtk::HeaderBar m_headerBar;
Gtk::Box m_statusBox{Gtk::Orientation::HORIZONTAL};
Gtk::Grid m_grid;
Gtk::Overlay m_overlay;
// Game status widgets
Gtk::Label m_minesLabel;
Gtk::Label m_flagsLabel;
Gtk::Label m_timeLabel;
// Header bar controls
Gtk::Button m_newGameButton;
Gtk::MenuButton m_difficultyButton;
// Game field and buttons
std::unique_ptr<MineField> m_field;
std::vector<std::shared_ptr<Gtk::ToggleButton>> m_buttons;
// Resources
std::shared_ptr<Gdk::Texture> m_textureBomb;
std::shared_ptr<Gdk::Texture> m_textureFlag;
std::shared_ptr<Gdk::Texture> m_textureFlagBomb;
std::shared_ptr<Gdk::Texture> m_textureExplosion;
// Game state tracking
bool m_firstClick;
sigc::connection m_timerConnection;
// Leaderboard
std::vector<GameScore> m_leaderboard;
Gtk::Dialog* m_leaderboardDialog;
Gtk::Dialog* m_difficultyDialog;
Gtk::Dialog* m_winDialog;
Gtk::Dialog* m_nameDialog;
std::string m_currentDifficulty;
// Methods
void setupUI();
void setupHeaderBar();
void setupStatusBar();
void setupGameBoard();
void setupLeaderboard();
void setupCSSProviders();
void loadResources();
// Event handlers
void onCellClick(int x, int y);
void onCellRightClick(int n_press, double n_x, double n_y, int index);
void onNewGameClick();
void onDifficultySelected(const GameDifficulty& difficulty);
void showDifficultyDialog();
// Game callbacks
void updateCell(int x, int y);
void updateFlagsLabel(int flags);
void updateTimeLabel();
bool updateTimer();
void onGameOver();
void onGameWon(int time);
void showGameOverAnimation();
void showGameWonAnimation();
void revealAllBombs();
// Leaderboard methods
void loadLeaderboard();
void saveLeaderboard();
void showLeaderboard();
void addScoreToLeaderboard(const std::string& playerName, const std::string& difficulty, int time);
std::string formatTime(int milliseconds) const;
// Helper methods
void resetGame();
void startNewGame(int cols, int rows, int mines, const std::string& difficulty);
void clearBoard();
void setupConfetti();
void showNameInputDialog(int time);
std::filesystem::path getConfigDir() const;
};