[gnome-taquin] Introduce GameView.



commit a41c014d9d7a1d14bb541ed7117cbe19e1547b7a
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Sat Jan 12 07:03:55 2019 +0100

    Introduce GameView.
    
    That is needed for subclassing BaseWindow.

 data/taquin.ui       | 15 +--------
 po/POTFILES.in       |  3 +-
 po/POTFILES.skip     |  1 +
 src/game-view.vala   | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/game-window.vala | 80 ++++++++++++++----------------------------------
 src/meson.build      |  1 +
 6 files changed, 114 insertions(+), 72 deletions(-)
---
diff --git a/data/taquin.ui b/data/taquin.ui
index 7088cb0..49ae07d 100644
--- a/data/taquin.ui
+++ b/data/taquin.ui
@@ -9,21 +9,8 @@
       </object>
     </child>
     <child>
-      <object class="GtkStack" id="stack">
+      <object class="GameView" id="game_view">
         <property name="visible">True</property>
-        <property name="homogeneous">True</property>
-        <child>
-          <object class="GtkBox" id="new_game_box">
-            <property name="orientation">vertical</property>
-            <property name="visible">True</property>
-            <property name="halign">center</property>
-            <property name="valign">center</property>
-            <property name="margin">25</property>
-            <property name="width-request">350</property>
-            <property name="height-request">350</property>
-            <property name="spacing">6</property>
-          </object>
-        </child>
       </object>
     </child>
   </template>
diff --git a/po/POTFILES.in b/po/POTFILES.in
index a7fc35f..a583a5a 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -7,7 +7,8 @@ data/game-headerbar.ui
 data/taquin-screens.ui
 data/taquin.ui
 src/game-headerbar.vala
+src/game-view.vala
 src/game-window.vala
-src/taquin-main.vala
 src/taquin-game.vala
+src/taquin-main.vala
 src/taquin-view.vala
diff --git a/po/POTFILES.skip b/po/POTFILES.skip
index 297b08b..c23fb2d 100644
--- a/po/POTFILES.skip
+++ b/po/POTFILES.skip
@@ -1,4 +1,5 @@
 src/game-headerbar.c
+src/game-view.c
 src/game-window.c
 src/taquin-main.c
 src/taquin-game.c
diff --git a/src/game-view.vala b/src/game-view.vala
new file mode 100644
index 0000000..120e76a
--- /dev/null
+++ b/src/game-view.vala
@@ -0,0 +1,86 @@
+/* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+ *
+ * Copyright (C) 2019 – Arnaud Bonatti <arnaud bonatti gmail com>
+ *
+ * This file is part of a GNOME game.
+ *
+ * This application 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 application 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 application. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using Gtk;
+
+private class GameView : Stack
+{
+    private Widget  game_content;
+    private Box     new_game_box;
+    private Button? start_game_button = null;
+
+    construct
+    {
+        new_game_box = new Box (Orientation.VERTICAL, /* spacing */ 6);
+        new_game_box.halign = Align.CENTER;
+        new_game_box.valign = Align.CENTER;
+        new_game_box.margin = 25;
+        new_game_box.width_request = 350;
+        new_game_box.height_request = 350;
+        new_game_box.show ();
+        add (new_game_box);
+    }
+
+    internal void set_content (GameWindowFlags flags, Box new_game_screen, Widget content)
+    {
+        new_game_box.pack_start (new_game_screen, true, true, 0);
+
+        if (GameWindowFlags.SHOW_START_BUTTON in flags)
+        {
+            /* Translators: when configuring a new game, label of the blue Start button (with a mnemonic 
that appears pressing Alt) */
+            start_game_button = new Button.with_mnemonic (_("_Start Game"));
+            start_game_button.width_request = 222;
+            start_game_button.height_request = 60;
+            start_game_button.halign = Align.CENTER;
+            start_game_button.set_action_name ("win.start-game");
+            /* Translators: when configuring a new game, tooltip text of the blue Start button */
+            // start_game_button.set_tooltip_text (_("Start a new game as configured"));
+            ((StyleContext) start_game_button.get_style_context ()).add_class ("suggested-action");
+            start_game_button.show ();
+            new_game_box.pack_end (start_game_button, false, false, 0);
+        }
+
+        game_content = content;
+        add (content);
+        content.margin = 25;
+        content.can_focus = true;
+        content.show ();
+    }
+
+    internal void show_new_game_box (bool grab_focus)
+    {
+        set_visible_child (new_game_box);
+        if (grab_focus && start_game_button != null)
+            start_game_button.grab_focus ();
+        // TODO else if (!grabs_focus && start_game_button == null)
+    }
+
+    internal void show_game_content (bool grab_focus)
+    {
+        set_visible_child (game_content);
+        if (grab_focus)
+            game_content.grab_focus ();
+    }
+
+    internal bool game_content_visible_if_true ()
+    {
+        return get_visible_child () == game_content;
+    }
+}
diff --git a/src/game-window.vala b/src/game-window.vala
index da53be3..e344c61 100644
--- a/src/game-window.vala
+++ b/src/game-window.vala
@@ -41,15 +41,9 @@ public class GameWindow : ApplicationWindow
 
     /* private widgets */
     [GtkChild] private GameHeaderBar    headerbar;
-    [GtkChild] private Stack            stack;
+    [GtkChild] private GameView         game_view;
 
-    private Button? start_game_button = null;
-
-    [GtkChild] private Box new_game_box;
-
-    private Widget view;
-
-    public GameWindow (string? css_resource, string name, int width, int height, bool maximized, bool 
start_now, GameWindowFlags flags, Box new_game_screen, Widget _view)
+    public GameWindow (string? css_resource, string name, int width, int height, bool maximized, bool 
start_now, GameWindowFlags flags, Box new_game_screen, Widget view_content)
     {
         if (css_resource != null)
         {
@@ -58,11 +52,13 @@ public class GameWindow : ApplicationWindow
             StyleContext.add_provider_for_screen (Gdk.Screen.get_default (), css_provider, 
STYLE_PROVIDER_PRIORITY_APPLICATION);
         }
 
-        view = _view;
-
         /* window actions */
         install_win_action_entries ();
 
+        /* add widgets */
+        game_view.set_content (flags, new_game_screen, view_content);
+        headerbar.add_controls (flags);
+
         /* window config */
         set_title (name);
         headerbar.set_title (name);
@@ -74,31 +70,6 @@ public class GameWindow : ApplicationWindow
         size_allocate.connect (size_allocate_cb);
         window_state_event.connect (window_state_event_cb);
 
-        /* add widgets */
-        new_game_box.pack_start (new_game_screen, true, true, 0);
-
-        if (GameWindowFlags.SHOW_START_BUTTON in flags)
-        {
-            /* Translators: when configuring a new game, label of the blue Start button (with a mnemonic 
that appears pressing Alt) */
-            start_game_button = new Button.with_mnemonic (_("_Start Game"));
-            start_game_button.width_request = 222;
-            start_game_button.height_request = 60;
-            start_game_button.halign = Align.CENTER;
-            start_game_button.set_action_name ("win.start-game");
-            /* Translators: when configuring a new game, tooltip text of the blue Start button */
-            // start_game_button.set_tooltip_text (_("Start a new game as configured"));
-            ((StyleContext) start_game_button.get_style_context ()).add_class ("suggested-action");
-            start_game_button.show ();
-            new_game_box.pack_end (start_game_button, false, false, 0);
-        }
-
-        stack.add (view);
-        view.margin = 25;
-        view.can_focus = true;
-        view.show ();
-
-        headerbar.add_controls (flags);
-
         /* start or not */
         if (start_now)
             show_view ();
@@ -142,7 +113,7 @@ public class GameWindow : ApplicationWindow
     public void cannot_undo_more ()
     {
         undo_action.set_enabled (false);
-        view.grab_focus ();
+        game_view.show_game_content (/* grab focus */ true);
     }
 
     public void set_subtitle (string? subtitle)
@@ -168,18 +139,13 @@ public class GameWindow : ApplicationWindow
     private void show_new_game_screen ()
     {
         bool grabs_focus = headerbar.show_new_game_screen (game_finished);
-        if (!grabs_focus && start_game_button != null)
-            start_game_button.grab_focus ();
-        // TODO else if (!grabs_focus && start_game_button == null)
-        stack.set_visible_child (new_game_box);
+        game_view.show_new_game_box (/* grab focus */ !grabs_focus);
     }
 
     private void show_view ()
     {
-        stack.set_visible_child (view);
         bool grabs_focus = headerbar.show_view (game_finished);
-        if (!grabs_focus)
-            view.grab_focus ();
+        game_view.show_game_content (/* grab focus */ !grabs_focus);
     }
 
     /*\
@@ -226,13 +192,13 @@ public class GameWindow : ApplicationWindow
 
     private void new_game_cb (/* SimpleAction action, Variant? variant */)
     {
-        if (stack.get_visible_child_name () != "frame")
+        if (!game_view.game_content_visible_if_true ())
             return;
 
         wait ();
 
-        stack.set_transition_type (StackTransitionType.SLIDE_LEFT);
-        stack.set_transition_duration (800);
+        game_view.set_transition_type (StackTransitionType.SLIDE_LEFT);
+        game_view.set_transition_duration (800);
 
         headerbar.new_game ();
         back_action.set_enabled (true);
@@ -241,7 +207,7 @@ public class GameWindow : ApplicationWindow
 
     private void start_game_cb (/* SimpleAction action, Variant? variant */)
     {
-        if (stack.get_visible_child_name () != "start-box")
+        if (game_view.game_content_visible_if_true ())
             return;
 
         game_finished = false;
@@ -251,19 +217,19 @@ public class GameWindow : ApplicationWindow
 
         play ();        // FIXME lag (see in Taquin…)
 
-        stack.set_transition_type (StackTransitionType.SLIDE_DOWN);
-        stack.set_transition_duration (1000);
+        game_view.set_transition_type (StackTransitionType.SLIDE_DOWN);
+        game_view.set_transition_duration (1000);
         show_view ();
     }
 
     private void back_cb (/* SimpleAction action, Variant? variant */)
     {
-        if (stack.get_visible_child_name () != "start-box")
+        if (game_view.game_content_visible_if_true ())
             return;
 
         // TODO change back headerbar subtitle?
-        stack.set_transition_type (StackTransitionType.SLIDE_RIGHT);
-        stack.set_transition_duration (800);
+        game_view.set_transition_type (StackTransitionType.SLIDE_RIGHT);
+        game_view.set_transition_duration (800);
         show_view ();
 
         back ();
@@ -271,13 +237,13 @@ public class GameWindow : ApplicationWindow
 
     private void undo_cb (/* SimpleAction action, Variant? variant */)
     {
-        if (stack.get_visible_child_name () != "frame")
+        if (!game_view.game_content_visible_if_true ())
             return;
 
         game_finished = false;
 
         if (headerbar.new_game_button_is_focus ())
-            view.grab_focus();
+            game_view.show_game_content (/* grab focus */ true);
         redo_action.set_enabled (true);
 
         undo ();
@@ -285,11 +251,11 @@ public class GameWindow : ApplicationWindow
 
     private void redo_cb (/* SimpleAction action, Variant? variant */)
     {
-        if (stack.get_visible_child_name () != "frame")
+        if (!game_view.game_content_visible_if_true ())
             return;
 
         if (headerbar.new_game_button_is_focus ())
-            view.grab_focus();
+            game_view.show_game_content (/* grab focus */ true);
         undo_action.set_enabled (true);
 
         redo ();
@@ -297,7 +263,7 @@ public class GameWindow : ApplicationWindow
 
     private void hint_cb (/* SimpleAction action, Variant? variant */)
     {
-        if (stack.get_visible_child_name () != "frame")
+        if (!game_view.game_content_visible_if_true ())
             return;
 
         hint ();
diff --git a/src/meson.build b/src/meson.build
index 251daf7..b508172 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -3,6 +3,7 @@ resources = gnome.compile_resources (meson.project_name(), 'taquin.gresource.xml
 executable(meson.project_name(),[
         'config.vapi',
         'game-headerbar.vala',
+        'game-view.vala',
         'game-window.vala',
         'taquin-game.vala',
         'taquin-main.vala',


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