Subversion Repositories Games.Chess Giants

Rev

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