[gnome-robots] Rewrite GamesControls in Vala
- From: Andrey Kutejko <akutejko src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-robots] Rewrite GamesControls in Vala
- Date: Tue, 6 Oct 2020 19:31:40 +0000 (UTC)
commit 0831098cbcb4ae2dae5e364549c97ee5d8ebb7a7
Author: Andrey Kutejko <andy128k gmail com>
Date: Sun Aug 23 23:39:19 2020 +0200
Rewrite GamesControls in Vala
src/controls.vala | 157 ++++++++++++++++++++++++
src/games-controls.c | 340 ---------------------------------------------------
src/games-controls.h | 43 -------
src/meson.build | 2 +-
src/properties.c | 23 ++--
5 files changed, 168 insertions(+), 397 deletions(-)
---
diff --git a/src/controls.vala b/src/controls.vala
new file mode 100644
index 0000000..afb8888
--- /dev/null
+++ b/src/controls.vala
@@ -0,0 +1,157 @@
+/*
+ * controls.vala: keyboard controls utility functions.
+ *
+ * Copyright © 2004 Paolo Borelli
+ * Copyright © 2007, 2009 Christian Persch
+ *
+ */
+
+using Gtk;
+
+public class GamesControlsList : ScrolledWindow {
+
+ private Gtk.ListStore store;
+ private TreeView view;
+
+ private GLib.Settings settings;
+ private ulong notify_handler_id;
+
+ private enum Column {
+ CONFKEY = 0,
+ LABEL,
+ KEYCODE,
+ KEYMODS,
+ DEFAULT_KEYCODE,
+ DEFAULT_KEYMODS,
+ COUNT
+ }
+
+ public GamesControlsList(GLib.Settings settings) {
+ store = new Gtk.ListStore (Column.COUNT,
+ Type.STRING,
+ Type.STRING,
+ Type.UINT,
+ Type.UINT,
+ Type.UINT,
+ Type.UINT);
+
+ view = new TreeView.with_model (store);
+ view.set_headers_visible (false);
+ view.set_enable_search (false);
+
+ /* label column */
+ var label_renderer = new CellRendererText ();
+ var column = new TreeViewColumn ();
+ column.title = "Control";
+ column.pack_start (label_renderer, true);
+ column.add_attribute (label_renderer, "text", Column.LABEL);
+
+ view.append_column (column);
+
+ /* key column */
+ var key_renderer = new CellRendererAccel ();
+ key_renderer.editable = true;
+ key_renderer.accel_mode = CellRendererAccelMode.OTHER;
+ key_renderer.accel_edited.connect ((_cell, path, keyval, _mask, _keycode) => {
+ accel_edited_cb (path, keyval);
+ });
+ key_renderer.accel_cleared.connect ((_cell, path) => {
+ accel_cleared_cb(path);
+ });
+
+ var column2 = new TreeViewColumn ();
+ column2.title = "Key";
+ column2.pack_start (key_renderer, true);
+ column2.add_attribute (key_renderer, "accel-key", Column.KEYCODE);
+ column2.add_attribute (key_renderer, "accel-mods", Column.KEYMODS);
+
+ view.append_column (column2);
+
+ hscrollbar_policy = PolicyType.NEVER;
+ vscrollbar_policy = PolicyType.AUTOMATIC;
+ shadow_type = ShadowType.IN;
+ add (view);
+
+ this.settings = settings;
+ notify_handler_id = settings.changed.connect ((_settings, key) => {
+ settings_changed_cb(key);
+ });
+ }
+
+ ~GamesControlsList () {
+ settings.disconnect (notify_handler_id);
+ }
+
+ public void add_control (string conf_key,
+ string label,
+ uint default_keyval) {
+ var keyval = settings.get_int (conf_key);
+
+ TreeIter iter;
+ store.append (out iter);
+ store.set_value (iter, Column.CONFKEY, conf_key);
+ store.set_value (iter, Column.LABEL, label);
+ store.set_value (iter, Column.KEYCODE, keyval);
+ store.set_value (iter, Column.KEYMODS, 0);
+ store.set_value (iter, Column.DEFAULT_KEYCODE, default_keyval);
+ store.set_value (iter, Column.DEFAULT_KEYMODS, 0);
+ }
+
+ private void accel_edited_cb (string path_string, uint keyval) {
+ var path = new TreePath.from_string (path_string);
+ if (path == null)
+ return;
+
+ TreeIter iter;
+ if (!store.get_iter (out iter, path)) {
+ return;
+ }
+
+ Value conf_key;
+ store.get_value (iter, Column.CONFKEY, out conf_key);
+
+ /* Note: the model is updated in the conf notification callback */
+ /* FIXME: what to do with the modifiers? */
+ settings.set_int (conf_key.get_string(), (int) keyval);
+ }
+
+ private void accel_cleared_cb (string path_string) {
+ var path = new TreePath.from_string (path_string);
+ if (path == null)
+ return;
+
+ TreeIter iter;
+ if (!store.get_iter (out iter, path))
+ return;
+
+ Value conf_key;
+ store.get_value (iter, Column.CONFKEY, out conf_key);
+
+ Value default_keyval;
+ store.get_value (iter, Column.DEFAULT_KEYCODE, out default_keyval);
+
+ /* Note: the model is updated in the conf notification callback */
+ /* FIXME: what to do with the modifiers? */
+ settings.set_int (conf_key.get_string(), (int) default_keyval.get_uint());
+ }
+
+ private void settings_changed_cb (string key) {
+ var keyval = settings.get_int (key);
+
+ /* find our gconf key in the list store and update it */
+ TreeIter iter;
+ if (store.get_iter_first (out iter)) {
+ do {
+ Value conf_key;
+ store.get_value (iter, Column.CONFKEY, out conf_key);
+
+ if (key == conf_key.get_string()) {
+ store.set_value (iter, Column.KEYCODE, keyval);
+ store.set_value (iter, Column.KEYMODS, 0); // FIXME?
+ break;
+ }
+ } while (store.iter_next(ref iter));
+ }
+ }
+}
+
diff --git a/src/meson.build b/src/meson.build
index 5705dc7..085e9a1 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -3,6 +3,7 @@ vala_sources = files(
'file-list.vala',
'find-file.vala',
'preimage.vala',
+ 'controls.vala',
)
vala_lib = static_library('riiv',
@@ -27,7 +28,6 @@ sources = files(
'cursors.c',
'game.c',
'gameconfig.c',
- 'games-controls.c',
'gnome-robots.c',
'graphics.c',
'keyboard.c',
diff --git a/src/properties.c b/src/properties.c
index fc15f49..d60c4cb 100644
--- a/src/properties.c
+++ b/src/properties.c
@@ -35,7 +35,6 @@
#include "keyboard.h"
#include "game.h"
#include "riiv.h"
-#include "games-controls.h"
/**********************************************************************/
@@ -505,18 +504,16 @@ show_properties_dialog (void)
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
gtk_box_pack_start (GTK_BOX (kpage), vbox, TRUE, TRUE, 0);
- controls_list = games_controls_list_new (settings);
- games_controls_list_add_controls (GAMES_CONTROLS_LIST (controls_list),
- "key00", _("Key to move NW"), g_settings_get_default_value (settings,
"key00"),
- "key01", _("Key to move N"), g_settings_get_default_value (settings,
"key01"),
- "key02", _("Key to move NE"), g_settings_get_default_value (settings,
"key02"),
- "key03", _("Key to move W"), g_settings_get_default_value (settings,
"key03"),
- "key04", _("Key to hold"), g_settings_get_default_value (settings,
"key04"),
- "key05", _("Key to move E"), g_settings_get_default_value (settings,
"key05"),
- "key06", _("Key to move SW"), g_settings_get_default_value (settings,
"key06"),
- "key07", _("Key to move S"), g_settings_get_default_value (settings,
"key07"),
- "key08", _("Key to move SE"), g_settings_get_default_value (settings,
"key08"),
- NULL);
+ controls_list = GTK_WIDGET (games_controls_list_new (settings));
+ games_controls_list_add_control (GAMES_CONTROLS_LIST (controls_list), "key00", _("Key to move NW"),
g_variant_get_uint32 (g_settings_get_default_value (settings, "key00")));
+ games_controls_list_add_control (GAMES_CONTROLS_LIST (controls_list), "key01", _("Key to move N"),
g_variant_get_uint32 (g_settings_get_default_value (settings, "key01")));
+ games_controls_list_add_control (GAMES_CONTROLS_LIST (controls_list), "key02", _("Key to move NE"),
g_variant_get_uint32 (g_settings_get_default_value (settings, "key02")));
+ games_controls_list_add_control (GAMES_CONTROLS_LIST (controls_list), "key03", _("Key to move W"),
g_variant_get_uint32 (g_settings_get_default_value (settings, "key03")));
+ games_controls_list_add_control (GAMES_CONTROLS_LIST (controls_list), "key04", _("Key to hold"),
g_variant_get_uint32 (g_settings_get_default_value (settings, "key04")));
+ games_controls_list_add_control (GAMES_CONTROLS_LIST (controls_list), "key05", _("Key to move E"),
g_variant_get_uint32 (g_settings_get_default_value (settings, "key05")));
+ games_controls_list_add_control (GAMES_CONTROLS_LIST (controls_list), "key06", _("Key to move SW"),
g_variant_get_uint32 (g_settings_get_default_value (settings, "key06")));
+ games_controls_list_add_control (GAMES_CONTROLS_LIST (controls_list), "key07", _("Key to move S"),
g_variant_get_uint32 (g_settings_get_default_value (settings, "key07")));
+ games_controls_list_add_control (GAMES_CONTROLS_LIST (controls_list), "key08", _("Key to move SE"),
g_variant_get_uint32 (g_settings_get_default_value (settings, "key08")));
gtk_box_pack_start (GTK_BOX (vbox), controls_list, TRUE, TRUE, 0);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]