summaryrefslogtreecommitdiff
path: root/src/gnome/gnome-board.c
blob: e003685ff0dc1d6bf054507cef2e8f98965383bf (plain)
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
/*
 *  Copyright (C) 2006 Benjamin Otte <otte@gnome.org>
 *
 *  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, 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, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *  $Id$
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif
#include <glib/gi18n.h>
#include "gnome-board.h"
#include <libgame/game-sprite.h>
#include <string.h>
#include <math.h>

enum {
  PROP_0,
  PROP_GRAPHIC
};

static GtkWidgetClass *parent_class = NULL;

/*** WIDGET IMPLEMENTATION ***/

static void
game_gnome_board_get_graphic_size (GameGnomeBoard *board, double *width, double *height)
{
  GameGraphic *graphic = board->graphic;
  
  if (width)
    *width = graphic ? graphic->rect.x2 - graphic->rect.x1 : 0;
  if (height)
    *height = graphic ? graphic->rect.y2 - graphic->rect.y1 : 0;
}

static void
game_gnome_board_size_request (GtkWidget *widget, GtkRequisition *requisition)
{
  double width, height;
  
  game_gnome_board_get_graphic_size (GAME_GNOME_BOARD (widget), 
      &width, &height);
  requisition->width = ceil (width);
  requisition->height = ceil (height);
}

static void
game_gnome_board_compute_window_size (GameGnomeBoard *board)
{
  double w, h;
  GtkAllocation allocation;
  
  gtk_widget_get_allocation (GTK_WIDGET (board), &allocation);
  
  if (board->graphic) {
    game_gnome_board_get_graphic_size (board, &w, &h);
    board->scale = MIN ((double) allocation.width / w, (double) allocation.height / h);
    w = ceil (w * board->scale);
    h = ceil (h * board->scale);
  } else {
    board->scale = 1.0;
    w = h = 1;
  }
  board->skip_x = (int) (allocation.width - w) / 2 + allocation.x;
  board->skip_y = (int) (allocation.height - h) / 2 + allocation.y;
}

static void
game_gnome_board_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
{
  GameGnomeBoard *board = GAME_GNOME_BOARD (widget);

  GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);

  game_gnome_board_compute_window_size (board);
}

static gboolean
game_gnome_board_expose (GtkWidget *widget, GdkEventExpose *event)
{
  GameGnomeBoard *board = GAME_GNOME_BOARD (widget);
  cairo_t *cr;

  if (!board->graphic)
    return FALSE;

  cr = gdk_cairo_create (event->window);
  gdk_cairo_region (cr, event->region);
  cairo_clip (cr);

  cairo_translate (cr, board->skip_x, board->skip_y);
  cairo_scale (cr, board->scale, board->scale);
  {
    static double time = 0.0;
    GTimer *timer = g_timer_new ();
    game_graphic_draw (board->graphic, cr);
    g_timer_stop (timer);
    if (g_timer_elapsed (timer, NULL) > time) {
      time = g_timer_elapsed (timer, NULL);
      g_print ("drawing took %gs\n", time);
    }
    g_timer_destroy (timer);
  }
  cairo_destroy (cr);
  return FALSE;
}

static void
callback_weak_notify (gpointer data, GObject *unused)
{
  GameGnomeBoard *board = GAME_GNOME_BOARD (data);
  
  board->graphic = NULL;
  g_object_notify (G_OBJECT (board), "graphic");
  gtk_widget_queue_draw (GTK_WIDGET (board));
}

static void
callback_invalidate (GameGraphic *graphic, const GameRectangle *rect,
    GameGnomeBoard *board)
{
  GameRectangle inval;
  GamePoint p = {- graphic->rect.x1, - graphic->rect.y1 };
   
  game_rectangle_move (&inval, rect, &p);
  game_rectangle_scale (&inval, &inval, board->scale, board->scale);

  gtk_widget_queue_draw_area (GTK_WIDGET (board),
                              floor (inval.x1) + board->skip_x, 
                              floor (inval.y1) + board->skip_y,
                              ceil (inval.x2) - floor (inval.x1),
                              ceil (inval.y2) - floor (inval.y1));
}

static void
game_gnome_board_set_graphic (GameGnomeBoard *board, GameGraphic* graphic)
{
  g_return_if_fail (GAME_IS_GNOME_BOARD (board));
  g_return_if_fail (graphic == NULL || GAME_IS_GRAPHIC (graphic));

  if (board->graphic == graphic)
    return;

  if (board->graphic) {
    if (g_signal_handlers_disconnect_matched (board->graphic, G_SIGNAL_MATCH_DATA,
						    0, 0, NULL, NULL, board) != 1) {
      g_assert_not_reached ();
    }
    g_object_weak_unref (G_OBJECT (board->graphic), callback_weak_notify, board);
  }

  if (graphic) {
    board->graphic = graphic;
    g_signal_connect (graphic, "invalidate", (GCallback) callback_invalidate, board);
    g_object_weak_ref (G_OBJECT (board->graphic), callback_weak_notify, board);
  } else {
    board->graphic = NULL;
  }
  g_object_notify (G_OBJECT (board), "graphic");
  gtk_widget_queue_resize (GTK_WIDGET (board));
  gtk_widget_queue_draw (GTK_WIDGET (board));
}

static void
game_gnome_board_get_property (GObject *object, guint param_id, GValue *value, GParamSpec *pspec)
{
  GameGnomeBoard *board = GAME_GNOME_BOARD (object);
    
  switch (param_id) {
    case PROP_GRAPHIC:
      g_value_set_object (value, board->graphic);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
      break;
  }
}

static void
game_gnome_board_set_property (GObject *object, guint param_id, 
    const GValue *value, GParamSpec *pspec)
{
  GameGnomeBoard *board = GAME_GNOME_BOARD (object);
    
  switch (param_id) {
    case PROP_GRAPHIC:
      game_gnome_board_set_graphic (board, g_value_get_object (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
      break;
  }
}

static void
game_gnome_board_dispose (GObject *object)
{
  GameGnomeBoard *board = GAME_GNOME_BOARD (object);
  
  game_gnome_board_set_graphic (board, NULL);

  if (G_OBJECT_CLASS (parent_class)->dispose)
    G_OBJECT_CLASS (parent_class)->dispose (object);
}

static void
game_gnome_board_class_init (gpointer g_class, gpointer class_data)
{
  GObjectClass *object_class = G_OBJECT_CLASS (g_class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (g_class);
  
  parent_class = g_type_class_peek_parent (g_class);

  object_class->dispose = game_gnome_board_dispose;
  object_class->set_property = game_gnome_board_set_property;
  object_class->get_property = game_gnome_board_get_property;
  
  g_object_class_install_property (object_class, PROP_GRAPHIC,
      g_param_spec_object ("graphic", _("graphic"), _("Graphic that is displayed"),
	  GAME_TYPE_GRAPHIC, G_PARAM_READWRITE));
  
  widget_class->size_request = game_gnome_board_size_request;
  widget_class->size_allocate = game_gnome_board_size_allocate;
  widget_class->expose_event = game_gnome_board_expose;
}

static void
game_gnome_board_init (GTypeInstance *instance, gpointer g_class)
{
  GtkWidget *widget = GTK_WIDGET (instance);

  gtk_widget_set_has_window (widget, FALSE);
}

GType
game_gnome_board_get_type ()
{
  static GType gnome_board_type = 0;
  
  if (!gnome_board_type) {
    static const GTypeInfo gnome_board_info = {
      sizeof (GameGnomeBoardClass),
      NULL,
      NULL,
      game_gnome_board_class_init,
      NULL,
      NULL,
      sizeof (GameGnomeBoard),
      0,
      game_gnome_board_init,
    };
    
    gnome_board_type = g_type_register_static (GTK_TYPE_WIDGET, "GameGnomeBoard", &gnome_board_info, 0);
  }
  
  return gnome_board_type;
}


GtkWidget *
game_gnome_board_new (GameGraphic *graphic)
{
  GameGnomeBoard *gnome_board;

  g_return_val_if_fail (graphic == NULL || GAME_IS_GRAPHIC (graphic), NULL);
 
  gnome_board = GAME_GNOME_BOARD (g_object_new (GAME_TYPE_GNOME_BOARD, 
	"graphic", graphic, NULL));

  return GTK_WIDGET (gnome_board); 
}