[gnome-games/wip/aplazas/konami-code: 1/2] ui: Add KonamiCode
- From: Adrien Plazas <aplazas src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-games/wip/aplazas/konami-code: 1/2] ui: Add KonamiCode
- Date: Thu, 11 May 2017 18:07:32 +0000 (UTC)
commit c0c2e6bcfbb9eb62a19a0652ceb211ae3501fbdf
Author: Adrien Plazas <kekun plazas laposte net>
Date: Sat Jul 23 08:35:56 2016 +0200
ui: Add KonamiCode
this will be used in next commit to monitor the Konami Code being
performed on a keyboard.
src/Makefile.am | 1 +
src/ui/konami-code.vala | 65 +++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 66 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index bf6f067..a9fcb6e 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -122,6 +122,7 @@ gnome_games_SOURCES = \
ui/error-info-bar.vala \
ui/game-icon-view.vala \
ui/game-thumbnail.vala \
+ ui/konami-code.vala \
ui/media-selector.vala \
ui/media-menu-button.vala \
ui/preferences-page.vala \
diff --git a/src/ui/konami-code.vala b/src/ui/konami-code.vala
new file mode 100644
index 0000000..ba0b668
--- /dev/null
+++ b/src/ui/konami-code.vala
@@ -0,0 +1,65 @@
+// This file is part of GNOME Games. License: GPLv3
+
+private class Games.KonamiCode : Object {
+ public const string CODE_STRING = "↑ ↑ ↓ ↓ ← → ← → B A";
+
+ private const int[] CODE_LOWER_KEYS = {
+ Gdk.Key.Up,
+ Gdk.Key.Up,
+ Gdk.Key.Down,
+ Gdk.Key.Down,
+ Gdk.Key.Left,
+ Gdk.Key.Right,
+ Gdk.Key.Left,
+ Gdk.Key.Right,
+ Gdk.Key.b,
+ Gdk.Key.a,
+ };
+
+ private const int[] CODE_UPPER_KEYS = {
+ Gdk.Key.Up,
+ Gdk.Key.Up,
+ Gdk.Key.Down,
+ Gdk.Key.Down,
+ Gdk.Key.Left,
+ Gdk.Key.Right,
+ Gdk.Key.Left,
+ Gdk.Key.Right,
+ Gdk.Key.B,
+ Gdk.Key.A
+ };
+
+ private const int LAST_INDEX = 9;
+
+ public signal void code_performed ();
+
+ private uint current_index;
+
+ public KonamiCode (Gtk.Widget widget) {
+ widget.key_press_event.connect (on_key_pressed);
+ }
+
+ public void reset () {
+ current_index = 0;
+ }
+
+ private bool on_key_pressed (Gdk.EventKey event) {
+ if (event.keyval != CODE_LOWER_KEYS[current_index] &&
+ event.keyval != CODE_UPPER_KEYS[current_index]) {
+ current_index = 0;
+
+ return false;
+ }
+
+ if (current_index == LAST_INDEX) {
+ current_index = 0;
+ code_performed ();
+
+ return false;
+ }
+
+ current_index++;
+
+ return false;
+ }
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]