[gnome-2048] Congratulate the user on obtaining the 2048 tile



commit d76af9b729debb3c77f21eb326f4aeaed618ea0a
Author: Juan R. GarcĂ­a Blanco <juanrgar gmail com>
Date:   Sat Feb 14 10:08:25 2015 +0100

    Congratulate the user on obtaining the 2048 tile
    
    The congrats message is automatially disabled after obtaining the target
    tile for the first time; it can be re-enabled in the preferences dialog.

 data/congrats.ui                |   98 +++++++++++++++++++++++++++++++++++++++
 data/org.gnome.2048.gschema.xml |    7 ++-
 data/preferences.ui             |   70 ++++++++++++++++++++++++++--
 src/application.vala            |   34 +++++++++++++-
 src/game.vala                   |    4 +-
 src/gnome-2048.gresource.xml    |    1 +
 6 files changed, 205 insertions(+), 9 deletions(-)
---
diff --git a/data/congrats.ui b/data/congrats.ui
new file mode 100644
index 0000000..7914d90
--- /dev/null
+++ b/data/congrats.ui
@@ -0,0 +1,98 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Generated with glade 3.18.3 -->
+<interface>
+  <requires lib="gtk+" version="3.12"/>
+  <object class="GtkDialog" id="congratsdialog">
+    <property name="can_focus">False</property>
+    <property name="border_width">12</property>
+    <property name="title" translatable="yes">Congratulations!</property>
+    <property name="resizable">False</property>
+    <property name="type_hint">dialog</property>
+    <child internal-child="vbox">
+      <object class="GtkBox" id="dialog-vbox1">
+        <property name="can_focus">False</property>
+        <property name="orientation">vertical</property>
+        <property name="spacing">12</property>
+        <child internal-child="action_area">
+          <object class="GtkButtonBox" id="dialog-action_area1">
+            <property name="can_focus">False</property>
+            <property name="layout_style">end</property>
+            <child>
+              <object class="GtkButton" id="button1">
+                <property name="label" translatable="yes">New Game</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">True</property>
+                <style>
+                  <class name="suggested-action"/>
+                </style>
+              </object>
+              <packing>
+                <property name="expand">True</property>
+                <property name="fill">True</property>
+                <property name="position">0</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkButton" id="button2">
+                <property name="label" translatable="yes">Keep Playing</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">True</property>
+              </object>
+              <packing>
+                <property name="expand">True</property>
+                <property name="fill">True</property>
+                <property name="position">1</property>
+              </packing>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">False</property>
+            <property name="position">0</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkGrid" id="grid1">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="column_homogeneous">True</property>
+            <child>
+              <object class="GtkLabel" id="messagelabel">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+              </object>
+              <packing>
+                <property name="left_attach">0</property>
+                <property name="top_attach">1</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkImage" id="image1">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="pixel_size">145</property>
+                <property name="icon_name">face-wink</property>
+                <property name="icon_size">6</property>
+              </object>
+              <packing>
+                <property name="left_attach">0</property>
+                <property name="top_attach">0</property>
+              </packing>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">True</property>
+            <property name="fill">True</property>
+            <property name="position">1</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+    <action-widgets>
+      <action-widget response="0">button1</action-widget>
+      <action-widget response="1">button2</action-widget>
+    </action-widgets>
+  </object>
+</interface>
diff --git a/data/org.gnome.2048.gschema.xml b/data/org.gnome.2048.gschema.xml
index 4677fff..aceb636 100644
--- a/data/org.gnome.2048.gschema.xml
+++ b/data/org.gnome.2048.gschema.xml
@@ -26,9 +26,14 @@
       <description>Game grid number of columns.</description>
     </key>
     <key name="target-value" type="i">
-      <default>32</default>
+      <default>2048</default>
       <summary>Target value</summary>
       <description>Tile value at which user is congratulated.</description>
     </key>
+    <key name="do-congrat" type="b">
+      <default>true</default>
+      <summary>Congrat on target tile</summary>
+      <description>Whether the user shall be contratulated on obtaining target tile.</description>
+    </key>
   </schema>
 </schemalist>
diff --git a/data/preferences.ui b/data/preferences.ui
index 0fe7751..c951206 100644
--- a/data/preferences.ui
+++ b/data/preferences.ui
@@ -22,7 +22,7 @@
     <property name="resizable">False</property>
     <property name="window_position">center-on-parent</property>
     <property name="type_hint">dialog</property>
-    <property name="use_header_bar">1</property>
+    <property name="use-header-bar">1</property>
     <child internal-child="vbox">
       <object class="GtkBox" id="dialog-vbox1">
         <property name="can_focus">False</property>
@@ -57,9 +57,9 @@
                 <property name="can_focus">False</property>
                 <property name="border_width">12</property>
                 <property name="orientation">vertical</property>
-                <property name="spacing">6</property>
+                <property name="spacing">12</property>
                 <child>
-                  <object class="GtkFrame" id="frame3">
+                  <object class="GtkFrame" id="frame1">
                     <property name="visible">True</property>
                     <property name="can_focus">False</property>
                     <property name="label_xalign">0</property>
@@ -75,7 +75,7 @@
                             <property name="can_focus">False</property>
                             <property name="border_width">6</property>
                             <property name="row_spacing">6</property>
-                            <property name="column_spacing">6</property>
+                            <property name="column_spacing">12</property>
                             <child>
                               <object class="GtkLabel" id="label6">
                                 <property name="visible">True</property>
@@ -154,6 +154,68 @@
                     <property name="position">0</property>
                   </packing>
                 </child>
+                <child>
+                  <object class="GtkFrame" id="frame2">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="label_xalign">0</property>
+                    <property name="shadow_type">none</property>
+                    <child>
+                      <object class="GtkAlignment" id="alignment1">
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <property name="left_padding">12</property>
+                        <child>
+                          <object class="GtkGrid" id="grid2">
+                            <property name="visible">True</property>
+                            <property name="can_focus">False</property>
+                            <property name="border_width">6</property>
+                            <property name="row_spacing">6</property>
+                            <property name="column_spacing">12</property>
+                            <child>
+                              <object class="GtkLabel" id="label2">
+                                <property name="visible">True</property>
+                                <property name="can_focus">False</property>
+                                <property name="xalign">1</property>
+                                <property name="label" translatable="yes">Display congrats</property>
+                              </object>
+                              <packing>
+                                <property name="left_attach">0</property>
+                                <property name="top_attach">0</property>
+                              </packing>
+                            </child>
+                            <child>
+                              <object class="GtkSwitch" id="congratswitch">
+                                <property name="visible">True</property>
+                                <property name="can_focus">True</property>
+                                <property name="active">True</property>
+                              </object>
+                              <packing>
+                                <property name="left_attach">1</property>
+                                <property name="top_attach">0</property>
+                              </packing>
+                            </child>
+                          </object>
+                        </child>
+                      </object>
+                    </child>
+                    <child type="label">
+                      <object class="GtkLabel" id="label3">
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <property name="label" translatable="yes">Interface</property>
+                        <attributes>
+                          <attribute name="weight" value="bold"/>
+                        </attributes>
+                      </object>
+                    </child>
+                  </object>
+                  <packing>
+                    <property name="expand">False</property>
+                    <property name="fill">True</property>
+                    <property name="position">1</property>
+                  </packing>
+                </child>
               </object>
               <packing>
                 <property name="left_attach">0</property>
diff --git a/src/application.vala b/src/application.vala
index ac831b3..0e51d85 100644
--- a/src/application.vala
+++ b/src/application.vala
@@ -25,6 +25,8 @@ public class Application : Gtk.Application
   private Gtk.Button _new_game_button;
   private Gtk.AboutDialog _about_dialog;
   private Gtk.Dialog _preferences_dialog;
+  private Gtk.Dialog _congrats_dialog;
+  private Gtk.Label _congrats_message;
   private Gtk.Label _score;
 
   private int _window_width;
@@ -69,6 +71,7 @@ public class Application : Gtk.Application
     _create_window (builder);
     _create_about_dialog ();
     _create_preferences_dialog (builder);
+    _create_congrats_dialog (builder);
 
     if (!_game.restore_game ())
       _game.new_game ();
@@ -122,7 +125,12 @@ public class Application : Gtk.Application
       debug ("finished");
     });
     _game.target_value_reached.connect ((s, v) => {
-      _header_bar.subtitle = _("Congratulations!");
+      if (_settings.get_boolean ("do-congrat")) {
+        string message = @"You have obtained the $v tile";
+        _congrats_message.set_text (message);
+        _congrats_dialog.present ();
+        _settings.set_boolean ("do-congrat", false);
+      }
       debug ("target value reached");
     });
   }
@@ -230,6 +238,30 @@ public class Application : Gtk.Application
 
     _settings.bind ("rows", builder.get_object ("rowsspin"), "value", GLib.SettingsBindFlags.DEFAULT);
     _settings.bind ("cols", builder.get_object ("colsspin"), "value", GLib.SettingsBindFlags.DEFAULT);
+    _settings.bind ("do-congrat", builder.get_object ("congratswitch"), "active", 
GLib.SettingsBindFlags.DEFAULT);
+  }
+
+  private void _create_congrats_dialog (Gtk.Builder builder)
+  {
+    try {
+      builder.add_from_resource ("/org/gnome/gnome-2048/data/congrats.ui");
+    } catch (GLib.Error e) {
+      stderr.printf ("%s\n", e.message);
+    }
+
+    _congrats_dialog = builder.get_object ("congratsdialog") as Gtk.Dialog;
+    _congrats_dialog.set_transient_for (_window);
+
+    _congrats_dialog.response.connect ((response_id) => {
+      if (response_id == 0)
+        _game.new_game ();
+      _congrats_dialog.hide ();
+    });
+    _congrats_dialog.delete_event.connect ((response_id) => {
+      return _congrats_dialog.hide_on_delete ();
+    });
+
+    _congrats_message = builder.get_object ("messagelabel") as Gtk.Label;
   }
 
   private void new_game_cb ()
diff --git a/src/game.vala b/src/game.vala
index d80eb58..6655fe7 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -67,8 +67,7 @@ public class Game : GLib.Object
     int cols = _settings.get_int ("cols");
     _grid = new Grid (rows, cols);
 
-    uint target_value = _settings.get_int ("target-value");
-    _grid.target_value = target_value;
+    _settings.bind ("target-value", _grid, "target_value", GLib.SettingsBindFlags.DEFAULT);
 
     _to_move = new Gee.LinkedList<TileMovement?> ();
     _to_hide = new Gee.LinkedList<TileMovement?> ();
@@ -665,7 +664,6 @@ public class Game : GLib.Object
     if (_grid.target_value_reached) {
       target_value_reached (_grid.target_value);
       _grid.target_value_reached = false;
-      _grid.target_value = 0;
     }
 
     if (_grid.is_finished ())
diff --git a/src/gnome-2048.gresource.xml b/src/gnome-2048.gresource.xml
index b8cfd23..bb559af 100644
--- a/src/gnome-2048.gresource.xml
+++ b/src/gnome-2048.gresource.xml
@@ -4,6 +4,7 @@
     <file preprocess="xml-stripblanks">data/menus.ui</file>
     <file preprocess="xml-stripblanks">data/mainwindow.ui</file>
     <file preprocess="xml-stripblanks">data/preferences.ui</file>
+    <file preprocess="xml-stripblanks">data/congrats.ui</file>
     <file>data/style.css</file>
   </gresource>
 </gresources>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]