1. billforsternz
  2. Tarrasch Chess GUI

Source

Tarrasch Chess GUI / src / BoardSetupControl.cpp

  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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/****************************************************************************
 * Control - A custom control, allow user to setup a chess position
 *  Included as one of the controls in the PositionDialog
 *  Author:  Bill Forster
 *  Licence: See licencing information in ChessPosition.cpp
 *  Copyright 2010, Triple Happy Ltd.  All rights reserved.
 ****************************************************************************/
#define _CRT_SECURE_NO_DEPRECATE
#include "wx/wx.h"
#include "wx/image.h"
#include "wx/file.h"
#include "Appdefs.h"
#include "ChessRules.h"
#include "DebugPrintf.h"
#include "BoardSetup.h"
#include "BoardSetupControl.h"
#include "bitmaps/board_setup_bitmap.xpm"
#include "bitmaps/custom_cursors.xpm"
using namespace std;
using namespace thc;

//static wxBitmap static_chess_bmp=wxBitmap( board_setup_bitmap_xpm );

BoardSetupControl::BoardSetupControl
(
    wxWindow* parent,
    wxWindowID     id, //= wxID_ANY,
    const wxPoint &point //= wxDefaultPosition
)  : wxControl( parent, id, point, wxDefaultSize, wxBORDER_NONE )
/*
#define wxDOUBLE_BORDER         wxBORDER_DOUBLE
#define wxSUNKEN_BORDER         wxBORDER_SUNKEN
#define wxRAISED_BORDER         wxBORDER_RAISED
#define wxBORDER                wxBORDER_SIMPLE
#define wxSIMPLE_BORDER         wxBORDER_SIMPLE
#define wxSTATIC_BORDER         wxBORDER_STATIC
#define wxNO_BORDER             wxBORDER_NONE */

{
    #define TIMER_ID 2000
    m_timer.SetOwner(this,TIMER_ID);
    state = UP_IDLE;
    cursor = 0;
    wait_cursor = 0;
    bs = NULL;
    movements = 0;
    long_clearing_click = false;
    wxClientDC dc(parent);
//    dc.SetFont(*wxNORMAL_FONT);
//    wxSize  size( max_w, n*max_h );
//    wxPoint pos = point;
//    SetPosition( pos );
//    SetSize    ( size );
    DebugPrintf(( "*BoardSetupControl::CaptureMouse*\n" ));
    //CaptureMouse();

    // try to find the directory with our images
    #if 0
    wxString dir;
    if ( wxFile::Exists(wxT("./position_setup.bmp")) )
        dir = wxT("./");
    else if ( wxFile::Exists(wxT("../position_setup.bmp")) )
        dir = wxT("../");
    else
        wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));

    wxImage image;
    if ( !image.LoadFile( dir + _T("position_setup.bmp"), wxBITMAP_TYPE_BMP ) )
        wxLogError(wxT("Can't load BMP image"));
    else
    {
        chess_bmp = wxBitmap( image );
        image.Destroy();
    #else
    if(true)
    {
        //chess_bmp = static_chess_bmp; //wxBitmap( board_setup_bitmap_xpm );
        chess_bmp = wxBitmap( board_setup_bitmap_xpm );
    #endif
        wxSize  size( chess_bmp.GetWidth(), chess_bmp.GetHeight() );
        SetSize( size );
        bs = new BoardSetup( &chess_bmp, this );
    }
}

BoardSetupControl::~BoardSetupControl()
{
    if( bs )
        delete bs;
    //ReleaseMouse();
}

void BoardSetupControl::SetState( const char *action, State new_state )
{
    bool timer_running = false;
    bool timer_running_before=false;
    bool timer_running_after =false;
    const char *before=NULL;
    const char *after=NULL;
    const char *s=NULL;
    State x = state;
    for( int i=0; i<2; i++ )
    {
        switch(x)
        {
            case UP_IDLE:               s="UP_IDLE";
                                        timer_running = false;      break;
            case DOWN_IDLE:             s="DOWN_IDLE";
                                        timer_running = false;      break;
            case UP_CURSOR_BOARD:       s="UP_CURSOR_BOARD";
                                        timer_running = false;      break;
            case UP_CURSOR_SIDE:        s="UP_CURSOR_SIDE";
                                        timer_running = false;      break;
            case UP_CURSOR_SIDE_WAIT:   s="UP_CURSOR_SIDE_WAIT";
                                        timer_running = true;       break;
            case DOWN_CURSOR_WAIT:      s="DOWN_CURSOR_WAIT";
                                        timer_running = true;       break;
            case DOWN_CURSOR_IDLE:      s="DOWN_CURSOR_IDLE";
                                        timer_running = false;      break;
            case DOWN_CURSOR_DRAG:      s="DOWN_CURSOR_DRAG";
                                        timer_running = false;      break;
        }
        x = new_state;
        if( i == 0 )
        {
            before = s;
            timer_running_before = timer_running;
        }
        else
        {
            after = s;
            timer_running_after = timer_running;
        }
    }
    DebugPrintf(( "Transition: %s: %s->%s\n", action, before, after ));
    state = new_state;
    if( timer_running_before && !timer_running_after )
        m_timer.Stop();
}


// Later - learn how to do this by sending an event to parent instead
extern void VeryUglyTemporaryCallback();
void BoardSetupControl::UpdateBoard()
{
    bs->SetPosition(squares);
    bs->Draw();
    VeryUglyTemporaryCallback();
}

void BoardSetupControl::OnMouseMove( wxMouseEvent& event )
{
    movements++;
    wxPoint point = event.GetPosition();
    char piece='\0', file='\0', rank='\0';
    bool hit = bs->HitTest( point, piece, file, rank );
/*  if( !hit )
        DebugPrintf(( "no hit; x=%d, y=%d\n", point.x, point.y ));
    else
    {
        DebugPrintf(( "hit; x=%d, y=%d -> piece=%c, file=%c, rank=%c\n",
                point.x, point.y,
                piece?piece:'?',
                file?file:'?',
                rank?rank:'?' ));
    } */
    switch( state )
    {
        default:
            break;
        case UP_CURSOR_BOARD:
        case UP_IDLE:
        {
            if( hit && piece )
            {
                cursor = piece;
                SetCustomCursor( cursor );
                SetState( "move", UP_CURSOR_SIDE );
            }
            break;
        }
        case UP_CURSOR_SIDE:
        {
            if( hit && piece && cursor!=piece )
            {
                //cursor = piece;
                //SetCustomCursor( cursor );
                m_timer.Start( 200, true );
                wait_cursor = piece;
                SetState( "move", UP_CURSOR_SIDE_WAIT );
            }
            else if( hit && !piece )
                SetState( "move", UP_CURSOR_BOARD );
            break;
        }
        case UP_CURSOR_SIDE_WAIT:
        {
            if( hit && piece )
            {
                if( cursor == piece )
                    SetState( "undo wait", UP_CURSOR_SIDE );
                else if( wait_cursor != piece )
                {
                    wait_cursor = piece;
                    m_timer.Start( 200, true );
                    SetState( "change wait_cursor", UP_CURSOR_SIDE_WAIT );
                }
            }
            else if( hit && !piece )
                SetState( "move", UP_CURSOR_BOARD );
            break;
        }
    }
}

/* State machine notes:
3 main possibilities; click curs on 1)empty square or 2)different piece or 3)same piece as curs
1) curs + empty = curs
2) curs + piece = curs  (quick click)
   curs + piece = piece (slow click)
3) curs + curs  = empty (quick click)
   curs + curs  = curs  (slow click)
Slow click mechanism is;
a) After timeout, curs changes to curs(piece) (if different piece)
b) square = empty
c) square = curs on release (may be different square)
*/

void BoardSetupControl::OnTimeout( wxTimerEvent& WXUNUSED(event) )
{
    DebugPrintf(( "Timeout\n" ));
    long_clearing_click = false;
    if( state == DOWN_CURSOR_WAIT )
    {
        SetState( "timeout", DOWN_CURSOR_DRAG );
        if( squares[wait_offset] == cursor )
        {
            long_clearing_click = true;
            movements = 0;
        }
        else
        {
            cursor = squares[wait_offset];
            SetCustomCursor( cursor );
        }
        squares[wait_offset] = ' ';
        UpdateBoard();
    }
    else if( state == UP_CURSOR_SIDE_WAIT )
    {
        cursor = wait_cursor;
        SetCustomCursor( cursor );
        SetState( "timeout", UP_CURSOR_SIDE );
    }
}

void BoardSetupControl::OnMouseLeftUp( wxMouseEvent& event )
{
    DebugPrintf(( "Up\n" ));
    wxPoint point = event.GetPosition();
    char piece='\0', file='\0', rank='\0';
    bool hit = bs->HitTest( point, piece, file, rank );
    if( state==UP_CURSOR_BOARD || state==UP_CURSOR_SIDE || state==UP_CURSOR_SIDE_WAIT )
        OnMouseLeftDown(event);  // This is very important. If you double click
                                 //  we want events as follows; down up down up
                                 //  but without this we get; down up up
    switch( state )
    {
        default:
        case UP_IDLE:
        case UP_CURSOR_BOARD:
        case UP_CURSOR_SIDE:
        case UP_CURSOR_SIDE_WAIT:
            break;
        case DOWN_IDLE:
        {
            SetState( "up", UP_IDLE );
            break;
        }
        case DOWN_CURSOR_IDLE:
        {
            SetState( "up", UP_CURSOR_BOARD );
            break;
        }

        // Quick click, released before timeout
        case DOWN_CURSOR_WAIT:
        {
            SetState( "up", UP_CURSOR_BOARD );
            if( hit && !piece )
            {
                int col = file-'a';     // file='a' -> col=0
                int row = 7-(rank-'1'); // rank='8' -> row=0
                int offset = row*8 + col;
                if( offset != wait_offset )
                {
                    // Quick drag
                    squares[offset] = cursor;
                    squares[wait_offset] = ' ';
                }
                else
                {
                    // Quick click
                    if( cursor == squares[wait_offset] )
                    {
                        squares[wait_offset] = ' ';
                        DebugPrintf(( "Quick click clear\n" ));
                    }
                    else
                    {
                        squares[wait_offset] = cursor;
                        DebugPrintf(( "Quick click set\n" ));
                    }
                }
            }
            else
                squares[wait_offset] = ' '; // clear waiting square
            UpdateBoard();
            DebugPrintf(( "Quick click end\n" ));
            break;
        }
        case DOWN_CURSOR_DRAG:
        {
            SetState( "up", UP_CURSOR_BOARD );
            if( hit && !piece )
            {
                int col = file-'a';     // file='a' -> col=0
                int row = 7-(rank-'1'); // rank='8' -> row=0
                int offset = row*8 + col;
                if( long_clearing_click && offset==wait_offset && movements<5 )
                    ; // don't drop piece onto newly empty square if we haven't
                      //  moved the cursor after clearing it  
                else
                {
                    squares[offset] = cursor;
                    UpdateBoard();
                }
            }
            break;
        }
    }
    long_clearing_click = false;
}

void BoardSetupControl::OnMouseLeftDown( wxMouseEvent& event )
{
    DebugPrintf(( "Down\n" ));
    long_clearing_click = false;
    wxPoint point = event.GetPosition();
    char piece='\0', file='\0', rank='\0';
    bool hit = bs->HitTest( point, piece, file, rank );
    if( !hit )
        DebugPrintf(( "no hit; x=%d, y=%d\n", point.x, point.y ));
    else
    {
        DebugPrintf(( "hit; x=%d, y=%d -> piece=%c, file=%c, rank=%c\n",
                point.x, point.y,
                piece?piece:'?',
                file?file:'?',
                rank?rank:'?' ));
    }
    switch( state )
    {
        default:
        case DOWN_IDLE:
            break;
        case UP_IDLE:
        {
            SetState( "down", DOWN_IDLE );
            if( hit && piece )
            {
                cursor = piece;
                SetCustomCursor( cursor );
                SetState( "down", DOWN_CURSOR_IDLE );
            }
            break;
        }
        case DOWN_CURSOR_WAIT:
        case DOWN_CURSOR_IDLE:
        case DOWN_CURSOR_DRAG:
//      {
//          SetState( "down", DOWN_CURSOR_IDLE );
//          break;
//      }
        case UP_CURSOR_BOARD:
        case UP_CURSOR_SIDE:
        case UP_CURSOR_SIDE_WAIT:
        {
            SetState( "down", DOWN_CURSOR_IDLE );
            if( hit )
            {
                if( piece )
                {
                    cursor = piece;
                    SetCustomCursor( cursor );
                }
                else
                {
                    int col = file-'a';     // file='a' -> col=0
                    int row = 7-(rank-'1'); // rank='8' -> row=0
                    int offset = row*8 + col;
                    if( squares[offset] == ' ' )
                    {
                        SetState( "down", DOWN_CURSOR_IDLE );
                        squares[offset] = cursor;
                        UpdateBoard();
                    }
                    else
                    {
                        SetState( "down", DOWN_CURSOR_WAIT );
                        wait_offset = offset;
                        m_timer.Start( 200, true );
                    }
                }
            }
            break;
        }
    }
}


void BoardSetupControl::OnPaint( wxPaintEvent& WXUNUSED(event) )
{
    wxPaintDC dc(this);
//    wxSize size = GetClientSize();
//  dc.SetFont(*wxNORMAL_FONT);
//    dc.SetBrush(*wxWHITE_BRUSH);
//    dc.SetPen(*wxLIGHT_GREY_PEN);
//    dc.SetBrush(*wxLIGHT_GREY_BRUSH);
//    dc.DrawRectangle( 0, y, size.x, y+height );
//    dc.DrawLine(0, y+height, size.x, y+height );
    if( chess_bmp.Ok() )
    {
        //bool dont_redraw = (captured&&suppress_redraw);
        //if( dont_redraw )
        //    DebugPrintf(( "OMG " ));
        dc.DrawBitmap( chess_bmp, 0, 0, true );
    }
}

void BoardSetupControl::OnEraseBackground(wxEraseEvent& WXUNUSED(evt))
{
    // intentionally empty
}

void BoardSetupControl::OnSize(wxSizeEvent& WXUNUSED(evt))
{
    Refresh();
}
void BoardSetupControl::OnMouseCaptureLost( wxMouseCaptureLostEvent& WXUNUSED(event) )
{
    //event.Skip(true);
    //event.StopPropagation();
}

void BoardSetupControl::SetCustomCursor( char piece )
{
    const char * const *ptr;
    switch( piece )
    {
        default:
        case 'P':   ptr = white_pawn_xpm;    break;
        case 'N':   ptr = white_knight_xpm;  break;
        case 'B':   ptr = white_bishop_xpm;  break;
        case 'R':   ptr = white_rook_xpm;    break;
        case 'Q':   ptr = white_queen_xpm;   break;
        case 'K':   ptr = white_king_xpm;    break;
        case 'p':   ptr = black_pawn_xpm;    break;
        case 'n':   ptr = black_knight_xpm;  break;
        case 'b':   ptr = black_bishop_xpm;  break;
        case 'r':   ptr = black_rook_xpm;    break;
        case 'q':   ptr = black_queen_xpm;   break;
        case 'k':   ptr = black_king_xpm;    break;
    }
    wxImage image( ptr );
    image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X,17);
    image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y,17);
    wxCursor cursor(image);
    SetCursor( cursor );    
}

void BoardSetupControl::ClearCustomCursor()
{
    SetState( "ClearCustomCursor", UP_IDLE );
    this->cursor = 0;
    wxCursor cursor(wxCURSOR_ARROW);
    SetCursor( cursor );    
}


BEGIN_EVENT_TABLE(BoardSetupControl, wxControl)
    EVT_PAINT(BoardSetupControl::OnPaint)
    EVT_SIZE(BoardSetupControl::OnSize)
    EVT_MOUSE_CAPTURE_LOST(BoardSetupControl::OnMouseCaptureLost)
//    EVT_MOUSE_EVENTS(BoardSetupControl::OnMouseEvent)
    EVT_LEFT_UP (BoardSetupControl::OnMouseLeftUp)
    EVT_LEFT_DOWN (BoardSetupControl::OnMouseLeftDown)
    EVT_MOTION (BoardSetupControl::OnMouseMove)
    EVT_ERASE_BACKGROUND(BoardSetupControl::OnEraseBackground)
    EVT_TIMER( TIMER_ID, BoardSetupControl::OnTimeout)
END_EVENT_TABLE()