Skip to content
Snippets Groups Projects
Commit 6b545eca authored by Bruce Cowan's avatar Bruce Cowan
Browse files

Make RugbyPossibility a GObject

parent 2e15fa20
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,7 @@ sources = files( ...@@ -5,6 +5,7 @@ sources = files(
'rugby-application.c', 'rugby-application.c',
'rugby-app-window.c', 'rugby-app-window.c',
'rugby-cell-renderer-score.c', 'rugby-cell-renderer-score.c',
'rugby-possibility.c',
'rugby-score-store.c', 'rugby-score-store.c',
'rugby-scoring.c' 'rugby-scoring.c'
) )
......
/* rugby-possibility.c
*
* Copyright © 2018 Bruce Cowan <bruce@bcowan.eu>
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "rugby-possibility.h"
struct _RugbyPossibility
{
GObject parent_instance;
gint tries;
gint utries;
gint kicks;
};
G_DEFINE_TYPE (RugbyPossibility, rugby_possibility, G_TYPE_OBJECT)
enum
{
PROP_0,
PROP_TRIES,
PROP_UTRIES,
PROP_KICKS,
N_PROPS
};
static GParamSpec *properties [N_PROPS];
static void
rugby_possibility_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
RugbyPossibility *self = RUGBY_POSSIBILITY (object);
switch (prop_id)
{
case PROP_TRIES:
g_value_set_int (value, self->tries);
break;
case PROP_UTRIES:
g_value_set_int (value, self->utries);
break;
case PROP_KICKS:
g_value_set_int (value, self->kicks);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
rugby_possibility_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
RugbyPossibility *self = RUGBY_POSSIBILITY (object);
switch (prop_id)
{
case PROP_TRIES:
self->tries = g_value_get_int (value);
break;
case PROP_UTRIES:
self->utries = g_value_get_int (value);
break;
case PROP_KICKS:
self->kicks = g_value_get_int (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
rugby_possibility_class_init (RugbyPossibilityClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->get_property = rugby_possibility_get_property;
object_class->set_property = rugby_possibility_set_property;
properties[PROP_TRIES] =
g_param_spec_int ("tries", "Tries", "Converted tries",
0, 20, 0,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
properties[PROP_UTRIES] =
g_param_spec_int ("utries", "Utries", "Unconverted tries",
0, 20, 0,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
properties[PROP_KICKS] =
g_param_spec_int ("kicks", "Kicks", "Penalties and drop goals",
0, 20, 0,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
g_object_class_install_properties (object_class,
N_PROPS,
properties);
}
static void
rugby_possibility_init (RugbyPossibility *self)
{
}
RugbyPossibility *
rugby_possibility_new (gint tries,
gint utries,
gint kicks)
{
return g_object_new (RUGBY_TYPE_POSSIBILITY,
"tries", tries,
"utries", utries,
"kicks", kicks,
NULL);
}
/* rugby-possibility.h
*
* Copyright © 2018 Bruce Cowan <bruce@bcowan.eu>
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <glib-object.h>
G_BEGIN_DECLS
#define RUGBY_TYPE_POSSIBILITY (rugby_possibility_get_type())
G_DECLARE_FINAL_TYPE (RugbyPossibility, rugby_possibility, RUGBY, POSSIBILITY, GObject)
RugbyPossibility * rugby_possibility_new (gint tries,
gint utries,
gint kicks);
G_END_DECLS
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "rugby-possibility.h"
#include "rugby-score-store.h" #include "rugby-score-store.h"
#include "rugby-scoring.h" #include "rugby-scoring.h"
...@@ -110,13 +111,17 @@ static void ...@@ -110,13 +111,17 @@ static void
populate_store_foreach (gpointer data, populate_store_foreach (gpointer data,
gpointer user_data) gpointer user_data)
{ {
RugbyPossibility *possiblity = (RugbyPossibility *) data; RugbyPossibility *possiblity = RUGBY_POSSIBILITY (data);
GtkListStore *store = GTK_LIST_STORE (user_data); GtkListStore *store = GTK_LIST_STORE (user_data);
gint tries, utries, kicks;
GtkTreeIter iter; GtkTreeIter iter;
gint tries = possiblity->tries;
gint utries = possiblity->utries; g_object_get (possiblity,
gint kicks = possiblity->kicks; "tries", &tries,
"utries", &utries,
"kicks", &kicks,
NULL);
g_autoptr (GString) string = g_string_new (NULL); g_autoptr (GString) string = g_string_new (NULL);
......
...@@ -16,15 +16,9 @@ ...@@ -16,15 +16,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "rugby-possibility.h"
#include "rugby-scoring.h" #include "rugby-scoring.h"
static inline void
possibility_free (gpointer possibility)
{
g_slice_free (RugbyPossibility, possibility);
}
GPtrArray * GPtrArray *
rugby_scoring_get_possibilities (gint score) rugby_scoring_get_possibilities (gint score)
{ {
...@@ -32,9 +26,8 @@ rugby_scoring_get_possibilities (gint score) ...@@ -32,9 +26,8 @@ rugby_scoring_get_possibilities (gint score)
g_return_val_if_fail (score >= 0, NULL); g_return_val_if_fail (score >= 0, NULL);
array = g_ptr_array_new_with_free_func (possibility_free); array = g_ptr_array_new_with_free_func (g_object_unref);
gint possibilities = 0;
gint max_tries = score / TRY_POINTS; gint max_tries = score / TRY_POINTS;
gint max_utries = score / UTRY_POINTS; gint max_utries = score / UTRY_POINTS;
...@@ -51,13 +44,10 @@ rugby_scoring_get_possibilities (gint score) ...@@ -51,13 +44,10 @@ rugby_scoring_get_possibilities (gint score)
{ {
gint kicks = left / KICK_POINTS; gint kicks = left / KICK_POINTS;
RugbyPossibility *possibility = g_slice_new (RugbyPossibility); RugbyPossibility *possibility = rugby_possibility_new (tries,
possibility->tries = tries; utries,
possibility->utries = utries; kicks);
possibility->kicks = kicks;
g_ptr_array_add (array, possibility); g_ptr_array_add (array, possibility);
possibilities++;
} }
} }
} }
......
...@@ -33,13 +33,6 @@ typedef enum ...@@ -33,13 +33,6 @@ typedef enum
RUGBY_SCORE_TYPE_KICK RUGBY_SCORE_TYPE_KICK
} RugbyScoreType; } RugbyScoreType;
typedef struct
{
gint tries;
gint utries;
gint kicks;
} RugbyPossibility;
GPtrArray * rugby_scoring_get_possibilities (gint score); GPtrArray * rugby_scoring_get_possibilities (gint score);
gint rugby_scoring_get_max_tries (gint score); gint rugby_scoring_get_max_tries (gint score);
gint rugby_scoring_get_max_kicks (gint score); gint rugby_scoring_get_max_kicks (gint score);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment