[gnome-taquin] Save best score.



commit b119fbf6e9adf0dd5c106a386b08bbc05dd9afea
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Tue Jan 22 06:20:06 2019 +0100

    Save best score.
    
    For some, it is fun to challenge the best
    score done for a given taquin. So, add an
    entry to the moves menu at the end of the
    game displaying the (updated) best score.

 data/game-headerbar.ui  | 15 --------------
 src/game-headerbar.vala | 53 ++++++++++++++++++++++++++++++++++++++++++++++++-
 src/game-window.vala    |  1 +
 3 files changed, 53 insertions(+), 16 deletions(-)
---
diff --git a/data/game-headerbar.ui b/data/game-headerbar.ui
index c78e126..d4cafac 100644
--- a/data/game-headerbar.ui
+++ b/data/game-headerbar.ui
@@ -17,20 +17,6 @@
 -->
 <interface>
   <requires lib="gtk+" version="3.12"/>
-  <menu id="history-menu">
-    <section>
-      <item>
-        <!-- Translators: during a game, entry in the menu of the history menubutton (with a mnemonic that 
appears pressing Alt) -->
-        <attribute name="label" translatable="yes">_Undo</attribute>
-        <attribute name="action">ui.undo</attribute>
-      </item>
-      <item>
-        <!-- Translators: during a game, entry in the menu of the history menubutton (with a mnemonic that 
appears pressing Alt) -->
-        <attribute name="label" translatable="yes">_Restart</attribute>
-        <attribute name="action">ui.restart</attribute>
-      </item>
-    </section>
-  </menu>
   <template class="GameHeaderBar" parent="BaseHeaderBar">
     <property name="show-close-button">True</property>
     <child>
@@ -38,7 +24,6 @@
         <property name="visible">True</property>
         <property name="valign">center</property>
         <property name="focus-on-click">False</property>
-        <property name="menu-model">history-menu</property>
         <property name="label">0</property>
         <property name="sensitive">False</property>
         <child internal-child="accessible">
diff --git a/src/game-headerbar.vala b/src/game-headerbar.vala
index da9c110..13e1e0d 100644
--- a/src/game-headerbar.vala
+++ b/src/game-headerbar.vala
@@ -34,6 +34,8 @@ private class GameHeaderBar : BaseHeaderBar
     {
         ((Label) new_game_button.get_child ()).set_ellipsize (Pango.EllipsizeMode.END); // can happen on 
small screen with big moves count
 
+        generate_moves_menu ();
+
         init_modes ();
 
         if (window_name != "")
@@ -186,10 +188,12 @@ private class GameHeaderBar : BaseHeaderBar
         return new_game_button.is_focus;
     }
 
+    private uint last_moves_count = 0;
     internal void set_moves_count (ref uint moves_count)
     {
         history_button.set_label (moves_count.to_string ());
-        history_button.set_sensitive (moves_count != 0);
+        history_button.set_sensitive ((moves_count != 0) || (best_score != 0));
+        last_moves_count = moves_count;
     }
 
     /*\
@@ -239,4 +243,51 @@ private class GameHeaderBar : BaseHeaderBar
             real_this.new_game_button.hide ();
         }
     }
+
+    /*\
+    * * moves menu
+    \*/
+
+    private uint best_score = 0;
+    internal void save_best_score ()
+    {
+        if ((best_score == 0) || (last_moves_count < best_score))
+            best_score = last_moves_count;
+        generate_moves_menu ();
+    }
+
+    private void generate_moves_menu ()
+    {
+        GLib.Menu menu = new GLib.Menu ();
+        generate_undo_actions_section (ref menu);
+        if (best_score != 0)
+            generate_best_score_section (ref best_score, ref menu);
+        menu.freeze ();
+        history_button.set_menu_model (menu);
+    }
+
+    private static inline void generate_undo_actions_section (ref GLib.Menu menu)
+    {
+        GLib.Menu section = new GLib.Menu ();
+
+        /* Translators: during a game, entry in the menu of the history menubutton (with a mnemonic that 
appears pressing Alt) */
+        section.append (_("_Undo"), "ui.undo");
+
+        /* Translators: during a game, entry in the menu of the history menubutton (with a mnemonic that 
appears pressing Alt) */
+        section.append (_("_Restart"), "ui.restart");
+
+        section.freeze ();
+        menu.append_section (null, section);
+    }
+
+    private static inline void generate_best_score_section (ref uint best_score, ref GLib.Menu menu)
+    {
+        GLib.Menu section = new GLib.Menu ();
+
+        /* Translators: during a game that has already been finished (and possibly restarted), entry in the 
menu of the moves button */
+        section.append (_("Best score: %u").printf (best_score), null);
+
+        section.freeze ();
+        menu.append_section (null, section);
+    }
 }
diff --git a/src/game-window.vala b/src/game-window.vala
index 7f269c8..88b1500 100644
--- a/src/game-window.vala
+++ b/src/game-window.vala
@@ -150,6 +150,7 @@ private class GameWindow : BaseWindow, AdaptativeWidget
     {
         game_finished = true;
         headerbar.new_game_button_grab_focus ();
+        headerbar.save_best_score ();
     }
 
     protected override bool escape_pressed ()


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