Add scores Window

This commit is contained in:
daleclack 2022-10-19 19:46:35 +08:00
parent 5a4c4effed
commit 7fb70bed6f
11 changed files with 208 additions and 27 deletions

View File

@ -26,7 +26,7 @@ find_package (Gettext REQUIRED)
set(PO_DIR ${CMAKE_BINARY_DIR}/po/zh_CN/LC_MESSAGES)
#Source files
set(SOURCE_FILE src/main.cc src/MineSweeper.cc src/InputBox.cc)
set(SOURCE_FILE src/main.cc src/MineSweeper.cc src/InputBox.cc src/ScoresWin.cc)
#Compile Resource
@ -45,7 +45,8 @@ set(RESOURCE_LIST
icons/scalable/status/maybe.svg
icons/scalable/status/mine.svg
STRIPBLANKS minesweeper.ui
STRIPBLANKS win_input.ui)
STRIPBLANKS win_input.ui
STRIPBLANKS scoreswin.ui)
compile_gresources(RESOURCE_FILE
XML_OUT

View File

@ -3,7 +3,8 @@
<cambalache-project version="0.11.2" target_tk="gtk+-3.0">
<ui>
(1,None,"minesweeper.ui","minesweeper.ui",None,None,None,None,None,None,None),
(2,None,None,"win_input.ui",None,None,None,None,None,None,None)
(2,None,None,"win_input.ui",None,None,None,None,None,None,None),
(3,None,None,"scoreswin.ui",None,None,None,None,None,None,None)
</ui>
<object>
(1,1,"GtkWindow","minesweeper",None,None,None,None,None,None),
@ -12,7 +13,13 @@
(1,4,"GtkGrid","start_page",3,None,None,None,None,None),
(1,5,"GtkBox","main_page",3,None,None,None,1,None),
(1,6,"GtkGrid","custom_page",3,None,None,None,2,None),
(2,1,"GtkDialog",None,None,None,None,None,None,None)
(2,1,"GtkDialog",None,None,None,None,None,None,None),
(3,1,"GtkWindow","scores_win",None,None,None,None,None,None),
(3,2,"GtkBox",None,1,None,None,None,-1,None),
(3,3,"GtkBox","btn_box",2,None,None,None,2,None),
(3,4,"GtkTreeView","scores_view",2,None,None,None,1,None),
(3,5,"GtkButton","btnclose",3,None,None,None,2,None),
(3,6,"GtkLabel",None,3,None,None,None,1,None)
</object>
<object_property>
(1,1,"GtkContainer","resize-mode","queue",None,None,None,None,None),
@ -30,6 +37,22 @@
(2,1,"GtkWidget","height-request","200",None,None,None,None,None),
(2,1,"GtkWidget","margin-start","3",None,None,None,None,None),
(2,1,"GtkWidget","width-request","400",None,None,None,None,None),
(2,1,"GtkWindow","gravity","center",None,None,None,None,None)
(2,1,"GtkWindow","gravity","center",None,None,None,None,None),
(3,2,"GtkOrientable","orientation","vertical",None,None,None,None,None),
(3,3,"GtkWidget","height-request","3",None,None,None,None,None),
(3,4,"GtkWidget","height-request","300",None,None,None,None,None),
(3,4,"GtkWidget","valign","start",None,None,None,None,None),
(3,4,"GtkWidget","width-request","400",None,None,None,None,None),
(3,5,"GtkButton","label","Close",None,None,None,None,None),
(3,6,"GtkLabel","label"," ",None,None,None,None,None),
(3,6,"GtkWidget","hexpand","True",None,None,None,None,None)
</object_property>
<object_layout_property>
(3,2,3,"GtkBoxLayoutChild","fill","False",None,None,None,None),
(3,2,3,"GtkBoxLayoutChild","position","2",None,None,None,None),
(3,2,4,"GtkBoxLayoutChild","position","1",None,None,None,None),
(3,3,5,"GtkBoxLayoutChild","position","2",None,None,None,None),
(3,3,6,"GtkBoxLayoutChild","fill","False",None,None,None,None),
(3,3,6,"GtkBoxLayoutChild","position","1",None,None,None,None)
</object_layout_property>
</cambalache-project>

View File

@ -0,0 +1,49 @@
<?xml version='1.0' encoding='UTF-8'?>
<!-- Created with Cambalache 0.11.2 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkWindow" id="scores_win">
<child>
<object class="GtkBox">
<property name="orientation">vertical</property>
<child>
<object class="GtkTreeView" id="scores_view">
<property name="height-request">300</property>
<property name="valign">start</property>
<property name="width-request">400</property>
</object>
<packing>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkBox" id="btn_box">
<property name="height-request">3</property>
<child>
<object class="GtkLabel">
<property name="hexpand">True</property>
<property name="label"> </property>
</object>
<packing>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btnclose">
<property name="label">Close</property>
</object>
<packing>
<property name="position">2</property>
</packing>
</child>
</object>
<packing>
<property name="fill">False</property>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

View File

@ -54,6 +54,15 @@
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">center</property>
<property name="valign">center</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkBox" id="main_box">
<property name="visible">True</property>
@ -90,6 +99,27 @@
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="check_scores">
<property name="label" translatable="yes">Show Scores Window</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">False</property>
<property name="draw-indicator">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>

View File

@ -7,15 +7,19 @@ InputBox::InputBox(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &re
{
// Get Widgets
ref_builder->get_widget("entry_name", entry_name);
ref_builder->get_widget("check_scores", check_scores);
entry_name->signal_activate().connect(sigc::mem_fun(*this, &InputBox::entry_activated));
}
void InputBox::on_response(int response_id){
if(response_id == Gtk::RESPONSE_OK){
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()){
if (outfile.is_open())
{
// Insert data to json
std::string name = std::string((entry_name->get_text()).c_str());
names.push_back(name);
@ -24,26 +28,35 @@ void InputBox::on_response(int response_id){
data["time"] = times;
// Output data
outfile<<data;
outfile << data;
}
outfile.close();
// If show scores checkbutton is checked, show scores window
if(check_scores->get_active()){
scores_win1->show_all();
}
}
hide();
}
void InputBox::set_game_time(int time){
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()){
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{
}
else
{
// Otherwist, create json data
data = json::parse(R"(
{
@ -58,19 +71,25 @@ void InputBox::set_game_time(int time){
game_time = time;
}
void InputBox::entry_activated(){
void InputBox::entry_activated()
{
// Default response
response(Gtk::RESPONSE_OK);
}
InputBox *InputBox::create(Gtk::Window &parent)
void InputBox::set_scores_window(ScoresWin *win1)
{
// Bind Scores Window
scores_win1 = win1;
}
InputBox *InputBox::create()
{
// 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

@ -2,15 +2,15 @@
#include <gtkmm.h>
#include <vector>
#include "../json_nlohmann/json.hpp"
using json = nlohmann::json;
#include "jsonfile.hh"
#include "ScoresWin.hh"
class InputBox : public Gtk::Dialog{
public:
static InputBox *create(Gtk::Window &parent);
static InputBox *create();
InputBox(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &ref_Glade);
void set_game_time(int time);
void set_scores_window(ScoresWin *win1);
protected:
void on_response(int response_id) override;
@ -28,6 +28,10 @@ class InputBox : public Gtk::Dialog{
// Child widget
Gtk::Entry *entry_name;
Gtk::CheckButton *check_scores;
// Scores Window
ScoresWin *scores_win1;
// Signal Handlers
void entry_activated();

View File

@ -34,13 +34,23 @@ MineSweeper::MineSweeper()
main_box.pack_start(btn_box, Gtk::PACK_SHRINK);
// Create a dialog
input_dialog = InputBox::create(*this);
input_dialog = InputBox::create();
// Create Scores Window
scores_win = ScoresWin::create();
// Show everything
add(main_box);
show_all_children();
}
MineSweeper::~MineSweeper(){
delete input_dialog;
if(cell != nullptr){
delete[] cell;
}
}
void MineSweeper::reset_game()
{
if(cell != nullptr){
@ -241,6 +251,9 @@ void MineSweeper::check_mines(int pos_x, int pos_y)
mytimer.disconnect();
// Save the time of game
input_dialog->set_transient_for(*this);
scores_win->set_transient_for(*this);
input_dialog->set_scores_window(scores_win);
input_dialog->set_game_time(timer_count);
input_dialog->show_all();
}

View File

@ -2,6 +2,7 @@
#include <gtkmm.h>
#include "InputBox.hh"
#include "ScoresWin.hh"
class MineCell : public Gtk::Button
{
@ -21,6 +22,7 @@ class MineSweeper : public Gtk::ApplicationWindow
{
public:
MineSweeper();
~MineSweeper();
private:
// Child widgets
@ -39,6 +41,9 @@ private:
// Input dialog
InputBox *input_dialog;
// Scores Window
ScoresWin *scores_win;
// Signal Handlers
void reset_game();
void calc_mines();

View File

@ -0,0 +1,19 @@
#include "ScoresWin.hh"
ScoresWin::ScoresWin(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &ref_Glade)
: Gtk::Window(cobject),
ref_builder(ref_Glade)
{
}
ScoresWin *ScoresWin::create()
{
// Create a window
auto builder = Gtk::Builder::create_from_resource("/org/gtk/daleclack/scoreswin.ui");
ScoresWin *main_win;
builder->get_widget_derived("scores_win", main_win);
return main_win;
}

View File

@ -0,0 +1,13 @@
#pragma once
#include <gtkmm.h>
#include "jsonfile.hh"
class ScoresWin : public Gtk::Window{
public:
static ScoresWin *create();
ScoresWin(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &ref_Glade);
private:
Glib::RefPtr<Gtk::Builder> ref_builder;
};

View File

@ -0,0 +1,5 @@
#pragma once
#include "../json_nlohmann/json.hpp"
using json = nlohmann::json;