Newer
Older
* SPDX-FileCopyrightText: 2018-2021 Bruce Cowan <bruce@bcowan.me.uk>
*
* SPDX-License-Identifier: GPL-3.0-or-later
RugbyPossibility *possibility;
};
G_DEFINE_TYPE (RugbyPossibilityRow, rugby_possibility_row, GTK_TYPE_LIST_BOX_ROW)
N_PROPS
};
static GParamSpec *properties[N_PROPS];
static void
rugby_possibility_row_dispose (GObject *object)
RugbyPossibilityRow *self = RUGBY_POSSIBILITY_ROW (object);
g_clear_object (&self->possibility);
G_OBJECT_CLASS (rugby_possibility_row_parent_class)->dispose (object);
rugby_possibility_row_get_property (GObject *object,
unsigned prop_id,
GValue *value,
GParamSpec *pspec)
RugbyPossibilityRow *self = RUGBY_POSSIBILITY_ROW (object);
switch (prop_id)
{
case PROP_POSSIBILITY:
g_value_set_object (value, self->possibility);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
rugby_possibility_row_set_property (GObject *object,
unsigned prop_id,
const GValue *value,
GParamSpec *pspec)
RugbyPossibilityRow *self = RUGBY_POSSIBILITY_ROW (object);
switch (prop_id)
{
case PROP_POSSIBILITY:
self->possibility = g_value_dup_object (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
float x,
float y,
float w,
float h,
GskRoundedRect rounded;
gsk_rounded_rect_init_from_rect (&rounded,
&GRAPHENE_RECT_INIT (x, y, w, h),
h / 2.0);
gtk_snapshot_push_rounded_clip (snapshot, &rounded);
gtk_snapshot_append_color (snapshot,
&color,
&GRAPHENE_RECT_INIT (x, y, w, h));
gtk_snapshot_append_border (snapshot,
&rounded,
(GdkRGBA[]) { black, black, black, black });
rugby_possibility_row_snapshot (GtkWidget *widget,
GtkSnapshot *snapshot)
RugbyPossibilityRow *self = RUGBY_POSSIBILITY_ROW (widget);
int try_points = g_settings_get_int (self->settings, "try-points");
int utry_points = g_settings_get_int (self->settings, "utry-points");
int kick_points = g_settings_get_int (self->settings, "kick-points");
int width = gtk_widget_get_width (widget);
int height = gtk_widget_get_height (widget);
g_object_get (self->possibility,
"tries", &tries,
"utries", &utries,
"kicks", &kicks,
NULL);
int score = tries * try_points + utries * utry_points + kicks * kick_points;
float w = width / (score / (float) try_points);
for (int i = 0; i < tries; i++)
{
render_bar (snapshot, x, 0.0, w, height,
(GdkRGBA) { 0.20, 0.82, 0.48, 1.0 });
x += w;
}
// Unconverted tries
w = width / (score / (float) utry_points);
for (int i = 0; i < utries; i++)
{
render_bar (snapshot, x, 0.0, w, height,
(GdkRGBA) { 0.88, 0.11, 0.14, 1.0 });
x += w;
}
// Unconverted kicks
w = width / (score / (float) kick_points);
for (int i = 0; i < kicks; i++)
{
render_bar (snapshot, x, 0.0, w, height,
(GdkRGBA) { 0.96, 0.83, 0.18, 1.0 });
x += w;
}
}
static void
rugby_possibility_row_constructed (GObject *obj)
RugbyPossibilityRow *self = RUGBY_POSSIBILITY_ROW (obj);
g_autoptr (GString) tooltip = g_string_new (NULL);
g_object_get (self->possibility,
"tries", &tries,
"utries", &utries,
"kicks", &kicks,
NULL);
if (tries == 1 && utries == 0)
g_string_printf (tooltip, "1 converted try");
else if (tries == 0 && utries == 1)
g_string_printf (tooltip, "1 unconverted try");
else
g_string_append_printf (tooltip,
ngettext ("%d try, %d converted",
"%d tries, %d converted",
tries + utries),
tries + utries, tries);
if (kicks > 0)
g_string_append_printf (tooltip,
ngettext (", %d kick",
", %d kicks",
kicks),
kicks);
}
else if (kicks > 0)
g_string_append_printf (tooltip,
ngettext ("%d kick",
"%d kicks", kicks),
kicks);
}
gtk_widget_set_tooltip_text (GTK_WIDGET (self), tooltip->str);
G_OBJECT_CLASS (rugby_possibility_row_parent_class)->constructed (obj);
rugby_possibility_row_class_init (RugbyPossibilityRowClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
object_class->constructed = rugby_possibility_row_constructed;
object_class->dispose = rugby_possibility_row_dispose;
object_class->get_property = rugby_possibility_row_get_property;
object_class->set_property = rugby_possibility_row_set_property;
widget_class->snapshot = rugby_possibility_row_snapshot;
g_param_spec_object ("possibility", "", "",
RUGBY_TYPE_POSSIBILITY,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
g_object_class_install_properties (object_class,
N_PROPS,
properties);
}
static void
rugby_possibility_row_init (RugbyPossibilityRow *self)
self->settings = g_settings_new ("uk.me.bcowan.Rugby");
rugby_possibility_row_new (RugbyPossibility *possibility)
return g_object_new (RUGBY_TYPE_POSSIBILITY_ROW,
"possibility", possibility,
NULL);
}