rnd-20060420-1-src
[rocksndiamonds.git] / src / screens.c
1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back!               *
3 *----------------------------------------------------------*
4 * (c) 1995-2002 Artsoft Entertainment                      *
5 *               Holger Schemel                             *
6 *               Detmolder Strasse 189                      *
7 *               33604 Bielefeld                            *
8 *               Germany                                    *
9 *               e-mail: info@artsoft.org                   *
10 *----------------------------------------------------------*
11 * screens.c                                                *
12 ***********************************************************/
13
14 #include "libgame/libgame.h"
15
16 #include "screens.h"
17 #include "events.h"
18 #include "game.h"
19 #include "tools.h"
20 #include "editor.h"
21 #include "files.h"
22 #include "tape.h"
23 #include "cartoons.h"
24 #include "network.h"
25 #include "init.h"
26
27 /* screens in the setup menu */
28 #define SETUP_MODE_MAIN                 0
29 #define SETUP_MODE_GAME                 1
30 #define SETUP_MODE_EDITOR               2
31 #define SETUP_MODE_INPUT                3
32 #define SETUP_MODE_SHORTCUT_1           4
33 #define SETUP_MODE_SHORTCUT_2           5
34 #define SETUP_MODE_GRAPHICS             6
35 #define SETUP_MODE_SOUND                7
36 #define SETUP_MODE_ARTWORK              8
37 #define SETUP_MODE_CHOOSE_GRAPHICS      9
38 #define SETUP_MODE_CHOOSE_SOUNDS        10
39 #define SETUP_MODE_CHOOSE_MUSIC         11
40
41 #define MAX_SETUP_MODES                 12
42
43 /* for input setup functions */
44 #define SETUPINPUT_SCREEN_POS_START     0
45 #define SETUPINPUT_SCREEN_POS_END       (SCR_FIELDY - 4)
46 #define SETUPINPUT_SCREEN_POS_EMPTY1    (SETUPINPUT_SCREEN_POS_START + 3)
47 #define SETUPINPUT_SCREEN_POS_EMPTY2    (SETUPINPUT_SCREEN_POS_END - 1)
48
49 /* screens on the info screen */
50 #define INFO_MODE_MAIN                  0
51 #define INFO_MODE_ELEMENTS              1
52 #define INFO_MODE_MUSIC                 2
53 #define INFO_MODE_CREDITS               3
54 #define INFO_MODE_PROGRAM               4
55 #define INFO_MODE_LEVELSET              5
56
57 #define MAX_INFO_MODES                  6
58
59 /* for various menu stuff  */
60 #define MENU_SCREEN_START_XPOS          1
61 #define MENU_SCREEN_START_YPOS          2
62 #define MENU_SCREEN_VALUE_XPOS          14
63 #define MENU_SCREEN_MAX_XPOS            (SCR_FIELDX - 1)
64 #define MENU_TITLE1_YPOS                8
65 #define MENU_TITLE2_YPOS                46
66 #define MAX_INFO_ELEMENTS_ON_SCREEN     10
67 #define MAX_MENU_ENTRIES_ON_SCREEN      (SCR_FIELDY - MENU_SCREEN_START_YPOS)
68 #define MAX_MENU_TEXT_LENGTH_BIG        (MENU_SCREEN_VALUE_XPOS -       \
69                                          MENU_SCREEN_START_XPOS)
70 #define MAX_MENU_TEXT_LENGTH_MEDIUM     (MAX_MENU_TEXT_LENGTH_BIG * 2)
71
72 /* buttons and scrollbars identifiers */
73 #define SCREEN_CTRL_ID_SCROLL_UP        0
74 #define SCREEN_CTRL_ID_SCROLL_DOWN      1
75 #define SCREEN_CTRL_ID_SCROLL_VERTICAL  2
76
77 #define NUM_SCREEN_SCROLLBUTTONS        2
78 #define NUM_SCREEN_SCROLLBARS           1
79 #define NUM_SCREEN_GADGETS              3
80
81 /* graphic position and size values for buttons and scrollbars */
82 #define SC_SCROLLBUTTON_XSIZE           TILEX
83 #define SC_SCROLLBUTTON_YSIZE           TILEY
84
85 #define SC_SCROLLBAR_XPOS               (SXSIZE - SC_SCROLLBUTTON_XSIZE)
86
87 #define SC_SCROLL_VERTICAL_XSIZE        SC_SCROLLBUTTON_XSIZE
88 #define SC_SCROLL_VERTICAL_YSIZE        ((MAX_MENU_ENTRIES_ON_SCREEN - 2) * \
89                                          SC_SCROLLBUTTON_YSIZE)
90
91 #define SC_SCROLL_UP_XPOS               SC_SCROLLBAR_XPOS
92 #define SC_SCROLL_UP_YPOS               (2 * SC_SCROLLBUTTON_YSIZE)
93
94 #define SC_SCROLL_VERTICAL_XPOS         SC_SCROLLBAR_XPOS
95 #define SC_SCROLL_VERTICAL_YPOS         (SC_SCROLL_UP_YPOS + \
96                                          SC_SCROLLBUTTON_YSIZE)
97
98 #define SC_SCROLL_DOWN_XPOS             SC_SCROLLBAR_XPOS
99 #define SC_SCROLL_DOWN_YPOS             (SC_SCROLL_VERTICAL_YPOS + \
100                                          SC_SCROLL_VERTICAL_YSIZE)
101
102 #define SC_BORDER_SIZE                  14
103
104
105 /* forward declarations of internal functions */
106 static void HandleScreenGadgets(struct GadgetInfo *);
107 static void HandleSetupScreen_Generic(int, int, int, int, int);
108 static void HandleSetupScreen_Input(int, int, int, int, int);
109 static void CustomizeKeyboard(int);
110 static void CalibrateJoystick(int);
111 static void execSetupArtwork(void);
112 static void HandleChooseTree(int, int, int, int, int, TreeInfo **);
113
114 static void DrawChooseLevel(void);
115 static void DrawInfoScreen(void);
116 static void DrawSetupScreen(void);
117
118 static void DrawInfoScreen_HelpAnim(int, int, boolean);
119 static void DrawInfoScreen_HelpText(int, int, int, int);
120 static void HandleInfoScreen_Main(int, int, int, int, int);
121 static void HandleInfoScreen_Elements(int);
122 static void HandleInfoScreen_Music(int);
123 static void HandleInfoScreen_Credits(int);
124 static void HandleInfoScreen_Program(int);
125
126 static void MapChooseTreeGadgets(TreeInfo *);
127
128 static struct GadgetInfo *screen_gadget[NUM_SCREEN_GADGETS];
129 static int setup_mode = SETUP_MODE_MAIN;
130 static int info_mode = INFO_MODE_MAIN;
131
132 #define DRAW_OFFSET_MODE(x)     (x >= GAME_MODE_MAIN &&                 \
133                                  x <= GAME_MODE_SETUP ? x :             \
134                                  x == GAME_MODE_PSEUDO_TYPENAME ?       \
135                                  GAME_MODE_MAIN : GAME_MODE_DEFAULT)
136
137 #define mSX (SX + menu.draw_xoffset[DRAW_OFFSET_MODE(game_status)])
138 #define mSY (SY + menu.draw_yoffset[DRAW_OFFSET_MODE(game_status)])
139
140 #define NUM_MENU_ENTRIES_ON_SCREEN (menu.list_size[game_status] > 2 ?   \
141                                     menu.list_size[game_status] :       \
142                                     MAX_MENU_ENTRIES_ON_SCREEN)
143
144 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
145 #define NUM_SCROLLBAR_BITMAPS           2
146 static Bitmap *scrollbar_bitmap[NUM_SCROLLBAR_BITMAPS];
147 #endif
148
149
150 static void drawCursorExt(int xpos, int ypos, int color, int g)
151 {
152   static int cursor_array[SCR_FIELDY];
153
154   if (xpos == 0)
155   {
156     if (g != 0)
157       cursor_array[ypos] = g;
158     else
159       g = cursor_array[ypos];
160   }
161
162   if (color == FC_RED)
163     g = (g == IMG_MENU_BUTTON_LEFT  ? IMG_MENU_BUTTON_LEFT_ACTIVE  :
164          g == IMG_MENU_BUTTON_RIGHT ? IMG_MENU_BUTTON_RIGHT_ACTIVE :
165          g == IMG_MENU_BUTTON_LEAVE_MENU ? IMG_MENU_BUTTON_LEAVE_MENU_ACTIVE :
166          g == IMG_MENU_BUTTON_ENTER_MENU ? IMG_MENU_BUTTON_ENTER_MENU_ACTIVE :
167          g == IMG_MENU_BUTTON_LAST_LEVEL ? IMG_MENU_BUTTON_LAST_LEVEL_ACTIVE :
168          g == IMG_MENU_BUTTON_NEXT_LEVEL ? IMG_MENU_BUTTON_NEXT_LEVEL_ACTIVE :
169          IMG_MENU_BUTTON_ACTIVE);
170
171   ypos += MENU_SCREEN_START_YPOS;
172
173   DrawBackground(mSX + xpos * TILEX, mSY + ypos * TILEY, TILEX, TILEY);
174   DrawGraphicThruMaskExt(drawto, mSX + xpos * TILEX, mSY + ypos * TILEY, g, 0);
175 }
176
177 static void initCursor(int ypos, int graphic)
178 {
179   drawCursorExt(0, ypos, FC_BLUE, graphic);
180 }
181
182 static void drawCursor(int ypos, int color)
183 {
184   drawCursorExt(0, ypos, color, 0);
185 }
186
187 static void drawCursorXY(int xpos, int ypos, int graphic)
188 {
189   drawCursorExt(xpos, ypos, -1, graphic);
190 }
191
192 static void drawChooseTreeCursor(int ypos, int color)
193 {
194   int last_game_status = game_status;   /* save current game status */
195
196   /* force LEVELS draw offset on artwork setup screen */
197   game_status = GAME_MODE_LEVELS;
198
199   drawCursorExt(0, ypos, color, 0);
200
201   game_status = last_game_status;       /* restore current game status */
202 }
203
204 static void PlayMenuSound()
205 {
206   int sound = menu.sound[game_status];
207
208   if (sound == SND_UNDEFINED)
209     return;
210
211   if (sound_info[sound].loop)
212     PlaySoundLoop(sound);
213   else
214     PlaySound(sound);
215 }
216
217 static void PlayMenuSoundIfLoop()
218 {
219   int sound = menu.sound[game_status];
220
221   if (sound == SND_UNDEFINED)
222     return;
223
224   if (sound_info[sound].loop)
225     PlaySoundLoop(sound);
226 }
227
228 static void PlayMenuMusic()
229 {
230   int music = menu.music[game_status];
231
232   if (music == MUS_UNDEFINED)
233     return;
234
235   PlayMusic(music);
236 }
237
238 void DrawHeadline()
239 {
240   DrawTextSCentered(MENU_TITLE1_YPOS, FONT_TITLE_1, PROGRAM_TITLE_STRING);
241   DrawTextSCentered(MENU_TITLE2_YPOS, FONT_TITLE_2, PROGRAM_COPYRIGHT_STRING);
242 }
243
244 static void ToggleFullscreenIfNeeded()
245 {
246   if (setup.fullscreen != video.fullscreen_enabled)
247   {
248     /* save old door content */
249     BlitBitmap(backbuffer, bitmap_db_door,
250                DX, DY, DXSIZE, DYSIZE, DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1);
251
252     /* toggle fullscreen */
253     ChangeVideoModeIfNeeded(setup.fullscreen);
254     setup.fullscreen = video.fullscreen_enabled;
255
256     /* redraw background to newly created backbuffer */
257     BlitBitmap(graphic_info[IMG_GLOBAL_BORDER].bitmap, backbuffer,
258                0,0, WIN_XSIZE,WIN_YSIZE, 0,0);
259
260     /* restore old door content */
261     BlitBitmap(bitmap_db_door, backbuffer,
262                DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1, DXSIZE, DYSIZE, DX, DY);
263
264     redraw_mask = REDRAW_ALL;
265   }
266 }
267
268 static int getLastLevelButtonPos()
269 {
270   return 10;
271 }
272
273 static int getCurrentLevelTextPos()
274 {
275   return (getLastLevelButtonPos() + 1);
276 }
277
278 static int getNextLevelButtonPos()
279 {
280   return getLastLevelButtonPos() + 3 + 1;
281 }
282
283 static int getLevelRangeTextPos()
284 {
285   return getNextLevelButtonPos() + 1;
286 }
287
288 void DrawMainMenu()
289 {
290   static LevelDirTree *leveldir_last_valid = NULL;
291   char *name_text = (!options.network && setup.team_mode ? "Team:" : "Name:");
292 #if 1
293   char *level_text = "Levelset";
294 #else
295   char *level_text = "Level:";
296 #endif
297   int name_width, level_width;
298   int i;
299
300   UnmapAllGadgets();
301   FadeSoundsAndMusic();
302
303   KeyboardAutoRepeatOn();
304   ActivateJoystick();
305
306   SetDrawDeactivationMask(REDRAW_NONE);
307   SetDrawBackgroundMask(REDRAW_FIELD);
308
309   audio.sound_deactivated = FALSE;
310
311   /* needed if last screen was the playing screen, invoked from level editor */
312   if (level_editor_test_game)
313   {
314     game_status = GAME_MODE_EDITOR;
315     DrawLevelEd();
316
317     return;
318   }
319
320   /* needed if last screen was the editor screen */
321   UndrawSpecialEditorDoor();
322
323   /* needed if last screen was the setup screen and fullscreen state changed */
324   ToggleFullscreenIfNeeded();
325
326   /* leveldir_current may be invalid (level group, parent link) */
327   if (!validLevelSeries(leveldir_current))
328     leveldir_current = getFirstValidTreeInfoEntry(leveldir_last_valid);
329
330   /* store valid level series information */
331   leveldir_last_valid = leveldir_current;
332
333   /* needed if last screen (level choice) changed graphics, sounds or music */
334   ReloadCustomArtwork(0);
335
336 #ifdef TARGET_SDL
337   SetDrawtoField(DRAW_BACKBUFFER);
338 #endif
339
340 #if 0
341   /* map gadgets for main menu screen */
342   MapTapeButtons();
343 #endif
344
345   /* level_nr may have been set to value over handicap with level editor */
346   if (setup.handicap && level_nr > leveldir_current->handicap_level)
347     level_nr = leveldir_current->handicap_level;
348
349   GetPlayerConfig();
350   LoadLevel(level_nr);
351
352   SetMainBackgroundImage(IMG_BACKGROUND_MAIN);
353   ClearWindow();
354
355   DrawHeadline();
356
357   DrawText(mSX + 32, mSY + 2 * 32, name_text,       FONT_MENU_1);
358   DrawText(mSX + 32, mSY + 3 * 32, level_text,      FONT_MENU_1);
359   DrawText(mSX + 32, mSY + 4 * 32, "Hall Of Fame",  FONT_MENU_1);
360   DrawText(mSX + 32, mSY + 5 * 32, "Level Creator", FONT_MENU_1);
361   DrawText(mSX + 32, mSY + 6 * 32, "Info Screen",   FONT_MENU_1);
362   DrawText(mSX + 32, mSY + 7 * 32, "Start Game",    FONT_MENU_1);
363   DrawText(mSX + 32, mSY + 8 * 32, "Setup",         FONT_MENU_1);
364   DrawText(mSX + 32, mSY + 9 * 32, "Quit",          FONT_MENU_1);
365
366   /* calculated after (possible) reload of custom artwork */
367   name_width  = getTextWidth(name_text,  FONT_MENU_1);
368 #if 1
369   level_width = 9 * 32;
370 #else
371 #if 1
372   level_width = 9 * getFontWidth(FONT_MENU_1);
373 #else
374   level_width = getTextWidth(level_text, FONT_MENU_1);
375 #endif
376 #endif
377
378   DrawText(mSX + 32 + name_width, mSY + 2 * 32, setup.player_name,
379            FONT_INPUT_1);
380
381 #if 1
382   DrawText(mSX + getCurrentLevelTextPos() * 32, mSY + 3 * 32,
383            int2str(level_nr, 3), FONT_VALUE_1);
384 #else
385 #if 1
386   DrawText(mSX + level_width + 2 * 32, mSY + 3 * 32, int2str(level_nr, 3),
387            FONT_VALUE_1);
388 #else
389   DrawText(mSX + level_width + 5 * 32, mSY + 3 * 32, int2str(level_nr, 3),
390            FONT_VALUE_1);
391 #endif
392 #endif
393
394   DrawMicroLevel(MICROLEVEL_XPOS, MICROLEVEL_YPOS, TRUE);
395
396 #if 1
397
398 #if 1
399   {
400     int text_height = getFontHeight(FONT_TEXT_3);
401 #if 1
402     int xpos = getLevelRangeTextPos() * 32;
403 #else
404     int xpos = level_width + 6 * 32;
405 #endif
406     int ypos2 = -SY + 3 * 32 + 16;
407     int ypos1 = ypos2 - text_height;
408
409     DrawTextF(mSX + xpos, mSY + ypos1, FONT_TEXT_3,
410               "%03d", leveldir_current->first_level);
411     DrawTextF(mSX + xpos, mSY + ypos2, FONT_TEXT_3,
412               "%03d", leveldir_current->last_level);
413   }
414 #else
415   DrawTextF(mSX + level_width + 6 * 32, mSY + 3 * 32 + 1, FONT_TEXT_3,
416             "%d", leveldir_current->levels);
417 #endif
418
419 #else
420   DrawTextF(mSX + 32 + level_width - 2, mSY + 3 * 32 + 1, FONT_TEXT_3, "%d-%d",
421             leveldir_current->first_level, leveldir_current->last_level);
422 #endif
423
424 #if 0
425   if (leveldir_current->readonly)
426   {
427     DrawTextS(mSX + level_width + 9 * 32 - 2,
428               mSY + 3 * 32 + 1 - 7, FONT_TEXT_3, "READ");
429     DrawTextS(mSX + level_width + 9 * 32 - 2,
430               mSY + 3 * 32 + 1 + 7, FONT_TEXT_3, "ONLY");
431   }
432 #endif
433
434   for (i = 0; i < 8; i++)
435     initCursor(i, (i == 1 || i == 4 || i == 6 ? IMG_MENU_BUTTON_ENTER_MENU :
436                    IMG_MENU_BUTTON));
437
438 #if 1
439   drawCursorXY(getLastLevelButtonPos(), 1, IMG_MENU_BUTTON_LAST_LEVEL);
440   drawCursorXY(getNextLevelButtonPos(), 1, IMG_MENU_BUTTON_NEXT_LEVEL);
441 #else
442 #if 1
443   drawCursorXY(level_width / 32 + 1, 1, IMG_MENU_BUTTON_LEFT);
444   drawCursorXY(level_width / 32 + 5, 1, IMG_MENU_BUTTON_RIGHT);
445 #else
446   drawCursorXY(level_width / 32 + 4, 1, IMG_MENU_BUTTON_LEFT);
447   drawCursorXY(level_width / 32 + 8, 1, IMG_MENU_BUTTON_RIGHT);
448 #endif
449 #endif
450
451   DrawTextSCentered(326, FONT_TITLE_2, "A Game by Artsoft Entertainment");
452
453   FadeToFront();
454   InitAnimation();
455
456   HandleMainMenu(0, 0, 0, 0, MB_MENU_INITIALIZE);
457
458   TapeStop();
459   if (TAPE_IS_EMPTY(tape))
460     LoadTape(level_nr);
461   DrawCompleteVideoDisplay();
462
463   PlayMenuSound();
464   PlayMenuMusic();
465
466   OpenDoor(DOOR_CLOSE_1 | DOOR_OPEN_2);
467
468 #if 1
469   /* map gadgets for main menu screen */
470   MapTapeButtons();
471 #endif
472 }
473
474 #if 0
475 static void gotoTopLevelDir()
476 {
477   /* move upwards to top level directory */
478   while (leveldir_current->node_parent)
479   {
480     /* write a "path" into level tree for easy navigation to last level */
481     if (leveldir_current->node_parent->node_group->cl_first == -1)
482     {
483       int num_leveldirs = numTreeInfoInGroup(leveldir_current);
484       int leveldir_pos = posTreeInfo(leveldir_current);
485       int num_page_entries;
486       int cl_first, cl_cursor;
487
488       if (num_leveldirs <= NUM_MENU_ENTRIES_ON_SCREEN)
489         num_page_entries = num_leveldirs;
490       else
491         num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
492
493       cl_first = MAX(0, leveldir_pos - num_page_entries + 1);
494       cl_cursor = leveldir_pos - cl_first;
495
496       leveldir_current->node_parent->node_group->cl_first = cl_first;
497       leveldir_current->node_parent->node_group->cl_cursor = cl_cursor;
498     }
499
500     leveldir_current = leveldir_current->node_parent;
501   }
502 }
503 #endif
504
505 void HandleMainMenu(int mx, int my, int dx, int dy, int button)
506 {
507   static unsigned long level_delay = 0;
508   static int choice = 5;
509   int x = 0;
510   int y = choice;
511
512   if (button == MB_MENU_INITIALIZE)
513   {
514     drawCursor(choice, FC_RED);
515     return;
516   }
517
518   if (mx || my)         /* mouse input */
519   {
520     x = (mx - mSX) / 32;
521     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
522   }
523   else if (dx || dy)    /* keyboard input */
524   {
525     if (dx && choice == 1)
526       x = (dx < 0 ? 10 : 14);
527     else if (dx > 0)
528     {
529       if (choice == 4 || choice == 6)
530         button = MB_MENU_CHOICE;
531     }
532     else if (dy)
533       y = choice + dy;
534   }
535
536   if (y == 1 && ((x == 10 && level_nr > leveldir_current->first_level) ||
537                  (x == 14 && level_nr < leveldir_current->last_level)) &&
538       button && DelayReached(&level_delay, GADGET_FRAME_DELAY))
539   {
540     int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
541     int old_level_nr = level_nr;
542     int new_level_nr;
543
544     new_level_nr = level_nr + (x == 10 ? -step : +step);
545     if (new_level_nr < leveldir_current->first_level)
546       new_level_nr = leveldir_current->first_level;
547     if (new_level_nr > leveldir_current->last_level)
548       new_level_nr = leveldir_current->last_level;
549
550     if (setup.handicap && new_level_nr > leveldir_current->handicap_level)
551     {
552       /* skipping levels is only allowed when trying to skip single level */
553       if (setup.skip_levels && step == 1 &&
554           Request("Level still unsolved ! Skip despite handicap ?", REQ_ASK))
555       {
556         leveldir_current->handicap_level++;
557         SaveLevelSetup_SeriesInfo();
558       }
559
560       new_level_nr = leveldir_current->handicap_level;
561     }
562
563     if (new_level_nr != old_level_nr)
564     {
565       level_nr = new_level_nr;
566
567       DrawText(mSX + 11 * 32, mSY + 3 * 32, int2str(level_nr, 3),
568                FONT_VALUE_1);
569
570       LoadLevel(level_nr);
571       DrawMicroLevel(MICROLEVEL_XPOS, MICROLEVEL_YPOS, TRUE);
572
573       TapeErase();
574       LoadTape(level_nr);
575       DrawCompleteVideoDisplay();
576
577       /* needed because DrawMicroLevel() takes some time */
578       BackToFront();
579       SyncDisplay();
580       DelayReached(&level_delay, 0);    /* reset delay counter */
581     }
582   }
583   else if (IN_VIS_FIELD(x, y) &&
584            y >= 0 && y <= 7 && (y != 1 || x < 10))
585   {
586     if (button)
587     {
588       if (y != choice)
589       {
590         drawCursor(y, FC_RED);
591         drawCursor(choice, FC_BLUE);
592         choice = y;
593       }
594     }
595     else
596     {
597       if (y == 0)
598       {
599         game_status = GAME_MODE_PSEUDO_TYPENAME;
600         HandleTypeName(strlen(setup.player_name), 0);
601       }
602       else if (y == 1)
603       {
604         if (leveldir_first)
605         {
606           game_status = GAME_MODE_LEVELS;
607           SaveLevelSetup_LastSeries();
608           SaveLevelSetup_SeriesInfo();
609
610 #if 0
611           gotoTopLevelDir();
612 #endif
613
614           DrawChooseLevel();
615         }
616       }
617       else if (y == 2)
618       {
619         game_status = GAME_MODE_SCORES;
620         DrawHallOfFame(-1);
621       }
622       else if (y == 3)
623       {
624         if (leveldir_current->readonly &&
625             !strEqual(setup.player_name, "Artsoft"))
626           Request("This level is read only !", REQ_CONFIRM);
627         game_status = GAME_MODE_EDITOR;
628         DrawLevelEd();
629       }
630       else if (y == 4)
631       {
632         game_status = GAME_MODE_INFO;
633         info_mode = INFO_MODE_MAIN;
634         DrawInfoScreen();
635       }
636       else if (y == 5)
637       {
638         StartGameActions(options.network, setup.autorecord, NEW_RANDOMIZE);
639       }
640       else if (y == 6)
641       {
642         game_status = GAME_MODE_SETUP;
643         setup_mode = SETUP_MODE_MAIN;
644
645         DrawSetupScreen();
646       }
647       else if (y == 7)
648       {
649         SaveLevelSetup_LastSeries();
650         SaveLevelSetup_SeriesInfo();
651
652         if (Request("Do you really want to quit ?", REQ_ASK | REQ_STAY_CLOSED))
653           game_status = GAME_MODE_QUIT;
654       }
655     }
656   }
657
658   if (game_status == GAME_MODE_MAIN)
659   {
660     DrawMicroLevel(MICROLEVEL_XPOS, MICROLEVEL_YPOS, FALSE);
661     DoAnimation();
662   }
663 }
664
665
666 /* ========================================================================= */
667 /* info screen functions                                                     */
668 /* ========================================================================= */
669
670 static struct TokenInfo *info_info;
671 static int num_info_info;
672
673 static void execInfoElements()
674 {
675   info_mode = INFO_MODE_ELEMENTS;
676   DrawInfoScreen();
677 }
678
679 static void execInfoMusic()
680 {
681   info_mode = INFO_MODE_MUSIC;
682   DrawInfoScreen();
683 }
684
685 static void execInfoCredits()
686 {
687   info_mode = INFO_MODE_CREDITS;
688   DrawInfoScreen();
689 }
690
691 static void execInfoProgram()
692 {
693   info_mode = INFO_MODE_PROGRAM;
694   DrawInfoScreen();
695 }
696
697 static void execInfoLevelSet()
698 {
699   info_mode = INFO_MODE_LEVELSET;
700   DrawInfoScreen();
701 }
702
703 static void execExitInfo()
704 {
705   game_status = GAME_MODE_MAIN;
706   DrawMainMenu();
707 }
708
709 static struct TokenInfo info_info_main[] =
710 {
711   { TYPE_ENTER_SCREEN,  execInfoElements,       "Elements Info"         },
712   { TYPE_ENTER_SCREEN,  execInfoMusic,          "Music Info"            },
713   { TYPE_ENTER_SCREEN,  execInfoCredits,        "Credits"               },
714   { TYPE_ENTER_SCREEN,  execInfoProgram,        "Program Info"          },
715   { TYPE_ENTER_SCREEN,  execInfoLevelSet,       "Level Set Info"        },
716   { TYPE_EMPTY,         NULL,                   ""                      },
717   { TYPE_LEAVE_MENU,    execExitInfo,           "Exit"                  },
718
719   { 0,                  NULL,                   NULL                    }
720 };
721
722 static void DrawInfoScreen_Main()
723 {
724   int i;
725
726   UnmapAllGadgets();
727   CloseDoor(DOOR_CLOSE_2);
728
729   ClearWindow();
730
731 #if 1
732   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Info Screen");
733 #else
734   DrawText(mSX + 16, mSY + 16, "Info Screen", FONT_TITLE_1);
735 #endif
736
737   info_info = info_info_main;
738   num_info_info = 0;
739
740   for (i = 0; info_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
741   {
742     int xpos = MENU_SCREEN_START_XPOS;
743     int ypos = MENU_SCREEN_START_YPOS + i;
744     int font_nr = FONT_MENU_1;
745
746     DrawText(mSX + xpos * 32, mSY + ypos * 32, info_info[i].text, font_nr);
747
748     if (info_info[i].type & TYPE_ENTER_MENU)
749       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
750     else if (info_info[i].type & TYPE_LEAVE_MENU)
751       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
752     else if (info_info[i].type & ~TYPE_SKIP_ENTRY)
753       initCursor(i, IMG_MENU_BUTTON);
754
755     num_info_info++;
756   }
757
758   FadeToFront();
759   InitAnimation();
760
761   PlayMenuSound();
762   PlayMenuMusic();
763
764   HandleInfoScreen_Main(0, 0, 0, 0, MB_MENU_INITIALIZE);
765 }
766
767 void HandleInfoScreen_Main(int mx, int my, int dx, int dy, int button)
768 {
769   static int choice_store[MAX_INFO_MODES];
770   int choice = choice_store[info_mode];         /* always starts with 0 */
771   int x = 0;
772   int y = choice;
773
774   if (button == MB_MENU_INITIALIZE)
775   {
776     /* advance to first valid menu entry */
777     while (choice < num_info_info &&
778            info_info[choice].type & TYPE_SKIP_ENTRY)
779       choice++;
780     choice_store[info_mode] = choice;
781
782     drawCursor(choice, FC_RED);
783     return;
784   }
785   else if (button == MB_MENU_LEAVE)
786   {
787     for (y = 0; y < num_info_info; y++)
788     {
789       if (info_info[y].type & TYPE_LEAVE_MENU)
790       {
791         void (*menu_callback_function)(void) = info_info[y].value;
792
793         menu_callback_function();
794         break;  /* absolutely needed because function changes 'info_info'! */
795       }
796     }
797
798     return;
799   }
800
801   if (mx || my)         /* mouse input */
802   {
803     x = (mx - mSX) / 32;
804     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
805   }
806   else if (dx || dy)    /* keyboard input */
807   {
808     if (dx)
809     {
810       int menu_navigation_type = (dx < 0 ? TYPE_LEAVE_MENU : TYPE_ENTER_MENU);
811
812       if (info_info[choice].type & menu_navigation_type ||
813           info_info[choice].type & TYPE_ENTER_SCREEN ||
814           info_info[choice].type & TYPE_BOOLEAN_STYLE)
815         button = MB_MENU_CHOICE;
816     }
817     else if (dy)
818       y = choice + dy;
819
820     /* jump to next non-empty menu entry (up or down) */
821     while (y > 0 && y < num_info_info - 1 &&
822            info_info[y].type & TYPE_SKIP_ENTRY)
823       y += dy;
824   }
825
826   if (IN_VIS_FIELD(x, y) &&
827       y >= 0 && y < num_info_info && info_info[y].type & ~TYPE_SKIP_ENTRY)
828   {
829     if (button)
830     {
831       if (y != choice)
832       {
833         drawCursor(y, FC_RED);
834         drawCursor(choice, FC_BLUE);
835         choice = choice_store[info_mode] = y;
836       }
837     }
838     else if (!(info_info[y].type & TYPE_GHOSTED))
839     {
840       if (info_info[y].type & TYPE_ENTER_OR_LEAVE_MENU)
841       {
842         void (*menu_callback_function)(void) = info_info[choice].value;
843
844         menu_callback_function();
845       }
846     }
847   }
848 }
849
850 void DrawInfoScreen_HelpAnim(int start, int max_anims, boolean init)
851 {
852   static int infoscreen_step[MAX_INFO_ELEMENTS_ON_SCREEN];
853   static int infoscreen_frame[MAX_INFO_ELEMENTS_ON_SCREEN];
854   int xstart = mSX + 16;
855   int ystart = mSY + 64 + 2 * 32;
856   int ystep = TILEY + 4;
857   int element, action, direction;
858   int graphic;
859   int delay;
860   int sync_frame;
861   int i, j;
862
863   if (init)
864   {
865     for (i = 0; i < MAX_INFO_ELEMENTS_ON_SCREEN; i++)
866       infoscreen_step[i] = infoscreen_frame[i] = 0;
867
868     ClearWindow();
869     DrawHeadline();
870
871     DrawTextSCentered(100, FONT_TEXT_1, "The Game Elements:");
872
873     DrawTextSCentered(SYSIZE - 20, FONT_TEXT_4,
874                       "Press any key or button for next page");
875
876     FrameCounter = 0;
877   }
878
879   i = j = 0;
880   while (helpanim_info[j].element != HELPANIM_LIST_END)
881   {
882     if (i >= start + MAX_INFO_ELEMENTS_ON_SCREEN ||
883         i >= max_anims)
884       break;
885     else if (i < start)
886     {
887       while (helpanim_info[j].element != HELPANIM_LIST_NEXT)
888         j++;
889
890       j++;
891       i++;
892
893       continue;
894     }
895
896     j += infoscreen_step[i - start];
897
898     element = helpanim_info[j].element;
899     action = helpanim_info[j].action;
900     direction = helpanim_info[j].direction;
901
902     if (element < 0)
903       element = EL_UNKNOWN;
904
905     if (action != -1 && direction != -1)
906       graphic = el_act_dir2img(element, action, direction);
907     else if (action != -1)
908       graphic = el_act2img(element, action);
909     else if (direction != -1)
910       graphic = el_dir2img(element, direction);
911     else
912       graphic = el2img(element);
913
914     delay = helpanim_info[j++].delay;
915
916     if (delay == -1)
917       delay = 1000000;
918
919     if (infoscreen_frame[i - start] == 0)
920     {
921       sync_frame = 0;
922       infoscreen_frame[i - start] = delay - 1;
923     }
924     else
925     {
926       sync_frame = delay - infoscreen_frame[i - start];
927       infoscreen_frame[i - start]--;
928     }
929
930     if (helpanim_info[j].element == HELPANIM_LIST_NEXT)
931     {
932       if (!infoscreen_frame[i - start])
933         infoscreen_step[i - start] = 0;
934     }
935     else
936     {
937       if (!infoscreen_frame[i - start])
938         infoscreen_step[i - start]++;
939       while (helpanim_info[j].element != HELPANIM_LIST_NEXT)
940         j++;
941     }
942
943     j++;
944
945     ClearRectangleOnBackground(drawto, xstart, ystart + (i - start) * ystep,
946                                TILEX, TILEY);
947     DrawGraphicAnimationExt(drawto, xstart, ystart + (i - start) * ystep,
948                             graphic, sync_frame, USE_MASKING);
949
950     if (init)
951       DrawInfoScreen_HelpText(element, action, direction, i - start);
952
953     i++;
954   }
955
956   redraw_mask |= REDRAW_FIELD;
957
958   FrameCounter++;
959 }
960
961 static char *getHelpText(int element, int action, int direction)
962 {
963   char token[MAX_LINE_LEN];
964
965   strcpy(token, element_info[element].token_name);
966
967   if (action != -1)
968     strcat(token, element_action_info[action].suffix);
969
970   if (direction != -1)
971     strcat(token, element_direction_info[MV_DIR_TO_BIT(direction)].suffix);
972
973   return getHashEntry(helptext_info, token);
974 }
975
976 void DrawInfoScreen_HelpText(int element, int action, int direction, int ypos)
977 {
978   int font_nr = FONT_LEVEL_NUMBER;
979   int font_width = getFontWidth(font_nr);
980   int sx = mSX + MINI_TILEX + TILEX + MINI_TILEX;
981   int sy = mSY + 65 + 2 * 32 + 1;
982   int ystep = TILEY + 4;
983   int pad_x = sx - SX;
984   int max_chars_per_line = (SXSIZE - pad_x - MINI_TILEX) / font_width;
985   int max_lines_per_text = 2;    
986   char *text = NULL;
987
988   if (action != -1 && direction != -1)          /* element.action.direction */
989     text = getHelpText(element, action, direction);
990
991   if (text == NULL && action != -1)             /* element.action */
992     text = getHelpText(element, action, -1);
993
994   if (text == NULL && direction != -1)          /* element.direction */
995     text = getHelpText(element, -1, direction);
996
997   if (text == NULL)                             /* base element */
998     text = getHelpText(element, -1, -1);
999
1000   if (text == NULL)                             /* not found */
1001     text = "No description available";
1002
1003   if (strlen(text) <= max_chars_per_line)       /* only one line of text */
1004     sy += getFontHeight(font_nr) / 2;
1005
1006   DrawTextWrapped(sx, sy + ypos * ystep, text, font_nr,
1007                   max_chars_per_line, max_lines_per_text);
1008 }
1009
1010 void DrawInfoScreen_Elements()
1011 {
1012   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_ELEMENTS);
1013
1014   LoadHelpAnimInfo();
1015   LoadHelpTextInfo();
1016
1017   HandleInfoScreen_Elements(MB_MENU_INITIALIZE);
1018
1019   FadeToFront();
1020   InitAnimation();
1021 }
1022
1023 void HandleInfoScreen_Elements(int button)
1024 {
1025   static unsigned long info_delay = 0;
1026   static int num_anims;
1027   static int num_pages;
1028   static int page;
1029   int anims_per_page = MAX_INFO_ELEMENTS_ON_SCREEN;
1030   int button_released = !button;
1031   int i;
1032
1033   if (button == MB_MENU_INITIALIZE)
1034   {
1035     boolean new_element = TRUE;
1036
1037     num_anims = 0;
1038     for (i = 0; helpanim_info[i].element != HELPANIM_LIST_END; i++)
1039     {
1040       if (helpanim_info[i].element == HELPANIM_LIST_NEXT)
1041         new_element = TRUE;
1042       else if (new_element)
1043       {
1044         num_anims++;
1045         new_element = FALSE;
1046       }
1047     }
1048
1049     num_pages = (num_anims + anims_per_page - 1) / anims_per_page;
1050     page = 0;
1051   }
1052   else if (button == MB_MENU_LEAVE)
1053   {
1054     info_mode = INFO_MODE_MAIN;
1055     DrawInfoScreen();
1056
1057     return;
1058   }
1059
1060   if (button_released || button == MB_MENU_INITIALIZE)
1061   {
1062     if (button != MB_MENU_INITIALIZE)
1063       page++;
1064
1065     if (page >= num_pages)
1066     {
1067       FadeSoundsAndMusic();
1068
1069       info_mode = INFO_MODE_MAIN;
1070       DrawInfoScreen();
1071
1072       return;
1073     }
1074
1075     DrawInfoScreen_HelpAnim(page * anims_per_page, num_anims, TRUE);
1076   }
1077   else
1078   {
1079     if (DelayReached(&info_delay, GameFrameDelay))
1080       if (page < num_pages)
1081         DrawInfoScreen_HelpAnim(page * anims_per_page, num_anims, FALSE);
1082
1083     PlayMenuSoundIfLoop();
1084   }
1085 }
1086
1087 void DrawInfoScreen_Music()
1088 {
1089   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_MUSIC);
1090
1091   ClearWindow();
1092   DrawHeadline();
1093
1094   LoadMusicInfo();
1095
1096   HandleInfoScreen_Music(MB_MENU_INITIALIZE);
1097 }
1098
1099 void HandleInfoScreen_Music(int button)
1100 {
1101   static struct MusicFileInfo *list = NULL;
1102   int ystart = 150, dy = 30;
1103   int ybottom = SYSIZE - 20;
1104   int button_released = !button;
1105
1106   if (button == MB_MENU_INITIALIZE)
1107   {
1108     list = music_file_info;
1109
1110     if (list == NULL)
1111     {
1112       FadeSoundsAndMusic();
1113
1114       ClearWindow();
1115       DrawHeadline();
1116
1117       DrawTextSCentered(100, FONT_TEXT_1, "No music info for this level set.");
1118
1119       DrawTextSCentered(ybottom, FONT_TEXT_4,
1120                         "Press any key or button for info menu");
1121
1122       return;
1123     }
1124   }
1125   else if (button == MB_MENU_LEAVE)
1126   {
1127     info_mode = INFO_MODE_MAIN;
1128     DrawInfoScreen();
1129
1130     return;
1131   }
1132
1133   if (button_released || button == MB_MENU_INITIALIZE)
1134   {
1135     int y = 0;
1136
1137     if (button != MB_MENU_INITIALIZE)
1138       if (list != NULL)
1139         list = list->next;
1140
1141     if (list == NULL)
1142     {
1143       info_mode = INFO_MODE_MAIN;
1144       DrawInfoScreen();
1145
1146       return;
1147     }
1148
1149     FadeSoundsAndMusic();
1150
1151     ClearWindow();
1152     DrawHeadline();
1153
1154     if (list->is_sound)
1155     {
1156       int sound = list->music;
1157
1158       if (sound_info[sound].loop)
1159         PlaySoundLoop(sound);
1160       else
1161         PlaySound(sound);
1162
1163       DrawTextSCentered(100, FONT_TEXT_1, "The Game Background Sounds:");
1164     }
1165     else
1166     {
1167       PlayMusic(list->music);
1168
1169       DrawTextSCentered(100, FONT_TEXT_1, "The Game Background Music:");
1170     }
1171
1172     if (!strEqual(list->title, UNKNOWN_NAME))
1173     {
1174       if (!strEqual(list->title_header, UNKNOWN_NAME))
1175         DrawTextSCentered(ystart + y++ * dy, FONT_TEXT_2, list->title_header);
1176
1177       DrawTextFCentered(ystart + y++ * dy, FONT_TEXT_3, "\"%s\"", list->title);
1178     }
1179
1180     if (!strEqual(list->artist, UNKNOWN_NAME))
1181     {
1182       if (!strEqual(list->artist_header, UNKNOWN_NAME))
1183         DrawTextSCentered(ystart + y++ * dy, FONT_TEXT_2, list->artist_header);
1184       else
1185         DrawTextSCentered(ystart + y++ * dy, FONT_TEXT_2, "by");
1186
1187       DrawTextFCentered(ystart + y++ * dy, FONT_TEXT_3, "%s", list->artist);
1188     }
1189
1190     if (!strEqual(list->album, UNKNOWN_NAME))
1191     {
1192       if (!strEqual(list->album_header, UNKNOWN_NAME))
1193         DrawTextSCentered(ystart + y++ * dy, FONT_TEXT_2, list->album_header);
1194       else
1195         DrawTextSCentered(ystart + y++ * dy, FONT_TEXT_2, "from the album");
1196
1197       DrawTextFCentered(ystart + y++ * dy, FONT_TEXT_3, "\"%s\"", list->album);
1198     }
1199
1200     if (!strEqual(list->year, UNKNOWN_NAME))
1201     {
1202       if (!strEqual(list->year_header, UNKNOWN_NAME))
1203         DrawTextSCentered(ystart + y++ * dy, FONT_TEXT_2, list->year_header);
1204       else
1205         DrawTextSCentered(ystart + y++ * dy, FONT_TEXT_2, "from the year");
1206
1207       DrawTextFCentered(ystart + y++ * dy, FONT_TEXT_3, "%s", list->year);
1208     }
1209
1210     DrawTextSCentered(ybottom, FONT_TEXT_4,
1211                       "Press any key or button for next page");
1212   }
1213
1214   if (list != NULL && list->is_sound && sound_info[list->music].loop)
1215     PlaySoundLoop(list->music);
1216 }
1217
1218 void DrawInfoScreen_Credits()
1219 {
1220   int ystart = 150, ystep = 30;
1221   int ybottom = SYSIZE - 20;
1222
1223   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_CREDITS);
1224
1225   FadeSoundsAndMusic();
1226
1227   ClearWindow();
1228   DrawHeadline();
1229
1230   DrawTextSCentered(100, FONT_TEXT_1, "Credits:");
1231   DrawTextSCentered(ystart + 0 * ystep, FONT_TEXT_2, "DOS port of the game:");
1232   DrawTextSCentered(ystart + 1 * ystep, FONT_TEXT_3, "Guido Schulz");
1233   DrawTextSCentered(ystart + 2 * ystep, FONT_TEXT_2, "Additional toons:");
1234   DrawTextSCentered(ystart + 3 * ystep, FONT_TEXT_3, "Karl Hörnell");
1235   DrawTextSCentered(ystart + 5 * ystep, FONT_TEXT_2,
1236                     "...and many thanks to all contributors");
1237   DrawTextSCentered(ystart + 6 * ystep, FONT_TEXT_2, "of new levels!");
1238
1239   DrawTextSCentered(ybottom, FONT_TEXT_4,
1240                     "Press any key or button for info menu");
1241 }
1242
1243 void HandleInfoScreen_Credits(int button)
1244 {
1245   int button_released = !button;
1246
1247   if (button == MB_MENU_LEAVE)
1248   {
1249     info_mode = INFO_MODE_MAIN;
1250     DrawInfoScreen();
1251
1252     return;
1253   }
1254
1255   if (button_released)
1256   {
1257     FadeSoundsAndMusic();
1258
1259     info_mode = INFO_MODE_MAIN;
1260     DrawInfoScreen();
1261   }
1262   else
1263   {
1264     PlayMenuSoundIfLoop();
1265   }
1266 }
1267
1268 void DrawInfoScreen_Program()
1269 {
1270   int ystart = 150, ystep = 30;
1271   int ybottom = SYSIZE - 20;
1272
1273   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_PROGRAM);
1274
1275   ClearWindow();
1276   DrawHeadline();
1277
1278   DrawTextSCentered(100, FONT_TEXT_1, "Program Information:");
1279
1280   DrawTextSCentered(ystart + 0 * ystep, FONT_TEXT_2,
1281                     "This game is Freeware!");
1282   DrawTextSCentered(ystart + 1 * ystep, FONT_TEXT_2,
1283                     "If you like it, send e-mail to:");
1284   DrawTextSCentered(ystart + 2 * ystep, FONT_TEXT_3,
1285                     "info@artsoft.org");
1286   DrawTextSCentered(ystart + 3 * ystep, FONT_TEXT_2,
1287                     "or SnailMail to:");
1288   DrawTextSCentered(ystart + 4 * ystep + 0, FONT_TEXT_3,
1289                     "Holger Schemel");
1290   DrawTextSCentered(ystart + 4 * ystep + 20, FONT_TEXT_3,
1291                     "Detmolder Strasse 189");
1292   DrawTextSCentered(ystart + 4 * ystep + 40, FONT_TEXT_3,
1293                     "33604 Bielefeld");
1294   DrawTextSCentered(ystart + 4 * ystep + 60, FONT_TEXT_3,
1295                     "Germany");
1296
1297   DrawTextSCentered(ystart + 7 * ystep, FONT_TEXT_2,
1298                     "If you have created new levels,");
1299   DrawTextSCentered(ystart + 8 * ystep, FONT_TEXT_2,
1300                     "send them to me to include them!");
1301   DrawTextSCentered(ystart + 9 * ystep, FONT_TEXT_2,
1302                     ":-)");
1303
1304   DrawTextSCentered(ybottom, FONT_TEXT_4,
1305                     "Press any key or button for info menu");
1306 }
1307
1308 void HandleInfoScreen_Program(int button)
1309 {
1310   int button_released = !button;
1311
1312   if (button == MB_MENU_LEAVE)
1313   {
1314     info_mode = INFO_MODE_MAIN;
1315     DrawInfoScreen();
1316
1317     return;
1318   }
1319
1320   if (button_released)
1321   {
1322     FadeSoundsAndMusic();
1323
1324     info_mode = INFO_MODE_MAIN;
1325     DrawInfoScreen();
1326   }
1327   else
1328   {
1329     PlayMenuSoundIfLoop();
1330   }
1331 }
1332
1333 void DrawInfoScreen_LevelSet()
1334 {
1335   int ystart = 150;
1336   int ybottom = SYSIZE - 20;
1337   char *filename = getLevelSetInfoFilename();
1338   int font_nr = FONT_LEVEL_NUMBER;
1339   int font_width = getFontWidth(font_nr);
1340   int font_height = getFontHeight(font_nr);
1341   int pad_x = 32;
1342   int pad_y = ystart;
1343   int sx = SX + pad_x;
1344   int sy = SY + pad_y;
1345   int max_chars_per_line = (SXSIZE - 2 * pad_x) / font_width;
1346   int max_lines_per_screen = (SYSIZE - pad_y) / font_height - 1;
1347
1348   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_LEVELSET);
1349
1350   ClearWindow();
1351   DrawHeadline();
1352
1353   DrawTextSCentered(100, FONT_TEXT_1, "Level Set Information:");
1354
1355   DrawTextSCentered(ybottom, FONT_TEXT_4,
1356                     "Press any key or button for info menu");
1357
1358   if (filename != NULL)
1359     DrawTextFromFile(sx, sy, filename, font_nr, max_chars_per_line,
1360                      max_lines_per_screen);
1361   else
1362     DrawTextSCentered(ystart, FONT_TEXT_2,
1363                       "No information for this level set.");
1364 }
1365
1366 void HandleInfoScreen_LevelSet(int button)
1367 {
1368   int button_released = !button;
1369
1370   if (button == MB_MENU_LEAVE)
1371   {
1372     info_mode = INFO_MODE_MAIN;
1373     DrawInfoScreen();
1374
1375     return;
1376   }
1377
1378   if (button_released)
1379   {
1380     FadeSoundsAndMusic();
1381
1382     info_mode = INFO_MODE_MAIN;
1383     DrawInfoScreen();
1384   }
1385   else
1386   {
1387     PlayMenuSoundIfLoop();
1388   }
1389 }
1390
1391 void DrawInfoScreen()
1392 {
1393   SetMainBackgroundImage(IMG_BACKGROUND_INFO);
1394
1395   if (info_mode == INFO_MODE_ELEMENTS)
1396     DrawInfoScreen_Elements();
1397   else if (info_mode == INFO_MODE_MUSIC)
1398     DrawInfoScreen_Music();
1399   else if (info_mode == INFO_MODE_CREDITS)
1400     DrawInfoScreen_Credits();
1401   else if (info_mode == INFO_MODE_PROGRAM)
1402     DrawInfoScreen_Program();
1403   else if (info_mode == INFO_MODE_LEVELSET)
1404     DrawInfoScreen_LevelSet();
1405   else
1406     DrawInfoScreen_Main();
1407
1408   if (info_mode != INFO_MODE_MUSIC)
1409   {
1410     PlayMenuSound();
1411     PlayMenuMusic();
1412   }
1413 }
1414
1415 void HandleInfoScreen(int mx, int my, int dx, int dy, int button)
1416 {
1417   if (info_mode == INFO_MODE_ELEMENTS)
1418     HandleInfoScreen_Elements(button);
1419   else if (info_mode == INFO_MODE_MUSIC)
1420     HandleInfoScreen_Music(button);
1421   else if (info_mode == INFO_MODE_CREDITS)
1422     HandleInfoScreen_Credits(button);
1423   else if (info_mode == INFO_MODE_PROGRAM)
1424     HandleInfoScreen_Program(button);
1425   else if (info_mode == INFO_MODE_LEVELSET)
1426     HandleInfoScreen_LevelSet(button);
1427   else
1428     HandleInfoScreen_Main(mx, my, dx, dy, button);
1429
1430   DoAnimation();
1431 }
1432
1433
1434 /* ========================================================================= */
1435 /* type name functions                                                       */
1436 /* ========================================================================= */
1437
1438 void HandleTypeName(int newxpos, Key key)
1439 {
1440   static int xpos = 0, ypos = 2;
1441   int font_width = getFontWidth(FONT_INPUT_1_ACTIVE);
1442   int name_width = getFontWidth(FONT_MENU_1) * strlen("Name:");
1443   int startx = mSX + 32 + name_width;
1444   int starty = mSY + ypos * 32;
1445
1446   if (newxpos)
1447   {
1448     xpos = newxpos;
1449
1450     DrawText(startx, starty, setup.player_name, FONT_INPUT_1_ACTIVE);
1451     DrawText(startx + xpos * font_width, starty, "_", FONT_INPUT_1_ACTIVE);
1452
1453     return;
1454   }
1455
1456   if (((key >= KSYM_A && key <= KSYM_Z) ||
1457        (key >= KSYM_a && key <= KSYM_z)) && 
1458       xpos < MAX_PLAYER_NAME_LEN)
1459   {
1460     char ascii;
1461
1462     if (key >= KSYM_A && key <= KSYM_Z)
1463       ascii = 'A' + (char)(key - KSYM_A);
1464     else
1465       ascii = 'a' + (char)(key - KSYM_a);
1466
1467     setup.player_name[xpos] = ascii;
1468     setup.player_name[xpos + 1] = 0;
1469     xpos++;
1470
1471     DrawText(startx, starty, setup.player_name, FONT_INPUT_1_ACTIVE);
1472     DrawText(startx + xpos * font_width, starty, "_", FONT_INPUT_1_ACTIVE);
1473   }
1474   else if ((key == KSYM_Delete || key == KSYM_BackSpace) && xpos > 0)
1475   {
1476     xpos--;
1477     setup.player_name[xpos] = 0;
1478
1479     DrawText(startx + xpos * font_width, starty, "_ ", FONT_INPUT_1_ACTIVE);
1480   }
1481   else if (key == KSYM_Return && xpos > 0)
1482   {
1483     DrawText(startx, starty, setup.player_name, FONT_INPUT_1);
1484     DrawText(startx + xpos * font_width, starty, " ", FONT_INPUT_1_ACTIVE);
1485
1486     SaveSetup();
1487     game_status = GAME_MODE_MAIN;
1488   }
1489 }
1490
1491
1492 /* ========================================================================= */
1493 /* tree menu functions                                                       */
1494 /* ========================================================================= */
1495
1496 static void DrawChooseTree(TreeInfo **ti_ptr)
1497 {
1498   UnmapAllGadgets();
1499
1500   FreeScreenGadgets();
1501   CreateScreenGadgets();
1502
1503   CloseDoor(DOOR_CLOSE_2);
1504
1505   ClearWindow();
1506
1507   HandleChooseTree(0, 0, 0, 0, MB_MENU_INITIALIZE, ti_ptr);
1508   MapChooseTreeGadgets(*ti_ptr);
1509
1510   FadeToFront();
1511   InitAnimation();
1512 }
1513
1514 static void AdjustChooseTreeScrollbar(int id, int first_entry, TreeInfo *ti)
1515 {
1516   struct GadgetInfo *gi = screen_gadget[id];
1517   int items_max, items_visible, item_position;
1518
1519   items_max = numTreeInfoInGroup(ti);
1520   items_visible = NUM_MENU_ENTRIES_ON_SCREEN;
1521   item_position = first_entry;
1522
1523   if (item_position > items_max - items_visible)
1524     item_position = items_max - items_visible;
1525
1526   ModifyGadget(gi, GDI_SCROLLBAR_ITEMS_MAX, items_max,
1527                GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
1528                GDI_SCROLLBAR_ITEM_POSITION, item_position, GDI_END);
1529 }
1530
1531 static void drawChooseTreeList(int first_entry, int num_page_entries,
1532                                TreeInfo *ti)
1533 {
1534   int i;
1535   char *title_string = NULL;
1536 #if 0
1537   int xoffset_sets = 16;
1538 #endif
1539   int yoffset_sets = MENU_TITLE1_YPOS;
1540 #if 0
1541   int xoffset_setup = 16;
1542 #endif
1543   int yoffset_setup = 16;
1544 #if 1
1545 #if 0
1546   int xoffset = (ti->type == TREE_TYPE_LEVEL_DIR ? xoffset_sets :
1547                  xoffset_setup);
1548 #endif
1549   int yoffset = (ti->type == TREE_TYPE_LEVEL_DIR ? yoffset_sets :
1550                  yoffset_setup);
1551 #else
1552   int xoffset = (ti->type == TREE_TYPE_LEVEL_DIR ? 0 : xoffset_setup);
1553   int yoffset = (ti->type == TREE_TYPE_LEVEL_DIR ? 0 : yoffset_setup);
1554 #endif
1555   int last_game_status = game_status;   /* save current game status */
1556
1557   title_string =
1558     (ti->type == TREE_TYPE_LEVEL_DIR ? "Level Sets" :
1559      ti->type == TREE_TYPE_GRAPHICS_DIR ? "Custom Graphics" :
1560      ti->type == TREE_TYPE_SOUNDS_DIR ? "Custom Sounds" :
1561      ti->type == TREE_TYPE_MUSIC_DIR ? "Custom Music" : "");
1562
1563 #if 1
1564   DrawTextSCentered(mSY - SY + yoffset, FONT_TITLE_1, title_string);
1565 #else
1566   DrawText(SX + xoffset, SY + yoffset, title_string, FONT_TITLE_1);
1567 #endif
1568
1569   /* force LEVELS font on artwork setup screen */
1570   game_status = GAME_MODE_LEVELS;
1571
1572   /* clear tree list area, but not title or scrollbar */
1573   DrawBackground(mSX, mSY + MENU_SCREEN_START_YPOS * 32,
1574                  SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset,
1575                  MAX_MENU_ENTRIES_ON_SCREEN * 32);
1576
1577   for (i = 0; i < num_page_entries; i++)
1578   {
1579     TreeInfo *node, *node_first;
1580     int entry_pos = first_entry + i;
1581     int xpos = MENU_SCREEN_START_XPOS;
1582     int ypos = MENU_SCREEN_START_YPOS + i;
1583     int startx = mSX + xpos * 32;
1584     int starty = mSY + ypos * 32;
1585     int font_nr = FONT_TEXT_1;
1586     int font_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
1587     int startx_text = startx + font_xoffset;
1588     int startx_scrollbar = mSX + SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset;
1589     int text_size = startx_scrollbar - startx_text;
1590     int max_buffer_len = text_size / getFontWidth(font_nr);
1591     char buffer[max_buffer_len + 1];
1592
1593     node_first = getTreeInfoFirstGroupEntry(ti);
1594     node = getTreeInfoFromPos(node_first, entry_pos);
1595
1596     strncpy(buffer, node->name, max_buffer_len);
1597     buffer[max_buffer_len] = '\0';
1598
1599     DrawText(startx, starty, buffer, font_nr + node->color);
1600
1601     if (node->parent_link)
1602       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
1603     else if (node->level_group)
1604       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
1605     else
1606       initCursor(i, IMG_MENU_BUTTON);
1607   }
1608
1609   game_status = last_game_status;       /* restore current game status */
1610
1611   redraw_mask |= REDRAW_FIELD;
1612 }
1613
1614 static void drawChooseTreeInfo(int entry_pos, TreeInfo *ti)
1615 {
1616   TreeInfo *node, *node_first;
1617   int x, last_redraw_mask = redraw_mask;
1618 #if 1
1619   int ypos = MENU_TITLE2_YPOS;
1620 #else
1621   int ypos = 40;
1622 #endif
1623
1624   if (ti->type != TREE_TYPE_LEVEL_DIR)
1625     return;
1626
1627   node_first = getTreeInfoFirstGroupEntry(ti);
1628   node = getTreeInfoFromPos(node_first, entry_pos);
1629
1630   DrawBackground(SX, SY + ypos, SXSIZE, getFontHeight(FONT_TITLE_2));
1631
1632   if (node->parent_link)
1633     DrawTextFCentered(ypos, FONT_TITLE_2, "leave group \"%s\"",
1634                       node->class_desc);
1635   else if (node->level_group)
1636     DrawTextFCentered(ypos, FONT_TITLE_2, "enter group \"%s\"",
1637                       node->class_desc);
1638   else if (ti->type == TREE_TYPE_LEVEL_DIR)
1639     DrawTextFCentered(ypos, FONT_TITLE_2, "%3d levels (%s)",
1640                       node->levels, node->class_desc);
1641
1642   /* let BackToFront() redraw only what is needed */
1643   redraw_mask = last_redraw_mask | REDRAW_TILES;
1644   for (x = 0; x < SCR_FIELDX; x++)
1645     MarkTileDirty(x, 1);
1646 }
1647
1648 static void HandleChooseTree(int mx, int my, int dx, int dy, int button,
1649                              TreeInfo **ti_ptr)
1650 {
1651   TreeInfo *ti = *ti_ptr;
1652   int x = 0;
1653   int y = ti->cl_cursor;
1654   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
1655   int num_entries = numTreeInfoInGroup(ti);
1656   int num_page_entries;
1657   int last_game_status = game_status;   /* save current game status */
1658   boolean position_set_by_scrollbar = (dx == 999);
1659
1660   /* force LEVELS draw offset on choose level and artwork setup screen */
1661   game_status = GAME_MODE_LEVELS;
1662
1663   if (num_entries <= NUM_MENU_ENTRIES_ON_SCREEN)
1664     num_page_entries = num_entries;
1665   else
1666     num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
1667
1668   game_status = last_game_status;       /* restore current game status */
1669
1670   if (button == MB_MENU_INITIALIZE)
1671   {
1672     int num_entries = numTreeInfoInGroup(ti);
1673     int entry_pos = posTreeInfo(ti);
1674
1675     if (ti->cl_first == -1)
1676     {
1677       /* only on initialization */
1678       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
1679       ti->cl_cursor = entry_pos - ti->cl_first;
1680     }
1681     else if (ti->cl_cursor >= num_page_entries ||
1682              (num_entries > num_page_entries &&
1683               num_entries - ti->cl_first < num_page_entries))
1684     {
1685       /* only after change of list size (by custom graphic configuration) */
1686       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
1687       ti->cl_cursor = entry_pos - ti->cl_first;
1688     }
1689
1690     if (position_set_by_scrollbar)
1691       ti->cl_first = dy;
1692     else
1693       AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
1694                                 ti->cl_first, ti);
1695
1696     drawChooseTreeList(ti->cl_first, num_page_entries, ti);
1697     drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
1698     drawChooseTreeCursor(ti->cl_cursor, FC_RED);
1699
1700     return;
1701   }
1702   else if (button == MB_MENU_LEAVE)
1703   {
1704     if (ti->node_parent)
1705     {
1706       *ti_ptr = ti->node_parent;
1707       DrawChooseTree(ti_ptr);
1708     }
1709     else if (game_status == GAME_MODE_SETUP)
1710     {
1711       execSetupArtwork();
1712     }
1713     else
1714     {
1715       game_status = GAME_MODE_MAIN;
1716       DrawMainMenu();
1717     }
1718
1719     return;
1720   }
1721
1722   if (mx || my)         /* mouse input */
1723   {
1724     int last_game_status = game_status; /* save current game status */
1725
1726     /* force LEVELS draw offset on artwork setup screen */
1727     game_status = GAME_MODE_LEVELS;
1728
1729     x = (mx - mSX) / 32;
1730     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
1731
1732     game_status = last_game_status;     /* restore current game status */
1733   }
1734   else if (dx || dy)    /* keyboard or scrollbar/scrollbutton input */
1735   {
1736     /* move cursor instead of scrolling when already at start/end of list */
1737     if (dy == -1 * SCROLL_LINE && ti->cl_first == 0)
1738       dy = -1;
1739     else if (dy == +1 * SCROLL_LINE &&
1740              ti->cl_first + num_page_entries == num_entries)
1741       dy = 1;
1742
1743     /* handle scrolling screen one line or page */
1744     if (ti->cl_cursor + dy < 0 ||
1745         ti->cl_cursor + dy > num_page_entries - 1)
1746     {
1747       if (ABS(dy) == SCROLL_PAGE)
1748         step = num_page_entries - 1;
1749
1750       if (dy < 0 && ti->cl_first > 0)
1751       {
1752         /* scroll page/line up */
1753
1754         ti->cl_first -= step;
1755         if (ti->cl_first < 0)
1756           ti->cl_first = 0;
1757
1758         drawChooseTreeList(ti->cl_first, num_page_entries, ti);
1759         drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
1760         drawChooseTreeCursor(ti->cl_cursor, FC_RED);
1761         AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
1762                                   ti->cl_first, ti);
1763       }
1764       else if (dy > 0 && ti->cl_first + num_page_entries < num_entries)
1765       {
1766         /* scroll page/line down */
1767
1768         ti->cl_first += step;
1769         if (ti->cl_first + num_page_entries > num_entries)
1770           ti->cl_first = MAX(0, num_entries - num_page_entries);
1771
1772         drawChooseTreeList(ti->cl_first, num_page_entries, ti);
1773         drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
1774         drawChooseTreeCursor(ti->cl_cursor, FC_RED);
1775         AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
1776                                   ti->cl_first, ti);
1777       }
1778
1779       return;
1780     }
1781
1782     /* handle moving cursor one line */
1783     y = ti->cl_cursor + dy;
1784   }
1785
1786   if (dx == 1)
1787   {
1788     TreeInfo *node_first, *node_cursor;
1789     int entry_pos = ti->cl_first + y;
1790
1791     node_first = getTreeInfoFirstGroupEntry(ti);
1792     node_cursor = getTreeInfoFromPos(node_first, entry_pos);
1793
1794     if (node_cursor->node_group)
1795     {
1796       node_cursor->cl_first = ti->cl_first;
1797       node_cursor->cl_cursor = ti->cl_cursor;
1798       *ti_ptr = node_cursor->node_group;
1799       DrawChooseTree(ti_ptr);
1800
1801       return;
1802     }
1803   }
1804   else if (dx == -1 && ti->node_parent)
1805   {
1806     *ti_ptr = ti->node_parent;
1807     DrawChooseTree(ti_ptr);
1808
1809     return;
1810   }
1811
1812   if (!anyScrollbarGadgetActive() &&
1813       IN_VIS_FIELD(x, y) &&
1814       mx < screen_gadget[SCREEN_CTRL_ID_SCROLL_VERTICAL]->x &&
1815       y >= 0 && y < num_page_entries)
1816   {
1817     if (button)
1818     {
1819       if (y != ti->cl_cursor)
1820       {
1821         drawChooseTreeCursor(y, FC_RED);
1822         drawChooseTreeCursor(ti->cl_cursor, FC_BLUE);
1823         drawChooseTreeInfo(ti->cl_first + y, ti);
1824         ti->cl_cursor = y;
1825       }
1826     }
1827     else
1828     {
1829       TreeInfo *node_first, *node_cursor;
1830       int entry_pos = ti->cl_first + y;
1831
1832       node_first = getTreeInfoFirstGroupEntry(ti);
1833       node_cursor = getTreeInfoFromPos(node_first, entry_pos);
1834
1835       if (node_cursor->node_group)
1836       {
1837         node_cursor->cl_first = ti->cl_first;
1838         node_cursor->cl_cursor = ti->cl_cursor;
1839         *ti_ptr = node_cursor->node_group;
1840         DrawChooseTree(ti_ptr);
1841       }
1842       else if (node_cursor->parent_link)
1843       {
1844         *ti_ptr = node_cursor->node_parent;
1845         DrawChooseTree(ti_ptr);
1846       }
1847       else
1848       {
1849         node_cursor->cl_first = ti->cl_first;
1850         node_cursor->cl_cursor = ti->cl_cursor;
1851         *ti_ptr = node_cursor;
1852
1853         if (ti->type == TREE_TYPE_LEVEL_DIR)
1854         {
1855           LoadLevelSetup_SeriesInfo();
1856
1857           SaveLevelSetup_LastSeries();
1858           SaveLevelSetup_SeriesInfo();
1859           TapeErase();
1860         }
1861
1862         if (game_status == GAME_MODE_SETUP)
1863         {
1864           execSetupArtwork();
1865         }
1866         else
1867         {
1868           game_status = GAME_MODE_MAIN;
1869           DrawMainMenu();
1870         }
1871       }
1872     }
1873   }
1874 }
1875
1876 void DrawChooseLevel()
1877 {
1878   SetMainBackgroundImage(IMG_BACKGROUND_LEVELS);
1879
1880   DrawChooseTree(&leveldir_current);
1881
1882   PlayMenuSound();
1883   PlayMenuMusic();
1884 }
1885
1886 void HandleChooseLevel(int mx, int my, int dx, int dy, int button)
1887 {
1888   HandleChooseTree(mx, my, dx, dy, button, &leveldir_current);
1889
1890   DoAnimation();
1891 }
1892
1893 void DrawHallOfFame(int highlight_position)
1894 {
1895   UnmapAllGadgets();
1896   FadeSoundsAndMusic();
1897   CloseDoor(DOOR_CLOSE_2);
1898
1899   if (highlight_position < 0) 
1900     LoadScore(level_nr);
1901
1902   FadeToFront();
1903   InitAnimation();
1904
1905   PlayMenuSound();
1906   PlayMenuMusic();
1907
1908   HandleHallOfFame(highlight_position, 0, 0, 0, MB_MENU_INITIALIZE);
1909 }
1910
1911 static void drawHallOfFameList(int first_entry, int highlight_position)
1912 {
1913   int i;
1914
1915   SetMainBackgroundImage(IMG_BACKGROUND_SCORES);
1916   ClearWindow();
1917
1918 #if 1
1919   DrawTextSCentered(MENU_TITLE1_YPOS, FONT_TITLE_1, "Hall Of Fame");
1920   DrawTextFCentered(MENU_TITLE2_YPOS, FONT_TITLE_2,
1921                     "HighScores of Level %d", level_nr);
1922 #else
1923   DrawText(mSX + 80, mSY + MENU_TITLE1_YPOS, "Hall Of Fame", FONT_TITLE_1);
1924   DrawTextFCentered(MENU_TITLE2_YPOS, FONT_TITLE_2,
1925                     "HighScores of Level %d", level_nr);
1926 #endif
1927
1928   for (i = 0; i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
1929   {
1930     int entry = first_entry + i;
1931     boolean active = (entry == highlight_position);
1932     int font_nr1 = (active ? FONT_TEXT_1_ACTIVE : FONT_TEXT_1);
1933     int font_nr2 = (active ? FONT_TEXT_2_ACTIVE : FONT_TEXT_2);
1934     int font_nr3 = (active ? FONT_TEXT_3_ACTIVE : FONT_TEXT_3);
1935     int font_nr4 = (active ? FONT_TEXT_4_ACTIVE : FONT_TEXT_4);
1936     int dx1 = 3 * getFontWidth(font_nr1);
1937     int dx2 = dx1 + getFontWidth(font_nr1);
1938     int dx3 = dx2 + 25 * getFontWidth(font_nr3);
1939     int sy = mSY + 64 + i * 32;
1940
1941     DrawText(mSX, sy, int2str(entry + 1, 3), font_nr1);
1942     DrawText(mSX + dx1, sy, ".", font_nr1);
1943     DrawText(mSX + dx2, sy, ".........................", font_nr3);
1944
1945     if (!strEqual(highscore[entry].Name, EMPTY_PLAYER_NAME))
1946       DrawText(mSX + dx2, sy, highscore[entry].Name, font_nr2);
1947
1948     DrawText(mSX + dx3, sy, int2str(highscore[entry].Score, 5), font_nr4);
1949   }
1950
1951   redraw_mask |= REDRAW_FIELD;
1952 }
1953
1954 void HandleHallOfFame(int mx, int my, int dx, int dy, int button)
1955 {
1956   static int first_entry = 0;
1957   static int highlight_position = 0;
1958   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
1959   int button_released = !button;
1960
1961   if (button == MB_MENU_INITIALIZE)
1962   {
1963     first_entry = 0;
1964     highlight_position = mx;
1965     drawHallOfFameList(first_entry, highlight_position);
1966
1967     return;
1968   }
1969
1970   if (ABS(dy) == SCROLL_PAGE)           /* handle scrolling one page */
1971     step = NUM_MENU_ENTRIES_ON_SCREEN - 1;
1972
1973   if (dy < 0)
1974   {
1975     if (first_entry > 0)
1976     {
1977       first_entry -= step;
1978       if (first_entry < 0)
1979         first_entry = 0;
1980
1981       drawHallOfFameList(first_entry, highlight_position);
1982     }
1983   }
1984   else if (dy > 0)
1985   {
1986     if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN < MAX_SCORE_ENTRIES)
1987     {
1988       first_entry += step;
1989       if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN > MAX_SCORE_ENTRIES)
1990         first_entry = MAX(0, MAX_SCORE_ENTRIES - NUM_MENU_ENTRIES_ON_SCREEN);
1991
1992       drawHallOfFameList(first_entry, highlight_position);
1993     }
1994   }
1995   else if (button_released)
1996   {
1997     FadeSound(SND_BACKGROUND_SCORES);
1998     game_status = GAME_MODE_MAIN;
1999     DrawMainMenu();
2000   }
2001
2002   if (game_status == GAME_MODE_SCORES)
2003     PlayMenuSoundIfLoop();
2004
2005   DoAnimation();
2006 }
2007
2008
2009 /* ========================================================================= */
2010 /* setup screen functions                                                    */
2011 /* ========================================================================= */
2012
2013 static struct TokenInfo *setup_info;
2014 static int num_setup_info;
2015
2016 static char *graphics_set_name;
2017 static char *sounds_set_name;
2018 static char *music_set_name;
2019
2020 static void execSetupMain()
2021 {
2022   setup_mode = SETUP_MODE_MAIN;
2023   DrawSetupScreen();
2024 }
2025
2026 static void execSetupGame()
2027 {
2028   setup_mode = SETUP_MODE_GAME;
2029   DrawSetupScreen();
2030 }
2031
2032 static void execSetupEditor()
2033 {
2034   setup_mode = SETUP_MODE_EDITOR;
2035   DrawSetupScreen();
2036 }
2037
2038 static void execSetupGraphics()
2039 {
2040   setup_mode = SETUP_MODE_GRAPHICS;
2041   DrawSetupScreen();
2042 }
2043
2044 static void execSetupSound()
2045 {
2046   setup_mode = SETUP_MODE_SOUND;
2047   DrawSetupScreen();
2048 }
2049
2050 static void execSetupArtwork()
2051 {
2052   setup.graphics_set = artwork.gfx_current->identifier;
2053   setup.sounds_set = artwork.snd_current->identifier;
2054   setup.music_set = artwork.mus_current->identifier;
2055
2056   /* needed if last screen (setup choice) changed graphics, sounds or music */
2057   ReloadCustomArtwork(0);
2058
2059   /* needed for displaying artwork name instead of artwork identifier */
2060   graphics_set_name = artwork.gfx_current->name;
2061   sounds_set_name = artwork.snd_current->name;
2062   music_set_name = artwork.mus_current->name;
2063
2064   setup_mode = SETUP_MODE_ARTWORK;
2065   DrawSetupScreen();
2066 }
2067
2068 static void execSetupChooseGraphics()
2069 {
2070   setup_mode = SETUP_MODE_CHOOSE_GRAPHICS;
2071   DrawSetupScreen();
2072 }
2073
2074 static void execSetupChooseSounds()
2075 {
2076   setup_mode = SETUP_MODE_CHOOSE_SOUNDS;
2077   DrawSetupScreen();
2078 }
2079
2080 static void execSetupChooseMusic()
2081 {
2082   setup_mode = SETUP_MODE_CHOOSE_MUSIC;
2083   DrawSetupScreen();
2084 }
2085
2086 static void execSetupInput()
2087 {
2088   setup_mode = SETUP_MODE_INPUT;
2089   DrawSetupScreen();
2090 }
2091
2092 static void execSetupShortcut1()
2093 {
2094   setup_mode = SETUP_MODE_SHORTCUT_1;
2095   DrawSetupScreen();
2096 }
2097
2098 static void execSetupShortcut2()
2099 {
2100   setup_mode = SETUP_MODE_SHORTCUT_2;
2101   DrawSetupScreen();
2102 }
2103
2104 static void execExitSetup()
2105 {
2106   game_status = GAME_MODE_MAIN;
2107   DrawMainMenu();
2108 }
2109
2110 static void execSaveAndExitSetup()
2111 {
2112   SaveSetup();
2113   execExitSetup();
2114 }
2115
2116 static struct TokenInfo setup_info_main[] =
2117 {
2118   { TYPE_ENTER_MENU,    execSetupGame,          "Game & Menu"           },
2119   { TYPE_ENTER_MENU,    execSetupEditor,        "Editor"                },
2120   { TYPE_ENTER_MENU,    execSetupGraphics,      "Graphics"              },
2121   { TYPE_ENTER_MENU,    execSetupSound,         "Sound & Music"         },
2122   { TYPE_ENTER_MENU,    execSetupArtwork,       "Custom Artwork"        },
2123   { TYPE_ENTER_MENU,    execSetupInput,         "Input Devices"         },
2124   { TYPE_ENTER_MENU,    execSetupShortcut1,     "Key Shortcuts 1"       },
2125   { TYPE_ENTER_MENU,    execSetupShortcut2,     "Key Shortcuts 2"       },
2126   { TYPE_EMPTY,         NULL,                   ""                      },
2127   { TYPE_LEAVE_MENU,    execExitSetup,          "Exit"                  },
2128   { TYPE_LEAVE_MENU,    execSaveAndExitSetup,   "Save and Exit"         },
2129
2130   { 0,                  NULL,                   NULL                    }
2131 };
2132
2133 static struct TokenInfo setup_info_game[] =
2134 {
2135   { TYPE_SWITCH,        &setup.team_mode,       "Team-Mode (Multi-Player):" },
2136   { TYPE_YES_NO,        &setup.input_on_focus,  "Only Move Focussed Player:" },
2137   { TYPE_SWITCH,        &setup.handicap,        "Handicap:"             },
2138   { TYPE_SWITCH,        &setup.skip_levels,     "Skip Unsolved Levels:" },
2139   { TYPE_SWITCH,        &setup.time_limit,      "Time Limit:"           },
2140   { TYPE_SWITCH,        &setup.autorecord,      "Auto-Record Tapes:"    },
2141   { TYPE_EMPTY,         NULL,                   ""                      },
2142   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
2143
2144   { 0,                  NULL,                   NULL                    }
2145 };
2146
2147 static struct TokenInfo setup_info_editor[] =
2148 {
2149   { TYPE_SWITCH,        &setup.editor.el_boulderdash,   "Boulder Dash:" },
2150   { TYPE_SWITCH,        &setup.editor.el_emerald_mine,  "Emerald Mine:" },
2151   { TYPE_SWITCH, &setup.editor.el_emerald_mine_club,    "Emerald Mine Club:" },
2152   { TYPE_SWITCH,        &setup.editor.el_more,          "Rocks'n'Diamonds:" },
2153   { TYPE_SWITCH,        &setup.editor.el_sokoban,       "Sokoban:"      },
2154   { TYPE_SWITCH,        &setup.editor.el_supaplex,      "Supaplex:"     },
2155   { TYPE_SWITCH,        &setup.editor.el_diamond_caves, "Diamond Caves II:" },
2156   { TYPE_SWITCH,        &setup.editor.el_dx_boulderdash,"DX-Boulderdash:" },
2157   { TYPE_SWITCH,        &setup.editor.el_chars,         "Text Characters:" },
2158   { TYPE_SWITCH,        &setup.editor.el_custom,  "Custom & Group Elements:" },
2159   { TYPE_SWITCH,        &setup.editor.el_headlines,     "Headlines:"    },
2160   { TYPE_SWITCH, &setup.editor.el_user_defined, "User defined element list:" },
2161   { TYPE_SWITCH,        &setup.editor.el_dynamic,  "Dynamic level elements:" },
2162   { TYPE_EMPTY,         NULL,                   ""                      },
2163   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
2164
2165   { 0,                  NULL,                   NULL                    }
2166 };
2167
2168 static struct TokenInfo setup_info_graphics[] =
2169 {
2170   { TYPE_SWITCH,        &setup.fullscreen,      "Fullscreen Mode:"      },
2171   { TYPE_SWITCH,        &setup.scroll_delay,    "Delayed Scrolling:"    },
2172 #if 0
2173   { TYPE_SWITCH,        &setup.soft_scrolling,  "Soft Scrolling:"       },
2174   { TYPE_SWITCH,        &setup.double_buffering,"Double-Buffering:"     },
2175   { TYPE_SWITCH,        &setup.fading,          "Fading:"               },
2176 #endif
2177   { TYPE_SWITCH,        &setup.quick_switch,    "Quick Player Focus Switch:" },
2178   { TYPE_SWITCH,        &setup.quick_doors,     "Quick Menu Doors:"     },
2179   { TYPE_SWITCH,        &setup.toons,           "Toons:"                },
2180   { TYPE_ECS_AGA,       &setup.prefer_aga_graphics,"EMC graphics preference:" },
2181   { TYPE_EMPTY,         NULL,                   ""                      },
2182   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
2183
2184   { 0,                  NULL,                   NULL                    }
2185 };
2186
2187 static struct TokenInfo setup_info_sound[] =
2188 {
2189   { TYPE_SWITCH,        &setup.sound_simple,    "Sound Effects (Normal):"  },
2190   { TYPE_SWITCH,        &setup.sound_loops,     "Sound Effects (Looping):" },
2191   { TYPE_SWITCH,        &setup.sound_music,     "Music:"                },
2192   { TYPE_EMPTY,         NULL,                   ""                      },
2193   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
2194
2195   { 0,                  NULL,                   NULL                    }
2196 };
2197
2198 static struct TokenInfo setup_info_artwork[] =
2199 {
2200   { TYPE_ENTER_MENU,    execSetupChooseGraphics,"Custom Graphics"       },
2201   { TYPE_STRING,        &graphics_set_name,     ""                      },
2202   { TYPE_ENTER_MENU,    execSetupChooseSounds,  "Custom Sounds"         },
2203   { TYPE_STRING,        &sounds_set_name,       ""                      },
2204   { TYPE_ENTER_MENU,    execSetupChooseMusic,   "Custom Music"          },
2205   { TYPE_STRING,        &music_set_name,        ""                      },
2206   { TYPE_EMPTY,         NULL,                   ""                      },
2207 #if 1
2208   { TYPE_YES_NO, &setup.override_level_graphics,"Override Level Graphics:" },
2209   { TYPE_YES_NO, &setup.override_level_sounds,  "Override Level Sounds:"   },
2210   { TYPE_YES_NO, &setup.override_level_music,   "Override Level Music:"    },
2211 #else
2212   { TYPE_STRING,        NULL,                   "Override Level Artwork:"},
2213   { TYPE_YES_NO,        &setup.override_level_graphics, "Graphics:"     },
2214   { TYPE_YES_NO,        &setup.override_level_sounds,   "Sounds:"       },
2215   { TYPE_YES_NO,        &setup.override_level_music,    "Music:"        },
2216 #endif
2217   { TYPE_EMPTY,         NULL,                   ""                      },
2218   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
2219
2220   { 0,                  NULL,                   NULL                    }
2221 };
2222
2223 static struct TokenInfo setup_info_shortcut_1[] =
2224 {
2225   { TYPE_KEYTEXT,       NULL,           "Quick Save Game to Tape:",     },
2226   { TYPE_KEY,           &setup.shortcut.save_game, ""                   },
2227   { TYPE_KEYTEXT,       NULL,           "Quick Load Game from Tape:",   },
2228   { TYPE_KEY,           &setup.shortcut.load_game, ""                   },
2229   { TYPE_KEYTEXT,       NULL,           "Start Game & Toggle Pause:",   },
2230   { TYPE_KEY,           &setup.shortcut.toggle_pause, ""                },
2231   { TYPE_EMPTY,         NULL,                   ""                      },
2232   { TYPE_YES_NO,        &setup.ask_on_escape,   "Ask on 'Esc' Key:"     },
2233   { TYPE_YES_NO, &setup.ask_on_escape_editor,   "Ask on 'Esc' Key (Editor):" },
2234   { TYPE_EMPTY,         NULL,                   ""                      },
2235   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
2236
2237   { 0,                  NULL,                   NULL                    }
2238 };
2239
2240 static struct TokenInfo setup_info_shortcut_2[] =
2241 {
2242   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 1:",       },
2243   { TYPE_KEY,           &setup.shortcut.focus_player[0], ""             },
2244   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 2:",       },
2245   { TYPE_KEY,           &setup.shortcut.focus_player[1], ""             },
2246   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 3:",       },
2247   { TYPE_KEY,           &setup.shortcut.focus_player[2], ""             },
2248   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 4:",       },
2249   { TYPE_KEY,           &setup.shortcut.focus_player[3], ""             },
2250   { TYPE_KEYTEXT,       NULL,           "Set Focus to All Players:",    },
2251   { TYPE_KEY,           &setup.shortcut.focus_player_all, ""            },
2252   { TYPE_EMPTY,         NULL,                   ""                      },
2253   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
2254
2255   { 0,                  NULL,                   NULL                    }
2256 };
2257
2258 static Key getSetupKey()
2259 {
2260   Key key = KSYM_UNDEFINED;
2261   boolean got_key_event = FALSE;
2262
2263   while (!got_key_event)
2264   {
2265     if (PendingEvent())         /* got event */
2266     {
2267       Event event;
2268
2269       NextEvent(&event);
2270
2271       switch(event.type)
2272       {
2273         case EVENT_KEYPRESS:
2274           {
2275             key = GetEventKey((KeyEvent *)&event, TRUE);
2276
2277             /* press 'Escape' or 'Enter' to keep the existing key binding */
2278             if (key == KSYM_Escape || key == KSYM_Return)
2279               key = KSYM_UNDEFINED;     /* keep old value */
2280
2281             got_key_event = TRUE;
2282           }
2283           break;
2284
2285         case EVENT_KEYRELEASE:
2286           key_joystick_mapping = 0;
2287           break;
2288
2289         default:
2290           HandleOtherEvents(&event);
2291           break;
2292       }
2293     }
2294
2295     DoAnimation();
2296     BackToFront();
2297
2298     /* don't eat all CPU time */
2299     Delay(10);
2300   }
2301
2302   return key;
2303 }
2304
2305 static int getSetupTextFont(int type)
2306 {
2307   if (type & (TYPE_SWITCH |
2308               TYPE_YES_NO |
2309               TYPE_STRING |
2310               TYPE_ECS_AGA |
2311               TYPE_KEYTEXT))
2312     return FONT_MENU_2;
2313   else
2314     return FONT_MENU_1;
2315 }
2316
2317 static int getSetupValueFont(int type, void *value)
2318 {
2319   if (type & TYPE_KEY)
2320     return (type & TYPE_QUERY ? FONT_INPUT_1_ACTIVE : FONT_VALUE_1);
2321   else if (type & TYPE_STRING)
2322     return FONT_VALUE_2;
2323   else if (type & TYPE_ECS_AGA)
2324     return FONT_VALUE_1;
2325   else if (type & TYPE_BOOLEAN_STYLE)
2326     return (*(boolean *)value ? FONT_OPTION_ON : FONT_OPTION_OFF);
2327   else
2328     return FONT_VALUE_1;
2329 }
2330
2331 static void drawSetupValue(int pos)
2332 {
2333   boolean font_draw_xoffset_modified = FALSE;
2334   int font_draw_xoffset_old = -1;
2335   int xpos = MENU_SCREEN_VALUE_XPOS;
2336   int ypos = MENU_SCREEN_START_YPOS + pos;
2337   int startx = mSX + xpos * 32;
2338   int starty = mSY + ypos * 32;
2339   int font_nr, font_width;
2340   int type = setup_info[pos].type;
2341   void *value = setup_info[pos].value;
2342   char *value_string = getSetupValue(type, value);
2343   int i;
2344
2345   if (value_string == NULL)
2346     return;
2347
2348   if (type & TYPE_KEY)
2349   {
2350 #if 1
2351     xpos = MENU_SCREEN_START_XPOS;
2352 #else
2353     xpos = 3;
2354 #endif
2355
2356     if (type & TYPE_QUERY)
2357     {
2358       value_string = "<press key>";
2359 #if 0
2360       font_nr = FONT_INPUT_1_ACTIVE;
2361 #endif
2362     }
2363   }
2364   else if (type & TYPE_STRING)
2365   {
2366     int max_value_len = (SCR_FIELDX - 2) * 2;
2367
2368     xpos = MENU_SCREEN_START_XPOS;
2369 #if 0
2370     font_nr = FONT_VALUE_2;
2371 #endif
2372
2373     if (strlen(value_string) > max_value_len)
2374       value_string[max_value_len] = '\0';
2375   }
2376   else if (type & TYPE_ECS_AGA)
2377   {
2378 #if 0
2379     font_nr = FONT_VALUE_1;
2380 #endif
2381   }
2382   else if (type & TYPE_BOOLEAN_STYLE)
2383   {
2384 #if 0
2385     font_nr = (*(boolean *)value ? FONT_OPTION_ON : FONT_OPTION_OFF);
2386 #endif
2387   }
2388
2389   startx = mSX + xpos * 32;
2390   starty = mSY + ypos * 32;
2391   font_nr = getSetupValueFont(type, value);
2392   font_width = getFontWidth(font_nr);
2393
2394   /* downward compatibility correction for Juergen Bonhagen's menu settings */
2395   if (setup_mode != SETUP_MODE_INPUT)
2396   {
2397     int check_font_nr = FONT_OPTION_ON; /* known font that needs correction */
2398     int font1_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
2399     int font2_xoffset = getFontBitmapInfo(check_font_nr)->draw_xoffset;
2400     int text_startx = mSX + MENU_SCREEN_START_XPOS * 32;
2401     int text_font_nr = getSetupTextFont(FONT_MENU_2);
2402     int text_font_xoffset = getFontBitmapInfo(text_font_nr)->draw_xoffset;
2403     int text_width = MAX_MENU_TEXT_LENGTH_MEDIUM * getFontWidth(text_font_nr);
2404     boolean correct_font_draw_xoffset = FALSE;
2405
2406     if (xpos == MENU_SCREEN_START_XPOS &&
2407         startx + font1_xoffset < text_startx + text_font_xoffset)
2408       correct_font_draw_xoffset = TRUE;
2409
2410     if (xpos == MENU_SCREEN_VALUE_XPOS &&
2411         startx + font2_xoffset < text_startx + text_width + text_font_xoffset)
2412       correct_font_draw_xoffset = TRUE;
2413
2414 #if 0
2415     printf("::: %d + %d < %d + %d + %d\n",
2416            startx, font_xoffset, text_startx, text_width, text_font_xoffset);
2417     printf("::: => need correction == %d\n", correct_font_draw_xoffset);
2418 #endif
2419
2420     /* check if setup value would overlap with setup text when printed */
2421     /* (this can happen for extreme/wrong values for font draw offset) */
2422     if (correct_font_draw_xoffset)
2423     {
2424       font_draw_xoffset_old = getFontBitmapInfo(font_nr)->draw_xoffset;
2425       font_draw_xoffset_modified = TRUE;
2426
2427       if (type & TYPE_KEY)
2428         getFontBitmapInfo(font_nr)->draw_xoffset += 2 * getFontWidth(font_nr);
2429       else if (!(type & TYPE_STRING))
2430         getFontBitmapInfo(font_nr)->draw_xoffset = text_font_xoffset + 20 -
2431           MAX_MENU_TEXT_LENGTH_MEDIUM * (16 - getFontWidth(text_font_nr));
2432     }
2433   }
2434
2435 #if 1
2436   for (i = 0; i <= MENU_SCREEN_MAX_XPOS - xpos; i++)
2437     DrawText(startx + i * font_width, starty, " ", font_nr);
2438 #else
2439 #if 1
2440   for (i = xpos; i <= MENU_SCREEN_MAX_XPOS; i++)
2441     DrawText(mSX + i * 32, starty, " ", font_nr);
2442 #else
2443   DrawText(startx, starty,
2444            (xpos == 3 ? "              " : "   "), font_nr);
2445 #endif
2446 #endif
2447
2448   DrawText(startx, starty, value_string, font_nr);
2449
2450   if (font_draw_xoffset_modified)
2451     getFontBitmapInfo(font_nr)->draw_xoffset = font_draw_xoffset_old;
2452 }
2453
2454 static void changeSetupValue(int pos)
2455 {
2456   if (setup_info[pos].type & TYPE_BOOLEAN_STYLE)
2457   {
2458     *(boolean *)setup_info[pos].value ^= TRUE;
2459   }
2460   else if (setup_info[pos].type & TYPE_KEY)
2461   {
2462     Key key;
2463
2464     setup_info[pos].type |= TYPE_QUERY;
2465     drawSetupValue(pos);
2466     setup_info[pos].type &= ~TYPE_QUERY;
2467
2468     key = getSetupKey();
2469     if (key != KSYM_UNDEFINED)
2470       *(Key *)setup_info[pos].value = key;
2471   }
2472
2473   drawSetupValue(pos);
2474 }
2475
2476 static void DrawSetupScreen_Generic()
2477 {
2478   char *title_string = NULL;
2479   int i;
2480
2481   UnmapAllGadgets();
2482   CloseDoor(DOOR_CLOSE_2);
2483
2484   ClearWindow();
2485
2486   if (setup_mode == SETUP_MODE_MAIN)
2487   {
2488     setup_info = setup_info_main;
2489     title_string = "Setup";
2490   }
2491   else if (setup_mode == SETUP_MODE_GAME)
2492   {
2493     setup_info = setup_info_game;
2494     title_string = "Setup Game";
2495   }
2496   else if (setup_mode == SETUP_MODE_EDITOR)
2497   {
2498     setup_info = setup_info_editor;
2499     title_string = "Setup Editor";
2500   }
2501   else if (setup_mode == SETUP_MODE_GRAPHICS)
2502   {
2503     setup_info = setup_info_graphics;
2504     title_string = "Setup Graphics";
2505   }
2506   else if (setup_mode == SETUP_MODE_SOUND)
2507   {
2508     setup_info = setup_info_sound;
2509     title_string = "Setup Sound";
2510   }
2511   else if (setup_mode == SETUP_MODE_ARTWORK)
2512   {
2513     setup_info = setup_info_artwork;
2514     title_string = "Custom Artwork";
2515   }
2516   else if (setup_mode == SETUP_MODE_SHORTCUT_1)
2517   {
2518     setup_info = setup_info_shortcut_1;
2519     title_string = "Setup Shortcuts";
2520   }
2521   else if (setup_mode == SETUP_MODE_SHORTCUT_2)
2522   {
2523     setup_info = setup_info_shortcut_2;
2524     title_string = "Setup Shortcuts";
2525   }
2526
2527 #if 1
2528   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, title_string);
2529 #else
2530   DrawText(mSX + 16, mSY + 16, title_string, FONT_TITLE_1);
2531 #endif
2532
2533   num_setup_info = 0;
2534   for (i = 0; setup_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
2535   {
2536     void *value_ptr = setup_info[i].value;
2537     int xpos = MENU_SCREEN_START_XPOS;
2538     int ypos = MENU_SCREEN_START_YPOS + i;
2539 #if 1
2540     int font_nr;
2541 #else
2542     int font_nr = FONT_MENU_1;
2543 #endif
2544
2545     /* set some entries to "unchangeable" according to other variables */
2546     if ((value_ptr == &setup.sound_simple && !audio.sound_available) ||
2547         (value_ptr == &setup.sound_loops  && !audio.loops_available) ||
2548         (value_ptr == &setup.sound_music  && !audio.music_available) ||
2549         (value_ptr == &setup.fullscreen   && !video.fullscreen_available))
2550       setup_info[i].type |= TYPE_GHOSTED;
2551
2552 #if 1
2553     font_nr = getSetupTextFont(setup_info[i].type);
2554 #else
2555 #if 1
2556     if (setup_info[i].type & (TYPE_SWITCH |
2557                               TYPE_YES_NO |
2558                               TYPE_STRING |
2559                               TYPE_ECS_AGA |
2560                               TYPE_KEYTEXT))
2561       font_nr = FONT_MENU_2;
2562 #else
2563     if (setup_info[i].type & TYPE_STRING)
2564       font_nr = FONT_MENU_2;
2565 #endif
2566 #endif
2567
2568     DrawText(mSX + xpos * 32, mSY + ypos * 32, setup_info[i].text, font_nr);
2569
2570     if (setup_info[i].type & TYPE_ENTER_MENU)
2571       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
2572     else if (setup_info[i].type & TYPE_LEAVE_MENU)
2573       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
2574     else if (setup_info[i].type & ~TYPE_SKIP_ENTRY)
2575       initCursor(i, IMG_MENU_BUTTON);
2576
2577     if (setup_info[i].type & TYPE_VALUE)
2578       drawSetupValue(i);
2579
2580     num_setup_info++;
2581   }
2582
2583 #if 0
2584   DrawTextSCentered(SYSIZE - 20, FONT_TEXT_4,
2585                     "Joysticks deactivated in setup menu");
2586 #endif
2587
2588   FadeToFront();
2589   InitAnimation();
2590   HandleSetupScreen_Generic(0, 0, 0, 0, MB_MENU_INITIALIZE);
2591 }
2592
2593 void HandleSetupScreen_Generic(int mx, int my, int dx, int dy, int button)
2594 {
2595   static int choice_store[MAX_SETUP_MODES];
2596   int choice = choice_store[setup_mode];        /* always starts with 0 */
2597   int x = 0;
2598   int y = choice;
2599
2600   if (button == MB_MENU_INITIALIZE)
2601   {
2602     /* advance to first valid menu entry */
2603     while (choice < num_setup_info &&
2604            setup_info[choice].type & TYPE_SKIP_ENTRY)
2605       choice++;
2606     choice_store[setup_mode] = choice;
2607
2608     drawCursor(choice, FC_RED);
2609     return;
2610   }
2611   else if (button == MB_MENU_LEAVE)
2612   {
2613     for (y = 0; y < num_setup_info; y++)
2614     {
2615       if (setup_info[y].type & TYPE_LEAVE_MENU)
2616       {
2617         void (*menu_callback_function)(void) = setup_info[y].value;
2618
2619         menu_callback_function();
2620         break;  /* absolutely needed because function changes 'setup_info'! */
2621       }
2622     }
2623
2624     return;
2625   }
2626
2627   if (mx || my)         /* mouse input */
2628   {
2629     x = (mx - mSX) / 32;
2630     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
2631   }
2632   else if (dx || dy)    /* keyboard input */
2633   {
2634     if (dx)
2635     {
2636       int menu_navigation_type = (dx < 0 ? TYPE_LEAVE_MENU : TYPE_ENTER_MENU);
2637
2638       if (setup_info[choice].type & menu_navigation_type ||
2639           setup_info[choice].type & TYPE_BOOLEAN_STYLE)
2640         button = MB_MENU_CHOICE;
2641     }
2642     else if (dy)
2643       y = choice + dy;
2644
2645     /* jump to next non-empty menu entry (up or down) */
2646     while (y > 0 && y < num_setup_info - 1 &&
2647            setup_info[y].type & TYPE_SKIP_ENTRY)
2648       y += dy;
2649   }
2650
2651   if (IN_VIS_FIELD(x, y) &&
2652       y >= 0 && y < num_setup_info && setup_info[y].type & ~TYPE_SKIP_ENTRY)
2653   {
2654     if (button)
2655     {
2656       if (y != choice)
2657       {
2658         drawCursor(y, FC_RED);
2659         drawCursor(choice, FC_BLUE);
2660         choice = choice_store[setup_mode] = y;
2661       }
2662     }
2663     else if (!(setup_info[y].type & TYPE_GHOSTED))
2664     {
2665       if (setup_info[y].type & TYPE_ENTER_OR_LEAVE_MENU)
2666       {
2667         void (*menu_callback_function)(void) = setup_info[choice].value;
2668
2669         menu_callback_function();
2670       }
2671       else
2672       {
2673         if (setup_info[y].type & TYPE_KEYTEXT &&
2674             setup_info[y + 1].type & TYPE_KEY)
2675           y++;
2676
2677         if (setup_info[y].type & TYPE_VALUE)
2678           changeSetupValue(y);
2679       }
2680     }
2681   }
2682 }
2683
2684 void DrawSetupScreen_Input()
2685 {
2686   ClearWindow();
2687
2688 #if 1
2689   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Setup Input");
2690 #else
2691   DrawText(mSX + 16, mSY + 16, "Setup Input", FONT_TITLE_1);
2692 #endif
2693
2694   initCursor(0,  IMG_MENU_BUTTON);
2695   initCursor(1,  IMG_MENU_BUTTON);
2696   initCursor(2,  IMG_MENU_BUTTON_ENTER_MENU);
2697   initCursor(13, IMG_MENU_BUTTON_LEAVE_MENU);
2698
2699   drawCursorXY(10, 0, IMG_MENU_BUTTON_LEFT);
2700   drawCursorXY(12, 0, IMG_MENU_BUTTON_RIGHT);
2701
2702   DrawText(mSX + 32, mSY +  2 * 32, "Player:", FONT_MENU_1);
2703   DrawText(mSX + 32, mSY +  3 * 32, "Device:", FONT_MENU_1);
2704   DrawText(mSX + 32, mSY + 15 * 32, "Back",   FONT_MENU_1);
2705
2706 #if 0
2707   DeactivateJoystickForCalibration();
2708 #endif
2709 #if 1
2710   DrawTextSCentered(SYSIZE - 20, FONT_TEXT_4,
2711                     "Joysticks deactivated on this screen");
2712 #endif
2713
2714   HandleSetupScreen_Input(0, 0, 0, 0, MB_MENU_INITIALIZE);
2715   FadeToFront();
2716   InitAnimation();
2717 }
2718
2719 static void setJoystickDeviceToNr(char *device_name, int device_nr)
2720 {
2721   if (device_name == NULL)
2722     return;
2723
2724   if (device_nr < 0 || device_nr >= MAX_PLAYERS)
2725     device_nr = 0;
2726
2727   if (strlen(device_name) > 1)
2728   {
2729     char c1 = device_name[strlen(device_name) - 1];
2730     char c2 = device_name[strlen(device_name) - 2];
2731
2732     if (c1 >= '0' && c1 <= '9' && !(c2 >= '0' && c2 <= '9'))
2733       device_name[strlen(device_name) - 1] = '0' + (char)(device_nr % 10);
2734   }
2735   else
2736     strncpy(device_name, getDeviceNameFromJoystickNr(device_nr),
2737             strlen(device_name));
2738 }
2739
2740 static void drawPlayerSetupInputInfo(int player_nr)
2741 {
2742   int i;
2743   static struct SetupKeyboardInfo custom_key;
2744   static struct
2745   {
2746     Key *key;
2747     char *text;
2748   } custom[] =
2749   {
2750     { &custom_key.left,  "Joystick Left"  },
2751     { &custom_key.right, "Joystick Right" },
2752     { &custom_key.up,    "Joystick Up"    },
2753     { &custom_key.down,  "Joystick Down"  },
2754     { &custom_key.snap,  "Button 1"       },
2755     { &custom_key.drop,  "Button 2"       }
2756   };
2757   static char *joystick_name[MAX_PLAYERS] =
2758   {
2759     "Joystick1",
2760     "Joystick2",
2761     "Joystick3",
2762     "Joystick4"
2763   };
2764
2765   InitJoysticks();
2766
2767   custom_key = setup.input[player_nr].key;
2768
2769   DrawText(mSX + 11 * 32, mSY + 2 * 32, int2str(player_nr + 1, 1),
2770            FONT_INPUT_1_ACTIVE);
2771
2772   ClearRectangleOnBackground(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
2773                              TILEX, TILEY);
2774   DrawGraphicThruMaskExt(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
2775                          PLAYER_NR_GFX(IMG_PLAYER_1, player_nr), 0);
2776
2777   if (setup.input[player_nr].use_joystick)
2778   {
2779     char *device_name = setup.input[player_nr].joy.device_name;
2780     char *text = joystick_name[getJoystickNrFromDeviceName(device_name)];
2781     int font_nr = (joystick.fd[player_nr] < 0 ? FONT_VALUE_OLD : FONT_VALUE_1);
2782
2783     DrawText(mSX + 8 * 32, mSY + 3 * 32, text, font_nr);
2784     DrawText(mSX + 32, mSY + 4 * 32, "Calibrate", FONT_MENU_1);
2785   }
2786   else
2787   {
2788     DrawText(mSX + 8 * 32, mSY + 3 * 32, "Keyboard ", FONT_VALUE_1);
2789     DrawText(mSX + 1 * 32, mSY + 4 * 32, "Customize", FONT_MENU_1);
2790   }
2791
2792   DrawText(mSX + 32, mSY + 5 * 32, "Actual Settings:", FONT_MENU_1);
2793   drawCursorXY(1, 4, IMG_MENU_BUTTON_LEFT);
2794   drawCursorXY(1, 5, IMG_MENU_BUTTON_RIGHT);
2795   drawCursorXY(1, 6, IMG_MENU_BUTTON_UP);
2796   drawCursorXY(1, 7, IMG_MENU_BUTTON_DOWN);
2797   DrawText(mSX + 2 * 32, mSY +  6 * 32, ":", FONT_VALUE_OLD);
2798   DrawText(mSX + 2 * 32, mSY +  7 * 32, ":", FONT_VALUE_OLD);
2799   DrawText(mSX + 2 * 32, mSY +  8 * 32, ":", FONT_VALUE_OLD);
2800   DrawText(mSX + 2 * 32, mSY +  9 * 32, ":", FONT_VALUE_OLD);
2801   DrawText(mSX + 1 * 32, mSY + 10 * 32, "Snap Field:", FONT_VALUE_OLD);
2802   DrawText(mSX + 1 * 32, mSY + 12 * 32, "Drop Element:", FONT_VALUE_OLD);
2803
2804   for (i = 0; i < 6; i++)
2805   {
2806     int ypos = 6 + i + (i > 3 ? i-3 : 0);
2807
2808     DrawText(mSX + 3 * 32, mSY + ypos * 32,
2809              "              ", FONT_VALUE_1);
2810     DrawText(mSX + 3 * 32, mSY + ypos * 32,
2811              (setup.input[player_nr].use_joystick ?
2812               custom[i].text :
2813               getKeyNameFromKey(*custom[i].key)), FONT_VALUE_1);
2814   }
2815 }
2816
2817 void HandleSetupScreen_Input(int mx, int my, int dx, int dy, int button)
2818 {
2819   static int choice = 0;
2820   static int player_nr = 0;
2821   int x = 0;
2822   int y = choice;
2823   int pos_start  = SETUPINPUT_SCREEN_POS_START;
2824   int pos_empty1 = SETUPINPUT_SCREEN_POS_EMPTY1;
2825   int pos_empty2 = SETUPINPUT_SCREEN_POS_EMPTY2;
2826   int pos_end    = SETUPINPUT_SCREEN_POS_END;
2827
2828   if (button == MB_MENU_INITIALIZE)
2829   {
2830     drawPlayerSetupInputInfo(player_nr);
2831     drawCursor(choice, FC_RED);
2832
2833     return;
2834   }
2835   else if (button == MB_MENU_LEAVE)
2836   {
2837     setup_mode = SETUP_MODE_MAIN;
2838     DrawSetupScreen();
2839     InitJoysticks();
2840
2841     return;
2842   }
2843
2844   if (mx || my)         /* mouse input */
2845   {
2846     x = (mx - mSX) / 32;
2847     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
2848   }
2849   else if (dx || dy)    /* keyboard input */
2850   {
2851     if (dx && choice == 0)
2852       x = (dx < 0 ? 10 : 12);
2853     else if ((dx && choice == 1) ||
2854              (dx == +1 && choice == 2) ||
2855              (dx == -1 && choice == pos_end))
2856       button = MB_MENU_CHOICE;
2857     else if (dy)
2858       y = choice + dy;
2859
2860     if (y >= pos_empty1 && y <= pos_empty2)
2861       y = (dy > 0 ? pos_empty2 + 1 : pos_empty1 - 1);
2862   }
2863
2864   if (IN_VIS_FIELD(x, y) &&
2865       y == 0 && ((x < 10 && !button) || ((x == 10 || x == 12) && button)))
2866   {
2867     static unsigned long delay = 0;
2868
2869     if (!DelayReached(&delay, GADGET_FRAME_DELAY))
2870       return;
2871
2872     player_nr = (player_nr + (x == 10 ? -1 : +1) + MAX_PLAYERS) % MAX_PLAYERS;
2873
2874     drawPlayerSetupInputInfo(player_nr);
2875   }
2876   else if (IN_VIS_FIELD(x, y) &&
2877            y >= pos_start && y <= pos_end &&
2878            !(y >= pos_empty1 && y <= pos_empty2))
2879   {
2880     if (button)
2881     {
2882       if (y != choice)
2883       {
2884         drawCursor(y, FC_RED);
2885         drawCursor(choice, FC_BLUE);
2886         choice = y;
2887       }
2888     }
2889     else
2890     {
2891       if (y == 1)
2892       {
2893         char *device_name = setup.input[player_nr].joy.device_name;
2894
2895         if (!setup.input[player_nr].use_joystick)
2896         {
2897           int new_device_nr = (dx >= 0 ? 0 : MAX_PLAYERS - 1);
2898
2899           setJoystickDeviceToNr(device_name, new_device_nr);
2900           setup.input[player_nr].use_joystick = TRUE;
2901         }
2902         else
2903         {
2904           int device_nr = getJoystickNrFromDeviceName(device_name);
2905           int new_device_nr = device_nr + (dx >= 0 ? +1 : -1);
2906
2907           if (new_device_nr < 0 || new_device_nr >= MAX_PLAYERS)
2908             setup.input[player_nr].use_joystick = FALSE;
2909           else
2910             setJoystickDeviceToNr(device_name, new_device_nr);
2911         }
2912
2913         drawPlayerSetupInputInfo(player_nr);
2914       }
2915       else if (y == 2)
2916       {
2917         if (setup.input[player_nr].use_joystick)
2918         {
2919           InitJoysticks();
2920           CalibrateJoystick(player_nr);
2921         }
2922         else
2923           CustomizeKeyboard(player_nr);
2924       }
2925       else if (y == pos_end)
2926       {
2927         InitJoysticks();
2928
2929         setup_mode = SETUP_MODE_MAIN;
2930         DrawSetupScreen();
2931       }
2932     }
2933   }
2934 }
2935
2936 void CustomizeKeyboard(int player_nr)
2937 {
2938   int i;
2939   int step_nr;
2940   boolean finished = FALSE;
2941   static struct SetupKeyboardInfo custom_key;
2942   static struct
2943   {
2944     Key *key;
2945     char *text;
2946   } customize_step[] =
2947   {
2948     { &custom_key.left,  "Move Left"    },
2949     { &custom_key.right, "Move Right"   },
2950     { &custom_key.up,    "Move Up"      },
2951     { &custom_key.down,  "Move Down"    },
2952     { &custom_key.snap,  "Snap Field"   },
2953     { &custom_key.drop,  "Drop Element" }
2954   };
2955
2956   /* read existing key bindings from player setup */
2957   custom_key = setup.input[player_nr].key;
2958
2959   ClearWindow();
2960
2961 #if 1
2962   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Keyboard Input");
2963 #else
2964   DrawText(mSX + 16, mSY + 16, "Keyboard Input", FONT_TITLE_1);
2965 #endif
2966
2967   BackToFront();
2968   InitAnimation();
2969
2970   step_nr = 0;
2971   DrawText(mSX, mSY + (2 + 2 * step_nr) * 32,
2972            customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
2973   DrawText(mSX, mSY + (2 + 2 * step_nr + 1) * 32,
2974            "Key:", FONT_INPUT_1_ACTIVE);
2975   DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
2976            getKeyNameFromKey(*customize_step[step_nr].key), FONT_VALUE_OLD);
2977
2978   while (!finished)
2979   {
2980     if (PendingEvent())         /* got event */
2981     {
2982       Event event;
2983
2984       NextEvent(&event);
2985
2986       switch(event.type)
2987       {
2988         case EVENT_KEYPRESS:
2989           {
2990             Key key = GetEventKey((KeyEvent *)&event, FALSE);
2991
2992             if (key == KSYM_Escape || (key == KSYM_Return && step_nr == 6))
2993             {
2994               finished = TRUE;
2995               break;
2996             }
2997
2998             /* all keys configured -- wait for "Escape" or "Return" key */
2999             if (step_nr == 6)
3000               break;
3001
3002             /* press 'Enter' to keep the existing key binding */
3003             if (key == KSYM_Return)
3004               key = *customize_step[step_nr].key;
3005
3006             /* check if key already used */
3007             for (i = 0; i < step_nr; i++)
3008               if (*customize_step[i].key == key)
3009                 break;
3010             if (i < step_nr)
3011               break;
3012
3013             /* got new key binding */
3014             *customize_step[step_nr].key = key;
3015             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
3016                      "             ", FONT_VALUE_1);
3017             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
3018                      getKeyNameFromKey(key), FONT_VALUE_1);
3019             step_nr++;
3020
3021             /* un-highlight last query */
3022             DrawText(mSX, mSY + (2 + 2 * (step_nr - 1)) * 32,
3023                      customize_step[step_nr - 1].text, FONT_MENU_1);
3024             DrawText(mSX, mSY + (2 + 2 * (step_nr - 1) + 1) * 32,
3025                      "Key:", FONT_MENU_1);
3026
3027             /* press 'Enter' to leave */
3028             if (step_nr == 6)
3029             {
3030               DrawText(mSX + 16, mSY + 15 * 32 + 16,
3031                        "Press Enter", FONT_TITLE_1);
3032               break;
3033             }
3034
3035             /* query next key binding */
3036             DrawText(mSX, mSY + (2 + 2 * step_nr) * 32,
3037                      customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
3038             DrawText(mSX, mSY + (2 + 2 * step_nr + 1) * 32,
3039                      "Key:", FONT_INPUT_1_ACTIVE);
3040             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
3041                      getKeyNameFromKey(*customize_step[step_nr].key),
3042                      FONT_VALUE_OLD);
3043           }
3044           break;
3045
3046         case EVENT_KEYRELEASE:
3047           key_joystick_mapping = 0;
3048           break;
3049
3050         default:
3051           HandleOtherEvents(&event);
3052           break;
3053       }
3054     }
3055
3056     DoAnimation();
3057     BackToFront();
3058
3059     /* don't eat all CPU time */
3060     Delay(10);
3061   }
3062
3063   /* write new key bindings back to player setup */
3064   setup.input[player_nr].key = custom_key;
3065
3066   StopAnimation();
3067   DrawSetupScreen_Input();
3068 }
3069
3070 static boolean CalibrateJoystickMain(int player_nr)
3071 {
3072   int new_joystick_xleft = JOYSTICK_XMIDDLE;
3073   int new_joystick_xright = JOYSTICK_XMIDDLE;
3074   int new_joystick_yupper = JOYSTICK_YMIDDLE;
3075   int new_joystick_ylower = JOYSTICK_YMIDDLE;
3076   int new_joystick_xmiddle, new_joystick_ymiddle;
3077
3078   int joystick_fd = joystick.fd[player_nr];
3079   int x, y, last_x, last_y, xpos = 8, ypos = 3;
3080   boolean check[3][3];
3081   int check_remaining = 3 * 3;
3082   int joy_x, joy_y;
3083   int joy_value;
3084   int result = -1;
3085
3086   if (joystick.status == JOYSTICK_NOT_AVAILABLE)
3087     return FALSE;
3088
3089   if (joystick_fd < 0 || !setup.input[player_nr].use_joystick)
3090     return FALSE;
3091
3092   ClearWindow();
3093
3094   for (y = 0; y < 3; y++)
3095   {
3096     for (x = 0; x < 3; x++)
3097     {
3098       DrawGraphic(xpos + x - 1, ypos + y - 1, IMG_MENU_CALIBRATE_BLUE, 0);
3099       check[x][y] = FALSE;
3100     }
3101   }
3102
3103   DrawTextSCentered(mSY - SY +  6 * 32, FONT_TITLE_1, "Rotate joystick");
3104   DrawTextSCentered(mSY - SY +  7 * 32, FONT_TITLE_1, "in all directions");
3105   DrawTextSCentered(mSY - SY +  9 * 32, FONT_TITLE_1, "if all balls");
3106   DrawTextSCentered(mSY - SY + 10 * 32, FONT_TITLE_1, "are marked,");
3107   DrawTextSCentered(mSY - SY + 11 * 32, FONT_TITLE_1, "center joystick");
3108   DrawTextSCentered(mSY - SY + 12 * 32, FONT_TITLE_1, "and");
3109   DrawTextSCentered(mSY - SY + 13 * 32, FONT_TITLE_1, "press any button!");
3110
3111   joy_value = Joystick(player_nr);
3112   last_x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
3113   last_y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
3114
3115   /* eventually uncalibrated center position (joystick could be uncentered) */
3116   if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
3117     return FALSE;
3118
3119   new_joystick_xmiddle = joy_x;
3120   new_joystick_ymiddle = joy_y;
3121
3122   DrawGraphic(xpos + last_x, ypos + last_y, IMG_MENU_CALIBRATE_RED, 0);
3123   BackToFront();
3124
3125   while (Joystick(player_nr) & JOY_BUTTON);     /* wait for released button */
3126   InitAnimation();
3127
3128   while (result < 0)
3129   {
3130     if (PendingEvent())         /* got event */
3131     {
3132       Event event;
3133
3134       NextEvent(&event);
3135
3136       switch(event.type)
3137       {
3138         case EVENT_KEYPRESS:
3139           switch(GetEventKey((KeyEvent *)&event, TRUE))
3140           {
3141             case KSYM_Return:
3142               if (check_remaining == 0)
3143                 result = 1;
3144               break;
3145
3146             case KSYM_Escape:
3147               result = 0;
3148               break;
3149
3150             default:
3151               break;
3152           }
3153           break;
3154
3155         case EVENT_KEYRELEASE:
3156           key_joystick_mapping = 0;
3157           break;
3158
3159         default:
3160           HandleOtherEvents(&event);
3161           break;
3162       }
3163     }
3164
3165     if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
3166       return FALSE;
3167
3168     new_joystick_xleft  = MIN(new_joystick_xleft,  joy_x);
3169     new_joystick_xright = MAX(new_joystick_xright, joy_x);
3170     new_joystick_yupper = MIN(new_joystick_yupper, joy_y);
3171     new_joystick_ylower = MAX(new_joystick_ylower, joy_y);
3172
3173     setup.input[player_nr].joy.xleft = new_joystick_xleft;
3174     setup.input[player_nr].joy.yupper = new_joystick_yupper;
3175     setup.input[player_nr].joy.xright = new_joystick_xright;
3176     setup.input[player_nr].joy.ylower = new_joystick_ylower;
3177     setup.input[player_nr].joy.xmiddle = new_joystick_xmiddle;
3178     setup.input[player_nr].joy.ymiddle = new_joystick_ymiddle;
3179
3180     CheckJoystickData();
3181
3182     joy_value = Joystick(player_nr);
3183
3184     if (joy_value & JOY_BUTTON && check_remaining == 0)
3185       result = 1;
3186
3187     x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
3188     y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
3189
3190     if (x != last_x || y != last_y)
3191     {
3192       DrawGraphic(xpos + last_x, ypos + last_y, IMG_MENU_CALIBRATE_YELLOW, 0);
3193       DrawGraphic(xpos + x,      ypos + y,      IMG_MENU_CALIBRATE_RED,    0);
3194
3195       last_x = x;
3196       last_y = y;
3197
3198       if (check_remaining > 0 && !check[x+1][y+1])
3199       {
3200         check[x+1][y+1] = TRUE;
3201         check_remaining--;
3202       }
3203
3204 #if 0
3205 #ifdef DEBUG
3206       printf("LEFT / MIDDLE / RIGHT == %d / %d / %d\n",
3207              setup.input[player_nr].joy.xleft,
3208              setup.input[player_nr].joy.xmiddle,
3209              setup.input[player_nr].joy.xright);
3210       printf("UP / MIDDLE / DOWN == %d / %d / %d\n",
3211              setup.input[player_nr].joy.yupper,
3212              setup.input[player_nr].joy.ymiddle,
3213              setup.input[player_nr].joy.ylower);
3214 #endif
3215 #endif
3216
3217     }
3218
3219     DoAnimation();
3220     BackToFront();
3221
3222     /* don't eat all CPU time */
3223     Delay(10);
3224   }
3225
3226   /* calibrated center position (joystick should now be centered) */
3227   if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
3228     return FALSE;
3229
3230   new_joystick_xmiddle = joy_x;
3231   new_joystick_ymiddle = joy_y;
3232
3233   StopAnimation();
3234
3235 #if 0
3236   DrawSetupScreen_Input();
3237 #endif
3238
3239   /* wait until the last pressed button was released */
3240   while (Joystick(player_nr) & JOY_BUTTON)
3241   {
3242     if (PendingEvent())         /* got event */
3243     {
3244       Event event;
3245
3246       NextEvent(&event);
3247       HandleOtherEvents(&event);
3248
3249       Delay(10);
3250     }
3251   }
3252
3253   return TRUE;
3254 }
3255
3256 void CalibrateJoystick(int player_nr)
3257 {
3258   if (!CalibrateJoystickMain(player_nr))
3259   {
3260     char *device_name = setup.input[player_nr].joy.device_name;
3261     int nr = getJoystickNrFromDeviceName(device_name) + 1;
3262     int xpos = mSX - SX;
3263     int ypos = mSY - SY;
3264
3265     ClearWindow();
3266
3267     DrawTextF(xpos + 16, ypos + 6 * 32, FONT_TITLE_1, "   JOYSTICK %d   ", nr);
3268     DrawTextF(xpos + 16, ypos + 7 * 32, FONT_TITLE_1, " NOT AVAILABLE! ");
3269     BackToFront();
3270
3271     Delay(2000);                /* show error message for a short time */
3272
3273     ClearEventQueue();
3274   }
3275
3276 #if 1
3277   DrawSetupScreen_Input();
3278 #endif
3279 }
3280
3281 void DrawSetupScreen()
3282 {
3283   DeactivateJoystick();
3284
3285   SetMainBackgroundImage(IMG_BACKGROUND_SETUP);
3286
3287   if (setup_mode == SETUP_MODE_INPUT)
3288     DrawSetupScreen_Input();
3289   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
3290     DrawChooseTree(&artwork.gfx_current);
3291   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
3292     DrawChooseTree(&artwork.snd_current);
3293   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
3294     DrawChooseTree(&artwork.mus_current);
3295   else
3296     DrawSetupScreen_Generic();
3297
3298   PlayMenuSound();
3299   PlayMenuMusic();
3300 }
3301
3302 void HandleSetupScreen(int mx, int my, int dx, int dy, int button)
3303 {
3304   if (setup_mode == SETUP_MODE_INPUT)
3305     HandleSetupScreen_Input(mx, my, dx, dy, button);
3306   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
3307     HandleChooseTree(mx, my, dx, dy, button, &artwork.gfx_current);
3308   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
3309     HandleChooseTree(mx, my, dx, dy, button, &artwork.snd_current);
3310   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
3311     HandleChooseTree(mx, my, dx, dy, button, &artwork.mus_current);
3312   else
3313     HandleSetupScreen_Generic(mx, my, dx, dy, button);
3314
3315   DoAnimation();
3316 }
3317
3318 void HandleGameActions()
3319 {
3320   if (game_status != GAME_MODE_PLAYING)
3321     return;
3322
3323   GameActions();        /* main game loop */
3324
3325   if (tape.auto_play && !tape.playing)
3326     AutoPlayTape();     /* continue automatically playing next tape */
3327 }
3328
3329
3330 /* ---------- new screen button stuff -------------------------------------- */
3331
3332 static struct
3333 {
3334   int gfx_unpressed, gfx_pressed;
3335   int x, y;
3336   int gadget_id;
3337   char *infotext;
3338 } scrollbutton_info[NUM_SCREEN_SCROLLBUTTONS] =
3339 {
3340   {
3341     IMG_MENU_BUTTON_UP, IMG_MENU_BUTTON_UP_ACTIVE,
3342     SC_SCROLL_UP_XPOS, SC_SCROLL_UP_YPOS,
3343     SCREEN_CTRL_ID_SCROLL_UP,
3344     "scroll up"
3345   },
3346   {
3347     IMG_MENU_BUTTON_DOWN, IMG_MENU_BUTTON_DOWN_ACTIVE,
3348     SC_SCROLL_DOWN_XPOS, SC_SCROLL_DOWN_YPOS,
3349     SCREEN_CTRL_ID_SCROLL_DOWN,
3350     "scroll down"
3351   }
3352 };
3353
3354 static struct
3355 {
3356 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
3357   Bitmap **gfx_unpressed, **gfx_pressed;
3358 #else
3359   int gfx_unpressed, gfx_pressed;
3360 #endif
3361   int x, y;
3362   int width, height;
3363   int type;
3364   int gadget_id;
3365   char *infotext;
3366 } scrollbar_info[NUM_SCREEN_SCROLLBARS] =
3367 {
3368   {
3369 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
3370     &scrollbar_bitmap[0], &scrollbar_bitmap[1],
3371 #else
3372     IMG_MENU_SCROLLBAR, IMG_MENU_SCROLLBAR_ACTIVE,
3373 #endif
3374     SC_SCROLL_VERTICAL_XPOS, SC_SCROLL_VERTICAL_YPOS,
3375     SC_SCROLL_VERTICAL_XSIZE, SC_SCROLL_VERTICAL_YSIZE,
3376     GD_TYPE_SCROLLBAR_VERTICAL,
3377     SCREEN_CTRL_ID_SCROLL_VERTICAL,
3378     "scroll level series vertically"
3379   }
3380 };
3381
3382 static void CreateScreenScrollbuttons()
3383 {
3384   struct GadgetInfo *gi;
3385   unsigned long event_mask;
3386   int i;
3387
3388   for (i = 0; i < NUM_SCREEN_SCROLLBUTTONS; i++)
3389   {
3390     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
3391     int gfx_unpressed, gfx_pressed;
3392     int x, y, width, height;
3393     int gd_x1, gd_x2, gd_y1, gd_y2;
3394     int id = scrollbutton_info[i].gadget_id;
3395
3396     event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
3397
3398     x = mSX + scrollbutton_info[i].x + menu.scrollbar_xoffset;
3399     y = mSY + scrollbutton_info[i].y;
3400     width = SC_SCROLLBUTTON_XSIZE;
3401     height = SC_SCROLLBUTTON_YSIZE;
3402
3403     if (id == SCREEN_CTRL_ID_SCROLL_DOWN)
3404       y = mSY + (SC_SCROLL_VERTICAL_YPOS +
3405                  (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE);
3406
3407     gfx_unpressed = scrollbutton_info[i].gfx_unpressed;
3408     gfx_pressed   = scrollbutton_info[i].gfx_pressed;
3409     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
3410     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
3411     gd_x1 = graphic_info[gfx_unpressed].src_x;
3412     gd_y1 = graphic_info[gfx_unpressed].src_y;
3413     gd_x2 = graphic_info[gfx_pressed].src_x;
3414     gd_y2 = graphic_info[gfx_pressed].src_y;
3415
3416     gi = CreateGadget(GDI_CUSTOM_ID, id,
3417                       GDI_CUSTOM_TYPE_ID, i,
3418                       GDI_INFO_TEXT, scrollbutton_info[i].infotext,
3419                       GDI_X, x,
3420                       GDI_Y, y,
3421                       GDI_WIDTH, width,
3422                       GDI_HEIGHT, height,
3423                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
3424                       GDI_STATE, GD_BUTTON_UNPRESSED,
3425                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
3426                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
3427                       GDI_DIRECT_DRAW, FALSE,
3428                       GDI_EVENT_MASK, event_mask,
3429                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
3430                       GDI_END);
3431
3432     if (gi == NULL)
3433       Error(ERR_EXIT, "cannot create gadget");
3434
3435     screen_gadget[id] = gi;
3436   }
3437 }
3438
3439 static void CreateScreenScrollbars()
3440 {
3441   int i;
3442
3443   for (i = 0; i < NUM_SCREEN_SCROLLBARS; i++)
3444   {
3445     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
3446 #if !defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
3447     int gfx_unpressed, gfx_pressed;
3448 #endif
3449     int x, y, width, height;
3450     int gd_x1, gd_x2, gd_y1, gd_y2;
3451     struct GadgetInfo *gi;
3452     int items_max, items_visible, item_position;
3453     unsigned long event_mask;
3454     int num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
3455     int id = scrollbar_info[i].gadget_id;
3456
3457     event_mask = GD_EVENT_MOVING | GD_EVENT_OFF_BORDERS;
3458
3459     x = mSX + scrollbar_info[i].x + menu.scrollbar_xoffset;
3460     y = mSY + scrollbar_info[i].y;
3461     width  = scrollbar_info[i].width;
3462     height = scrollbar_info[i].height;
3463
3464     if (id == SCREEN_CTRL_ID_SCROLL_VERTICAL)
3465       height = (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE;
3466
3467     items_max = num_page_entries;
3468     items_visible = num_page_entries;
3469     item_position = 0;
3470
3471 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
3472     gd_bitmap_unpressed = *scrollbar_info[i].gfx_unpressed;
3473     gd_bitmap_pressed   = *scrollbar_info[i].gfx_pressed;
3474     gd_x1 = 0;
3475     gd_y1 = 0;
3476     gd_x2 = 0;
3477     gd_y2 = 0;
3478 #else
3479     gfx_unpressed = scrollbar_info[i].gfx_unpressed;
3480     gfx_pressed   = scrollbar_info[i].gfx_pressed;
3481     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
3482     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
3483     gd_x1 = graphic_info[gfx_unpressed].src_x;
3484     gd_y1 = graphic_info[gfx_unpressed].src_y;
3485     gd_x2 = graphic_info[gfx_pressed].src_x;
3486     gd_y2 = graphic_info[gfx_pressed].src_y;
3487 #endif
3488
3489     gi = CreateGadget(GDI_CUSTOM_ID, id,
3490                       GDI_CUSTOM_TYPE_ID, i,
3491                       GDI_INFO_TEXT, scrollbar_info[i].infotext,
3492                       GDI_X, x,
3493                       GDI_Y, y,
3494                       GDI_WIDTH, width,
3495                       GDI_HEIGHT, height,
3496                       GDI_TYPE, scrollbar_info[i].type,
3497                       GDI_SCROLLBAR_ITEMS_MAX, items_max,
3498                       GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
3499                       GDI_SCROLLBAR_ITEM_POSITION, item_position,
3500                       GDI_STATE, GD_BUTTON_UNPRESSED,
3501                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
3502                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
3503                       GDI_BORDER_SIZE, SC_BORDER_SIZE, SC_BORDER_SIZE,
3504                       GDI_DIRECT_DRAW, FALSE,
3505                       GDI_EVENT_MASK, event_mask,
3506                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
3507                       GDI_END);
3508
3509     if (gi == NULL)
3510       Error(ERR_EXIT, "cannot create gadget");
3511
3512     screen_gadget[id] = gi;
3513   }
3514 }
3515
3516 void CreateScreenGadgets()
3517 {
3518   int last_game_status = game_status;   /* save current game status */
3519
3520 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
3521   int i;
3522
3523   for (i = 0; i < NUM_SCROLLBAR_BITMAPS; i++)
3524   {
3525     scrollbar_bitmap[i] = CreateBitmap(TILEX, TILEY, DEFAULT_DEPTH);
3526
3527     /* copy pointers to clip mask and GC */
3528     scrollbar_bitmap[i]->clip_mask =
3529       graphic_info[IMG_MENU_SCROLLBAR + i].clip_mask;
3530     scrollbar_bitmap[i]->stored_clip_gc =
3531       graphic_info[IMG_MENU_SCROLLBAR + i].clip_gc;
3532
3533     BlitBitmap(graphic_info[IMG_MENU_SCROLLBAR + i].bitmap,
3534                scrollbar_bitmap[i],
3535                graphic_info[IMG_MENU_SCROLLBAR + i].src_x,
3536                graphic_info[IMG_MENU_SCROLLBAR + i].src_y,
3537                TILEX, TILEY, 0, 0);
3538   }
3539 #endif
3540
3541   /* force LEVELS draw offset for scrollbar / scrollbutton gadgets */
3542   game_status = GAME_MODE_LEVELS;
3543
3544   CreateScreenScrollbuttons();
3545   CreateScreenScrollbars();
3546
3547   game_status = last_game_status;       /* restore current game status */
3548 }
3549
3550 void FreeScreenGadgets()
3551 {
3552   int i;
3553
3554 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
3555   for (i = 0; i < NUM_SCROLLBAR_BITMAPS; i++)
3556   {
3557     /* prevent freeing clip mask and GC twice */
3558     scrollbar_bitmap[i]->clip_mask = None;
3559     scrollbar_bitmap[i]->stored_clip_gc = None;
3560
3561     FreeBitmap(scrollbar_bitmap[i]);
3562   }
3563 #endif
3564
3565   for (i = 0; i < NUM_SCREEN_GADGETS; i++)
3566     FreeGadget(screen_gadget[i]);
3567 }
3568
3569 void MapChooseTreeGadgets(TreeInfo *ti)
3570 {
3571   int num_entries = numTreeInfoInGroup(ti);
3572   int i;
3573
3574   if (num_entries <= NUM_MENU_ENTRIES_ON_SCREEN)
3575     return;
3576
3577   for (i = 0; i < NUM_SCREEN_GADGETS; i++)
3578     MapGadget(screen_gadget[i]);
3579 }
3580
3581 #if 0
3582 void UnmapChooseTreeGadgets()
3583 {
3584   int i;
3585
3586   for (i = 0; i < NUM_SCREEN_GADGETS; i++)
3587     UnmapGadget(screen_gadget[i]);
3588 }
3589 #endif
3590
3591 static void HandleScreenGadgets(struct GadgetInfo *gi)
3592 {
3593   int id = gi->custom_id;
3594
3595   if (game_status != GAME_MODE_LEVELS && game_status != GAME_MODE_SETUP)
3596     return;
3597
3598   switch (id)
3599   {
3600     case SCREEN_CTRL_ID_SCROLL_UP:
3601       if (game_status == GAME_MODE_LEVELS)
3602         HandleChooseLevel(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
3603       else if (game_status == GAME_MODE_SETUP)
3604         HandleSetupScreen(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
3605       break;
3606
3607     case SCREEN_CTRL_ID_SCROLL_DOWN:
3608       if (game_status == GAME_MODE_LEVELS)
3609         HandleChooseLevel(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
3610       else if (game_status == GAME_MODE_SETUP)
3611         HandleSetupScreen(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
3612       break;
3613
3614     case SCREEN_CTRL_ID_SCROLL_VERTICAL:
3615       if (game_status == GAME_MODE_LEVELS)
3616         HandleChooseLevel(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
3617       else if (game_status == GAME_MODE_SETUP)
3618         HandleSetupScreen(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
3619       break;
3620
3621     default:
3622       break;
3623   }
3624 }