[four-in-a-row/KaKnife/four-in-a-row-vala: 34/65] rework GameBoardView to reduce need of global vars



commit 53fc406044ec7e739ef35c2b4c3a256adbaf8883
Author: Jacob Humphrey <jacob ryan humphrey gmail com>
Date:   Fri Dec 14 20:34:23 2018 -0600

    rework GameBoardView to reduce need of global vars

 src/gfx.vala  | 53 +++++++++++++++--------------------------------------
 src/main.vala | 40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+), 38 deletions(-)
---
diff --git a/src/gfx.vala b/src/gfx.vala
index 8cb6f9a..37e4f53 100644
--- a/src/gfx.vala
+++ b/src/gfx.vala
@@ -101,7 +101,7 @@ class GameBoardView : Gtk.DrawingArea {
             return false;
 
         refresh_pixmaps();
-        GameBoardView.instance.draw_all();
+        instance.draw_all();
         return true;
     }
 
@@ -275,46 +275,23 @@ class GameBoardView : Gtk.DrawingArea {
         return true;
     }
 
-    protected override bool button_press_event(Gdk.EventButton e) {
-        int x, y;
-        if (application.player_active) {
-            return false;
-        }
-
-        if (application.gameover && application.timeout == 0) {
-            application.blink_winner(2);
-        } else if (application.is_player_human() && application.timeout == 0) {
-            get_window().get_device_position(e.device, out x, out y, null);
-            application.game_process_move(GameBoardView.instance.get_column(x));
-        }
+    /**
+     * column_clicked:
+     *
+     * emited when a column on the game board is clicked
+     *
+     * @column:
+     *
+     * Which column was clicked on
+     */
+    public signal bool column_clicked(int column);
 
-        return true;
+    protected override bool button_press_event(Gdk.EventButton e) {
+        int x;
+        get_window().get_device_position(e.device, out x, null, null);
+        return column_clicked(get_column(x));
     }
 
-    protected override bool key_press_event(Gdk.EventKey  e) {
-        if ((application.player_active) || application.timeout != 0 ||
-                (e.keyval != p.keypress[Move.LEFT] &&
-                e.keyval != p.keypress[Move.RIGHT] &&
-                e.keyval != p.keypress[Move.DROP])) {
-            return false;
-        }
-
-        if (application.gameover) {
-            application.blink_winner(2);
-            return true;
-        }
-
-        if (e.keyval == p.keypress[Move.LEFT] && application.column != 0) {
-            application.column_moveto--;
-            application.move_cursor(application.column_moveto);
-        } else if (e.keyval == p.keypress[Move.RIGHT] && application.column < 6) {
-            application.column_moveto++;
-            application.move_cursor(application.column_moveto);
-        } else if (e.keyval == p.keypress[Move.DROP]) {
-            application.game_process_move(application.column);
-        }
-        return true;
-    }
 }
 
 
diff --git a/src/main.vala b/src/main.vala
index 537de75..b62ef46 100644
--- a/src/main.vala
+++ b/src/main.vala
@@ -184,6 +184,19 @@ class FourInARow : Gtk.Application {
         add_action_entries(app_entries, this);
     }
 
+     bool column_clicked_cb(int column) {
+        if (player_active) {
+            return false;
+        }
+
+        if (gameover && timeout == 0) {
+            blink_winner(2);
+        } else if (is_player_human() && timeout == 0) {
+            game_process_move(column);
+        }
+        return true;
+    }
+
     void on_game_new(Variant? v) {
         stop_anim();
         game_reset();
@@ -777,12 +790,39 @@ class FourInARow : Gtk.Application {
         frame = builder.get_object("frame") as Gtk.AspectFrame;
 
         frame.add(GameBoardView.instance);
+        GameBoardView.instance.column_clicked.connect(column_clicked_cb);
+        GameBoardView.instance.key_press_event.connect(on_key_press);
 
         hint_action.set_enabled(false);
         undo_action.set_enabled(false);
     }
 
     Gtk.HeaderBar headerbar;
+
+    bool on_key_press(Gdk.EventKey  e) {
+        if ((player_active) || timeout != 0 ||
+                (e.keyval != p.keypress[Move.LEFT] &&
+                e.keyval != p.keypress[Move.RIGHT] &&
+                e.keyval != p.keypress[Move.DROP])) {
+            return false;
+        }
+
+        if (gameover) {
+            blink_winner(2);
+            return true;
+        }
+
+        if (e.keyval == p.keypress[Move.LEFT] && column != 0) {
+            column_moveto--;
+            move_cursor(column_moveto);
+        } else if (e.keyval == p.keypress[Move.RIGHT] && column < 6) {
+            column_moveto++;
+            move_cursor(column_moveto);
+        } else if (e.keyval == p.keypress[Move.DROP]) {
+            game_process_move(column);
+        }
+        return true;
+    }
 }
 
 SimpleAction hint_action;


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