Add scores dialog for gtk141

This commit is contained in:
daleclack 2022-10-17 23:05:19 +08:00
parent 1e5e6e5c24
commit 1e3406285e
6 changed files with 118 additions and 31 deletions

View File

@ -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

View File

@ -3,12 +3,18 @@
<!-- Created with Cambalache 0.11.2 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkDialog">
<object class="GtkDialog" id="dialog">
<property name="width-request">300</property>
<property name="height-request">150</property>
<property name="can-focus">False</property>
<property name="type-hint">dialog</property>
<child internal-child="vbox">
<object class="GtkBox">
<property name="can-focus">False</property>
<property name="margin-start">10</property>
<property name="margin-end">10</property>
<property name="margin-top">10</property>
<property name="margin-bottom">10</property>
<property name="orientation">vertical</property>
<property name="spacing">2</property>
<child internal-child="action_area">
@ -49,8 +55,7 @@
</packing>
</child>
<child>
<!-- n-columns=3 n-rows=3 -->
<object class="GtkGrid">
<object class="GtkBox" id="main_box">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">center</property>
@ -58,31 +63,31 @@
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<child>
<placeholder/>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">center</property>
<property name="valign">center</property>
<property name="label" translatable="yes">Input Name:</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
<object class="GtkEntry" id="entry_name">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="halign">center</property>
<property name="valign">center</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>

View File

@ -1,15 +1,70 @@
#include "InputBox.hh"
#include <fstream>
InputBox::InputBox(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &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<<data;
}
outfile.close();
}
hide();
}
void InputBox::set_game_time(int time){
// Try to open json file
std::fstream jsonfile;
jsonfile.open("scores.json", std::ios_base::in);
// If json file opened, read the data
if(jsonfile.is_open()){
data = json::parse(jsonfile);
std::vector<std::string> names1 = data["name"];
std::vector<int> 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;
}

View File

@ -1,12 +1,31 @@
#pragma once
#include <gtkmm.h>
#include <vector>
#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<Gtk::Builder> &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<std::string> names;
std::vector<int> times;
// Builder Object
Glib::RefPtr<Gtk::Builder> ref_builder;
// Child widget
Gtk::Entry *entry_name;
};

View File

@ -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();
}
}

View File

@ -1,6 +1,7 @@
#pragma once
#include <gtkmm.h>
#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();