From 9e48830dc81fcc411bc6b8457edbbd91e5a7eba0 Mon Sep 17 00:00:00 2001 From: scorpion-26 <58082714+scorpion-26@users.noreply.github.com> Date: Sat, 4 Feb 2023 16:23:13 +0100 Subject: [PATCH] Abstract out quad calculation from Sensor The calculation of the bounding quad is now in CairoArea, as it is quite useful --- src/Widget.cpp | 48 ++++++++++++++++++++++++++---------------------- src/Widget.h | 7 +++++++ 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/src/Widget.cpp b/src/Widget.cpp index 667e4f4..8b4e4d3 100644 --- a/src/Widget.cpp +++ b/src/Widget.cpp @@ -229,6 +229,28 @@ void CairoArea::Create() ApplyPropertiesToWidget(); } +Quad CairoArea::GetQuad() +{ + GtkAllocation dim; + gtk_widget_get_allocation(m_Widget, &dim); + Quad q; + if (dim.height >= dim.width) + { + // Height greater than width; Fill in x and add margin at the top and bottom + q.size = dim.width; + q.x = 0; + q.y = ((double)dim.height - (double)dim.width) / 2; + } + else if (dim.width < dim.height) + { + // Height greater than width; Fill in y and add margin at the sides + q.size = dim.height; + q.y = 0; + q.x = ((double)dim.width - (double)dim.height) / 2; + } + return q; +} + void Sensor::SetValue(double val) { m_Val = val; @@ -245,29 +267,11 @@ void Sensor::SetStyle(SensorStyle style) void Sensor::Draw(cairo_t* cr) { - GtkAllocation dim; - gtk_widget_get_allocation(m_Widget, &dim); - double xStart = 0; - double yStart = 0; - double size = dim.width; - if (dim.height >= dim.width) - { - // Height greater than width; Fill in x and add margin at the top and bottom - size = dim.width; - xStart = 0; - yStart = ((double)dim.height - (double)dim.width) / 2; - } - else if (dim.width < dim.height) - { - // Height greater than width; Fill in y and add margin at the sides - size = dim.height; - yStart = 0; - xStart = ((double)dim.width - (double)dim.height) / 2; - } + Quad q = GetQuad(); - double xCenter = xStart + size / 2; - double yCenter = yStart + size / 2; - double radius = (size / 2) - (m_Style.strokeWidth / 2); + double xCenter = q.x + q.size / 2; + double yCenter = q.y + q.size / 2; + double radius = (q.size / 2) - (m_Style.strokeWidth / 2); double beg = m_Style.start * (M_PI / 180); double angle = m_Val * 2 * M_PI; diff --git a/src/Widget.h b/src/Widget.h index 271bce4..e7dec80 100644 --- a/src/Widget.h +++ b/src/Widget.h @@ -47,6 +47,11 @@ struct Transition uint32_t durationMS; }; +struct Quad +{ + double x, y, size; +}; + struct SensorStyle { double start = -90; // 0 = leftmost; -90 = topmost @@ -203,6 +208,8 @@ public: protected: virtual void Draw(cairo_t* cr) = 0; + + Quad GetQuad(); }; class Sensor : public CairoArea