Commit 6ad24ed2 authored by Iulian Radu's avatar Iulian Radu

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
parent a3f1fa66
......@@ -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;
......
This diff is collapsed.
......@@ -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);
}
/*\
......
......@@ -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 */
......
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