aboutsummaryrefslogtreecommitdiff
path: root/src/components/MoveTable.cpp
blob: b08bf9e2d962774654bb5b305dfd959288709907 (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
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
#include "MoveTable.hpp"
#include <iostream>

namespace cgeditor {

MoveTable::MoveTable(Status *s) : Component(s) {}

void MoveTable::Refresh() {
  status->MoveTableMaxX = 0;
  status->MoveTableMaxY = 0;
  elements.clear();
  VariationMargins.clear();
  CurrentMove = -1; // No current move by default
  if (status->Moves != NULL) {
    UpdateMoves(status->Moves, 0, 0, status->Moves->IsBlack);
    // We only set the type after the call to UpdateMoves()
    // This way only a single move will be the current move
    if (CurrentMove >= 0) {
      if (status->UseMoveIcons) {
        elements[CurrentMove].prop |= Property::Current;
        elements[CurrentMove + 1].prop |= Property::Current;
      } else {
        elements[CurrentMove].prop |= Property::Current;
      }
    }
  } else {
    Element e;
    e.prop = Property::Text;
    e.text = "No move to show";
    e.x = status->MarginBarWidth;
    e.y = 0;
    e.height = status->MoveHeight;
    e.width = -1;
    elements.push_back(e);
  }
}

bool MoveTable::IsMouseOver(const Element &e) const {
  // Check if we clicked on scroll bars
  if (status->IsMenuOpen ||
      status->MouseX >= (status->CanvasWidth - status->ScrollbarWidth) ||
      status->MouseY >= (status->CanvasHeight - status->ScrollbarWidth)) {
    return (false);
  }
  return (e.IsOver(status->MouseX - status->ScrollX,
                   status->MouseY - status->ScrollY));
}

std::uint32_t MoveTable::UpdateMoves(CGEHalfMove *m, std::uint32_t line,
                                     std::uint32_t indent, bool only_black) {

  //---------- Check black or white ----------
  char indent_black = 0;
  if (m->IsBlack) {
    indent_black++;
  }

  //---------- Create temporary move surrounding area ----------
  Element move_bound;
  move_bound.prop = Property::Move;
  if (m->IsBlack) {
    move_bound.prop |= Property::Black;
  }
  move_bound.x = status->MarginBarWidth +
                 status->MoveWidth * (indent + indent_black) +
                 ((indent + 1) / 2 * status->MarginBarWidth);
  move_bound.y = status->MoveHeight * line;
  move_bound.width = status->MoveWidth;
  move_bound.height = status->MoveHeight;
  move_bound.text = m->move;
  move_bound.ShouldApplyScroll = true;
  bool isMouseOver = IsMouseOver(move_bound);

  //---------- Update current focus move ----------
  if (isMouseOver) {
    if (status->LeftClick) {
      if (!status->IsMenuOpen) {
        status->Events.push_back({Event::Type::Goto, m});
        status->CurrentMove = m;
      }
    } else if (status->RightClick) {
      status->IsMenuOpen = true;
      status->SelectedMove = m;
    }
  }

  //---------- Check if current move is focused ----------
  if (status->CurrentMove == m) {
    CurrentMove = elements.size();
  }

  //---------- Draw move ----------
  if (status->UseMoveIcons) {
    // Image
    Element img;
    img.prop = move_bound.prop | Property::Image;
    img.x = move_bound.x;
    img.y = status->MoveHeight * line;
    img.width = status->MoveIconWidth;
    img.height = status->MoveHeight;
    img.ShouldApplyScroll = true;
    // Move
    Element e;
    e.prop = move_bound.prop | Property::Text;
    e.text = m->move;
    if (m->move.size() > 0) {
      char c = m->move[0];
      if (!(c == 'a' || c == 'b' || c == 'c' || c == 'd' || c == 'e' ||
            c == 'f' || c == 'g' || c == 'h' || c == 'O' || c == '0')) {
        e.text = m->move.substr(1, m->move.size());
        if (c == 'N') {
          img.prop |= Property::Knight;
        } else if (c == 'B') {
          img.prop |= Property::Bishop;
        } else if (c == 'R') {
          img.prop |= Property::Rook;
        } else if (c == 'Q') {
          img.prop |= Property::Queen;
        } else {
          img.prop |= Property::King;
        }
      } else {
        img.prop |= Property::Pawn;
      }
    }
    e.x = status->MoveIconWidth + move_bound.x;
    e.y = status->MoveHeight * line;
    e.width = status->MoveWidth - status->MoveIconWidth;
    e.height = status->MoveHeight;
    e.ShouldApplyScroll = true;
    // Add elements:
    elements.push_back(img);
    elements.push_back(e);
  } else {
    move_bound.prop |= Property::Text;
    elements.push_back(move_bound);
  }

  //---------- NAG ----------
  if(m->nag.size()>0){
    Element nag;
    nag.text = m->nag;
    nag.x = move_bound.x + status->MoveWidth - status->NagWidth - status->NagRightMargin;
    nag.y = status->MoveHeight * line;
    nag.width = status->NagWidth;
    nag.height = status->NagHeight;
    nag.prop = move_bound.prop | Property::Text | Property::Nag;
    nag.ShouldApplyScroll = true;
    elements.push_back(nag);
  }

  //---------- Move number in marge or for variation ----------
  if (indent == 0 && (!m->IsBlack || only_black)) {
    DRAW_NB(0, status->MoveHeight * line, m->Number);
  } else if (indent > 0 && (!m->IsBlack || only_black)) {
    if (only_black) {
      DRAW_NB_VAR((move_bound.x - status->MoveWidth) - status->MarginBarWidth,
                  status->MoveHeight * line, m->Number);
    } else {
      DRAW_NB_VAR(move_bound.x - ((indent + 1) / 2 * status->MarginBarWidth),
                  status->MoveHeight * line, m->Number);
    }
  }

  //---------- Draw dots ----------
  if (only_black) {
    DRAW_DOTS(move_bound.x - status->MoveWidth, move_bound.y);
  }

  //---------- Scrolling infos ----------
  if ((move_bound.x + move_bound.width) > status->MoveTableMaxX) {
    status->MoveTableMaxX = move_bound.x + move_bound.width;
  }
  if ((move_bound.y + move_bound.height) > status->MoveTableMaxY) {
    status->MoveTableMaxY = move_bound.y + move_bound.height;
  }

  //---------- Comments ----------
  if (m->comment.size() > 0) {
    line = DrawComment(m, line, indent, move_bound, indent_black);
  }

  //---------- Variations ----------
  if (m->variations.size() > 0) {
    line = DrawVariations(m, line, indent, move_bound, indent_black);
  }

  //---------- Mainline ----------
  if (m->MainLine != NULL) {
    only_black = (m->MainLine->IsBlack &&
                  (m->comment.size() > 0 || m->variations.size()));
    if (m->IsBlack) {
      line = UpdateMoves(m->MainLine, line + 1, indent, only_black);
    } else {
      line = UpdateMoves(m->MainLine, line, indent, only_black);
    }
  }

  return (line);
}

std::uint32_t MoveTable::DrawComment(CGEHalfMove *m, std::uint32_t line,
                                     std::uint32_t indent,
                                     const Element &move_bound,
                                     const char &indent_black) {
  // Show three dots
  if (!m->IsBlack) {
    DRAW_DOTS(status->MarginBarWidth + status->MoveWidth * (indent + 1) +
                  ((indent + 1) / 2 * status->MarginBarWidth),
              status->MoveHeight * line);
  }
  line++; // Goto the right line

  /// ----- Compute comment bounding box values:
  int nchar=m->comment.size();
  int nline=ceil((double)nchar/(double)status->CommentCharPerLine);
  std::uint16_t nrow=ceil(((nline*status->CommentCharHeight)+2*status->CommentPadding)/status->MoveHeight);
  int width=status->CommentCharPerLine*status->CommentCharWidth+2*status->CommentPadding;
  int height=nrow*status->MoveHeight;

  // ----- Draw comment background:
  Element e;
  e.prop = Property::Rectangle | Property::Comment;
  e.x = move_bound.x -
        (indent_black *
         status->MoveWidth); // status->MarginBarWidth + status->MoveX;
  e.y = status->MoveHeight * line;
  e.width = -1; // Negative width means expands to the edge of the canvas
  e.height = height;
  e.ShouldApplyScroll = true;
  elements.push_back(e);
  // ----- Update scrolling:
  if ((e.x + width) > status->MoveTableMaxX) {
    status->MoveTableMaxX = e.x + width;
  }
  if ((e.y + height) > status->MoveTableMaxY) {
    status->MoveTableMaxY = e.y + height;
  }
  // ----- Handle events:
  if (status->LeftClick && IsMouseOver(e)) {
    status->Events.push_back({Event::Type::CommentSelected, m});
  }
  // ----- Now draw each lines of the comment:
  Element l; // One line of the comment
  l.prop = Property::Comment|Property::Text;
  l.x=e.x+status->CommentPadding;
  l.y=e.y+status->CommentPadding;
  l.width=width;
  l.height=status->CommentCharHeight;
  l.ShouldApplyScroll = true;
  for(int i=0;i<nline;i++){
    l.text=m->comment.substr(i*status->CommentCharPerLine,status->CommentCharPerLine);
    // Remove leading space:
    if(l.text.size()>2 && l.text[0]==' '){
      l.text=l.text.substr(1,l.text.size());
    }
    elements.push_back(l);
    l.y+=status->CommentCharHeight;
  }
  // ----- Do not forget to add marging before comment if indented:
  if (indent > 0) {
    e.x -= status->MarginBarWidth;
    VariationMargins.push_back(e);
  }
  line += nrow; // Skip right amount of lines
  // ----- Since we already increment line for black later on:
  if (m->IsBlack || m->variations.size() > 0) {
    line--;
  }
  return (line);
}

std::uint32_t MoveTable::DrawVariations(CGEHalfMove *m, std::uint32_t line,
                                        std::uint32_t indent,
                                        const Element &move_bound,
                                        const char &indent_black) {
  // Show three dots next to move if white turn
  if ((m->variations.size() == 0) && !m->IsBlack) {
    DRAW_DOTS(status->MarginBarWidth + status->MoveWidth * (indent + 1),
              status->MoveHeight * line);
  }
  // Show button on the right side of the move
  {
    Element e;
    e.prop = Property::Rectangle | Property::Button;
    e.x = move_bound.x + status->MoveWidth;
    if (!m->IsBlack)
      e.x += status->MoveWidth;
    e.y = move_bound.y + std::ceil(status->MoveHeight / 4);
    e.width = std::ceil(status->MoveHeight / 2);
    e.height = e.width;
    e.ShouldApplyScroll = true;
    if (status->LeftClick && IsMouseOver(e)) {
      m->Folded = !m->Folded;
    }
    if (!m->Folded) {
      e.prop |= Property::On;
    }
    elements.push_back(e);
  }
  if (!m->Folded) {
    for (CGEHalfMove *v : m->variations) {
      // For each variation show show/hide button
      {
        Element e;
        e.prop = Property::Rectangle | Property::Button;
        e.x = (status->MarginBarWidth + status->MoveWidth * indent) +
              status->MoveWidth - std::ceil(status->MoveHeight / 2) -
              std::ceil(status->MoveHeight / 4);
        e.y = (status->MoveHeight * (line + 1)) +
              std::ceil(status->MoveHeight / 4);
        e.width = std::ceil(status->MoveHeight / 2);
        e.height = e.width;
        e.ShouldApplyScroll = true;
        if (status->LeftClick && IsMouseOver(e)) {
          v->Hide = !v->Hide;
        }
        if (!v->Hide) {
          e.prop |= Property::On;
        }
        elements.push_back(e);
      }
      if (!v->Hide) {
        line = UpdateMoves(v, line + 1, indent + 1, v->IsBlack);
      } else {
        line++;
      }
    }
  }
  // New line after variation
  if (m->MainLine != NULL && m->MainLine->IsBlack) {
    line++;
  }
  return (line);
}
} // namespace cgeditor