[gnome-nibbles/arnaudb/modernize-code] Split Controls.



commit 080e76e28cb02a2ac48e126325ae8d01029ec6c9
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Mon Jun 1 14:30:20 2020 +0200

    Split Controls.

 data/controls.ui                 | 68 ++++++++++++++++++++++++++++++++++++++++
 data/gnome-nibbles.gresource.xml | 11 ++++---
 data/nibbles.ui                  | 46 +--------------------------
 po/POTFILES.in                   |  1 +
 src/controls.vala                | 31 ++++++++++++++++++
 src/nibbles-window.vala          | 22 ++-----------
 6 files changed, 110 insertions(+), 69 deletions(-)
---
diff --git a/data/controls.ui b/data/controls.ui
new file mode 100644
index 0000000..4679165
--- /dev/null
+++ b/data/controls.ui
@@ -0,0 +1,68 @@
+<?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="Controls" parent="GtkBox">
+    <property name="orientation">vertical</property>
+    <child>
+      <object class="GtkBox">
+        <property name="visible">True</property>
+        <property name="orientation">vertical</property>
+        <property name="valign">center</property>
+        <property name="spacing">25</property>
+        <child>
+          <object class="GtkLabel">
+            <property name="visible">True</property>
+            <property name="label" translatable="yes">Controls</property>
+            <property name="valign">center</property>
+            <style><class name="menu-title"/></style>
+          </object>
+        </child>
+        <child>
+          <object class="GtkBox" id="grids_box">
+            <property name="visible">True</property>
+            <property name="orientation">horizontal</property>
+            <property name="homogeneous">True</property>
+            <property name="valign">center</property>
+          </object>
+        </child>
+      </object>
+    </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">_Start</property>
+        <property name="action-name">win.start-game</property>
+        <property name="halign">center</property>
+        <property name="width-request">116</property>
+        <property name="height-request">34</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/data/gnome-nibbles.gresource.xml b/data/gnome-nibbles.gresource.xml
index adddd20..8d31368 100644
--- a/data/gnome-nibbles.gresource.xml
+++ b/data/gnome-nibbles.gresource.xml
@@ -1,12 +1,13 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <gresources>
   <gresource prefix="/org/gnome/nibbles/ui">
-    <file>nibbles.ui</file>
+    <file preprocess="xml-stripblanks"                  >controls.ui</file>
+    <file preprocess="xml-stripblanks"                  >controls-grid.ui</file>
     <file>nibbles.css</file>
-    <file>scoreboard.ui</file>
-    <file>player-score-box.ui</file>
-    <file>controls-grid.ui</file>
-    <file>preferences-dialog.ui</file>
+    <file preprocess="xml-stripblanks"                  >nibbles.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>
   </gresource>
 </gresources>
 
diff --git a/data/nibbles.ui b/data/nibbles.ui
index 3673b72..eb85f24 100644
--- a/data/nibbles.ui
+++ b/data/nibbles.ui
@@ -372,52 +372,8 @@
                   </packing>
                 </child>
                 <child>
-                  <object class="GtkBox">
+                  <object class="Controls" id="controls">
                     <property name="visible">True</property>
-                    <property name="orientation">vertical</property>
-                    <child>
-                      <object class="GtkBox">
-                        <property name="visible">True</property>
-                        <property name="orientation">vertical</property>
-                        <property name="valign">center</property>
-                        <property name="spacing">25</property>
-                        <child>
-                          <object class="GtkLabel">
-                            <property name="visible">True</property>
-                            <property name="label" translatable="yes">Controls</property>
-                            <property name="valign">center</property>
-                            <style><class name="menu-title"/></style>
-                          </object>
-                        </child>
-                        <child>
-                          <object class="GtkBox" id="grids_box">
-                            <property name="visible">True</property>
-                            <property name="orientation">horizontal</property>
-                            <property name="homogeneous">True</property>
-                            <property name="valign">center</property>
-                          </object>
-                        </child>
-                      </object>
-                    </child>
-                    <child>
-                      <object class="GtkButton" id="start_button">
-                        <property name="visible">True</property>
-                        <property name="can-default">True</property>
-                        <property name="use-underline">True</property>
-                        <property name="label" translatable="yes">_Start</property>
-                        <property name="action-name">win.start-game</property>
-                        <property name="halign">center</property>
-                        <property name="width-request">116</property>
-                        <property name="height-request">34</property>
-                        <style>
-                          <class name="suggested-action"/>
-                          <class name="play"/>
-                        </style>
-                      </object>
-                      <packing>
-                        <property name="pack-type">end</property>
-                      </packing>
-                    </child>
                   </object>
                   <packing>
                     <property name="name">controls</property>
diff --git a/po/POTFILES.in b/po/POTFILES.in
index e830d2c..89615e7 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -1,5 +1,6 @@
 # List of source files containing translatable strings.
 # Please keep this file in alphabetical order.
+data/controls.ui
 data/controls-grid.ui
 data/nibbles.ui
 data/org.gnome.Nibbles.appdata.xml.in
diff --git a/src/controls.vala b/src/controls.vala
index 4b04708..dde9551 100644
--- a/src/controls.vala
+++ b/src/controls.vala
@@ -1,6 +1,7 @@
 /* -*- 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>
+ * Copyright (C) 2020 Arnaud Bonatti <arnaud bonatti 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
@@ -18,6 +19,36 @@
 
 using Gtk;
 
+[GtkTemplate (ui = "/org/gnome/nibbles/ui/controls.ui")]
+private class Controls : Box
+{
+    [GtkChild] private Box grids_box;
+
+    private Gdk.Pixbuf arrow_pixbuf;
+    private Gdk.Pixbuf arrow_key_pixbuf;
+
+    internal void load_pixmaps (int tile_size)
+    {
+        arrow_pixbuf     = NibblesView.load_pixmap_file ("arrow.svg",     5 * tile_size, 5 * tile_size);
+        arrow_key_pixbuf = NibblesView.load_pixmap_file ("arrow-key.svg", 5 * tile_size, 5 * tile_size);
+    }
+
+    internal void prepare (Gee.LinkedList<Worm> worms, Gee.HashMap<Worm, WormProperties?> worm_props)
+    {
+        foreach (var grid in grids_box.get_children ())
+            grid.destroy ();
+
+        foreach (var worm in worms)
+        {
+            if (worm.is_human)
+            {
+                var grid = new ControlsGrid (worm.id, worm_props.@get (worm), arrow_pixbuf, 
arrow_key_pixbuf);
+                grids_box.add (grid);
+            }
+        }
+    }
+}
+
 [GtkTemplate (ui = "/org/gnome/nibbles/ui/controls-grid.ui")]
 private class ControlsGrid : Grid
 {
diff --git a/src/nibbles-window.vala b/src/nibbles-window.vala
index f29c68d..c5e4ca6 100644
--- a/src/nibbles-window.vala
+++ b/src/nibbles-window.vala
@@ -46,11 +46,8 @@ private class NibblesWindow : ApplicationWindow
     [GtkChild] private ToggleButton worms6;
 
     [GtkChild] private Button next_button;
-    [GtkChild] private Button start_button;
 
-    [GtkChild] private Box grids_box;
-    private Gdk.Pixbuf arrow_pixbuf;
-    private Gdk.Pixbuf arrow_key_pixbuf;
+    [GtkChild] private Controls controls;
 
     /* Statusbar widgets */
     [GtkChild] private Stack statusbar_stack;
@@ -168,8 +165,7 @@ private class NibblesWindow : ApplicationWindow
         update_buttons_labels ();
 
         /* Controls screen */
-        arrow_pixbuf = NibblesView.load_pixmap_file ("arrow.svg", 5 * view.tile_size, 5 * view.tile_size);
-        arrow_key_pixbuf = NibblesView.load_pixmap_file ("arrow-key.svg", 5 * view.tile_size, 5 * 
view.tile_size);
+        controls.load_pixmaps (view.tile_size);
 
         /* Check whether to display the first run screen */
         var first_run = settings.get_boolean ("first-run");
@@ -469,19 +465,7 @@ private class NibblesWindow : ApplicationWindow
         game.create_worms ();
         game.load_worm_properties (worm_settings);
 
-        foreach (var grid in grids_box.get_children ())
-            grid.destroy ();
-
-        foreach (var worm in game.worms)
-        {
-            if (worm.is_human)
-            {
-                var grid = new ControlsGrid (worm.id, game.worm_props.@get (worm), arrow_pixbuf, 
arrow_key_pixbuf);
-                grids_box.add (grid);
-            }
-        }
-
-        set_default (start_button);
+        controls.prepare (game.worms, game.worm_props);
 
         main_stack.set_visible_child_name ("controls");
     }


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