1 /*****************************************************************************
2 * puzzle.c : Puzzle game
3 *****************************************************************************
4 * Copyright (C) 2005-2009 VLC authors and VideoLAN
5 * Copyright (C) 2013 Vianney Boyer
8 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
9 * Vianney Boyer <vlcvboyer -at- gmail -dot- com>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_filter.h>
40 #include "filter_picture.h"
43 #include "puzzle_bezier.h"
44 #include "puzzle_lib.h"
45 #include "puzzle_pce.h"
46 #include "puzzle_mgt.h"
48 /*****************************************************************************
50 *****************************************************************************/
51 #define ROWS_TEXT N_("Number of puzzle rows")
52 #define ROWS_LONGTEXT N_("Number of puzzle rows")
53 #define COLS_TEXT N_("Number of puzzle columns")
54 #define COLS_LONGTEXT N_("Number of puzzle columns")
55 #define MODE_TEXT N_("Game mode")
56 #define MODE_LONGTEXT N_("Select game mode variation from jigsaw puzzle to sliding puzzle.")
57 #define BORDER_TEXT N_("Border")
58 #define BORDER_LONGTEXT N_("Unshuffled Border width.")
59 #define PREVIEW_TEXT N_("Small preview")
60 #define PREVIEW_LONGTEXT N_("Show small preview.")
61 #define PREVIEWSIZE_TEXT N_("Small preview size")
62 #define PREVIEWSIZE_LONGTEXT N_("Show small preview size (percent of source).")
63 #define SHAPE_SIZE_TEXT N_("Piece edge shape size")
64 #define SHAPE_SIZE_LONGTEXT N_("Size of the curve along the piece's edge")
65 #define AUTO_SHUFFLE_TEXT N_("Auto shuffle")
66 #define AUTO_SHUFFLE_LONGTEXT N_("Auto shuffle delay during game")
67 #define AUTO_SOLVE_TEXT N_("Auto solve")
68 #define AUTO_SOLVE_LONGTEXT N_("Auto solve delay during game")
69 #define ROTATION_TEXT N_("Rotation")
70 #define ROTATION_LONGTEXT N_("Rotation parameter: none;180;90-270;mirror")
72 const int pi_mode_values[] = { (int) 0, (int) 1, (int) 2, (int) 3 };
73 const char *const ppsz_mode_descriptions[] = { N_("jigsaw puzzle"), N_("sliding puzzle"), N_("swap puzzle"), N_("exchange puzzle") };
74 const int pi_rotation_values[] = { (int) 0, (int) 1, (int) 2, (int) 3 };
75 const char *const ppsz_rotation_descriptions[] = { N_("0"), N_("0/180"), N_("0/90/180/270"), N_("0/90/180/270/mirror") };
77 #define CFG_PREFIX "puzzle-"
79 int Open ( vlc_object_t * );
80 void Close( vlc_object_t * );
83 set_description( N_("Puzzle interactive game video filter") )
84 set_shortname( N_( "Puzzle" ))
85 set_capability( "video filter2", 0 )
86 set_category( CAT_VIDEO )
87 set_subcategory( SUBCAT_VIDEO_VFILTER )
89 add_integer_with_range( CFG_PREFIX "rows", 4, 2, 16,
90 ROWS_TEXT, ROWS_LONGTEXT, false )
91 add_integer_with_range( CFG_PREFIX "cols", 4, 2, 16,
92 COLS_TEXT, COLS_LONGTEXT, false )
93 add_integer_with_range( CFG_PREFIX "border", 3, 0, 40,
94 BORDER_TEXT, BORDER_LONGTEXT, false )
95 add_bool( CFG_PREFIX "preview", false,
96 PREVIEW_TEXT, PREVIEW_LONGTEXT, false )
97 add_integer_with_range( CFG_PREFIX "preview-size", 15, 0, 100,
98 PREVIEWSIZE_TEXT, PREVIEWSIZE_LONGTEXT, false )
99 add_integer_with_range( CFG_PREFIX "shape-size", 90, 0, 100,
100 SHAPE_SIZE_TEXT, SHAPE_SIZE_LONGTEXT, false )
101 add_integer_with_range( CFG_PREFIX "auto-shuffle", 0, 0, 30000,
102 AUTO_SHUFFLE_TEXT, AUTO_SHUFFLE_LONGTEXT, false )
103 add_integer_with_range( CFG_PREFIX "auto-solve", 0, 0, 30000,
104 AUTO_SOLVE_TEXT, AUTO_SOLVE_LONGTEXT, false )
105 add_integer( CFG_PREFIX "rotation", 0,
106 ROTATION_TEXT, ROTATION_LONGTEXT, false )
107 change_integer_list(pi_rotation_values, ppsz_rotation_descriptions )
108 add_integer( CFG_PREFIX "mode", 0,
109 MODE_TEXT, MODE_LONGTEXT, false )
110 change_integer_list(pi_mode_values, ppsz_mode_descriptions )
112 set_callbacks( Open, Close )
115 /*****************************************************************************
117 *****************************************************************************/
119 const char *const ppsz_filter_options[] = {
120 "rows", "cols","border", "preview", "preview-size", "mode", "shape-size", "auto-shuffle", "auto-solve", "rotation", NULL
126 int Open( vlc_object_t *p_this )
128 filter_t *p_filter = (filter_t *)p_this;
131 /* Assert video in match with video out */
132 if( !es_format_IsSimilar( &p_filter->fmt_in, &p_filter->fmt_out ) ) {
133 msg_Err( p_filter, "Input and output format does not match" );
137 const vlc_chroma_description_t *p_chroma =
138 vlc_fourcc_GetChromaDescription( p_filter->fmt_in.video.i_chroma );
139 if( p_chroma == NULL || p_chroma->plane_count == 0 )
142 /* Allocate structure */
143 p_filter->p_sys = p_sys = calloc(1, sizeof( *p_sys ) );
147 /* init some values */
148 p_sys->b_shuffle_rqst = true;
149 p_sys->b_change_param = true;
150 p_sys->i_mouse_drag_pce = NO_PCE;
151 p_sys->i_pointed_pce = NO_PCE;
152 p_sys->i_magnet_accuracy = 3;
154 /* Generate values of random bezier shapes */
155 p_sys->ps_bezier_pts_H = calloc( SHAPES_QTY, sizeof( point_t *) );
156 if( !p_sys->ps_bezier_pts_H )
158 free(p_filter->p_sys);
159 p_filter->p_sys = NULL;
162 for (int32_t i_shape = 0; i_shape<SHAPES_QTY; i_shape++)
163 p_sys->ps_bezier_pts_H[i_shape] = puzzle_rand_bezier(7);
166 config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
169 vlc_mutex_init( &p_sys->lock );
170 vlc_mutex_init( &p_sys->pce_lock );
172 p_sys->s_new_param.i_rows =
173 var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "rows" );
174 p_sys->s_new_param.i_cols =
175 var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "cols" );
176 p_sys->s_new_param.i_border =
177 var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "border" );
178 p_sys->s_new_param.b_preview =
179 var_CreateGetBoolCommand( p_filter, CFG_PREFIX "preview" );
180 p_sys->s_new_param.i_preview_size =
181 var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "preview-size" );
182 p_sys->s_new_param.i_shape_size =
183 var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "shape-size" );
184 p_sys->s_new_param.i_auto_shuffle_speed =
185 var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "auto-shuffle" );
186 p_sys->s_new_param.i_auto_solve_speed =
187 var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "auto-solve" );
188 p_sys->s_new_param.i_rotate =
189 var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "rotation" );
190 p_sys->s_new_param.i_mode =
191 var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "mode" );
193 var_AddCallback( p_filter, CFG_PREFIX "rows", puzzle_Callback, p_sys );
194 var_AddCallback( p_filter, CFG_PREFIX "cols", puzzle_Callback, p_sys );
195 var_AddCallback( p_filter, CFG_PREFIX "border", puzzle_Callback, p_sys );
196 var_AddCallback( p_filter, CFG_PREFIX "preview", puzzle_Callback, p_sys );
197 var_AddCallback( p_filter, CFG_PREFIX "preview-size", puzzle_Callback, p_sys );
198 var_AddCallback( p_filter, CFG_PREFIX "shape-size", puzzle_Callback, p_sys );
199 var_AddCallback( p_filter, CFG_PREFIX "auto-shuffle", puzzle_Callback, p_sys );
200 var_AddCallback( p_filter, CFG_PREFIX "auto-solve", puzzle_Callback, p_sys );
201 var_AddCallback( p_filter, CFG_PREFIX "rotation", puzzle_Callback, p_sys );
202 var_AddCallback( p_filter, CFG_PREFIX "mode", puzzle_Callback, p_sys );
204 p_filter->pf_video_filter = Filter;
205 p_filter->pf_video_mouse = puzzle_mouse;
213 void Close( vlc_object_t *p_this ) {
214 filter_t *p_filter = (filter_t *)p_this;
215 filter_sys_t *p_sys = p_filter->p_sys;
217 var_DelCallback( p_filter, CFG_PREFIX "rows", puzzle_Callback, p_sys );
218 var_DelCallback( p_filter, CFG_PREFIX "cols", puzzle_Callback, p_sys );
219 var_DelCallback( p_filter, CFG_PREFIX "border", puzzle_Callback, p_sys );
220 var_DelCallback( p_filter, CFG_PREFIX "preview", puzzle_Callback, p_sys );
221 var_DelCallback( p_filter, CFG_PREFIX "preview-size", puzzle_Callback, p_sys );
222 var_DelCallback( p_filter, CFG_PREFIX "shape-size", puzzle_Callback, p_sys );
223 var_DelCallback( p_filter, CFG_PREFIX "auto-shuffle", puzzle_Callback, p_sys );
224 var_DelCallback( p_filter, CFG_PREFIX "auto-solve", puzzle_Callback, p_sys );
225 var_DelCallback( p_filter, CFG_PREFIX "rotation", puzzle_Callback, p_sys );
226 var_DelCallback( p_filter, CFG_PREFIX "mode", puzzle_Callback, p_sys );
228 vlc_mutex_destroy( &p_sys->lock );
229 vlc_mutex_destroy( &p_sys->pce_lock );
231 /* Free allocated memory */
232 puzzle_free_ps_puzzle_array ( p_filter );
233 puzzle_free_ps_pieces_shapes ( p_filter);
234 puzzle_free_ps_pieces ( p_filter );
235 free(p_sys->ps_desk_planes);
236 free(p_sys->ps_pict_planes);
237 free( p_sys->pi_order );
239 for (int32_t i_shape = 0; i_shape<SHAPES_QTY; i_shape++)
240 free(p_sys->ps_bezier_pts_H[i_shape]);
241 free(p_sys->ps_bezier_pts_H);
249 picture_t *Filter( filter_t *p_filter, picture_t *p_pic_in ) {
250 if( !p_pic_in || !p_filter) return NULL;
252 const video_format_t *p_fmt_in = &p_filter->fmt_in.video;
253 filter_sys_t *p_sys = p_filter->p_sys;
255 picture_t *p_pic_out = filter_NewPicture( p_filter );
257 picture_Release( p_pic_in );
262 p_sys->b_bake_request = false;
264 if ((p_sys->pi_order == NULL) || (p_sys->ps_desk_planes == NULL) || (p_sys->ps_pict_planes == NULL) || (p_sys->ps_puzzle_array == NULL) || (p_sys->ps_pieces == NULL))
265 p_sys->b_init = false;
267 if ((p_sys->ps_pieces_shapes == NULL) && p_sys->s_current_param.b_advanced && (p_sys->s_current_param.i_shape_size != 0))
268 p_sys->b_init = false;
270 /* assert initialized & allocated data match with current frame characteristics */
271 if ( p_sys->s_allocated.i_planes != p_pic_out->i_planes)
272 p_sys->b_init = false;
273 p_sys->s_current_param.i_planes = p_pic_out->i_planes;
274 if (p_sys->ps_pict_planes != NULL) {
275 for (uint8_t i_plane = 0; i_plane < p_sys->s_allocated.i_planes; i_plane++) {
276 if ( (p_sys->ps_pict_planes[i_plane].i_lines != p_pic_in->p[i_plane].i_visible_lines)
277 || (p_sys->ps_pict_planes[i_plane].i_width != p_pic_in->p[i_plane].i_visible_pitch / p_pic_in->p[i_plane].i_pixel_pitch)
278 || (p_sys->ps_desk_planes[i_plane].i_lines != p_pic_out->p[i_plane].i_visible_lines)
279 || (p_sys->ps_desk_planes[i_plane].i_width != p_pic_out->p[i_plane].i_visible_pitch / p_pic_out->p[i_plane].i_pixel_pitch) )
280 p_sys->b_init = false;
284 p_sys->s_current_param.i_pict_width = (int) p_pic_in->p[0].i_visible_pitch / p_pic_in->p[0].i_pixel_pitch;
285 p_sys->s_current_param.i_pict_height = (int) p_pic_in->p[0].i_visible_lines;
286 p_sys->s_current_param.i_desk_width = (int) p_pic_out->p[0].i_visible_pitch / p_pic_out->p[0].i_pixel_pitch;
287 p_sys->s_current_param.i_desk_height = (int) p_pic_out->p[0].i_visible_lines;
289 /* assert no mismatch between sizes */
290 if ( p_sys->s_current_param.i_pict_width != p_sys->s_current_param.i_desk_width
291 || p_sys->s_current_param.i_pict_height != p_sys->s_current_param.i_desk_height
292 || p_sys->s_current_param.i_pict_width != (int) p_fmt_in->i_width
293 || p_sys->s_current_param.i_pict_height != (int) p_fmt_in->i_height )
296 vlc_mutex_lock( &p_sys->lock );
298 /* check if we have to compute initial data */
299 if ( p_sys->b_change_param || p_sys->b_bake_request || !p_sys->b_init ) {
300 if ( p_sys->s_allocated.i_rows != p_sys->s_new_param.i_rows
301 || p_sys->s_allocated.i_cols != p_sys->s_new_param.i_cols
302 || p_sys->s_allocated.i_rotate != p_sys->s_new_param.i_rotate
303 || p_sys->s_allocated.i_mode != p_sys->s_new_param.i_mode
304 || p_sys->b_bake_request || !p_sys->b_init )
306 p_sys->b_bake_request = true;
307 p_sys->b_init = false;
308 p_sys->b_shuffle_rqst = true;
309 p_sys->b_shape_init = false;
312 if ( p_sys->s_current_param.i_border != p_sys->s_new_param.i_border
313 || p_sys->s_current_param.i_shape_size != p_sys->s_new_param.i_shape_size )
315 p_sys->b_bake_request = true;
316 p_sys->b_shape_init = false;
319 /* depending on the game selected, set associated internal flags */
320 switch ( p_sys->s_new_param.i_mode )
322 case 0: /* jigsaw puzzle */
323 p_sys->s_new_param.b_advanced = true;
324 p_sys->s_new_param.b_blackslot = false;
325 p_sys->s_new_param.b_near = false;
327 case 1: /* sliding puzzle */
328 p_sys->s_new_param.b_advanced = false;
329 p_sys->s_new_param.b_blackslot = true;
330 p_sys->s_new_param.b_near = true;
332 case 2: /* swap puzzle */
333 p_sys->s_new_param.b_advanced = false;
334 p_sys->s_new_param.b_blackslot = false;
335 p_sys->s_new_param.b_near = true;
337 case 3: /* exchange puzzle */
338 p_sys->s_new_param.b_advanced = false;
339 p_sys->s_new_param.b_blackslot = false;
340 p_sys->s_new_param.b_near = false;
343 p_sys->s_current_param.i_mode = p_sys->s_new_param.i_mode;
345 if ( p_sys->s_current_param.b_blackslot != p_sys->s_new_param.b_blackslot
346 && p_sys->i_selected == NO_PCE
347 && p_sys->s_current_param.b_blackslot )
348 p_sys->i_selected = 0;
350 if ( p_sys->s_current_param.i_auto_shuffle_speed != p_sys->s_new_param.i_auto_shuffle_speed )
351 p_sys->i_auto_shuffle_countdown_val = init_countdown(p_sys->s_new_param.i_auto_shuffle_speed);
353 if ( p_sys->s_current_param.i_auto_solve_speed != p_sys->s_new_param.i_auto_solve_speed )
354 p_sys->i_auto_solve_countdown_val = init_countdown(p_sys->s_current_param.i_auto_solve_speed);
356 p_sys->s_current_param.i_rows = p_sys->s_new_param.i_rows;
357 p_sys->s_current_param.i_cols = p_sys->s_new_param.i_cols;
358 p_sys->s_current_param.i_pieces_nbr = p_sys->s_current_param.i_rows * p_sys->s_current_param.i_cols;
359 p_sys->s_current_param.b_advanced = p_sys->s_new_param.b_advanced;
360 if (!p_sys->s_new_param.b_advanced) {
361 p_sys->s_current_param.b_blackslot = p_sys->s_new_param.b_blackslot;
362 p_sys->s_current_param.b_near = p_sys->s_new_param.b_near || p_sys->s_new_param.b_blackslot;
363 p_sys->s_current_param.i_border = 0;
364 p_sys->s_current_param.b_preview = false;
365 p_sys->s_current_param.i_preview_size= 0;
366 p_sys->s_current_param.i_shape_size = 0;
367 p_sys->s_current_param.i_auto_shuffle_speed = 0;
368 p_sys->s_current_param.i_auto_solve_speed = 0;
369 p_sys->s_current_param.i_rotate = 0;
373 p_sys->s_current_param.b_blackslot = false;
374 p_sys->s_current_param.b_near = false;
375 p_sys->s_current_param.i_border = p_sys->s_new_param.i_border;
376 p_sys->s_current_param.b_preview = p_sys->s_new_param.b_preview;
377 p_sys->s_current_param.i_preview_size = p_sys->s_new_param.i_preview_size;
378 p_sys->s_current_param.i_shape_size = p_sys->s_new_param.i_shape_size;
379 p_sys->s_current_param.i_auto_shuffle_speed = p_sys->s_new_param.i_auto_shuffle_speed;
380 p_sys->s_current_param.i_auto_solve_speed = p_sys->s_new_param.i_auto_solve_speed;
381 p_sys->s_current_param.i_rotate = p_sys->s_new_param.i_rotate;
383 p_sys->b_change_param = false;
386 vlc_mutex_unlock( &p_sys->lock );
388 /* generate initial puzzle data when needed */
389 if ( p_sys->b_bake_request ) {
390 if (!p_sys->b_shuffle_rqst) {
391 /* here we have to keep the same position
392 * we have to save locations before generating new data
394 save_game_t *ps_save_game = puzzle_save(p_filter);
396 return CopyInfoAndRelease( p_pic_out, p_pic_in );
397 i_ret = puzzle_bake( p_filter, p_pic_out, p_pic_in );
398 if ( i_ret != VLC_SUCCESS )
400 free(ps_save_game->ps_pieces);
402 return CopyInfoAndRelease( p_pic_out, p_pic_in );
404 puzzle_load( p_filter, ps_save_game);
405 free(ps_save_game->ps_pieces);
409 i_ret = puzzle_bake( p_filter, p_pic_out, p_pic_in );
410 if ( i_ret != VLC_SUCCESS )
411 return CopyInfoAndRelease( p_pic_out, p_pic_in );
415 /* shuffle the desk and generate pieces data */
416 if ( p_sys->b_shuffle_rqst && p_sys->b_init ) {
417 i_ret = puzzle_bake_piece ( p_filter );
418 if (i_ret != VLC_SUCCESS)
419 return CopyInfoAndRelease( p_pic_out, p_pic_in );
422 /* preset output pic */
423 if ( !p_sys->b_bake_request && !p_sys->b_shuffle_rqst && p_sys->b_init && !p_sys->b_finished )
424 puzzle_preset_desk_background(p_pic_out, 0, 127, 127);
426 /* copy src to dst during init & bake process */
427 for( uint8_t i_plane = 0; i_plane < p_pic_out->i_planes; i_plane++ )
428 memcpy( p_pic_out->p[i_plane].p_pixels, p_pic_in->p[i_plane].p_pixels,
429 p_pic_in->p[i_plane].i_pitch * (int32_t) p_pic_in->p[i_plane].i_visible_lines );
432 vlc_mutex_lock( &p_sys->pce_lock );
434 /* manage the game, adjust locations, groups and regenerate some corrupted data if any */
435 for (uint32_t i = 0; i < __MAX( 4, p_sys->s_allocated.i_pieces_nbr / 4 )
436 && ( !p_sys->b_bake_request && !p_sys->b_mouse_drag
437 && p_sys->b_init && p_sys->s_current_param.b_advanced ); i++)
439 puzzle_solve_pces_accuracy( p_filter );
442 for (uint32_t i = 0; i < __MAX( 4, p_sys->s_allocated.i_pieces_nbr / 4 )
443 && ( !p_sys->b_bake_request && !p_sys->b_mouse_drag
444 && p_sys->b_init && p_sys->s_current_param.b_advanced ); i++)
446 puzzle_solve_pces_group( p_filter );
449 if ( !p_sys->b_bake_request && !p_sys->b_mouse_drag && p_sys->b_init
450 && p_sys->s_current_param.b_advanced )
451 puzzle_count_pce_group( p_filter);
452 if ( !p_sys->b_bake_request && !p_sys->b_mouse_drag && p_sys->b_init
453 && p_sys->s_current_param.b_advanced ) {
454 i_ret = puzzle_sort_layers( p_filter);
455 if (i_ret != VLC_SUCCESS)
457 vlc_mutex_unlock( &p_sys->pce_lock );
458 return CopyInfoAndRelease( p_pic_out, p_pic_in );
462 for (uint32_t i = 0; i < __MAX( 4, p_sys->s_allocated.i_pieces_nbr / 24 )
463 && ( !p_sys->b_bake_request && !p_sys->b_mouse_drag
464 && p_sys->b_init && p_sys->s_current_param.b_advanced ); i++)
466 p_sys->i_calc_corn_loop++;
467 p_sys->i_calc_corn_loop %= p_sys->s_allocated.i_pieces_nbr;
468 puzzle_calculate_corners( p_filter, p_sys->i_calc_corn_loop );
471 /* computer moves some piece depending on auto_solve and auto_shuffle param */
472 if ( !p_sys->b_bake_request && !p_sys->b_mouse_drag && p_sys->b_init
473 && p_sys->ps_puzzle_array != NULL && p_sys->s_current_param.b_advanced )
475 puzzle_auto_shuffle( p_filter );
476 puzzle_auto_solve( p_filter );
479 vlc_mutex_unlock( &p_sys->pce_lock );
481 /* draw output pic */
482 if ( !p_sys->b_bake_request && p_sys->b_init && p_sys->ps_puzzle_array != NULL ) {
484 puzzle_draw_borders(p_filter, p_pic_in, p_pic_out);
486 p_sys->i_pointed_pce = NO_PCE;
487 puzzle_draw_pieces(p_filter, p_pic_in, p_pic_out);
489 /* when puzzle_draw_pieces() has not updated p_sys->i_pointed_pce,
490 * use puzzle_find_piece to define the piece pointed by the mouse
492 if (p_sys->i_pointed_pce == NO_PCE)
493 p_sys->i_mouse_drag_pce = puzzle_find_piece( p_filter, p_sys->i_mouse_x, p_sys->i_mouse_y, -1);
495 p_sys->i_mouse_drag_pce = p_sys->i_pointed_pce;
497 if (p_sys->s_current_param.b_preview )
498 puzzle_draw_preview(p_filter, p_pic_in, p_pic_out);
500 /* highlight the selected piece when not playing jigsaw mode */
501 if ( p_sys->i_selected != NO_PCE && !p_sys->s_current_param.b_blackslot
502 && !p_sys->s_current_param.b_advanced )
504 int32_t c = (p_sys->i_selected % p_sys->s_allocated.i_cols);
505 int32_t r = (p_sys->i_selected / p_sys->s_allocated.i_cols);
507 puzzle_draw_rectangle(p_pic_out,
508 p_sys->ps_puzzle_array[r][c][0].i_x,
509 p_sys->ps_puzzle_array[r][c][0].i_y,
510 p_sys->ps_puzzle_array[r][c][0].i_width,
511 p_sys->ps_puzzle_array[r][c][0].i_lines,
515 /* draw the blackslot when playing sliding puzzle mode */
516 if ( p_sys->i_selected != NO_PCE && p_sys->s_current_param.b_blackslot
517 && !p_sys->s_current_param.b_advanced )
519 int32_t c = (p_sys->i_selected % p_sys->s_allocated.i_cols);
520 int32_t r = (p_sys->i_selected / p_sys->s_allocated.i_cols);
522 puzzle_fill_rectangle(p_pic_out,
523 p_sys->ps_puzzle_array[r][c][0].i_x,
524 p_sys->ps_puzzle_array[r][c][0].i_y,
525 p_sys->ps_puzzle_array[r][c][0].i_width,
526 p_sys->ps_puzzle_array[r][c][0].i_lines,
530 /* Draw the 'puzzle_shuffle' button if the puzzle is finished */
531 if ( p_sys->b_finished )
532 puzzle_draw_sign(p_pic_out, 0, 0, SHUFFLE_WIDTH, SHUFFLE_LINES, ppsz_shuffle_button, false);
534 /* draw an arrow at mouse pointer to indicate current action (rotation...) */
535 if ((p_sys->i_mouse_drag_pce != NO_PCE) && !p_sys->b_mouse_drag
536 && !p_sys->b_finished && p_sys->s_current_param.b_advanced )
538 vlc_mutex_lock( &p_sys->pce_lock );
542 if (p_sys->s_current_param.i_rotate != 3)
544 else if ( (p_sys->ps_pieces[p_sys->i_mouse_drag_pce].i_actual_angle & 1) == 0)
545 i_delta_x = p_sys->ps_desk_planes[0].i_pce_max_width / 6;
547 i_delta_x = p_sys->ps_desk_planes[0].i_pce_max_lines / 6;
549 if (p_sys->s_current_param.i_rotate == 0)
550 p_sys->i_mouse_action = 0;
551 else if (p_sys->s_current_param.i_rotate == 1)
552 p_sys->i_mouse_action = 2;
553 else if ( p_sys->i_mouse_x >= ( p_sys->ps_pieces[p_sys->i_mouse_drag_pce].i_center_x + i_delta_x) )
554 p_sys->i_mouse_action = -1; /* rotate counterclockwise */
555 else if ( p_sys->i_mouse_x <= ( p_sys->ps_pieces[p_sys->i_mouse_drag_pce].i_center_x - i_delta_x) )
556 p_sys->i_mouse_action = +1;
558 p_sys->i_mouse_action = 4; /* center click: only mirror */
560 if ( p_sys->i_mouse_action == +1 )
561 puzzle_draw_sign(p_pic_out, p_sys->i_mouse_x - ARROW_WIDTH,
562 p_sys->i_mouse_y, ARROW_WIDTH, ARROW_LINES, ppsz_rot_arrow_sign, false);
563 else if ( p_sys->i_mouse_action == -1 )
564 puzzle_draw_sign(p_pic_out, p_sys->i_mouse_x - ARROW_WIDTH,
565 p_sys->i_mouse_y, ARROW_WIDTH, ARROW_LINES, ppsz_rot_arrow_sign, true);
566 else if ( p_sys->i_mouse_action == 4 )
567 puzzle_draw_sign(p_pic_out, p_sys->i_mouse_x - ARROW_WIDTH,
568 p_sys->i_mouse_y, ARROW_WIDTH, ARROW_LINES, ppsz_mir_arrow_sign, false);
570 vlc_mutex_unlock( &p_sys->pce_lock );
574 return CopyInfoAndRelease( p_pic_out, p_pic_in );
577 /*****************************************************************************
579 *****************************************************************************/
580 int puzzle_Callback( vlc_object_t *p_this, char const *psz_var,
581 vlc_value_t oldval, vlc_value_t newval,
584 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
585 filter_sys_t *p_sys = (filter_sys_t *)p_data;
587 vlc_mutex_lock( &p_sys->lock );
588 if( !strcmp( psz_var, CFG_PREFIX "rows" ) ) {
589 p_sys->s_new_param.i_rows = __MAX( 1, newval.i_int );
591 else if( !strcmp( psz_var, CFG_PREFIX "cols" ) ) {
592 p_sys->s_new_param.i_cols = __MAX( 1, newval.i_int );
594 else if( !strcmp( psz_var, CFG_PREFIX "border" ) ) {
595 p_sys->s_new_param.i_border = __MAX( 0, newval.i_int );
597 else if( !strcmp( psz_var, CFG_PREFIX "preview" ) ) {
598 p_sys->s_new_param.b_preview = newval.b_bool;
600 else if( !strcmp( psz_var, CFG_PREFIX "preview-size" ) ) {
601 p_sys->s_new_param.i_preview_size = newval.i_int;
603 else if( !strcmp( psz_var, CFG_PREFIX "shape-size" ) ) {
604 p_sys->s_new_param.i_shape_size = newval.i_int;
606 else if( !strcmp( psz_var, CFG_PREFIX "auto-shuffle" ) ) {
607 p_sys->s_new_param.i_auto_shuffle_speed = newval.i_int;
609 else if( !strcmp( psz_var, CFG_PREFIX "auto-solve" ) ) {
610 p_sys->s_new_param.i_auto_solve_speed = newval.i_int;
612 else if( !strcmp( psz_var, CFG_PREFIX "rotation" ) ) {
613 p_sys->s_new_param.i_rotate = newval.i_int;
615 else if( !strcmp( psz_var, CFG_PREFIX "mode" ) ) {
616 p_sys->s_new_param.i_mode = newval.i_int;
619 p_sys->b_change_param = true;
620 vlc_mutex_unlock( &p_sys->lock );
626 int puzzle_mouse( filter_t *p_filter, vlc_mouse_t *p_mouse,
627 const vlc_mouse_t *p_old, const vlc_mouse_t *p_new )
629 filter_sys_t *p_sys = p_filter->p_sys;
630 const video_format_t *p_fmt_in = &p_filter->fmt_in.video;
632 /* Only take events inside the puzzle area */
633 if( p_new->i_x < 0 || p_new->i_x >= (int)p_fmt_in->i_width ||
634 p_new->i_y < 0 || p_new->i_y >= (int)p_fmt_in->i_height )
637 if (! p_sys->b_init || p_sys->b_change_param) {
642 p_sys->i_mouse_x = p_new->i_x;
643 p_sys->i_mouse_y = p_new->i_y;
645 /* If the puzzle is finished, shuffle it if needed */
646 if( p_sys->b_finished ) {
647 p_sys->b_mouse_drag = false;
648 p_sys->b_mouse_mvt = false;
649 if( vlc_mouse_HasPressed( p_old, p_new, MOUSE_BUTTON_LEFT ) &&
650 p_new->i_x < SHUFFLE_WIDTH && p_new->i_y < SHUFFLE_LINES )
652 p_sys->b_shuffle_rqst = true;
657 /* otherwise we can forward the mouse */
663 if ( !p_sys->s_current_param.b_advanced ) {
664 /* "square" game mode (sliding puzzle, swap...) */
665 const bool b_clicked = vlc_mouse_HasPressed( p_old, p_new, MOUSE_BUTTON_LEFT );
670 const int32_t i_border_width = p_fmt_in->i_width * p_sys->s_current_param.i_border / 100 / 2;
671 const int32_t i_border_height = p_fmt_in->i_height * p_sys->s_current_param.i_border / 100 / 2;
672 const int32_t i_pos_x = (p_new->i_x - i_border_width) * p_sys->s_allocated.i_cols / (p_fmt_in->i_width - 2*i_border_width);
673 const int32_t i_pos_y = (p_new->i_y - i_border_height) * p_sys->s_allocated.i_rows / (p_fmt_in->i_height - 2*i_border_height);
675 const int32_t i_pos = i_pos_y * p_sys->s_allocated.i_cols + i_pos_x;
676 p_sys->i_mouse_drag_pce = i_pos;
678 /* do not take into account if border clicked */
679 if ((p_new->i_x <= i_border_width) || (p_new->i_y <= i_border_height) || (p_new->i_x >= (int) p_fmt_in->i_width - i_border_width) || (p_new->i_y >= (int) p_fmt_in->i_height - i_border_height ) )
684 else if( p_sys->i_selected == NO_PCE )
685 p_sys->i_selected = i_pos;
686 else if( p_sys->i_selected == i_pos && !p_sys->s_current_param.b_blackslot )
687 p_sys->i_selected = -1;
688 else if( ( p_sys->i_selected == i_pos + 1 && p_sys->i_selected%p_sys->s_allocated.i_cols != 0 )
689 || ( p_sys->i_selected == i_pos - 1 && i_pos % p_sys->s_allocated.i_cols != 0 )
690 || p_sys->i_selected == i_pos + p_sys->s_allocated.i_cols
691 || p_sys->i_selected == i_pos - p_sys->s_allocated.i_cols
692 || !p_sys->s_current_param.b_near )
695 /* Swap two pieces */
696 int32_t a = p_sys->pi_order[ p_sys->i_selected ];
697 p_sys->pi_order[ p_sys->i_selected ] = p_sys->pi_order[ i_pos ];
698 p_sys->pi_order[ i_pos ] = a;
700 /* regen piece location from updated pi_order */
701 if ( p_sys->ps_pieces != NULL && p_sys->pi_order != NULL )
704 for (int32_t row = 0; row < p_sys->s_allocated.i_rows; row++) {
705 for (int32_t col = 0; col < p_sys->s_allocated.i_cols; col++) {
706 int32_t orow = p_sys->pi_order[i] / (p_sys->s_allocated.i_cols);
707 int32_t ocol = p_sys->pi_order[i] % (p_sys->s_allocated.i_cols);
709 p_sys->ps_pieces[i].i_original_row = orow;
710 p_sys->ps_pieces[i].i_original_col = ocol;
711 p_sys->ps_pieces[i].i_top_shape = 0;
712 p_sys->ps_pieces[i].i_btm_shape = 0;
713 p_sys->ps_pieces[i].i_right_shape = 0;
714 p_sys->ps_pieces[i].i_left_shape = 0;
715 p_sys->ps_pieces[i].i_actual_angle = 0;
716 p_sys->ps_pieces[i].i_actual_mirror = +1;
717 p_sys->ps_pieces[i].b_overlap = false;
718 p_sys->ps_pieces[i].b_finished = false;
719 p_sys->ps_pieces[i].i_group_ID = i;
721 for (uint8_t i_plane = 0; i_plane < p_sys->s_allocated.i_planes; i_plane++) {
722 p_sys->ps_pieces[i].ps_piece_in_plane[i_plane].i_width = p_sys->ps_puzzle_array[row][col][i_plane].i_width;
723 p_sys->ps_pieces[i].ps_piece_in_plane[i_plane].i_lines = p_sys->ps_puzzle_array[row][col][i_plane].i_lines;
724 p_sys->ps_pieces[i].ps_piece_in_plane[i_plane].i_original_x = p_sys->ps_puzzle_array[orow][ocol][i_plane].i_x;
725 p_sys->ps_pieces[i].ps_piece_in_plane[i_plane].i_original_y = p_sys->ps_puzzle_array[orow][ocol][i_plane].i_y;
726 p_sys->ps_pieces[i].ps_piece_in_plane[i_plane].i_actual_x = p_sys->ps_puzzle_array[row][col][i_plane].i_x;
727 p_sys->ps_pieces[i].ps_piece_in_plane[i_plane].i_actual_y = p_sys->ps_puzzle_array[row][col][i_plane].i_y;
734 p_sys->i_selected = p_sys->s_current_param.b_blackslot ? i_pos : NO_PCE;
735 p_sys->b_finished = puzzle_is_finished( p_sys, p_sys->pi_order );
739 else /* jigsaw puzzle mode */
741 if ((p_sys->ps_desk_planes == NULL) || (p_sys->ps_pict_planes == NULL) || (p_sys->ps_puzzle_array == NULL) || (p_sys->ps_pieces == NULL)) {
746 if( vlc_mouse_HasPressed( p_old, p_new, MOUSE_BUTTON_LEFT ) )
749 vlc_mutex_lock( &p_sys->pce_lock );
751 if (p_sys->i_mouse_drag_pce != NO_PCE) {
752 int i_ret = puzzle_piece_foreground( p_filter, p_sys->i_mouse_drag_pce);
753 if (i_ret != VLC_SUCCESS)
755 vlc_mutex_unlock( &p_sys->pce_lock );
758 p_sys->i_mouse_drag_pce = 0;
760 uint32_t i_group_ID = p_sys->ps_pieces[0].i_group_ID;
761 for (uint32_t i = 0; i < p_sys->s_allocated.i_pieces_nbr; i++) {
762 if ( i_group_ID == p_sys->ps_pieces[i].i_group_ID ) {
763 p_sys->ps_pieces[i].b_finished = false;
770 p_sys->b_mouse_drag = true;
771 p_sys->b_mouse_mvt = false;
774 /* player click an empty area then search a piece which is overlapping another one and place it here */
775 p_sys->b_mouse_drag = false;
776 for (uint32_t i = 0; i < p_sys->s_allocated.i_pieces_nbr; i++)
777 if ( p_sys->ps_pieces[i].b_overlap ) {
778 puzzle_move_group( p_filter, i, p_new->i_x - p_sys->ps_pieces[i].i_center_x, p_new->i_y - p_sys->ps_pieces[i].i_center_y );
779 p_sys->ps_pieces[i].b_overlap = false;
782 p_sys->b_mouse_drag = false;
785 vlc_mutex_unlock( &p_sys->pce_lock );
788 else if( vlc_mouse_HasReleased( p_old, p_new, MOUSE_BUTTON_LEFT ) )
790 if ( !p_sys->b_mouse_mvt && p_sys->b_mouse_drag ) {
791 /* piece clicked without any mouse mvt => rotate it or mirror */
792 if ( p_sys->s_current_param.i_rotate != 0) {
793 vlc_mutex_lock( &p_sys->pce_lock );
795 uint32_t i_group_ID = p_sys->ps_pieces[0].i_group_ID;
797 for (uint32_t i = 0; i < p_sys->s_allocated.i_pieces_nbr; i++)
798 if ( i_group_ID == p_sys->ps_pieces[i].i_group_ID )
799 puzzle_rotate_pce( p_filter, i, p_sys->i_mouse_action, p_sys->ps_pieces[0].i_center_x, p_sys->ps_pieces[0].i_center_y, p_sys->i_mouse_action != 4 ? true : false );
801 vlc_mutex_unlock( &p_sys->pce_lock );
804 p_sys->b_mouse_drag = false;
805 p_sys->b_mouse_mvt = false;
807 else /* no action on left button */
809 /* check if the mouse is in the preview area */
810 switch ( p_sys->i_preview_pos )
813 if ( p_new->i_x < (int)p_fmt_in->i_width / 2 && p_new->i_y < (int)p_fmt_in->i_height / 2 )
814 p_sys->i_preview_pos++;
817 if ( p_new->i_x > (int)p_fmt_in->i_width / 2 && p_new->i_y < (int)p_fmt_in->i_height / 2 )
818 p_sys->i_preview_pos++;
821 if ( p_new->i_x > (int)p_fmt_in->i_width / 2 && p_new->i_y > (int)p_fmt_in->i_height / 2 )
822 p_sys->i_preview_pos++;
825 if ( p_new->i_x < (int)p_fmt_in->i_width / 2 && p_new->i_y > (int)p_fmt_in->i_height / 2 )
826 p_sys->i_preview_pos++;
829 p_sys->i_preview_pos %= 4;
831 if ( !vlc_mouse_IsLeftPressed( p_new ) )
832 p_sys->b_mouse_drag = false;
835 vlc_mouse_GetMotion( &i_dx, &i_dy, p_old, p_new );
836 if ( i_dx != 0 || i_dy != 0 )
837 p_sys->b_mouse_mvt = true;
839 if (p_sys->b_mouse_drag) {
840 if ( ( p_new->i_x <= 0 ) || ( p_new->i_y <= 0 ) || ( p_new->i_x >= (int) p_fmt_in->i_width )
841 || ( p_new->i_y >= (int) p_fmt_in->i_height ) )
843 /* if the mouse is outside the window, stop moving the piece/group */
844 p_sys->b_mouse_drag = false;
845 p_sys->b_mouse_mvt = true;
847 else if ( i_dx != 0 || i_dy != 0 )
849 vlc_mutex_lock( &p_sys->pce_lock );
851 puzzle_move_group( p_filter, p_sys->i_mouse_drag_pce, i_dx, i_dy);
853 vlc_mutex_unlock( &p_sys->pce_lock );