From 1e3406285e7869e216b7d4b685dad9db050bbf8e Mon Sep 17 00:00:00 2001 From: daleclack Date: Mon, 17 Oct 2022 23:05:19 +0800 Subject: [PATCH] Add scores dialog for gtk141 --- Gtkmm3/gtk141_minesweeper2/CMakeLists.txt | 4 +- Gtkmm3/gtk141_minesweeper2/res/win_input.ui | 57 ++++++++++--------- Gtkmm3/gtk141_minesweeper2/src/InputBox.cc | 57 ++++++++++++++++++- Gtkmm3/gtk141_minesweeper2/src/InputBox.hh | 21 ++++++- Gtkmm3/gtk141_minesweeper2/src/MineSweeper.cc | 6 +- Gtkmm3/gtk141_minesweeper2/src/MineSweeper.hh | 4 ++ 6 files changed, 118 insertions(+), 31 deletions(-) diff --git a/Gtkmm3/gtk141_minesweeper2/CMakeLists.txt b/Gtkmm3/gtk141_minesweeper2/CMakeLists.txt index 56080a1..edf6d51 100644 --- a/Gtkmm3/gtk141_minesweeper2/CMakeLists.txt +++ b/Gtkmm3/gtk141_minesweeper2/CMakeLists.txt @@ -44,8 +44,8 @@ set(RESOURCE_LIST icons/scalable/status/incorrect.svg icons/scalable/status/maybe.svg icons/scalable/status/mine.svg - minesweeper.ui - win_input.ui) + STRIPBLANKS minesweeper.ui + STRIPBLANKS win_input.ui) compile_gresources(RESOURCE_FILE XML_OUT diff --git a/Gtkmm3/gtk141_minesweeper2/res/win_input.ui b/Gtkmm3/gtk141_minesweeper2/res/win_input.ui index 4af5419..9560854 100644 --- a/Gtkmm3/gtk141_minesweeper2/res/win_input.ui +++ b/Gtkmm3/gtk141_minesweeper2/res/win_input.ui @@ -3,12 +3,18 @@ - + + 300 + 150 False dialog False + 10 + 10 + 10 + 10 vertical 2 @@ -49,8 +55,7 @@ - - + True False center @@ -58,31 +63,31 @@ True True - + + True + False + center + center + Input Name: + + + False + True + 0 + - - - - - - - - - - - - - - - - - - - - - - + + True + True + center + center + + + False + True + 1 + diff --git a/Gtkmm3/gtk141_minesweeper2/src/InputBox.cc b/Gtkmm3/gtk141_minesweeper2/src/InputBox.cc index 974fc83..12dfe41 100644 --- a/Gtkmm3/gtk141_minesweeper2/src/InputBox.cc +++ b/Gtkmm3/gtk141_minesweeper2/src/InputBox.cc @@ -1,15 +1,70 @@ #include "InputBox.hh" +#include InputBox::InputBox(BaseObjectType *cobject, const Glib::RefPtr &ref_Glade) : Gtk::Dialog(cobject), ref_builder(ref_Glade) { + // Get Widgets + ref_builder->get_widget("entry_name", entry_name); } -InputBox *InputBox::create() +void InputBox::on_response(int response_id){ + if(response_id == Gtk::RESPONSE_OK){ + // 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< names1 = data["name"]; + std::vector times1 = data["time"]; + names = names1; + times = times1; + }else{ + // Otherwist, create json data + data = json::parse(R"( + { + "name":[" "], + "time":[0] + } + )"); + } + jsonfile.close(); + + // Initalize time + game_time = time; +} + +InputBox *InputBox::create(Gtk::Window &parent) { + // Create a inputbox object auto builder = Gtk::Builder::create_from_resource("/org/gtk/daleclack/win_input.ui"); InputBox *dialog; builder->get_widget_derived("dialog", dialog); + dialog->set_transient_for(parent); + + return dialog; } diff --git a/Gtkmm3/gtk141_minesweeper2/src/InputBox.hh b/Gtkmm3/gtk141_minesweeper2/src/InputBox.hh index eb9b751..dc4478b 100644 --- a/Gtkmm3/gtk141_minesweeper2/src/InputBox.hh +++ b/Gtkmm3/gtk141_minesweeper2/src/InputBox.hh @@ -1,12 +1,31 @@ #pragma once #include +#include +#include "../json_nlohmann/json.hpp" + +using json = nlohmann::json; class InputBox : public Gtk::Dialog{ public: - static InputBox *create(); + static InputBox *create(Gtk::Window &parent); InputBox(BaseObjectType *cobject, const Glib::RefPtr &ref_Glade); + void set_game_time(int time); + + protected: + void on_response(int response_id) override; private: + int game_time; + + // Data to write to json file + json data; + std::vector names; + std::vector times; + + // Builder Object Glib::RefPtr ref_builder; + + // Child widget + Gtk::Entry *entry_name; }; \ No newline at end of file diff --git a/Gtkmm3/gtk141_minesweeper2/src/MineSweeper.cc b/Gtkmm3/gtk141_minesweeper2/src/MineSweeper.cc index c101d35..081ae29 100644 --- a/Gtkmm3/gtk141_minesweeper2/src/MineSweeper.cc +++ b/Gtkmm3/gtk141_minesweeper2/src/MineSweeper.cc @@ -33,6 +33,9 @@ MineSweeper::MineSweeper() main_box.pack_start(mine_grid); main_box.pack_start(btn_box, Gtk::PACK_SHRINK); + // Create a dialog + input_dialog = InputBox::create(*this); + // Show everything add(main_box); show_all_children(); @@ -238,6 +241,7 @@ void MineSweeper::check_mines(int pos_x, int pos_y) mytimer.disconnect(); // Save the time of game - + input_dialog->set_game_time(timer_count); + input_dialog->show_all(); } } diff --git a/Gtkmm3/gtk141_minesweeper2/src/MineSweeper.hh b/Gtkmm3/gtk141_minesweeper2/src/MineSweeper.hh index 3e972cd..9c8d4bc 100644 --- a/Gtkmm3/gtk141_minesweeper2/src/MineSweeper.hh +++ b/Gtkmm3/gtk141_minesweeper2/src/MineSweeper.hh @@ -1,6 +1,7 @@ #pragma once #include +#include "InputBox.hh" class MineCell : public Gtk::Button { @@ -35,6 +36,9 @@ private: int timer_count; sigc::connection mytimer; + // Input dialog + InputBox *input_dialog; + // Signal Handlers void reset_game(); void calc_mines();