~ubuntu-branches/ubuntu/precise/gnome-games/precise-proposed

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
/*
  Copyright © 2004 Callum McKenzie
  Copyright © 2007, 2008 Christian Persch

  This library 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.

  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, see <http://www.gnu.org/licenses/>.
 */

/* Authors:   Callum McKenzie <callum@physics.otago.ac.nz> */

#include <config.h>

#include <string.h>
#include <glib.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gtk/gtk.h>

#include <libgames-support/games-preimage.h>
#include <libgames-support/games-preimage-private.h>
#include <libgames-support/games-runtime.h>
#include <libgames-support/games-string-utils.h>

#include "ar-card-theme.h"
#include "ar-card-theme-private.h"

struct _ArCardThemeSVGClass {
  ArCardThemePreimageClass parent_class;
};

struct _ArCardThemeSVG {
  ArCardThemePreimage parent_instance;
};

#define N_ROWS ((double) 5.0)
#define N_COLS ((double) 13.0)

#define DELTA (0.0f)

/* Class implementation */

G_DEFINE_TYPE (ArCardThemeSVG, ar_card_theme_svg, AR_TYPE_CARD_THEME_PREIMAGE);

static GdkPixbuf *
ar_card_theme_svg_get_card_pixbuf (ArCardTheme *card_theme,
                                      int card_id)
{
  ArCardThemePreimage *preimage_card_theme = (ArCardThemePreimage *) card_theme;
  GamesPreimage *preimage = preimage_card_theme->cards_preimage;
  GdkPixbuf *subpixbuf;
  int suit, rank;
  double card_width, card_height;
  double width, height;
  double offsetx, offsety;
  double zoomx, zoomy;
  char node[32];

  if (G_UNLIKELY (card_id == AR_CARD_SLOT)) {
    subpixbuf = games_preimage_render (preimage_card_theme->slot_preimage,
                                       preimage_card_theme->card_size.width,
                                       preimage_card_theme->card_size.height);

    return subpixbuf;
  }

  suit = card_id / 13;
  rank = card_id % 13;

  card_width = ((double) games_preimage_get_width (preimage)) / N_COLS;
  card_height = ((double) games_preimage_get_height (preimage)) / N_ROWS;

  width = preimage_card_theme->card_size.width - 2 * DELTA;
  height = preimage_card_theme->card_size.height - 2 * DELTA;

  offsetx = -((double) rank) * card_width + DELTA;
  offsety = -((double) suit) * card_height + DELTA;

  zoomx = width / card_width;
  zoomy = height / card_height;

  ar_card_get_node_by_suit_and_rank_snprintf (node, sizeof (node), suit, rank);

  subpixbuf = games_preimage_render_sub (preimage,
                                         node,
                                         preimage_card_theme->card_size.width,
                                         preimage_card_theme->card_size.height,
                                         offsetx, offsety,
                                         zoomx, zoomy);

  return subpixbuf;
}

#if GTK_CHECK_VERSION (2, 10,0)
static void
ar_card_theme_svg_paint_card (ArCardTheme *card_theme,
                              cairo_t *cr,
                              int card_id)
{
  ArCardThemePreimage *preimage_card_theme = (ArCardThemePreimage *) card_theme;
  GamesPreimage *preimage = preimage_card_theme->cards_preimage;
  int suit, rank;
  double card_width, card_height;
  double width, height;
  double offsetx, offsety;
  double zoomx, zoomy;
  char node[32];

  if (G_UNLIKELY (card_id == AR_CARD_SLOT)) {
    games_preimage_render_cairo (preimage_card_theme->slot_preimage,
                                 cr,
                                 preimage_card_theme->card_size.width,
                                 preimage_card_theme->card_size.height);
    return;
  }

  suit = card_id / 13;
  rank = card_id % 13;

  card_width = ((double) games_preimage_get_width (preimage)) / N_COLS;
  card_height = ((double) games_preimage_get_height (preimage)) / N_ROWS;

  width = preimage_card_theme->card_size.width - 2 * DELTA;
  height = preimage_card_theme->card_size.height - 2 * DELTA;

  offsetx = -((double) rank) * card_width + DELTA;
  offsety = -((double) suit) * card_height + DELTA;

  zoomx = width / card_width;
  zoomy = height / card_height;

  ar_card_get_node_by_suit_and_rank_snprintf (node, sizeof (node), suit, rank);

  games_preimage_render_cairo_sub (preimage,
                                   cr,
                                   node,
                                   preimage_card_theme->card_size.width,
                                   preimage_card_theme->card_size.height,
                                   offsetx, offsety,
                                   zoomx, zoomy);
}
#endif /* GTK 2.10 */

static void
ar_card_theme_svg_init (ArCardThemeSVG * cardtheme)
{
}

static ArCardThemeInfo *
ar_card_theme_svg_class_get_theme_info (ArCardThemeClass *klass,
                                           const char *path,
                                           const char *filename)
{
  ArCardThemeInfo *info;

  info = AR_CARD_THEME_CLASS (ar_card_theme_svg_parent_class)->get_theme_info (klass, path, filename);
  if (info) {
    g_assert (info->pref_name == NULL);

    /* SVG is the default. For pref backward compatibility,
     * we don't add a svg: prefix there. We don't strip the .svg
     * extension anymore though.
     */
    info->pref_name = g_strdup (filename);
    return info;
  }

  return NULL;
}

static gboolean
ar_card_theme_svg_class_foreach_theme_dir (ArCardThemeClass *klass,
                                              ArCardThemeForeachFunc callback,
                                              gpointer data)
{
  if (!_ar_card_theme_class_foreach_env (klass, "AR_CARD_THEME_PATH_SVG", callback, data))
    return FALSE;

  if (!callback (klass, games_runtime_get_directory (GAMES_RUNTIME_SCALABLE_CARDS_DIRECTORY), data))
    return FALSE;

  /* If we're installed in a non-system prefix, also load the card themes
   * from the system prefix.
   */
  if (!games_runtime_is_system_prefix ())
    return callback (klass, "/usr/share/gnome-games-common/cards", data);

  return TRUE;
}

static void
ar_card_theme_svg_class_init (ArCardThemeSVGClass * klass)
{
  ArCardThemeClass *theme_class = AR_CARD_THEME_CLASS (klass);
  ArCardThemePreimageClass *preimage_theme_class = AR_CARD_THEME_PREIMAGE_CLASS (klass);

  theme_class->get_theme_info = ar_card_theme_svg_class_get_theme_info;
  theme_class->foreach_theme_dir = ar_card_theme_svg_class_foreach_theme_dir;

  theme_class->get_card_pixbuf = ar_card_theme_svg_get_card_pixbuf;
#if GTK_CHECK_VERSION (2, 10, 0)
  theme_class->paint_card = ar_card_theme_svg_paint_card;
#endif

  preimage_theme_class->needs_scalable_cards = TRUE;
}

/* private API */

/**
 * ar_card_theme_svg_new:
 *
 * Returns: a new #ArCardThemeSVG
 */
ArCardTheme*
ar_card_theme_svg_new (void)
{
  return g_object_new (AR_TYPE_CARD_THEME_SVG, NULL);
}