Commit b119fbf6 authored by Arnaud Bonatti's avatar Arnaud Bonatti

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.
parent f321e740
...@@ -17,20 +17,6 @@ ...@@ -17,20 +17,6 @@
--> -->
<interface> <interface>
<requires lib="gtk+" version="3.12"/> <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"> <template class="GameHeaderBar" parent="BaseHeaderBar">
<property name="show-close-button">True</property> <property name="show-close-button">True</property>
<child> <child>
...@@ -38,7 +24,6 @@ ...@@ -38,7 +24,6 @@
<property name="visible">True</property> <property name="visible">True</property>
<property name="valign">center</property> <property name="valign">center</property>
<property name="focus-on-click">False</property> <property name="focus-on-click">False</property>
<property name="menu-model">history-menu</property>
<property name="label">0</property> <property name="label">0</property>
<property name="sensitive">False</property> <property name="sensitive">False</property>
<child internal-child="accessible"> <child internal-child="accessible">
......
...@@ -34,6 +34,8 @@ private class GameHeaderBar : BaseHeaderBar ...@@ -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 ((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 (); init_modes ();
if (window_name != "") if (window_name != "")
...@@ -186,10 +188,12 @@ private class GameHeaderBar : BaseHeaderBar ...@@ -186,10 +188,12 @@ private class GameHeaderBar : BaseHeaderBar
return new_game_button.is_focus; return new_game_button.is_focus;
} }
private uint last_moves_count = 0;
internal void set_moves_count (ref uint moves_count) internal void set_moves_count (ref uint moves_count)
{ {
history_button.set_label (moves_count.to_string ()); 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 ...@@ -239,4 +243,51 @@ private class GameHeaderBar : BaseHeaderBar
real_this.new_game_button.hide (); 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);
}
} }
...@@ -150,6 +150,7 @@ private class GameWindow : BaseWindow, AdaptativeWidget ...@@ -150,6 +150,7 @@ private class GameWindow : BaseWindow, AdaptativeWidget
{ {
game_finished = true; game_finished = true;
headerbar.new_game_button_grab_focus (); headerbar.new_game_button_grab_focus ();
headerbar.save_best_score ();
} }
protected override bool escape_pressed () protected override bool escape_pressed ()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment