[gnome-nibbles/arnaudb/modernize-code] Use GAction for number of ais.
- From: Arnaud B. <arnaudb src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-nibbles/arnaudb/modernize-code] Use GAction for number of ais.
- Date: Sun, 31 May 2020 21:17:19 +0000 (UTC)
commit 8cf8511e69527fd9af46bc76175285f0d7cd881a
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date: Sun May 31 21:50:24 2020 +0200
Use GAction for number of ais.
data/nibbles.ui | 19 ++++++++++++-
src/nibbles-window.vala | 76 ++++++++++++++-----------------------------------
2 files changed, 40 insertions(+), 55 deletions(-)
---
diff --git a/data/nibbles.ui b/data/nibbles.ui
index 3d9ba4a..63a798c 100644
--- a/data/nibbles.ui
+++ b/data/nibbles.ui
@@ -299,6 +299,9 @@
<object class="GtkToggleButton" id="ai0">
<property name="use-underline">True</property>
<property name="label">_0</property>
+ <property name="action-name">win.change-number-of-ais</property>
+ <property name="action-target">@i 0</property>
+ <property name="sensitive" bind-source="ai0" bind-property="active"
bind-flags="invert-boolean"/>
<style>
<class name="number-box"/>
</style>
@@ -311,6 +314,9 @@
<object class="GtkToggleButton" id="ai1">
<property name="use-underline">True</property>
<property name="label">_1</property>
+ <property name="action-name">win.change-number-of-ais</property>
+ <property name="action-target">@i 1</property>
+ <property name="sensitive" bind-source="ai1" bind-property="active"
bind-flags="invert-boolean"/>
<style>
<class name="number-box"/>
</style>
@@ -323,6 +329,9 @@
<object class="GtkToggleButton" id="ai2">
<property name="use-underline">True</property>
<property name="label">_2</property>
+ <property name="action-name">win.change-number-of-ais</property>
+ <property name="action-target">@i 2</property>
+ <property name="sensitive" bind-source="ai2" bind-property="active"
bind-flags="invert-boolean"/>
<style>
<class name="number-box"/>
</style>
@@ -336,7 +345,9 @@
<property name="visible">True</property>
<property name="use-underline">True</property>
<property name="label">_3</property>
- <property name="active">True</property>
+ <property name="action-name">win.change-number-of-ais</property>
+ <property name="action-target">@i 3</property>
+ <property name="sensitive" bind-source="ai3" bind-property="active"
bind-flags="invert-boolean"/>
<style>
<class name="number-box"/>
</style>
@@ -350,6 +361,9 @@
<property name="visible">True</property>
<property name="use-underline">True</property>
<property name="label">_4</property>
+ <property name="action-name">win.change-number-of-ais</property>
+ <property name="action-target">@i 4</property>
+ <property name="sensitive" bind-source="ai4" bind-property="active"
bind-flags="invert-boolean"/>
<style>
<class name="number-box"/>
</style>
@@ -363,6 +377,9 @@
<property name="visible">True</property>
<property name="use-underline">True</property>
<property name="label">_5</property>
+ <property name="action-name">win.change-number-of-ais</property>
+ <property name="action-target">@i 5</property>
+ <property name="sensitive" bind-source="ai5" bind-property="active"
bind-flags="invert-boolean"/>
<style>
<class name="number-box"/>
</style>
diff --git a/src/nibbles-window.vala b/src/nibbles-window.vala
index 403c652..f7c686a 100644
--- a/src/nibbles-window.vala
+++ b/src/nibbles-window.vala
@@ -95,6 +95,7 @@ private class NibblesWindow : ApplicationWindow
{"preferences", preferences_cb},
{"scores", scores_cb},
+ {"change-number-of-ais", null, "i", "3", change_number_of_ais },
{"change-humans-number", null, "i", "1", change_humans_number },
{"show-new-game-screen", show_new_game_screen_cb},
{"show-controls-screen", show_controls_screen_cb},
@@ -129,17 +130,11 @@ private class NibblesWindow : ApplicationWindow
key_controller.key_pressed.connect (key_press_event_cb);
number_of_ai_buttons = new Gee.LinkedList<ToggleButton> ();
- ai0.toggled.connect (change_number_of_ai_cb);
number_of_ai_buttons.add (ai0);
- ai1.toggled.connect (change_number_of_ai_cb);
number_of_ai_buttons.add (ai1);
- ai2.toggled.connect (change_number_of_ai_cb);
number_of_ai_buttons.add (ai2);
- ai3.toggled.connect (change_number_of_ai_cb);
number_of_ai_buttons.add (ai3);
- ai4.toggled.connect (change_number_of_ai_cb);
number_of_ai_buttons.add (ai4);
- ai5.toggled.connect (change_number_of_ai_cb);
number_of_ai_buttons.add (ai5);
/* Create game */
@@ -173,7 +168,9 @@ private class NibblesWindow : ApplicationWindow
/* Number of worms */
game.numhumans = settings.get_int ("players");
game.numai = settings.get_int ("ai");
+ ((SimpleAction) lookup_action ("change-number-of-ais")).set_state (game.numai);
((SimpleAction) lookup_action ("change-humans-number")).set_state (game.numhumans);
+ update_visible_buttons ();
/* Controls screen */
arrow_pixbuf = NibblesView.load_pixmap_file ("arrow.svg", 5 * view.tile_size, 5 * view.tile_size);
@@ -471,8 +468,6 @@ private class NibblesWindow : ApplicationWindow
new_game_button.hide ();
pause_button.hide ();
- number_of_ai_buttons[game.numai].set_active (true);
-
set_default (next_button);
main_stack.set_transition_type (StackTransitionType.NONE);
@@ -482,21 +477,6 @@ private class NibblesWindow : ApplicationWindow
private void show_controls_screen_cb ()
{
- /* 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;
-
- /* Remember the option for the following runs */
- settings.set_int ("ai", game.numai);
- break;
- }
- }
-
/* Create worms and load properties */
game.create_worms ();
game.load_worm_properties (worm_settings);
@@ -561,6 +541,8 @@ private class NibblesWindow : ApplicationWindow
private inline void change_humans_number (SimpleAction action, Variant variant)
{
+ int number_of_worms = game.numhumans + game.numai;
+
int humans_number = variant.get_int32 ();
if (humans_number < 1 || humans_number > 4)
assert_not_reached ();
@@ -568,48 +550,34 @@ private class NibblesWindow : ApplicationWindow
game.numhumans = humans_number;
settings.set_int ("players", humans_number);
- int min_ai = 4 - humans_number;
- int max_ai = NibblesGame.MAX_WORMS - humans_number;
+ update_visible_buttons ();
+ number_of_ai_buttons[number_of_worms - game.numhumans].set_active (true);
+ }
+
+ private void update_visible_buttons ()
+ {
+ int min_ai = 4 - game.numhumans;
+ int max_ai = NibblesGame.MAX_WORMS - game.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 (humans_number == 4)
- {
+ if (game.numhumans == 4)
number_of_ai_buttons[0].show ();
- }
-
- number_of_ai_buttons[min_ai].set_active (true);
}
- private void change_number_of_ai_cb (ToggleButton button)
+ private inline void change_number_of_ais (SimpleAction action, Variant variant)
{
- foreach (var other_button in number_of_ai_buttons)
- {
- 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;
- }
- }
- }
- button.set_active (true);
+ int number_of_ais = variant.get_int32 ();
+ if (number_of_ais > 5)
+ assert_not_reached ();
+ action.set_state (number_of_ais);
+ game.numai = number_of_ais;
+ settings.set_int ("ai", number_of_ais);
}
/*\
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]