[gnome-sudoku] Show game chooser screen on clicking 'New Game' button



commit 384f4f375241eb634eef5ec9620d6655df4a8394
Author: Parin Porecha <parinporecha gmail com>
Date:   Tue Jun 3 19:43:20 2014 +0530

    Show game chooser screen on clicking 'New Game' button

 data/gnome-sudoku.ui  |  146 ++++++++++++++++++++++++++++++++++++++++++++++++-
 src/gnome-sudoku.vala |  107 +++++++++++++++++++++++++++++++++++-
 src/sudoku-view.vala  |   19 +++---
 3 files changed, 258 insertions(+), 14 deletions(-)
---
diff --git a/data/gnome-sudoku.ui b/data/gnome-sudoku.ui
index 076d1a1..a0a5b80 100644
--- a/data/gnome-sudoku.ui
+++ b/data/gnome-sudoku.ui
@@ -51,15 +51,158 @@
                         </child>
                     </object>
                 </child>
+                <child>
+                    <object class="GtkButton" id="back_button">
+                        <property name="visible">False</property>
+                        <property name="halign">center</property>
+                        <property name="valign">center</property>
+                        <property name="label" translatable="yes">_Back</property>
+                        <property name="tooltip-text" translatable="yes">Go back to the current 
game</property>
+                        <property name="use_underline">True</property>
+                        <property name="action-name">app.back</property>
+                        <style>
+                            <class name="text-button"/>
+                        </style>
+                    </object>
+                </child>
             </object>
         </child>
         <child>
             <object class="GtkBox" id="main_box">
                 <property name="visible">True</property>
+                <property name="halign">center</property>
+                <child>
+                    <object class="GtkBox" id="start_box">
+                        <property name="orientation">vertical</property>
+                        <property name="visible">False</property>
+                        <property name="halign">center</property>
+                        <child>
+                            <object class="GtkBox" id="new_game_box">
+                                 <property name="visible">True</property>
+                                 <property name="orientation">vertical</property>
+                                 <child>
+                                    <object class="GtkBox" id="easy_medium_box">
+                                        <property name="visible">True</property>
+                                        <child>
+                                            <object class="GtkBox" id="easy_grid">
+                                                <property name="visible">True</property>
+                                                <property name="orientation">vertical</property>
+                                                <child>
+                                                    <object class="GtkLabel" id="easy_label">
+                                                        <property name="visible">True</property>
+                                                        <property name="can_focus">False</property>
+                                                        <property name="label" 
translatable="yes">Easy</property>
+                                                        <attributes>
+                                                            <attribute name="weight" value="bold"/>
+                                                        </attributes>
+                                                    </object>
+                                                </child>
+                                            </object>
+                                            <packing>
+                                                <property name="expand">True</property>
+                                                <property name="fill">True</property>
+                                                <property name="padding">20</property>
+                                            </packing>
+                                        </child>
+                                        <child>
+                                            <object class="GtkBox" id="medium_grid">
+                                                <property name="visible">True</property>
+                                                <property name="orientation">vertical</property>
+                                                <child>
+                                                    <placeholder/>
+                                                </child>
+                                                <child>
+                                                    <object class="GtkLabel" id="medium_label">
+                                                        <property name="visible">True</property>
+                                                        <property name="can_focus">False</property>
+                                                        <property name="label" 
translatable="yes">Medium</property>
+                                                        <attributes>
+                                                            <attribute name="weight" value="bold"/>
+                                                        </attributes>
+                                                    </object>
+                                                </child>
+                                            </object>
+                                            <packing>
+                                                <property name="expand">True</property>
+                                                <property name="fill">True</property>
+                                                <property name="padding">20</property>
+                                            </packing>
+                                        </child>
+                                    </object>
+                                    <packing>
+                                        <property name="pack_type">start</property>
+                                    </packing>
+                                </child>
+                                <child>
+                                    <object class="GtkBox" id="hard_veryhard_box">
+                                         <property name="visible">True</property>
+                                         <child>
+                                            <object class="GtkBox" id="hard_grid">
+                                                <property name="visible">True</property>
+                                                <property name="orientation">vertical</property>
+                                                <child>
+                                                    <placeholder/>
+                                                </child>
+                                                <child>
+                                                    <object class="GtkLabel" id="hard_label">
+                                                        <property name="visible">True</property>
+                                                        <property name="can_focus">False</property>
+                                                        <property name="label" 
translatable="yes">Hard</property>
+                                                        <attributes>
+                                                            <attribute name="weight" value="bold"/>
+                                                        </attributes>
+                                                    </object>
+                                                </child>
+                                            </object>
+                                            <packing>
+                                                <property name="expand">True</property>
+                                                <property name="fill">True</property>
+                                                <property name="padding">20</property>
+                                            </packing>
+                                        </child>
+                                        <child>
+                                            <object class="GtkBox" id="very_hard_grid">
+                                                <property name="visible">True</property>
+                                                <property name="orientation">vertical</property>
+                                                <child>
+                                                    <placeholder/>
+                                                </child>
+                                                <child>
+                                                    <object class="GtkLabel" id="very_hard_label">
+                                                        <property name="visible">True</property>
+                                                        <property name="can_focus">False</property>
+                                                        <property name="label" translatable="yes">Very 
Hard</property>
+                                                        <attributes>
+                                                            <attribute name="weight" value="bold"/>
+                                                        </attributes>
+                                                    </object>
+                                                </child>
+                                            </object>
+                                            <packing>
+                                                <property name="expand">True</property>
+                                                <property name="fill">True</property>
+                                                <property name="padding">20</property>
+                                            </packing>
+                                         </child>
+                                     </object>
+                                    <packing>
+                                        <property name="pack_type">end</property>
+                                    </packing>
+                                 </child>
+                            </object> <!-- End of new game box -->
+                            <packing>
+                                <property name="expand">True</property>
+                                <property name="fill">True</property>
+                                <property name="position">1</property>
+                                <property name="padding">20</property>
+                            </packing>
+                        </child>
+                    </object> <!-- End of new_start_box -->
+                </child>
                 <child>
                     <object class="GtkBox" id="game_box">
                         <property name="visible">True</property>
-                        <property name="spacing">2</property>
+                        <property name="spacing">20</property>
                         <property name="margin">20</property>
                         <child>
                             <object class="GtkBox" id="controls_box">
@@ -76,7 +219,6 @@
                         <child>
                             <object class="GtkBox" id="grid_box">
                                 <property name="visible">True</property>
-                                <property name="margin-right">20</property>
                                 <child>
                                     <placeholder/>
                                 </child>
diff --git a/src/gnome-sudoku.vala b/src/gnome-sudoku.vala
index 466c0a4..56d4a32 100644
--- a/src/gnome-sudoku.vala
+++ b/src/gnome-sudoku.vala
@@ -20,6 +20,21 @@ public class Sudoku : Gtk.Application
     private Box game_box; // Holds the grid and controls boxes
     private Box grid_box; // Holds the view
     private Box controls_box; // Holds the controls
+    private Box start_box; // Holds the new game screen
+
+    private Button undo_button;
+    private Button redo_button;
+    private Button back_button;
+
+    private Box easy_grid;
+    private Box medium_grid;
+    private Box hard_grid;
+    private Box very_hard_grid;
+
+    private SudokuView easy_preview;
+    private SudokuView medium_preview;
+    private SudokuView hard_preview;
+    private SudokuView very_hard_preview;
 
     private SudokuStore sudoku_store;
     private SudokuSaver saver;
@@ -27,12 +42,15 @@ public class Sudoku : Gtk.Application
     private SimpleAction undo_action;
     private SimpleAction redo_action;
 
+    private string header_bar_subtitle;
+
     private bool show_possibilities = false;
 
     private const GLib.ActionEntry action_entries[] =
     {
         {"new-game", new_game_cb                                    },
         {"reset", reset_cb                                          },
+        {"back", back_cb                                            },
         {"undo", undo_cb                                            },
         {"redo", redo_cb                                            },
         {"print", print_cb                                          },
@@ -119,6 +137,11 @@ public class Sudoku : Gtk.Application
         game_box = (Box) builder.get_object ("game_box");
         grid_box = (Box) builder.get_object ("grid_box");
         controls_box = (Box) builder.get_object ("controls_box");
+        start_box = (Box) builder.get_object ("start_box");
+
+        undo_button = (Button) builder.get_object ("undo_button");
+        redo_button = (Button) builder.get_object ("redo_button");
+        back_button = (Button) builder.get_object ("back_button");
 
         var new_button = new Gtk.Button ();
         var new_label = new Gtk.Label.with_mnemonic (_("_New Puzzle"));
@@ -149,6 +172,39 @@ public class Sudoku : Gtk.Application
         saver = new SudokuSaver ();
         //SudokuGenerator gen = new SudokuGenerator();
 
+        easy_grid = (Box) builder.get_object ("easy_grid");
+        medium_grid = (Box) builder.get_object ("medium_grid");
+        hard_grid = (Box) builder.get_object ("hard_grid");
+        very_hard_grid = (Box) builder.get_object ("very_hard_grid");
+
+        easy_grid.button_press_event.connect ((event) => {
+            if (event.button == 1)
+                start_game (easy_preview.game.board);
+
+            return false;
+        });
+
+        medium_grid.button_press_event.connect ((event) => {
+            if (event.button == 1)
+                start_game (medium_preview.game.board);
+
+            return false;
+        });
+
+        hard_grid.button_press_event.connect ((event) => {
+            if (event.button == 1)
+                start_game (hard_preview.game.board);
+
+            return false;
+        });
+
+        very_hard_grid.button_press_event.connect ((event) => {
+            if (event.button == 1)
+                start_game (very_hard_preview.game.board);
+
+            return false;
+        });
+
         var savegame = saver.get_savedgame ();
         if (savegame != null)
             start_game (savegame.board);
@@ -175,7 +231,6 @@ public class Sudoku : Gtk.Application
         var rater = new SudokuRater(ref completed_board);
         var rating = rater.get_difficulty ();
         debug ("\n%s", rating.to_string ());
-        header_bar.set_subtitle (rating.get_catagory ().to_string ());
         undo_action.set_enabled (false);
         redo_action.set_enabled (false);
 
@@ -183,6 +238,9 @@ public class Sudoku : Gtk.Application
             grid_box.remove (view);
         }
 
+        header_bar_subtitle = rating.get_catagory ().to_string ();
+        back_cb ();
+
         game = new SudokuGame (board);
 
         game.timer.start ();
@@ -242,8 +300,41 @@ public class Sudoku : Gtk.Application
 
     private void new_game_cb ()
     {
-        var random_difficulty = (DifficultyCatagory) Random.int_range (0, 4);
-        start_game (sudoku_store.get_random_board (random_difficulty));
+        start_box.visible = true;
+        back_button.visible = true;
+        game_box.visible = false;
+        undo_button.visible = false;
+        redo_button.visible = false;
+        header_bar_subtitle = header_bar.get_subtitle ();
+        header_bar.set_subtitle (null);
+
+        if (easy_preview != null)
+            easy_preview.destroy ();
+        var easy_board = sudoku_store.get_random_easy_board ();
+        easy_preview = new SudokuView (new SudokuGame (easy_board), true);
+        easy_preview.show ();
+        easy_grid.pack_start (easy_preview);
+
+        if (medium_preview != null)
+            medium_preview.destroy ();
+        var medium_board = sudoku_store.get_random_medium_board ();
+        medium_preview = new SudokuView (new SudokuGame (medium_board), true);
+        medium_preview.show ();
+        medium_grid.pack_start (medium_preview);
+
+        if (hard_preview != null)
+            hard_preview.destroy ();
+        var hard_board = sudoku_store.get_random_hard_board ();
+        hard_preview = new SudokuView (new SudokuGame (hard_board), true);
+        hard_preview.show ();
+        hard_grid.pack_start (hard_preview);
+
+        if (very_hard_preview != null)
+            very_hard_preview.destroy ();
+        var very_hard_board = sudoku_store.get_random_very_hard_board ();
+        very_hard_preview = new SudokuView (new SudokuGame (very_hard_board), true);
+        very_hard_preview.show ();
+        very_hard_grid.pack_start (very_hard_preview);
     }
 
     private void reset_cb ()
@@ -259,6 +350,16 @@ public class Sudoku : Gtk.Application
         dialog.show ();
     }
 
+    private void back_cb ()
+    {
+        start_box.visible = false;
+        back_button.visible = false;
+        game_box.visible = true;
+        undo_button.visible = true;
+        redo_button.visible = true;
+        header_bar.set_subtitle (header_bar_subtitle);
+    }
+
     private void undo_cb ()
     {
         game.undo ();
diff --git a/src/sudoku-view.vala b/src/sudoku-view.vala
index d71e08c..b3a26cc 100644
--- a/src/sudoku-view.vala
+++ b/src/sudoku-view.vala
@@ -131,6 +131,15 @@ private class SudokuCellView : Gtk.DrawingArea
         this._row = row;
         this._col = col;
 
+        style.font_desc.set_size (Pango.SCALE * 13);
+        this.value = game.board [_row, _col];
+
+        if (small)
+        {
+            size_ratio = 0.83;
+            return;
+        }
+
         number_picker = new NumberPicker(ref game.board);
         number_picker.number_picked.connect ((o, number) => {
             value = number;
@@ -163,14 +172,6 @@ private class SudokuCellView : Gtk.DrawingArea
 
         can_focus = true;
 
-        style.font_desc.set_size (Pango.SCALE * 13);
-        this.value = game.board [_row, _col];
-
-        if (small)
-        {
-            size_ratio = 1;
-        }
-
         events = EventMask.EXPOSURE_MASK | EventMask.BUTTON_PRESS_MASK | EventMask.KEY_PRESS_MASK;
         focus_out_event.connect (focus_out_cb);
         game.cell_changed.connect (cell_changed_cb);
@@ -417,7 +418,7 @@ private class SudokuCellView : Gtk.DrawingArea
 
 public class SudokuView : Gtk.AspectFrame
 {
-    private SudokuGame game;
+    public SudokuGame game;
     private SudokuCellView[,] cells;
 
     public signal void cell_focus_in_event (int row, int col);


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