[gnome-robots] Introduce Properties.Keys class to hold keyboard keys.
- From: Andrey Kutejko <akutejko src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-robots] Introduce Properties.Keys class to hold keyboard keys.
- Date: Tue, 6 Oct 2020 19:31:42 +0000 (UTC)
commit f771caae61dd7271e2b783764ebe000f493f7168
Author: Andrey Kutejko <andy128k gmail com>
Date: Sun Oct 4 12:28:57 2020 +0200
Introduce Properties.Keys class to hold keyboard keys.
src/controls.vala | 4 +--
src/properties-dialog.vala | 2 +-
src/properties.vala | 84 ++++++++++++++++++++++++++++------------------
src/robots.vala | 2 +-
4 files changed, 56 insertions(+), 36 deletions(-)
---
diff --git a/src/controls.vala b/src/controls.vala
index 462c4cb..b07a2aa 100644
--- a/src/controls.vala
+++ b/src/controls.vala
@@ -142,8 +142,8 @@ public class GamesControlsList : ScrolledWindow {
int key_index = key_index_value.get_int();
/* FIXME: what to do with the modifiers? */
- var keyval = properties.default_keys[key_index];
- properties.keys[key_index] = keyval;
+ properties.keys.reset (key_index);
+ var keyval = properties.keys[key_index];
store.set_value (iter, Column.KEYCODE, keyval);
}
diff --git a/src/properties-dialog.vala b/src/properties-dialog.vala
index 03d06ef..e754efa 100644
--- a/src/properties-dialog.vala
+++ b/src/properties-dialog.vala
@@ -224,7 +224,7 @@ public class PropertiesDialog : Dialog {
}
private void reset_keys () {
- properties.reset_keys ();
+ properties.keys.reset_all ();
}
public static void show_dialog (Gtk.Window parent_window,
diff --git a/src/properties.vala b/src/properties.vala
index 4724f35..6340b0d 100644
--- a/src/properties.vala
+++ b/src/properties.vala
@@ -25,9 +25,55 @@ public class Properties {
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;
+ public class Keys {
+ private GLib.Settings settings;
+
+ internal Keys (GLib.Settings settings) {
+ this.settings = settings;
+ }
+
+ public int size {
+ get { return 9; }
+ }
+
+ public uint get (uint index) {
+ if (index < size) {
+ return (uint) settings.get_int ("key%02u".printf (index));
+ } else {
+ return 0;
+ }
+ }
+
+ public void set (uint index, uint keyval) {
+ if (index < size) {
+ settings.set_int ("key%02u".printf (index), (int) keyval);
+ }
+ }
+
+ public uint get_default (uint index) {
+ if (index < size) {
+ return (uint) settings
+ .get_default_value ("key%02u".printf (index))
+ .get_int32 ();
+ } else {
+ return 0;
+ }
+ }
+
+ public void reset (uint index) {
+ if (index < size) {
+ string key = "key%02u".printf (index);
+ settings.reset (key);
+ }
+ }
+
+ public void reset_all () {
+ for (var index = 0; index < size; ++index) {
+ reset (index);
+ }
+ }
+ }
public bool safe_moves {
get {
@@ -83,30 +129,9 @@ public class Properties {
}
}
- public uint[] keys {
+ public Keys keys {
get {
- uint result[N_KEYS];
- for (int i = 0; i < N_KEYS; i++) {
- result[i] = (uint) settings.get_int ("key%02d".printf (i));
- }
- return result;
- }
- set {
- for (int i = 0; i < N_KEYS; i++) {
- settings.set_int ("key%02d".printf (i), (int) value[i]);
- }
- }
- }
-
- public uint[] default_keys {
- get {
- uint result[N_KEYS];
- for (int i = 0; i < N_KEYS; i++) {
- result[i] = (uint) settings
- .get_default_value ("key%02d".printf (i))
- .get_int32 ();
- }
- return result;
+ return _keys;
}
}
@@ -120,24 +145,19 @@ public class Properties {
}
private GLib.Settings settings;
+ private Keys _keys;
private ulong notify_handler_id;
public signal void changed ();
public Properties (GLib.Settings settings) {
this.settings = settings;
+ this._keys = new Keys (settings);
notify_handler_id = settings.changed.connect (() => changed ());
}
~Properties () {
settings.disconnect (notify_handler_id);
}
-
- public void reset_keys () {
- for (int i = 0; i < N_KEYS; ++i) {
- string key = "key%02d".printf (i);
- settings.reset (key);
- }
- }
}
diff --git a/src/robots.vala b/src/robots.vala
index 4d48795..d1527df 100644
--- a/src/robots.vala
+++ b/src/robots.vala
@@ -185,7 +185,7 @@ public class RobotsWindow : ApplicationWindow {
char pressed = ((char) keyval).toupper ();
- for (var i = 0; i < properties.keys.length; ++i) {
+ for (var i = 0; i < properties.keys.size; ++i) {
if (pressed == ((char)properties.keys[i]).toupper ()) {
game_area.player_command ((PlayerCommand)i);
return true;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]