~ubuntu-branches/ubuntu/saucy/ricochet/saucy-proposed

« back to all changes in this revision

Viewing changes to ricochet-0.2/debian/ricochet/usr/share/ricochet/client-draw.5c

  • Committer: Package Import Robot
  • Author(s): Keith Packard
  • Date: 2012-06-11 13:37:57 UTC
  • Revision ID: package-import@ubuntu.com-20120611133757-zn0ukd22vz56ymto
Tags: 0.3
* Improve appearance of board
* Fix user list when removing/adding same user

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright © 2012 Keith Packard <keithp@keithp.com>
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or modify
5
 
 * it under the terms of the GNU General Public License as published by
6
 
 * the Free Software Foundation; version 2 of the License.
7
 
 *
8
 
 * This program is distributed in the hope that it will be useful, but
9
 
 * WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
 
 * General Public License for more details.
12
 
 *
13
 
 * You should have received a copy of the GNU General Public License along
14
 
 * with this program; if not, write to the Free Software Foundation, Inc.,
15
 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16
 
 */
17
 
 
18
 
autoload Cairo;
19
 
autoload Client;
20
 
autoload Client::SVG;
21
 
 
22
 
extend namespace Client {
23
 
 
24
 
        public namespace Draw {
25
 
 
26
 
                import Cairo;
27
 
                import Cairo::Rsvg;
28
 
                import RR;
29
 
 
30
 
                public typedef struct {
31
 
                        int     xoff, yoff;
32
 
                        real    xscale, yscale;
33
 
                } transform_t;
34
 
 
35
 
                typedef struct {
36
 
                        Cairo::surface_t        surface;
37
 
                        int                     width, height;
38
 
                        rsvg_t                  svg;
39
 
                } sprite_t;
40
 
 
41
 
                sprite_t sprite_from_string(string xml) = (sprite_t) {
42
 
                        .svg = Cairo::Rsvg::new_from_string(xml),
43
 
                        .width = -1,
44
 
                        .height = -1
45
 
                };
46
 
 
47
 
                sprite_t[2]     cell = { sprite_from_string(Client::SVG::cell1),
48
 
                                         sprite_from_string(Client::SVG::cell2) };
49
 
 
50
 
                sprite_t[Color] robots = {
51
 
                        Color.Red => sprite_from_string(Client::SVG::robot_red),
52
 
                        Color.Blue => sprite_from_string(Client::SVG::robot_blue),
53
 
                        Color.Yellow => sprite_from_string(Client::SVG::robot_yellow),
54
 
                        Color.Green => sprite_from_string(Client::SVG::robot_green) };
55
 
 
56
 
                sprite_t robot_shadow = sprite_from_string(Client::SVG::robot_shadow);
57
 
 
58
 
                sprite_t[Color][Shape]  targets = {
59
 
                        Color.Red => {
60
 
                                Shape.Triangle => sprite_from_string(Client::SVG::target_red_triangle),
61
 
                                Shape.Square => sprite_from_string(Client::SVG::target_red_square),
62
 
                                Shape.Octagon => sprite_from_string(Client::SVG::target_red_octagon),
63
 
                                Shape.Circle => sprite_from_string(Client::SVG::target_red_circle),
64
 
                        },
65
 
                        Color.Yellow => {
66
 
                                Shape.Triangle => sprite_from_string(Client::SVG::target_yellow_triangle),
67
 
                                Shape.Square => sprite_from_string(Client::SVG::target_yellow_square),
68
 
                                Shape.Octagon => sprite_from_string(Client::SVG::target_yellow_octagon),
69
 
                                Shape.Circle => sprite_from_string(Client::SVG::target_yellow_circle),
70
 
                        },
71
 
                        Color.Green => {
72
 
                                Shape.Triangle => sprite_from_string(Client::SVG::target_green_triangle),
73
 
                                Shape.Square => sprite_from_string(Client::SVG::target_green_square),
74
 
                                Shape.Octagon => sprite_from_string(Client::SVG::target_green_octagon),
75
 
                                Shape.Circle => sprite_from_string(Client::SVG::target_green_circle),
76
 
                        },
77
 
                        Color.Blue => {
78
 
                                Shape.Triangle => sprite_from_string(Client::SVG::target_blue_triangle),
79
 
                                Shape.Square => sprite_from_string(Client::SVG::target_blue_square),
80
 
                                Shape.Octagon => sprite_from_string(Client::SVG::target_blue_octagon),
81
 
                                Shape.Circle => sprite_from_string(Client::SVG::target_blue_circle),
82
 
                        },
83
 
                        Color.Whirl => {
84
 
                                Shape.Whirl => sprite_from_string(Client::SVG::target_whirl),
85
 
                        },
86
 
                };
87
 
 
88
 
                sprite_t                wall = sprite_from_string(Client::SVG::wall);
89
 
 
90
 
                void draw_sprite(&sprite_t sprite, cairo_t cr) {
91
 
                        render(sprite.svg, cr);
92
 
                }
93
 
 
94
 
                dimensions_t cell_dim  = get_dimensions(cell[0].svg);
95
 
 
96
 
                public int cell_width = cell_dim.width;
97
 
                public int cell_height = cell_dim.height;
98
 
 
99
 
                void draw_cached_sprite(&sprite_t sprite, cairo_t cr, &transform_t t) {
100
 
                        int     width = ceil(cell_width * t.xscale);
101
 
                        int     height = ceil(cell_width * t.yscale);
102
 
 
103
 
                        save(cr);
104
 
                        if (width != sprite.width || height != sprite.height) {
105
 
                                if (!is_uninit(&sprite.surface))
106
 
                                        Cairo::Surface::destroy(sprite.surface);
107
 
                                sprite.width = width;
108
 
                                sprite.height = height;
109
 
                                if (width > 0 && height > 0) {
110
 
                                        sprite.surface = Cairo::Surface::create_similar(Cairo::get_target(cr),
111
 
                                                                                        Cairo::content_t.COLOR_ALPHA,
112
 
                                                                                        width, height);
113
 
                                        cairo_t scr = Cairo::create(sprite.surface);
114
 
                                        scale(scr, t.xscale, t.yscale);
115
 
                                        draw_sprite(&sprite, scr);
116
 
                                        Cairo::destroy(scr);
117
 
                                }
118
 
                        }
119
 
                        if (sprite.width > 0 && sprite.height > 0) {
120
 
                                set_source_surface(cr, sprite.surface, 0, 0);
121
 
                                paint(cr);
122
 
                        }
123
 
                        restore(cr);
124
 
                }
125
 
 
126
 
                public void background (cairo_t cr, int x, int y, RR::Object object, &transform_t t) {
127
 
                        save(cr);
128
 
                        translate(cr, x * cell_width * t.xscale + t.xoff, y * cell_height * t.yscale + t.yoff);
129
 
 
130
 
                        draw_cached_sprite(&cell[x+y & 1], cr, &t);
131
 
                        restore(cr);
132
 
                }
133
 
                
134
 
                public void walls(cairo_t cr, int x, int y, RR::Object object, &transform_t t) {
135
 
                        save(cr);
136
 
                        translate(cr, t.xoff, t.yoff);
137
 
                        scale(cr, t.xscale, t.yscale);
138
 
                        translate(cr, x * cell_width, y * cell_height);
139
 
                        rectangle(cr, 0, 0, cell_width, cell_height);
140
 
                        clip(cr);
141
 
 
142
 
                        void draw_wall (bool doit, bool vertical, bool shift) {
143
 
                                if (!doit) return;
144
 
                                save(cr);
145
 
                                if (vertical) {
146
 
                                        rotate(cr, pi/2);
147
 
                                        if (shift)
148
 
                                                translate(cr, 0, -cell_height);
149
 
                                } else if (shift)
150
 
                                                translate(cr, 0, cell_height);
151
 
                                draw_sprite(&wall, cr);
152
 
                                restore(cr);
153
 
                        }
154
 
                        draw_wall (object.walls.left, true, false);
155
 
                        draw_wall (object.walls.above, false, false);
156
 
                        draw_wall (object.walls.right, true, true);
157
 
                        draw_wall (object.walls.below, false, true);
158
 
                        restore(cr);
159
 
                }
160
 
                
161
 
                public void contents (cairo_t cr, int x, int y, RR::Object object, RR::RobotOrNone active_robot,
162
 
                                      &transform_t t) {
163
 
                        save(cr);
164
 
                        translate(cr, x * cell_width * t.xscale + t.xoff, y * cell_height * t.yscale + t.yoff);
165
 
 
166
 
                        union switch (object.target) {
167
 
                        case none:
168
 
                                break;
169
 
                        case target target:
170
 
                                draw_cached_sprite(&targets[target.color][target.shape], cr, &t);
171
 
                                break;
172
 
                        }
173
 
                        union switch(object.robot) {
174
 
                        case none:
175
 
                                break;
176
 
                        case robot r:
177
 
                                union switch (active_robot) {
178
 
                                case robot a:
179
 
                                        if (a.color == r.color) {
180
 
                                                save(cr);
181
 
                                                scale(cr, t.xscale, t.yscale);
182
 
                                                translate(cr, 5, 1);
183
 
                                                scale(cr, 1.1, 1.1);
184
 
                                                draw_sprite(&robot_shadow, cr);
185
 
                                                restore(cr);
186
 
                                        }
187
 
                                        break;
188
 
                                default:
189
 
                                }
190
 
                                draw_cached_sprite(&robots[r.color], cr, &t);
191
 
                                break;
192
 
                        }
193
 
                        restore(cr);
194
 
                }
195
 
 
196
 
                public void target (cairo_t cr, real x, real y, TargetOrNone target, &transform_t t) {
197
 
                        save(cr);
198
 
                        translate(cr, x * cell_width * t.xscale + t.xoff, y * cell_height * t.yscale + t.yoff);
199
 
                        scale(cr, 2 * t.xscale, 2 *t.yscale);
200
 
                        union switch (target) {
201
 
                        case target t:
202
 
                                draw_sprite(&targets[t.color][t.shape], cr);
203
 
                                break;
204
 
                        default:
205
 
                                break;
206
 
                        }
207
 
                        restore(cr);
208
 
                }
209
 
        }
210
 
}