/* piece.c generated by valac 0.56.19, the Vala compiler
 * generated from piece.vala, do not modify */

/* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
   This file is part of GNOME Reversi, also known as Iagno.

   Copyright 2026 Andrey Kutejko

   GNOME Reversi is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   GNOME Reversi 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GNOME Reversi.  If not, see <https://www.gnu.org/licenses/>.
*/

#include <glib-object.h>
#include <gdk/gdk.h>
#include <glib.h>
#include <float.h>
#include <math.h>
#include <gtk/gtk.h>
#include <graphene-gobject.h>
#include <string.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif
#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define TYPE_PIECE (piece_get_type ())
#define PIECE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_PIECE, Piece))
#define PIECE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_PIECE, PieceClass))
#define IS_PIECE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_PIECE))
#define IS_PIECE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_PIECE))
#define PIECE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_PIECE, PieceClass))

typedef struct _Piece Piece;
typedef struct _PieceClass PieceClass;
typedef struct _PiecePrivate PiecePrivate;

#define TYPE_THEME (theme_get_type ())
#define THEME(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_THEME, Theme))
#define THEME_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_THEME, ThemeClass))
#define IS_THEME(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_THEME))
#define IS_THEME_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_THEME))
#define THEME_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_THEME, ThemeClass))

typedef struct _Theme Theme;
typedef struct _ThemeClass ThemeClass;
typedef enum  {
	PLAYER_NONE,
	PLAYER_DARK,
	PLAYER_LIGHT
} Player;

#define TYPE_PLAYER (player_get_type ())
enum  {
	PIECE_0_PROPERTY,
	PIECE_THEME_PROPERTY,
	PIECE_PLAYER_PROPERTY,
	PIECE_NUM_PROPERTIES
};
static GParamSpec* piece_properties[PIECE_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

struct _Piece {
	GObject parent_instance;
	PiecePrivate * priv;
};

struct _PieceClass {
	GObjectClass parent_class;
};

struct _PiecePrivate {
	Theme* _theme;
	Player _player;
	GdkTexture* texture;
};

static gint Piece_private_offset;
static gpointer piece_parent_class = NULL;
static GdkPaintableInterface * piece_gdk_paintable_parent_iface = NULL;

VALA_EXTERN GType piece_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (Piece, g_object_unref)
VALA_EXTERN GType theme_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (Theme, g_object_unref)
VALA_EXTERN GType player_get_type (void) G_GNUC_CONST ;
static void piece_real_snapshot (GdkPaintable* base,
                          GdkSnapshot* gdk_snapshot,
                          gdouble width,
                          gdouble height);
VALA_EXTERN Theme* piece_get_theme (Piece* self);
VALA_EXTERN GdkTexture* theme_tileset_for_size (Theme* self,
                                    gint tile_size);
VALA_EXTERN Player piece_get_player (Piece* self);
VALA_EXTERN Piece* piece_new (void);
VALA_EXTERN Piece* piece_construct (GType object_type);
VALA_EXTERN void piece_set_theme (Piece* self,
                      Theme* value);
VALA_EXTERN void piece_set_player (Piece* self,
                       Player value);
static GObject * piece_constructor (GType type,
                             guint n_construct_properties,
                             GObjectConstructParam * construct_properties);
static void _piece___lambda7_ (Piece* self);
static void __piece___lambda7__g_object_notify (GObject* _sender,
                                         GParamSpec* pspec,
                                         gpointer self);
static void _piece___lambda8_ (Piece* self);
static void __piece___lambda8__g_object_notify (GObject* _sender,
                                         GParamSpec* pspec,
                                         gpointer self);
static void piece_finalize (GObject * obj);
static GType piece_get_type_once (void);
static void _vala_piece_get_property (GObject * object,
                               guint property_id,
                               GValue * value,
                               GParamSpec * pspec);
static void _vala_piece_set_property (GObject * object,
                               guint property_id,
                               const GValue * value,
                               GParamSpec * pspec);

static inline gpointer
piece_get_instance_private (Piece* self)
{
	return G_STRUCT_MEMBER_P (self, Piece_private_offset);
}

static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

static void
piece_real_snapshot (GdkPaintable* base,
                     GdkSnapshot* gdk_snapshot,
                     gdouble width,
                     gdouble height)
{
	Piece * self;
	GtkSnapshot* snapshot = NULL;
	GtkSnapshot* _tmp0_;
	gint tile_size = 0;
	gboolean _tmp1_ = FALSE;
	GdkTexture* _tmp2_;
	GdkTexture* _tmp7_;
	GdkTexture* texture_ = NULL;
	GdkTexture* _tmp8_;
	GdkTexture* _tmp9_;
	gint pixmap = 0;
	Player _tmp10_;
	graphene_rect_t tile_rect = {0};
	graphene_point_t _tmp11_ = {0};
	graphene_size_t _tmp12_ = {0};
	graphene_rect_t _tmp13_ = {0};
	GtkSnapshot* _tmp14_;
	graphene_rect_t _tmp15_;
	GtkSnapshot* _tmp16_;
	GtkSnapshot* _tmp17_;
	graphene_point_t _tmp18_ = {0};
	GdkTexture* _tmp19_;
	GtkSnapshot* _tmp20_;
	GdkTexture* _tmp21_;
	GdkTexture* _tmp22_;
	GtkSnapshot* _tmp23_;
	GtkSnapshot* _tmp24_;
	self = (Piece*) base;
	g_return_if_fail (gdk_snapshot != NULL);
	_tmp0_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (gdk_snapshot, gtk_snapshot_get_type (), GtkSnapshot));
	snapshot = _tmp0_;
	tile_size = (gint) MIN (width, height);
	_tmp2_ = self->priv->texture;
	if (_tmp2_ == NULL) {
		_tmp1_ = TRUE;
	} else {
		GdkTexture* _tmp3_;
		_tmp3_ = self->priv->texture;
		_tmp1_ = gdk_texture_get_height (G_TYPE_CHECK_INSTANCE_CAST (_tmp3_, gdk_texture_get_type (), GdkTexture)) != (tile_size * 4);
	}
	if (_tmp1_) {
		Theme* _tmp4_;
		_g_object_unref0 (self->priv->texture);
		self->priv->texture = NULL;
		_tmp4_ = self->priv->_theme;
		if (_tmp4_ != NULL) {
			Theme* _tmp5_;
			GdkTexture* _tmp6_;
			_tmp5_ = self->priv->_theme;
			_tmp6_ = theme_tileset_for_size (G_TYPE_CHECK_INSTANCE_CAST (_tmp5_, TYPE_THEME, Theme), tile_size);
			_g_object_unref0 (self->priv->texture);
			self->priv->texture = _tmp6_;
		}
	}
	_tmp7_ = self->priv->texture;
	if (_tmp7_ == NULL) {
		g_warning ("piece.vala:48: No piece texture for a size %d", tile_size);
		_g_object_unref0 (snapshot);
		return;
	}
	_tmp8_ = self->priv->texture;
	_tmp9_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp8_, gdk_texture_get_type (), GdkTexture));
	texture_ = _tmp9_;
	_tmp10_ = self->priv->_player;
	switch (_tmp10_) {
		case PLAYER_NONE:
		{
			_g_object_unref0 (texture_);
			_g_object_unref0 (snapshot);
			return;
		}
		case PLAYER_DARK:
		{
			pixmap = 1;
			break;
		}
		case PLAYER_LIGHT:
		{
			pixmap = 31;
			break;
		}
		default:
		{
			g_assert_not_reached ();
		}
	}
	_tmp11_.x = (gfloat) 0;
	_tmp11_.y = (gfloat) 0;
	_tmp12_.width = (gfloat) tile_size;
	_tmp12_.height = (gfloat) tile_size;
	memset (&_tmp13_, 0, sizeof (graphene_rect_t));
	_tmp13_.origin = _tmp11_;
	_tmp13_.size = _tmp12_;
	tile_rect = _tmp13_;
	_tmp14_ = snapshot;
	_tmp15_ = tile_rect;
	gtk_snapshot_push_clip (_tmp14_, &_tmp15_);
	_tmp16_ = snapshot;
	gtk_snapshot_save (_tmp16_);
	_tmp17_ = snapshot;
	memset (&_tmp18_, 0, sizeof (graphene_point_t));
	_tmp18_.x = (gfloat) ((-(pixmap % 8)) * tile_size);
	_tmp18_.y = (gfloat) ((-(pixmap / 8)) * tile_size);
	gtk_snapshot_translate (_tmp17_, &_tmp18_);
	_tmp19_ = texture_;
	_tmp20_ = snapshot;
	_tmp21_ = texture_;
	_tmp22_ = texture_;
	gdk_paintable_snapshot ((GdkPaintable*) _tmp19_, (GdkSnapshot*) _tmp20_, (gdouble) gdk_texture_get_width (_tmp21_), (gdouble) gdk_texture_get_height (_tmp22_));
	_tmp23_ = snapshot;
	gtk_snapshot_restore (_tmp23_);
	_tmp24_ = snapshot;
	gtk_snapshot_pop (_tmp24_);
	_g_object_unref0 (texture_);
	_g_object_unref0 (snapshot);
}

Piece*
piece_construct (GType object_type)
{
	Piece * self = NULL;
	self = (Piece*) g_object_new (object_type, NULL);
	return self;
}

Piece*
piece_new (void)
{
	return piece_construct (TYPE_PIECE);
}

Theme*
piece_get_theme (Piece* self)
{
	Theme* result;
	Theme* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_theme;
	result = _tmp0_;
	return result;
}

void
piece_set_theme (Piece* self,
                 Theme* value)
{
	Theme* old_value;
	g_return_if_fail (self != NULL);
	old_value = piece_get_theme (self);
	if (old_value != value) {
		Theme* _tmp0_;
		_tmp0_ = _g_object_ref0 (value);
		_g_object_unref0 (self->priv->_theme);
		self->priv->_theme = _tmp0_;
		g_object_notify_by_pspec ((GObject *) self, piece_properties[PIECE_THEME_PROPERTY]);
	}
}

Player
piece_get_player (Piece* self)
{
	Player result;
	g_return_val_if_fail (self != NULL, 0);
	result = self->priv->_player;
	return result;
}

void
piece_set_player (Piece* self,
                  Player value)
{
	Player old_value;
	g_return_if_fail (self != NULL);
	old_value = piece_get_player (self);
	if (old_value != value) {
		self->priv->_player = value;
		g_object_notify_by_pspec ((GObject *) self, piece_properties[PIECE_PLAYER_PROPERTY]);
	}
}

static void
_piece___lambda7_ (Piece* self)
{
	_g_object_unref0 (self->priv->texture);
	self->priv->texture = NULL;
	gdk_paintable_invalidate_contents ((GdkPaintable*) self);
}

static void
__piece___lambda7__g_object_notify (GObject* _sender,
                                    GParamSpec* pspec,
                                    gpointer self)
{
	_piece___lambda7_ ((Piece*) self);
}

static void
_piece___lambda8_ (Piece* self)
{
	gdk_paintable_invalidate_contents ((GdkPaintable*) self);
}

static void
__piece___lambda8__g_object_notify (GObject* _sender,
                                    GParamSpec* pspec,
                                    gpointer self)
{
	_piece___lambda8_ ((Piece*) self);
}

static GObject *
piece_constructor (GType type,
                   guint n_construct_properties,
                   GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	Piece * self;
	parent_class = G_OBJECT_CLASS (piece_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_PIECE, Piece);
	g_signal_connect_object ((GObject*) self, "notify::theme", (GCallback) __piece___lambda7__g_object_notify, self, 0);
	g_signal_connect_object ((GObject*) self, "notify::player", (GCallback) __piece___lambda8__g_object_notify, self, 0);
	return obj;
}

static void
piece_class_init (PieceClass * klass,
                  gpointer klass_data)
{
	piece_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &Piece_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_piece_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_piece_set_property;
	G_OBJECT_CLASS (klass)->constructor = piece_constructor;
	G_OBJECT_CLASS (klass)->finalize = piece_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), PIECE_THEME_PROPERTY, piece_properties[PIECE_THEME_PROPERTY] = g_param_spec_object ("theme", "theme", "theme", TYPE_THEME, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), PIECE_PLAYER_PROPERTY, piece_properties[PIECE_PLAYER_PROPERTY] = g_param_spec_enum ("player", "player", "player", TYPE_PLAYER, PLAYER_NONE, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
}

static void
piece_gdk_paintable_interface_init (GdkPaintableInterface * iface,
                                    gpointer iface_data)
{
	piece_gdk_paintable_parent_iface = g_type_interface_peek_parent (iface);
	iface->snapshot = (void (*) (GdkPaintable*, GdkSnapshot*, gdouble, gdouble)) piece_real_snapshot;
}

static void
piece_instance_init (Piece * self,
                     gpointer klass)
{
	self->priv = piece_get_instance_private (self);
	self->priv->_player = PLAYER_NONE;
	self->priv->texture = NULL;
}

static void
piece_finalize (GObject * obj)
{
	Piece * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_PIECE, Piece);
	_g_object_unref0 (self->priv->_theme);
	_g_object_unref0 (self->priv->texture);
	G_OBJECT_CLASS (piece_parent_class)->finalize (obj);
}

 G_GNUC_NO_INLINE static GType
piece_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (PieceClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) piece_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (Piece), 0, (GInstanceInitFunc) piece_instance_init, NULL };
	static const GInterfaceInfo gdk_paintable_info = { (GInterfaceInitFunc) piece_gdk_paintable_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	GType piece_type_id;
	piece_type_id = g_type_register_static (G_TYPE_OBJECT, "Piece", &g_define_type_info, G_TYPE_FLAG_NONE);
	g_type_add_interface_static (piece_type_id, gdk_paintable_get_type (), &gdk_paintable_info);
	Piece_private_offset = g_type_add_instance_private (piece_type_id, sizeof (PiecePrivate));
	return piece_type_id;
}

GType
piece_get_type (void)
{
	static gsize piece_type_id__once = 0;
	if (g_once_init_enter (&piece_type_id__once)) {
		GType piece_type_id;
		piece_type_id = piece_get_type_once ();
		g_once_init_leave (&piece_type_id__once, piece_type_id);
	}
	return piece_type_id__once;
}

static void
_vala_piece_get_property (GObject * object,
                          guint property_id,
                          GValue * value,
                          GParamSpec * pspec)
{
	Piece * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, TYPE_PIECE, Piece);
	switch (property_id) {
		case PIECE_THEME_PROPERTY:
		g_value_set_object (value, piece_get_theme (self));
		break;
		case PIECE_PLAYER_PROPERTY:
		g_value_set_enum (value, piece_get_player (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_piece_set_property (GObject * object,
                          guint property_id,
                          const GValue * value,
                          GParamSpec * pspec)
{
	Piece * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, TYPE_PIECE, Piece);
	switch (property_id) {
		case PIECE_THEME_PROPERTY:
		piece_set_theme (self, g_value_get_object (value));
		break;
		case PIECE_PLAYER_PROPERTY:
		piece_set_player (self, g_value_get_enum (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

