[gnome-nibbles] Allow selecting the number of computer players from the 'Number of Players' screen



commit 6ad24ed22cdf00817edfb9a7e0f27b230ad54515
Author: Iulian Radu <iulian radu67 gmail com>
Date:   Mon Feb 15 18:30:29 2016 +0200

    Allow selecting the number of computer players from the 'Number of
    Players' screen
    
    We decided that it's best that we we have a minimum number (4) of
    worms in a game. Therefore, the minimum number of AI should be
    the maximum number of possible worms minus the number of human players.
    Knowing this, we only present buttons to select the number of AI players
    based on the number of human worms selected by the user.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=762042

 data/nibbles.css       |    4 +-
 data/nibbles.ui        |  251 +++++++++++++++++++++++++++++++++---------------
 src/gnome-nibbles.vala |  100 ++++++++++++++++----
 src/nibbles-game.vala  |    2 +-
 4 files changed, 255 insertions(+), 102 deletions(-)
---
diff --git a/data/nibbles.css b/data/nibbles.css
index 7df6ee1..3591183 100644
--- a/data/nibbles.css
+++ b/data/nibbles.css
@@ -15,6 +15,7 @@
   font-size: 3em;
   font-weight: bold;
   font-family: Sans;
+  padding: 1px 38px;
   background-image: linear-gradient(to bottom, #3e4444);
 }
 .number-box:hover {
@@ -23,9 +24,6 @@
 .number-box:checked {
   background-image: linear-gradient(to bottom, #215d9c);
 }
-.number-box-focus:hover {
-  background-image: linear-gradient(to bottom, #2260a0, #1f5894 60%, #1d538b);
-}
 .controls-button {
   padding: 4px 8px;
   border: 1px solid;
diff --git a/data/nibbles.ui b/data/nibbles.ui
index 9387ff3..ad2a050 100644
--- a/data/nibbles.ui
+++ b/data/nibbles.ui
@@ -114,7 +114,7 @@
                                                 <property name="pack-type">end</property>
                                             </packing>
                                         </child>
-                                    </object>
+                                    </object> <!-- End of first_run -->
                                     <packing>
                                         <property name="name">first_run</property>
                                     </packing>
@@ -124,6 +124,7 @@
                                         <property name="visible">True</property>
                                         <property name="orientation">vertical</property>
                                         <property name="halign">center</property>
+                                        <property name="width-request">644</property>
                                         <child>
                                             <object class="GtkBox">
                                                 <property name="visible">True</property>
@@ -142,115 +143,207 @@
                                                                 <style><class name="menu-title"/></style>
                                                             </object>
                                                         </child>
-                                                        <child>
-                                                            <object class="GtkLabel">
-                                                                <property name="visible">True</property>
-                                                                <property name="label" 
translatable="yes">Select the number of human players.</property>
-                                                            </object>
-                                                        </child>
                                                     </object>
                                                 </child>
                                                 <child>
-                                                    <object class="GtkBox" id="number_of_players_box">
+                                                    <object class="GtkBox">
                                                         <property name="visible">True</property>
-                                                        <property name="orientation">horizontal</property>
-                                                        <property name="width-request">630</property>
-                                                        <property name="height-request">90</property>
-                                                        <property name="valign">center</property>
+                                                        <property name="orientation">vertical</property>
                                                         <property name="spacing">10</property>
-                                                        <property name="vexpand">True</property>
                                                         <child>
-                                                            <object class="GtkToggleButton" id="players1">
+                                                            <object class="GtkLabel">
                                                                 <property name="visible">True</property>
-                                                                <property 
name="use-underline">True</property>
-                                                                <property name="label">_1</property>
-                                                                <property name="width-request">150</property>
-                                                                <style>
-                                                                    <class name="number-box"/>
-                                                                </style>
+                                                                <property name="label" 
translatable="yes">Select the number of human players.</property>
                                                             </object>
-                                                            <packing>
-                                                                <property name="pack-type">start</property>
-                                                            </packing>
                                                         </child>
                                                         <child>
-                                                            <object class="GtkToggleButton" id="players2">
+                                                            <object class="GtkBox" 
id="number_of_players_box">
                                                                 <property name="visible">True</property>
-                                                                <property 
name="use-underline">True</property>
-                                                                <property name="label">_2</property>
-                                                                <property name="width-request">150</property>
-                                                                <style>
-                                                                    <class name="number-box"/>
-                                                                </style>
+                                                                <property 
name="orientation">horizontal</property>
+                                                                <property name="valign">center</property>
+                                                                <property name="halign">center</property>
+                                                                <property name="spacing">10</property>
+                                                                <property name="vexpand">True</property>
+                                                                <child>
+                                                                    <object class="GtkToggleButton" 
id="players1">
+                                                                        <property 
name="visible">True</property>
+                                                                        <property 
name="use-underline">True</property>
+                                                                        <property name="label">_1</property>
+                                                                        <property 
name="active">True</property>
+                                                                        <style>
+                                                                            <class name="number-box"/>
+                                                                        </style>
+                                                                    </object>
+                                                                    <packing>
+                                                                        <property 
name="pack-type">start</property>
+                                                                    </packing>
+                                                                </child>
+                                                                <child>
+                                                                    <object class="GtkToggleButton" 
id="players2">
+                                                                        <property 
name="visible">True</property>
+                                                                        <property 
name="use-underline">True</property>
+                                                                        <property name="label">_2</property>
+                                                                        <style>
+                                                                            <class name="number-box"/>
+                                                                        </style>
+                                                                    </object>
+                                                                    <packing>
+                                                                        <property 
name="pack-type">start</property>
+                                                                    </packing>
+                                                                </child>
+                                                                <child>
+                                                                    <object class="GtkToggleButton" 
id="players3">
+                                                                        <property 
name="visible">True</property>
+                                                                        <property 
name="use-underline">True</property>
+                                                                        <property name="label">_3</property>
+                                                                        <style>
+                                                                            <class name="number-box"/>
+                                                                        </style>
+                                                                    </object>
+                                                                    <packing>
+                                                                        <property 
name="pack-type">start</property>
+                                                                    </packing>
+                                                                </child>
+                                                                <child>
+                                                                    <object class="GtkToggleButton" 
id="players4">
+                                                                        <property 
name="visible">True</property>
+                                                                        <property 
name="use-underline">True</property>
+                                                                        <property name="label">_4</property>
+                                                                        <style>
+                                                                            <class name="number-box"/>
+                                                                        </style>
+                                                                    </object>
+                                                                    <packing>
+                                                                        <property 
name="pack-type">start</property>
+                                                                    </packing>
+                                                                </child>
                                                             </object>
-                                                            <packing>
-                                                                <property name="pack-type">start</property>
-                                                            </packing>
                                                         </child>
+                                                    </object>
+                                                </child>
+                                                <child>
+                                                    <object class="GtkBox">
+                                                        <property name="visible">True</property>
+                                                        <property name="orientation">vertical</property>
+                                                        <property name="spacing">10</property>
                                                         <child>
-                                                            <object class="GtkToggleButton" id="players3">
+                                                            <object class="GtkLabel">
                                                                 <property name="visible">True</property>
-                                                                <property 
name="use-underline">True</property>
-                                                                <property name="label">_3</property>
-                                                                <property name="width-request">150</property>
-                                                                <style>
-                                                                    <class name="number-box"/>
-                                                                </style>
+                                                                <property name="label" 
translatable="yes">Select the number of computer players.</property>
                                                             </object>
-                                                            <packing>
-                                                                <property name="pack-type">start</property>
-                                                            </packing>
                                                         </child>
                                                         <child>
-                                                            <object class="GtkToggleButton" id="players4">
+                                                            <object class="GtkBox" id="number_of_ai_box">
                                                                 <property name="visible">True</property>
-                                                                <property 
name="use-underline">True</property>
-                                                                <property name="label">_4</property>
-                                                                <property name="width-request">150</property>
-                                                                <style>
-                                                                    <class name="number-box"/>
-                                                                </style>
+                                                                <property 
name="orientation">horizontal</property>
+                                                                <property name="valign">center</property>
+                                                                <property name="halign">center</property>
+                                                                <property name="spacing">10</property>
+                                                                <property name="vexpand">True</property>
+                                                                <child>
+                                                                    <object class="GtkToggleButton" id="ai0">
+                                                                        <property 
name="use-underline">True</property>
+                                                                        <property name="label">_0</property>
+                                                                        <style>
+                                                                            <class name="number-box"/>
+                                                                        </style>
+                                                                    </object>
+                                                                    <packing>
+                                                                        <property 
name="pack-type">start</property>
+                                                                    </packing>
+                                                                </child>
+                                                                <child>
+                                                                    <object class="GtkToggleButton" id="ai1">
+                                                                        <property 
name="use-underline">True</property>
+                                                                        <property name="label">_1</property>
+                                                                        <style>
+                                                                            <class name="number-box"/>
+                                                                        </style>
+                                                                    </object>
+                                                                    <packing>
+                                                                        <property 
name="pack-type">start</property>
+                                                                    </packing>
+                                                                </child>
+                                                                <child>
+                                                                    <object class="GtkToggleButton" id="ai2">
+                                                                        <property 
name="use-underline">True</property>
+                                                                        <property name="label">_2</property>
+                                                                        <style>
+                                                                            <class name="number-box"/>
+                                                                        </style>
+                                                                    </object>
+                                                                    <packing>
+                                                                        <property 
name="pack-type">start</property>
+                                                                    </packing>
+                                                                </child>
+                                                                <child>
+                                                                    <object class="GtkToggleButton" id="ai3">
+                                                                        <property 
name="visible">True</property>
+                                                                        <property 
name="use-underline">True</property>
+                                                                        <property name="label">_3</property>
+                                                                        <property 
name="active">True</property>
+                                                                        <style>
+                                                                            <class name="number-box"/>
+                                                                        </style>
+                                                                    </object>
+                                                                    <packing>
+                                                                        <property 
name="pack-type">start</property>
+                                                                    </packing>
+                                                                </child>
+                                                                <child>
+                                                                    <object class="GtkToggleButton" id="ai4">
+                                                                        <property 
name="visible">True</property>
+                                                                        <property 
name="use-underline">True</property>
+                                                                        <property name="label">_4</property>
+                                                                        <style>
+                                                                            <class name="number-box"/>
+                                                                        </style>
+                                                                    </object>
+                                                                    <packing>
+                                                                        <property 
name="pack-type">start</property>
+                                                                    </packing>
+                                                                </child>
+                                                                <child>
+                                                                    <object class="GtkToggleButton" id="ai5">
+                                                                        <property 
name="visible">True</property>
+                                                                        <property 
name="use-underline">True</property>
+                                                                        <property name="label">_5</property>
+                                                                        <style>
+                                                                            <class name="number-box"/>
+                                                                        </style>
+                                                                    </object>
+                                                                    <packing>
+                                                                        <property 
name="pack-type">start</property>
+                                                                    </packing>
+                                                                </child>
                                                             </object>
-                                                            <packing>
-                                                                <property name="pack-type">start</property>
-                                                            </packing>
                                                         </child>
                                                     </object>
-                                                    <packing>
-                                                        <property name="pack-type">start</property>
-                                                    </packing>
                                                 </child>
-                                                </object>
-                                                <packing>
-                                                    <property name="pack-type">start</property>
-                                                </packing>
+                                            </object>
+                                            <packing>
+                                                <property name="pack-type">start</property>
+                                            </packing>
                                         </child>
                                         <child>
-                                            <object class="GtkRevealer" id="next_button_revealer">
+                                            <object class="GtkButton">
                                                 <property name="visible">True</property>
-                                                <property name="transition-type">4</property> <!-- SLIDE_UP 
-->
-                                                <property name="transition-duration">500</property>
-                                                <child>
-                                                    <object class="GtkButton">
-                                                        <property name="visible">True</property>
-                                                        <property name="use-underline">True</property>
-                                                        <property name="label" 
translatable="yes">_Next</property>
-                                                        <property 
name="action-name">app.show-controls-screen</property>
-                                                        <property name="width-request">116</property>
-                                                        <property name="height-request">34</property>
-                                                        <property name="halign">center</property>
-                                                        <style>
-                                                            <class name="suggested-action"/>
-                                                            <class name="play"/>
-                                                        </style>
-                                                    </object>
-                                                </child>
+                                                <property name="use-underline">True</property>
+                                                <property name="label" translatable="yes">_Next</property>
+                                                <property 
name="action-name">app.show-controls-screen</property>
+                                                <property name="width-request">116</property>
+                                                <property name="height-request">34</property>
+                                                <property name="halign">center</property>
+                                                <style>
+                                                    <class name="suggested-action"/>
+                                                    <class name="play"/>
+                                                </style>
                                             </object>
                                             <packing>
                                                 <property name="pack-type">end</property>
                                             </packing>
                                         </child>
-                                    </object>
+                                    </object> <!-- End of number_of_players -->
                                     <packing>
                                         <property name="name">number_of_players</property>
                                     </packing>
diff --git a/src/gnome-nibbles.vala b/src/gnome-nibbles.vala
index e69f74c..b794215 100644
--- a/src/gnome-nibbles.vala
+++ b/src/gnome-nibbles.vala
@@ -39,7 +39,7 @@ public class Nibbles : Gtk.Application
 
     /* Pre-game screen widgets */
     private Gee.LinkedList<Gtk.ToggleButton> number_of_players_buttons;
-    private Gtk.Revealer next_button_revealer;
+    private Gee.LinkedList<Gtk.ToggleButton> number_of_ai_buttons;
 
     private Gtk.Box grids_box;
     private Gdk.Pixbuf arrow_pixbuf;
@@ -93,11 +93,11 @@ public class Nibbles : Gtk.Application
         {"back", back_cb}
     };
 
-    private static const OptionEntry[] option_entries =
+    private const OptionEntry[] option_entries =
     {
         { "version", 'v', 0, OptionArg.NONE, null,
         /* Help string for command line --version flag */
-        N_("Show release version"), null},
+        N_("Show release version"), null },
 
         { null }
     };
@@ -175,7 +175,13 @@ public class Nibbles : Gtk.Application
             button.toggled.connect (change_number_of_players_cb);
             number_of_players_buttons.add (button);
         }
-        next_button_revealer = (Gtk.Revealer) builder.get_object ("next_button_revealer");
+        number_of_ai_buttons = new Gee.LinkedList<Gtk.ToggleButton> ();
+        for (int i = 0; i <= NibblesGame.MAX_AI; i++)
+        {
+            var button = (Gtk.ToggleButton) builder.get_object ("ai%d".printf (i));
+            button.toggled.connect (change_number_of_ai_cb);
+            number_of_ai_buttons.add (button);
+        }
         grids_box = (Gtk.Box) builder.get_object ("grids_box");
         window.set_titlebar (headerbar);
 
@@ -508,8 +514,22 @@ public class Nibbles : Gtk.Application
         {
             if (button.get_active ())
             {
-                var label = button.get_label ();
-                game.numhumans = int.parse (label.replace ("_", ""));
+                int numhumans = -1;
+                button.get_label ().scanf ("_%d", &numhumans);
+                game.numhumans = numhumans;
+                break;
+            }
+        }
+
+        /* Save selected number of computer players before changing the screen */
+        foreach (var button in number_of_ai_buttons)
+        {
+            if (button.get_active ())
+            {
+                int numai = -1;
+                button.get_label ().scanf ("_%d", &numai);
+                game.numai = numai;
+                break;
             }
         }
 
@@ -567,27 +587,69 @@ public class Nibbles : Gtk.Application
 
     private void change_number_of_players_cb (Gtk.ToggleButton button)
     {
-        if (!button.get_active () && button.get_style_context ().has_class ("number-box-focus"))
+        foreach (var other_button in number_of_players_buttons)
         {
-            button.set_active (true);
+            if (button != other_button)
+            {
+                if (other_button.get_active ())
+                {
+                    /* We are blocking the signal to prevent another callback when setting the previous
+                     * checked button to inactive
+                     */
+                    SignalHandler.block_matched (other_button, SignalMatchType.DATA, 0, 0, null, null, this);
+                    other_button.set_active (false);
+                    SignalHandler.unblock_matched (other_button, SignalMatchType.DATA, 0, 0, null, null, 
this);
+                    break;
+                }
+            }
         }
-        else if (button.get_active () && !button.get_style_context ().has_class ("number-box-focus"))
+        button.set_active (true);
+
+        int numhumans = -1;
+        button.get_label ().scanf ("_%d", &numhumans);
+
+        int min_ai = 4 - numhumans;
+        int max_ai = NibblesGame.MAX_WORMS - numhumans;
+        for (int i = 0; i < min_ai; i++)
+        {
+            number_of_ai_buttons[i].hide ();
+        }
+        for (int i = min_ai; i <= max_ai; i++)
+        {
+            number_of_ai_buttons[i].show ();
+        }
+        for (int i = max_ai + 1; i < number_of_ai_buttons.size; i++)
+        {
+            number_of_ai_buttons[i].hide ();
+        }
+
+        if (numhumans == 4)
+        {
+            number_of_ai_buttons[0].show ();
+        }
+
+        number_of_ai_buttons[min_ai].set_active (true);
+    }
+
+    private void change_number_of_ai_cb (Gtk.ToggleButton button)
+    {
+        foreach (var other_button in number_of_ai_buttons)
         {
-            next_button_revealer.set_reveal_child (true);
-            button.get_style_context ().add_class ("number-box-focus");
-            foreach (var other_button in number_of_players_buttons)
+            if (button != other_button)
             {
-                if (button != other_button)
+                if (other_button.get_active ())
                 {
-                    if (other_button.get_active ())
-                    {
-                        other_button.get_style_context ().remove_class ("number-box-focus");
-                        other_button.set_active (false);
-                        break;
-                    }
+                    /* We are blocking the signal to prevent another callback when setting the previous
+                     * checked button to inactive
+                     */
+                    SignalHandler.block_matched (other_button, SignalMatchType.DATA, 0, 0, null, null, this);
+                    other_button.set_active (false);
+                    SignalHandler.unblock_matched (other_button, SignalMatchType.DATA, 0, 0, null, null, 
this);
+                    break;
                 }
             }
         }
+        button.set_active (true);
     }
 
     /*\
diff --git a/src/nibbles-game.vala b/src/nibbles-game.vala
index e8f7830..5a9c76d 100644
--- a/src/nibbles-game.vala
+++ b/src/nibbles-game.vala
@@ -59,7 +59,7 @@ public class NibblesGame : Object
 
     /* Worms data */
     public int numhumans { get; set; }
-    private int numai = 3; // TODO: Add a gsetting
+    public int numai { get; set; }
     public int numworms { get; private set; }
 
     /* Game models */


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