puzzle-view.vala 15.7 KB
Newer Older
1 2
/* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
Robert Ancell's avatar
Robert Ancell committed
3 4 5 6 7 8 9 10 11
 * Copyright (C) 2010-2013 Robert Ancell
 *
 * 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. See http://www.gnu.org/copyleft/gpl.html the full text of the
 * license.
 */

12
private class TileImage : Object
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
{
    /* Tile being moved */
    public Tile tile;

    /* Location of tile */
    public double x;
    public double y;

    /* Co-ordinates to move from */
    public double source_x;
    public double source_y;

    /* Time started moving */
    public double source_time;

    /* Co-ordinates to target for */
    public double target_x;
    public double target_y;

    /* Duration of movement */
    public double duration;

    public TileImage (Tile tile)
    {
        this.tile = tile;
    }
}

public class PuzzleView : Gtk.DrawingArea
{
    /* Minimum size of a tile */
Michael Catanzaro's avatar
Michael Catanzaro committed
44
    private const int minimum_size = 80;
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

    /* Puzzle being rendered */
    private Puzzle? _puzzle = null;
    public Puzzle puzzle
    {
        get { return _puzzle; }
        set
        {
            if (_puzzle != null)
                SignalHandler.disconnect_by_func (_puzzle, null, this);
            _puzzle = value;
            tiles.remove_all ();
            for (var y = 0; y < puzzle.size; y++)
            {
                for (var x = 0; x < puzzle.size * 2; x++)
                {
                    var tile = puzzle.get_tile (x, y);
                    if (tile == null)
                        continue;

                    var image = new TileImage (tile);
                    move_tile_to_location (image, x, y);
                    tiles.insert (tile, image);
                }
            }
            _puzzle.tile_moved.connect (tile_moved_cb);
71
            _puzzle.paused_changed.connect (() => { queue_draw (); });
72 73 74
            queue_resize ();
        }
    }
Sahil Sareen's avatar
Sahil Sareen committed
75

76 77 78 79 80 81
    /* Theme */
    private Theme theme;

    /* Tile being controlled by the mouse */
    private TileImage? selected_tile = null;

82 83 84
    /* Timeout to detect if a click is a selection or a drag */
    private uint selection_timeout = 0;

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    /* The position inside the tile where the cursor is */
    private double selected_x_offset;
    private double selected_y_offset;

    /* Tile images */
    private HashTable<Tile, TileImage> tiles;

    /* Animation timer */
    private Timer animation_timer;
    private uint animation_timeout = 0;

    public PuzzleView ()
    {
        set_events (Gdk.EventMask.EXPOSURE_MASK | Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.POINTER_MOTION_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK);

        tiles = new HashTable <Tile, TileImage> (direct_hash, direct_equal);
Sahil Sareen's avatar
Sahil Sareen committed
101

102 103 104 105 106
        theme = new Theme ();

        animation_timer = new Timer ();
        animation_timer.start ();
    }
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

    public bool game_in_progress
    {
        get
        {
            if (puzzle.is_solved)
                return false;

            for (uint y = 0; y < puzzle.size; y++)
            {
                for (uint x = puzzle.size; x < puzzle.size * 2; x++)
                {
                    if (puzzle.get_tile (x, y) == null)
                        return true;
                }
            }
            return false;
        }
    }
Sahil Sareen's avatar
Sahil Sareen committed
126

127 128 129 130 131
    private void redraw_tile (TileImage image)
    {
        uint x_offset, y_offset, size, gap;
        get_dimensions (out x_offset, out y_offset, out size, out gap);

Sahil Sareen's avatar
Sahil Sareen committed
132
        queue_draw_area ((int) (image.x + 0.5), (int) (image.y + 0.5), (int) size, (int) size);
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    }

    private void move_tile_to_location (TileImage image, uint x, uint y, double duration = 0)
    {
        uint x_offset, y_offset, size, gap;
        get_dimensions (out x_offset, out y_offset, out size, out gap);

        var target_x = x_offset + x * size;
        if (x >= puzzle.size)
            target_x += gap;
        var target_y = y_offset + y * size;
        move_tile (image, target_x, target_y, duration);
    }

    private void move_tile (TileImage image, double x, double y, double duration = 0)
    {
        if (image.x == x && image.y == y)
            return;
Sahil Sareen's avatar
Sahil Sareen committed
151

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
        image.source_x = image.x;
        image.source_y = image.y;
        image.source_time = animation_timer.elapsed ();
        image.target_x = x;
        image.target_y = y;
        image.duration = duration;

        /* Move immediately */
        if (duration == 0)
        {
            redraw_tile (image);
            image.x = image.target_x;
            image.y = image.target_y;
            redraw_tile (image);
            return;
        }

        /* Start animation (maximum of 100fps) */
        if (animation_timeout == 0)
            animation_timeout = Timeout.add (10, animate_cb);
Sahil Sareen's avatar
Sahil Sareen committed
172 173
    }

174 175 176
    private bool animate_cb ()
    {
        var t = animation_timer.elapsed ();
Sahil Sareen's avatar
Sahil Sareen committed
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
        uint x_offset, y_offset, size, gap;
        get_dimensions (out x_offset, out y_offset, out size, out gap);

        var animating = false;
        var iter = HashTableIter<Tile, TileImage> (tiles);
        while (true)
        {
            Tile tile;
            TileImage image;
            if (!iter.next (out tile, out image))
                break;

            if (image.x == image.target_x && image.y == image.target_y)
                continue;

            /* Redraw where the tile was */
            redraw_tile (image);

            /* Move the tile */
            if (t >= image.source_time + image.duration)
            {
                image.x = image.target_x;
                image.y = image.target_y;
            }
            else
            {
                var d = (t - image.source_time) / image.duration;
                image.x = image.source_x + (image.target_x - image.source_x) * d;
                image.y = image.source_y + (image.target_y - image.source_y) * d;
                animating = true;
            }

            /* Draw where the tile is */
            redraw_tile (image);
        }

        /* Keep animating if still have tiles */
        if (animating)
            return true;

        animation_timeout = 0;
        return false;
    }

    public override void get_preferred_width (out int minimum, out int natural)
    {
        var size = 0;
        if (puzzle != null)
            size = (int) ((puzzle.size * 2 + 1.5) * minimum_size);
        minimum = natural = int.max (size, 500);
    }

    public override void get_preferred_height (out int minimum, out int natural)
    {
        var size = 0;
        if (puzzle != null)
            size = (int) ((puzzle.size + 1) * minimum_size);
        minimum = natural = int.max (size, 300);
    }
Sahil Sareen's avatar
Sahil Sareen committed
237

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    private void get_dimensions (out uint x, out uint y, out uint size, out uint gap)
    {
        /* Fit in with a half tile border and spacing between boards */
        var width = (uint) (get_allocated_width () / (2 * puzzle.size + 1.5));
        var height = (uint) (get_allocated_height () / (puzzle.size + 1));
        size = uint.min (width, height);
        gap = size / 2;
        x = (get_allocated_width () - 2 * puzzle.size * size - gap) / 2;
        y = (get_allocated_height () - puzzle.size * size) / 2;
    }

    private void tile_moved_cb (Puzzle puzzle, Tile tile, uint x, uint y)
    {
        move_tile_to_location (tiles.lookup (tile), x, y, 0.2);
    }
Sahil Sareen's avatar
Sahil Sareen committed
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
    public override bool configure_event (Gdk.EventConfigure event)
    {
        /* Move everything to its correct location */
        var iter = HashTableIter<Tile, TileImage> (tiles);
        while (true)
        {
            Tile tile;
            TileImage image;
            if (!iter.next (out tile, out image))
                break;
            uint x, y;
            puzzle.get_tile_location (tile, out x, out y);
            move_tile_to_location (image, x, y);
        }
        selected_tile = null;

        return false;
    }

    public override bool draw (Cairo.Context context)
    {
        if (puzzle == null)
            return false;

        uint x_offset, y_offset, size, gap;
        get_dimensions (out x_offset, out y_offset, out size, out gap);

        /* Draw arrow */
        context.save ();
        var w = gap * 0.5;
        var ax = x_offset + puzzle.size * size + (gap - w) * 0.5;
        var ay = y_offset + puzzle.size * size * 0.5;
        context.translate (ax, ay);
        theme.draw_arrow (context, size, gap);
        context.restore ();

        /* Draw sockets */
        for (var y = 0; y < puzzle.size; y++)
        {
            for (var x = 0; x < puzzle.size * 2; x++)
            {
                context.save ();
                if (x >= puzzle.size)
                    context.translate (x_offset + gap + x * size, y_offset + y * size);
                else
                    context.translate (x_offset + x * size, y_offset + y * size);
                theme.draw_socket (context, size);
                context.restore ();
            }
        }

        /* Draw stationary tiles */
        var iter = HashTableIter<Tile, TileImage> (tiles);
        while (true)
        {
            Tile tile;
            TileImage image;
            if (!iter.next (out tile, out image))
                break;

            if (image == selected_tile || image.x != image.target_x || image.y != image.target_y)
                continue;

            context.save ();
            context.translate ((int) (image.x + 0.5), (int) (image.y + 0.5));
319
            if (puzzle.paused)
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
                theme.draw_paused_tile (context, size);
            else
                theme.draw_tile (context, size, tile);
            context.restore ();
        }

        /* Draw moving tiles */
        iter = HashTableIter<Tile, TileImage> (tiles);
        while (true)
        {
            Tile tile;
            TileImage image;
            if (!iter.next (out tile, out image))
                break;

            if (image != selected_tile && image.x == image.target_x && image.y == image.target_y)
                continue;

            context.save ();
            context.translate ((int) (image.x + 0.5), (int) (image.y + 0.5));
340
            if (puzzle.paused)
341 342 343 344 345 346 347
                theme.draw_paused_tile (context, size);
            else
                theme.draw_tile (context, size, tile);
            context.restore ();
        }

        /* Draw pause overlay */
348
        if (puzzle.paused)
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
        {
            context.set_source_rgba (0, 0, 0, 0.75);
            context.paint ();

            context.select_font_face ("Sans", Cairo.FontSlant.NORMAL, Cairo.FontWeight.BOLD);
            context.set_font_size (get_allocated_width () * 0.125);

            var text = _("Paused");
            Cairo.TextExtents extents;
            context.text_extents (text, out extents);
            context.move_to ((get_allocated_width () - extents.width) / 2.0, (get_allocated_height () + extents.height) / 2.0);
            context.set_source_rgb (1, 1, 1);
            context.show_text (text);
        }

        return false;
    }
Sahil Sareen's avatar
Sahil Sareen committed
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
    private void pick_tile (double x, double y)
    {
        if (selected_tile != null)
            return;

        if (puzzle.is_solved)
            return;

        uint x_offset, y_offset, size, gap;
        get_dimensions (out x_offset, out y_offset, out size, out gap);

        var iter = HashTableIter<Tile, TileImage> (tiles);
        while (true)
        {
            Tile tile;
            TileImage image;
            if (!iter.next (out tile, out image))
                break;

            if (x >= image.x && x <= image.x + size && y >= image.y && y <= image.y + size)
            {
                selected_tile = image;
                selected_x_offset = x - image.x;
                selected_y_offset = y - image.y;
391 392 393 394

                if (selection_timeout != 0)
                    Source.remove (selection_timeout);
                selection_timeout = Timeout.add (200, selection_timeout_cb);
395 396 397
            }
        }
    }
Sahil Sareen's avatar
Sahil Sareen committed
398

399 400 401 402 403
    private bool selection_timeout_cb ()
    {
        selection_timeout = 0;
        return false;
    }
404

405 406 407 408 409 410 411 412
    private bool on_right_half (double x)
    {
        uint x_offset, y_offset, size, gap;
        get_dimensions (out x_offset, out y_offset, out size, out gap);

        return x > x_offset + size * puzzle.size + gap * 0.5;
    }

413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
    private void drop_tile (double x, double y)
    {
        if (selected_tile == null)
            return;

        uint x_offset, y_offset, size, gap;
        get_dimensions (out x_offset, out y_offset, out size, out gap);

        /* Select from the middle of the tile */
        x += size * 0.5 - selected_x_offset;
        y += size * 0.5 - selected_x_offset;

        var tile_y = (int) Math.floor ((y - y_offset) / size);
        tile_y = int.max (tile_y, 0);
        tile_y = int.min (tile_y, (int) puzzle.size - 1);

        /* Check which side we are on */
        int tile_x;
431
        if (on_right_half (x))
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
        {
            tile_x = (int) puzzle.size + (int) Math.floor ((x - (x_offset + puzzle.size * size + gap)) / size);
            tile_x = int.max (tile_x, (int) puzzle.size);
            tile_x = int.min (tile_x, 2 * (int) puzzle.size - 1);
        }
        else
        {
            tile_x = (int) Math.floor ((x - x_offset) / size);
            tile_x = int.max (tile_x, 0);
            tile_x = int.min (tile_x, (int) puzzle.size - 1);
        }

        /* Drop the tile here, or move it back if can't */
        uint selected_x, selected_y;
        puzzle.get_tile_location (selected_tile.tile, out selected_x, out selected_y);
        if (puzzle.can_switch (selected_x, selected_y, (uint) tile_x, (uint) tile_y))
            puzzle.switch_tiles (selected_x, selected_y, (uint) tile_x, (uint) tile_y);
        else
            move_tile_to_location (selected_tile, selected_x, selected_y, 0.2);
        selected_tile = null;
    }

454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
    private void move_tile_to_right_half (TileImage image)
    {
        /* Pick the first open spot on the right side of the board */
        for (uint y = 0; y < puzzle.size; y++)
        {
            for (uint x = puzzle.size; x < puzzle.size * 2; x++)
            {
                if (puzzle.get_tile (x, y) == null)
                {
                    uint source_x, source_y;
                    puzzle.get_tile_location (image.tile, out source_x, out source_y);
                    puzzle.switch_tiles (source_x, source_y, x, y);
                    return;
                }
            }
        }
        assert_not_reached ();
    }

473 474
    public override bool button_press_event (Gdk.EventButton event)
    {
475
        if (puzzle.paused)
476 477 478 479
            return false;

        if (event.button == 1)
        {
480 481 482 483 484 485 486 487 488 489
            if (event.type == Gdk.EventType.BUTTON_PRESS)
            {
                if (selected_tile == null)
                    pick_tile (event.x, event.y);
                else if (selected_tile != null)
                    drop_tile (event.x, event.y);
            }
            else if (event.type == Gdk.EventType.DOUBLE_BUTTON_PRESS)
            {
                /* Move tile from left to right on double click */
490
                pick_tile (event.x, event.y);
491 492 493 494
                if (selected_tile != null && !on_right_half (selected_tile.x))
                    move_tile_to_right_half (selected_tile);
                selected_tile = null;
            }
495 496 497 498 499 500 501
        }

        return false;
    }

    public override bool button_release_event (Gdk.EventButton event)
    {
502
        if (event.button == 1 && selected_tile != null && selection_timeout == 0)
503
            drop_tile (event.x, event.y);
Sahil Sareen's avatar
Sahil Sareen committed
504

505 506 507 508
        if (selection_timeout != 0)
            Source.remove (selection_timeout);
        selection_timeout = 0;

509 510 511 512 513 514 515 516 517 518 519
        return false;
    }

    public override bool motion_notify_event (Gdk.EventMotion event)
    {
        if (selected_tile != null)
            move_tile (selected_tile, (int) (event.x - selected_x_offset), (int) (event.y - selected_y_offset));

        return false;
    }
}