Add angle property to widgets

This allows us to rotate various widgets.
This commit is contained in:
scorpion-26 2023-07-19 01:04:00 +02:00
parent e0255024c3
commit b95cf0d886
2 changed files with 49 additions and 0 deletions

View file

@ -470,6 +470,11 @@ void NetworkSensor::Draw(cairo_t* cr)
gtk_style_context_get(gtk_widget_get_style_context(contextUp->Get()), GTK_STATE_FLAG_NORMAL, GTK_STYLE_PROPERTY_COLOR, &colUp, NULL); gtk_style_context_get(gtk_widget_get_style_context(contextUp->Get()), GTK_STATE_FLAG_NORMAL, GTK_STYLE_PROPERTY_COLOR, &colUp, NULL);
gtk_style_context_get(gtk_widget_get_style_context(contextDown->Get()), GTK_STATE_FLAG_NORMAL, GTK_STYLE_PROPERTY_COLOR, &colDown, NULL); gtk_style_context_get(gtk_widget_get_style_context(contextDown->Get()), GTK_STATE_FLAG_NORMAL, GTK_STYLE_PROPERTY_COLOR, &colDown, NULL);
// Rotate around center of Quad
cairo_translate(cr, q.x + q.size / 2, q.y + q.size / 2);
cairo_rotate(cr, m_Angle * M_PI / 180.0);
cairo_translate(cr, -(q.x + q.size / 2), -(q.y + q.size / 2));
// Upload // Upload
cairo_set_source_rgb(cr, colUp->red, colUp->green, colUp->blue); cairo_set_source_rgb(cr, colUp->red, colUp->green, colUp->blue);
@ -563,9 +568,19 @@ void Text::SetText(const std::string& text)
m_Text = text; m_Text = text;
} }
void Text::SetAngle(double angle)
{
if (m_Widget && angle != m_Angle)
{
gtk_label_set_angle((GtkLabel*)m_Widget, angle);
}
m_Angle = angle;
}
void Text::Create() void Text::Create()
{ {
m_Widget = gtk_label_new(m_Text.c_str()); m_Widget = gtk_label_new(m_Text.c_str());
gtk_label_set_angle((GtkLabel*)m_Widget, m_Angle);
ApplyPropertiesToWidget(); ApplyPropertiesToWidget();
} }
@ -580,6 +595,15 @@ void Button::Create()
return GDK_EVENT_STOP; return GDK_EVENT_STOP;
}; };
g_signal_connect(m_Widget, "clicked", G_CALLBACK(+clickFn), this); g_signal_connect(m_Widget, "clicked", G_CALLBACK(+clickFn), this);
gtk_container_foreach((GtkContainer*)m_Widget,
[](GtkWidget* child, void* userData)
{
if (GTK_IS_LABEL(child))
{
gtk_label_set_angle((GtkLabel*)child, *(double*)userData);
}
},
&m_Angle);
ApplyPropertiesToWidget(); ApplyPropertiesToWidget();
} }
@ -592,6 +616,23 @@ void Button::SetText(const std::string& text)
m_Text = text; m_Text = text;
} }
void Button::SetAngle(double angle)
{
if (m_Widget && angle != m_Angle)
{
gtk_container_foreach((GtkContainer*)m_Widget,
[](GtkWidget* child, void* userData)
{
if (GTK_IS_LABEL(child))
{
gtk_label_set_angle((GtkLabel*)child, *(double*)userData);
}
},
&angle);
}
m_Angle = angle;
}
void Button::OnClick(Callback<Button>&& callback) void Button::OnClick(Callback<Button>&& callback)
{ {
m_OnClick = std::move(callback); m_OnClick = std::move(callback);

View file

@ -257,6 +257,8 @@ public:
void SetUp(double val); void SetUp(double val);
void SetDown(double val); void SetDown(double val);
void SetAngle(double angle) { m_Angle = angle; };
private: private:
void Draw(cairo_t* cr) override; void Draw(cairo_t* cr) override;
@ -266,6 +268,8 @@ private:
Range limitUp; Range limitUp;
Range limitDown; Range limitDown;
double m_Angle;
// What I do here is a little bit gross, but I need a working style context // What I do here is a little bit gross, but I need a working style context
// Just manually creating a style context doesn't work for me. // Just manually creating a style context doesn't work for me.
std::unique_ptr<Box> contextUp; std::unique_ptr<Box> contextUp;
@ -315,11 +319,13 @@ public:
virtual ~Text() = default; virtual ~Text() = default;
void SetText(const std::string& text); void SetText(const std::string& text);
void SetAngle(double angle);
virtual void Create() override; virtual void Create() override;
private: private:
std::string m_Text; std::string m_Text;
double m_Angle;
}; };
class Button : public Widget class Button : public Widget
@ -329,6 +335,7 @@ public:
virtual ~Button() = default; virtual ~Button() = default;
void SetText(const std::string& text); void SetText(const std::string& text);
void SetAngle(double angle);
virtual void Create() override; virtual void Create() override;
@ -336,6 +343,7 @@ public:
private: private:
std::string m_Text; std::string m_Text;
double m_Angle;
Callback<Button> m_OnClick; Callback<Button> m_OnClick;
}; };