[four-in-a-row: 29/72] removed some global variabales
- From: Michael Catanzaro <mcatanzaro src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [four-in-a-row: 29/72] removed some global variabales
- Date: Sun, 16 Dec 2018 20:59:13 +0000 (UTC)
commit 2ff166c05e8c7f7e0a79cd9af8b9825e0f7dcac0
Author: Jacob Humphrey <jacob ryan humphrey gmail com>
Date: Fri Dec 14 17:11:30 2018 -0600
removed some global variabales
src/gfx.vala | 34 +++++++++++-----------
src/main.vala | 84 ++++++++++++++++++++++++++-----------------------------
src/prefs.vala | 29 +++++++++++--------
src/scorebox.vala | 41 +++++++++++++++++++--------
4 files changed, 104 insertions(+), 84 deletions(-)
---
diff --git a/src/gfx.vala b/src/gfx.vala
index de53a02..8cb6f9a 100644
--- a/src/gfx.vala
+++ b/src/gfx.vala
@@ -50,10 +50,10 @@ class GameBoardView : Gtk.DrawingArea {
events = Gdk.EventMask.EXPOSURE_MASK |
Gdk.EventMask.BUTTON_PRESS_MASK |
Gdk.EventMask.BUTTON_RELEASE_MASK;
- configure_event.connect(resize);
- draw.connect(expose);
+ //configure_event.connect(resize);
+ //draw.connect(expose);
//button_press_event.connect(button_press_event);
- key_press_event.connect(this.on_key_press);
+ //key_press_event.connect(this.on_key_press);
}
public int get_column(int xpos) {
@@ -75,7 +75,7 @@ class GameBoardView : Gtk.DrawingArea {
queue_draw_area(0, 0, boardsize, boardsize);
}
- public bool resize(Gdk.EventConfigure e) {
+ protected override bool configure_event(Gdk.EventConfigure e) {
int width, height;
width = get_allocated_width();
@@ -105,7 +105,7 @@ class GameBoardView : Gtk.DrawingArea {
return true;
}
- public bool expose(Cairo.Context cr) {
+ protected override bool draw(Cairo.Context cr) {
int r, c;
/* draw the background */
@@ -281,9 +281,9 @@ class GameBoardView : Gtk.DrawingArea {
return false;
}
- if (application.gameover && timeout == 0) {
+ if (application.gameover && application.timeout == 0) {
application.blink_winner(2);
- } else if (application.is_player_human() && timeout == 0) {
+ } 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));
}
@@ -291,8 +291,8 @@ class GameBoardView : Gtk.DrawingArea {
return true;
}
- bool on_key_press(Gtk.Widget w, Gdk.EventKey e) {
- if ((application.player_active) || timeout != 0 ||
+ 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])) {
@@ -304,19 +304,17 @@ class GameBoardView : Gtk.DrawingArea {
return true;
}
- if (e.keyval == p.keypress[Move.LEFT] && column != 0) {
- column_moveto--;
- application.move_cursor(column_moveto);
- } else if (e.keyval == p.keypress[Move.RIGHT] && column < 6) {
- column_moveto++;
- application.move_cursor(column_moveto);
+ 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(column);
+ application.game_process_move(application.column);
}
return true;
}
-
-
}
diff --git a/src/main.vala b/src/main.vala
index fd5c543..5ac3605 100644
--- a/src/main.vala
+++ b/src/main.vala
@@ -76,6 +76,25 @@ public enum SoundID {
class FourInARow : Gtk.Application {
public bool gameover;
public bool player_active;
+ PlayerID player;
+ PlayerID winner;
+ public PlayerID who_starts;
+ public int score[3];
+ static AnimID anim;
+ char vstr[53];
+ int moves;
+ public int column;
+ public int column_moveto;
+ int row;
+ int row_dropto;
+ int blink_r1 = 0;
+ int blink_c1 = 0;
+ int blink_r2 = 0;
+ int blink_c2 = 0;
+ int blink_t = 0;
+ int blink_n = 0;
+ bool blink_on = false;
+ public uint timeout = 0;
const ActionEntry app_entries[] = {
{"scores", on_game_scores},
@@ -215,7 +234,7 @@ class FourInARow : Gtk.Application {
window.show_all();
GameBoardView.instance.refresh_pixmaps();
GameBoardView.instance.draw_all();
- scorebox.update(); /* update visible player descriptions */
+ Scorebox.instance.update(); /* update visible player descriptions */
prompt_player();
game_reset();
}
@@ -349,7 +368,7 @@ class FourInARow : Gtk.Application {
if (gameover) {
score[winner]++;
- scorebox.update();
+ Scorebox.instance.update();
prompt_player();
} else {
swap_player();
@@ -535,14 +554,8 @@ class FourInARow : Gtk.Application {
}
void on_game_scores(SimpleAction action, Variant? parameter) {
- if (scorebox != null) {
- scorebox.present();
+ Scorebox.instance.present();
return;
- }
-
- scorebox = new Scorebox();
- scorebox.show_all();
- scorebox.update();
}
void on_game_exit(SimpleAction action, Variant? parameter) {
@@ -565,12 +578,12 @@ class FourInARow : Gtk.Application {
break;
case AnimID.HINT:
case AnimID.MOVE:
- if (column < column_moveto) {
- application.move(column + 1);
- } else if (column > column_moveto) {
- application.move(column - 1);
+ if (application.column < application.column_moveto) {
+ application.move(application.column + 1);
+ } else if (application.column > application.column_moveto) {
+ application.move(application.column - 1);
} else {
- timeout = 0;
+ application.timeout = 0;
if (anim == AnimID.MOVE) {
anim = AnimID.NONE;
application.process_move2(c);
@@ -581,25 +594,26 @@ class FourInARow : Gtk.Application {
}
break;
case AnimID.DROP:
- if (row < row_dropto) {
+ if (application.row < application.row_dropto) {
application.drop();
} else {
anim = AnimID.NONE;
- timeout = 0;
+ application.timeout = 0;
application.process_move3(c);
return false;
}
break;
case AnimID.BLINK:
- application.draw_line(blink_r1, blink_c1, blink_r2, blink_c2, blink_on ? blink_t
- : Tile.CLEAR);
- blink_n--;
- if (blink_n <= 0 && blink_on) {
+ application.draw_line(application.blink_r1, application.blink_c1,
+ application.blink_r2, application.blink_c2,
+ application.blink_on ? application.blink_t : Tile.CLEAR);
+ application.blink_n--;
+ if (application.blink_n <= 0 && application.blink_on) {
anim = AnimID.NONE;
- timeout = 0;
+ application.timeout = 0;
return false;
}
- blink_on = !blink_on;
+ application.blink_on = !application.blink_on;
break;
}
return true;
@@ -619,7 +633,7 @@ class FourInARow : Gtk.Application {
if (gameover) {
score[winner]--;
- scorebox.update();
+ Scorebox.instance.update();
gameover = false;
prompt_player();
} else {
@@ -772,27 +786,9 @@ SimpleAction new_game_action;
FourInARow? application;
Gtk.ApplicationWindow window;
-Scorebox? scorebox = null;
-
-PlayerID player;
-PlayerID winner;
-PlayerID who_starts;
-int score[3];
-AnimID anim;
-char vstr[53];
-int moves;
-int column;
-int column_moveto;
-int row;
-int row_dropto;
-int blink_r1 = 0;
-int blink_c1 = 0;
-int blink_r2 = 0;
-int blink_c2 = 0;
-int blink_t = 0;
-int blink_n = 0;
-bool blink_on = false;
-uint timeout = 0;
+//Scorebox? scorebox = null;
+
+
public int main(string[] argv) {
Intl.setlocale();
diff --git a/src/prefs.vala b/src/prefs.vala
index 37df47d..8dccbdc 100644
--- a/src/prefs.vala
+++ b/src/prefs.vala
@@ -20,13 +20,20 @@
*/
class Prefs {
- public bool do_sound;
+ bool _do_sound;
+ public bool do_sound {
+ get {
+ return settings.get_boolean("sound");
+ }
+ private set {
+ settings.set_boolean("sound", value);
+ }
+ }
public int theme_id;
public Level level[2];
public int keypress[3];
public Prefs() {
- do_sound = settings.get_boolean("sound");
level[PlayerID.PLAYER1] = Level.HUMAN; /* Human. Always human. */
level[PlayerID.PLAYER2] = (Level) settings.get_int("opponent");
keypress[Move.LEFT] = settings.get_int("key-left");
@@ -49,7 +56,7 @@ class Prefs {
public void settings_changed_cb(string key) {
if (key == "sound") {
- p.do_sound = settings.get_boolean("sound");
+ //p.do_sound = settings.get_boolean("sound");
((Gtk.ToggleButton)checkbutton_sound).set_active(p.do_sound);
} else if (key == "key-left") {
p.keypress[Move.LEFT] = settings.get_int("key-left");
@@ -78,6 +85,10 @@ class Prefs {
return 2;
}
+ public void on_toggle_sound(Gtk.ToggleButton t) {
+ p.do_sound = t.get_active();
+ }
+
}
Settings settings;
@@ -111,10 +122,6 @@ public void on_select_theme(Gtk.ComboBox combo) {
settings.set_int("theme-id", id);
}
-public void on_toggle_sound(Gtk.ToggleButton t) {
- p.do_sound = t.get_active();
- settings.set_boolean("sound", t.get_active());
-}
public void on_select_opponent(Gtk.ComboBox w) {
Gtk.TreeIter iter;
@@ -125,8 +132,8 @@ public void on_select_opponent(Gtk.ComboBox w) {
p.level[PlayerID.PLAYER2] = (Level)value;
settings.set_int("opponent", value);
- scorebox.reset();
- who_starts = PlayerID.PLAYER2; /* This gets reversed in game_reset. */
+ Scorebox.instance.reset();
+ application.who_starts = PlayerID.PLAYER2; /* This gets reversed in game_reset. */
application.game_reset();
}
@@ -227,10 +234,10 @@ public void prefsbox_open() {
checkbutton_sound.set_active(p.do_sound);
/* connect signals */
-
+ prefsbox.response.connect(() => {prefsbox.hide();});
combobox_theme.changed.connect(on_select_theme);
- checkbutton_sound.toggled.connect(on_toggle_sound);
+ checkbutton_sound.toggled.connect(p.on_toggle_sound);
prefsbox.show_all();
}
diff --git a/src/scorebox.vala b/src/scorebox.vala
index 9ac7b8d..0f60097 100644
--- a/src/scorebox.vala
+++ b/src/scorebox.vala
@@ -6,12 +6,25 @@
const string scorebox_gettext_package = Config.GETTEXT_PACKAGE;
class Scorebox : Gtk.Dialog {
- Gtk.Label label_name[3];
+ Gtk.Label[] label_name;
Gtk.Label label_score[3];
+ public FourInARow application;
+
+ static Once<Scorebox> _instance;
+ public static Scorebox instance {
+ get {
+ return _instance.once(() => {
+ var scorebox = new Scorebox();
+ //scorebox.show_all();
+ scorebox.update();
+ return scorebox;
+ });
+ }
+ }
- public Scorebox() {
+ Scorebox() {
Object(title: _("Scores"),
- parent: window,
+ //parent: window,
use_header_bar: 1,
destroy_with_parent: true,
resizable: false,
@@ -20,6 +33,9 @@ class Scorebox : Gtk.Dialog {
Gtk.Grid grid, grid2;
+ label_name = new Gtk.Label[3];
+ label_score = new Gtk.Label[3];
+
grid = new Gtk.Grid();
grid.halign = Gtk.Align.CENTER;
grid.row_spacing = 6;
@@ -61,15 +77,18 @@ class Scorebox : Gtk.Dialog {
grid2.attach(label_score[PlayerID.NOBODY], 1, 0, 1, 1);
label_score[PlayerID.NOBODY].set_xalign(0);
label_score[PlayerID.NOBODY].set_yalign(0.5f);
+ grid.show_all();
+
+ application = global::application;
}
public void update() {
if (p.get_n_human_players() == 1) {
if (p.level[PlayerID.PLAYER1] == Level.HUMAN) {
- label_score[PlayerID.PLAYER1].label = _("You:");
+ label_score[PlayerID.PLAYER1].set_text(_("You:"));
label_score[PlayerID.PLAYER2].label = _("Me:");
} else {
- label_score[PlayerID.PLAYER2].label = _("You:");
+ label_score[PlayerID.PLAYER2].set_text(_("You:"));
label_score[PlayerID.PLAYER1].label = _("Me:");
}
} else {
@@ -77,16 +96,16 @@ class Scorebox : Gtk.Dialog {
label_name[PlayerID.PLAYER2].label = theme_get_player(PlayerID.PLAYER2);
}
- label_score[PlayerID.PLAYER1].label = (string)score[PlayerID.PLAYER1];
- label_score[PlayerID.PLAYER2].label = (string)score[PlayerID.PLAYER2];
- label_score[PlayerID.NOBODY].label = (string)score[PlayerID.NOBODY];
+ label_score[PlayerID.PLAYER1].label = (string)global::application.score[PlayerID.PLAYER1];
+ label_score[PlayerID.PLAYER2].label = (string)application.score[PlayerID.PLAYER2];
+ label_score[PlayerID.NOBODY].label = (string)application.score[PlayerID.NOBODY];
}
public void reset() {
- score[PlayerID.PLAYER1] = 0;
- score[PlayerID.PLAYER2] = 0;
- score[PlayerID.NOBODY] = 0;
+ application.score[PlayerID.PLAYER1] = 0;
+ application.score[PlayerID.PLAYER2] = 0;
+ application.score[PlayerID.NOBODY] = 0;
update();
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]