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



commit 7dce8828df42bda4a68de1fe81c69281af6befbf
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.

 src/Makefile.am          |    1 +
 src/ui/gamepad-view.vala |   99 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 100 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 22924e3..c847a04 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -142,6 +142,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/gamepad-view-configuration.vala \
diff --git a/src/ui/gamepad-view.vala b/src/ui/gamepad-view.vala
new file mode 100644
index 0000000..1399186
--- /dev/null
+++ b/src/ui/gamepad-view.vala
@@ -0,0 +1,99 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+private class Games.GamepadView : Gtk.DrawingArea {
+       private const Gtk.StateFlags DEFAULT_STATE = Gtk.StateFlags.NORMAL;
+       private const Gtk.StateFlags HIGHLIGHT_STATE = Gtk.StateFlags.LINK;
+
+       private Rsvg.Handle handle;
+       private GamepadViewConfiguration configuration;
+       private bool[] input_highlights;
+
+       public GamepadView (GamepadViewConfiguration configuration) {
+               this.configuration = configuration;
+               input_highlights = new bool[configuration.input_paths.length];
+               try {
+                       handle = new Rsvg.Handle.from_file (configuration.svg_path);
+                       set_size_request (handle.width, handle.height);
+               }
+               catch (Error e) {
+                       warning (e.message);
+               }
+       }
+
+       public void reset () {
+               for (var i = 0; i < input_highlights.length; ++i)
+                       input_highlights[i] = false;
+
+               queue_draw ();
+       }
+
+       public void highlight (GamepadInput input, bool highlight) {
+               for (var i = 0; i < configuration.input_paths.length; ++i)
+                       if (configuration.input_paths[i].input == input)
+                               input_highlights[i] = highlight;
+
+               queue_draw ();
+       }
+
+       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);
+               highlight_gamepad (context, 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, 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);
+               
+               for (var i = 0; i < configuration.input_paths.length; ++i)
+                       if (input_highlights[i])
+                               handle.render_cairo_sub (highlight_context, "#" + 
configuration.input_paths[i].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) {
+               double w = get_allocated_width ();
+               double h = get_allocated_height ();
+               double allocation_ratio = w / h;
+               double image_ratio = (double) 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]