Update gtk141

This commit is contained in:
daleclack 2022-10-24 21:53:55 +08:00
parent 496dd402d5
commit b728db1fc6
4 changed files with 57 additions and 35 deletions

View File

@ -12,14 +12,6 @@ InputBox::InputBox(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &re
void InputBox::on_response(int response_id) void InputBox::on_response(int response_id)
{ {
if (response_id == Gtk::RESPONSE_OK)
{
read_scores(check_scores->get_active());
}
hide();
}
void InputBox::read_scores(bool show_scores_win){
// Open a file to save json data // Open a file to save json data
std::fstream outfile; std::fstream outfile;
outfile.open("scores.json", std::ios_base::out); outfile.open("scores.json", std::ios_base::out);
@ -36,10 +28,19 @@ void InputBox::read_scores(bool show_scores_win){
outfile << data; outfile << data;
} }
outfile.close(); outfile.close();
if (response_id == Gtk::RESPONSE_OK)
{
read_scores(check_scores->get_active());
}
hide();
}
void InputBox::read_scores(bool show_scores_win)
{
// If show scores checkbutton is checked, show scores window // If show scores checkbutton is checked, show scores window
if(show_scores_win){ if (show_scores_win)
scores_win1->show_with_vectors(names, times); {
scores_win1->update_and_show();
} }
} }

View File

@ -213,6 +213,8 @@ void MineSweeper::cell_clicked(MineCell *cell1)
winned = false; winned = false;
cell1->cleared = true; cell1->cleared = true;
cell1->set_image_from_icon_name("exploded", Gtk::ICON_SIZE_LARGE_TOOLBAR); cell1->set_image_from_icon_name("exploded", Gtk::ICON_SIZE_LARGE_TOOLBAR);
// End the game
game_lost(cell1->y * 7 + cell1->x); game_lost(cell1->y * 7 + cell1->x);
status_label.set_label("You lost!"); status_label.set_label("You lost!");
game_ended = true; game_ended = true;

View File

@ -18,29 +18,48 @@ ScoresWin::ScoresWin(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &
tree_view->append_column("time", column1.win_time); tree_view->append_column("time", column1.win_time);
} }
void ScoresWin::show_with_vectors(std::vector<std::string> &name_vec, std::vector<int> &time_vec){ void ScoresWin::update_and_show()
{
std::fstream infile;
infile.open("scores.json", std::ios_base::in);
if (infile.is_open())
{
// Read data from json file
json data = json::parse(infile);
std::vector<std::string> name_vec = data["name"];
std::vector<int> time_vec = data["time"];
// Clear the store // Clear the store
store->clear(); store->clear();
// Append data to the store // Append data to the store
for(int i = 0; i < name_vec.size(); i++){ for (int i = 0; i < name_vec.size(); i++)
{
auto row = *(store->append()); auto row = *(store->append());
row[column1.player_name] = name_vec[i]; row[column1.player_name] = name_vec[i];
row[column1.win_time] = time_vec[i]; row[column1.win_time] = time_vec[i];
} }
}
show_all(); show_all();
} }
int ScoresWin::sort_func(const Gtk::TreeModel::iterator &iter1, const Gtk::TreeModel::iterator &iter2){ int ScoresWin::sort_func(const Gtk::TreeModel::iterator &iter1, const Gtk::TreeModel::iterator &iter2)
{
// Sort by the game time
auto row1 = *iter1; auto row1 = *iter1;
auto row2 = *iter2; auto row2 = *iter2;
if(row1[column1.win_time] < row2[column1.win_time]){ if (row1[column1.win_time] < row2[column1.win_time])
{
return -1; return -1;
} }
if(row1[column1.win_time] == row2[column1.win_time]){ if (row1[column1.win_time] == row2[column1.win_time])
{
return 0; return 0;
}else{ }
else
{
return 1; return 1;
} }
} }

View File

@ -7,7 +7,7 @@ class ScoresWin : public Gtk::Window{
public: public:
static ScoresWin *create(); static ScoresWin *create();
ScoresWin(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &ref_Glade); ScoresWin(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &ref_Glade);
void show_with_vectors(std::vector<std::string> &name_vec, std::vector<int> &time_vec); void update_and_show();
private: private:
Glib::RefPtr<Gtk::Builder> ref_builder; Glib::RefPtr<Gtk::Builder> ref_builder;