From 97c74a7c43bcc7e0551a967c6fa2720e9cb58efb Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 12 May 2025 17:02:36 +0200 Subject: lib: add active and defeated properties to Turns.Participant --- lib/src/turns-participant.cpp | 75 +++++++++++++++++++++++++++++++++++++++---- lib/src/turns-participant.h | 32 ++++++++++++++++++ 2 files changed, 100 insertions(+), 7 deletions(-) (limited to 'lib/src') diff --git a/lib/src/turns-participant.cpp b/lib/src/turns-participant.cpp index a94462e..49f29ff 100644 --- a/lib/src/turns-participant.cpp +++ b/lib/src/turns-participant.cpp @@ -17,6 +17,8 @@ struct _TurnsParticipant { GObject parent_instance; + gboolean active; + gboolean defeated; TurnsDisposition disposition; gchar * name; gfloat priority; @@ -30,7 +32,9 @@ namespace { enum struct property { - Disposition = 1, + Active = 1, + Defeated, + Disposition, Name, Priority, N_PROPERTIES, @@ -53,15 +57,16 @@ namespace switch (static_cast(id)) { + case property::Active: + return g_value_set_boolean(value, turns_participant_get_active(participant)); + case property::Defeated: + return g_value_set_boolean(value, turns_participant_get_defeated(participant)); case property::Name: - g_value_set_string(value, turns_participant_get_name(participant)); - return; + return g_value_set_string(value, turns_participant_get_name(participant)); case property::Priority: - g_value_set_float(value, turns_participant_get_priority(participant)); - return; + return g_value_set_float(value, turns_participant_get_priority(participant)); case property::Disposition: - g_value_set_enum(value, turns_participant_get_disposition(participant)); - return; + return g_value_set_enum(value, turns_participant_get_disposition(participant)); default: G_OBJECT_WARN_INVALID_PROPERTY_ID(self, id, specification); }; @@ -73,6 +78,10 @@ namespace switch (static_cast(id)) { + case property::Active: + return turns_participant_set_active(participant, g_value_get_boolean(value)); + case property::Defeated: + return turns_participant_set_defeated(participant, g_value_get_boolean(value)); case property::Name: return turns_participant_set_name(participant, g_value_get_string(value)); case property::Priority: @@ -97,6 +106,20 @@ static void turns_participant_class_init(TurnsParticipantClass * klass) object_class->set_property = set_property; object_class->finalize = finalize; + properties[static_cast(property::Active)] = + g_param_spec_boolean("active", + "Active", + "Whether or not the participant is currently active (taking their turn)", + false, + static_cast(G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY)); + + properties[static_cast(property::Defeated)] = + g_param_spec_boolean("defeated", + "Defeated", + "Whether or not the participant has been defeated", + false, + static_cast(G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY)); + properties[static_cast(property::Name)] = g_param_spec_string("name", "Name", @@ -142,6 +165,18 @@ TurnsParticipant * turns_participant_new_with(gchar const * name, gfloat priorit g_object_new(TURNS_TYPE_PARTICIPANT, "name", name, "priority", priority, "disposition", static_cast(disposition), nullptr)); } +gboolean turns_participant_get_active(TurnsParticipant const * self) +{ + g_return_val_if_fail(TURNS_IS_PARTICIPANT(const_cast(self)), false); + return self->active; +} + +gboolean turns_participant_get_defeated(TurnsParticipant const * self) +{ + g_return_val_if_fail(TURNS_IS_PARTICIPANT(const_cast(self)), false); + return self->defeated; +} + TurnsDisposition turns_participant_get_disposition(TurnsParticipant const * self) { g_return_val_if_fail(TURNS_IS_PARTICIPANT(const_cast(self)), TurnsDisposition::TURNS_DISPOSITION_NEUTRAL); @@ -160,6 +195,32 @@ gfloat turns_participant_get_priority(TurnsParticipant const * self) return self->priority; } +void turns_participant_set_active(TurnsParticipant * self, gboolean value) +{ + g_return_if_fail(TURNS_IS_PARTICIPANT(self)); + + if (value == self->active) + { + return; + } + + self->active = value; + g_object_notify_by_pspec(G_OBJECT(self), properties[static_cast(property::Active)]); +} + +void turns_participant_set_defeated(TurnsParticipant * self, gboolean value) +{ + g_return_if_fail(TURNS_IS_PARTICIPANT(self)); + + if (value == self->defeated) + { + return; + } + + self->defeated = value; + g_object_notify_by_pspec(G_OBJECT(self), properties[static_cast(property::Defeated)]); +} + void turns_participant_set_disposition(TurnsParticipant * self, TurnsDisposition value) { g_return_if_fail(TURNS_IS_PARTICIPANT(self)); diff --git a/lib/src/turns-participant.h b/lib/src/turns-participant.h index a2af8d6..a0e0d98 100644 --- a/lib/src/turns-participant.h +++ b/lib/src/turns-participant.h @@ -31,6 +31,22 @@ TurnsParticipant * turns_participant_new() G_GNUC_WARN_UNUSED_RESULT; */ TurnsParticipant * turns_participant_new_with(gchar const * name, gfloat priority, TurnsDisposition disposition) G_GNUC_WARN_UNUSED_RESULT; +/** + * @brief Get the active state of a participant. + * + * @param self A Participant instance. The value *must not* be NULL. + * @return The active state of the instance. + */ +gboolean turns_participant_get_active(TurnsParticipant const * self); + +/** + * @brief Get the defeated state of a participant. + * + * @param self A Participant instance. The value *must not* be NULL. + * @return The defeated state of the instance. + */ +gboolean turns_participant_get_defeated(TurnsParticipant const * self); + /** * @brief Get the disposition of a participant. * @@ -55,6 +71,22 @@ gchar const * turns_participant_get_name(TurnsParticipant const * self); */ gfloat turns_participant_get_priority(TurnsParticipant const * self); +/** + * @brief Set the active state of a participant. + * + * @param self A Participant instance. The value *must not* be NULL. + * @return The new active state. + */ +void turns_participant_set_active(TurnsParticipant * self, gboolean value); + +/** + * @brief Set the defeated state of a participant. + * + * @param self A Participant instance. The value *must not* be NULL. + * @param value The new defeated state. + */ +void turns_participant_set_defeated(TurnsParticipant * self, gboolean value); + /** * @brief Set the disposition of a participant. * -- cgit v1.2.3