[gnome-nibbles/arnaudb/modernize-code] Split Players.
- From: Arnaud B. <arnaudb src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-nibbles/arnaudb/modernize-code] Split Players.
- Date: Mon, 1 Jun 2020 18:51:13 +0000 (UTC)
commit e2977ef5572c2d8352609f7e43f73dd3f4c496d7
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date: Mon Jun 1 18:11:36 2020 +0200
Split Players.
data/gnome-nibbles.gresource.xml | 1 +
data/nibbles.ui | 188 +----------------------------------
data/players.ui | 208 +++++++++++++++++++++++++++++++++++++++
po/POTFILES.in | 2 +
po/POTFILES.skip | 1 +
src/meson.build | 1 +
src/nibbles-window.vala | 59 ++---------
src/players.vala | 86 ++++++++++++++++
8 files changed, 310 insertions(+), 236 deletions(-)
---
diff --git a/data/gnome-nibbles.gresource.xml b/data/gnome-nibbles.gresource.xml
index 8d31368..2464792 100644
--- a/data/gnome-nibbles.gresource.xml
+++ b/data/gnome-nibbles.gresource.xml
@@ -5,6 +5,7 @@
<file preprocess="xml-stripblanks" >controls-grid.ui</file>
<file>nibbles.css</file>
<file preprocess="xml-stripblanks" >nibbles.ui</file>
+ <file preprocess="xml-stripblanks" >players.ui</file>
<file preprocess="xml-stripblanks" >player-score-box.ui</file>
<file preprocess="xml-stripblanks" compressed="true">preferences-dialog.ui</file>
<file preprocess="xml-stripblanks" >scoreboard.ui</file>
diff --git a/data/nibbles.ui b/data/nibbles.ui
index eb85f24..9200961 100644
--- a/data/nibbles.ui
+++ b/data/nibbles.ui
@@ -180,193 +180,9 @@
</object>
</child>
<child>
- <object class="GtkBox">
+ <object class="Players" id="players">
<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>
- <property name="orientation">vertical</property>
- <property name="valign">center</property>
- <property name="spacing">60</property>
- <child>
- <object class="GtkBox">
- <property name="visible">True</property>
- <property name="orientation">vertical</property>
- <property name="spacing">10</property>
- <child>
- <object class="GtkLabel">
- <property name="visible">True</property>
- <property name="label" translatable="yes">Number of players</property>
- <style><class name="menu-title"/></style>
- </object>
- </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="GtkLabel">
- <property name="visible">True</property>
- <property name="label" translatable="yes">Select the number of human
players.</property>
- </object>
- </child>
- <child>
- <object class="GtkBox">
- <property name="visible">True</property>
- <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="action-name">win.change-humans-number</property>
- <property name="action-target">@i 1</property>
- <property name="sensitive" bind-source="players1" bind-property="active"
bind-flags="invert-boolean"/>
- <style>
- <class name="number-box"/>
- </style>
- </object>
- </child>
- <child>
- <object class="GtkToggleButton" id="players2">
- <property name="visible">True</property>
- <property name="use-underline">True</property>
- <property name="label">_2</property>
- <property name="action-name">win.change-humans-number</property>
- <property name="action-target">@i 2</property>
- <property name="sensitive" bind-source="players2" bind-property="active"
bind-flags="invert-boolean"/>
- <style>
- <class name="number-box"/>
- </style>
- </object>
- </child>
- <child>
- <object class="GtkToggleButton" id="players3">
- <property name="visible">True</property>
- <property name="use-underline">True</property>
- <property name="label">_3</property>
- <property name="action-name">win.change-humans-number</property>
- <property name="action-target">@i 3</property>
- <property name="sensitive" bind-source="players3" bind-property="active"
bind-flags="invert-boolean"/>
- <style>
- <class name="number-box"/>
- </style>
- </object>
- </child>
- <child>
- <object class="GtkToggleButton" id="players4">
- <property name="visible">True</property>
- <property name="use-underline">True</property>
- <property name="label">_4</property>
- <property name="action-name">win.change-humans-number</property>
- <property name="action-target">@i 4</property>
- <property name="sensitive" bind-source="players4" bind-property="active"
bind-flags="invert-boolean"/>
- <style>
- <class name="number-box"/>
- </style>
- </object>
- </child>
- </object>
- </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="GtkLabel">
- <property name="visible">True</property>
- <property name="label" translatable="yes">Select the number of computer
players.</property>
- </object>
- </child>
- <child>
- <object class="GtkBox">
- <property name="visible">True</property>
- <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="worms4">
- <property name="visible">True</property>
- <property name="use-underline">True</property>
- <property name="label">_3</property>
- <property name="action-name">win.change-worms-number</property>
- <property name="action-target">@i 4</property>
- <property name="sensitive" bind-source="worms4" bind-property="active"
bind-flags="invert-boolean"/>
- <style>
- <class name="number-box"/>
- </style>
- </object>
- </child>
- <child>
- <object class="GtkToggleButton" id="worms5">
- <property name="visible">True</property>
- <property name="use-underline">True</property>
- <property name="label">_4</property>
- <property name="action-name">win.change-worms-number</property>
- <property name="action-target">@i 5</property>
- <property name="sensitive" bind-source="worms5" bind-property="active"
bind-flags="invert-boolean"/>
- <style>
- <class name="number-box"/>
- </style>
- </object>
- </child>
- <child>
- <object class="GtkToggleButton" id="worms6">
- <property name="visible">True</property>
- <property name="use-underline">True</property>
- <property name="label">_5</property>
- <property name="action-name">win.change-worms-number</property>
- <property name="action-target">@i 6</property>
- <property name="sensitive" bind-source="worms6" bind-property="active"
bind-flags="invert-boolean"/>
- <style>
- <class name="number-box"/>
- </style>
- </object>
- </child>
- </object>
- </child>
- </object>
- </child>
- </object>
- <packing>
- <property name="pack-type">start</property>
- </packing>
- </child>
- <child>
- <object class="GtkButton" id="next_button">
- <property name="visible">True</property>
- <property name="can-default">True</property>
- <property name="use-underline">True</property>
- <property name="label" translatable="yes">_Next</property>
- <property name="action-name">win.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> <!-- End of number_of_players -->
+ </object>
<packing>
<property name="name">number_of_players</property>
</packing>
diff --git a/data/players.ui b/data/players.ui
new file mode 100644
index 0000000..ff22fcf
--- /dev/null
+++ b/data/players.ui
@@ -0,0 +1,208 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ This file is part of GNOME Nibbles.
+
+ Copyright (C) 2015-2016 Iulian Radu
+ Copyright (C) 2020 Arnaud Bonatti
+
+ GNOME Nibbles is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ GNOME Nibbles is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along
+ with this GNOME Nibbles. If not, see <https://www.gnu.org/licenses/>.
+-->
+<interface>
+ <template class="Players" parent="GtkBox">
+ <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>
+ <property name="orientation">vertical</property>
+ <property name="valign">center</property>
+ <property name="spacing">60</property>
+ <child>
+ <object class="GtkBox">
+ <property name="visible">True</property>
+ <property name="orientation">vertical</property>
+ <property name="spacing">10</property>
+ <child>
+ <object class="GtkLabel">
+ <property name="visible">True</property>
+ <property name="label" translatable="yes">Number of players</property>
+ <style><class name="menu-title"/></style>
+ </object>
+ </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="GtkLabel">
+ <property name="visible">True</property>
+ <property name="label" translatable="yes">Select the number of human players.</property>
+ </object>
+ </child>
+ <child>
+ <object class="GtkBox">
+ <property name="visible">True</property>
+ <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="action-name">players.change-players-number</property>
+ <property name="action-target">@i 1</property>
+ <property name="sensitive" bind-source="players1" bind-property="active"
bind-flags="invert-boolean"/>
+ <style>
+ <class name="number-box"/>
+ </style>
+ </object>
+ </child>
+ <child>
+ <object class="GtkToggleButton" id="players2">
+ <property name="visible">True</property>
+ <property name="use-underline">True</property>
+ <property name="label">_2</property>
+ <property name="action-name">players.change-players-number</property>
+ <property name="action-target">@i 2</property>
+ <property name="sensitive" bind-source="players2" bind-property="active"
bind-flags="invert-boolean"/>
+ <style>
+ <class name="number-box"/>
+ </style>
+ </object>
+ </child>
+ <child>
+ <object class="GtkToggleButton" id="players3">
+ <property name="visible">True</property>
+ <property name="use-underline">True</property>
+ <property name="label">_3</property>
+ <property name="action-name">players.change-players-number</property>
+ <property name="action-target">@i 3</property>
+ <property name="sensitive" bind-source="players3" bind-property="active"
bind-flags="invert-boolean"/>
+ <style>
+ <class name="number-box"/>
+ </style>
+ </object>
+ </child>
+ <child>
+ <object class="GtkToggleButton" id="players4">
+ <property name="visible">True</property>
+ <property name="use-underline">True</property>
+ <property name="label">_4</property>
+ <property name="action-name">players.change-players-number</property>
+ <property name="action-target">@i 4</property>
+ <property name="sensitive" bind-source="players4" bind-property="active"
bind-flags="invert-boolean"/>
+ <style>
+ <class name="number-box"/>
+ </style>
+ </object>
+ </child>
+ </object>
+ </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="GtkLabel">
+ <property name="visible">True</property>
+ <property name="label" translatable="yes">Select the number of computer players.</property>
+ </object>
+ </child>
+ <child>
+ <object class="GtkBox">
+ <property name="visible">True</property>
+ <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="worms4">
+ <property name="visible">True</property>
+ <property name="use-underline">True</property>
+ <property name="label">_3</property>
+ <property name="action-name">players.change-nibbles-number</property>
+ <property name="action-target">@i 4</property>
+ <property name="sensitive" bind-source="worms4" bind-property="active"
bind-flags="invert-boolean"/>
+ <style>
+ <class name="number-box"/>
+ </style>
+ </object>
+ </child>
+ <child>
+ <object class="GtkToggleButton" id="worms5">
+ <property name="visible">True</property>
+ <property name="use-underline">True</property>
+ <property name="label">_4</property>
+ <property name="action-name">players.change-nibbles-number</property>
+ <property name="action-target">@i 5</property>
+ <property name="sensitive" bind-source="worms5" bind-property="active"
bind-flags="invert-boolean"/>
+ <style>
+ <class name="number-box"/>
+ </style>
+ </object>
+ </child>
+ <child>
+ <object class="GtkToggleButton" id="worms6">
+ <property name="visible">True</property>
+ <property name="use-underline">True</property>
+ <property name="label">_5</property>
+ <property name="action-name">players.change-nibbles-number</property>
+ <property name="action-target">@i 6</property>
+ <property name="sensitive" bind-source="worms6" bind-property="active"
bind-flags="invert-boolean"/>
+ <style>
+ <class name="number-box"/>
+ </style>
+ </object>
+ </child>
+ </object>
+ </child>
+ </object>
+ </child>
+ </object>
+ <packing>
+ <property name="pack-type">start</property>
+ </packing>
+ </child>
+ <child>
+ <object class="GtkButton">
+ <property name="visible">True</property>
+ <property name="can-default">True</property>
+ <property name="use-underline">True</property>
+ <property name="label" translatable="yes">_Next</property>
+ <property name="action-name">win.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>
+ </template>
+</interface>
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 89615e7..b5e7a5f 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -6,11 +6,13 @@ data/nibbles.ui
data/org.gnome.Nibbles.appdata.xml.in
data/org.gnome.Nibbles.desktop.in
data/org.gnome.nibbles.gschema.xml
+data/players.ui
data/player-score-box.ui
data/preferences-dialog.ui
src/controls.vala
src/gnome-nibbles.vala
src/nibbles-view.vala
src/nibbles-window.vala
+src/players.vala
src/preferences-dialog.vala
src/scoreboard.vala
diff --git a/po/POTFILES.skip b/po/POTFILES.skip
index 0c012df..2a15376 100644
--- a/po/POTFILES.skip
+++ b/po/POTFILES.skip
@@ -2,5 +2,6 @@ src/controls.c
src/gnome-nibbles.c
src/nibbles-view.c
src/nibbles-window.c
+src/players.c
src/preferences-dialog.c
src/scoreboard.c
diff --git a/src/meson.build b/src/meson.build
index 623cda7..0eb7db5 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -27,6 +27,7 @@ gnome_nibbles_sources = [
'nibbles-game.vala',
'nibbles-view.vala',
'nibbles-window.vala',
+ 'players.vala',
'preferences-dialog.vala',
'scoreboard.vala',
'warp.vala',
diff --git a/src/nibbles-window.vala b/src/nibbles-window.vala
index c5e4ca6..295b519 100644
--- a/src/nibbles-window.vala
+++ b/src/nibbles-window.vala
@@ -41,12 +41,7 @@ private class NibblesWindow : ApplicationWindow
[GtkChild] private Button pause_button;
/* Pre-game screen widgets */
- [GtkChild] private ToggleButton worms4;
- [GtkChild] private ToggleButton worms5;
- [GtkChild] private ToggleButton worms6;
-
- [GtkChild] private Button next_button;
-
+ [GtkChild] private Players players;
[GtkChild] private Controls controls;
/* Statusbar widgets */
@@ -88,8 +83,6 @@ private class NibblesWindow : ApplicationWindow
{"preferences", preferences_cb},
{"scores", scores_cb},
- {"change-worms-number", null, "i", "4", change_worms_number },
- {"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},
{"back", back_cb}
@@ -160,9 +153,7 @@ private class NibblesWindow : ApplicationWindow
settings.set_int ("ai", numai);
}
game.numai = numai;
- ((SimpleAction) lookup_action ("change-worms-number" )).set_state (game.numhumans + numai);
- ((SimpleAction) lookup_action ("change-humans-number")).set_state (game.numhumans);
- update_buttons_labels ();
+ players.set_values (game.numhumans, numai);
/* Controls screen */
controls.load_pixmaps (view.tile_size);
@@ -452,8 +443,6 @@ private class NibblesWindow : ApplicationWindow
new_game_button.hide ();
pause_button.hide ();
- set_default (next_button);
-
main_stack.set_transition_type (StackTransitionType.NONE);
main_stack.set_visible_child_name ("number_of_players");
main_stack.set_transition_type (StackTransitionType.SLIDE_UP);
@@ -461,6 +450,13 @@ private class NibblesWindow : ApplicationWindow
private void show_controls_screen_cb ()
{
+ int numhumans, numai;
+ players.get_values (out numhumans, out numai);
+ game.numhumans = numhumans;
+ game.numai = numai;
+ settings.set_int ("players", numhumans);
+ settings.set_int ("ai", numai);
+
/* Create worms and load properties */
game.create_worms ();
game.load_worm_properties (worm_settings);
@@ -511,43 +507,6 @@ private class NibblesWindow : ApplicationWindow
main_stack.set_transition_type (StackTransitionType.SLIDE_UP);
}
- 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 ();
- action.set_state (humans_number);
- game.numhumans = humans_number;
- settings.set_int ("players", humans_number);
-
- int number_of_ais = number_of_worms - humans_number;
- game.numai = number_of_ais;
- settings.set_int ("ai", number_of_ais);
- update_buttons_labels ();
- }
-
- private void update_buttons_labels ()
- {
- int min_ai = 4 - game.numhumans;
-
- worms4.set_label (@"_$min_ai");
- worms5.set_label (@"_$(min_ai + 1)");
- worms6.set_label (@"_$(min_ai + 2)");
- }
-
- private inline void change_worms_number (SimpleAction action, Variant variant)
- {
- int worms_number = variant.get_int32 ();
- if (worms_number < 4 || worms_number > 6)
- assert_not_reached ();
- action.set_state (worms_number);
- int number_of_ais = worms_number - game.numhumans;
- game.numai = number_of_ais;
- settings.set_int ("ai", number_of_ais);
- }
-
/*\
* * Scoring
\*/
diff --git a/src/players.vala b/src/players.vala
new file mode 100644
index 0000000..a7117af
--- /dev/null
+++ b/src/players.vala
@@ -0,0 +1,86 @@
+/* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+ * Gnome Nibbles: Gnome Worm Game
+ * Copyright (C) 2015 Iulian-Gabriel Radu <iulian radu67 gmail com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using Gtk;
+
+[GtkTemplate (ui = "/org/gnome/nibbles/ui/players.ui")]
+private class Players : Box
+{
+ [GtkChild] private ToggleButton worms4;
+ [GtkChild] private ToggleButton worms5;
+ [GtkChild] private ToggleButton worms6;
+
+ private SimpleAction nibbles_number_action;
+ private SimpleAction players_number_action;
+
+ private const GLib.ActionEntry [] players_action_entries =
+ {
+ { "change-nibbles-number", null, "i", "4", change_nibbles_number },
+ { "change-players-number", null, "i", "1", change_players_number }
+ };
+
+ construct
+ {
+ SimpleActionGroup action_group = new SimpleActionGroup ();
+ action_group.add_action_entries (players_action_entries, this);
+ insert_action_group ("players", action_group);
+
+ nibbles_number_action = (SimpleAction) action_group.lookup_action ("change-nibbles-number");
+ players_number_action = (SimpleAction) action_group.lookup_action ("change-players-number");
+ }
+
+ internal void set_values (int players_number, int number_of_ais)
+ {
+ nibbles_number_action.set_state (players_number + number_of_ais);
+ players_number_action.set_state (players_number);
+ update_buttons_labels ();
+ }
+
+ internal void get_values (out int players_number, out int number_of_ais)
+ {
+ players_number = players_number_action.get_state ().get_int32 ();
+ number_of_ais = nibbles_number_action.get_state ().get_int32 () - players_number;
+ }
+
+ private inline void change_players_number (SimpleAction _players_number_action, Variant variant)
+ {
+ int players_number = variant.get_int32 ();
+ if (players_number < 1 || players_number > 4)
+ assert_not_reached ();
+ _players_number_action.set_state (players_number);
+
+ update_buttons_labels ();
+ }
+
+ private void update_buttons_labels ()
+ {
+ int min_ai = 4 - players_number_action.get_state ().get_int32 ();
+
+ worms4.set_label (@"_$min_ai");
+ worms5.set_label (@"_$(min_ai + 1)");
+ worms6.set_label (@"_$(min_ai + 2)");
+ }
+
+ private inline void change_nibbles_number (SimpleAction _nibbles_number_action, Variant variant)
+ {
+ int nibbles_number = variant.get_int32 ();
+ if (nibbles_number < 4 || nibbles_number > 6)
+ assert_not_reached ();
+ _nibbles_number_action.set_state (nibbles_number);
+ }
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]