File: chessboard.cpp

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (214 lines) | stat: -rw-r--r-- 6,439 bytes parent folder | download | duplicates (4)
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
/////////////////////////////////////////////////////////////////////////////
// Name:        samples/event/chessboard.cpp
// Purpose:     Part of wxWidgets event sample, shows how to create a new wxEvent class
// Author:      PB <pbfordev@gmail.com>
// Created:     2019-10-29
// Copyright:   (c) 2019 wxWidgets development team
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

#include "chessboard.h"


//
// ChessBoardEvent
//

// Define new event types associated with ChessBoardEvent
wxDEFINE_EVENT(myEVT_CHESSBOARD_CLICKED, ChessBoardEvent);
wxDEFINE_EVENT(myEVT_CHESSBOARD_DRAGGED, ChessBoardEvent);


//
// ChessBoard
//
class ChessBoard : public wxPanel
{
public:
    ChessBoard(wxWindow* parent);

private:
    static const wxUint8 SquaresPerSide = 8;
    static const int SquareSizeInDIPs   = 40;

    // square on which mouse left button was pressed
    char m_fileLeftMDown;
    wxUint8 m_rankLeftMDown;

    bool ConvertMousePosToFileAndRank(const wxPoint& pos,
                                      char& file, wxUint8& rank) const;

    void OnPaint(wxPaintEvent& event);
    void OnMouseLeftDown(wxMouseEvent& event);
    void OnMouseLeftUp(wxMouseEvent& event);

    virtual wxSize DoGetBestClientSize() const wxOVERRIDE;
};

ChessBoard::ChessBoard(wxWindow* parent)
    : wxPanel(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxBORDER_RAISED),
      m_fileLeftMDown('a'), m_rankLeftMDown(1)
{
    SetCursor(wxCursor(wxCURSOR_HAND));
    SetBackgroundColour(*wxWHITE);

    Bind(wxEVT_PAINT, &ChessBoard::OnPaint, this);
    Bind(wxEVT_LEFT_DOWN, &ChessBoard::OnMouseLeftDown, this);
    Bind(wxEVT_LEFT_UP, &ChessBoard::OnMouseLeftUp, this);
}

 bool ChessBoard::ConvertMousePosToFileAndRank(const wxPoint& pos,
                                               char& file, wxUint8& rank) const
{
    static const char FileLetters[SquaresPerSide] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};

    const int squareSizeInPx = FromDIP(SquareSizeInDIPs);
    const int colIdx = pos.x / squareSizeInPx;
    const int rowIdx = pos.y / squareSizeInPx;

    if ( colIdx < 0 || colIdx >= SquaresPerSide
         || rowIdx < 0 || rowIdx >= SquaresPerSide )
    {
        return false; // not on a square
    }

    file = FileLetters[colIdx],
    // convert rowIdx (top to down 0 to 7) to rank (top to down 8 to 1)
    rank = static_cast<wxUint8>(SquaresPerSide - rowIdx);

    return true;
}

// Background is already painted white,
// so we need to draw only the black squares over it.
void ChessBoard::OnPaint(wxPaintEvent& WXUNUSED(event))
{
    const int squareSizeInPx = FromDIP(SquareSizeInDIPs);

    wxPaintDC dc(this);
    wxDCPenChanger penChanger(dc, *wxTRANSPARENT_PEN);
    wxDCBrushChanger brushChanger(dc, *wxBLACK_BRUSH);
    wxRect squareRect(wxSize(squareSizeInPx, squareSizeInPx));

    // draw from top to down, left to right
    for ( size_t rank = SquaresPerSide; rank >= 1; --rank )
    {
        if ( rank % 2 )
            squareRect.SetLeft(0); // odd ranks start with a black square
        else
            squareRect.SetLeft(squareSizeInPx); // even ranks start with a white square

        for ( size_t file = 0; file < SquaresPerSide / 2; ++file )
        {
            dc.DrawRectangle(squareRect);
            squareRect.Offset(2 * squareSizeInPx, 0);
        }

        squareRect.Offset(0, squareSizeInPx);
    }
}

void ChessBoard::OnMouseLeftDown(wxMouseEvent& event)
{
    char file;
    wxUint8 rank;

    event.Skip();

    if ( !ConvertMousePosToFileAndRank(event.GetPosition(), file, rank) )
        return; // mouse cursor not on a square

    m_fileLeftMDown = file;
    m_rankLeftMDown = rank;
    CaptureMouse();
}

void ChessBoard::OnMouseLeftUp(wxMouseEvent& event)
{
    event.Skip();

    if ( !HasCapture() )
        return; // capture lost or mouse left down was not on a square

    const wxRect clientRect = GetClientRect();
    char file;
    wxUint8 rank;

    ReleaseMouse();

    if (  !clientRect.Contains(event.GetPosition()) ||
          !ConvertMousePosToFileAndRank(event.GetPosition(), file, rank) )
    {
        return; // mouse cursor not on a square
    }

    ChessBoardEvent chessBoardEvent;

    chessBoardEvent.SetId(GetId());
    chessBoardEvent.SetEventObject(this);
    chessBoardEvent.SetFile(m_fileLeftMDown);
    chessBoardEvent.SetRank(m_rankLeftMDown);

    if ( file == m_fileLeftMDown && rank == m_rankLeftMDown )
    {   // mouse left and down on the same square, consider it a click
        chessBoardEvent.SetEventType(myEVT_CHESSBOARD_CLICKED);
    }
    else
    {   // mouse dragged from one square to another
        chessBoardEvent.SetEventType(myEVT_CHESSBOARD_DRAGGED);
        chessBoardEvent.SetFileTo(file);
        chessBoardEvent.SetRankTo(rank);
    }

    ProcessWindowEvent(chessBoardEvent);
}

wxSize ChessBoard::DoGetBestClientSize() const
{
    const int boardSideSizeInPx = SquaresPerSide * FromDIP(SquareSizeInDIPs);

    return wxSize(boardSideSizeInPx, boardSideSizeInPx);
}

//
// MyChessBoardDialog implementation
//

// For demonstration, event table is used for myEVT_CHESSBOARD_CLICKED
// while Bind() is used for myEVT_CHESSBOARD_DRAGGED.
wxBEGIN_EVENT_TABLE(MyChessBoardDialog, wxDialog)
    EVT_CHESSBOARD_CLICKED(wxID_ANY, MyChessBoardDialog::OnChessBoardClicked)
wxEND_EVENT_TABLE()

MyChessBoardDialog::MyChessBoardDialog(wxWindow* parent)
    : wxDialog(parent, wxID_ANY, "New event class demonstration")
{
    wxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);

    mainSizer->Add(
        new wxStaticText(this, wxID_ANY, "Click or drag the mouse over the chessboard"),
        wxSizerFlags().Border().CenterHorizontal());

    mainSizer->Add(new ChessBoard(this),
        wxSizerFlags().TripleBorder().CenterHorizontal());

    mainSizer->Add(CreateStdDialogButtonSizer(wxCLOSE),
        wxSizerFlags().Expand().Border());

    SetSizerAndFit(mainSizer);

    Bind(myEVT_CHESSBOARD_DRAGGED, &MyChessBoardDialog::OnChessBoardDragged, this);
}

void MyChessBoardDialog::OnChessBoardClicked(ChessBoardEvent& event)
{
    wxLogMessage("myEVT_CHESSBOARD_CLICKED: %c%hhu",
        event.GetFile(), event.GetRank());
}

void MyChessBoardDialog::OnChessBoardDragged(ChessBoardEvent& event)
{
    wxLogMessage("myEVT_CHESSBOARD_DRAGGED: from %c%hhu to %c%hhu",
        event.GetFile(), event.GetRank(),
        event.GetFileTo(), event.GetRankTo());
}