[gnome-robots] Rewrite properties in Vala
- From: Andrey Kutejko <akutejko src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-robots] Rewrite properties in Vala
- Date: Tue, 6 Oct 2020 19:31:40 +0000 (UTC)
commit c3b555044c15f152bb00abbf7119e8c75bdd7449
Author: Andrey Kutejko <andy128k gmail com>
Date: Tue Aug 25 23:19:07 2020 +0200
Rewrite properties in Vala
src/file-list.vala | 2 +-
src/game.c | 1 -
src/gnome-robots.c | 1 -
src/graphics.c | 21 +-
src/graphics.h | 2 +-
src/main.vapi | 14 +-
src/meson.build | 2 +-
src/properties.c | 783 ----------------------------------------------------
src/properties.h | 34 ---
src/properties.vala | 483 ++++++++++++++++++++++++++++++++
10 files changed, 509 insertions(+), 834 deletions(-)
---
diff --git a/src/file-list.vala b/src/file-list.vala
index a8bc5e3..6bdd5e0 100644
--- a/src/file-list.vala
+++ b/src/file-list.vala
@@ -151,7 +151,7 @@ public class GamesFileList {
*
* Return value: A widget with the list of names.
**/
- public Widget create_widget (string? selection, Flags flags) {
+ public ComboBox create_widget (string? selection, Flags flags) {
var model = create_model (flags);
var widget = new ComboBox.with_model (model);
var renderer = new CellRendererText ();
diff --git a/src/game.c b/src/game.c
index 0f06a85..1e2be47 100644
--- a/src/game.c
+++ b/src/game.c
@@ -32,7 +32,6 @@
#include "keyboard.h"
#include "game.h"
#include "gnome-robots.h"
-#include "properties.h"
#include "graphics.h"
/**********************************************************************/
diff --git a/src/gnome-robots.c b/src/gnome-robots.c
index 3a716dd..ebe336b 100644
--- a/src/gnome-robots.c
+++ b/src/gnome-robots.c
@@ -36,7 +36,6 @@
#include "gbdefs.h"
#include "riiv.h"
#include "graphics.h"
-#include "properties.h"
#include "game.h"
/* Minimum sizes. */
diff --git a/src/graphics.c b/src/graphics.c
index 7aee3d0..0c79475 100644
--- a/src/graphics.c
+++ b/src/graphics.c
@@ -36,7 +36,6 @@
#include "gbdefs.h"
#include "game.h"
#include "gnome-robots.h"
-#include "properties.h"
/**********************************************************************/
@@ -264,7 +263,7 @@ free_game_graphics (void)
}
void
-set_background_color (GdkRGBA color)
+set_background_color (GdkRGBA * color)
{
gdouble brightness;
@@ -274,16 +273,16 @@ set_background_color (GdkRGBA color)
/* While the two colours are labelled "light" and "dark" which one is
* which actually depends on how light or dark the base colour is. */
- brightness = color.red + color.green + color.blue;
+ brightness = color->red + color->green + color->blue;
if (brightness > (1.0 / 1.1)) {
/* Darken light colours. */
- light_background.red = 0.9 * color.red;
- light_background.green = 0.9 * color.green;
- light_background.blue = 0.9 * color.blue;
+ light_background.red = 0.9 * color->red;
+ light_background.green = 0.9 * color->green;
+ light_background.blue = 0.9 * color->blue;
} else if (brightness > 0.04) { /* Lighten darker colours. */
- light_background.red = 1.1 * color.red;
- light_background.green = 1.1 * color.green;
- light_background.blue = 1.1 * color.blue;
+ light_background.red = 1.1 * color->red;
+ light_background.green = 1.1 * color->green;
+ light_background.blue = 1.1 * color->blue;
} else { /* Very dark colours, add rather than multiply. */
light_background.red += 0.04;
light_background.green += 0.04;
@@ -291,7 +290,7 @@ set_background_color (GdkRGBA color)
}
light_background.alpha = 1.0;
- dark_background = color;
+ dark_background = *color;
clear_game_area ();
}
@@ -307,7 +306,7 @@ set_background_color_from_name (gchar * name)
if (!gdk_rgba_parse (&color, name)) {
gdk_rgba_parse (&color, "#7590AE");
}
- set_background_color (color);
+ set_background_color (&color);
}
/**
diff --git a/src/graphics.h b/src/graphics.h
index 5ed44e2..57f7a1f 100644
--- a/src/graphics.h
+++ b/src/graphics.h
@@ -15,7 +15,7 @@ gboolean draw_cb (GtkWidget * w, cairo_t * cr, gpointer data);
gboolean load_game_graphics (void);
gboolean free_game_graphics (void);
gint num_game_graphics (void);
-void set_background_color (GdkRGBA color);
+void set_background_color (GdkRGBA * color);
void set_background_color_from_name (gchar * name);
void add_yahoo_bubble (gint, gint);
diff --git a/src/main.vapi b/src/main.vapi
index cc958b0..785dcbd 100644
--- a/src/main.vapi
+++ b/src/main.vapi
@@ -1,2 +1,14 @@
-public bool properties_sound ();
+public void keyboard_set (uint keys[9]);
+
+public bool load_game_graphics ();
+public void clear_game_area ();
+public void set_background_color (Gdk.RGBA color);
+
+public void start_new_game ();
+
+[CCode (cheader_filename = "gnome-robots.h")]
+public Gtk.Window window;
+
+[CCode (cheader_filename = "gnome-robots.h")]
+public GLib.Settings settings;
diff --git a/src/meson.build b/src/meson.build
index 20c975d..d9a3f9d 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -18,6 +18,7 @@ vala_sources = files(
'game-config.vala',
'sound.vala',
'cursors.vala',
+ 'properties.vala',
)
vala_lib = static_library('riiv',
@@ -44,7 +45,6 @@ sources = files(
'gnome-robots.c',
'graphics.c',
'keyboard.c',
- 'properties.c',
)
resources = gnome.compile_resources(
'resources',
diff --git a/src/properties.vala b/src/properties.vala
new file mode 100644
index 0000000..79004ac
--- /dev/null
+++ b/src/properties.vala
@@ -0,0 +1,483 @@
+/*
+ * Gnome Robots II
+ * written by Mark Rae <m rae inpharmatica co uk>
+ *
+ * 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
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 program; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * For more details see the file COPYING.
+ */
+
+using Gtk;
+using Gdk;
+
+public GameConfigs game_configs;
+
+const int KB_TEXT_WIDTH = 60;
+const int KB_TEXT_HEIGHT = 32;
+
+const string KEY_PREFERENCES_GROUP = "preferences";
+const string KEY_BACKGROUND_COLOR = "background-color";
+const string KEY_CONFIGURATION = "configuration";
+const string KEY_ENABLE_SOUND = "enable-sound";
+const string KEY_SAFE_MOVES = "use-safe-moves";
+const string KEY_SHOW_TOOLBAR = "show-toolbar";
+const string KEY_SUPER_SAFE_MOVES = "use-super-safe-moves";
+const string KEY_THEME = "theme";
+const string KEY_CONTROL_KEY = "key%02d";
+
+const int N_KEYS = 9;
+
+struct Properties {
+ bool safe_moves;
+ bool super_safe_moves;
+ bool sound;
+ bool show_toolbar;
+ Gdk.RGBA bgcolour;
+ int selected_config;
+ uint keys[9];
+ string themename;
+}
+
+Dialog propbox = null;
+GamesFileList theme_list = null;
+Properties properties;
+
+/**
+ * Applies the changes made by the user
+ **/
+void apply_changes () {
+ load_keys ();
+ keyboard_set (properties.keys);
+}
+
+/**
+ * handles apply button events
+ *
+ * Returns:
+ * TRUE if the event was handled
+ **/
+void apply_cb () {
+ apply_changes ();
+
+ propbox.destroy ();
+ propbox = null;
+}
+
+/**
+ * handles pixmap selection messages
+ **/
+void pmap_selection (ComboBox combo) {
+ TreeIter iter;
+ if (combo.get_active_iter (out iter)) {
+ TreeModel model;
+ Value val;
+
+ model = combo.get_model ();
+ model.get_value (iter, 1, out val);
+
+ /* FIXME: Should be de-suffixed. */
+ properties.themename = val.get_string ();
+
+ conf_set_theme (properties.themename);
+
+ load_game_graphics ();
+ clear_game_area ();
+ }
+}
+
+/**
+ * handles configuration selection messages
+ **/
+void type_selection (int num) {
+ properties.selected_config = num;
+
+ var config = game_configs.@get ((uint)properties.selected_config);
+ var config_name = config.name ();
+ conf_set_configuration (config_name);
+
+ game_configs.set_current_index ((uint)properties.selected_config);
+
+ start_new_game ();
+}
+
+/**
+ * handles message from the default key buttons
+ **/
+void defkey_cb () {
+ for (int i = 0; i < N_KEYS; ++i) {
+ string key = "key%02d".printf (i);
+ settings.reset (key);
+ properties.keys[i] = settings.get_default_value (key).get_uint32 ();
+ }
+
+ keyboard_set (properties.keys);
+}
+
+
+/**
+ * fills the listbox with configuration names
+ **/
+void fill_typemenu (ComboBoxText menu) {
+ for (int i = 0; i < game_configs.count (); ++i) {
+ var config = game_configs.get_name ((uint)i);
+ menu.append_text (config);
+ }
+ menu.set_active (properties.selected_config);
+}
+
+
+/**
+ * fills the listbox with pixmap names
+ **/
+ComboBox make_theme_menu () {
+ var dir = Path.build_filename (DATA_DIRECTORY, "themes");
+ theme_list = new GamesFileList.images (dir);
+ theme_list.transform_basename ();
+
+ /* FIXME: Get rid of the bubbles images from the list (preferably by
+ * getting tid of the bubble pixmaps. */
+
+ return theme_list.create_widget (properties.themename,
+ GamesFileList.Flags.REMOVE_EXTENSION |
GamesFileList.Flags.REPLACE_UNDERSCORES);
+}
+
+void bg_color_callback (ColorChooser color_chooser) {
+ properties.bgcolour = color_chooser.get_rgba ();
+ set_background_color (properties.bgcolour);
+ clear_game_area ();
+ conf_set_background_color (properties.bgcolour);
+}
+
+public string properties_theme_name () {
+ return properties.themename;
+}
+
+/**
+ * show_properties_dialog
+ *
+ * Description:
+ * displays the properties dialog
+ **/
+public void show_properties_dialog () {
+ if (propbox != null)
+ return;
+
+ propbox = new Dialog.with_buttons (_("Preferences"),
+ window,
+ DialogFlags.USE_HEADER_BAR | DialogFlags.MODAL);
+
+ propbox.border_width = 5;
+ propbox.get_content_area ().set_spacing (2);
+ propbox.destroy.connect (() => propbox = null);
+
+ /* Set up notebook and add it to hbox of the gtk_dialog */
+ var notebook = new Notebook ();
+ notebook.border_width = 5;
+ propbox.get_content_area ().pack_start (notebook, true, true, 0);
+
+ /* The configuration page */
+ var cpage = new Box (Orientation.VERTICAL, 18);
+ cpage.border_width = 12;
+
+ var grid = new Grid ();
+ grid.set_row_spacing (6);
+ grid.set_column_spacing (12);
+ cpage.pack_start (grid, false, false, 0);
+
+ var label = new Label (_("Game Type"));
+ grid.attach (label, 0, 0, 1, 1);
+
+ var typemenu = new ComboBoxText ();
+ fill_typemenu (typemenu);
+ typemenu.changed.connect ((combo) => {
+ type_selection(combo.active);
+ });
+ grid.attach (typemenu, 1, 0, 1, 1);
+
+ var safe_chkbox = new CheckButton.with_mnemonic (_("_Use safe moves"));
+ safe_chkbox.set_active (properties.safe_moves);
+ safe_chkbox.set_tooltip_text (_("Prevent accidental moves that result in getting killed."));
+ grid.attach (safe_chkbox, 0, 1, 2, 1);
+
+ var super_safe_chkbox = new CheckButton.with_mnemonic (_("U_se super safe moves"));
+ super_safe_chkbox.set_active (properties.super_safe_moves);
+ super_safe_chkbox.set_tooltip_text (_("Prevents all moves that result in getting killed."));
+ super_safe_chkbox.set_sensitive (properties.safe_moves);
+ grid.attach (super_safe_chkbox, 0, 2, 2, 1);
+
+ safe_chkbox.toggled.connect ((toggle) => {
+ properties.safe_moves = toggle.active;
+ conf_set_use_safe_moves (properties.safe_moves);
+ super_safe_chkbox.set_sensitive (properties.safe_moves);
+ });
+ super_safe_chkbox.toggled.connect ((toggle) => {
+ properties.super_safe_moves = toggle.get_active ();
+ conf_set_use_super_safe_moves (properties.super_safe_moves);
+ });
+
+ var sound_chkbox = new CheckButton.with_mnemonic (_("_Enable sounds"));
+ sound_chkbox.set_active (properties.sound);
+ sound_chkbox.toggled.connect ((toggle) => {
+ properties.sound = toggle.active;
+ conf_set_enable_sound (properties.sound);
+ });
+ sound_chkbox.set_tooltip_text (_("Play sounds for events like winning a level and dying."));
+ grid.attach (sound_chkbox, 0, 3, 2, 1);
+
+ label = new Label.with_mnemonic (_("Game"));
+ notebook.append_page (cpage, label);
+
+ /* The graphics page */
+ var gpage = new Box (Orientation.VERTICAL, 18);
+ gpage.set_border_width (12);
+
+ grid = new Grid ();
+ grid.set_row_spacing (6);
+ grid.set_column_spacing (12);
+ gpage.pack_start (grid, false, false, 0);
+
+ label = new Label.with_mnemonic (_("_Image theme:"));
+ label.set_hexpand (true);
+ label.set_halign (Align.START);
+ grid.attach (label, 0, 0, 1, 1);
+
+ var pmapmenu = make_theme_menu ();
+ pmapmenu.changed.connect ((combo) => pmap_selection(combo));
+ label.set_mnemonic_widget (pmapmenu);
+ grid.attach (pmapmenu, 1, 0, 1, 1);
+
+ label = new Label.with_mnemonic (_("_Background color:"));
+ label.set_halign (Align.START);
+ grid.attach (label, 0, 1, 1, 1);
+
+ var w = new ColorButton ();
+ w.set_rgba (properties.bgcolour);
+ w.color_set.connect((color) => bg_color_callback(color));
+ label.set_mnemonic_widget (w);
+ grid.attach (w, 1, 1, 1, 1);
+
+ label = new Label.with_mnemonic (_("Appearance"));
+ notebook.append_page (gpage, label);
+
+ /* The keyboard page */
+ var kpage = new Box (Orientation.VERTICAL, 18);
+ kpage.set_border_width (12);
+
+ var vbox = new Box (Orientation.VERTICAL, 6);
+ kpage.pack_start (vbox, true, true, 0);
+
+ var controls_list = new GamesControlsList (settings);
+ controls_list.add_control ("key00", _("Key to move NW"), settings.get_default_value ("key00").get_uint32
());
+ controls_list.add_control ("key01", _("Key to move N"), settings.get_default_value ("key01").get_uint32
());
+ controls_list.add_control ("key02", _("Key to move NE"), settings.get_default_value ("key02").get_uint32
());
+ controls_list.add_control ("key03", _("Key to move W"), settings.get_default_value ("key03").get_uint32
());
+ controls_list.add_control ("key04", _("Key to hold"), settings.get_default_value ("key04").get_uint32
());
+ controls_list.add_control ("key05", _("Key to move E"), settings.get_default_value ("key05").get_uint32
());
+ controls_list.add_control ("key06", _("Key to move SW"), settings.get_default_value ("key06").get_uint32
());
+ controls_list.add_control ("key07", _("Key to move S"), settings.get_default_value ("key07").get_uint32
());
+ controls_list.add_control ("key08", _("Key to move SE"), settings.get_default_value ("key08").get_uint32
());
+
+ vbox.pack_start (controls_list, true, true, 0);
+
+ var hbox = new ButtonBox (Orientation.HORIZONTAL);
+ hbox.set_layout (ButtonBoxStyle.START);
+ vbox.pack_start (hbox, false, false, 0);
+
+ var dbut = new Button.with_mnemonic (_("_Restore Defaults"));
+ dbut.clicked.connect (() => defkey_cb());
+ hbox.pack_start (dbut, false, false, 0);
+
+ label = new Label.with_mnemonic (_("Keyboard"));
+ notebook.append_page (kpage, label);
+
+ propbox.delete_event.connect (() => {
+ propbox = null;
+ return false;
+ });
+ propbox.response.connect (() => apply_cb());
+
+ propbox.show_all ();
+}
+
+/**
+ * loads the game properties from a file
+ **/
+public void load_properties () {
+ load_keys ();
+
+ var bgcolour = settings.get_string (KEY_BACKGROUND_COLOR);
+ RGBA colour = RGBA ();
+ colour.parse (bgcolour);
+ properties.bgcolour = colour;
+ set_background_color (properties.bgcolour);
+
+ properties.themename = settings.get_string (KEY_THEME);
+
+ var cname = settings.get_string (KEY_CONFIGURATION);
+
+ properties.selected_config = 0;
+ for (int i = 0; i < game_configs.count (); ++i) {
+ var config = game_configs.get_name ((uint)i);
+ if (cname == config) {
+ properties.selected_config = i;
+ break;
+ }
+ }
+
+ properties.safe_moves = settings.get_boolean (KEY_SAFE_MOVES);
+ properties.super_safe_moves = settings.get_boolean (KEY_SUPER_SAFE_MOVES);
+ properties.sound = settings.get_boolean (KEY_ENABLE_SOUND);
+ properties.show_toolbar = settings.get_boolean (KEY_SHOW_TOOLBAR);
+
+ load_game_graphics ();
+ game_configs.set_current_index ((uint)properties.selected_config);
+ keyboard_set (properties.keys);
+}
+
+public void load_keys () {
+ for (int i = 0; i < N_KEYS; i++) {
+ var key = "key%02d".printf (i);
+ properties.keys[i] = settings.get_int (key);
+ }
+}
+
+public void conf_set_theme (string val) {
+ settings.set_string (KEY_THEME, val);
+}
+
+void conf_set_background_color (RGBA c) {
+ var colour = "#%04x%04x%04x".printf ((int) (c.red * 65535 + 0.5), (int) (c.green * 65535 + 0.5), (int)
(c.blue * 65535 + 0.5));
+ settings.set_string (KEY_BACKGROUND_COLOR, colour);
+}
+
+public void conf_set_configuration (string val) {
+ settings.set_string (KEY_CONFIGURATION, val);
+}
+
+public void conf_set_use_safe_moves (bool val) {
+ settings.set_boolean (KEY_SAFE_MOVES, val);
+}
+
+public void conf_set_use_super_safe_moves (bool val) {
+ settings.set_boolean (KEY_SUPER_SAFE_MOVES, val);
+}
+
+public void conf_set_enable_sound (bool val) {
+ settings.set_boolean (KEY_ENABLE_SOUND, val);
+}
+
+public void conf_set_show_toolbar (bool val) {
+ settings.set_boolean (KEY_SHOW_TOOLBAR, val);
+}
+
+public void conf_set_control_key (int i, uint keyval) {
+ var key = "key%02d".printf (i);
+ var keyval_name = keyval_name (keyval);
+ settings.set_string (key, keyval_name);
+}
+
+/**
+ * saves the game properties to a file
+ **/
+public void save_properties () {
+ for (int i = 0; i < N_KEYS; i++) {
+ conf_set_control_key (i, properties.keys[i]);
+ }
+
+ conf_set_theme (properties.themename);
+
+ var config = game_configs.get_name ((uint)properties.selected_config);
+ conf_set_configuration (config);
+
+ conf_set_use_safe_moves (properties.safe_moves);
+ conf_set_use_super_safe_moves (properties.super_safe_moves);
+ conf_set_enable_sound (properties.sound);
+}
+
+
+/**
+ * properties_safe_moves
+ *
+ * Description:
+ * returns safe-moves setting
+ *
+ * Returns:
+ * TRUE if safe-moves are selected
+ **/
+public bool properties_safe_moves () {
+ return properties.safe_moves;
+}
+
+
+/**
+ * properties_super_safe_moves
+ *
+ * Description:
+ * returns super-safe-moves setting
+ *
+ * Returns:
+ * TRUE if safe-moves are selected
+ **/
+public bool properties_super_safe_moves () {
+ return properties.super_safe_moves;
+}
+
+
+/**
+ * properties_sound
+ *
+ * Description:
+ * returns sound setting
+ *
+ * Returns:
+ * TRUE if sound is selected
+ **/
+public bool properties_sound () {
+ return properties.sound;
+}
+
+
+/**
+ * properties_show_toolbar
+ *
+ * Description:
+ * returns toolbar setting
+ *
+ * Returns:
+ * TRUE if splats are selected
+ **/
+public bool properties_show_toolbar () {
+ return properties.show_toolbar;
+}
+
+/**
+ * properties_set_config
+ * @n: config number
+ *
+ * Description:
+ * sets the current configuration
+ *
+ * Returns:
+ * TRUE if successful, FALSE otherwise
+ **/
+public bool properties_set_config (int n) {
+ if (n >= game_configs.count ())
+ return false;
+
+ game_configs.set_current_index ((uint)n);
+ properties.selected_config = n;
+
+ return true;
+}
+
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]