X-Git-Url: http://vcs.maemo.org/git/?a=blobdiff_plain;f=src%2Frating-widget.vala;h=d432b485508fa3ff269e5ad09f35228e29a47a18;hb=d9edb26f6cf6d89dd02c7eea7d6a2f6704b24264;hp=70b27f4669bf9d0df76d2467558f05831f25405f;hpb=45ff969eb86dd5d4b2c9644e7caa99f27c79e051;p=cinaest diff --git a/src/rating-widget.vala b/src/rating-widget.vala index 70b27f4..d432b48 100644 --- a/src/rating-widget.vala +++ b/src/rating-widget.vala @@ -40,19 +40,28 @@ public class RatingWidget : DrawingArea { public override bool expose_event (Gdk.EventExpose event) { var cr = Gdk.cairo_create (this.window); + var radius = double.min (this.allocation.width / 20, + this.allocation.height / 2); cr.rectangle (event.area.x, event.area.y, event.area.width, event.area.height); cr.clip (); - for (int i = 0; i < 10; i++) { - draw_star (cr, i, (i >= rating) ? disabled_color : active_color); - } + cr.save (); + cr.rectangle (radius * rating / 5.0, 0, this.allocation.width - radius * rating / 5.0, this.allocation.height); + cr.clip (); + for (int i = rating/10; i < 10; i++) + draw_star (cr, i, radius, disabled_color); + cr.restore (); + + cr.new_path (); + cr.rectangle (0, 0, radius * rating / 5.0, this.allocation.height); + cr.clip (); + for (int i = 0; i < (rating+5)/10; i++) + draw_star (cr, i, radius, active_color); return false; } - private void draw_star (Cairo.Context cr, int n, Gdk.Color color) { - var radius = double.min (this.allocation.width / 20, - this.allocation.height / 2); + private void draw_star (Cairo.Context cr, int n, double radius, Gdk.Color color) { var x = radius * (2 * n + 1); var y = this.allocation.height / 2; @@ -73,7 +82,7 @@ public class RatingWidget : DrawingArea { public override bool button_press_event (Gdk.EventButton event) { button_pressed = true; - rate (10 * event.x / this.allocation.width); + rate (event.x); return false; } @@ -86,18 +95,24 @@ public class RatingWidget : DrawingArea { public override bool motion_notify_event (Gdk.EventMotion event) { if (button_pressed) - rate (10 * event.x / this.allocation.width); + rate (event.x); return false; } - private void rate (double r) { - if (r <= 0) + private void rate (double x) { + double r = 10 * x / this.allocation.width; + if (r <= 0) { rating = 0; - else - rating = (int) r + 1; - if (rating > 10) - rating = 10; + } else { + int star = (int) r; + rating = 10 * (star + 1); + r -= star; + if (r <= 0.333) + rating -= 5; + if (rating > 100) + rating = 100; + } unowned Gdk.Region region = this.window.get_clip_region (); // redraw the cairo canvas completely by exposing it