diff --git a/src/gui/bubble-circle.vala b/src/gui/bubble-circle.vala index 039916a..4646bd7 100644 --- a/src/gui/bubble-circle.vala +++ b/src/gui/bubble-circle.vala @@ -1,43 +1,60 @@ -class TheCatTools.GUI.BoubleCircle : DrawingAreaOrientable { - public double value_x { get; set; default = 0; } - public double value_y { get; set; default = 0; } +class TheCatTools.GUI.BoubleCircle : Adw.Bin, Gtk.Orientable { + public int requested_size { get; set; default = 60; } public Gtk.Adjustment adjustment { get; set; default = new Gtk.Adjustment (0, -90, 90, 0, 0, 0); } + public Gtk.Orientation orientation { get; set; } + public Bubble bubble = new Bubble (); + public Circle clamps_ok = new Circle (); + public Gtk.Overlay overlay = new Gtk.Overlay (); construct { + width_request = 60; + add_css_class ("bubble-bar"); notify["value"].connect (queue_draw); - add_css_class ("bubble-circle"); + notify["orientation"].connect (orientation_changed); orientation_changed (); - vexpand = true; + bubble.vexpand = true; + bubble.hexpand = true; + overlay.add_overlay (clamps_ok); + overlay.add_overlay (bubble); + set_child (overlay); + bind_property ("orientation", clamps_ok, "orientation", GLib.BindingFlags.SYNC_CREATE); + bind_property ("orientation", bubble, "orientation", GLib.BindingFlags.SYNC_CREATE); + clamps_ok.bind_state_to_bubble_pos (bubble); } - void set_color_rgba (Cairo.Context cr, Gdk.RGBA color) { - cr.set_source_rgba (color.red, color.green, color.blue, color.alpha); + public void orientation_changed () { + var orientation = orientation == Gtk.Orientation.HORIZONTAL; + width_request = -1; + width_request = orientation ? -1 : requested_size; + height_request = orientation ? requested_size : -1; } - double get_adjusted_value () { - var res = value * -1; - if (res > 90) - res = 180 - res; - if (res < -90) - res = -180 - res; - return res.clamp (adjustment.lower, adjustment.upper); + public class Circle : DrawingAreaOrientable { + public Gtk.Adjustment adjustment { get; set; default = new Gtk.Adjustment (0, -90, 90, 0, 0, 0); } + public Gtk.Adjustment clamps { get; set; default = new Gtk.Adjustment (0, -10, 10, 0, 0, 0); } + construct { + add_css_class ("circle"); + } + public void bind_state_to_bubble_pos (Bubble bubble) { + bubble.notify["value-x"].connect (() => { + bool inb = true; + if (bubble.value_x >= clamps.upper) { + set_state_flags (Gtk.StateFlags.CHECKED, false); + inb = false; + } else if (bubble.value_x <= clamps.lower) { + set_state_flags (Gtk.StateFlags.CHECKED, false); + inb = false; + } else + unset_state_flags (Gtk.StateFlags.CHECKED); + }); + } + + public override void draw_func (Gtk.DrawingArea self, Cairo.Context cr, int width, int height) { + var range = adjustment.upper - adjustment.lower; + Utils.set_color_rgba (cr, get_color ()); + var radius = width / range * (clamps.upper - clamps.lower); + cr.arc (width / 2, height / 2, radius, 0, Math.PI * 2); + cr.stroke (); + } } - - public override void draw_func (Gtk.DrawingArea self, Cairo.Context cr, int width, int height) { - var gap = 2; - var radius = width / 2 - gap / 2; - var val = get_adjusted_value (); - var position = radius + (height - 2 * radius) * (val - adjustment.lower) / (adjustment.upper - adjustment.lower); - - set_color_rgba (cr, get_color ()); - cr.arc (radius, position, radius, 0, Math.PI * 2); - cr.fill (); - - cr.move_to (0, 0); - cr.line_to (width, height); - cr.set_line_width (3); - cr.stroke (); - cr.move_to (10, 10); - cr.show_text ("Meow"); - } -} +} \ No newline at end of file diff --git a/src/gui/meson.build b/src/gui/meson.build index 918526b..3caf80b 100644 --- a/src/gui/meson.build +++ b/src/gui/meson.build @@ -3,4 +3,6 @@ gui_sources = [ 'drawing-area-orientable.vala', 'bubble.vala', 'bubble-bar.vala', + 'bubble-circle.vala', + ] diff --git a/src/window.blp b/src/window.blp index 57592f7..6ed95af 100644 --- a/src/window.blp +++ b/src/window.blp @@ -27,14 +27,14 @@ template $TheCatToolsWindow: Adw.ApplicationWindow { margin-end: 12; orientation: horizontal; column-spacing: 24; + row-spacing: 24; - [start] $TheCatToolsGUIBoubleBar hbar { orientation: horizontal; layout { column: 2; - row: 1; + row: 2; } halign: fill; @@ -43,7 +43,6 @@ template $TheCatToolsWindow: Adw.ApplicationWindow { vexpand: false; } - [start] $TheCatToolsGUIBoubleBar vbar { orientation: vertical; vexpand: true; @@ -53,6 +52,16 @@ template $TheCatToolsWindow: Adw.ApplicationWindow { row: 1; } } + + $TheCatToolsGUIBoubleCircle circle { + orientation: vertical; + vexpand: true; + + layout { + column: 2; + row: 1; + } + } } } }; diff --git a/src/window.vala b/src/window.vala index f4f7f89..a32d224 100644 --- a/src/window.vala +++ b/src/window.vala @@ -26,6 +26,8 @@ public class TheCatTools.Window : Adw.ApplicationWindow { private unowned GUI.BoubleBar hbar; [GtkChild] private unowned GUI.BoubleBar vbar; + [GtkChild] + private unowned GUI.BoubleCircle circle; private SSC.SensorAccelerometer accel; async void start_sensors() { @@ -43,6 +45,8 @@ public class TheCatTools.Window : Adw.ApplicationWindow { message("%f\t%f", x_angle, y_angle); hbar.bubble.value_x = (float) x_angle; vbar.bubble.value_x = (float) y_angle; + circle.bubble.value_x = (float) x_angle; + circle.bubble.value_y = (float) y_angle; }); } catch (Error err) { message("Failed to open Sensor %s", err.message);