File: puzzle-view.vala

package info (click to toggle)
gnome-klotski 1:3.32.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,500 kB
  • sloc: xml: 101; python: 11; sh: 7; makefile: 6
file content (294 lines) | stat: -rw-r--r-- 9,477 bytes parent folder | download
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
/*
 * 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 3 of the License, or (at your option) any later
 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
 * license.
 */

public class PuzzleView : Gtk.DrawingArea
{
    private const int SPACE_OFFSET = 4;
    private const int SPACE_PADDING = 5;
    private const int THEME_OVERLAY_SIZE = 8;
    private const int THEME_TILE_SEGMENTS = 27;
    private const int THEME_TILE_CENTER = 14;
    private const int THEME_TILE_SIZE = 34;

    private int render_size = 0;

    private int piece_x = 0;
    private int piece_y = 0;
    private bool piece_unmoved = false;

    private char _piece_id = '\0';
    private char piece_id
    {
        get { return _piece_id; }
        set { _piece_id = value; queue_draw (); }
    }

    private char last_piece_id = '\0';

    private double kx = 0;
    private double ky = 0;

    private Rsvg.Handle tiles_handle = null;
    private File image_file = null;
    private Cairo.Surface surface = null;

    private Puzzle? _puzzle = null;
    public Puzzle? puzzle
    {
        get { return _puzzle; }
        set
        {
            if (_puzzle != null)
                SignalHandler.disconnect_by_func (_puzzle, null, this);
            _puzzle = value;
            _puzzle.changed.connect (puzzle_changed_cb);
            piece_x = 0;
            piece_y = 0;
            piece_id = '\0';
            queue_draw ();
        }
    }

    private int tile_size
    {
        get
        {
            var s = int.min ((get_allocated_width () - SPACE_PADDING) / puzzle.width, (get_allocated_height () - SPACE_PADDING) / puzzle.height);
            /* SVG theme renders best when tile size is multiple of 2 */
            if (s % 2 != 0)
                s--;
            return s;
        }
    }

    public PuzzleView ()
    {
        set_size_request (250, 250);    // TODO enough? Taquin is in 350^2
        set_events (Gdk.EventMask.EXPOSURE_MASK | Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.POINTER_MOTION_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK);
        load_image ();
    }

    private void load_image ()
    {
        image_file = File.new_for_uri("resource:///org/gnome/Klotski/ui/assets.svg");

        try
        {
            tiles_handle = new Rsvg.Handle.from_gfile_sync (image_file, FLAGS_NONE);
        }
        catch (Error e)
        {
            /*var dialog = new Gtk.MessageDialog (window,
                                                Gtk.DialogFlags.MODAL,
                                                Gtk.MessageType.ERROR,
                                                Gtk.ButtonsType.OK,
                                                _("Could not find the image:\n%s\n\nPlease check that Klotski is installed correctly."),
                                                e.message);
            dialog.run ();*/
            stderr.printf ("%s %s\n", "Error in puzzle-view.vala load image:", e.message);
            Posix.exit (Posix.EXIT_FAILURE);
        }
    }

    protected override bool draw (Cairo.Context cr)
    {
        if (tile_size != render_size)
        {
            if (tiles_handle != null)
            {
                int height = tile_size * 2;
                int width = tile_size * THEME_TILE_SEGMENTS;

                surface = new Cairo.Surface.similar (cr.get_target (), Cairo.Content.COLOR_ALPHA, width, height);
                var c = new Cairo.Context (surface);

                /* calc scale factor */
                double sfw = (double) width / 918;
                double sfh = (double) height / 68;

                c.scale (sfw, sfh);

                tiles_handle.render_cairo (c);
            }
            render_size = tile_size;
        }

        var style = get_style_context ();
        style.save ();
        style.set_state (Gtk.StateFlags.NORMAL);
        var fg = style.get_color (Gtk.StateFlags.NORMAL);
        var bg = style.get_background_color (Gtk.StateFlags.NORMAL);
        style.restore ();

        Gdk.cairo_set_source_rgba (cr, bg);
        cr.paint ();

        int width = this.get_allocated_width ();
        int height = this.get_allocated_height ();

        Gdk.cairo_set_source_rgba (cr, fg);
        cr.set_line_width (1.0);

        double kwidth = puzzle.width * tile_size + SPACE_PADDING - 2.0;
        double kheight = puzzle.height * tile_size + SPACE_PADDING - 2.0;
        kx = (width - kwidth) / 2.0;
        ky = (height - kheight) / 2.0;

        cr.rectangle (kx, ky, kwidth, kheight);
        cr.stroke ();

        for (var y = 0; y < puzzle.height; y++)
            for (var x = 0; x < puzzle.width; x++)
            {
                draw_square (cr, x, y, kx, ky);

                if (piece_id == puzzle.get_piece_id (puzzle.map, x, y))
                {
                    Gdk.cairo_set_source_rgba (cr, {1.0, 1.0, 1.0, 0.2});
                    cr.rectangle (x*tile_size + kx, y*tile_size + ky, tile_size, tile_size);
                    cr.fill ();
                }
            }

        return false;
    }

    private void draw_square (Cairo.Context cr, int x, int y, double kx, double ky)
    {
        var rect = Gdk.Rectangle ();
        rect.x = x * tile_size + SPACE_OFFSET + (int)kx - 1;
        rect.y = y * tile_size + SPACE_OFFSET + (int)ky - 1;
        rect.width = tile_size;
        rect.height = tile_size;

        var style = get_style_context ();
        style.save ();
        style.set_state (Gtk.StateFlags.NORMAL);
        var bg = style.get_background_color (Gtk.StateFlags.NORMAL);
        style.restore ();

        Gdk.cairo_rectangle (cr, rect);
        Gdk.cairo_set_source_rgba (cr, bg);

        cr.fill ();

        if (puzzle.get_piece_id (puzzle.map, x, y) != ' ')
        {
            Gdk.cairo_rectangle (cr, rect);
            cr.set_source_surface (surface, rect.x - puzzle.get_piece_nr (x, y) * tile_size, rect.y - tile_size / 2);
            cr.fill ();
        }

        if (puzzle.get_piece_id (puzzle.map, x, y) == '*')
        {
            var value = 22;
            if (puzzle.get_piece_id (puzzle.orig_map, x, y) == '.')
                value = 20;

            var overlay_size = THEME_OVERLAY_SIZE * tile_size / THEME_TILE_SIZE;
            var overlay_offset = THEME_TILE_CENTER * tile_size / THEME_TILE_SIZE - overlay_size / 2;

            cr.rectangle (rect.x + overlay_offset, rect.y + overlay_offset,
                          overlay_size, overlay_size);

            cr.set_source_surface (surface, rect.x - value * tile_size, rect.y - tile_size / 2);
            cr.fill ();
        }
    }

    protected override bool button_press_event (Gdk.EventButton event)
    {
        if (event.button == Gdk.BUTTON_PRIMARY)
        {
            if (puzzle.game_over ())
                return false;

            piece_x = (int) (event.x - kx) / tile_size;
            piece_y = (int) (event.y - ky) / tile_size;
            char new_piece_id = puzzle.get_piece_id (puzzle.map, piece_x, piece_y);

            if (piece_id != '\0' && piece_unmoved)
            {
                piece_id = '\0';
                return false;
            }
            if (new_piece_id == ' ' || new_piece_id == '.' || new_piece_id == '-'
                || new_piece_id == '#' || new_piece_id == piece_id)
                return false;

            piece_unmoved = true;
            piece_id = new_piece_id;
            puzzle.move_map = puzzle.map;
        }

        return false;
    }

    protected override bool button_release_event (Gdk.EventButton event)
    {
        if (event.button == Gdk.BUTTON_PRIMARY && piece_id != '\0')
        {
            if (piece_unmoved)
                return false;

            if (puzzle.movable (piece_id) && puzzle.mapcmp (puzzle.move_map, puzzle.map))
            {
                if (last_piece_id == '\0' || last_piece_id != piece_id)
                {
                    puzzle.undomove_map = puzzle.lastmove_map;
                    if (puzzle.moves < 999)
                        puzzle.moves++;
                }

                if (puzzle.moves > 0 && !puzzle.mapcmp (puzzle.undomove_map, puzzle.map))
                {
                    puzzle.moves--;
                    last_piece_id = '\0';
                }
                else
                    last_piece_id = piece_id;

                puzzle.lastmove_map = puzzle.map;

                puzzle.moved ();
            }
            piece_id = '\0';
        }

        return false;
    }

    protected override bool motion_notify_event (Gdk.EventMotion event)
    {
        int new_piece_x, new_piece_y;

        if (piece_id != '\0')
        {
            new_piece_x = (int) (event.x - kx) / tile_size;
            new_piece_y = (int) (event.y - ky) / tile_size;
            if (new_piece_x >= puzzle.width || event.x < 0 || new_piece_y >= puzzle.height || event.y < 0)
                return false;
            if (puzzle.move_piece (piece_id, piece_x, piece_y, new_piece_x, new_piece_y))
            {
                piece_unmoved = false;
                piece_x = new_piece_x;
                piece_y = new_piece_y;
            }
            return true;
        }

        return false;
    }

    private void puzzle_changed_cb ()
    {
        queue_draw ();
    }
}