Subversion Repositories Games.Chess Giants

Rev

Rev 30 | Rev 59 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 pmbaty 1
// window_main.cpp
2
 
3
#include "../common.h"
4
 
5
 
21 pmbaty 6
// global variables used in this module only
7
static wchar_t folder_path[MAX_PATH];
8
 
9
 
1 pmbaty 10
// prototypes of local functions
11
static bool Button_IsHovered (guibutton_t *button, int gui_x, int gui_y);
12
static bool Button_UpdateHoverState (guibutton_t *button, int gui_x, int gui_y);
13
 
14
 
15
LRESULT CALLBACK WindowProc_Main (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
16
{
17
   // this is the main message handler for the program
18
 
19
   static wchar_t fen_string[128];
20
   static char selectable_parts[14] = "PRNBQK kqbnrp";
21
   static int prevgui_x = 0;
22
   static int prevgui_y = 0;
3 pmbaty 23
   static bool ctrl_pressed = false;
1 pmbaty 24
   static bool rbutton_pushed = false;
25
 
26
   unsigned short wParam_hiword;
27
   unsigned short wParam_loword;
28
   int prev_hovered_position[2];
29
   player_t *current_player;
30
   player_t *opposite_player;
31
   player_t *local_player;
32
   player_t *remote_player;
33
   ccreply_t *entered_ccreply;
34
   boardmove_t *current_move;
35
   float board_x;
36
   float board_y;
37
   int gui_x;
38
   int gui_y;
39
   int line;
40
   int column;
41
   int index_line;
42
   int index_column;
43
   int part_index;
44
   int part_color;
24 pmbaty 45
   int viewer_index;
1 pmbaty 46
 
47
   // filter out the commonly used message values
48
   wParam_hiword = HIWORD (wParam);
49
   wParam_loword = LOWORD (wParam);
50
 
51
   ////////////////////////////////////////////////////////////////////////////////////////////////
52
   // has the window just been fired up ?
53
   if (message == WM_CREATE)
54
   {
55
      the_scene.gui.is_entering_text = false; // we are NOT entering text yet
56
 
57
      // call the default window message processing function to keep things going
58
      return (DefWindowProc (hWnd, message, wParam, lParam));
59
   }
60
 
61
   ////////////////////////////////////////////////////////////////////////////////////////////////
62
   // else have we clicked on the close button ?
63
   else if (message == WM_CLOSE)
64
   {
65
      if (the_board.game_state == STATE_PLAYING)
66
         DialogBox_Quit (); // if a game has started, ask for confirmation
67
      else
68
         is_dialogbox_quit_validated = true; // if game hasn't started yet, quit without question
69
 
70
      return (0); // don't let Windows do the default processing on this message
71
   }
72
 
73
   ////////////////////////////////////////////////////////////////////////////////////////////////
74
   // else is the user closing its session OR the window is being destroyed ?
75
   else if ((message == WM_QUERYENDSESSION) || (message == WM_ENDSESSION) || (message == WM_DESTROY))
76
      terminate_everything = true; // suicide (for some reason, PostQuitMessage() is unreliable !?)
77
 
21 pmbaty 78
   /////////////////
79
   // MENU EVENTS //
80
   /////////////////
81
 
82
   ///////////////
83
   // menu command
1 pmbaty 84
   else if (message == WM_COMMAND)
85
   {
86
      // game menu, new game
87
      if (wParam_loword == MENUID_GAME_NEWGAME)
88
      {
89
         if ((the_board.game_state == STATE_PLAYING) && (the_board.move_count > 1))
90
            DialogBox_Resign (); // if a game is playing, ask to resign first
91
         else
92
            DialogBox_NewGame (); // game not started yet, show the new game dialog box
93
      }
94
 
95
      // game menu, setup start position
96
      else if (wParam_loword == MENUID_GAME_SETUPPOSITION)
97
      {
98
         if ((the_board.game_state == STATE_PLAYING) && (the_board.move_count > 1))
99
            DialogBox_Resign (); // if a game is playing, ask to resign first
100
         else
101
         {
102
            current_move = &the_board.moves[0]; // quick access to start move
103
            memset (&current_move->slots, 0, sizeof (current_move->slots)); // erase all slots
104
 
105
            the_board.game_state = STATE_SETUPPOSITION; // game not started yet, enter board setup mode
106
            the_board.reevaluate = true; // evaluate board again
107
 
108
            // display the "please choose start position" phrase in the middle of the screen
109
            Scene_SetText (&the_scene.gui.central_text, 50.0f, 50.0f, -1, ALIGN_CENTER, ALIGN_CENTER, ALIGN_CENTER, centermsg_fontindex, RGBA_TO_RGBACOLOR (255, 255, 255, 191),
110
                           999999.0f, true, LOCALIZE (L"SetupMode"));
111
            the_scene.gui.partspick_selectedpart = ' '; // no selected part yet
112
            the_scene.update = true; // and update the 3D scene
113
         }
114
      }
115
 
116
      // game menu, load game
117
      else if (wParam_loword == MENUID_GAME_LOAD)
118
         DialogBox_Load (); // fire up the load dialog box
119
 
120
      // game menu, save game
121
      else if (wParam_loword == MENUID_GAME_SAVE)
122
      {
123
         if (save_pathname[0] != 0)
124
            is_dialogbox_save_validated = true; // if the filename is known, save it directly
125
         else
126
            DialogBox_Save (); // else fire up the save dialog box
127
      }
128
 
129
      // game menu, save as
130
      else if (wParam_loword == MENUID_GAME_SAVEAS)
131
         DialogBox_Save (); // fire up the save dialog box
132
 
133
      // game menu, save position as
134
      else if (wParam_loword == MENUID_GAME_SAVEPOSITIONAS)
135
         DialogBox_SavePosition (); // fire up the save position dialog box
136
 
137
      // game menu, resign
138
      else if (wParam_loword == MENUID_GAME_RESIGN)
139
         DialogBox_Resign (); // if a game has started, ask for confirmation
140
 
141
      // game menu, statistics (stats are only available in online mode)
142
      else if (wParam_loword == MENUID_GAME_STATISTICS)
143
      {
144
         local_player = Player_FindByType (PLAYER_HUMAN);
145
         if (local_player != NULL)
146
            PlayerCard_FindOrCreate (local_player->name); // display player's own player card
147
      }
148
 
149
      // game menu, options
150
      else if (wParam_loword == MENUID_GAME_OPTIONS)
151
         DialogBox_Options (); // fire up the options dialog box
152
 
153
      // game menu, quit
154
      else if (wParam_loword == MENUID_GAME_QUIT)
155
      {
156
         if (the_board.game_state == STATE_PLAYING)
157
            DialogBox_Quit (); // if a game has started, ask for confirmation
158
         else
159
            is_dialogbox_quit_validated = true; // if game hasn't started yet, quit without question
160
      }
161
 
162
      // chessboard menu, suggest move
163
      else if (wParam_loword == MENUID_CHESSBOARD_SUGGESTMOVE)
164
         the_board.players[current_viewer].wants_hint = true; // remember this player wants a hint
165
 
166
      // chessboard menu, cancel last move
167
      else if (wParam_loword == MENUID_CHESSBOARD_CANCELLASTMOVE)
168
         the_board.players[current_viewer].wants_cancel = true; // remember this player wants to cancel his last move
169
 
170
      // chessboard menu, comment move
171
      else if (wParam_loword == MENUID_CHESSBOARD_COMMENTMOVE)
172
         DialogBox_Comment (); // fire up the comment dialog box
173
 
174
      // chessboard menu, go to move
175
      else if (wParam_loword == MENUID_CHESSBOARD_GOTOMOVE)
176
         DialogBox_GoToMove (); // fire up the go to move dialog box
177
 
178
      // chessboard menu, swap sides
179
      else if (wParam_loword == MENUID_CHESSBOARD_SWAPSIDES)
180
         the_board.want_playerswap = true; // remember board sides are to be swapped
181
 
182
      // chessboard menu, beginning of game
183
      else if (wParam_loword == MENUID_CHESSBOARD_BEGINNINGOFGAME)
184
      {
185
         the_board.viewed_move = 0; // enter view mode and go to the beginning of the game
186
         Audio_PlaySound (SOUNDTYPE_CLICK); // make a click sound
187
         the_board.reevaluate = true; // evaluate board again
188
      }
189
 
190
      // chessboard menu, previous move (only if arrow is enabled)
191
      else if ((wParam_loword == MENUID_CHESSBOARD_PREVIOUSMOVE) && (the_scene.gui.larrow.state != 0))
192
      {
193
         the_board.viewed_move--; // enter view mode and go back one move
194
         Audio_PlaySound (SOUNDTYPE_CLICK); // make a click sound
195
         the_board.reevaluate = true; // evaluate board again
196
      }
197
 
198
      // chessboard menu, next move (only if arrow is enabled)
199
      else if ((wParam_loword == MENUID_CHESSBOARD_NEXTMOVE) && (the_scene.gui.rarrow.state != 0))
200
      {
201
         the_board.viewed_move++; // enter view mode and go forward one move
202
         Audio_PlaySound (SOUNDTYPE_CLICK); // make a click sound
203
         the_board.reevaluate = true; // evaluate board again
204
      }
205
 
206
      // chessboard menu, current state of game
207
      else if (wParam_loword == MENUID_CHESSBOARD_CURRENTSTATEOFGAME)
208
      {
209
         the_board.viewed_move = the_board.move_count - 1; // enter view mode and go to the current state of the game
210
         Audio_PlaySound (SOUNDTYPE_CLICK); // make a click sound
211
         the_board.reevaluate = true; // evaluate board again
212
      }
213
 
214
      // chessboard menu, top view
215
      else if (wParam_loword == MENUID_CHESSBOARD_TOPVIEW)
216
      {
24 pmbaty 217
         // cycle through both players and change their view angles EXCEPT the opponent if he's human
218
         for (viewer_index = 0; viewer_index < 2; viewer_index++)
219
            if ((the_board.players[viewer_index].type == PLAYER_COMPUTER)
220
                || (the_board.players[viewer_index].type == PLAYER_INTERNET)
221
                || (viewer_index == current_viewer))
222
            {
25 pmbaty 223
               the_board.players[viewer_index].view_pitch = 89.99f;
24 pmbaty 224
               the_board.players[viewer_index].view_yaw = (current_viewer == COLOR_BLACK ? 90.0f : -90.0f);
225
               the_board.players[viewer_index].view_distance = 58.0f;
226
            }
1 pmbaty 227
      }
228
 
229
      // chessboard menu, default view
230
      else if (wParam_loword == MENUID_CHESSBOARD_DEFAULTVIEW)
231
      {
24 pmbaty 232
         // cycle through both players and change their view angles EXCEPT the opponent if he's human
233
         for (viewer_index = 0; viewer_index < 2; viewer_index++)
234
            if ((the_board.players[viewer_index].type == PLAYER_COMPUTER)
235
                || (the_board.players[viewer_index].type == PLAYER_INTERNET)
236
                || (viewer_index == current_viewer))
237
            {
238
               the_board.players[viewer_index].view_pitch = 55.0f;
239
               the_board.players[viewer_index].view_yaw = (current_viewer == COLOR_BLACK ? 90.0f : -90.0f);
240
               the_board.players[viewer_index].view_distance = 70.0f;
241
            }
1 pmbaty 242
      }
243
 
244
      // chessboard menu, reset view
245
      else if (wParam_loword == MENUID_CHESSBOARD_RESETVIEW)
246
      {
24 pmbaty 247
         // cycle through both players and change their view angles EXCEPT the opponent if he's human
248
         for (viewer_index = 0; viewer_index < 2; viewer_index++)
249
            if ((the_board.players[viewer_index].type == PLAYER_COMPUTER)
250
                || (the_board.players[viewer_index].type == PLAYER_INTERNET)
251
                || (viewer_index == current_viewer))
252
            {
253
               the_board.players[viewer_index].view_pitch = the_board.players[current_viewer].custom_pitch;
254
               the_board.players[viewer_index].view_yaw = the_board.players[current_viewer].custom_yaw;
255
               the_board.players[viewer_index].view_distance = the_board.players[current_viewer].custom_distance;
256
            }
1 pmbaty 257
      }
258
 
259
      // chessboard menu, zoom in
260
      else if (wParam_loword == MENUID_CHESSBOARD_ZOOMIN)
261
      {
262
         // scroll up & save this as the new custom distance
263
         the_board.players[current_viewer].view_distance = max (48.0f, the_board.players[current_viewer].view_distance - 2.0f);
264
         the_board.players[current_viewer].custom_distance = the_board.players[current_viewer].view_distance;
265
         the_scene.update = true; // update the 3D scene
266
      }
267
 
268
      // chessboard menu, zoom out
269
      else if (wParam_loword == MENUID_CHESSBOARD_ZOOMOUT)
270
      {
271
         // scroll down & save this as the new custom distance
272
         the_board.players[current_viewer].view_distance = min (100.0f, the_board.players[current_viewer].view_distance + 2.0f);
273
         the_board.players[current_viewer].custom_distance = the_board.players[current_viewer].view_distance;
274
         the_scene.update = true; // update the 3D scene
275
      }
276
 
277
      // chessboard menu, change appearance
278
      else if (wParam_loword == MENUID_CHESSBOARD_CHANGEAPPEARANCE)
279
         DialogBox_ChangeAppearance ();
280
 
281
      // chessboard menu, display windows desktop
282
      else if (wParam_loword == MENUID_CHESSBOARD_DISPLAYWINDOWSDESKTOP)
283
         ShowWindow (hWnd, SW_MINIMIZE);
284
 
285
      // internet menu, show online players
286
      else if (wParam_loword == MENUID_INTERNET_SHOWONLINEPLAYERS)
287
         Window_Opponents ();
288
 
289
      // internet menu, show sought games
290
      else if (wParam_loword == MENUID_INTERNET_SHOWSOUGHTGAMES)
291
         Window_Sought ();
292
 
293
      // internet menu, seek game
294
      else if (wParam_loword == MENUID_INTERNET_SEEKGAME)
295
         DialogBox_SendSeek ();
296
 
297
      // internet menu, chatter channels
298
      else if (wParam_loword == MENUID_INTERNET_CHATTERCHANNELS)
299
         Window_ChatterChannels ();
300
 
301
      // internet menu, enter chat text
302
      else if (wParam_loword == MENUID_INTERNET_ENTERCHATTEXT)
303
         PostMessage (hWnd, WM_CHAR, L' ', 0); // emulate a space bar hit
304
 
305
      // internet menu, display player card
306
      else if (wParam_loword == MENUID_INTERNET_DISPLAYPLAYERCARD)
307
         DialogBox_PlayerInfoName ();
308
 
309
      // internet menu, display your card
310
      else if (wParam_loword == MENUID_INTERNET_DISPLAYYOURCARD)
311
      {
312
         local_player = Player_FindByType (PLAYER_HUMAN);
313
         if (local_player != NULL)
314
            PlayerCard_FindOrCreate (local_player->name); // display player's own player card
315
      }
316
 
317
      // internet menu, MOTD
318
      else if (wParam_loword == MENUID_INTERNET_MOTD)
319
         Window_MOTD ();
320
 
321
      // help menu, help
322
      else if (wParam_loword == MENUID_HELP_HELP)
29 pmbaty 323
      {
30 pmbaty 324
         swprintf_s (folder_path, WCHAR_SIZEOF (folder_path), L"%s\\Chess Giants (%s).html", app_path, os_language);
29 pmbaty 325
         ShellExecute (NULL, L"open", folder_path, NULL, NULL, SW_MAXIMIZE); // fire up the help file
326
      }
1 pmbaty 327
 
328
      // help menu, get chess games
329
      else if (wParam_loword == MENUID_HELP_GETCHESSGAMES)
330
         ShellExecute (NULL, L"open", L"http://www.chessgames.com", NULL, NULL, SW_MAXIMIZE); // fire up the browser
331
 
21 pmbaty 332
      // help menu, add/modify visual themes
333
      else if (wParam_loword == MENUID_HELP_ADDMODIFYVISUALTHEMES)
334
      {
335
         swprintf_s (folder_path, WCHAR_SIZEOF (folder_path), L"%s\\themes", app_path);
336
         ShellExecute (NULL, L"open", folder_path, NULL, NULL, SW_SHOWNORMAL); // fire up Windows Explorer
337
      }
338
 
50 pmbaty 339
      // help menu, add/modify engines
340
      else if (wParam_loword == MENUID_HELP_ADDMODIFYENGINES)
341
      {
342
         swprintf_s (folder_path, WCHAR_SIZEOF (folder_path), L"%s\\engines", app_path);
343
         ShellExecute (NULL, L"open", folder_path, NULL, NULL, SW_SHOWNORMAL); // fire up Windows Explorer
344
      }
345
 
21 pmbaty 346
      // help menu, add/modify translations
347
      else if (wParam_loword == MENUID_HELP_ADDMODIFYTRANSLATIONS)
348
      {
349
         swprintf_s (folder_path, WCHAR_SIZEOF (folder_path), L"%s\\data\\languages", app_path);
350
         ShellExecute (NULL, L"open", folder_path, NULL, NULL, SW_SHOWNORMAL); // fire up Windows Explorer
351
      }
352
 
1 pmbaty 353
      // help menu, about
354
      else if (wParam_loword == MENUID_HELP_ABOUT)
355
         DialogBox_About (); // show the About dialog box
356
 
357
      // call the default window message processing function to keep things going
358
      return (DefWindowProc (hWnd, message, wParam, lParam));
359
   }
360
 
3 pmbaty 361
   /////////////////////////////////////////////////////
362
   // ctrl key press or release (while not in animation)
363
   else if ((message == WM_KEYDOWN) && (wParam == VK_CONTROL) && (animation_endtime + 1.0f < current_time))
364
   {
365
      ctrl_pressed = true; // remember the ctrl key is pressed
366
 
367
      // call the default window message processing function to keep things going
368
      return (DefWindowProc (hWnd, message, wParam, lParam));
369
   }
370
   else if ((message == WM_KEYUP) && (wParam == VK_CONTROL) && (animation_endtime + 1.0f < current_time))
371
   {
372
      ctrl_pressed = false; // remember the ctrl key is released
373
      rbutton_pushed = false; // remember button is released
374
      the_scene.update = true; // update the 3D scene
375
 
376
      // call the default window message processing function to keep things going
377
      return (DefWindowProc (hWnd, message, wParam, lParam));
378
   }
379
 
21 pmbaty 380
   //////////////////
381
   // MOUSE EVENTS //
382
   //////////////////
383
 
1 pmbaty 384
   ////////////////////////////////////////////////////////////////////////////////////////////////
21 pmbaty 385
   // left mouse button push
386
   else if (message == WM_LBUTTONDOWN)
3 pmbaty 387
   {
21 pmbaty 388
      // are we in animation OR are mouse commands NOT allowed ?
389
      if ((animation_endtime + 1.0f >= current_time) || (command_ignoretime >= current_time))
390
         return (DefWindowProc (hWnd, message, wParam, lParam)); // if so, call the default message proc to keep things going
391
 
3 pmbaty 392
      // is the ctrl key pressed (emulates a right click) ?
393
      if (ctrl_pressed)
394
      {
395
         prevgui_x = GET_X_LPARAM (lParam); // remember mouse coordinates
396
         prevgui_y = GET_Y_LPARAM (lParam);
397
 
398
         // if we click something, stop moving the table immediately
399
 
24 pmbaty 400
         // cycle through both players and change their view angles EXCEPT the opponent if he's human
401
         for (viewer_index = 0; viewer_index < 2; viewer_index++)
402
            if ((the_board.players[viewer_index].type == PLAYER_COMPUTER)
403
                || (the_board.players[viewer_index].type == PLAYER_INTERNET)
404
                || (viewer_index == current_viewer))
405
            {
406
               the_board.players[viewer_index].view_pitch = current_pitch;
407
               the_board.players[viewer_index].view_yaw = current_yaw;
408
            }
409
 
3 pmbaty 410
         rbutton_pushed = true; // remember button is clicked
411
      }
412
 
413
      // call the default window message processing function to keep things going
414
      return (DefWindowProc (hWnd, message, wParam, lParam));
415
   }
416
 
417
   ////////////////////////////////////////////////////////////////////////////////////////////////
21 pmbaty 418
   // left mouse button release
419
   else if (message == WM_LBUTTONUP)
1 pmbaty 420
   {
21 pmbaty 421
      // are we in animation OR are mouse commands NOT allowed ?
422
      if ((animation_endtime + 1.0f >= current_time) || (command_ignoretime >= current_time))
423
         return (DefWindowProc (hWnd, message, wParam, lParam)); // if so, call the default message proc to keep things going
424
 
3 pmbaty 425
      // is the ctrl key pressed (emulates a right click) ?
426
      if (ctrl_pressed)
427
      {
428
         rbutton_pushed = false; // remember button is released
429
         the_scene.update = true; // update the 3D scene
430
 
431
         // call the default window message processing function to keep things going
432
         return (DefWindowProc (hWnd, message, wParam, lParam));
433
      }
434
 
1 pmbaty 435
      prevgui_x = GET_X_LPARAM (lParam); // remember mouse coordinates
436
      prevgui_y = GET_Y_LPARAM (lParam);
437
 
438
      // get mouse coordinates
439
      gui_x = GET_X_LPARAM (lParam);
440
      gui_y = GET_Y_LPARAM (lParam);
441
 
442
      // is the left arrow displayed AND is the mouse hovering it ?
443
      if ((the_scene.gui.larrow.state != 0) && Button_IsHovered (&the_scene.gui.larrow, gui_x, gui_y))
444
         SendMessage (hWnd, WM_COMMAND, MENUID_CHESSBOARD_PREVIOUSMOVE, NULL); // send a "previous move" event
445
 
446
      // is the right arrow displayed AND is the mouse hovering it ?
447
      if ((the_scene.gui.rarrow.state != 0) && Button_IsHovered (&the_scene.gui.rarrow, gui_x, gui_y))
448
         SendMessage (hWnd, WM_COMMAND, MENUID_CHESSBOARD_NEXTMOVE, NULL); // send a "next move" event
449
 
450
      // is the chat button displayed AND is the mouse hovering it ?
451
      if ((the_scene.gui.chatbutton.state != 0) && Button_IsHovered (&the_scene.gui.chatbutton, gui_x, gui_y))
452
      {
453
         opposite_player = Player_FindByType (PLAYER_INTERNET); // get a hand on the remote player
454
 
455
         // find or create the corresponding interlocutor structure and fire it up
456
         if (opposite_player != NULL)
457
            Interlocutor_FindOrCreate (opposite_player->name);
458
      }
459
 
460
      // is the games button displayed AND is the mouse hovering it ?
461
      if ((the_scene.gui.gamesbutton.state != 0) && Button_IsHovered (&the_scene.gui.gamesbutton, gui_x, gui_y))
462
         Window_Sought (); // if so, display the sought games window
463
 
464
      // is the people button displayed AND is the mouse hovering it ?
465
      if ((the_scene.gui.peoplebutton.state != 0) && Button_IsHovered (&the_scene.gui.peoplebutton, gui_x, gui_y))
466
         Window_Opponents (); // if so, display the opponents window
467
 
468
      // is the parts selection line displayed AND is the mouse anywhere near it ?
469
      if (the_scene.gui.is_partspick_displayed && Render_IsMouseInBox (gui_x, gui_y, 0.0f, 0.0f, 100.0f, 11.0f))
470
      {
471
         // for each selectable part, if the mouse is on it, mark it as selected
472
         for (part_index = 0; part_index < 13; part_index++)
473
            if (Render_IsMouseInBox (gui_x, gui_y, part_index * (100.0f / 13.0f), 0, 100.0f / 13.0f, 11.0f))
474
            {
475
               the_scene.gui.partspick_selectedpart = selectable_parts[part_index]; // mark it as selected
476
               Audio_PlaySound (SOUNDTYPE_CLICK); // make a click sound
477
               break; // no need to search further if one selection was found
478
            }
479
      }
480
 
481
      // get current player and see if we're online
482
      current_player = Player_GetCurrent ();
483
      remote_player = Player_FindByType (PLAYER_INTERNET);
484
 
485
      // if we are not allowed to select anything, don't even try
486
      if ((current_player->type != PLAYER_HUMAN) || (the_board.viewed_move != the_board.move_count - 1)
487
          || ((remote_player != NULL) && !remote_player->is_in_game))
488
      {
489
         the_scene.update = true; // update the 3D scene
490
 
491
         // call the default window message processing function to keep things going
492
         return (DefWindowProc (hWnd, message, wParam, lParam));
493
      }
494
 
495
      // it's a single click. The user clicked on a square.
496
 
497
      // figure out the coordinates on table
498
      Render_MouseToFloor (gui_x, gui_y, &board_x, &board_y);
499
 
500
      // translate them to board coordinates
501
      the_board.hovered_position[0] = (int) floor ((20.0f - board_y) / 5.0f);
502
      the_board.hovered_position[1] = 7 - (int) floor ((20.0f - board_x) / 5.0f);
503
      highlight_endtime = 0; // stop highlighting anything
504
 
505
      // if it's outside the table, end the job
506
      if (!IS_VALID_POSITION (the_board.hovered_position))
507
      {
508
         the_scene.update = true; // update the 3D scene
509
 
510
         // call the default window message processing function to keep things going
511
         return (DefWindowProc (hWnd, message, wParam, lParam));
512
      }
513
 
514
      // we clicked a valid slot on the table
515
 
516
      current_move = &the_board.moves[the_board.viewed_move]; // quick access to current move
517
 
518
      ///////////////////////////////////
519
      // are we in parts placement mode ?
520
      if (the_board.game_state == STATE_SETUPPOSITION)
521
      {
522
         // quick access to line and column
523
         line = the_board.hovered_position[0];
524
         column = the_board.hovered_position[1];
525
 
526
         // figure out the color of the part we are placing
527
         if (the_scene.gui.partspick_selectedpart == tolower (the_scene.gui.partspick_selectedpart))
528
            part_color = COLOR_BLACK; // black color
529
         else
530
            part_color = COLOR_WHITE; // white color
531
 
532
         // are we erasing a king ? if so, replace it at its default location
533
         if ((current_move->slots[line][column].part == PART_KING) && (current_move->slots[line][column].color == COLOR_BLACK)
534
             && (the_scene.gui.partspick_selectedpart != 'k'))
535
         {
536
            // is it ALREADY the king's default location ? if so, give up
537
            if ((line == 7) && (column == 4))
538
            {
539
               Audio_PlaySound (SOUNDTYPE_ILLEGALMOVE); // play the "illegal move" sound
540
               return (DefWindowProc (hWnd, message, wParam, lParam)); // call the default window message proc
541
            }
542
 
543
            Move_SetSlot (current_move, 7, 4, COLOR_BLACK, PART_KING); // replace black king
544
         }
545
         else if ((current_move->slots[line][column].part == PART_KING) && (current_move->slots[line][column].color == COLOR_WHITE)
546
                  && (the_scene.gui.partspick_selectedpart != 'K'))
547
         {
548
            // is it ALREADY the king's default location ? if so, give up
549
            if ((line == 0) && (column == 4))
550
            {
551
               Audio_PlaySound (SOUNDTYPE_ILLEGALMOVE); // play the "illegal move" sound
552
               return (DefWindowProc (hWnd, message, wParam, lParam)); // call the default window message proc
553
            }
554
 
555
            Move_SetSlot (current_move, 0, 4, COLOR_WHITE, PART_KING); // replace white king
556
         }
557
 
558
         // place the selected part on the board
559
         if (tolower (the_scene.gui.partspick_selectedpart) == 'p')
560
            Move_SetSlot (current_move, line, column, part_color, PART_PAWN); // pawn
561
         else if (tolower (the_scene.gui.partspick_selectedpart) == 'r')
562
            Move_SetSlot (current_move, line, column, part_color, PART_ROOK); // rook
563
         else if (tolower (the_scene.gui.partspick_selectedpart) == 'n')
564
            Move_SetSlot (current_move, line, column, part_color, PART_KNIGHT); // knight
565
         else if (tolower (the_scene.gui.partspick_selectedpart) == 'b')
566
            Move_SetSlot (current_move, line, column, part_color, PART_BISHOP); // bishop
567
         else if (tolower (the_scene.gui.partspick_selectedpart) == 'q')
568
            Move_SetSlot (current_move, line, column, part_color, PART_QUEEN); // queen
569
         else if (tolower (the_scene.gui.partspick_selectedpart) == 'k')
570
         {
571
            // parse the board for other kings of the same color and erase them
572
            for (index_line = 0; index_line < 8; index_line++)
573
               for (index_column = 0; index_column < 8; index_column++)
574
                  if ((current_move->slots[index_line][index_column].color == part_color)
575
                      && (current_move->slots[index_line][index_column].part == PART_KING))
576
                     memset (&current_move->slots[index_line][index_column], 0, sizeof (boardslot_t)); // erase this slot
577
 
578
            Move_SetSlot (current_move, line, column, part_color, PART_KING); // king
579
         }
580
         else
581
            Move_SetSlot (current_move, line, column, 0, PART_NONE); // no part
582
 
583
         // figure out which sound to play
584
         if (the_scene.gui.partspick_selectedpart != ' ')
585
            Audio_PlaySound (SOUNDTYPE_MOVE); // play a move sound when placing a part
586
 
587
         the_scene.update = true; // update the 3D scene
588
 
589
         // call the default window message processing function to keep things going
590
         return (DefWindowProc (hWnd, message, wParam, lParam));
591
      }
592
      // end of parts placement mode
593
      //////////////////////////////
594
 
595
      // does a selection NOT exist yet ?
596
      if (!IS_VALID_POSITION (the_board.selected_position))
597
      {
598
         // is there a selectable part at this location ?
599
         if ((current_move->slots[the_board.hovered_position[0]][the_board.hovered_position[1]].part != PART_NONE)
600
            && (current_move->slots[the_board.hovered_position[0]][the_board.hovered_position[1]].color == Board_ColorToMove (&the_board)))
601
         {
602
            // mark the selected position as selected (and remember it)
603
            the_board.selected_position[0] = the_board.hovered_position[0];
604
            the_board.selected_position[1] = the_board.hovered_position[1];
605
         }
606
 
607
         the_scene.update = true; // update the 3D scene
608
 
609
         // call the default window message processing function to keep things going
610
         return (DefWindowProc (hWnd, message, wParam, lParam));
611
      }
612
 
613
      // a selection exists already
614
 
615
      // is it the slot that was previously selected ? (i.e, user wants to "unselect" it)
616
      if ((the_board.hovered_position[0] == the_board.selected_position[0]) && (the_board.hovered_position[1] == the_board.selected_position[1]))
617
      {
618
         // forget the selected position
619
         the_board.selected_position[0] = -1;
620
         the_board.selected_position[1] = -1;
621
 
622
         the_scene.update = true; // update the 3D scene
623
 
624
         // call the default window message processing function to keep things going
625
         return (DefWindowProc (hWnd, message, wParam, lParam));
626
      }
627
 
628
      // else is it another part of the same color ? (i.e, user wants to change the part he selected)
629
      else if ((current_move->slots[the_board.hovered_position[0]][the_board.hovered_position[1]].part != PART_NONE)
630
               && (current_move->slots[the_board.hovered_position[0]][the_board.hovered_position[1]].color == Board_ColorToMove (&the_board)))
631
      {
632
         // mark the selected position as selected (and remember it)
633
         the_board.selected_position[0] = the_board.hovered_position[0];
634
         the_board.selected_position[1] = the_board.hovered_position[1];
635
 
636
         the_scene.update = true; // update the 3D scene
637
 
638
         // call the default window message processing function to keep things going
639
         return (DefWindowProc (hWnd, message, wParam, lParam));
640
      }
641
 
642
      // else is it a possible move ?
643
      else if ((current_move->slots[the_board.hovered_position[0]][the_board.hovered_position[1]].flags & FLAG_POSSIBLEMOVE)
644
               || (current_move->slots[the_board.hovered_position[0]][the_board.hovered_position[1]].flags & FLAG_TAKEABLE))
645
      {
646
         // are we in check after the move ? (FIXME: call EP version of this func for en passant moves)
647
         if (Move_IsColorInCheckAfterTestMove (current_move, the_board.selected_position[0], the_board.selected_position[1], the_board.hovered_position[0], the_board.hovered_position[1], Board_ColorToMove (&the_board)))
648
         {
649
            Audio_PlaySound (SOUNDTYPE_ILLEGALMOVE); // play the "illegal move" sound
650
 
651
            the_scene.update = true; // update the 3D scene
652
 
653
            // call the default window message processing function to keep things going
654
            return (DefWindowProc (hWnd, message, wParam, lParam));
655
         }
656
 
657
         ////////////////////
658
         // movement is valid
659
 
660
         // do it
661
         Board_AppendMove (&the_board, the_board.selected_position[0], the_board.selected_position[1], the_board.hovered_position[0], the_board.hovered_position[1], PART_NONE, NULL);
662
         current_move = &the_board.moves[the_board.move_count - 1]; // update current move pointer
663
 
664
         // are we in internet mode ?
665
         if (remote_player != NULL)
666
            the_board.reevaluate = false; // if so, don't reevaluate the board yet, let the server do it
667
 
668
         // was it a pawn being promoted ? if so, display the dialog box and wait for the reply
669
         if ((current_move->slots[the_board.hovered_position[0]][the_board.hovered_position[1]].part == PART_PAWN)
670
             && ((the_board.hovered_position[0] == 0) || (the_board.hovered_position[0] == 7)))
671
            DialogBox_PawnPromotion (); // display the pawn promotion dialog box
672
 
673
         // else it was a normal move
674
         else
675
         {
676
            Board_SetSelectedAndHovered (&the_board, -1, -1, -1, -1); // forget the hovered and selected positions
677
            the_board.has_playerchanged = true; // and switch players
678
         }
679
 
680
         the_scene.update = true; // update the 3D scene
681
         animation_endtime = current_time + ANIMATION_DURATION; // play animation now
682
         sound_playtime = current_time + ANIMATION_DURATION - 0.1f; // play sound near the end of animation
683
 
684
         // call the default window message processing function to keep things going
685
         return (DefWindowProc (hWnd, message, wParam, lParam));
686
      }
687
 
688
      // else it's another location
689
 
690
      Audio_PlaySound (SOUNDTYPE_ILLEGALMOVE); // play the "illegal move" sound
691
 
692
      the_scene.update = true; // update the 3D scene
693
 
694
      // call the default window message processing function to keep things going
695
      return (DefWindowProc (hWnd, message, wParam, lParam));
696
   }
697
 
698
   ////////////////////////////////////////////////////////////////////////////////////////////////
21 pmbaty 699
   // right mouse button push
700
   else if (message == WM_RBUTTONDOWN)
1 pmbaty 701
   {
21 pmbaty 702
      // are we in animation OR are mouse commands NOT allowed ?
703
      if ((animation_endtime + 1.0f >= current_time) || (command_ignoretime >= current_time))
704
         return (DefWindowProc (hWnd, message, wParam, lParam)); // if so, call the default message proc to keep things going
705
 
1 pmbaty 706
      prevgui_x = GET_X_LPARAM (lParam); // remember mouse coordinates
707
      prevgui_y = GET_Y_LPARAM (lParam);
708
 
709
      // if we click something, stop moving the table immediately
710
 
24 pmbaty 711
      // cycle through both players and change their view angles EXCEPT the opponent if he's human
712
      for (viewer_index = 0; viewer_index < 2; viewer_index++)
713
         if ((the_board.players[viewer_index].type == PLAYER_COMPUTER)
714
               || (the_board.players[viewer_index].type == PLAYER_INTERNET)
715
               || (viewer_index == current_viewer))
716
         {
717
            the_board.players[viewer_index].view_pitch = current_pitch;
718
            the_board.players[viewer_index].view_yaw = current_yaw;
719
         }
720
 
1 pmbaty 721
      rbutton_pushed = true; // remember button is clicked
722
 
723
      // call the default window message processing function to keep things going
724
      return (DefWindowProc (hWnd, message, wParam, lParam));
725
   }
726
 
727
   ////////////////////////////////////////////////////////////////////////////////////////////////
21 pmbaty 728
   // right mouse button release
729
   else if (message == WM_RBUTTONUP)
1 pmbaty 730
   {
21 pmbaty 731
      // are we in animation OR are mouse commands NOT allowed ?
732
      if ((animation_endtime + 1.0f >= current_time) || (command_ignoretime >= current_time))
733
         return (DefWindowProc (hWnd, message, wParam, lParam)); // if so, call the default message proc to keep things going
734
 
1 pmbaty 735
      rbutton_pushed = false; // remember button is released
736
      the_scene.update = true; // update the 3D scene
737
 
738
      // call the default window message processing function to keep things going
739
      return (DefWindowProc (hWnd, message, wParam, lParam));
740
   }
741
 
21 pmbaty 742
   ////////////////////////////////////////////////////////////////////////////////////////////////
1 pmbaty 743
   // left mouse button DOUBLE-click
21 pmbaty 744
   else if (message == WM_LBUTTONDBLCLK)
1 pmbaty 745
   {
21 pmbaty 746
      // are we in animation OR are mouse commands NOT allowed ?
747
      if ((animation_endtime + 1.0f >= current_time) || (command_ignoretime >= current_time))
748
         return (DefWindowProc (hWnd, message, wParam, lParam)); // if so, call the default message proc to keep things going
749
 
1 pmbaty 750
      // get mouse coordinates
751
      gui_x = GET_X_LPARAM (lParam);
752
      gui_y = GET_Y_LPARAM (lParam);
753
 
754
      // are we in game and did we click the move comments area ?
755
      if ((the_board.game_state >= STATE_PLAYING) && (the_board.viewed_move > 0) && Render_IsMouseInBox (gui_x, gui_y, 10.0f, 0.0f, 80.0f, 10.0f))
756
      {
757
         DialogBox_Comment (); // fire up the comment dialog box
758
         return (DefWindowProc (hWnd, message, wParam, lParam)); // call the default window message processing function to keep things going
759
      }
21 pmbaty 760
 
761
      // call the default window message processing function to keep things going
762
      return (DefWindowProc (hWnd, message, wParam, lParam));
1 pmbaty 763
   }
764
 
765
   ////////////////////////////////////////////////////////////////////////////////////////////////
766
   // mouse move (while not in animation)
21 pmbaty 767
   else if (message == WM_MOUSEMOVE)
1 pmbaty 768
   {
21 pmbaty 769
      // are we in animation OR are mouse commands NOT allowed ?
770
      if ((animation_endtime + 1.0f >= current_time) || (command_ignoretime >= current_time))
771
         return (DefWindowProc (hWnd, message, wParam, lParam)); // if so, call the default message proc to keep things going
772
 
1 pmbaty 773
      // get mouse coordinates
774
      gui_x = GET_X_LPARAM (lParam);
775
      gui_y = GET_Y_LPARAM (lParam);
776
 
777
      // handle button update status
778
      the_scene.update |= Button_UpdateHoverState (&the_scene.gui.larrow, gui_x, gui_y);
779
      the_scene.update |= Button_UpdateHoverState (&the_scene.gui.rarrow, gui_x, gui_y);
780
      the_scene.update |= Button_UpdateHoverState (&the_scene.gui.chatbutton, gui_x, gui_y);
781
      the_scene.update |= Button_UpdateHoverState (&the_scene.gui.gamesbutton, gui_x, gui_y);
782
      the_scene.update |= Button_UpdateHoverState (&the_scene.gui.peoplebutton, gui_x, gui_y);
783
 
784
      // see if we're online
785
      remote_player = Player_FindByType (PLAYER_INTERNET);
786
 
787
      // are we online AND do we NOT have the right to select anything ?
788
      if ((remote_player != NULL) && !remote_player->is_in_game)
789
         return (DefWindowProc (hWnd, message, wParam, lParam)); // if so, call the default window message processing function to keep things going
790
 
791
      // is the parts selection line displayed AND is the mouse anywhere in it ?
792
      if (the_scene.gui.is_partspick_displayed && Render_IsMouseInBox (gui_x, gui_y, 0.0f, 0.0f, 100.0f, 11.0f))
793
      {
794
         // for each selectable part...
795
         for (part_index = 0; part_index < 13; part_index++)
796
         {
797
            // is the mouse hovering it ?
798
            if (Render_IsMouseInBox (gui_x, gui_y, part_index * (100.0f / 13.0f), 0, 100.0f / 13.0f, 11.0f))
799
            {
800
               // was it NOT hovered before ?
801
               if (the_scene.gui.partspick_hoveredpart != selectable_parts[part_index])
802
               {
803
                  the_scene.gui.partspick_hoveredpart = selectable_parts[part_index]; // mark it as hovered
804
                  the_scene.update = true; // update the scene
805
               }
806
            }
807
 
808
            // else was it hovered before ?
809
            else if (the_scene.gui.partspick_hoveredpart == selectable_parts[part_index])
810
            {
811
               the_scene.gui.partspick_hoveredpart = 0; // clear the hovered part
812
               the_scene.update = true; // update the scene
813
            }
814
         }
815
      }
816
      else
817
         the_scene.gui.partspick_hoveredpart = 0; // clear the hovered part
818
 
819
      // get current and opposite players
820
      current_player = Player_GetCurrent ();
821
      opposite_player = Player_GetOpposite ();
822
 
823
      // if right button was clicked, compute new pitch and yaw
824
      if (rbutton_pushed)
825
      {
24 pmbaty 826
         // cycle through both players and change their view angles EXCEPT the opponent if he's human
827
         for (viewer_index = 0; viewer_index < 2; viewer_index++)
828
            if ((the_board.players[viewer_index].type == PLAYER_COMPUTER)
829
                || (the_board.players[viewer_index].type == PLAYER_INTERNET)
830
                || (viewer_index == current_viewer))
831
            {
832
               the_board.players[viewer_index].view_pitch += (gui_y - prevgui_y) * 0.3f;
833
               if (the_board.players[viewer_index].view_pitch < 10.0f)
834
                  the_board.players[viewer_index].view_pitch = 10.0f; // wrap angles around so that they
25 pmbaty 835
               if (the_board.players[viewer_index].view_pitch > 89.99f)
836
                  the_board.players[viewer_index].view_pitch = 89.99f; // stay in the [10, 89.99] bounds
1 pmbaty 837
 
24 pmbaty 838
               the_board.players[viewer_index].view_yaw += (gui_x - prevgui_x) * 0.3f;
839
               the_board.players[viewer_index].view_yaw = WrapAngle (the_board.players[viewer_index].view_yaw);
1 pmbaty 840
 
24 pmbaty 841
               // save these as the new custom angles
842
               the_board.players[viewer_index].custom_pitch = the_board.players[viewer_index].view_pitch;
843
               the_board.players[viewer_index].custom_yaw = the_board.players[viewer_index].view_yaw;
1 pmbaty 844
 
24 pmbaty 845
               // when moving the table around, jump to ideal angles immediately
846
               current_pitch = the_board.players[viewer_index].view_pitch;
847
               current_yaw = the_board.players[viewer_index].view_yaw;
848
            }
1 pmbaty 849
 
850
         the_scene.update = true; // button was clicked, update the 3D scene
851
      }
852
 
853
      // else it's just the mouse wandering around ; have we the right to select something ?
854
      else if ((the_board.viewed_move == the_board.move_count - 1) && (current_player->type == PLAYER_HUMAN) && (highlight_endtime < current_time))
855
      {
856
         // save the old positions
857
         prev_hovered_position[0] = the_board.hovered_position[0];
858
         prev_hovered_position[1] = the_board.hovered_position[1];
859
 
860
         // figure out the coordinates on table
861
         Render_MouseToFloor (gui_x, gui_y, &board_x, &board_y);
862
 
863
         // translate them to board coordinates
864
         the_board.hovered_position[0] = (int) floor ((20.0f - board_y) / 5.0f);
865
         the_board.hovered_position[1] = 7 - (int) floor ((20.0f - board_x) / 5.0f);
866
 
867
         // do they differ from last time ?
868
         if ((the_board.hovered_position[0] != prev_hovered_position[0]) || (the_board.hovered_position[1] != prev_hovered_position[1]))
869
            the_scene.update = true; // if so, update scene
870
      }
871
 
872
      // has the user the right to leave a command AND is there no comment yet ?
873
      if ((the_board.game_state >= STATE_PLAYING) && (the_board.viewed_move > 0)
874
          && ((the_board.moves[the_board.viewed_move].comment == NULL) || (the_board.moves[the_board.viewed_move].comment[0] == 0)))
875
      {
876
         // is the mouse above the comments zone ? if so, display a dimmed hint text
877
         if (Render_IsMouseInBox (gui_x, gui_y, 30.0f, 0.0f, 40.0f, 10.0f))
878
         {
879
            if (!the_scene.gui.comment_text.is_displayed)
880
            {
881
               Scene_SetText (&the_scene.gui.comment_text, 50.0f, 5.0f, 40.0f, ALIGN_CENTER, ALIGN_CENTER, ALIGN_LEFT, chat_fontindex, RGBA_TO_RGBACOLOR (255, 255, 255, 127), 999999.0f, false, LOCALIZE (L"DoubleClickToEnterComment"));
882
               the_scene.update = true; // and update the scene
883
            }
884
         }
885
         else
886
         {
887
            if (the_scene.gui.comment_text.is_displayed)
888
            {
889
               the_scene.gui.comment_text.is_displayed = false; // if not, erase the hint text
890
               the_scene.update = true; // and update the scene
891
            }
892
         }
893
      }
894
 
895
      // remember these coordinates for next time
896
      prevgui_x = gui_x;
897
      prevgui_y = gui_y;
898
 
899
      // call the default window message processing function to keep things going
900
      return (DefWindowProc (hWnd, message, wParam, lParam));
901
   }
902
 
903
   ////////////////////////////////////////////////////////////////////////////////////////////////
21 pmbaty 904
   // mouse scroll
905
   else if (message == WM_MOUSEWHEEL)
1 pmbaty 906
   {
21 pmbaty 907
      // are we in animation OR are mouse commands NOT allowed ?
908
      if ((animation_endtime + 1.0f >= current_time) || (command_ignoretime >= current_time))
909
         return (DefWindowProc (hWnd, message, wParam, lParam)); // if so, call the default message proc to keep things going
910
 
1 pmbaty 911
      // see if we're online
912
      remote_player = Player_FindByType (PLAYER_INTERNET);
913
 
914
      // are we online AND do we NOT have the right to select anything ?
915
      if ((remote_player != NULL) && !remote_player->is_in_game)
916
         return (DefWindowProc (hWnd, message, wParam, lParam)); // if so, call the default window message processing function to keep things going
917
 
918
      // scroll up / scroll down ?
919
      if (GET_WHEEL_DELTA_WPARAM (wParam) > 0)
920
         the_board.players[current_viewer].view_distance = max (48.0f, the_board.players[current_viewer].view_distance - 2.0f);
921
      else if (GET_WHEEL_DELTA_WPARAM (wParam) < 0)
922
         the_board.players[current_viewer].view_distance = min (100.0f, the_board.players[current_viewer].view_distance + 2.0f);
923
 
924
      // save this as the new custom distance
925
      the_board.players[current_viewer].custom_distance = the_board.players[current_viewer].view_distance;
926
 
927
      // when moving the table around, jump to ideal angles immediately
928
      current_distance = the_board.players[current_viewer].view_distance;
929
 
930
      the_scene.update = true; // update the 3D scene
931
 
932
      // call the default window message processing function to keep things going
933
      return (DefWindowProc (hWnd, message, wParam, lParam));
934
   }
935
 
936
   ////////////////////////////////////////////////////////////////////////////////////////////////
937
   // keyboard release
938
   else if (message == WM_CHAR)
939
   {
940
      // are we in position setup mode ?
941
      if (the_board.game_state == STATE_SETUPPOSITION)
942
      {
943
         // is it the enter key ? if so, exit setup mode and start playing
944
         if (wParam == L'\r')
945
         {
946
            current_move = &the_board.moves[the_board.viewed_move]; // quick access to current move
947
 
948
            // (in)validate the castling positions
949
            if ((current_move->slots[0][0].color == COLOR_WHITE) && (current_move->slots[0][0].part == PART_ROOK)
950
                && (current_move->slots[0][4].color == COLOR_WHITE) && (current_move->slots[0][4].part == PART_KING))
951
               current_move->sides[COLOR_WHITE].longcastle_allowed = true; // white castling queenside allowed
952
            else
953
               current_move->sides[COLOR_WHITE].longcastle_allowed = false; // white castling queenside no longer possible
954
            if ((current_move->slots[0][7].color == COLOR_WHITE) && (current_move->slots[0][7].part == PART_ROOK)
955
                && (current_move->slots[0][4].color == COLOR_WHITE) && (current_move->slots[0][4].part == PART_KING))
956
               current_move->sides[COLOR_WHITE].shortcastle_allowed = true; // white castling kingside allowed
957
            else
958
               current_move->sides[COLOR_WHITE].shortcastle_allowed = false; // white castling kingside no longer possible
959
            if ((current_move->slots[7][0].color == COLOR_BLACK) && (current_move->slots[7][0].part == PART_ROOK)
960
                && (current_move->slots[7][4].color == COLOR_BLACK) && (current_move->slots[7][4].part == PART_KING))
961
               current_move->sides[COLOR_BLACK].longcastle_allowed = true; // white castling queenside allowed
962
            else
963
               current_move->sides[COLOR_BLACK].longcastle_allowed = false; // white castling queenside no longer possible
964
            if ((current_move->slots[7][7].color == COLOR_BLACK) && (current_move->slots[7][7].part == PART_ROOK)
965
                && (current_move->slots[7][4].color == COLOR_BLACK) && (current_move->slots[7][4].part == PART_KING))
966
               current_move->sides[COLOR_BLACK].shortcastle_allowed = true; // white castling kingside allowed
967
            else
968
               current_move->sides[COLOR_BLACK].shortcastle_allowed = false; // white castling kingside no longer possible
969
 
970
            current_move->color = COLOR_BLACK; // so that game starts with white
971
 
972
            // validate this board in Forsyth-Edwards Notation and reset it
973
            Move_DescribeInFEN (current_move);
974
            wcscpy_s (fen_string, WCHAR_SIZEOF (fen_string), current_move->fen_string); // have a copy of fen string
975
            Board_Reset (&the_board, fen_string);
976
 
977
            the_board.game_state = STATE_PLAYING; // start the game now
978
            the_board.reevaluate = true; // evaluate board again
979
 
980
            the_scene.gui.central_text.disappear_time = current_time + 1.0f; // fade out help text now (FIXME: ugly)
981
            the_scene.update = true; // update the 3D scene
982
 
983
            // call the default window message processing function to keep things going
984
            return (DefWindowProc (hWnd, message, wParam, lParam));
985
         }
986
      }
987
 
988
      // else are we in internet mode ?
989
      else if (Player_FindByType (PLAYER_INTERNET) != NULL)
990
      {
991
         entered_ccreply = &the_scene.gui.entered_ccreply; // quick access to entered ccreply
992
         local_player = Player_FindByType (PLAYER_HUMAN); // quick access to local player
993
         if (local_player == NULL)
994
            return (DefWindowProc (hWnd, message, wParam, lParam)); // theoretically impossible condition, but better be sure
995
         if (selected_chatterchannel == NULL)
996
            return (DefWindowProc (hWnd, message, wParam, lParam)); // theoretically impossible condition, but better be sure
997
 
998
         // are we NOT entering text yet ?
999
         if (!the_scene.gui.is_entering_text)
1000
         {
1001
            // is it the space bar ?
1002
            if (wParam == L' ')
1003
            {
1004
               the_scene.gui.is_entering_text = true; // remember we are entering text
1005
 
1006
               // reset the entered text buffer
1007
               entered_ccreply->text = (wchar_t *) SAFE_malloc (1, sizeof (wchar_t), false);
1008
               entered_ccreply->text[0] = 0; // only the null terminator will fit
1009
               entered_ccreply->text_length = 0; // and set its length to zero
1010
            }
1011
         }
1012
 
1013
         // else we are currently in text entering mode
1014
         else
1015
         {
1016
            // is the typed character a printable character ?
1017
            if (iswprint (wParam))
1018
            {
1019
               // if so, reallocate space in the typed buffer (include null terminator)
1020
               entered_ccreply->text = (wchar_t *) SAFE_realloc (entered_ccreply->text, entered_ccreply->text_length + 1, entered_ccreply->text_length + 1 + 1, sizeof (wchar_t), false);
1021
               swprintf_s (&entered_ccreply->text[entered_ccreply->text_length], 1 + 1, L"%c", wParam); // append character
1022
               entered_ccreply->text_length++; // buffer holds now one character more
1023
            }
1024
 
1025
            // else is the typed character a backspace AND is there text to erase ?
1026
            else if ((wParam == 0x08) && (entered_ccreply->text_length > 0))
1027
            {
1028
               // if so, reallocate space in the typed buffer (include null terminator)
1029
               entered_ccreply->text = (wchar_t *) SAFE_realloc (entered_ccreply->text, entered_ccreply->text_length + 1, entered_ccreply->text_length + 1 - 1, sizeof (wchar_t), false);
1030
               entered_ccreply->text[entered_ccreply->text_length - 1] = 0; // backspace, delete one character
1031
               entered_ccreply->text_length--; // buffer holds now one character less
1032
            }
1033
 
1034
            // else is the typed character the escape key ?
1035
            else if (wParam == 0x1b)
1036
            {
1037
               SAFE_free ((void **) &entered_ccreply->text); // reset the entered text buffer
1038
               entered_ccreply->text_length = 0; // and set its length to zero
1039
               the_scene.gui.is_entering_text = false; // and exit from the text entering mode
1040
            }
1041
 
1042
            // else is the typed character the enter key ?
1043
            else if (wParam == 0x0d)
1044
               the_scene.gui.is_entering_text = false; // enter, exit from the text entering mode (this will validate our reply)
1045
 
1046
            // else it's an illegal character
1047
            else
1048
               Audio_PlaySound (SOUNDTYPE_ILLEGALMOVE); // illegal character, beep the illegal move sound
1049
         }
1050
 
1051
         the_scene.update = true; // update the 3D scene
1052
 
1053
         // call the default window message processing function to keep things going
1054
         return (DefWindowProc (hWnd, message, wParam, lParam));
1055
      }
1056
   }
1057
 
1058
   ////////////////////////////////////////////////////////////////////////////////////////////////
1059
   // mouse move outside the window
1060
   else if (message == WM_NCMOUSEMOVE)
1061
   {
1062
      rbutton_pushed = false; // remember right button is released
1063
 
1064
      // call the default window message processing function to keep things going
1065
      return (DefWindowProc (hWnd, message, wParam, lParam));
1066
   }
1067
 
1068
   ////////////////////////////////////////////////////////////////////////////////////////////////
1069
   // window repaint
1070
   else if (message == WM_PAINT)
1071
   {
1072
      the_scene.update = true; // update the 3D scene
1073
 
1074
      // call the default window message processing function to keep things going
1075
      return (DefWindowProc (hWnd, message, wParam, lParam));
1076
   }
1077
 
1078
   // call the default window message processing function to keep things going
1079
   return (DefWindowProc (hWnd, message, wParam, lParam));
1080
}
1081
 
1082
 
1083
static bool Button_IsHovered (guibutton_t *button, int gui_x, int gui_y)
1084
{
1085
   // handy wrapper that returns whether a particular GUI button is hovered when the mouse is at the given coordinates
1086
 
1087
   return (Render_IsMouseInBox (gui_x, gui_y, button->left, button->top, button->width, button->height));
1088
}
1089
 
1090
 
1091
static bool Button_UpdateHoverState (guibutton_t *button, int gui_x, int gui_y)
1092
{
1093
   // this function updates the hover state of a GUI button, and returns TRUE if the
1094
   // scene needs to be updated, FALSE otherwise.
1095
 
1096
   // is the button displayed ?
1097
   if (button->state != 0)
1098
   {
1099
      // is the mouse hovering it ?
1100
      if (Render_IsMouseInBox (gui_x, gui_y, button->left, button->top, button->width, button->height))
1101
      {
1102
         // was it NOT hovered before ?
1103
         if (button->state != 2)
1104
         {
1105
            button->state = 2; // mark it as hovered
1106
            return (true); // return TRUE so as to update the scene
1107
         }
1108
      }
1109
 
1110
      // else was it hovered before ?
1111
      else if (button->state == 2)
1112
      {
1113
         button->state = 1; // else mark it as not hovered
1114
         return (true); // return TRUE so as to update the scene
1115
      }
1116
   }
1117
 
1118
   return (false); // no need to update the scene
1119
}