[gnome-games] Stop using COUNT values in retro-gtk enums



commit bdb0cdc9f26970b657bfe623bc151dac4dcf0d11
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Sat Sep 12 16:38:02 2020 +0500

    Stop using COUNT values in retro-gtk enums
    
    See https://gitlab.gnome.org/GNOME/retro-gtk/-/merge_requests/90

 src/keyboard/keyboard-mapping-builder.vala |  7 +++++--
 src/keyboard/keyboard-mapping-manager.vala | 14 ++++++++------
 src/ui/keyboard-tester.vala                |  3 ++-
 3 files changed, 15 insertions(+), 9 deletions(-)
---
diff --git a/src/keyboard/keyboard-mapping-builder.vala b/src/keyboard/keyboard-mapping-builder.vala
index f3327b13..ff286d6f 100644
--- a/src/keyboard/keyboard-mapping-builder.vala
+++ b/src/keyboard/keyboard-mapping-builder.vala
@@ -9,12 +9,15 @@ private class Games.KeyboardMappingBuilder : Object {
 
        public bool set_input_mapping (GamepadInput input, uint16 keycode) {
                var joypad_id = Retro.JoypadId.from_button_code (input.code);
-               if (joypad_id == Retro.JoypadId.COUNT)
+               int count = Retro.ControllerType.JOYPAD.get_id_count ();
+
+               if (joypad_id >= count)
                        return false;
 
-               for (Retro.JoypadId i = 0; i < Retro.JoypadId.COUNT; i += 1)
+               for (Retro.JoypadId i = 0; i < count; i += 1)
                        if (mapping.get_button_key (i) == keycode)
                                return false;
+
                mapping.set_button_key (joypad_id, keycode);
 
                return true;
diff --git a/src/keyboard/keyboard-mapping-manager.vala b/src/keyboard/keyboard-mapping-manager.vala
index c389a5b3..be6f28af 100644
--- a/src/keyboard/keyboard-mapping-manager.vala
+++ b/src/keyboard/keyboard-mapping-manager.vala
@@ -47,11 +47,12 @@ private class Games.KeyboardMappingManager : Object {
                }
 
                var enumc = (EnumClass) typeof (Retro.JoypadId).class_ref ();
-               for (int i = 0; enumc.values[i].value < Retro.JoypadId.COUNT; ++i) {
-                       var button = enumc.values[i].value_nick;
+
+               foreach (var id in enumc.values) {
+                       var button = id.value_nick;
                        try {
                                var key = mapping_key_file.get_integer (GROUP_NAME, button);
-                               mapping.set_button_key ((Retro.JoypadId) enumc.values[i].value, (uint16) key);
+                               mapping.set_button_key ((Retro.JoypadId) id.value, (uint16) key);
                        }
                        catch (Error e) {
                                critical (e.message);
@@ -71,9 +72,10 @@ private class Games.KeyboardMappingManager : Object {
 
                var mapping_key_file = new KeyFile ();
                var enumc = (EnumClass) typeof (Retro.JoypadId).class_ref ();
-               for (int i = 0; enumc.values[i].value < Retro.JoypadId.COUNT; ++i) {
-                       var button = enumc.values[i].value_nick;
-                       var key = mapping.get_button_key ((Retro.JoypadId) enumc.values[i].value);
+
+               foreach (var id in enumc.values) {
+                       var button = id.value_nick;
+                       var key = mapping.get_button_key ((Retro.JoypadId) id.value);
                        mapping_key_file.set_integer (GROUP_NAME, button, key);
                }
 
diff --git a/src/ui/keyboard-tester.vala b/src/ui/keyboard-tester.vala
index 43a8dc0c..f41b01b1 100644
--- a/src/ui/keyboard-tester.vala
+++ b/src/ui/keyboard-tester.vala
@@ -54,7 +54,8 @@ private class Games.KeyboardTester : Gtk.Bin {
        }
 
        private void update_gamepad_view (Gdk.EventKey key, bool highlight) {
-               for (Retro.JoypadId joypad_id = 0; joypad_id < Retro.JoypadId.COUNT; joypad_id += 1) {
+               int count = Retro.ControllerType.JOYPAD.get_id_count ();
+               for (Retro.JoypadId joypad_id = 0; joypad_id < count; joypad_id += 1) {
                        if (mapping.get_button_key (joypad_id) == key.hardware_keycode) {
                                var code = joypad_id.to_button_code ();
                                gamepad_view.highlight ({ EventCode.EV_KEY, code }, highlight);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]