summaryrefslogtreecommitdiff
path: root/saedit/spritedrawingarea/sdalayer.c
blob: 9794b80587f13178a75aedaea6af76fc912048e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "spritedrawingarea.h"
#include "sdalayer.h"
#include "sdalayerprivate.h"

enum {
	PROP_0,
	PROP_VISIBLE,
	PROP_OFFSET_X,
	PROP_OFFSET_Y,
	PROP_DRAW_FUNC,
	PROP_USER_DATA,
	PROP_Z_INDEX,
	PROP_COUNT
};

static void
sda_layer_get_property (
	GObject *object,
	guint prop_id,
	GValue *value,
	GParamSpec *pspec
) {
	SDALayer *layer = SDA_LAYER (object);
	SDALayerPrivate *priv = layer->priv;

	switch (prop_id) {
		case PROP_VISIBLE:
			g_value_set_boolean (
				value, priv->visible
			);
			break;

		case PROP_OFFSET_X:
			g_value_set_int (
				value, priv->offset_x
			);
			break;

		case PROP_OFFSET_Y:
			g_value_set_int (
				value, priv->offset_y
			);
			break;

		case PROP_Z_INDEX:
			g_value_set_int (
				value, priv->z_index
			);
			break;

		case PROP_DRAW_FUNC:
			g_value_set_pointer (
				value, (gpointer) priv->draw_func
			);
			break;

		case PROP_USER_DATA:
			g_value_set_pointer (
				value, priv->user_data
			);
			break;

		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
			break;
	}
}

static void
sda_layer_set_property (
	GObject *object,
	guint prop_id,
	const GValue *value,
	GParamSpec *pspec
) {
	SDALayer *layer = SDA_LAYER (object);
	SDALayerPrivate *priv = layer->priv;

	switch (prop_id) {
		case PROP_VISIBLE:
			priv->visible = g_value_get_boolean (value); 
			break;

		case PROP_OFFSET_X:
			priv->offset_x = g_value_get_int (value);
			break;

		case PROP_OFFSET_Y:
			priv->offset_y = g_value_get_int (value);
			break;

		case PROP_Z_INDEX:
			priv->z_index = g_value_get_int (value);
			break;

		case PROP_DRAW_FUNC:
			priv->draw_func = (SDALayerDrawFunc) g_value_get_pointer (value);
			break;

		case PROP_USER_DATA:
			priv->user_data = g_value_get_pointer (value);
			break;

		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
			break;
	}
}

void
sda_layer_init (
	SDALayer *layer,
	SDALayerClass *class
) {
	layer->priv = G_TYPE_INSTANCE_GET_PRIVATE (
		layer,
		SDA_TYPE_LAYER,
		SDALayerPrivate
	);
	
	layer->priv->draw_func = NULL;
	layer->priv->user_data = NULL;
}

void
sda_layer_class_init (
	SDALayerClass *klass,
	gpointer class_data
) {
	GObjectClass *object_class;

	g_type_class_add_private (klass, sizeof (SDALayerPrivate));

	object_class = G_OBJECT_CLASS (klass);
	object_class->set_property = sda_layer_set_property;
	object_class->get_property = sda_layer_get_property;

	g_object_class_install_property (
		object_class,
		PROP_VISIBLE,
		g_param_spec_boolean (
			"visible",
			"Visible",
			"Whether this layer is visible",
			TRUE,
			G_PARAM_READWRITE | G_PARAM_CONSTRUCT
		)
	);

	g_object_class_install_property (
		object_class,
		PROP_OFFSET_X,
		g_param_spec_int (
			"offset-x",
			"Offset X",
			"The X coordinate of the layer offset",
			-SPRITE_DRAWING_AREA_FIELD_SIZE,
			 SPRITE_DRAWING_AREA_FIELD_SIZE,
			0,
			G_PARAM_READWRITE | G_PARAM_CONSTRUCT
		)
	);

	g_object_class_install_property (
		object_class,
		PROP_OFFSET_Y,
		g_param_spec_int (
			"offset-y",
			"Offset y",
			"The Y coordinate of the layer offset",
			-SPRITE_DRAWING_AREA_FIELD_SIZE,
			 SPRITE_DRAWING_AREA_FIELD_SIZE,
			0,
			G_PARAM_READWRITE | G_PARAM_CONSTRUCT
		)
	);

	g_object_class_install_property (
		object_class,
		PROP_DRAW_FUNC,
		g_param_spec_pointer (
			"draw-func",
			"Draw function",
			"A function invoked to draw the layer",
			G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY
		)
	);

	g_object_class_install_property (
		object_class,
		PROP_USER_DATA,
		g_param_spec_pointer (
			"user-data",
			"User data",
			"Data that will be propagated to the draw"
			" function upon call",
			G_PARAM_READWRITE | G_PARAM_CONSTRUCT
		)
	);

	g_object_class_install_property (
		object_class,
		PROP_Z_INDEX,
		g_param_spec_int (
			"z-index",
			"Z-index",
			"Z-index of the layer",
			-1024, 1024, 0,
			G_PARAM_READWRITE | G_PARAM_CONSTRUCT
		)
	);
}

GType
sda_layer_get_type (void) {
	static GType sda_layer_type = 0;

	if (sda_layer_type == 0) {
		const GTypeInfo sda_layer_info = {
			sizeof (SDALayerClass),
			NULL, 	/* base_init */
			NULL, 	/* base_finalize */
			(GClassInitFunc) sda_layer_class_init,
			NULL, 	/* class_finalize */
			NULL,	/* class_data */
			sizeof (SDALayer),
			0,	/* n_preallocs */
			(GInstanceInitFunc) sda_layer_init,
			NULL,	/* value_table */
		};

		sda_layer_type = g_type_register_static (
			G_TYPE_OBJECT,
			"SDALayer",
			&sda_layer_info,
			0
		);
	}

	return sda_layer_type;
}

SDALayer *sda_layer_new (
	SDALayerDrawFunc draw_func,
	gpointer user_data
) {
	return g_object_new (
		SDA_TYPE_LAYER,
		"draw-func", draw_func,
		"user-data", user_data,
		NULL
	);
}

gint
sda_layer_compare_by_z_index (
	const SDALayer *a,
	const SDALayer *b
) {
	return a->priv->z_index - b->priv->z_index;
}

void
sda_layer_set_z_index (SDALayer *layer, gint z_index) {
	g_object_set (layer, "z-index", z_index, NULL);
}

void
sda_layer_set_visible (SDALayer *layer, gboolean visible) {
	g_object_set (layer, "visible", visible, NULL);
}