Fixing the clock and adding new signals
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
#include "window.hpp"
|
||||
#include "gdkmm/texture.h"
|
||||
#include "sigc++/functors/mem_fun.h"
|
||||
|
||||
|
||||
//}
|
||||
@@ -19,7 +21,7 @@ void MainWindow::OnCellRightClick(int n_press, double n_x, double n_y, int index
|
||||
field.toggleFlag(x, y);
|
||||
if(field.isFlagged(x, y)) {
|
||||
auto imgflag = Gtk::make_managed<Gtk::Image>();
|
||||
imgflag->set(m_pixbufFlag);
|
||||
imgflag->set(m_textureFlag);
|
||||
buttons.at(pos)->set_child(*imgflag);
|
||||
buttons.at(pos)->set_active(true);
|
||||
}
|
||||
@@ -35,7 +37,6 @@ void MainWindow::updateFlagsLabel(int flags) {
|
||||
Glib::ustring msg = Glib::ustring::compose("Remaining flags: %1", flags);
|
||||
flagLabel.set_label(msg);
|
||||
}
|
||||
|
||||
// void MainWindow::OnNewButtonClick() {
|
||||
// newGame = true;
|
||||
// gameOver = false;
|
||||
@@ -67,14 +68,13 @@ void MainWindow::OnCellClick(int x, int y) {
|
||||
if(field.isFlagged(x, y)) {
|
||||
buttons.at(x + y * field.getRows())->set_active(true);
|
||||
}
|
||||
else if(field.isBomb(x, y)) {
|
||||
openBombs();
|
||||
}
|
||||
else {
|
||||
else {
|
||||
field.openCell(x, y);
|
||||
if(field.isBomb(x, y)) {
|
||||
openBombs();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void MainWindow::openBombs() {
|
||||
for(int i=0; i < field.getCols() * field.getRows(); i++) {
|
||||
@@ -86,12 +86,12 @@ void MainWindow::openBombs() {
|
||||
if(field.isBomb(x, y)) {
|
||||
if(field.isFlagged(x, y)) {
|
||||
auto imgFlagBomb = std::make_shared<Gtk::Image>();
|
||||
imgFlagBomb->set(m_pixbufFlagBomb);
|
||||
imgFlagBomb->set(m_textureFlagBomb);
|
||||
buttons.at(i)->set_child(*imgFlagBomb);
|
||||
}
|
||||
else {
|
||||
auto imgBomb = std::make_shared<Gtk::Image>();
|
||||
imgBomb->set(m_pixbufBomb);
|
||||
imgBomb->set(m_textureBomb);
|
||||
buttons.at(i)->set_child(*imgBomb);
|
||||
}
|
||||
buttons.at(i)->set_active(true);
|
||||
@@ -158,31 +158,31 @@ void MainWindow::updateCell(int x, int y) {
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
void MainWindow::gameOver() {
|
||||
clockSignalConn.disconnect();
|
||||
std::cout << "Signal gameOver emmited\n";
|
||||
}
|
||||
|
||||
bool MainWindow::updateClockLabel()
|
||||
{
|
||||
++m_elapsedTime;
|
||||
|
||||
// bool MainWindow::UpdateClockLabel()
|
||||
// {
|
||||
// if(gameOver) return false;
|
||||
|
||||
// elapsedTime++;
|
||||
int deciseconds = m_elapsedTime % 10;
|
||||
int seconds = (m_elapsedTime / 10) % 60;
|
||||
int minutes = (m_elapsedTime /600) % 60;
|
||||
|
||||
// int deciseconds = elapsedTime % 10;
|
||||
// int seconds = (elapsedTime / 10) % 60;
|
||||
// int minutes = (elapsedTime /600) % 60;
|
||||
|
||||
// Glib::ustring msg = Glib::ustring::compose("Elapsed time: %1:%2.%3", \
|
||||
// Glib::ustring::format(std::setfill(L'0'), std::setw(2), minutes), \
|
||||
// Glib::ustring::format(std::setfill(L'0'), std::setw(2), seconds), \
|
||||
// Glib::ustring::format(std::setfill(L'0'), std::setw(1), deciseconds));
|
||||
// clockLabel.set_label(msg);
|
||||
|
||||
// return true;
|
||||
// }
|
||||
Glib::ustring msg = Glib::ustring::compose("Elapsed time: %1:%2.%3", \
|
||||
Glib::ustring::format(std::setfill(L'0'), std::setw(2), minutes), \
|
||||
Glib::ustring::format(std::setfill(L'0'), std::setw(2), seconds), \
|
||||
Glib::ustring::format(std::setfill(L'0'), std::setw(1), deciseconds));
|
||||
clockLabel.set_label(msg);
|
||||
return true;
|
||||
}
|
||||
|
||||
MainWindow::MainWindow()
|
||||
{
|
||||
// ApplyStyles(); // Load the CSS file
|
||||
elapsedTime = 0;
|
||||
m_elapsedTime = 0;
|
||||
newGame = true;
|
||||
set_title("MineSweeper");
|
||||
set_default_size(400, 400);
|
||||
@@ -222,9 +222,9 @@ MainWindow::MainWindow()
|
||||
|
||||
|
||||
//TODO check if it's okay to mix std::shared_ptr with Gdk::ptr
|
||||
m_pixbufBomb = Gdk::Pixbuf::create_from_resource("/minesweeper/bomb-solid");
|
||||
m_pixbufFlag = Gdk::Pixbuf::create_from_resource("/minesweeper/flag-solid");
|
||||
m_pixbufFlagBomb = Gdk::Pixbuf::create_from_resource("/minesweeper/flag-bomb");
|
||||
m_textureBomb = Gdk::Texture::create_from_resource("/minesweeper/bomb-solid");
|
||||
m_textureFlag = Gdk::Texture::create_from_resource("/minesweeper/flag-solid");
|
||||
m_textureFlagBomb = Gdk::Texture::create_from_resource("/minesweeper/flag-bomb");
|
||||
|
||||
// bombPix.set_from_resource("/minesweeper/bomb-solid");
|
||||
|
||||
@@ -266,12 +266,17 @@ MainWindow::MainWindow()
|
||||
|
||||
field.openCellSignal.connect(sigc::bind(sigc::mem_fun(*this, &MainWindow::updateCell)));
|
||||
field.remainingFlagsSignal.connect(sigc::bind(sigc::mem_fun(*this, &MainWindow::updateFlagsLabel)));
|
||||
field.gameOverSignal.connect(sigc::bind(sigc::mem_fun(*this, &MainWindow::gameOver)));
|
||||
//newGameButton.set_label("New");
|
||||
//newGameButton.add_css_class("suggested-action");
|
||||
//newGameButton.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::OnNewButtonClick));
|
||||
|
||||
//optionButton.set_icon_name("open-menu");
|
||||
|
||||
//if (clockSignalConn.connected()) clockSignalConn.disconnect();
|
||||
//elapsedTime = 0;
|
||||
clockSignalConn = Glib::signal_timeout().connect(sigc::mem_fun(*this, &MainWindow::updateClockLabel), 100);
|
||||
//}
|
||||
//create the minefield
|
||||
//field = new MineField(COLS, MINES);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user