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,6 +12,22 @@ InputBox::InputBox(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &re
void InputBox::on_response(int response_id)
{
// Open a file to save json data
std::fstream outfile;
outfile.open("scores.json", std::ios_base::out);
if (outfile.is_open())
{
// Insert data to json
std::string name = std::string((entry_name->get_text()).c_str());
names.push_back(name);
times.push_back(game_time);
data["name"] = names;
data["time"] = times;
// Output data
outfile << data;
}
outfile.close();
if (response_id == Gtk::RESPONSE_OK)
{
read_scores(check_scores->get_active());
@ -19,28 +35,13 @@ void InputBox::on_response(int response_id)
hide();
}
void InputBox::read_scores(bool show_scores_win){
// Open a file to save json data
std::fstream outfile;
outfile.open("scores.json", std::ios_base::out);
if (outfile.is_open())
{
// Insert data to json
std::string name = std::string((entry_name->get_text()).c_str());
names.push_back(name);
times.push_back(game_time);
data["name"] = names;
data["time"] = times;
// Output data
outfile << data;
}
outfile.close();
// If show scores checkbutton is checked, show scores window
if(show_scores_win){
scores_win1->show_with_vectors(names, times);
}
void InputBox::read_scores(bool show_scores_win)
{
// If show scores checkbutton is checked, show scores window
if (show_scores_win)
{
scores_win1->update_and_show();
}
}
void InputBox::set_game_time(int time)

View File

@ -213,6 +213,8 @@ void MineSweeper::cell_clicked(MineCell *cell1)
winned = false;
cell1->cleared = true;
cell1->set_image_from_icon_name("exploded", Gtk::ICON_SIZE_LARGE_TOOLBAR);
// End the game
game_lost(cell1->y * 7 + cell1->x);
status_label.set_label("You lost!");
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);
}
void ScoresWin::show_with_vectors(std::vector<std::string> &name_vec, std::vector<int> &time_vec){
// Clear the store
store->clear();
void ScoresWin::update_and_show()
{
std::fstream infile;
infile.open("scores.json", std::ios_base::in);
// Append data to the store
for(int i = 0; i < name_vec.size(); i++){
auto row = *(store->append());
row[column1.player_name] = name_vec[i];
row[column1.win_time] = time_vec[i];
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
store->clear();
// Append data to the store
for (int i = 0; i < name_vec.size(); i++)
{
auto row = *(store->append());
row[column1.player_name] = name_vec[i];
row[column1.win_time] = time_vec[i];
}
}
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 row2 = *iter2;
if(row1[column1.win_time] < row2[column1.win_time]){
if (row1[column1.win_time] < row2[column1.win_time])
{
return -1;
}
if(row1[column1.win_time] == row2[column1.win_time]){
if (row1[column1.win_time] == row2[column1.win_time])
{
return 0;
}else{
}
else
{
return 1;
}
}

View File

@ -7,7 +7,7 @@ class ScoresWin : public Gtk::Window{
public:
static ScoresWin *create();
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:
Glib::RefPtr<Gtk::Builder> ref_builder;