File: callbacks.c

package info (click to toggle)
gweled 0.9.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,468 kB
  • ctags: 516
  • sloc: ansic: 4,170; sh: 2,082; makefile: 100
file content (417 lines) | stat: -rw-r--r-- 10,491 bytes parent folder | download | duplicates (4)
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/* Gweled
 *
 * Copyright (C) 2003-2005 Sebastien Delestaing <sebastien.delestaing@wanadoo.fr>
 * Copyright (C) 2010 Daniele Napolitano <dnax88@gmail.com>
 *
 * This program 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 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gtk/gtk.h>
#include <glib/gi18n-lib.h>

#include "main.h"
#include "sound.h"
#include "sge_core.h"
#include "board_engine.h"
#include "graphic_engine.h"

extern gint gi_game_running;
extern gint gi_score;
extern gboolean g_do_not_score;

extern GtkBuilder *gweled_xml;
extern GtkWidget *g_main_window;
extern GtkWidget *g_pref_window;
extern GtkWidget *g_score_window;
extern GtkWidget *g_drawing_area;
extern GdkPixmap *g_buffer_pixmap;
extern GtkWidget *g_menu_pause;
extern GtkWidget *g_pref_music_button;
extern GtkWidget *g_pref_sounds_button;

extern gint gi_gem_clicked;
extern gint gi_x_click;
extern gint gi_y_click;

extern gint gi_gem_dragged;
extern gint gi_x_drag;
extern gint gi_y_drag;

extern guint board_engine_id;
extern GweledPrefs prefs;

static gint gi_dragging = 0;

static gboolean game_mode_changed = FALSE;

void
on_new1_activate (GtkMenuItem * menuitem, gpointer user_data)
{
	GtkWidget *box;
	gint response;

	if (gi_game_running) {
		box = gtk_message_dialog_new (GTK_WINDOW (g_main_window),
					      GTK_DIALOG_DESTROY_WITH_PARENT,
					      GTK_MESSAGE_QUESTION,
					      GTK_BUTTONS_YES_NO,
					      _("Do you really want to abort this game?"));

		gtk_dialog_set_default_response (GTK_DIALOG (box),
						 GTK_RESPONSE_NO);
		response = gtk_dialog_run (GTK_DIALOG (box));
		gtk_widget_destroy (box);

		if (response != GTK_RESPONSE_YES)
			return;
	}

    gweled_stop_game ();

    welcome_screen_visibility (TRUE);
}

void
on_scores1_activate (GtkMenuItem * menuitem, gpointer user_data)
{
 	show_hiscores (0, FALSE);
}

void
on_pause_activate (GtkMenuItem *menuitem, gpointer user_data)
{

    if(!gi_game_running) return;

    if(board_get_pause() == TRUE )
	    board_set_pause(FALSE);
    else
	    board_set_pause(TRUE);
}

gboolean
on_window_unmap_event (GtkWidget *widget,
                       GdkEvent  *event,
                       gpointer   user_data)
{
    if(gi_game_running && prefs.game_mode == TIMED_MODE && board_get_pause() == FALSE )
        board_set_pause(TRUE);

    return FALSE;
}

gboolean
on_window_focus_out_event (GtkWidget *widget, GdkEventFocus *event, gpointer data)
{
    if( gi_game_running && prefs.game_mode == TIMED_MODE && board_get_pause() == FALSE )
        board_set_pause(TRUE);

    return FALSE;
}

void
on_quit1_activate (GtkMenuItem * menuitem, gpointer user_data)
{
    GtkWidget *box;
    gint response;

    if (gi_game_running) {
        box = gtk_message_dialog_new (GTK_WINDOW (g_main_window),
                          GTK_DIALOG_DESTROY_WITH_PARENT,
                          GTK_MESSAGE_QUESTION,
                          GTK_BUTTONS_YES_NO,
                          _("Do you want to save the current game?"));

        gtk_dialog_set_default_response (GTK_DIALOG (box),
                         GTK_RESPONSE_NO);
        response = gtk_dialog_run (GTK_DIALOG (box));
        gtk_widget_destroy (box);

        if (response == GTK_RESPONSE_YES)
            save_current_game();
        else {
            gchar *filename;

            filename = g_strconcat(g_get_user_config_dir(), "/gweled.saved-game", NULL);
            unlink(filename);
        }
    }

	gtk_main_quit ();
}

void
on_preferences1_activate (GtkMenuItem * menuitem, gpointer user_data)
{
	gtk_widget_show (g_pref_window);
}
void
on_about1_activate (GtkMenuItem * menuitem, gpointer user_data)
{
	const gchar *authors[] = {
	    "Sebastien Delestaing <sebdelestaing@free.fr>",
	    "Daniele Napolitano <dnax88@gmail.com>",
	    NULL
	};

	const gchar *translator_credits = _("translator-credits");

    gtk_show_about_dialog (GTK_WINDOW(g_main_window),
             "authors", authors,
		     "translator-credits", g_strcmp0("translator-credits", translator_credits) ? translator_credits : NULL,
             "comments", _("A puzzle game with gems"),
             "copyright", "Copyright © 2003-2005 Sebastien Delestaing\nCopyright © 2010 Daniele Napolitano",
             "version", VERSION,
             "website", "http://gweled.org",
		     "logo-icon-name", "gweled",
             NULL);
}

gboolean
drawing_area_expose_event_cb (GtkWidget * widget, GdkEventExpose * event, gpointer user_data)
{
	gdk_draw_drawable (GDK_DRAWABLE (widget->window),
			   widget->style->fg_gc[gtk_widget_get_state (widget)],
			   g_buffer_pixmap, event->area.x, event->area.y,
			   event->area.x, event->area.y, event->area.width,
			   event->area.height);

	return FALSE;
}

gboolean
drawing_area_button_event_cb (GtkWidget * widget, GdkEventButton * event, gpointer user_data)
{
	static int x_press = -1;
	static int y_press = -1;
	static int x_release = -1;
	static int y_release = -1;

    // resume game on click
    if(gi_game_running && board_get_pause() == TRUE && event->type == GDK_BUTTON_RELEASE ) {
        board_set_pause(FALSE);
        // not handle this click
        return FALSE;
    }

    // in pause mode don't accept events
    if(board_get_pause())
        return FALSE;

    // only handle left button
    if(event->button != 1)
        return FALSE;

    respawn_board_engine_loop();

	switch (event->type) {
	case GDK_BUTTON_PRESS:
		x_press = event->x / prefs.tile_size;
		y_press = event->y / prefs.tile_size;
		if (gi_game_running) {
			gi_x_click = x_press;
			gi_y_click = y_press;
			gi_gem_clicked = -1;
			gi_dragging = -1;

			if(prefs.sounds_on)
			    sound_play_sample(CLICK_EVENT);
		}
		break;

	case GDK_BUTTON_RELEASE:
		gi_dragging = 0;
		gi_gem_dragged = 0;
		x_release = event->x / prefs.tile_size;
		y_release = event->y / prefs.tile_size;
		if( (x_release < 0) ||
			(x_release > BOARD_WIDTH - 1) ||
			(y_release < 0) ||
			(y_release > BOARD_HEIGHT - 1) )
			break;
		if( (x_press != x_release) ||
			(y_press != y_release))
		{
			if (gi_game_running) {
				gi_x_click = x_release;
				gi_y_click = y_release;
				gi_gem_clicked = -1;
			}
		}
		break;

	default:
		break;
	}

	return FALSE;
}

gboolean
drawing_area_motion_event_cb (GtkWidget * widget, GdkEventMotion * event, gpointer user_data)
{
	if (gi_dragging) {
		gi_x_drag = event->x / prefs.tile_size;
		gi_y_drag = event->y / prefs.tile_size;
		gi_gem_dragged = -1;
	}

	return FALSE;
}

gboolean
on_app_delete_event (GtkWidget * widget, GdkEvent * event, gpointer user_data)
{
    on_quit1_activate (NULL, NULL);

	return FALSE;
}

gboolean
on_preferencesDialog_delete_event (GtkWidget * widget, GdkEvent * event, gpointer user_data)
{
	gtk_widget_hide (widget);
	return TRUE;
}

void
on_closebutton1_clicked (GtkButton * button, gpointer user_data)
{
	save_preferences();

    // unpause the game if running
    if(gi_game_running)
	    board_set_pause(FALSE);

	gtk_widget_hide (gtk_widget_get_toplevel (GTK_WIDGET (button)));
}

void on_buttonNormal_clicked (GtkButton * button, gpointer user_data)
{
    prefs.game_mode = NORMAL_MODE;
    welcome_screen_visibility(FALSE);

    gweled_draw_board ();
    gweled_start_new_game ();
    respawn_board_engine_loop();
    gtk_widget_set_sensitive(g_menu_pause, TRUE);
}

void on_buttonTimed_clicked (GtkButton * button, gpointer user_data)
{
    prefs.game_mode = TIMED_MODE;
    welcome_screen_visibility(FALSE);

    gweled_draw_board ();
    gweled_start_new_game ();
    respawn_board_engine_loop();
    gtk_widget_set_sensitive(g_menu_pause, TRUE);
}

void on_buttonEndless_clicked (GtkButton * button, gpointer user_data)
{
    prefs.game_mode = ENDLESS_MODE;
    welcome_screen_visibility(FALSE);

    gweled_draw_board ();
    gweled_start_new_game ();
    respawn_board_engine_loop();
    gtk_widget_set_sensitive(g_menu_pause, TRUE);
}

void
on_smallRadiobutton_toggled (GtkToggleButton * togglebutton, gpointer user_data)
{
	if (gtk_toggle_button_get_active (togglebutton)) {
		prefs.tile_size = 32;

		gweled_set_objects_size (prefs.tile_size);
	}

	if(!gi_game_running)
        welcome_screen_visibility(TRUE);
}

void
on_mediumRadiobutton_toggled (GtkToggleButton * togglebutton, gpointer user_data)
{
	if (gtk_toggle_button_get_active (togglebutton)) {
		prefs.tile_size = 48;

		gweled_set_objects_size (prefs.tile_size);
	}

	if(!gi_game_running)
        welcome_screen_visibility(TRUE);
}

void
on_largeRadiobutton_toggled (GtkToggleButton * togglebutton, gpointer user_data)
{
	if (gtk_toggle_button_get_active (togglebutton)) {
		prefs.tile_size = 64;

		gweled_set_objects_size (prefs.tile_size);
	}

    if(!gi_game_running)
        welcome_screen_visibility(TRUE);
}

void
on_music_checkbutton_toggled (GtkToggleButton * togglebutton, gpointer user_data)
{
	if (gtk_toggle_button_get_active (togglebutton)) {
	    prefs.music_on = TRUE;
	    if(sound_get_enabled() == FALSE) {
	        sound_init();
		    if(sound_get_enabled() == FALSE) {
	            gtk_widget_set_sensitive(g_pref_music_button, FALSE);
	            gtk_widget_set_sensitive(g_pref_sounds_button, FALSE);
	        }
		}
		sound_music_play();
	} else {
	    prefs.music_on = FALSE;
		sound_music_stop();
		if(prefs.sounds_on == FALSE)
		    sound_destroy();
    }
}

void
on_sounds_checkbutton_toggled (GtkToggleButton * togglebutton, gpointer user_data)
{
	if (gtk_toggle_button_get_active (togglebutton)) {
		prefs.sounds_on = TRUE;
		if(sound_get_enabled() == FALSE) {
	        sound_init();
		    if(sound_get_enabled() == FALSE) {
	            gtk_widget_set_sensitive(g_pref_music_button, FALSE);
	            gtk_widget_set_sensitive(g_pref_sounds_button, FALSE);
	        }
		}
		sound_load_samples();
	} else {
		prefs.sounds_on = FALSE;
		sound_unload_samples();
		if(prefs.music_on == FALSE)
		    sound_destroy();
    }
}