|
| 1 | +/* |
| 2 | +* Copyright (c) 2017 Felipe Escoto (https://github.com/Philip-Scott/Spice-up) |
| 3 | +* |
| 4 | +* This program is free software; you can redistribute it and/or |
| 5 | +* modify it under the terms of the GNU General Public |
| 6 | +* License as published by the Free Software Foundation; either |
| 7 | +* version 2 of the License, or (at your option) any later version. |
| 8 | +* |
| 9 | +* This program is distributed in the hope that it will be useful, |
| 10 | +* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | +* General Public License for more details. |
| 13 | +* |
| 14 | +* You should have received a copy of the GNU General Public |
| 15 | +* License along with this program; if not, write to the |
| 16 | +* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 17 | +* Boston, MA 02111-1307, USA. |
| 18 | +* |
| 19 | +* Authored by: Felipe Escoto <felescoto95@hotmail.com> |
| 20 | +*/ |
| 21 | + |
| 22 | +public class Spice.GamepadSlideController : Object { |
| 23 | + private static GamepadSlideController? instance = null; |
| 24 | + private unowned SlideManager slide_manager; |
| 25 | + |
| 26 | + private LibGamepad.GamepadMonitor gamepad_monitor; |
| 27 | + private LibGamepad.Gamepad[] gamepads = {}; |
| 28 | + |
| 29 | + public static void startup (SlideManager _slide_manager) { |
| 30 | + if (instance == null) { |
| 31 | + instance = new GamepadSlideController (); |
| 32 | + } |
| 33 | + |
| 34 | + instance.slide_manager = _slide_manager; |
| 35 | + } |
| 36 | + |
| 37 | + private GamepadSlideController () { |
| 38 | + gamepad_monitor = new LibGamepad.GamepadMonitor (); |
| 39 | + |
| 40 | + // On plugin, connect signals to the gamepad and store it in the gamepads array so it is not deleted by reference counting |
| 41 | + gamepad_monitor.gamepad_plugged.connect ((gamepad) => { |
| 42 | + print (@"GM Plugged in $(gamepad.raw_gamepad.identifier)- $(gamepad.raw_name)\n"); |
| 43 | + gamepads += gamepad; |
| 44 | + // Bind events |
| 45 | + gamepad.button_event.connect (button_event); |
| 46 | + //gamepad.axis_event.connect ((axis, value) => print (@"$(gamepad.raw_name) - $(axis.to_string ()) - $value\n")); |
| 47 | + gamepad.unplugged.connect (() => print (@"$(gamepad.raw_name) - G Unplugged\n")); |
| 48 | + }); |
| 49 | + |
| 50 | + // Initialize initially plugged in gamepads |
| 51 | + gamepad_monitor.foreach_gamepad ((gamepad) => { |
| 52 | + print (@"GM Initially Plugged in $(gamepad.raw_gamepad.identifier) - $(gamepad.guid) - $(gamepad.raw_name)\n"); |
| 53 | + gamepads += gamepad; |
| 54 | + // Bind events |
| 55 | + gamepad.button_event.connect (button_event); |
| 56 | + //gamepad.axis_event.connect ((axis, value) => print (@"$(gamepad.name) - $(axis.to_string ()) - $value\n")); |
| 57 | + gamepad.unplugged.connect (() => print (@"$(gamepad.raw_name) - G Unplugged\n")); |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + private void button_event (int button) { |
| 62 | + debug ("Gamepad Button event: %d\n", button); |
| 63 | + |
| 64 | + switch (button) { |
| 65 | + case 0: |
| 66 | + next_slide (); |
| 67 | + break; |
| 68 | + case 2: |
| 69 | + previous_slide (); |
| 70 | + break; |
| 71 | + case 12: |
| 72 | + toggle_present (); |
| 73 | + break; |
| 74 | + case 1: |
| 75 | + jump_to_checkpoint (); |
| 76 | + break; |
| 77 | + case 3: |
| 78 | + set_checkpoint (); |
| 79 | + break; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private void jump_to_checkpoint () { |
| 84 | + this.slide_manager.jump_to_checkpoint (); |
| 85 | + } |
| 86 | + |
| 87 | + private void set_checkpoint () { |
| 88 | + this.slide_manager.set_checkpoint (); |
| 89 | + } |
| 90 | + |
| 91 | + private void next_slide () { |
| 92 | + if (window.is_fullscreen) { |
| 93 | + this.slide_manager.next_slide (); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private void previous_slide () { |
| 98 | + if (window.is_fullscreen) { |
| 99 | + this.slide_manager.previous_slide (); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private void toggle_present () { |
| 104 | + if (window.is_fullscreen) { |
| 105 | + window.unfullscreen (); |
| 106 | + } else { |
| 107 | + window.fullscreen (); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments