File: board.h

package info (click to toggle)
eboard 1.1.1-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,548 kB
  • ctags: 2,803
  • sloc: cpp: 24,724; perl: 1,012; sh: 866; makefile: 66
file content (382 lines) | stat: -rw-r--r-- 9,745 bytes parent folder | download | duplicates (5)
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* $Id: board.h,v 1.61 2008/02/08 14:25:50 bergo Exp $ */

/*

    eboard - chess client
    http://eboard.sourceforge.net
    Copyright (C) 2000-2003 Felipe Paulo Guazzi Bergo
    bergo@seul.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 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, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/



#ifndef BOARD_H
#define BOARD_H

#include "stl.h"
#include "eboard.h"
#include "widgetproxy.h"
#include "pieces.h"
#include "position.h"
#include "chess.h"
#include "clock.h"
#include "notebook.h"

// foreign classes referenced here
class ChessGame;

class AnimatedPiece {
 public:
  virtual void create(GdkWindow *parent,PieceSet *set,int sqside)=0;
  virtual void lemming()=0;
  virtual void destroy()=0;
  virtual void step()=0;
  
  int over();

  int getX();
  int getY();
  piece getPiece();
  bool getSoundHold();

 protected:
  piece Piece;
  int X0,Y0,X,Y,XF,YF,DX,DY;
  int steps,ower;
  int square;
  bool SoundHold;
};

class FlatAnimatedPiece : public AnimatedPiece {
 public:
  FlatAnimatedPiece(piece p,int x0,int y0,int xf,int yf,int s,bool sndhold);
  virtual void create(GdkWindow *parent,PieceSet *set,int sqside);
  virtual void lemming();
  virtual void destroy();
  virtual void step();
};

class Rect {
 public:
  Rect() { x=0; y=0; w=0; h=0; }
  Rect(int a,int b,int c,int d) { x=a; y=b; w=c; h=d; }
  void translate(int dx,int dy) { x+=dx; y+=dy; }
  int x,y,w,h;
};

class DropSource {
 public:
  DropSource(piece p, int x1,int y1,int w,int h, bool d=true);
  bool hit(int x,int y);

  int X,Y,X2,Y2;
  piece P;
  bool dragged;
};

class TargetManager {
 public:
  void cleanTargets();
  void addTarget(DropSource *ds);
  DropSource *hitTarget(int x,int y);

 private:
  vector<DropSource *> targets;
};

class RootBoard {
 protected:
  void clockString(int val,char *z,int maxlen);
};

// I just can't work out the correct syntax to put the
// bodies of methods in the .cc file, if you know, email me
template<class T> 
class mblock {
 public:

  mblock(int _sz=0) { data=0; sz=0; if (_sz) ensure(_sz); }

  ~mblock() { if (sz) g_free((gpointer) data); sz=0; data=0; }

  bool ensure(int _sz) {
    if (_sz <= sz) return true; // never shrink
    if (sz==0) data=(T *) g_malloc(_sz * sizeof(T));
    else data=(T *) g_realloc((gpointer) data, _sz * sizeof(T));
    sz=_sz;
    if (!data) cerr << "eboard ** mblock error, expect oddities\n";
    return(data!=0);
  }
  
  T * data;
  int sz;
};

class Board : public WidgetProxy,
	      public ClockHost,
	      public RootBoard,
	      public NotebookInsider,
              public TargetManager,
	      public PieceChangeListener
{
 public:
  Board();
  virtual ~Board();

  void repaint();

  void        setSensitive(bool state);
  void        setPartnerGame();
  void        setSelColor(int color);
  void        clearSel();
  void        setFlipped(bool flip);
  void        setFlipInversion(bool v);
  bool        getFlipInversion();
  ChessClock *getClock();
  int         getClockValue2(bool white);
  void        setClock2(int wmsecs,int bmsecs,int actv,int cdown);
  void        setInfo(int idx,char *s);
  void        setInfo(int idx,string &s);
  void        setCanMove(bool value);
  void        stopClock();
  void        reset();
  
  void  setGame(ChessGame *game);
  ChessGame *getGame();
  void  reloadFonts();

  void freeze();
  void thaw();
  void queueRepaint();
  void invalidate();

  virtual void show();

  void update(bool sndflag=false);
  virtual void updateClock();

  virtual void pieceSetChanged();

  void walkBack1();
  void walkBackAll();
  void walkForward1();
  void walkForwardAll();
  void openMovelist();
  bool hasGame();

  bool effectiveFlip() { return(FlipInvert?(!flipped):flipped); }

  void dump();

  void supressNextAnimation();

  bool FreeMove; // editing positions, examining games
  bool EditMode; // explicit edit mode (with pieces to drop and additional commands)
 
  static Board * PopupOwner;

  virtual void renderClock();

  void joystickCursor(int axis, int value);
  void joystickSelect();

 protected:
  GdkPixmap *clkbar;
  GdkGC *clkgc;
  LayoutBox C;
  int clock_ch;

  ChessGame *mygame;

  // inline calculations
  int Width()  { return( (sqside<<3) + (borx<<1) ); }
  int Height() { return( (sqside<<3) + (bory<<1) + morey); }

 private:
  GtkWidget *yidget;

  PieceSet *cur;
  GdkPixmap *buffer;
  GdkGC *wgc;
  rgbptr scrap;

  mblock<unsigned char> M[2];

  bool canselect;
  bool allowMove;
  bool FlipInvert;
  bool flipped;
  bool repaint_due;
  bool update_queue;
  bool UpdateClockAfterConfigure;

  int sel_color;
  int frozen_lvl;
  int LockAnimate;

  vector<SMove *> LastMove;

  list<AnimatedPiece *> animes;
  int gotAnimationLoop;
  int AnimationTimeout;

  Position position;
  Position currently_rendered;
  Position fake;

  ChessClock clock;

  int hilite_ch;

  // selection/highlight
  int ax[2],ay[2],sp;
  int jx,jy,jpxv,jpyv; // discrete joystick cursor
  int jsx,jsy,jsvx,jsvy,jstid,jsnt; // smooth joystick cursor

  // premove
  int premoveq;
  int pmx[2],pmy[2];
  piece pmp;  

  // dragged piece info
  bool  dr_active;
  bool  dr_fromstock;
  int   dr_ox, dr_oy, dr_c, dr_r;
  piece dr_p;
  int   dr_step;
  int   dr_dirty[2];
  bool  dr_fto;

  // animation
  list<Rect *> adirty;

  // info lines
  string info[6];

  int dropsq[2];

  int sqside;
  int sqw, sqh; /* canvas size */
  int borx, bory; // borders (when coordinates are shown)
  int morey; // extra y space for extruded sets

  stack<bool> UpdateStack;

  void popupDropMenu(int col,int row,int sx,int sy,guint32 etime);
  void popupProtocolMenu(int x,int y, guint32 etime);
  void sendMove();
  void outlineRectangle(GdkGC *gc, int x,int y,int color,int pen);
  void drawJoystickCursor(GdkGC *gc);
  void drawBall(GdkGC *gc, int x,int y,int color,int radius);
  void drop(piece p);
  void drawCoordinates(GdkPixmap *dest,GdkGC *gc,int side);

  void composeVecBoard(GdkGC *gc);
  void pasteVecBoard();

  void shadyText(int x,int y, int f, char *z);

  friend gboolean board_animate(gpointer data);
  friend gboolean board_joycursor(gpointer data);
  friend gboolean vec_board_animate(gpointer data);

  friend gboolean board_expose_event(GtkWidget *widget,GdkEventExpose *ee,
				     gpointer data);
  friend gboolean board_configure_event(GtkWidget *widget,
					GdkEventConfigure *ce,
					gpointer data);
  friend gboolean board_button_press_event(GtkWidget *widget,
					   GdkEventButton *be,
					   gpointer data);
  friend gboolean board_button_release_event(GtkWidget *widget,
					     GdkEventButton *be,
					     gpointer data);
  friend gboolean board_motion_event(GtkWidget *widget,
				     GdkEventMotion *em,
				     gpointer data);

  friend gboolean gtkDgtnixEvent(GIOChannel* channel, GIOCondition cond, gpointer data);
  

  friend void drop_callbackP(GtkMenuItem *item,gpointer data);
  friend void drop_callbackR(GtkMenuItem *item,gpointer data);
  friend void drop_callbackN(GtkMenuItem *item,gpointer data);
  friend void drop_callbackB(GtkMenuItem *item,gpointer data);
  friend void drop_callbackQ(GtkMenuItem *item,gpointer data);

  friend void menu_whitep(GtkMenuItem *item, gpointer data);
  friend void menu_blackp(GtkMenuItem *item, gpointer data);
  friend void menu_gamep(GtkMenuItem *item, gpointer data);

#if MAEMO
  friend void tap_and_hold_cb(GtkWidget *widget, gpointer user_data);
#endif

};

class EditBoard : public Board {
 public:
  EditBoard(ChessGame *cg);
  virtual ~EditBoard() {}
  void popRunEngine(int x,int y);
  void flipSide();
  void getFEN();
  void applyFEN(string &s);

  virtual void renderClock();

 private:
  friend void eb_runengine_bm(GtkMenuItem *item,gpointer data);
  friend void eb_runengine_nobm(GtkMenuItem *item,gpointer data);
};

class GetFENDialog : public ModalDialog {
 public:
  GetFENDialog(EditBoard *_owner);

 private:
  EditBoard *owner;
  GtkWidget *e;

  friend void getfen_ok(GtkWidget *w, gpointer data);
};


/* friend declarations for GCC 4.x */
gboolean board_animate(gpointer data);
gboolean vec_board_animate(gpointer data);
gboolean board_expose_event(GtkWidget *widget,GdkEventExpose *ee,gpointer data);
gboolean board_configure_event(GtkWidget *widget,GdkEventConfigure *ce,gpointer data);
gboolean board_button_press_event(GtkWidget *widget,GdkEventButton *be,gpointer data);
gboolean board_button_release_event(GtkWidget *widget,GdkEventButton *be,gpointer data);
gboolean board_motion_event(GtkWidget *widget,GdkEventMotion *em,gpointer data);
void drop_callbackP(GtkMenuItem *item,gpointer data);
void drop_callbackR(GtkMenuItem *item,gpointer data);
void drop_callbackN(GtkMenuItem *item,gpointer data);
void drop_callbackB(GtkMenuItem *item,gpointer data);
void drop_callbackQ(GtkMenuItem *item,gpointer data);
void menu_whitep(GtkMenuItem *item, gpointer data);
void menu_blackp(GtkMenuItem *item, gpointer data);
void menu_gamep(GtkMenuItem *item, gpointer data);
void eb_runengine_bm(GtkMenuItem *item,gpointer data);
void eb_runengine_nobm(GtkMenuItem *item,gpointer data);
void getfen_ok(GtkWidget *w, gpointer data);
gboolean board_joycursor(gpointer data);

#endif