[gnome-games/wip/abhinavsingh/gamepad-config: 9/23] ui: Add GamepadView



commit ba3d156f22ae08ed22f2be4076c29ea4672535c0
Author: theawless <theawless gmail com>
Date:   Mon Jul 10 21:55:28 2017 +0530

    ui: Add GamepadView
    
    This adds the ability to highlight gamepad svg's individual inputs.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=780754

 src/Makefile.am          |    1 +
 src/ui/gamepad-view.vala |  102 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 103 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index abcac75..aa3ac4f 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -125,6 +125,7 @@ gnome_games_SOURCES = \
        ui/empty-collection.vala \
        ui/error-display.vala \
        ui/error-info-bar.vala \
+       ui/gamepad-view.vala \
        ui/game-icon-view.vala \
        ui/game-thumbnail.vala \
        ui/media-selector.vala \
diff --git a/src/ui/gamepad-view.vala b/src/ui/gamepad-view.vala
new file mode 100644
index 0000000..68f5cba
--- /dev/null
+++ b/src/ui/gamepad-view.vala
@@ -0,0 +1,102 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+private class Games.GamepadView : Gtk.DrawingArea {
+       private const string STANDARD_GAMEPAD_SVG_PATH = 
"resource:///org/gnome/Games/gamepads/standard-gamepad.svg";
+       private const Gtk.StateFlags DEFAULT_STATE = Gtk.StateFlags.NORMAL;
+       private const Gtk.StateFlags HIGHLIGHT_STATE = Gtk.StateFlags.LINK;
+
+       private Rsvg.Handle handle;
+       private string[] highlight_paths = { "a", "b", "x", "y",
+                                            "leftstick", "rightstick",
+                                            "leftshoulder", "rightshoulder", "lefttrigger", "righttrigger",
+                                            "dpup", "dpleft", "dpdown", "dpright",
+                                            "back", "guide", "start",
+                                            "leftx", "rightx", "lefty", "righty" };
+       private GamepadInput[] inputs;
+       private int current_input_index;
+
+       construct {
+               try {
+                       handle = new Rsvg.Handle.from_file (STANDARD_GAMEPAD_SVG_PATH);
+                       set_size_request (handle.width, handle.height);
+               }
+               catch (Error e) {
+                       warning (e.message);
+               }
+
+               inputs = STANDARD_GAMEPAD_INPUTS;
+               current_input_index = -1;
+       }
+
+       public GamepadInput? highlight_next () {
+               current_input_index += 1;
+               queue_draw ();
+
+               if (current_input_index >= inputs.length)
+                       return null;
+
+               return inputs[current_input_index];
+       }
+
+       public override bool draw (Cairo.Context context) {
+               double x, y, scale;
+               calculate_image_dimensions (out x, out y, out scale);
+
+               color_gamepad (context, x, y, scale);
+               if (!(0 <= current_input_index && current_input_index < inputs.length))
+                       return false;
+
+               var highlight_path = "#" + highlight_paths[current_input_index];
+               highlight_gamepad (context, highlight_path, x, y, scale);
+
+               return false;
+       }
+
+       private void color_gamepad (Cairo.Context gamepad_context, double x, double y, double scale) {
+               var color_context = create_similar_context (gamepad_context);
+               var color_suface = color_context.get_target ();
+               color_context.translate (x, y);
+               color_context.scale (scale, scale);
+               handle.render_cairo (color_context);
+
+               var color = get_style_context ().get_color (DEFAULT_STATE);
+               gamepad_context.set_source_rgba (color.red, color.green, color.blue, color.alpha);
+               gamepad_context.mask_surface (color_suface, 0, 0);
+       }
+
+       private void highlight_gamepad (Cairo.Context gamepad_context, string highlight_path, double x, 
double y, double scale) {
+               var highlight_context = create_similar_context (gamepad_context);
+               var highlight_suface = highlight_context.get_target ();
+               highlight_context.translate (x, y);
+               highlight_context.scale (scale, scale);
+               handle.render_cairo_sub (highlight_context, highlight_path);
+
+               var color = get_style_context ().get_color (HIGHLIGHT_STATE);
+               gamepad_context.set_source_rgba (color.red, color.green, color.blue, color.alpha);
+               gamepad_context.mask_surface (highlight_suface, 0, 0);
+       }
+
+       private Cairo.Context create_similar_context (Cairo.Context context) {
+               var w = get_allocated_width ();
+               var h = get_allocated_height ();
+               var surface = context.get_target ();
+               var similar_suface = new Cairo.Surface.similar (surface, Cairo.Content.COLOR_ALPHA, w, h);
+               return new Cairo.Context (similar_suface);
+       }
+
+       private void calculate_image_dimensions (out double x, out double y, out double scale) {
+               var w = get_allocated_width ();
+               var h = get_allocated_height ();
+               var allocation_ratio = w / h;
+               var image_ratio = handle.width / handle.height;
+
+               if (allocation_ratio > image_ratio) {
+                       scale = h / handle.height;
+               }
+               else {
+                       scale = w / handle.width;
+               }
+               x = (w - handle.width * scale) / 2;
+               y = (h - handle.height * scale) / 2;
+       }
+}


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