Newer
Older
* SPDX-FileCopyrightText: 2018-2021 Bruce Cowan <bruce@bcowan.me.uk>
*
* SPDX-License-Identifier: GPL-3.0-or-later
#include "rugby-possibility-widget.h"
struct _RugbyPossibilityWidget
{
GtkDrawingArea parent_instance;
RugbyPossibility *possibility;
};
G_DEFINE_TYPE (RugbyPossibilityWidget, rugby_possibility_widget, GTK_TYPE_DRAWING_AREA)
enum
{
PROP_0,
PROP_POSSIBILITY,
N_PROPS
};
static GParamSpec *properties[N_PROPS];
static void
rugby_possibility_widget_dispose (GObject *object)
{
RugbyPossibilityWidget *self = RUGBY_POSSIBILITY_WIDGET (object);
g_clear_object (&self->possibility);
G_OBJECT_CLASS (rugby_possibility_widget_parent_class)->dispose (object);
}
static void
rugby_possibility_widget_get_property (GObject *object,
GValue *value,
GParamSpec *pspec)
{
RugbyPossibilityWidget *self = RUGBY_POSSIBILITY_WIDGET (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_widget_set_property (GObject *object,
const GValue *value,
GParamSpec *pspec)
{
RugbyPossibilityWidget *self = RUGBY_POSSIBILITY_WIDGET (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
render_bar (cairo_t *cr,
GtkStyleContext *context,
double x,
double y,
double w,
double h,
const char *style)
{
gtk_style_context_save (context);
gtk_style_context_add_class (context, style);
gtk_render_background (context, cr, x, y, w, h);
gtk_style_context_restore (context);
}
static void
draw_func (GtkDrawingArea *drawing_area,
cairo_t *cr,
int width,
int height,
G_GNUC_UNUSED gpointer user_data)
RugbyPossibilityWidget *self = RUGBY_POSSIBILITY_WIDGET (drawing_area);
GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET (drawing_area));
gtk_widget_add_css_class (GTK_WIDGET (drawing_area), "possibility");
double x = 0.0;
gtk_render_background (context, cr, x, 0.0, width, height);
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");
g_object_get (self->possibility,
"tries", &tries,
"utries", &utries,
"kicks", &kicks,
NULL);
int score = tries * try_points + utries * utry_points + kicks * kick_points;
gtk_widget_add_css_class (GTK_WIDGET (drawing_area), "score-block");
for (int i = 0; i < tries; i++)
{
x += w;
}
// Unconverted tries
for (int i = 0; i < utries; i++)
{
render_bar (cr, context, x, 0.0, w, height, "utry");
x += w;
}
// Unconverted kicks
for (int i = 0; i < kicks; i++)
{
render_bar (cr, context, x, 0.0, w, height, "kick");
x += w;
}
}
static void
rugby_possibility_widget_constructed (GObject *obj)
{
RugbyPossibilityWidget *self = RUGBY_POSSIBILITY_WIDGET (obj);
g_autoptr (GString) tooltip = g_string_new (NULL);
g_object_get (self->possibility,
"tries", &tries,
"utries", &utries,
"kicks", &kicks,
NULL);
if ((tries + utries) > 0)
{
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);
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
}
gtk_widget_set_tooltip_text (GTK_WIDGET (self), tooltip->str);
G_OBJECT_CLASS (rugby_possibility_widget_parent_class)->constructed (obj);
}
static void
rugby_possibility_widget_class_init (RugbyPossibilityWidgetClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->constructed = rugby_possibility_widget_constructed;
object_class->dispose = rugby_possibility_widget_dispose;
object_class->get_property = rugby_possibility_widget_get_property;
object_class->set_property = rugby_possibility_widget_set_property;
properties[PROP_POSSIBILITY] = g_param_spec_object ("possibility",
"Possibility",
"Possibility to be represented",
RUGBY_TYPE_POSSIBILITY,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
g_object_class_install_properties (object_class,
N_PROPS,
properties);
}
static void
rugby_possibility_widget_init (RugbyPossibilityWidget *self)
gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (self),
draw_func, NULL, NULL);
self->settings = g_settings_new ("uk.me.bcowan.Rugby");