Fix sensors

Signed-off-by: Vasiliy Doylov <nekodevelopper@gmail.com>
This commit is contained in:
Vasiliy Doylov 2025-03-02 14:57:46 +03:00
parent 996a63bdaf
commit 23740b77da
Signed by: NekoCWD
GPG key ID: B7BE22D44474A582
7 changed files with 120 additions and 53 deletions

View file

@ -1,63 +1,27 @@
class TheCatTools.GUI.BoubleBar : Gtk.DrawingArea, Gtk.Orientable { class TheCatTools.GUI.BoubleBar : Adw.Bin, Gtk.Orientable {
public double value { get; set; default = 0; } 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.Adjustment adjustment { get; set; default = new Gtk.Adjustment (0, -90, 90, 0, 0, 0); }
public Gtk.Orientation orientation { get; set; } public Gtk.Orientation orientation { get; set; }
public Bubble bubble = new Bubble ();
public Gtk.Overlay overlay = new Gtk.Overlay ();
construct { construct {
set_draw_func (draw); width_request = 60;
notify["value"].connect (queue_draw); notify["value"].connect (queue_draw);
add_css_class ("bubble-bar"); add_css_class ("bubble-bar");
vexpand = true;
bubble.vexpand = true;
bubble.hexpand = true;
overlay.add_overlay (bubble);
set_child (overlay);
notify["orientation"].connect (orientation_changed); notify["orientation"].connect (orientation_changed);
orientation_changed (); orientation_changed ();
vexpand = true;
} }
void orientation_changed () { public void orientation_changed () {
message ("Request orientation");
var orientation = orientation == Gtk.Orientation.HORIZONTAL; var orientation = orientation == Gtk.Orientation.HORIZONTAL;
width_request = -1; width_request = -1;
width_request = orientation ? -1 : 60; width_request = orientation ? -1 : requested_size;
height_request = orientation ? 60 : -1; height_request = orientation ? requested_size : -1;
}
void set_color_rgba (Cairo.Context cr, Gdk.RGBA color) {
cr.set_source_rgba (color.red, color.green, color.blue, color.alpha);
}
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);
}
void draw_meow (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");
}
void draw (Gtk.DrawingArea self, Cairo.Context cr, int width, int height) {
message ("Draw %dx%d", width, height);
if (orientation == Gtk.Orientation.HORIZONTAL) {
cr.rotate (Math.PI / 2);
cr.translate (0, -width);
draw_meow (cr, height, width);
} else {
draw_meow (cr, width, height);
}
} }
} }

View file

@ -0,0 +1,43 @@
class TheCatTools.GUI.BoubleCircle : DrawingAreaOrientable {
public double value_x { get; set; default = 0; }
public double value_y { get; set; default = 0; }
public Gtk.Adjustment adjustment { get; set; default = new Gtk.Adjustment (0, -90, 90, 0, 0, 0); }
construct {
notify["value"].connect (queue_draw);
add_css_class ("bubble-circle");
orientation_changed ();
vexpand = true;
}
void set_color_rgba (Cairo.Context cr, Gdk.RGBA color) {
cr.set_source_rgba (color.red, color.green, color.blue, color.alpha);
}
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 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");
}
}

33
src/gui/bubble.vala Normal file
View file

@ -0,0 +1,33 @@
class TheCatTools.GUI.Bubble : DrawingAreaOrientable {
public double value_x { get; set; default = 0; }
public double value_y { get; set; default = 0; }
public Gtk.Adjustment adjustment { get; set; default = new Gtk.Adjustment (0, -90, 90, 0, 0, 0); }
construct {
add_css_class ("bubble");
this.notify["value-x"].connect (queue_draw);
this.notify["value-y"].connect (queue_draw);
}
double clamped (double 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 override void draw_func (Gtk.DrawingArea self, Cairo.Context cr, int width, int height) {
message ("Draw %d %d", width, height);
var radius = int.min (width, height) / 2;
var val_x = clamped (value_x);
var val_y = clamped (value_y);
var range = adjustment.upper - adjustment.lower;
var position_y = radius + (height - 2 * radius) * (val_y - adjustment.lower) / range;
var position_x = radius + (width - 2 * radius) * (val_x - adjustment.lower) / range;
Utils.set_color_rgba (cr, get_color ());
cr.arc (position_x, position_y, radius, 0, Math.PI * 2);
cr.fill ();
}
}

View file

@ -0,0 +1,20 @@
abstract class TheCatTools.GUI.DrawingAreaOrientable : Gtk.DrawingArea, Gtk.Orientable {
public Gtk.Orientation orientation { get; set; }
construct {
set_draw_func (_sdraw_func);
}
private void _sdraw_func (Gtk.DrawingArea self, Cairo.Context cr, int width, int height) {
if (orientation == Gtk.Orientation.HORIZONTAL) {
cr.rotate (Math.PI / 2);
cr.translate (0, -width);
draw_func (self, cr, height, width);
} else {
draw_func (self, cr, width, height);
}
}
public abstract void draw_func (Gtk.DrawingArea self, Cairo.Context cr, int width, int height);
}

5
src/gui/helper.vala Normal file
View file

@ -0,0 +1,5 @@
namespace TheCatTools.GUI.Utils {
void set_color_rgba (Cairo.Context cr, Gdk.RGBA color) {
cr.set_source_rgba (color.red, color.green, color.blue, color.alpha);
}
}

View file

@ -1,4 +1,6 @@
gui_sources = [ gui_sources = [
'helper.vala',
'drawing-area-orientable.vala',
'bubble.vala',
'bubble-bar.vala', 'bubble-bar.vala',
'bubble-circle.vala',
] ]

View file

@ -41,8 +41,8 @@ public class TheCatTools.Window : Adw.ApplicationWindow {
message("%f\t%f\t%f", x, y, z); message("%f\t%f\t%f", x, y, z);
message("%f\t%f", x_angle, y_angle); message("%f\t%f", x_angle, y_angle);
hbar.value = (float) x_angle; hbar.bubble.value_x = (float) x_angle;
vbar.value = (float) y_angle; vbar.bubble.value_y = (float) y_angle;
}); });
} catch (Error err) { } catch (Error err) {
message("Failed to open Sensor %s", err.message); message("Failed to open Sensor %s", err.message);