[four-in-a-row] Remove prefs.vala.
- From: Arnaud B. <arnaudb src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [four-in-a-row] Remove prefs.vala.
- Date: Sat, 21 Dec 2019 16:37:56 +0000 (UTC)
commit 5e6c553cc84188dbf0e7cc9f14289f18865d3d2f
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date: Sat Dec 21 17:33:02 2019 +0100
Remove prefs.vala.
The singleton was making hard
to understand which class was
using which settings and why.
src/four-in-a-row.vala | 69 ++++++++++++++++++++++++++++++------------------
src/game-board-view.vala | 27 ++++++++++++++-----
src/meson.build | 1 -
src/prefs.vala | 44 ------------------------------
src/scorebox.vala | 6 +++--
src/theme.vala | 18 ++++++-------
6 files changed, 77 insertions(+), 88 deletions(-)
---
diff --git a/src/four-in-a-row.vala b/src/four-in-a-row.vala
index 6e6f864..22b71a5 100644
--- a/src/four-in-a-row.vala
+++ b/src/four-in-a-row.vala
@@ -23,6 +23,8 @@ using Gtk;
private class FourInARow : Gtk.Application
{
+ private GLib.Settings settings = new GLib.Settings ("org.gnome.Four-in-a-row");
+
/* Translators: application name, as used in the window manager, the window title, the about dialog... */
private const string PROGRAM_NAME = _("Four-in-a-row");
private const int SIZE_VSTR = 53;
@@ -53,7 +55,7 @@ private class FourInARow : Gtk.Application
* The scores for the current instance (Player 1, Player 2, Draw)
*/
private int [] score = { 0, 0, 0 };
- private bool reset_score = false;
+ private bool reset_score = true;
// widgets
private Scorebox scorebox;
@@ -80,6 +82,13 @@ private class FourInARow : Gtk.Application
private bool blink_on = false;
private uint timeout = 0;
+ /* settings */
+ [CCode (notify = false)] internal int keypress_drop { private get; internal set; }
+ [CCode (notify = false)] internal int keypress_right { private get; internal set; }
+ [CCode (notify = false)] internal int keypress_left { private get; internal set; }
+ [CCode (notify = false)] internal bool sound_on { private get; internal set; }
+ [CCode (notify = false)] private int theme_id { private get; private set; }
+
private const GLib.ActionEntry app_entries [] = // see also add_actions()
{
{ "game-type", null, "s", "'dark'", change_game_type },
@@ -113,11 +122,17 @@ private class FourInARow : Gtk.Application
{
base.startup ();
+ settings.bind ("key-drop", this, "keypress-drop", SettingsBindFlags.GET |
SettingsBindFlags.NO_SENSITIVITY);
+ settings.bind ("key-right", this, "keypress-right", SettingsBindFlags.GET |
SettingsBindFlags.NO_SENSITIVITY);
+ settings.bind ("key-left", this, "keypress-left", SettingsBindFlags.GET |
SettingsBindFlags.NO_SENSITIVITY);
+ settings.bind ("sound", this, "sound-on", SettingsBindFlags.GET |
SettingsBindFlags.NO_SENSITIVITY);
+
/* UI parts */
new_game_screen = new NewGameScreen ();
new_game_screen.show ();
- game_board_view = new GameBoardView (game_board);
+ game_board_view = new GameBoardView (game_board, settings.get_int ("theme-id"));
+ settings.bind ("theme-id", game_board_view, "theme-id", SettingsBindFlags.GET |
SettingsBindFlags.NO_SENSITIVITY);
game_board_view.show ();
GLib.Menu app_menu = new GLib.Menu ();
@@ -170,7 +185,13 @@ private class FourInARow : Gtk.Application
history_button_2);
scorebox = new Scorebox (window, this);
- scorebox.update (score, one_player_game); /* update visible player descriptions */
+ settings.bind ("theme-id", scorebox, "theme-id", SettingsBindFlags.GET |
SettingsBindFlags.NO_SENSITIVITY);
+ settings.changed ["theme-id"].connect (() => {
+ scorebox.update (score, one_player_game);
+ theme_id = settings.get_int ("theme-id");
+ prompt_player ();
+ });
+ theme_id = settings.get_int ("theme-id");
add_actions ();
@@ -192,8 +213,6 @@ private class FourInARow : Gtk.Application
}
private inline void add_actions ()
{
- GLib.Settings settings = Prefs.instance.settings;
-
add_action (settings.create_action ("sound"));
add_action (settings.create_action ("theme-id"));
add_action (settings.create_action ("num-players"));
@@ -270,7 +289,7 @@ private class FourInARow : Gtk.Application
window.allow_undo (false);
window.allow_hint (false);
- one_player_game = Prefs.instance.settings.get_int ("num-players") == 1;
+ one_player_game = settings.get_int ("num-players") == 1;
if (reset_score)
{
score = { 0, 0, 0 };
@@ -279,9 +298,9 @@ private class FourInARow : Gtk.Application
}
if (one_player_game)
{
- player = Prefs.instance.settings.get_string ("first-player") == "computer" ? PlayerID.PLAYER2 :
PlayerID.PLAYER1;
- Prefs.instance.settings.set_string ("first-player", player == PlayerID.PLAYER1 ? "computer" :
"human");
- ai_level = Prefs.instance.settings.get_int ("opponent");
+ player = settings.get_string ("first-player") == "computer" ? PlayerID.PLAYER2 :
PlayerID.PLAYER1;
+ settings.set_string ("first-player", player == PlayerID.PLAYER1 ? "computer" : "human");
+ ai_level = settings.get_int ("opponent");
}
else
{
@@ -413,11 +432,11 @@ private class FourInARow : Gtk.Application
{
string who;
if (gameover)
- who = player == PLAYER1 ? theme_get_player_win (PlayerID.PLAYER1)
- : theme_get_player_win (PlayerID.PLAYER2);
+ who = player == PLAYER1 ? theme_get_player_win (PlayerID.PLAYER1, theme_id)
+ : theme_get_player_win (PlayerID.PLAYER2, theme_id);
else
- who = player == PLAYER1 ? theme_get_player_turn (PlayerID.PLAYER1)
- : theme_get_player_turn (PlayerID.PLAYER2);
+ who = player == PLAYER1 ? theme_get_player_turn (PlayerID.PLAYER1, theme_id)
+ : theme_get_player_turn (PlayerID.PLAYER2, theme_id);
set_status_message (_(who));
}
@@ -777,11 +796,11 @@ private class FourInARow : Gtk.Application
// game_type_action.set_state ((!) gvariant);
switch (type)
{
- case "human" : Prefs.instance.settings.set_int ("num-players", 1);
new_game_screen.update_sensitivity (true);
- Prefs.instance.settings.set_string ("first-player", "human");
return;
- case "computer" : Prefs.instance.settings.set_int ("num-players", 1);
new_game_screen.update_sensitivity (true);
- Prefs.instance.settings.set_string ("first-player", "computer");
return;
- case "two" : Prefs.instance.settings.set_int ("num-players", 2);
new_game_screen.update_sensitivity (false); return;
+ case "human" : settings.set_int ("num-players", 1); new_game_screen.update_sensitivity
(true);
+ settings.set_string ("first-player", "human");
return;
+ case "computer" : settings.set_int ("num-players", 1); new_game_screen.update_sensitivity
(true);
+ settings.set_string ("first-player", "computer");
return;
+ case "two" : settings.set_int ("num-players", 2); new_game_screen.update_sensitivity
(false); return;
default: assert_not_reached ();
}
}
@@ -805,9 +824,9 @@ private class FourInARow : Gtk.Application
private inline bool on_key_press (Gdk.EventKey e)
{
if (timeout != 0
- || (e.keyval != Prefs.instance.keypress_left
- && e.keyval != Prefs.instance.keypress_right
- && e.keyval != Prefs.instance.keypress_drop))
+ || (e.keyval != keypress_left
+ && e.keyval != keypress_right
+ && e.keyval != keypress_drop))
return false;
if (gameover)
@@ -816,17 +835,17 @@ private class FourInARow : Gtk.Application
return true;
}
- if (e.keyval == Prefs.instance.keypress_left && column != 0)
+ if (e.keyval == keypress_left && column != 0)
{
column_moveto--;
move_cursor (column_moveto);
}
- else if (e.keyval == Prefs.instance.keypress_right && column < 6)
+ else if (e.keyval == keypress_right && column < 6)
{
column_moveto++;
move_cursor (column_moveto);
}
- else if (e.keyval == Prefs.instance.keypress_drop)
+ else if (e.keyval == keypress_drop)
process_move (column);
return true;
@@ -960,7 +979,7 @@ private class FourInARow : Gtk.Application
private void play_sound (SoundID id)
{
- if (Prefs.instance.settings.get_boolean ("sound"))
+ if (sound_on)
{
if (sound_context_state == SoundContextState.INITIAL)
init_sound ();
diff --git a/src/game-board-view.vala b/src/game-board-view.vala
index e9bc682..4fae611 100644
--- a/src/game-board-view.vala
+++ b/src/game-board-view.vala
@@ -22,9 +22,20 @@ private class GameBoardView : Gtk.DrawingArea
{
[CCode (notify = false)] public Board game_board { private get; protected construct; }
- internal GameBoardView (Board game_board)
+ private int _theme_id = 0;
+ [CCode (notify = false)] public int theme_id
{
- Object (game_board: game_board);
+ private get { return _theme_id; }
+ internal construct set
+ {
+ _theme_id = value;
+ change_theme ();
+ }
+ }
+
+ internal GameBoardView (Board game_board, int theme_id)
+ {
+ Object (game_board: game_board, theme_id: theme_id);
}
construct
@@ -32,7 +43,6 @@ private class GameBoardView : Gtk.DrawingArea
events = Gdk.EventMask.EXPOSURE_MASK
| Gdk.EventMask.BUTTON_PRESS_MASK
| Gdk.EventMask.BUTTON_RELEASE_MASK;
- Prefs.instance.notify ["theme-id"].connect (change_theme);
load_pixmaps ();
}
@@ -141,7 +151,7 @@ private class GameBoardView : Gtk.DrawingArea
const double dashes [] = { 4.0, 4.0 };
Gdk.RGBA color = Gdk.RGBA ();
- color.parse (theme [Prefs.instance.theme_id].grid_color);
+ color.parse (theme [theme_id].grid_color);
Gdk.cairo_set_source_rgba (cr, color);
cr.set_operator (Cairo.Operator.SOURCE);
cr.set_line_width (1.0);
@@ -190,6 +200,9 @@ private class GameBoardView : Gtk.DrawingArea
private void refresh_pixmaps ()
{
+ if (tile_size == 0) // happens at game start
+ return;
+
Gdk.Pixbuf? tmp_pixbuf;
tmp_pixbuf = pb_tileset_raw.scale_simple (tile_size * 6, tile_size, Gdk.InterpType.BILINEAR);
@@ -207,10 +220,10 @@ private class GameBoardView : Gtk.DrawingArea
private void load_pixmaps ()
{
- load_image (theme [Prefs.instance.theme_id].fname_tileset, out pb_tileset_raw);
+ load_image (theme [theme_id].fname_tileset, out pb_tileset_raw);
- if (theme [Prefs.instance.theme_id].fname_bground != null)
- load_image ((!) theme [Prefs.instance.theme_id].fname_bground, out pb_bground_raw);
+ if (theme [theme_id].fname_bground != null)
+ load_image ((!) theme [theme_id].fname_bground, out pb_bground_raw);
else
create_background ();
}
diff --git a/src/meson.build b/src/meson.build
index f6c261f..5ad148d 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -27,7 +27,6 @@ sources = files(
'game-window.vala',
'history-button.vala',
'new-game-screen.vala',
- 'prefs.vala',
'scorebox.vala',
'theme.vala',
'vapi/config.vapi'
diff --git a/src/scorebox.vala b/src/scorebox.vala
index f824ec1..50c4583 100644
--- a/src/scorebox.vala
+++ b/src/scorebox.vala
@@ -21,6 +21,8 @@
using Gtk;
private class Scorebox : Dialog {
+ [CCode (notify = false)] internal int theme_id { private get; internal set; }
+
private Label[] label_name;
private Label[] label_score;
@@ -113,8 +115,8 @@ private class Scorebox : Dialog {
label_score[1].label = scores[0].to_string();
}
} else {
- label_name[0].label = theme_get_player(PlayerID.PLAYER1); // FIXME missing ":" at end
- label_name[1].label = theme_get_player(PlayerID.PLAYER2); // idem
+ label_name[0].label = theme_get_player(PlayerID.PLAYER1, theme_id); // FIXME missing ":" at
end
+ label_name[1].label = theme_get_player(PlayerID.PLAYER2, theme_id); // idem
label_score[PlayerID.PLAYER1].label = scores[PlayerID.PLAYER1].to_string();
label_score[PlayerID.PLAYER2].label = scores[PlayerID.PLAYER2].to_string();
diff --git a/src/theme.vala b/src/theme.vala
index 20b16a8..161ab72 100644
--- a/src/theme.vala
+++ b/src/theme.vala
@@ -44,25 +44,25 @@ private static string theme_get_title (int id)
return _(theme [id].title); // FIXME this gettext call feels horrible
}
-private static string theme_get_player_turn (PlayerID who)
+private static string theme_get_player_turn (PlayerID who, int theme_id)
{
if (who == PlayerID.PLAYER1)
- return theme [Prefs.instance.theme_id].player1_turn;
- return theme [Prefs.instance.theme_id].player2_turn;
+ return theme [theme_id].player1_turn;
+ return theme [theme_id].player2_turn;
}
-private static string theme_get_player_win (PlayerID who)
+private static string theme_get_player_win (PlayerID who, int theme_id)
{
if (who == PlayerID.PLAYER1)
- return theme [Prefs.instance.theme_id].player1_win;
- return theme [Prefs.instance.theme_id].player2_win;
+ return theme [theme_id].player1_win;
+ return theme [theme_id].player2_win;
}
-private static string theme_get_player (PlayerID who)
+private static string theme_get_player (PlayerID who, int theme_id)
{
if (who == PlayerID.PLAYER1)
- return theme [Prefs.instance.theme_id].player1;
- return theme [Prefs.instance.theme_id].player2;
+ return theme [theme_id].player1;
+ return theme [theme_id].player2;
}
private const Theme theme [] = {
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]