/* game-view.c generated by valac 0.14.0, the Vala compiler
 * generated from game-view.vala, do not modify */


#include <glib.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <gdk-pixbuf/gdk-pixdata.h>
#include <games-preimage.h>
#include <stdlib.h>
#include <string.h>
#include <cairo.h>
#include <float.h>
#include <math.h>


#define TYPE_GAME_VIEW (game_view_get_type ())
#define GAME_VIEW(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_GAME_VIEW, GameView))
#define GAME_VIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_GAME_VIEW, GameViewClass))
#define IS_GAME_VIEW(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_GAME_VIEW))
#define IS_GAME_VIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_GAME_VIEW))
#define GAME_VIEW_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_GAME_VIEW, GameViewClass))

typedef struct _GameView GameView;
typedef struct _GameViewClass GameViewClass;
typedef struct _GameViewPrivate GameViewPrivate;

#define TYPE_GAME (game_get_type ())
#define GAME(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_GAME, Game))
#define GAME_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_GAME, GameClass))
#define IS_GAME(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_GAME))
#define IS_GAME_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_GAME))
#define GAME_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_GAME, GameClass))

typedef struct _Game Game;
typedef struct _GameClass GameClass;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _game_unref0(var) ((var == NULL) ? NULL : (var = (game_unref (var), NULL)))
typedef struct _GamePrivate GamePrivate;

#define TYPE_MAP (map_get_type ())
#define MAP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_MAP, Map))
#define MAP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_MAP, MapClass))
#define IS_MAP(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_MAP))
#define IS_MAP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_MAP))
#define MAP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_MAP, MapClass))

typedef struct _Map Map;
typedef struct _MapClass MapClass;

#define TYPE_TILE (tile_get_type ())
#define TILE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_TILE, Tile))
#define TILE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_TILE, TileClass))
#define IS_TILE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_TILE))
#define IS_TILE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_TILE))
#define TILE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_TILE, TileClass))

typedef struct _Tile Tile;
typedef struct _TileClass TileClass;
typedef struct _TilePrivate TilePrivate;

#define TYPE_SLOT (slot_get_type ())
#define SLOT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_SLOT, Slot))
#define SLOT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_SLOT, SlotClass))
#define IS_SLOT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_SLOT))
#define IS_SLOT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_SLOT))
#define SLOT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_SLOT, SlotClass))

typedef struct _Slot Slot;
typedef struct _SlotClass SlotClass;
#define _tile_unref0(var) ((var == NULL) ? NULL : (var = (tile_unref (var), NULL)))
typedef struct _SlotPrivate SlotPrivate;
#define _cairo_destroy0(var) ((var == NULL) ? NULL : (var = (cairo_destroy (var), NULL)))
#define _cairo_surface_destroy0(var) ((var == NULL) ? NULL : (var = (cairo_surface_destroy (var), NULL)))

struct _GameView {
	GtkDrawingArea parent_instance;
	GameViewPrivate * priv;
	GdkColor background_color;
};

struct _GameViewClass {
	GtkDrawingAreaClass parent_class;
};

struct _GameViewPrivate {
	GdkPixbuf* tile_textures;
	gint tile_texture_width;
	gint tile_texture_height;
	gint x_offset;
	gint y_offset;
	gint tile_width;
	gint tile_height;
	gint tile_layer_offset_x;
	gint tile_layer_offset_y;
	Game* _game;
	GamesPreimage* _theme;
	gboolean _paused;
};

struct _Game {
	GTypeInstance parent_instance;
	volatile int ref_count;
	GamePrivate * priv;
	Map* map;
	GList* tiles;
	Tile* hint_tiles[2];
	gint move_number;
	guint hint_blink_counter;
};

struct _GameClass {
	GTypeClass parent_class;
	void (*finalize) (Game *self);
};

struct _Tile {
	GTypeInstance parent_instance;
	volatile int ref_count;
	TilePrivate * priv;
	gint number;
	Slot* slot;
	gboolean visible;
	gint move_number;
};

struct _TileClass {
	GTypeClass parent_class;
	void (*finalize) (Tile *self);
};

struct _Slot {
	GTypeInstance parent_instance;
	volatile int ref_count;
	SlotPrivate * priv;
	gint x;
	gint y;
	gint layer;
};

struct _SlotClass {
	GTypeClass parent_class;
	void (*finalize) (Slot *self);
};


static gpointer game_view_parent_class = NULL;

GType game_view_get_type (void) G_GNUC_CONST;
gpointer game_ref (gpointer instance);
void game_unref (gpointer instance);
GParamSpec* param_spec_game (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags);
void value_set_game (GValue* value, gpointer v_object);
void value_take_game (GValue* value, gpointer v_object);
gpointer value_get_game (const GValue* value);
GType game_get_type (void) G_GNUC_CONST;
#define GAME_VIEW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_GAME_VIEW, GameViewPrivate))
enum  {
	GAME_VIEW_DUMMY_PROPERTY,
	GAME_VIEW_GAME,
	GAME_VIEW_THEME,
	GAME_VIEW_PAUSED
};
GameView* game_view_new (void);
GameView* game_view_construct (GType object_type);
void game_view_set_background (GameView* self, const gchar* colour);
static gboolean game_view_real_configure_event (GtkWidget* base, GdkEventConfigure* event);
static void game_view_draw_game (GameView* self, cairo_t* cr, gboolean render_indexes);
GamesPreimage* game_view_get_theme (GameView* self);
static void game_view_update_dimensions (GameView* self);
Game* game_view_get_game (GameView* self);
gpointer map_ref (gpointer instance);
void map_unref (gpointer instance);
GParamSpec* param_spec_map (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags);
void value_set_map (GValue* value, gpointer v_object);
void value_take_map (GValue* value, gpointer v_object);
gpointer value_get_map (const GValue* value);
GType map_get_type (void) G_GNUC_CONST;
gpointer tile_ref (gpointer instance);
void tile_unref (gpointer instance);
GParamSpec* param_spec_tile (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags);
void value_set_tile (GValue* value, gpointer v_object);
void value_take_tile (GValue* value, gpointer v_object);
gpointer value_get_tile (const GValue* value);
GType tile_get_type (void) G_GNUC_CONST;
gpointer slot_ref (gpointer instance);
void slot_unref (gpointer instance);
GParamSpec* param_spec_slot (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags);
void value_set_slot (GValue* value, gpointer v_object);
void value_take_slot (GValue* value, gpointer v_object);
gpointer value_get_slot (const GValue* value);
GType slot_get_type (void) G_GNUC_CONST;
static void game_view_get_tile_position (GameView* self, Tile* tile, gint* x, gint* y);
static gint game_view_get_image_offset (GameView* self, gint number);
gboolean game_view_get_paused (GameView* self);
Tile* game_get_selected_tile (Game* self);
guint map_get_width (Map* self);
guint map_get_height (Map* self);
static void game_view_redraw_tile_cb (GameView* self, Tile* tile);
static gboolean game_view_real_draw (GtkWidget* base, cairo_t* cr);
static gboolean game_view_real_button_press_event (GtkWidget* base, GdkEventButton* event);
static Tile* game_view_find_tile (GameView* self, guint x, guint y);
gboolean game_tile_can_move (Game* self, Tile* tile);
void game_set_selected_tile (Game* self, Tile* value);
gboolean tile_matches (Tile* self, Tile* tile);
gboolean game_remove_pair (Game* self, Tile* tile0, Tile* tile1);
void game_view_set_game (GameView* self, Game* value);
static void _game_view_redraw_tile_cb_game_redraw_tile (Game* _sender, Tile* tile, gpointer self);
void game_view_set_theme (GameView* self, GamesPreimage* value);
void game_view_set_paused (GameView* self, gboolean value);
static void game_view_finalize (GObject* obj);
static void _vala_game_view_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec);
static void _vala_game_view_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec);


GameView* game_view_construct (GType object_type) {
	GameView * self = NULL;
	self = (GameView*) g_object_new (object_type, NULL);
	g_object_set ((GtkWidget*) self, "can-focus", TRUE, NULL);
	gtk_widget_add_events ((GtkWidget*) self, (gint) GDK_BUTTON_PRESS_MASK);
	return self;
}


GameView* game_view_new (void) {
	return game_view_construct (TYPE_GAME_VIEW);
}


void game_view_set_background (GameView* self, const gchar* colour) {
	gboolean _tmp0_ = FALSE;
	const gchar* _tmp1_;
	gboolean _tmp5_;
	g_return_if_fail (self != NULL);
	_tmp1_ = colour;
	if (_tmp1_ == NULL) {
		_tmp0_ = TRUE;
	} else {
		const gchar* _tmp2_;
		GdkColor _tmp3_ = {0};
		gboolean _tmp4_ = FALSE;
		_tmp2_ = colour;
		_tmp4_ = gdk_color_parse (_tmp2_, &_tmp3_);
		self->background_color = _tmp3_;
		_tmp0_ = !_tmp4_;
	}
	_tmp5_ = _tmp0_;
	if (_tmp5_) {
		guint16 _tmp6_;
		guint16 _tmp7_;
		self->background_color.blue = (guint16) 0;
		_tmp6_ = self->background_color.blue;
		self->background_color.green = _tmp6_;
		_tmp7_ = self->background_color.green;
		self->background_color.red = _tmp7_;
	}
	gtk_widget_queue_draw ((GtkWidget*) self);
}


static gboolean game_view_real_configure_event (GtkWidget* base, GdkEventConfigure* event) {
	GameView * self;
	gboolean result = FALSE;
	self = (GameView*) base;
	g_return_val_if_fail (event != NULL, FALSE);
	_g_object_unref0 (self->priv->tile_textures);
	self->priv->tile_textures = NULL;
	result = FALSE;
	return result;
}


static gpointer _tile_ref0 (gpointer self) {
	return self ? tile_ref (self) : NULL;
}


static void game_view_draw_game (GameView* self, cairo_t* cr, gboolean render_indexes) {
	GamesPreimage* _tmp0_;
	GamesPreimage* _tmp1_;
	gint _tmp2_;
	gint _tmp3_;
	gint image_width;
	gint _tmp4_;
	gint _tmp5_;
	gint image_height;
	gboolean _tmp6_ = FALSE;
	gboolean _tmp7_;
	gboolean _tmp17_;
	Game* _tmp26_;
	Game* _tmp27_;
	GList* _tmp28_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (cr != NULL);
	_tmp0_ = game_view_get_theme (self);
	_tmp1_ = _tmp0_;
	if (_tmp1_ == NULL) {
		return;
	}
	game_view_update_dimensions (self);
	_tmp2_ = self->priv->tile_width;
	_tmp3_ = self->priv->tile_layer_offset_x;
	image_width = _tmp2_ + _tmp3_;
	_tmp4_ = self->priv->tile_height;
	_tmp5_ = self->priv->tile_layer_offset_y;
	image_height = _tmp4_ + _tmp5_;
	_tmp7_ = render_indexes;
	if (!_tmp7_) {
		gboolean _tmp8_ = FALSE;
		gboolean _tmp9_ = FALSE;
		GdkPixbuf* _tmp10_;
		gboolean _tmp13_;
		gboolean _tmp16_;
		_tmp10_ = self->priv->tile_textures;
		if (_tmp10_ == NULL) {
			_tmp9_ = TRUE;
		} else {
			gint _tmp11_;
			gint _tmp12_;
			_tmp11_ = self->priv->tile_texture_width;
			_tmp12_ = image_width;
			_tmp9_ = _tmp11_ != _tmp12_;
		}
		_tmp13_ = _tmp9_;
		if (_tmp13_) {
			_tmp8_ = TRUE;
		} else {
			gint _tmp14_;
			gint _tmp15_;
			_tmp14_ = self->priv->tile_texture_height;
			_tmp15_ = image_height;
			_tmp8_ = _tmp14_ != _tmp15_;
		}
		_tmp16_ = _tmp8_;
		_tmp6_ = _tmp16_;
	} else {
		_tmp6_ = FALSE;
	}
	_tmp17_ = _tmp6_;
	if (_tmp17_) {
		gint _tmp18_;
		gint _tmp19_;
		GamesPreimage* _tmp20_;
		GamesPreimage* _tmp21_;
		gint _tmp22_;
		gint _tmp23_;
		GdkPixbuf* _tmp24_ = NULL;
		GdkPixbuf* _tmp25_;
		_tmp18_ = image_width;
		self->priv->tile_texture_width = _tmp18_;
		_tmp19_ = image_height;
		self->priv->tile_texture_height = _tmp19_;
		_tmp20_ = game_view_get_theme (self);
		_tmp21_ = _tmp20_;
		_tmp22_ = image_width;
		_tmp23_ = image_height;
		_tmp24_ = games_preimage_render (_tmp21_, (gint) (_tmp22_ * 43), _tmp23_ * 2);
		_g_object_unref0 (self->priv->tile_textures);
		self->priv->tile_textures = _tmp24_;
		_tmp25_ = self->priv->tile_textures;
		if (_tmp25_ == NULL) {
			return;
		}
	}
	_tmp26_ = game_view_get_game (self);
	_tmp27_ = _tmp26_;
	_tmp28_ = _tmp27_->tiles;
	{
		GList* tile_collection = NULL;
		GList* tile_it = NULL;
		tile_collection = _tmp28_;
		for (tile_it = tile_collection; tile_it != NULL; tile_it = tile_it->next) {
			Tile* _tmp29_;
			Tile* tile = NULL;
			_tmp29_ = _tile_ref0 ((Tile*) tile_it->data);
			tile = _tmp29_;
			{
				Tile* _tmp30_;
				gboolean _tmp31_;
				gint x = 0;
				gint y = 0;
				Tile* _tmp32_;
				gint _tmp33_ = 0;
				gint _tmp34_ = 0;
				Tile* _tmp35_;
				gint _tmp36_;
				gint _tmp37_ = 0;
				gint _tmp38_;
				gint texture_x;
				gint texture_y;
				gboolean _tmp39_;
				gboolean _tmp40_;
				gboolean _tmp65_;
				cairo_t* _tmp79_;
				gint _tmp80_;
				gint _tmp81_;
				gint _tmp82_;
				gint _tmp83_;
				cairo_t* _tmp84_;
				_tmp30_ = tile;
				_tmp31_ = _tmp30_->visible;
				if (!_tmp31_) {
					_tile_unref0 (tile);
					continue;
				}
				_tmp32_ = tile;
				game_view_get_tile_position (self, _tmp32_, &_tmp33_, &_tmp34_);
				x = _tmp33_;
				y = _tmp34_;
				_tmp35_ = tile;
				_tmp36_ = _tmp35_->number;
				_tmp37_ = game_view_get_image_offset (self, _tmp36_);
				_tmp38_ = image_width;
				texture_x = _tmp37_ * _tmp38_;
				texture_y = 0;
				_tmp39_ = game_view_get_paused (self);
				_tmp40_ = _tmp39_;
				if (_tmp40_) {
					gint _tmp41_ = 0;
					gint _tmp42_;
					_tmp41_ = game_view_get_image_offset (self, -1);
					_tmp42_ = image_width;
					texture_x = _tmp41_ * _tmp42_;
					texture_y = 0;
				} else {
					Tile* _tmp43_;
					Game* _tmp44_;
					Game* _tmp45_;
					Tile* _tmp46_;
					Tile* _tmp47_;
					_tmp43_ = tile;
					_tmp44_ = game_view_get_game (self);
					_tmp45_ = _tmp44_;
					_tmp46_ = game_get_selected_tile (_tmp45_);
					_tmp47_ = _tmp46_;
					if (_tmp43_ == _tmp47_) {
						gint _tmp48_;
						_tmp48_ = image_height;
						texture_y = _tmp48_;
					} else {
						gboolean _tmp49_ = FALSE;
						Game* _tmp50_;
						Game* _tmp51_;
						guint _tmp52_;
						gboolean _tmp63_;
						_tmp50_ = game_view_get_game (self);
						_tmp51_ = _tmp50_;
						_tmp52_ = _tmp51_->hint_blink_counter;
						if ((_tmp52_ % 2) == ((guint) 1)) {
							gboolean _tmp53_ = FALSE;
							Tile* _tmp54_;
							Game* _tmp55_;
							Game* _tmp56_;
							Tile* _tmp57_;
							gboolean _tmp62_;
							_tmp54_ = tile;
							_tmp55_ = game_view_get_game (self);
							_tmp56_ = _tmp55_;
							_tmp57_ = _tmp56_->hint_tiles[0];
							if (_tmp54_ == _tmp57_) {
								_tmp53_ = TRUE;
							} else {
								Tile* _tmp58_;
								Game* _tmp59_;
								Game* _tmp60_;
								Tile* _tmp61_;
								_tmp58_ = tile;
								_tmp59_ = game_view_get_game (self);
								_tmp60_ = _tmp59_;
								_tmp61_ = _tmp60_->hint_tiles[1];
								_tmp53_ = _tmp58_ == _tmp61_;
							}
							_tmp62_ = _tmp53_;
							_tmp49_ = _tmp62_;
						} else {
							_tmp49_ = FALSE;
						}
						_tmp63_ = _tmp49_;
						if (_tmp63_) {
							gint _tmp64_;
							_tmp64_ = image_height;
							texture_y = _tmp64_;
						}
					}
				}
				_tmp65_ = render_indexes;
				if (_tmp65_) {
					cairo_t* _tmp66_;
					Tile* _tmp67_;
					gint _tmp68_;
					Tile* _tmp69_;
					gint _tmp70_;
					Tile* _tmp71_;
					gint _tmp72_;
					_tmp66_ = cr;
					_tmp67_ = tile;
					_tmp68_ = _tmp67_->number;
					_tmp69_ = tile;
					_tmp70_ = _tmp69_->number;
					_tmp71_ = tile;
					_tmp72_ = _tmp71_->number;
					cairo_set_source_rgb (_tmp66_, _tmp68_ / 255.0, _tmp70_ / 255.0, _tmp72_ / 255.0);
				} else {
					cairo_t* _tmp73_;
					GdkPixbuf* _tmp74_;
					gint _tmp75_;
					gint _tmp76_;
					gint _tmp77_;
					gint _tmp78_;
					_tmp73_ = cr;
					_tmp74_ = self->priv->tile_textures;
					_tmp75_ = x;
					_tmp76_ = texture_x;
					_tmp77_ = y;
					_tmp78_ = texture_y;
					gdk_cairo_set_source_pixbuf (_tmp73_, _tmp74_, (gdouble) (_tmp75_ - _tmp76_), (gdouble) (_tmp77_ - _tmp78_));
				}
				_tmp79_ = cr;
				_tmp80_ = x;
				_tmp81_ = y;
				_tmp82_ = image_width;
				_tmp83_ = image_height;
				cairo_rectangle (_tmp79_, (gdouble) _tmp80_, (gdouble) _tmp81_, (gdouble) _tmp82_, (gdouble) _tmp83_);
				_tmp84_ = cr;
				cairo_fill (_tmp84_);
				_tile_unref0 (tile);
			}
		}
	}
}


static void game_view_update_dimensions (GameView* self) {
	gint _tmp0_ = 0;
	gint width;
	gint _tmp1_ = 0;
	gint height;
	GamesPreimage* _tmp2_;
	GamesPreimage* _tmp3_;
	GamesPreimage* _tmp4_;
	GamesPreimage* _tmp5_;
	gint _tmp6_ = 0;
	GamesPreimage* _tmp7_;
	GamesPreimage* _tmp8_;
	gint _tmp9_ = 0;
	gdouble aspect;
	Game* _tmp10_;
	Game* _tmp11_;
	Map* _tmp12_;
	guint _tmp13_;
	guint _tmp14_;
	gdouble map_width;
	Game* _tmp15_;
	Game* _tmp16_;
	Map* _tmp17_;
	guint _tmp18_;
	guint _tmp19_;
	gdouble _tmp20_;
	gdouble map_height;
	gint _tmp21_;
	gdouble _tmp22_;
	gint _tmp23_;
	gdouble _tmp24_;
	gdouble _tmp25_ = 0.0;
	gdouble unit_width;
	gdouble _tmp26_;
	gdouble _tmp27_;
	gdouble unit_height;
	gdouble _tmp28_;
	gdouble _tmp29_;
	gint _tmp30_;
	gint _tmp31_;
	gint _tmp32_;
	Game* _tmp33_;
	Game* _tmp34_;
	Map* _tmp35_;
	guint _tmp36_;
	guint _tmp37_;
	gdouble _tmp38_;
	gint _tmp39_;
	Game* _tmp40_;
	Game* _tmp41_;
	Map* _tmp42_;
	guint _tmp43_;
	guint _tmp44_;
	gdouble _tmp45_;
	g_return_if_fail (self != NULL);
	_tmp0_ = gtk_widget_get_allocated_width ((GtkWidget*) self);
	width = _tmp0_;
	_tmp1_ = gtk_widget_get_allocated_height ((GtkWidget*) self);
	height = _tmp1_;
	_tmp2_ = game_view_get_theme (self);
	_tmp3_ = _tmp2_;
	if (_tmp3_ == NULL) {
		return;
	}
	_tmp4_ = game_view_get_theme (self);
	_tmp5_ = _tmp4_;
	_tmp6_ = games_preimage_get_height (_tmp5_);
	_tmp7_ = game_view_get_theme (self);
	_tmp8_ = _tmp7_;
	_tmp9_ = games_preimage_get_width (_tmp8_);
	aspect = (((gdouble) _tmp6_) / 2) / (((gdouble) _tmp9_) / 43);
	_tmp10_ = game_view_get_game (self);
	_tmp11_ = _tmp10_;
	_tmp12_ = _tmp11_->map;
	_tmp13_ = map_get_width (_tmp12_);
	_tmp14_ = _tmp13_;
	map_width = _tmp14_ + 2.0;
	_tmp15_ = game_view_get_game (self);
	_tmp16_ = _tmp15_;
	_tmp17_ = _tmp16_->map;
	_tmp18_ = map_get_height (_tmp17_);
	_tmp19_ = _tmp18_;
	_tmp20_ = aspect;
	map_height = (_tmp19_ + 2.0) * _tmp20_;
	_tmp21_ = width;
	_tmp22_ = map_width;
	_tmp23_ = height;
	_tmp24_ = map_height;
	_tmp25_ = MIN (_tmp21_ / _tmp22_, _tmp23_ / _tmp24_);
	unit_width = _tmp25_;
	_tmp26_ = unit_width;
	_tmp27_ = aspect;
	unit_height = _tmp26_ * _tmp27_;
	_tmp28_ = unit_width;
	self->priv->tile_width = (gint) (_tmp28_ * 2);
	_tmp29_ = unit_height;
	self->priv->tile_height = (gint) (_tmp29_ * 2);
	_tmp30_ = self->priv->tile_width;
	self->priv->tile_layer_offset_x = _tmp30_ / 7;
	_tmp31_ = self->priv->tile_height;
	self->priv->tile_layer_offset_y = _tmp31_ / 10;
	_tmp32_ = width;
	_tmp33_ = game_view_get_game (self);
	_tmp34_ = _tmp33_;
	_tmp35_ = _tmp34_->map;
	_tmp36_ = map_get_width (_tmp35_);
	_tmp37_ = _tmp36_;
	_tmp38_ = unit_width;
	self->priv->x_offset = ((gint) (_tmp32_ - (_tmp37_ * _tmp38_))) / 2;
	_tmp39_ = height;
	_tmp40_ = game_view_get_game (self);
	_tmp41_ = _tmp40_;
	_tmp42_ = _tmp41_->map;
	_tmp43_ = map_get_height (_tmp42_);
	_tmp44_ = _tmp43_;
	_tmp45_ = unit_height;
	self->priv->y_offset = ((gint) (_tmp39_ - (_tmp44_ * _tmp45_))) / 2;
}


static void game_view_get_tile_position (GameView* self, Tile* tile, gint* x, gint* y) {
	gint _vala_x = 0;
	gint _vala_y = 0;
	gint _tmp0_;
	Tile* _tmp1_;
	Slot* _tmp2_;
	gint _tmp3_;
	gint _tmp4_;
	Tile* _tmp5_;
	Slot* _tmp6_;
	gint _tmp7_;
	gint _tmp8_;
	gint _tmp9_;
	Tile* _tmp10_;
	Slot* _tmp11_;
	gint _tmp12_;
	gint _tmp13_;
	Tile* _tmp14_;
	Slot* _tmp15_;
	gint _tmp16_;
	gint _tmp17_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (tile != NULL);
	_tmp0_ = self->priv->x_offset;
	_tmp1_ = tile;
	_tmp2_ = _tmp1_->slot;
	_tmp3_ = _tmp2_->x;
	_tmp4_ = self->priv->tile_width;
	_tmp5_ = tile;
	_tmp6_ = _tmp5_->slot;
	_tmp7_ = _tmp6_->layer;
	_tmp8_ = self->priv->tile_layer_offset_x;
	_vala_x = (_tmp0_ + ((_tmp3_ * _tmp4_) / 2)) + (_tmp7_ * _tmp8_);
	_tmp9_ = self->priv->y_offset;
	_tmp10_ = tile;
	_tmp11_ = _tmp10_->slot;
	_tmp12_ = _tmp11_->y;
	_tmp13_ = self->priv->tile_height;
	_tmp14_ = tile;
	_tmp15_ = _tmp14_->slot;
	_tmp16_ = _tmp15_->layer;
	_tmp17_ = self->priv->tile_layer_offset_y;
	_vala_y = (_tmp9_ + ((_tmp12_ * _tmp13_) / 2)) - (_tmp16_ * _tmp17_);
	if (x) {
		*x = _vala_x;
	}
	if (y) {
		*y = _vala_y;
	}
}


static gint game_view_get_image_offset (GameView* self, gint number) {
	gint result = 0;
	gint _tmp0_;
	gint set;
	gboolean _tmp1_ = FALSE;
	gint _tmp2_;
	gboolean _tmp4_;
	gint _tmp5_;
	gint _tmp7_;
	gint _tmp9_;
	g_return_val_if_fail (self != NULL, 0);
	_tmp0_ = number;
	set = _tmp0_ / 4;
	_tmp2_ = number;
	if (_tmp2_ < 0) {
		_tmp1_ = TRUE;
	} else {
		gint _tmp3_;
		_tmp3_ = set;
		_tmp1_ = _tmp3_ >= 36;
	}
	_tmp4_ = _tmp1_;
	if (_tmp4_) {
		result = 42;
		return result;
	}
	_tmp5_ = set;
	if (_tmp5_ == 33) {
		gint _tmp6_;
		_tmp6_ = number;
		result = 33 + (_tmp6_ % 4);
		return result;
	}
	_tmp7_ = set;
	if (_tmp7_ == 35) {
		gint _tmp8_;
		_tmp8_ = number;
		result = 38 + (_tmp8_ % 4);
		return result;
	}
	_tmp9_ = set;
	if (_tmp9_ == 34) {
		result = 37;
		return result;
	}
	result = set;
	return result;
}


static void game_view_redraw_tile_cb (GameView* self, Tile* tile) {
	gint x = 0;
	gint y = 0;
	Tile* _tmp0_;
	gint _tmp1_ = 0;
	gint _tmp2_ = 0;
	gint _tmp3_;
	gint _tmp4_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (tile != NULL);
	game_view_update_dimensions (self);
	_tmp0_ = tile;
	game_view_get_tile_position (self, _tmp0_, &_tmp1_, &_tmp2_);
	x = _tmp1_;
	y = _tmp2_;
	_tmp3_ = self->priv->tile_texture_width;
	_tmp4_ = self->priv->tile_texture_height;
	gtk_widget_queue_draw_area ((GtkWidget*) self, x, y, _tmp3_, _tmp4_);
}


static gboolean game_view_real_draw (GtkWidget* base, cairo_t* cr) {
	GameView * self;
	gboolean result = FALSE;
	Game* _tmp0_;
	Game* _tmp1_;
	cairo_t* _tmp2_;
	GdkColor _tmp3_;
	cairo_t* _tmp4_;
	cairo_t* _tmp5_;
	self = (GameView*) base;
	g_return_val_if_fail (cr != NULL, FALSE);
	_tmp0_ = game_view_get_game (self);
	_tmp1_ = _tmp0_;
	if (_tmp1_ == NULL) {
		result = FALSE;
		return result;
	}
	_tmp2_ = cr;
	_tmp3_ = self->background_color;
	gdk_cairo_set_source_color (_tmp2_, &_tmp3_);
	_tmp4_ = cr;
	cairo_paint (_tmp4_);
	_tmp5_ = cr;
	game_view_draw_game (self, _tmp5_, FALSE);
	result = TRUE;
	return result;
}


static gboolean game_view_real_button_press_event (GtkWidget* base, GdkEventButton* event) {
	GameView * self;
	gboolean result = FALSE;
	gboolean _tmp0_ = FALSE;
	Game* _tmp1_;
	Game* _tmp2_;
	gboolean _tmp5_;
	GdkEventButton _tmp6_;
	GdkEventType _tmp7_;
	GdkEventButton _tmp8_;
	gdouble _tmp9_;
	GdkEventButton _tmp10_;
	gdouble _tmp11_;
	Tile* _tmp12_ = NULL;
	Tile* tile;
	gboolean _tmp13_ = FALSE;
	Tile* _tmp14_;
	gboolean _tmp19_;
	GdkEventButton _tmp20_;
	guint _tmp21_;
	self = (GameView*) base;
	g_return_val_if_fail (event != NULL, FALSE);
	_tmp1_ = game_view_get_game (self);
	_tmp2_ = _tmp1_;
	if (_tmp2_ == NULL) {
		_tmp0_ = TRUE;
	} else {
		gboolean _tmp3_;
		gboolean _tmp4_;
		_tmp3_ = game_view_get_paused (self);
		_tmp4_ = _tmp3_;
		_tmp0_ = _tmp4_;
	}
	_tmp5_ = _tmp0_;
	if (_tmp5_) {
		result = FALSE;
		return result;
	}
	_tmp6_ = *event;
	_tmp7_ = _tmp6_.type;
	if (_tmp7_ != GDK_BUTTON_PRESS) {
		result = FALSE;
		return result;
	}
	_tmp8_ = *event;
	_tmp9_ = _tmp8_.x;
	_tmp10_ = *event;
	_tmp11_ = _tmp10_.y;
	_tmp12_ = game_view_find_tile (self, (guint) _tmp9_, (guint) _tmp11_);
	tile = _tmp12_;
	_tmp14_ = tile;
	if (_tmp14_ == NULL) {
		_tmp13_ = TRUE;
	} else {
		Game* _tmp15_;
		Game* _tmp16_;
		Tile* _tmp17_;
		gboolean _tmp18_ = FALSE;
		_tmp15_ = game_view_get_game (self);
		_tmp16_ = _tmp15_;
		_tmp17_ = tile;
		_tmp18_ = game_tile_can_move (_tmp16_, _tmp17_);
		_tmp13_ = !_tmp18_;
	}
	_tmp19_ = _tmp13_;
	if (_tmp19_) {
		result = TRUE;
		_tile_unref0 (tile);
		return result;
	}
	_tmp20_ = *event;
	_tmp21_ = _tmp20_.button;
	if (_tmp21_ == ((guint) 1)) {
		Game* _tmp22_;
		Game* _tmp23_;
		Tile* _tmp24_;
		Tile* _tmp25_;
		Tile* _tmp29_;
		Game* _tmp30_;
		Game* _tmp31_;
		Tile* _tmp32_;
		Tile* _tmp33_;
		Game* _tmp36_;
		Game* _tmp37_;
		Tile* _tmp38_;
		Tile* _tmp39_;
		Tile* _tmp40_;
		gboolean _tmp41_ = FALSE;
		_tmp22_ = game_view_get_game (self);
		_tmp23_ = _tmp22_;
		_tmp24_ = game_get_selected_tile (_tmp23_);
		_tmp25_ = _tmp24_;
		if (_tmp25_ == NULL) {
			Game* _tmp26_;
			Game* _tmp27_;
			Tile* _tmp28_;
			_tmp26_ = game_view_get_game (self);
			_tmp27_ = _tmp26_;
			_tmp28_ = tile;
			game_set_selected_tile (_tmp27_, _tmp28_);
			result = TRUE;
			_tile_unref0 (tile);
			return result;
		}
		_tmp29_ = tile;
		_tmp30_ = game_view_get_game (self);
		_tmp31_ = _tmp30_;
		_tmp32_ = game_get_selected_tile (_tmp31_);
		_tmp33_ = _tmp32_;
		if (_tmp29_ == _tmp33_) {
			Game* _tmp34_;
			Game* _tmp35_;
			_tmp34_ = game_view_get_game (self);
			_tmp35_ = _tmp34_;
			game_set_selected_tile (_tmp35_, NULL);
			result = TRUE;
			_tile_unref0 (tile);
			return result;
		}
		_tmp36_ = game_view_get_game (self);
		_tmp37_ = _tmp36_;
		_tmp38_ = game_get_selected_tile (_tmp37_);
		_tmp39_ = _tmp38_;
		_tmp40_ = tile;
		_tmp41_ = tile_matches (_tmp39_, _tmp40_);
		if (_tmp41_) {
			Game* _tmp42_;
			Game* _tmp43_;
			Game* _tmp44_;
			Game* _tmp45_;
			Tile* _tmp46_;
			Tile* _tmp47_;
			Tile* _tmp48_;
			_tmp42_ = game_view_get_game (self);
			_tmp43_ = _tmp42_;
			_tmp44_ = game_view_get_game (self);
			_tmp45_ = _tmp44_;
			_tmp46_ = game_get_selected_tile (_tmp45_);
			_tmp47_ = _tmp46_;
			_tmp48_ = tile;
			game_remove_pair (_tmp43_, _tmp47_, _tmp48_);
			result = TRUE;
			_tile_unref0 (tile);
			return result;
		}
	}
	result = FALSE;
	_tile_unref0 (tile);
	return result;
}


static Tile* game_view_find_tile (GameView* self, guint x, guint y) {
	Tile* result = NULL;
	cairo_surface_t* _tmp0_;
	cairo_surface_t* surface;
	cairo_surface_t* _tmp1_;
	cairo_t* _tmp2_;
	cairo_t* cr;
	cairo_t* _tmp3_;
	cairo_t* _tmp4_;
	cairo_t* _tmp5_;
	guint _tmp6_;
	guint _tmp7_;
	cairo_t* _tmp8_;
	cairo_surface_t* _tmp9_;
	guchar* _tmp10_ = NULL;
	guchar* data;
	gint data_length1;
	gint _data_size_;
	guchar* _tmp11_;
	gint _tmp11__length1;
	guchar _tmp12_;
	guchar number;
	Game* _tmp13_;
	Game* _tmp14_;
	GList* _tmp15_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 1, 1);
	surface = _tmp0_;
	_tmp1_ = surface;
	_tmp2_ = cairo_create (_tmp1_);
	cr = _tmp2_;
	_tmp3_ = cr;
	cairo_set_source_rgba (_tmp3_, (gdouble) 255, (gdouble) 255, (gdouble) 255, (gdouble) 255);
	_tmp4_ = cr;
	cairo_paint (_tmp4_);
	_tmp5_ = cr;
	_tmp6_ = x;
	_tmp7_ = y;
	cairo_translate (_tmp5_, (gdouble) (-_tmp6_), (gdouble) (-_tmp7_));
	_tmp8_ = cr;
	game_view_draw_game (self, _tmp8_, TRUE);
	_tmp9_ = surface;
	_tmp10_ = cairo_image_surface_get_data (_tmp9_);
	data = _tmp10_;
	data_length1 = -1;
	_data_size_ = data_length1;
	_tmp11_ = data;
	_tmp11__length1 = data_length1;
	_tmp12_ = _tmp11_[0];
	number = _tmp12_;
	_tmp13_ = game_view_get_game (self);
	_tmp14_ = _tmp13_;
	_tmp15_ = _tmp14_->tiles;
	{
		GList* tile_collection = NULL;
		GList* tile_it = NULL;
		tile_collection = _tmp15_;
		for (tile_it = tile_collection; tile_it != NULL; tile_it = tile_it->next) {
			Tile* _tmp16_;
			Tile* tile = NULL;
			_tmp16_ = _tile_ref0 ((Tile*) tile_it->data);
			tile = _tmp16_;
			{
				Tile* _tmp17_;
				gint _tmp18_;
				guchar _tmp19_;
				_tmp17_ = tile;
				_tmp18_ = _tmp17_->number;
				_tmp19_ = number;
				if (_tmp18_ == ((gint) _tmp19_)) {
					result = tile;
					_cairo_destroy0 (cr);
					_cairo_surface_destroy0 (surface);
					return result;
				}
				_tile_unref0 (tile);
			}
		}
	}
	result = NULL;
	_cairo_destroy0 (cr);
	_cairo_surface_destroy0 (surface);
	return result;
}


Game* game_view_get_game (GameView* self) {
	Game* result;
	Game* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_game;
	result = _tmp0_;
	return result;
}


static gpointer _game_ref0 (gpointer self) {
	return self ? game_ref (self) : NULL;
}


static void _game_view_redraw_tile_cb_game_redraw_tile (Game* _sender, Tile* tile, gpointer self) {
	game_view_redraw_tile_cb (self, tile);
}


void game_view_set_game (GameView* self, Game* value) {
	Game* _tmp0_;
	Game* _tmp1_;
	Game* _tmp2_;
	g_return_if_fail (self != NULL);
	_tmp0_ = value;
	_tmp1_ = _game_ref0 (_tmp0_);
	_game_unref0 (self->priv->_game);
	self->priv->_game = _tmp1_;
	_tmp2_ = self->priv->_game;
	g_signal_connect_object (_tmp2_, "redraw-tile", (GCallback) _game_view_redraw_tile_cb_game_redraw_tile, self, 0);
	gtk_widget_queue_draw ((GtkWidget*) self);
	g_object_notify ((GObject *) self, "game");
}


GamesPreimage* game_view_get_theme (GameView* self) {
	GamesPreimage* result;
	GamesPreimage* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_theme;
	result = _tmp0_;
	return result;
}


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


void game_view_set_theme (GameView* self, GamesPreimage* value) {
	GamesPreimage* _tmp0_;
	GamesPreimage* _tmp1_;
	g_return_if_fail (self != NULL);
	_tmp0_ = value;
	_tmp1_ = _g_object_ref0 (_tmp0_);
	_g_object_unref0 (self->priv->_theme);
	self->priv->_theme = _tmp1_;
	_g_object_unref0 (self->priv->tile_textures);
	self->priv->tile_textures = NULL;
	gtk_widget_queue_draw ((GtkWidget*) self);
	g_object_notify ((GObject *) self, "theme");
}


gboolean game_view_get_paused (GameView* self) {
	gboolean result;
	gboolean _tmp0_;
	g_return_val_if_fail (self != NULL, FALSE);
	_tmp0_ = self->priv->_paused;
	result = _tmp0_;
	return result;
}


void game_view_set_paused (GameView* self, gboolean value) {
	gboolean _tmp0_;
	g_return_if_fail (self != NULL);
	_tmp0_ = value;
	self->priv->_paused = _tmp0_;
	gtk_widget_queue_draw ((GtkWidget*) self);
	g_object_notify ((GObject *) self, "paused");
}


static void game_view_class_init (GameViewClass * klass) {
	game_view_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (GameViewPrivate));
	GTK_WIDGET_CLASS (klass)->configure_event = game_view_real_configure_event;
	GTK_WIDGET_CLASS (klass)->draw = game_view_real_draw;
	GTK_WIDGET_CLASS (klass)->button_press_event = game_view_real_button_press_event;
	G_OBJECT_CLASS (klass)->get_property = _vala_game_view_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_game_view_set_property;
	G_OBJECT_CLASS (klass)->finalize = game_view_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), GAME_VIEW_GAME, param_spec_game ("game", "game", "game", TYPE_GAME, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE | G_PARAM_WRITABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), GAME_VIEW_THEME, g_param_spec_object ("theme", "theme", "theme", GAMES_TYPE_PREIMAGE, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE | G_PARAM_WRITABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), GAME_VIEW_PAUSED, g_param_spec_boolean ("paused", "paused", "paused", FALSE, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE | G_PARAM_WRITABLE));
}


static void game_view_instance_init (GameView * self) {
	self->priv = GAME_VIEW_GET_PRIVATE (self);
	self->priv->tile_textures = NULL;
	self->priv->tile_texture_width = 0;
	self->priv->tile_texture_height = 0;
	self->priv->_theme = NULL;
	self->priv->_paused = FALSE;
}


static void game_view_finalize (GObject* obj) {
	GameView * self;
	self = GAME_VIEW (obj);
	_g_object_unref0 (self->priv->tile_textures);
	_game_unref0 (self->priv->_game);
	_g_object_unref0 (self->priv->_theme);
	G_OBJECT_CLASS (game_view_parent_class)->finalize (obj);
}


GType game_view_get_type (void) {
	static volatile gsize game_view_type_id__volatile = 0;
	if (g_once_init_enter (&game_view_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (GameViewClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) game_view_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GameView), 0, (GInstanceInitFunc) game_view_instance_init, NULL };
		GType game_view_type_id;
		game_view_type_id = g_type_register_static (GTK_TYPE_DRAWING_AREA, "GameView", &g_define_type_info, 0);
		g_once_init_leave (&game_view_type_id__volatile, game_view_type_id);
	}
	return game_view_type_id__volatile;
}


static void _vala_game_view_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec) {
	GameView * self;
	self = GAME_VIEW (object);
	switch (property_id) {
		case GAME_VIEW_GAME:
		value_set_game (value, game_view_get_game (self));
		break;
		case GAME_VIEW_THEME:
		g_value_set_object (value, game_view_get_theme (self));
		break;
		case GAME_VIEW_PAUSED:
		g_value_set_boolean (value, game_view_get_paused (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}


static void _vala_game_view_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec) {
	GameView * self;
	self = GAME_VIEW (object);
	switch (property_id) {
		case GAME_VIEW_GAME:
		game_view_set_game (self, value_get_game (value));
		break;
		case GAME_VIEW_THEME:
		game_view_set_theme (self, g_value_get_object (value));
		break;
		case GAME_VIEW_PAUSED:
		game_view_set_paused (self, g_value_get_boolean (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}



