f0f2a51ff509ab0daa422be7d5a25c76a183ca21
[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             4
33 #define SETUP_MODE_GRAPHICS             5
34 #define SETUP_MODE_SOUND                6
35 #define SETUP_MODE_ARTWORK              7
36 #define SETUP_MODE_CHOOSE_GRAPHICS      8
37 #define SETUP_MODE_CHOOSE_SOUNDS        9
38 #define SETUP_MODE_CHOOSE_MUSIC         10
39
40 #define MAX_SETUP_MODES                 11
41
42 /* for input setup functions */
43 #define SETUPINPUT_SCREEN_POS_START     0
44 #define SETUPINPUT_SCREEN_POS_END       (SCR_FIELDY - 4)
45 #define SETUPINPUT_SCREEN_POS_EMPTY1    (SETUPINPUT_SCREEN_POS_START + 3)
46 #define SETUPINPUT_SCREEN_POS_EMPTY2    (SETUPINPUT_SCREEN_POS_END - 1)
47
48 /* for various menu stuff  */
49 #define MAX_MENU_ENTRIES_ON_SCREEN      (SCR_FIELDY - 2)
50 #define MENU_SCREEN_START_YPOS          2
51 #define MENU_SCREEN_VALUE_XPOS          14
52
53 /* buttons and scrollbars identifiers */
54 #define SCREEN_CTRL_ID_SCROLL_UP        0
55 #define SCREEN_CTRL_ID_SCROLL_DOWN      1
56 #define SCREEN_CTRL_ID_SCROLL_VERTICAL  2
57
58 #define NUM_SCREEN_SCROLLBUTTONS        2
59 #define NUM_SCREEN_SCROLLBARS           1
60 #define NUM_SCREEN_GADGETS              3
61
62 /* forward declarations of internal functions */
63 static void HandleScreenGadgets(struct GadgetInfo *);
64 static void HandleSetupScreen_Generic(int, int, int, int, int);
65 static void HandleSetupScreen_Input(int, int, int, int, int);
66 static void CustomizeKeyboard(int);
67 static void CalibrateJoystick(int);
68 static void execSetupArtwork(void);
69 static void HandleChooseTree(int, int, int, int, int, TreeInfo **);
70
71 static struct GadgetInfo *screen_gadget[NUM_SCREEN_GADGETS];
72 static int setup_mode = SETUP_MODE_MAIN;
73
74 #define mSX (SX + (game_status >= GAME_MODE_MAIN &&     \
75                    game_status <= GAME_MODE_SETUP ?     \
76                    menu.draw_xoffset[game_status] : menu.draw_xoffset_default))
77 #define mSY (SY + (game_status >= GAME_MODE_MAIN &&     \
78                    game_status <= GAME_MODE_SETUP ?     \
79                    menu.draw_yoffset[game_status] : menu.draw_yoffset_default))
80
81 #define NUM_MENU_ENTRIES_ON_SCREEN (menu.list_size[game_status] > 2 ?   \
82                                     menu.list_size[game_status] :       \
83                                     MAX_MENU_ENTRIES_ON_SCREEN)
84
85 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
86 #define NUM_SCROLLBAR_BITMAPS           2
87 static Bitmap *scrollbar_bitmap[NUM_SCROLLBAR_BITMAPS];
88 #endif
89
90
91 static void drawCursorExt(int xpos, int ypos, int color, int graphic)
92 {
93   static int cursor_array[SCR_FIELDY];
94
95   if (xpos == 0)
96   {
97     if (graphic != 0)
98       cursor_array[ypos] = graphic;
99     else
100       graphic = cursor_array[ypos];
101   }
102
103   if (color == FC_RED)
104     graphic = (graphic == IMG_MENU_BUTTON_LEFT  ? IMG_MENU_BUTTON_LEFT_ACTIVE :
105                graphic == IMG_MENU_BUTTON_RIGHT ? IMG_MENU_BUTTON_RIGHT_ACTIVE:
106                IMG_MENU_BUTTON_ACTIVE);
107
108   ypos += MENU_SCREEN_START_YPOS;
109
110   DrawBackground(mSX + xpos * TILEX, mSY + ypos * TILEY, TILEX, TILEY);
111   DrawGraphicThruMaskExt(drawto, mSX + xpos * TILEX, mSY + ypos * TILEY,
112                          graphic, 0);
113 }
114
115 static void initCursor(int ypos, int graphic)
116 {
117   drawCursorExt(0, ypos, FC_BLUE, graphic);
118 }
119
120 static void drawCursor(int ypos, int color)
121 {
122   drawCursorExt(0, ypos, color, 0);
123 }
124
125 static void drawCursorXY(int xpos, int ypos, int graphic)
126 {
127   drawCursorExt(xpos, ypos, -1, graphic);
128 }
129
130 static void drawChooseTreeCursor(int ypos, int color)
131 {
132   int last_game_status = game_status;   /* save current game status */
133
134   /* force LEVELS draw offset on artwork setup screen */
135   game_status = GAME_MODE_LEVELS;
136
137   drawCursorExt(0, ypos, color, 0);
138
139   game_status = last_game_status;       /* restore current game status */
140 }
141
142 static void PlaySound_Menu_Start(int sound)
143 {
144   if (sound_info[sound].loop)
145     PlaySoundLoop(sound);
146   else
147     PlaySound(sound);
148 }
149
150 static void PlaySound_Menu_Continue(int sound)
151 {
152   if (sound_info[sound].loop)
153     PlaySoundLoop(sound);
154 }
155
156 void DrawHeadline()
157 {
158   int font1_width = getFontWidth(FONT_TITLE_1);
159   int font2_width = getFontWidth(FONT_TITLE_2);
160   int x1 = SX + (SXSIZE - strlen(PROGRAM_TITLE_STRING)   * font1_width) / 2;
161   int x2 = SX + (SXSIZE - strlen(WINDOW_SUBTITLE_STRING) * font2_width) / 2;
162
163   DrawText(x1, SY + 8,  PROGRAM_TITLE_STRING,   FONT_TITLE_1);
164   DrawText(x2, SY + 46, WINDOW_SUBTITLE_STRING, FONT_TITLE_2);
165 }
166
167 static void ToggleFullscreenIfNeeded()
168 {
169   if (setup.fullscreen != video.fullscreen_enabled)
170   {
171     /* save old door content */
172     BlitBitmap(backbuffer, bitmap_db_door,
173                DX, DY, DXSIZE, DYSIZE, DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1);
174
175     /* toggle fullscreen */
176     ChangeVideoModeIfNeeded(setup.fullscreen);
177     setup.fullscreen = video.fullscreen_enabled;
178
179     /* redraw background to newly created backbuffer */
180     BlitBitmap(graphic_info[IMG_GLOBAL_BORDER].bitmap, backbuffer,
181                0,0, WIN_XSIZE,WIN_YSIZE, 0,0);
182
183     /* restore old door content */
184     BlitBitmap(bitmap_db_door, backbuffer,
185                DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1, DXSIZE, DYSIZE, DX, DY);
186
187     redraw_mask = REDRAW_ALL;
188   }
189 }
190
191 void DrawMainMenu()
192 {
193   static LevelDirTree *leveldir_last_valid = NULL;
194   char *name_text = (!options.network && setup.team_mode ? "Team:" : "Name:");
195   int font_width = getFontWidth(FONT_MENU_1);
196   int name_width = font_width * strlen("Name:");
197   int level_width = font_width * strlen("Level:");
198   int i;
199
200   UnmapAllGadgets();
201   FadeSounds();
202
203   KeyboardAutoRepeatOn();
204   ActivateJoystick();
205
206   SetDrawDeactivationMask(REDRAW_NONE);
207   SetDrawBackgroundMask(REDRAW_FIELD);
208
209   audio.sound_deactivated = FALSE;
210
211   /* needed if last screen was the playing screen, invoked from level editor */
212   if (level_editor_test_game)
213   {
214     game_status = GAME_MODE_EDITOR;
215     DrawLevelEd();
216     return;
217   }
218
219   /* needed if last screen was the editor screen */
220   UndrawSpecialEditorDoor();
221
222   /* needed if last screen was the setup screen and fullscreen state changed */
223   ToggleFullscreenIfNeeded();
224
225   /* needed if last screen (level choice) changed graphics, sounds or music */
226   ReloadCustomArtwork();
227
228 #ifdef TARGET_SDL
229   SetDrawtoField(DRAW_BACKBUFFER);
230 #endif
231
232   /* map gadgets for main menu screen */
233   MapTapeButtons();
234
235   /* leveldir_current may be invalid (level group, parent link) */
236   if (!validLevelSeries(leveldir_current))
237     leveldir_current = getFirstValidTreeInfoEntry(leveldir_last_valid);
238
239   /* store valid level series information */
240   leveldir_last_valid = leveldir_current;
241
242   /* level_nr may have been set to value over handicap with level editor */
243   if (setup.handicap && level_nr > leveldir_current->handicap_level)
244     level_nr = leveldir_current->handicap_level;
245
246   GetPlayerConfig();
247   LoadLevel(level_nr);
248
249   SetMainBackgroundImage(IMG_BACKGROUND_MAIN);
250   ClearWindow();
251
252   DrawHeadline();
253
254   DrawText(mSX + 32, mSY + 2*32, name_text, FONT_MENU_1);
255   DrawText(mSX + 32, mSY + 3*32, "Level:", FONT_MENU_1);
256   DrawText(mSX + 32, mSY + 4*32, "Hall Of Fame", FONT_MENU_1);
257   DrawText(mSX + 32, mSY + 5*32, "Level Creator", FONT_MENU_1);
258   DrawText(mSX + 32, mSY + 6*32, "Info Screen", FONT_MENU_1);
259   DrawText(mSX + 32, mSY + 7*32, "Start Game", FONT_MENU_1);
260   DrawText(mSX + 32, mSY + 8*32, "Setup", FONT_MENU_1);
261   DrawText(mSX + 32, mSY + 9*32, "Quit", FONT_MENU_1);
262
263   DrawText(mSX + 32 + name_width, mSY + 2*32, setup.player_name, FONT_INPUT_1);
264   DrawText(mSX + level_width + 5 * 32, mSY + 3*32, int2str(level_nr,3),
265            FONT_VALUE_1);
266
267   DrawMicroLevel(MICROLEV_XPOS, MICROLEV_YPOS, TRUE);
268
269   DrawTextF(mSX + 32 + level_width - 2, mSY + 3*32 + 1, FONT_TEXT_3, "%d-%d",
270             leveldir_current->first_level, leveldir_current->last_level);
271
272   if (leveldir_current->readonly)
273   {
274     DrawTextF(mSX + level_width + 9 * 32 - 2,
275               mSY + 3 * 32 + 1 - 7, FONT_TEXT_3, "READ");
276     DrawTextF(mSX + level_width + 9 * 32 - 2,
277               mSY + 3 * 32 + 1 + 7, FONT_TEXT_3, "ONLY");
278   }
279
280   for(i=0; i<8; i++)
281     initCursor(i, (i == 1 || i == 6 ? IMG_MENU_BUTTON_RIGHT :IMG_MENU_BUTTON));
282
283   drawCursorXY(level_width/32 + 4, 1, IMG_MENU_BUTTON_LEFT);
284   drawCursorXY(level_width/32 + 8, 1, IMG_MENU_BUTTON_RIGHT);
285
286   DrawText(SX + 56, SY + 326, "A Game by Artsoft Entertainment", FONT_TITLE_2);
287
288   FadeToFront();
289   InitAnimation();
290   HandleMainMenu(0,0, 0,0, MB_MENU_INITIALIZE);
291
292   TapeStop();
293   if (TAPE_IS_EMPTY(tape))
294     LoadTape(level_nr);
295   DrawCompleteVideoDisplay();
296
297   OpenDoor(DOOR_CLOSE_1 | DOOR_OPEN_2);
298
299 #if 0
300   ClearEventQueue();
301 #endif
302 }
303
304 static void gotoTopLevelDir()
305 {
306   /* move upwards to top level directory */
307   while (leveldir_current->node_parent)
308   {
309     /* write a "path" into level tree for easy navigation to last level */
310     if (leveldir_current->node_parent->node_group->cl_first == -1)
311     {
312       int num_leveldirs = numTreeInfoInGroup(leveldir_current);
313       int leveldir_pos = posTreeInfo(leveldir_current);
314       int num_page_entries;
315       int cl_first, cl_cursor;
316
317       if (num_leveldirs <= NUM_MENU_ENTRIES_ON_SCREEN)
318         num_page_entries = num_leveldirs;
319       else
320         num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
321
322       cl_first = MAX(0, leveldir_pos - num_page_entries + 1);
323       cl_cursor = leveldir_pos - cl_first;
324
325       leveldir_current->node_parent->node_group->cl_first = cl_first;
326       leveldir_current->node_parent->node_group->cl_cursor = cl_cursor;
327     }
328
329     leveldir_current = leveldir_current->node_parent;
330   }
331 }
332
333 void HandleMainMenu(int mx, int my, int dx, int dy, int button)
334 {
335   static int choice = 0;
336   int x = 0;
337   int y = choice;
338
339   if (button == MB_MENU_INITIALIZE)
340   {
341     drawCursor(choice, FC_RED);
342     return;
343   }
344
345   if (mx || my)         /* mouse input */
346   {
347     x = (mx - mSX) / 32;
348     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
349   }
350   else if (dx || dy)    /* keyboard input */
351   {
352     if (dx && choice == 1)
353       x = (dx < 0 ? 10 : 14);
354     else if (dy)
355       y = choice + dy;
356   }
357
358   if (y == 1 && ((x == 10 && level_nr > leveldir_current->first_level) ||
359                  (x == 14 && level_nr < leveldir_current->last_level)) &&
360       button)
361   {
362     static unsigned long level_delay = 0;
363     int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
364     int new_level_nr, old_level_nr = level_nr;
365
366     new_level_nr = level_nr + (x == 10 ? -step : +step);
367     if (new_level_nr < leveldir_current->first_level)
368       new_level_nr = leveldir_current->first_level;
369     if (new_level_nr > leveldir_current->last_level)
370       new_level_nr = leveldir_current->last_level;
371
372     if (setup.handicap && new_level_nr > leveldir_current->handicap_level)
373       new_level_nr = leveldir_current->handicap_level;
374
375     if (old_level_nr == new_level_nr ||
376         !DelayReached(&level_delay, GADGET_FRAME_DELAY))
377       goto out;
378
379     level_nr = new_level_nr;
380
381     DrawText(mSX + 11 * 32, mSY + 3 * 32, int2str(level_nr, 3), FONT_VALUE_1);
382
383     LoadLevel(level_nr);
384     DrawMicroLevel(MICROLEV_XPOS, MICROLEV_YPOS, TRUE);
385
386     TapeErase();
387     LoadTape(level_nr);
388     DrawCompleteVideoDisplay();
389
390     /* needed because DrawMicroLevel() takes some time */
391     BackToFront();
392     SyncDisplay();
393     DelayReached(&level_delay, 0);      /* reset delay counter */
394   }
395   else if (x == 0 && y >= 0 && y <= 7)
396   {
397     if (button)
398     {
399       if (y != choice)
400       {
401         drawCursor(y, FC_RED);
402         drawCursor(choice, FC_BLUE);
403         choice = y;
404       }
405     }
406     else
407     {
408       if (y == 0)
409       {
410         game_status = GAME_MODE_PSEUDO_TYPENAME;
411         HandleTypeName(strlen(setup.player_name), 0);
412       }
413       else if (y == 1)
414       {
415         if (leveldir_first)
416         {
417           game_status = GAME_MODE_LEVELS;
418           SaveLevelSetup_LastSeries();
419           SaveLevelSetup_SeriesInfo();
420
421           gotoTopLevelDir();
422
423           DrawChooseLevel();
424         }
425       }
426       else if (y == 2)
427       {
428         game_status = GAME_MODE_SCORES;
429         DrawHallOfFame(-1);
430       }
431       else if (y == 3)
432       {
433         if (leveldir_current->readonly &&
434             strcmp(setup.player_name, "Artsoft") != 0)
435           Request("This level is read only !", REQ_CONFIRM);
436         game_status = GAME_MODE_EDITOR;
437         DrawLevelEd();
438       }
439       else if (y == 4)
440       {
441         game_status = GAME_MODE_INFO;
442         DrawHelpScreen();
443       }
444       else if (y == 5)
445       {
446         if (setup.autorecord)
447           TapeStartRecording();
448
449 #if defined(PLATFORM_UNIX)
450         if (options.network)
451           SendToServer_StartPlaying();
452         else
453 #endif
454         {
455           game_status = GAME_MODE_PLAYING;
456           StopAnimation();
457           InitGame();
458         }
459       }
460       else if (y == 6)
461       {
462         game_status = GAME_MODE_SETUP;
463         setup_mode = SETUP_MODE_MAIN;
464         DrawSetupScreen();
465       }
466       else if (y == 7)
467       {
468         SaveLevelSetup_LastSeries();
469         SaveLevelSetup_SeriesInfo();
470         if (Request("Do you really want to quit ?", REQ_ASK | REQ_STAY_CLOSED))
471           game_status = GAME_MODE_QUIT;
472       }
473     }
474   }
475
476   BackToFront();
477
478   out:
479
480   if (game_status == GAME_MODE_MAIN)
481   {
482     DrawMicroLevel(MICROLEV_XPOS, MICROLEV_YPOS, FALSE);
483     DoAnimation();
484   }
485 }
486
487
488 #define MAX_HELPSCREEN_ELS      10
489 #define HA_NEXT                 -999
490 #define HA_END                  -1000
491
492 static long helpscreen_state;
493 static int helpscreen_step[MAX_HELPSCREEN_ELS];
494 static int helpscreen_frame[MAX_HELPSCREEN_ELS];
495
496 static int helpscreen_action[] =
497 {
498   IMG_PLAYER_1_MOVING_DOWN,             16,
499   IMG_PLAYER_1_MOVING_UP,               16,
500   IMG_PLAYER_1_MOVING_LEFT,             16,
501   IMG_PLAYER_1_MOVING_RIGHT,            16,
502   IMG_PLAYER_1_PUSHING_LEFT,            16,
503   IMG_PLAYER_1_PUSHING_RIGHT,           16,                     HA_NEXT,
504
505   IMG_SAND,                             -1,                     HA_NEXT,
506
507   IMG_EMPTY_SPACE,                      -1,                     HA_NEXT,
508
509   IMG_QUICKSAND_EMPTY,                  -1,                     HA_NEXT,
510
511   IMG_STEELWALL,                        -1,                     HA_NEXT,
512
513   IMG_WALL,                             -1,                     HA_NEXT,
514
515   IMG_EXPANDABLE_WALL_GROWING_LEFT,     20,
516   IMG_WALL,                             50,
517   IMG_EMPTY_SPACE,                      20,
518   IMG_EXPANDABLE_WALL_GROWING_RIGHT,    20,
519   IMG_WALL,                             50,
520   IMG_EMPTY_SPACE,                      20,
521   IMG_EXPANDABLE_WALL_GROWING_UP,       20,
522   IMG_WALL,                             50,
523   IMG_EMPTY_SPACE,                      20,
524   IMG_EXPANDABLE_WALL_GROWING_DOWN,     20,
525   IMG_WALL,                             50,
526   IMG_EMPTY_SPACE,                      20,                     HA_NEXT,
527
528   IMG_INVISIBLE_WALL,                   -1,                     HA_NEXT,
529
530   IMG_WALL_CRUMBLED,                    -1,                     HA_NEXT,
531
532   IMG_FONT_GAME_INFO,                   -1,                     HA_NEXT,
533
534   IMG_EMERALD,                          -1,                     HA_NEXT,
535
536   IMG_DIAMOND,                          -1,                     HA_NEXT,
537
538   IMG_BD_DIAMOND,                       -1,                     HA_NEXT,
539
540   IMG_EMERALD_YELLOW,                   50,
541   IMG_EMERALD_RED,                      50,
542   IMG_EMERALD_PURPLE,                   50,                     HA_NEXT,
543
544   IMG_BD_ROCK,                          -1,                     HA_NEXT,
545
546   IMG_BOMB,                             100,
547   IMG_EXPLOSION,                        16,
548   IMG_EMPTY_SPACE,                      10,                     HA_NEXT,
549
550   IMG_NUT,                              100,
551   IMG_NUT_BREAKING,                     6,
552   IMG_EMERALD,                          20,                     HA_NEXT,
553
554   IMG_WALL_EMERALD,                     100,
555   IMG_EXPLOSION,                        16,
556   IMG_EMERALD,                          20,                     HA_NEXT,
557
558   IMG_WALL_DIAMOND,                     100,
559   IMG_EXPLOSION,                        16,
560   IMG_DIAMOND,                          20,                     HA_NEXT,
561
562   IMG_WALL_BD_DIAMOND,                  100,
563   IMG_EXPLOSION,                        16,
564   IMG_BD_DIAMOND,                       20,                     HA_NEXT,
565
566   IMG_WALL_EMERALD_YELLOW,              100,
567   IMG_EXPLOSION,                        16,
568   IMG_EMERALD_YELLOW,                   20,
569   IMG_WALL_EMERALD_RED,                 100,
570   IMG_EXPLOSION,                        16,
571   IMG_EMERALD_RED,                      20,
572   IMG_WALL_EMERALD_PURPLE,              100,
573   IMG_EXPLOSION,                        16,
574   IMG_EMERALD_PURPLE,                   20,                     HA_NEXT,
575
576   IMG_ACID,                             -1,                     HA_NEXT,
577
578   IMG_KEY_1,                            50,
579   IMG_KEY_2,                            50,
580   IMG_KEY_3,                            50,
581   IMG_KEY_4,                            50,                     HA_NEXT,
582
583   IMG_GATE_1,                           50,
584   IMG_GATE_2,                           50,
585   IMG_GATE_3,                           50,
586   IMG_GATE_4,                           50,                     HA_NEXT,
587
588   IMG_GATE_1_GRAY,                      50,
589   IMG_GATE_2_GRAY,                      50,
590   IMG_GATE_3_GRAY,                      50,
591   IMG_GATE_4_GRAY,                      50,                     HA_NEXT,
592
593   IMG_DYNAMITE,                         -1,                     HA_NEXT,
594
595   IMG_DYNAMITE_ACTIVE,                  96,
596   IMG_EXPLOSION,                        16,
597   IMG_EMPTY_SPACE,                      20,                     HA_NEXT,
598
599   IMG_DYNABOMB_ACTIVE,                  100,
600   IMG_EXPLOSION,                        16,
601   IMG_EMPTY_SPACE,                      20,                     HA_NEXT,
602
603   IMG_DYNABOMB_INCREASE_NUMBER,         -1,                     HA_NEXT,
604
605   IMG_DYNABOMB_INCREASE_SIZE,           -1,                     HA_NEXT,
606
607   IMG_DYNABOMB_INCREASE_POWER,          -1,                     HA_NEXT,
608
609   IMG_SPACESHIP_RIGHT,                  16,
610   IMG_SPACESHIP_UP,                     16,
611   IMG_SPACESHIP_LEFT,                   16,
612   IMG_SPACESHIP_DOWN,                   16,                     HA_NEXT,
613
614   IMG_BUG_RIGHT,                        16,
615   IMG_BUG_UP,                           16,
616   IMG_BUG_LEFT,                         16,
617   IMG_BUG_DOWN,                         16,                     HA_NEXT,
618
619   IMG_BD_BUTTERFLY,                     -1,                     HA_NEXT,
620
621   IMG_BD_FIREFLY,                       -1,                     HA_NEXT,
622
623   IMG_PACMAN_RIGHT,                     16,
624   IMG_PACMAN_UP,                        16,
625   IMG_PACMAN_LEFT,                      16,
626   IMG_PACMAN_DOWN,                      16,                     HA_NEXT,
627
628   IMG_YAMYAM,                           -1,                     HA_NEXT,
629
630   IMG_DARK_YAMYAM,                      -1,                     HA_NEXT,
631
632   IMG_ROBOT,                            -1,                     HA_NEXT,
633
634   IMG_MOLE_MOVING_RIGHT,                16,
635   IMG_MOLE_MOVING_UP,                   16,
636   IMG_MOLE_MOVING_LEFT,                 16,
637   IMG_MOLE_MOVING_DOWN,                 16,                     HA_NEXT,
638
639   IMG_PENGUIN_MOVING_RIGHT,             16,
640   IMG_PENGUIN_MOVING_UP,                16,
641   IMG_PENGUIN_MOVING_LEFT,              16,
642   IMG_PENGUIN_MOVING_DOWN,              16,                     HA_NEXT,
643
644   IMG_PIG_MOVING_RIGHT,                 16,
645   IMG_PIG_MOVING_UP,                    16,
646   IMG_PIG_MOVING_LEFT,                  16,
647   IMG_PIG_MOVING_DOWN,                  16,                     HA_NEXT,
648
649   IMG_DRAGON_MOVING_RIGHT,              16,
650   IMG_DRAGON_MOVING_UP,                 16,
651   IMG_DRAGON_MOVING_LEFT,               16,
652   IMG_DRAGON_MOVING_DOWN,               16,                     HA_NEXT,
653
654   IMG_SATELLITE,                        -1,                     HA_NEXT,
655
656   IMG_ROBOT_WHEEL,                      50,
657   IMG_ROBOT_WHEEL_ACTIVE,               100,                    HA_NEXT,
658
659   IMG_LAMP,                             50,
660   IMG_LAMP_ACTIVE,                      50,                     HA_NEXT,
661
662   IMG_TIME_ORB_FULL,                    50,
663   IMG_TIME_ORB_EMPTY,                   50,                     HA_NEXT,
664
665   IMG_AMOEBA_DROP,                      50,
666   IMG_AMOEBA_GROWING,                   6,
667   IMG_AMOEBA_WET,                       20,                     HA_NEXT,
668
669   IMG_AMOEBA_DEAD,                      -1,                     HA_NEXT,
670
671   IMG_AMOEBA_WET,                       -1,                     HA_NEXT,
672
673   IMG_AMOEBA_WET,                       100,
674   IMG_AMOEBA_GROWING,                   6,                      HA_NEXT,
675
676   IMG_AMOEBA_FULL,                      50,
677   IMG_AMOEBA_DEAD,                      50,
678   IMG_EXPLOSION,                        16,
679   IMG_DIAMOND,                          20,                     HA_NEXT,
680
681   IMG_GAME_OF_LIFE,                     -1,                     HA_NEXT,
682
683   IMG_BIOMAZE,                          -1,                     HA_NEXT,
684
685   IMG_MAGIC_WALL_ACTIVE,                -1,                     HA_NEXT,
686
687   IMG_BD_MAGIC_WALL_ACTIVE,             -1,                     HA_NEXT,
688
689   IMG_EXIT_CLOSED,                      200,
690   IMG_EXIT_OPENING,                     16,
691   IMG_EXIT_OPEN,                        100,                    HA_NEXT,
692
693   IMG_EXIT_OPEN,                        -1,                     HA_NEXT,
694
695   IMG_SOKOBAN_OBJECT,                   -1,                     HA_NEXT,
696
697   IMG_SOKOBAN_FIELD_EMPTY,              -1,                     HA_NEXT,
698
699   IMG_SOKOBAN_FIELD_FULL,               -1,                     HA_NEXT,
700
701   IMG_SPEED_PILL,                       -1,                     HA_NEXT,
702
703   HA_END
704 };
705 static char *helpscreen_eltext[][2] =
706 {
707  {"THE HERO:",                          "(Is _this_ guy good old Rockford?)"},
708  {"Normal sand:",                       "You can dig through it"},
709  {"Empty field:",                       "You can walk through it"},
710  {"Quicksand: You cannot pass it,",     "but rocks can fall through it"},
711  {"Massive Wall:",                      "Nothing can go through it"},
712  {"Normal Wall: You can't go through",  "it, but you can bomb it away"},
713  {"Growing Wall: Grows in several di-", "rections if there is an empty field"},
714  {"Invisible Wall: Behaves like normal","wall, but is invisible"},
715  {"Old Wall: Like normal wall, but",    "some things can fall down from it"},
716  {"Letter Wall: Looks like a letter,",  "behaves like a normal wall"},
717  {"Emerald: You must collect enough of","them to finish a level"},
718  {"Diamond: Counts as 3 emeralds, but", "can be destroyed by rocks"},
719  {"Diamond (BD style): Counts like one","emerald and behaves a bit different"},
720  {"Colorful Gems:",                     "Seem to behave like Emeralds"},
721  {"Rock: Smashes several things;",      "Can be moved by the player"},
722  {"Bomb: You can move it, but be",      "careful when dropping it"},
723  {"Nut: Throw a rock on it to open it;","Each nut contains an emerald"},
724  {"Wall with an emerald inside:",       "Bomb the wall away to get it"},
725  {"Wall with a diamond inside:",        "Bomb the wall away to get it"},
726  {"Wall with BD style diamond inside:", "Bomb the wall away to get it"},
727  {"Wall with colorful gem inside:",     "Bomb the wall away to get it"},
728  {"Acid: Things that fall in are gone", "forever (including our hero)"},
729  {"Key: Opens the door that has the",   "same color (red/yellow/green/blue)"},
730  {"Door: Can be opened by the key",     "with the same color"},
731  {"Door: You have to find out the",     "right color of the key for it"},
732  {"Dynamite: Collect it and use it to", "destroy walls or kill enemies"},
733  {"Dynamite: This one explodes after",  "a few seconds"},
734  {"Dyna Bomb: Explodes in 4 directions","with variable explosion size"},
735  {"Dyna Bomb: Increases the number of", "dyna bombs available at a time"},
736  {"Dyna Bomb: Increases the size of",   "explosion of dyna bombs"},
737  {"Dyna Bomb: Increases the power of",  "explosion of dyna bombs"},
738  {"Spaceship: Moves at the left side",  "of walls; don't touch it!"},
739  {"Bug: Moves at the right side",       "of walls; don't touch it!"},
740  {"Butterfly: Moves at the right side", "of walls; don't touch it!"},
741  {"Firefly: Moves at the left side",    "of walls; don't touch it!"},
742  {"Pacman: Eats the amoeba and you,",   "if you're not careful"},
743  {"Cruncher: Eats diamonds and you,",   "if you're not careful"},
744  {"Cruncher (BD style):",               "Eats almost everything"},
745  {"Robot: Tries to kill the player",    ""},
746  {"The mole: Eats the amoeba and turns","empty space into normal sand"},
747  {"The penguin: Guide him to the exit,","but keep him away from monsters!"},
748  {"The Pig: Harmless, but eats all",    "gems it can get"},
749  {"The Dragon: Breathes fire,",         "especially to some monsters"},
750  {"Sonde: Follows you everywhere;",     "harmless, but may block your way"},
751  {"Magic Wheel: Touch it to get rid of","the robots for some seconds"},
752  {"Light Bulb: All of them must be",    "switched on to finish a level"},
753  {"Extra Time Orb: Adds some seconds",  "to the time available for the level"},
754  {"Amoeba Drop: Grows to an amoeba on", "the ground - don't touch it"},
755  {"Dead Amoeba: Does not grow, but",    "can still kill bugs and spaceships"},
756  {"Normal Amoeba: Grows through empty", "fields, sand and quicksand"},
757  {"Dropping Amoeba: This one makes",    "drops that grow to a new amoeba"},
758  {"Living Amoeba (BD style): Contains", "other element, when surrounded"},
759  {"Game Of Life: Behaves like the well","known 'Game Of Life' (2333 style)"},
760  {"Biomaze: A bit like the 'Game Of",   "Life', but builds crazy mazes"},
761  {"Magic Wall: Changes rocks, emeralds","and diamonds when they pass it"},
762  {"Magic Wall (BD style):",             "Changes rocks and BD style diamonds"},
763  {"Exit door: Opens if you have enough","emeralds to finish the level"},
764  {"Open exit door: Enter here to leave","the level and exit the actual game"},
765  {"Sokoban element: Object which must", "be pushed to an empty field"},
766  {"Sokoban element: Empty field where", "a Sokoban object can be placed on"},
767  {"Sokoban element: Field with object", "which can be pushed away"},
768  {"Speed pill: Lets the player run",    "twice as fast as normally"},
769 };
770 static int num_helpscreen_els = sizeof(helpscreen_eltext) / (2*sizeof(char *));
771
772 static char *helpscreen_music[][3] =
773 {
774   { "Alchemy",                  "Ian Boddy",            "Drive" },
775   { "The Chase",                "Propaganda",           "A Secret Wish" },
776   { "Network 23",               "Tangerine Dream",      "Exit" },
777   { "Czardasz",                 "Robert Pieculewicz",   "Czardasz" },
778   { "21st Century Common Man",  "Tangerine Dream",      "Tyger" },
779   { "Voyager",                  "The Alan Parsons Project","Pyramid" },
780   { "Twilight Painter",         "Tangerine Dream",      "Heartbreakers" }
781 };
782 static int num_helpscreen_music = 7;
783 static int helpscreen_musicpos;
784
785 #if 0
786 void OLD_DrawHelpScreenElAction(int start)
787 {
788   int i = 0, j = 0;
789   int frame, graphic;
790   int xstart = SX+16, ystart = SY+64+2*32, ystep = TILEY+4;
791
792   while(helpscreen_action[j] != HA_END)
793   {
794     if (i>=start+MAX_HELPSCREEN_ELS || i>=num_helpscreen_els)
795       break;
796     else if (i<start || helpscreen_delay[i-start])
797     {
798       if (i>=start && helpscreen_delay[i-start])
799         helpscreen_delay[i-start]--;
800
801       while(helpscreen_action[j] != HA_NEXT)
802         j++;
803       j++;
804       i++;
805       continue;
806     }
807
808     j += 3*helpscreen_step[i-start];
809     graphic = helpscreen_action[j++];
810
811     if (helpscreen_frame[i-start])
812     {
813       frame = helpscreen_action[j++] - helpscreen_frame[i-start];
814       helpscreen_frame[i-start]--;
815     }
816     else
817     {
818       frame = 0;
819       helpscreen_frame[i-start] = helpscreen_action[j++]-1;
820     }
821
822     helpscreen_delay[i-start] = helpscreen_action[j++] - 1;
823
824     if (helpscreen_action[j] == HA_NEXT)
825     {
826       if (!helpscreen_frame[i-start])
827         helpscreen_step[i-start] = 0;
828     }
829     else
830     {
831       if (!helpscreen_frame[i-start])
832         helpscreen_step[i-start]++;
833       while(helpscreen_action[j] != HA_NEXT)
834         j++;
835     }
836     j++;
837
838     DrawOldGraphicExt(drawto, xstart, ystart+(i-start)*ystep, graphic+frame);
839     i++;
840   }
841
842   for(i=2;i<16;i++)
843   {
844     MarkTileDirty(0,i);
845     MarkTileDirty(1,i);
846   }
847 }
848 #endif
849
850 void DrawHelpScreenElAction(int start)
851 {
852   int i = 0, j = 0;
853   int xstart = mSX + 16;
854   int ystart = mSY + 64 + 2 * 32;
855   int ystep = TILEY + 4;
856   int graphic;
857   int frame_count;
858   int sync_frame;
859
860   while (helpscreen_action[j] != HA_END)
861   {
862     if (i >= start + MAX_HELPSCREEN_ELS || i >= num_helpscreen_els)
863       break;
864     else if (i < start)
865     {
866       while (helpscreen_action[j] != HA_NEXT)
867         j++;
868
869       j++;
870       i++;
871
872       continue;
873     }
874
875     j += 2 * helpscreen_step[i-start];
876     graphic = helpscreen_action[j++];
877     frame_count = helpscreen_action[j++];
878     if (frame_count == -1)
879       frame_count = 1000000;
880
881     if (helpscreen_frame[i-start] == 0)
882     {
883       sync_frame = 0;
884       helpscreen_frame[i-start] = frame_count - 1;
885     }
886     else
887     {
888       sync_frame = frame_count - helpscreen_frame[i-start];
889       helpscreen_frame[i-start]--;
890     }
891
892     if (helpscreen_action[j] == HA_NEXT)
893     {
894       if (!helpscreen_frame[i-start])
895         helpscreen_step[i-start] = 0;
896     }
897     else
898     {
899       if (!helpscreen_frame[i-start])
900         helpscreen_step[i-start]++;
901       while(helpscreen_action[j] != HA_NEXT)
902         j++;
903     }
904     j++;
905
906 #if 1
907     ClearRectangleOnBackground(drawto, xstart, ystart + (i - start) * ystep,
908                                TILEX, TILEY);
909     DrawGraphicAnimationExt(drawto, xstart, ystart + (i - start) * ystep,
910                             graphic, sync_frame, USE_MASKING);
911 #else
912     frame = getGraphicAnimationFrame(graphic, sync_frame);
913
914     DrawGraphicExt(drawto, xstart, ystart + (i-start) * ystep,
915                    graphic, frame);
916 #endif
917
918     i++;
919   }
920
921   for(i=2; i<16; i++)
922   {
923     MarkTileDirty(0, i);
924     MarkTileDirty(1, i);
925   }
926
927   FrameCounter++;
928 }
929
930 void DrawHelpScreenElText(int start)
931 {
932   int i;
933   int xstart = mSX + 56, ystart = mSY + 65 + 2 * 32, ystep = TILEY + 4;
934   int ybottom = SYSIZE - 20;
935
936   SetMainBackgroundImage(IMG_BACKGROUND_INFO);
937   ClearWindow();
938   DrawHeadline();
939
940   DrawTextFCentered(100, FONT_TEXT_1, "The game elements:");
941
942   for(i=start; i < start + MAX_HELPSCREEN_ELS && i < num_helpscreen_els; i++)
943   {
944     DrawText(xstart,
945              ystart + (i - start) * ystep + (*helpscreen_eltext[i][1] ? 0 : 8),
946              helpscreen_eltext[i][0], FONT_TEXT_2);
947     DrawText(xstart, ystart + (i - start) * ystep + 16,
948              helpscreen_eltext[i][1], FONT_TEXT_2);
949   }
950
951   DrawTextFCentered(ybottom, FONT_TEXT_4,
952                     "Press any key or button for next page");
953 }
954
955 void DrawHelpScreenMusicText(int num)
956 {
957   int ystart = 150, ystep = 30;
958   int ybottom = SYSIZE - 20;
959
960   FadeSounds();
961   ClearWindow();
962   DrawHeadline();
963
964   DrawTextFCentered(100, FONT_TEXT_1, "The game background music loops:");
965
966   DrawTextFCentered(ystart + 0 * ystep, FONT_TEXT_2, "Excerpt from");
967   DrawTextFCentered(ystart + 1 * ystep, FONT_TEXT_3,
968                     "\"%s\"", helpscreen_music[num][0]);
969   DrawTextFCentered(ystart + 2 * ystep, FONT_TEXT_2, "by");
970   DrawTextFCentered(ystart + 3 * ystep, FONT_TEXT_3,
971                     "%s", helpscreen_music[num][1]);
972   DrawTextFCentered(ystart + 4 * ystep, FONT_TEXT_2, "from the album");
973   DrawTextFCentered(ystart + 5 * ystep, FONT_TEXT_3,
974                     "\"%s\"", helpscreen_music[num][2]);
975
976   DrawTextFCentered(ybottom, FONT_TEXT_4,
977                     "Press any key or button for next page");
978
979 #if 0
980   PlaySoundLoop(background_loop[num]);
981 #endif
982 }
983
984 void DrawHelpScreenCreditsText()
985 {
986   int ystart = 150, ystep = 30;
987   int ybottom = SYSIZE - 20;
988
989   FadeSounds();
990   ClearWindow();
991   DrawHeadline();
992
993   DrawTextFCentered(100, FONT_TEXT_1, "Credits:");
994   DrawTextFCentered(ystart + 0 * ystep, FONT_TEXT_2, "DOS port of the game:");
995   DrawTextFCentered(ystart + 1 * ystep, FONT_TEXT_3, "Guido Schulz");
996   DrawTextFCentered(ystart + 2 * ystep, FONT_TEXT_2, "Additional toons:");
997   DrawTextFCentered(ystart + 3 * ystep, FONT_TEXT_3, "Karl Hörnell");
998   DrawTextFCentered(ystart + 5 * ystep, FONT_TEXT_2,
999                     "...and many thanks to all contributors");
1000   DrawTextFCentered(ystart + 6 * ystep, FONT_TEXT_2, "of new levels!");
1001
1002   DrawTextFCentered(ybottom, FONT_TEXT_4,
1003                     "Press any key or button for next page");
1004 }
1005
1006 void DrawHelpScreenContactText()
1007 {
1008   int ystart = 150, ystep = 30;
1009   int ybottom = SYSIZE - 20;
1010
1011   ClearWindow();
1012   DrawHeadline();
1013
1014   DrawTextFCentered(100, FONT_TEXT_1, "Program information:");
1015
1016   DrawTextFCentered(ystart + 0 * ystep, FONT_TEXT_2,
1017                     "This game is Freeware!");
1018   DrawTextFCentered(ystart + 1 * ystep, FONT_TEXT_2,
1019                     "If you like it, send e-mail to:");
1020   DrawTextFCentered(ystart + 2 * ystep, FONT_TEXT_3,
1021                     "info@artsoft.org");
1022   DrawTextFCentered(ystart + 3 * ystep, FONT_TEXT_2,
1023                     "or SnailMail to:");
1024   DrawTextFCentered(ystart + 4 * ystep + 0, FONT_TEXT_3,
1025                     "Holger Schemel");
1026   DrawTextFCentered(ystart + 4 * ystep + 20, FONT_TEXT_3,
1027                     "Detmolder Strasse 189");
1028   DrawTextFCentered(ystart + 4 * ystep + 40, FONT_TEXT_3,
1029                     "33604 Bielefeld");
1030   DrawTextFCentered(ystart + 4 * ystep + 60, FONT_TEXT_3,
1031                     "Germany");
1032
1033   DrawTextFCentered(ystart + 7 * ystep, FONT_TEXT_2,
1034                     "If you have created new levels,");
1035   DrawTextFCentered(ystart + 8 * ystep, FONT_TEXT_2,
1036                     "send them to me to include them!");
1037   DrawTextFCentered(ystart + 9 * ystep, FONT_TEXT_2,
1038                     ":-)");
1039
1040   DrawTextFCentered(ybottom, FONT_TEXT_4,
1041                     "Press any key or button for main menu");
1042 }
1043
1044 void DrawHelpScreen()
1045 {
1046   int i;
1047
1048   UnmapAllGadgets();
1049   CloseDoor(DOOR_CLOSE_2);
1050
1051   for(i=0;i<MAX_HELPSCREEN_ELS;i++)
1052     helpscreen_step[i] = helpscreen_frame[i] = 0;
1053   helpscreen_musicpos = 0;
1054   helpscreen_state = 0;
1055
1056   DrawHelpScreenElText(0);
1057   DrawHelpScreenElAction(0);
1058
1059   FadeToFront();
1060   InitAnimation();
1061
1062 #if 0
1063   PlaySoundLoop(SND_BACKGROUND_INFO);
1064 #else
1065   PlaySound_Menu_Start(SND_BACKGROUND_INFO);
1066 #endif
1067 }
1068
1069 void HandleHelpScreen(int button)
1070 {
1071   static unsigned long hs_delay = 0;
1072   int num_helpscreen_els_pages =
1073     (num_helpscreen_els + MAX_HELPSCREEN_ELS-1) / MAX_HELPSCREEN_ELS;
1074   int button_released = !button;
1075   int i;
1076
1077   if (button_released)
1078   {
1079     if (helpscreen_state < num_helpscreen_els_pages - 1)
1080     {
1081       for(i=0;i<MAX_HELPSCREEN_ELS;i++)
1082         helpscreen_step[i] = helpscreen_frame[i] = 0;
1083       helpscreen_state++;
1084
1085       FrameCounter = 0;
1086       DrawHelpScreenElText(helpscreen_state * MAX_HELPSCREEN_ELS);
1087       DrawHelpScreenElAction(helpscreen_state * MAX_HELPSCREEN_ELS);
1088     }
1089     else if (helpscreen_state <
1090              num_helpscreen_els_pages + num_helpscreen_music - 1)
1091     {
1092       helpscreen_state++;
1093       DrawHelpScreenMusicText(helpscreen_state - num_helpscreen_els_pages);
1094     }
1095     else if (helpscreen_state ==
1096              num_helpscreen_els_pages + num_helpscreen_music - 1)
1097     {
1098       helpscreen_state++;
1099       DrawHelpScreenCreditsText();
1100     }
1101     else if (helpscreen_state ==
1102              num_helpscreen_els_pages + num_helpscreen_music)
1103     {
1104       helpscreen_state++;
1105       DrawHelpScreenContactText();
1106     }
1107     else
1108     {
1109       FadeSounds();
1110
1111       game_status = GAME_MODE_MAIN;
1112       DrawMainMenu();
1113     }
1114   }
1115   else
1116   {
1117     if (DelayReached(&hs_delay, GAME_FRAME_DELAY))
1118     {
1119       if (helpscreen_state < num_helpscreen_els_pages)
1120         DrawHelpScreenElAction(helpscreen_state * MAX_HELPSCREEN_ELS);
1121     }
1122
1123     /* !!! workaround for playing "music" that is really a sound loop (and
1124        must therefore periodically be reactivated with the current sound
1125        engine !!! */
1126 #if 0
1127     PlaySoundLoop(SND_BACKGROUND_INFO);
1128 #else
1129     PlaySound_Menu_Continue(SND_BACKGROUND_INFO);
1130 #endif
1131
1132     DoAnimation();
1133   }
1134
1135   BackToFront();
1136 }
1137
1138 void HandleTypeName(int newxpos, Key key)
1139 {
1140   static int xpos = 0, ypos = 2;
1141   int font_width = getFontWidth(FONT_INPUT_1_ACTIVE);
1142   int name_width = getFontWidth(FONT_MENU_1) * strlen("Name:");
1143   int startx = mSX + 32 + name_width;
1144   int starty = mSY + ypos * 32;
1145
1146   if (newxpos)
1147   {
1148     xpos = newxpos;
1149
1150     DrawText(startx, starty, setup.player_name, FONT_INPUT_1_ACTIVE);
1151     DrawText(startx + xpos * font_width, starty, "_", FONT_INPUT_1_ACTIVE);
1152
1153     return;
1154   }
1155
1156   if (((key >= KSYM_A && key <= KSYM_Z) ||
1157        (key >= KSYM_a && key <= KSYM_z)) && 
1158       xpos < MAX_PLAYER_NAME_LEN)
1159   {
1160     char ascii;
1161
1162     if (key >= KSYM_A && key <= KSYM_Z)
1163       ascii = 'A' + (char)(key - KSYM_A);
1164     else
1165       ascii = 'a' + (char)(key - KSYM_a);
1166
1167     setup.player_name[xpos] = ascii;
1168     setup.player_name[xpos + 1] = 0;
1169     xpos++;
1170
1171     DrawText(startx, starty, setup.player_name, FONT_INPUT_1_ACTIVE);
1172     DrawText(startx + xpos * font_width, starty, "_", FONT_INPUT_1_ACTIVE);
1173   }
1174   else if ((key == KSYM_Delete || key == KSYM_BackSpace) && xpos > 0)
1175   {
1176     xpos--;
1177     setup.player_name[xpos] = 0;
1178
1179     DrawText(startx + xpos * font_width, starty, "_ ", FONT_INPUT_1_ACTIVE);
1180   }
1181   else if (key == KSYM_Return && xpos > 0)
1182   {
1183     DrawText(startx, starty, setup.player_name, FONT_INPUT_1);
1184     DrawText(startx + xpos * font_width, starty, " ", FONT_INPUT_1_ACTIVE);
1185
1186     SaveSetup();
1187     game_status = GAME_MODE_MAIN;
1188   }
1189
1190   BackToFront();
1191 }
1192
1193 static void DrawChooseTree(TreeInfo **ti_ptr)
1194 {
1195   UnmapAllGadgets();
1196   CloseDoor(DOOR_CLOSE_2);
1197
1198   ClearWindow();
1199
1200   HandleChooseTree(0,0, 0,0, MB_MENU_INITIALIZE, ti_ptr);
1201   MapChooseTreeGadgets(*ti_ptr);
1202
1203   FadeToFront();
1204   InitAnimation();
1205 }
1206
1207 static void AdjustChooseTreeScrollbar(int id, int first_entry, TreeInfo *ti)
1208 {
1209   struct GadgetInfo *gi = screen_gadget[id];
1210   int items_max, items_visible, item_position;
1211
1212   items_max = numTreeInfoInGroup(ti);
1213   items_visible = NUM_MENU_ENTRIES_ON_SCREEN;
1214   item_position = first_entry;
1215
1216   if (item_position > items_max - items_visible)
1217     item_position = items_max - items_visible;
1218
1219   ModifyGadget(gi, GDI_SCROLLBAR_ITEMS_MAX, items_max,
1220                GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
1221                GDI_SCROLLBAR_ITEM_POSITION, item_position, GDI_END);
1222 }
1223
1224 static void drawChooseTreeList(int first_entry, int num_page_entries,
1225                                TreeInfo *ti)
1226 {
1227   int i;
1228   char buffer[SCR_FIELDX * 2];
1229   int max_buffer_len = (SCR_FIELDX - 2) * 2;
1230   char *title_string = NULL;
1231   int xoffset_setup = 16;
1232   int yoffset_setup = 16;
1233   int xoffset = (ti->type == TREE_TYPE_LEVEL_DIR ? 0 : xoffset_setup);
1234   int yoffset = (ti->type == TREE_TYPE_LEVEL_DIR ? 0 : yoffset_setup);
1235   int last_game_status = game_status;   /* save current game status */
1236
1237   DrawBackground(SX, SY, SXSIZE - 32, SYSIZE);
1238   redraw_mask |= REDRAW_FIELD;
1239
1240   title_string =
1241     (ti->type == TREE_TYPE_LEVEL_DIR ? "Level Directories" :
1242      ti->type == TREE_TYPE_GRAPHICS_DIR ? "Custom Graphics" :
1243      ti->type == TREE_TYPE_SOUNDS_DIR ? "Custom Sounds" :
1244      ti->type == TREE_TYPE_MUSIC_DIR ? "Custom Music" : "");
1245
1246   DrawText(SX + xoffset, SY + yoffset, title_string, FONT_TITLE_1);
1247
1248   /* force LEVELS font on artwork setup screen */
1249   game_status = GAME_MODE_LEVELS;
1250
1251   for(i=0; i<num_page_entries; i++)
1252   {
1253     TreeInfo *node, *node_first;
1254     int entry_pos = first_entry + i;
1255     int ypos = MENU_SCREEN_START_YPOS + i;
1256
1257     node_first = getTreeInfoFirstGroupEntry(ti);
1258     node = getTreeInfoFromPos(node_first, entry_pos);
1259
1260     strncpy(buffer, node->name , max_buffer_len);
1261     buffer[max_buffer_len] = '\0';
1262
1263     DrawText(mSX + 32, mSY + ypos * 32, buffer, FONT_TEXT_1 + node->color);
1264
1265     if (node->parent_link)
1266       initCursor(i, IMG_MENU_BUTTON_LEFT);
1267     else if (node->level_group)
1268       initCursor(i, IMG_MENU_BUTTON_RIGHT);
1269     else
1270       initCursor(i, IMG_MENU_BUTTON);
1271   }
1272
1273   game_status = last_game_status;       /* restore current game status */
1274 }
1275
1276 static void drawChooseTreeInfo(int entry_pos, TreeInfo *ti)
1277 {
1278   TreeInfo *node, *node_first;
1279   int x, last_redraw_mask = redraw_mask;
1280
1281   if (ti->type != TREE_TYPE_LEVEL_DIR)
1282     return;
1283
1284   node_first = getTreeInfoFirstGroupEntry(ti);
1285   node = getTreeInfoFromPos(node_first, entry_pos);
1286
1287   DrawBackground(SX + 32, SY + 32, SXSIZE - 64, 32);
1288
1289   if (node->parent_link)
1290     DrawTextFCentered(40, FONT_TITLE_2, "leave group \"%s\"",
1291                       node->class_desc);
1292   else if (node->level_group)
1293     DrawTextFCentered(40, FONT_TITLE_2, "enter group \"%s\"",
1294                       node->class_desc);
1295   else if (ti->type == TREE_TYPE_LEVEL_DIR)
1296     DrawTextFCentered(40, FONT_TITLE_2, "%3d levels (%s)",
1297                       node->levels, node->class_desc);
1298
1299   /* let BackToFront() redraw only what is needed */
1300   redraw_mask = last_redraw_mask | REDRAW_TILES;
1301   for (x=0; x<SCR_FIELDX; x++)
1302     MarkTileDirty(x, 1);
1303 }
1304
1305 static void HandleChooseTree(int mx, int my, int dx, int dy, int button,
1306                              TreeInfo **ti_ptr)
1307 {
1308   TreeInfo *ti = *ti_ptr;
1309   int x = 0;
1310   int y = ti->cl_cursor;
1311   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
1312   int num_entries = numTreeInfoInGroup(ti);
1313   int num_page_entries;
1314
1315   if (num_entries <= NUM_MENU_ENTRIES_ON_SCREEN)
1316     num_page_entries = num_entries;
1317   else
1318     num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
1319
1320   if (button == MB_MENU_INITIALIZE)
1321   {
1322     int num_entries = numTreeInfoInGroup(ti);
1323     int entry_pos = posTreeInfo(ti);
1324
1325     if (ti->cl_first == -1)
1326     {
1327       /* only on initialization */
1328       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
1329       ti->cl_cursor = entry_pos - ti->cl_first;
1330     }
1331     else if (ti->cl_cursor >= num_page_entries ||
1332              (num_entries > num_page_entries &&
1333               num_entries - ti->cl_first < num_page_entries))
1334     {
1335       /* only after change of list size (by custom graphic configuration) */
1336       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
1337       ti->cl_cursor = entry_pos - ti->cl_first;
1338     }
1339
1340     if (dx == 999)      /* first entry is set by scrollbar position */
1341       ti->cl_first = dy;
1342     else
1343       AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
1344                                 ti->cl_first, ti);
1345
1346     drawChooseTreeList(ti->cl_first, num_page_entries, ti);
1347     drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
1348     drawChooseTreeCursor(ti->cl_cursor, FC_RED);
1349
1350     return;
1351   }
1352   else if (button == MB_MENU_LEAVE)
1353   {
1354     if (ti->node_parent)
1355     {
1356       *ti_ptr = ti->node_parent;
1357       DrawChooseTree(ti_ptr);
1358     }
1359     else if (game_status == GAME_MODE_SETUP)
1360     {
1361       execSetupArtwork();
1362     }
1363     else
1364     {
1365       game_status = GAME_MODE_MAIN;
1366       DrawMainMenu();
1367     }
1368
1369     return;
1370   }
1371
1372   if (mx || my)         /* mouse input */
1373   {
1374     int last_game_status = game_status; /* save current game status */
1375
1376     /* force LEVELS draw offset on artwork setup screen */
1377     game_status = GAME_MODE_LEVELS;
1378
1379     x = (mx - mSX) / 32;
1380     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
1381
1382     game_status = last_game_status;     /* restore current game status */
1383   }
1384   else if (dx || dy)    /* keyboard or scrollbar/scrollbutton input */
1385   {
1386     /* move cursor instead of scrolling when already at start/end of list */
1387     if (dy == -1 * SCROLL_LINE && ti->cl_first == 0)
1388       dy = -1;
1389     else if (dy == +1 * SCROLL_LINE &&
1390              ti->cl_first + num_page_entries == num_entries)
1391       dy = 1;
1392
1393     /* handle scrolling screen one line or page */
1394     if (ti->cl_cursor + dy < 0 ||
1395         ti->cl_cursor + dy > num_page_entries - 1)
1396     {
1397       if (ABS(dy) == SCROLL_PAGE)
1398         step = num_page_entries - 1;
1399
1400       if (dy < 0 && ti->cl_first > 0)
1401       {
1402         /* scroll page/line up */
1403
1404         ti->cl_first -= step;
1405         if (ti->cl_first < 0)
1406           ti->cl_first = 0;
1407
1408         drawChooseTreeList(ti->cl_first, num_page_entries, ti);
1409         drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
1410         drawChooseTreeCursor(ti->cl_cursor, FC_RED);
1411         AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
1412                                   ti->cl_first, ti);
1413       }
1414       else if (dy > 0 && ti->cl_first + num_page_entries < num_entries)
1415       {
1416         /* scroll page/line down */
1417
1418         ti->cl_first += step;
1419         if (ti->cl_first + num_page_entries > num_entries)
1420           ti->cl_first = MAX(0, num_entries - num_page_entries);
1421
1422         drawChooseTreeList(ti->cl_first, num_page_entries, ti);
1423         drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
1424         drawChooseTreeCursor(ti->cl_cursor, FC_RED);
1425         AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
1426                                   ti->cl_first, ti);
1427       }
1428
1429       return;
1430     }
1431
1432     /* handle moving cursor one line */
1433     y = ti->cl_cursor + dy;
1434   }
1435
1436   if (dx == 1)
1437   {
1438     TreeInfo *node_first, *node_cursor;
1439     int entry_pos = ti->cl_first + y;
1440
1441     node_first = getTreeInfoFirstGroupEntry(ti);
1442     node_cursor = getTreeInfoFromPos(node_first, entry_pos);
1443
1444     if (node_cursor->node_group)
1445     {
1446       node_cursor->cl_first = ti->cl_first;
1447       node_cursor->cl_cursor = ti->cl_cursor;
1448       *ti_ptr = node_cursor->node_group;
1449       DrawChooseTree(ti_ptr);
1450
1451       return;
1452     }
1453   }
1454   else if (dx == -1 && ti->node_parent)
1455   {
1456     *ti_ptr = ti->node_parent;
1457     DrawChooseTree(ti_ptr);
1458
1459     return;
1460   }
1461
1462   if (x == 0 && y >= 0 && y < num_page_entries)
1463   {
1464     if (button)
1465     {
1466       if (y != ti->cl_cursor)
1467       {
1468         drawChooseTreeCursor(y, FC_RED);
1469         drawChooseTreeCursor(ti->cl_cursor, FC_BLUE);
1470         drawChooseTreeInfo(ti->cl_first + y, ti);
1471         ti->cl_cursor = y;
1472       }
1473     }
1474     else
1475     {
1476       TreeInfo *node_first, *node_cursor;
1477       int entry_pos = ti->cl_first + y;
1478
1479       node_first = getTreeInfoFirstGroupEntry(ti);
1480       node_cursor = getTreeInfoFromPos(node_first, entry_pos);
1481
1482       if (node_cursor->node_group)
1483       {
1484         node_cursor->cl_first = ti->cl_first;
1485         node_cursor->cl_cursor = ti->cl_cursor;
1486         *ti_ptr = node_cursor->node_group;
1487         DrawChooseTree(ti_ptr);
1488       }
1489       else if (node_cursor->parent_link)
1490       {
1491         *ti_ptr = node_cursor->node_parent;
1492         DrawChooseTree(ti_ptr);
1493       }
1494       else
1495       {
1496         node_cursor->cl_first = ti->cl_first;
1497         node_cursor->cl_cursor = ti->cl_cursor;
1498         *ti_ptr = node_cursor;
1499
1500         if (ti->type == TREE_TYPE_LEVEL_DIR)
1501         {
1502           LoadLevelSetup_SeriesInfo();
1503
1504           SaveLevelSetup_LastSeries();
1505           SaveLevelSetup_SeriesInfo();
1506           TapeErase();
1507         }
1508
1509         if (game_status == GAME_MODE_SETUP)
1510         {
1511           execSetupArtwork();
1512         }
1513         else
1514         {
1515           game_status = GAME_MODE_MAIN;
1516           DrawMainMenu();
1517         }
1518       }
1519     }
1520   }
1521
1522   BackToFront();
1523
1524   if (game_status == GAME_MODE_LEVELS || game_status == GAME_MODE_SETUP)
1525     DoAnimation();
1526 }
1527
1528 void DrawChooseLevel()
1529 {
1530   SetMainBackgroundImage(IMG_BACKGROUND_LEVELS);
1531
1532   DrawChooseTree(&leveldir_current);
1533 }
1534
1535 void HandleChooseLevel(int mx, int my, int dx, int dy, int button)
1536 {
1537   HandleChooseTree(mx, my, dx, dy, button, &leveldir_current);
1538 }
1539
1540 void DrawHallOfFame(int highlight_position)
1541 {
1542   UnmapAllGadgets();
1543   FadeSounds();
1544   CloseDoor(DOOR_CLOSE_2);
1545
1546   if (highlight_position < 0) 
1547     LoadScore(level_nr);
1548
1549   FadeToFront();
1550   InitAnimation();
1551
1552   HandleHallOfFame(highlight_position,0, 0,0, MB_MENU_INITIALIZE);
1553
1554 #if 0
1555   PlaySound(SND_BACKGROUND_SCORES);
1556 #else
1557   PlaySound_Menu_Start(SND_BACKGROUND_SCORES);
1558 #endif
1559 }
1560
1561 static void drawHallOfFameList(int first_entry, int highlight_position)
1562 {
1563   int i;
1564
1565   SetMainBackgroundImage(IMG_BACKGROUND_SCORES);
1566   ClearWindow();
1567
1568   DrawText(mSX + 80, mSY + 8, "Hall Of Fame", FONT_TITLE_1);
1569   DrawTextFCentered(46, FONT_TITLE_2, "HighScores of Level %d", level_nr);
1570
1571   for(i=0; i<NUM_MENU_ENTRIES_ON_SCREEN; i++)
1572   {
1573     int entry = first_entry + i;
1574     boolean active = (entry == highlight_position);
1575     int font_nr1 = (active ? FONT_TEXT_1_ACTIVE : FONT_TEXT_1);
1576     int font_nr2 = (active ? FONT_TEXT_2_ACTIVE : FONT_TEXT_2);
1577     int font_nr3 = (active ? FONT_TEXT_3_ACTIVE : FONT_TEXT_3);
1578     int font_nr4 = (active ? FONT_TEXT_4_ACTIVE : FONT_TEXT_4);
1579     int dx1 = 3 * getFontWidth(font_nr1);
1580     int dx2 = dx1 + getFontWidth(font_nr1);
1581     int dx3 = dx2 + 25 * getFontWidth(font_nr3);
1582     int sy = mSY + 64 + i * 32;
1583
1584     DrawText(mSX, sy, int2str(entry + 1, 3), font_nr1);
1585     DrawText(mSX + dx1, sy, ".", font_nr1);
1586     DrawText(mSX + dx2, sy, ".........................", font_nr3);
1587     if (strcmp(highscore[entry].Name, EMPTY_PLAYER_NAME) != 0)
1588       DrawText(mSX + dx2, sy, highscore[entry].Name, font_nr2);
1589     DrawText(mSX + dx3, sy, int2str(highscore[entry].Score, 5), font_nr4);
1590   }
1591 }
1592
1593 void HandleHallOfFame(int mx, int my, int dx, int dy, int button)
1594 {
1595   static int first_entry = 0;
1596   static int highlight_position = 0;
1597   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
1598   int button_released = !button;
1599
1600   if (button == MB_MENU_INITIALIZE)
1601   {
1602     first_entry = 0;
1603     highlight_position = mx;
1604     drawHallOfFameList(first_entry, highlight_position);
1605
1606     return;
1607   }
1608
1609   if (ABS(dy) == SCROLL_PAGE)           /* handle scrolling one page */
1610     step = NUM_MENU_ENTRIES_ON_SCREEN - 1;
1611
1612   if (dy < 0)
1613   {
1614     if (first_entry > 0)
1615     {
1616       first_entry -= step;
1617       if (first_entry < 0)
1618         first_entry = 0;
1619
1620       drawHallOfFameList(first_entry, highlight_position);
1621
1622       return;
1623     }
1624   }
1625   else if (dy > 0)
1626   {
1627     if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN < MAX_SCORE_ENTRIES)
1628     {
1629       first_entry += step;
1630       if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN > MAX_SCORE_ENTRIES)
1631         first_entry = MAX(0, MAX_SCORE_ENTRIES - NUM_MENU_ENTRIES_ON_SCREEN);
1632
1633       drawHallOfFameList(first_entry, highlight_position);
1634
1635       return;
1636     }
1637   }
1638
1639   if (button_released)
1640   {
1641     FadeSound(SND_BACKGROUND_SCORES);
1642     game_status = GAME_MODE_MAIN;
1643     DrawMainMenu();
1644   }
1645
1646   BackToFront();
1647
1648   if (game_status == GAME_MODE_SCORES)
1649   {
1650     DoAnimation();
1651 #if 1
1652     PlaySound_Menu_Continue(SND_BACKGROUND_SCORES);
1653 #endif
1654   }
1655 }
1656
1657
1658 /* ========================================================================= */
1659 /* setup screen functions                                                    */
1660 /* ========================================================================= */
1661
1662 static struct TokenInfo *setup_info;
1663 static int num_setup_info;
1664
1665 static char *graphics_set_name;
1666 static char *sounds_set_name;
1667 static char *music_set_name;
1668
1669 static void execSetupMain()
1670 {
1671   setup_mode = SETUP_MODE_MAIN;
1672   DrawSetupScreen();
1673 }
1674
1675 static void execSetupGame()
1676 {
1677   setup_mode = SETUP_MODE_GAME;
1678   DrawSetupScreen();
1679 }
1680
1681 static void execSetupEditor()
1682 {
1683   setup_mode = SETUP_MODE_EDITOR;
1684   DrawSetupScreen();
1685 }
1686
1687 static void execSetupGraphics()
1688 {
1689   setup_mode = SETUP_MODE_GRAPHICS;
1690   DrawSetupScreen();
1691 }
1692
1693 static void execSetupSound()
1694 {
1695   setup_mode = SETUP_MODE_SOUND;
1696   DrawSetupScreen();
1697 }
1698
1699 static void execSetupArtwork()
1700 {
1701   /* needed if last screen (setup choice) changed graphics, sounds or music */
1702   ReloadCustomArtwork();
1703
1704   setup.graphics_set = artwork.gfx_current->identifier;
1705   setup.sounds_set = artwork.snd_current->identifier;
1706   setup.music_set = artwork.mus_current->identifier;
1707
1708   /* needed for displaying artwork name instead of artwork identifier */
1709   graphics_set_name = artwork.gfx_current->name;
1710   sounds_set_name = artwork.snd_current->name;
1711   music_set_name = artwork.mus_current->name;
1712
1713   setup_mode = SETUP_MODE_ARTWORK;
1714   DrawSetupScreen();
1715 }
1716
1717 static void execSetupChooseGraphics()
1718 {
1719   setup_mode = SETUP_MODE_CHOOSE_GRAPHICS;
1720   DrawSetupScreen();
1721 }
1722
1723 static void execSetupChooseSounds()
1724 {
1725   setup_mode = SETUP_MODE_CHOOSE_SOUNDS;
1726   DrawSetupScreen();
1727 }
1728
1729 static void execSetupChooseMusic()
1730 {
1731   setup_mode = SETUP_MODE_CHOOSE_MUSIC;
1732   DrawSetupScreen();
1733 }
1734
1735 static void execSetupInput()
1736 {
1737   setup_mode = SETUP_MODE_INPUT;
1738   DrawSetupScreen();
1739 }
1740
1741 static void execSetupShortcut()
1742 {
1743   setup_mode = SETUP_MODE_SHORTCUT;
1744   DrawSetupScreen();
1745 }
1746
1747 static void execExitSetup()
1748 {
1749   game_status = GAME_MODE_MAIN;
1750   DrawMainMenu();
1751 }
1752
1753 static void execSaveAndExitSetup()
1754 {
1755   SaveSetup();
1756   execExitSetup();
1757 }
1758
1759 static struct TokenInfo setup_info_main[] =
1760 {
1761   { TYPE_ENTER_MENU,    execSetupGame,          "Game Settings"         },
1762   { TYPE_ENTER_MENU,    execSetupEditor,        "Editor Settings"       },
1763   { TYPE_ENTER_MENU,    execSetupGraphics,      "Graphics"              },
1764   { TYPE_ENTER_MENU,    execSetupSound,         "Sound & Music"         },
1765   { TYPE_ENTER_MENU,    execSetupArtwork,       "Custom Artwork"        },
1766   { TYPE_ENTER_MENU,    execSetupInput,         "Input Devices"         },
1767   { TYPE_ENTER_MENU,    execSetupShortcut,      "Key Shortcuts"         },
1768   { TYPE_EMPTY,         NULL,                   ""                      },
1769   { TYPE_LEAVE_MENU,    execExitSetup,          "Exit"                  },
1770   { TYPE_LEAVE_MENU,    execSaveAndExitSetup,   "Save and Exit"         },
1771   { 0,                  NULL,                   NULL                    }
1772 };
1773
1774 static struct TokenInfo setup_info_game[] =
1775 {
1776   { TYPE_SWITCH,        &setup.team_mode,       "Team-Mode:"            },
1777   { TYPE_SWITCH,        &setup.handicap,        "Handicap:"             },
1778   { TYPE_SWITCH,        &setup.time_limit,      "Timelimit:"            },
1779   { TYPE_SWITCH,        &setup.autorecord,      "Auto-Record:"          },
1780   { TYPE_EMPTY,         NULL,                   ""                      },
1781   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
1782   { 0,                  NULL,                   NULL                    }
1783 };
1784
1785 static struct TokenInfo setup_info_editor[] =
1786 {
1787   { TYPE_STRING,        NULL,                   "Offer Special Elements:"},
1788   { TYPE_SWITCH,        &setup.editor.el_boulderdash,   "BoulderDash:"  },
1789   { TYPE_SWITCH,        &setup.editor.el_emerald_mine,  "Emerald Mine:" },
1790   { TYPE_SWITCH,        &setup.editor.el_more,          "More:"         },
1791   { TYPE_SWITCH,        &setup.editor.el_sokoban,       "Sokoban:"      },
1792   { TYPE_SWITCH,        &setup.editor.el_supaplex,      "Supaplex:"     },
1793   { TYPE_SWITCH,        &setup.editor.el_diamond_caves, "Diamd. Caves:" },
1794   { TYPE_SWITCH,        &setup.editor.el_dx_boulderdash,"DX Boulderd.:" },
1795   { TYPE_SWITCH,        &setup.editor.el_chars,         "Characters:"   },
1796   { TYPE_SWITCH,        &setup.editor.el_custom,        "Custom:"       },
1797   { TYPE_EMPTY,         NULL,                   ""                      },
1798   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
1799   { 0,                  NULL,                   NULL                    }
1800 };
1801
1802 static struct TokenInfo setup_info_graphics[] =
1803 {
1804   { TYPE_SWITCH,        &setup.fullscreen,      "Fullscreen:"           },
1805   { TYPE_SWITCH,        &setup.scroll_delay,    "Scroll Delay:"         },
1806   { TYPE_SWITCH,        &setup.soft_scrolling,  "Soft Scroll.:"         },
1807 #if 0
1808   { TYPE_SWITCH,        &setup.double_buffering,"Buffered gfx:"         },
1809   { TYPE_SWITCH,        &setup.fading,          "Fading:"               },
1810 #endif
1811   { TYPE_SWITCH,        &setup.quick_doors,     "Quick Doors:"          },
1812   { TYPE_SWITCH,        &setup.toons,           "Toons:"                },
1813   { TYPE_EMPTY,         NULL,                   ""                      },
1814   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
1815   { 0,                  NULL,                   NULL                    }
1816 };
1817
1818 static struct TokenInfo setup_info_sound[] =
1819 {
1820   { TYPE_SWITCH,        &setup.sound,           "Sound:",               },
1821   { TYPE_EMPTY,         NULL,                   ""                      },
1822   { TYPE_SWITCH,        &setup.sound_simple,    "Simple Sound:"         },
1823   { TYPE_SWITCH,        &setup.sound_loops,     "Sound Loops:"          },
1824   { TYPE_SWITCH,        &setup.sound_music,     "Game Music:"           },
1825   { TYPE_EMPTY,         NULL,                   ""                      },
1826   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
1827   { 0,                  NULL,                   NULL                    }
1828 };
1829
1830 static struct TokenInfo setup_info_artwork[] =
1831 {
1832   { TYPE_ENTER_MENU,    execSetupChooseGraphics,"Custom Graphics"       },
1833   { TYPE_STRING,        &graphics_set_name,     ""                      },
1834   { TYPE_ENTER_MENU,    execSetupChooseSounds,  "Custom Sounds"         },
1835   { TYPE_STRING,        &sounds_set_name,       ""                      },
1836   { TYPE_ENTER_MENU,    execSetupChooseMusic,   "Custom Music"          },
1837   { TYPE_STRING,        &music_set_name,        ""                      },
1838   { TYPE_EMPTY,         NULL,                   ""                      },
1839   { TYPE_STRING,        NULL,                   "Override Level Artwork:"},
1840   { TYPE_YES_NO,        &setup.override_level_graphics, "Graphics:"     },
1841   { TYPE_YES_NO,        &setup.override_level_sounds,   "Sounds:"       },
1842   { TYPE_YES_NO,        &setup.override_level_music,    "Music:"        },
1843   { TYPE_EMPTY,         NULL,                   ""                      },
1844   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
1845   { 0,                  NULL,                   NULL                    }
1846 };
1847
1848 static struct TokenInfo setup_info_shortcut[] =
1849 {
1850   { TYPE_KEYTEXT,       NULL,                   "Quick Save Game:",     },
1851   { TYPE_KEY,           &setup.shortcut.save_game,      ""              },
1852   { TYPE_KEYTEXT,       NULL,                   "Quick Load Game:",     },
1853   { TYPE_KEY,           &setup.shortcut.load_game,      ""              },
1854   { TYPE_KEYTEXT,       NULL,                   "Toggle Pause:",        },
1855   { TYPE_KEY,           &setup.shortcut.toggle_pause,   ""              },
1856   { TYPE_EMPTY,         NULL,                   ""                      },
1857   { TYPE_YES_NO,        &setup.ask_on_escape,   "Ask on Esc:"           },
1858   { TYPE_EMPTY,         NULL,                   ""                      },
1859   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
1860   { 0,                  NULL,                   NULL                    }
1861 };
1862
1863 static Key getSetupKey()
1864 {
1865   Key key = KSYM_UNDEFINED;
1866   boolean got_key_event = FALSE;
1867
1868   while (!got_key_event)
1869   {
1870     if (PendingEvent())         /* got event */
1871     {
1872       Event event;
1873
1874       NextEvent(&event);
1875
1876       switch(event.type)
1877       {
1878         case EVENT_KEYPRESS:
1879           {
1880             key = GetEventKey((KeyEvent *)&event, TRUE);
1881
1882             /* press 'Escape' or 'Enter' to keep the existing key binding */
1883             if (key == KSYM_Escape || key == KSYM_Return)
1884               key = KSYM_UNDEFINED;     /* keep old value */
1885
1886             got_key_event = TRUE;
1887           }
1888           break;
1889
1890         case EVENT_KEYRELEASE:
1891           key_joystick_mapping = 0;
1892           break;
1893
1894         default:
1895           HandleOtherEvents(&event);
1896           break;
1897       }
1898     }
1899
1900     BackToFront();
1901     DoAnimation();
1902
1903     /* don't eat all CPU time */
1904     Delay(10);
1905   }
1906
1907   return key;
1908 }
1909
1910 static void drawSetupValue(int pos)
1911 {
1912   int xpos = MENU_SCREEN_VALUE_XPOS;
1913   int ypos = MENU_SCREEN_START_YPOS + pos;
1914   int font_nr = FONT_VALUE_1;
1915   char *value_string = getSetupValue(setup_info[pos].type & ~TYPE_GHOSTED,
1916                                      setup_info[pos].value);
1917
1918   if (value_string == NULL)
1919     return;
1920
1921   if (setup_info[pos].type & TYPE_KEY)
1922   {
1923     xpos = 3;
1924
1925     if (setup_info[pos].type & TYPE_QUERY)
1926     {
1927       value_string = "<press key>";
1928       font_nr = FONT_INPUT_1_ACTIVE;
1929     }
1930   }
1931   else if (setup_info[pos].type & TYPE_STRING)
1932   {
1933     int max_value_len = (SCR_FIELDX - 2) * 2;
1934
1935     xpos = 1;
1936     font_nr = FONT_VALUE_2;
1937
1938     if (strlen(value_string) > max_value_len)
1939       value_string[max_value_len] = '\0';
1940   }
1941   else if (setup_info[pos].type & TYPE_BOOLEAN_STYLE)
1942   {
1943     font_nr = (*(boolean *)(setup_info[pos].value) ? FONT_OPTION_ON :
1944                FONT_OPTION_OFF);
1945   }
1946
1947   DrawText(mSX + xpos * 32, mSY + ypos * 32,
1948            (xpos == 3 ? "              " : "   "), font_nr);
1949   DrawText(mSX + xpos * 32, mSY + ypos * 32, value_string, font_nr);
1950 }
1951
1952 static void changeSetupValue(int pos)
1953 {
1954   if (setup_info[pos].type & TYPE_BOOLEAN_STYLE)
1955   {
1956     *(boolean *)setup_info[pos].value ^= TRUE;
1957   }
1958   else if (setup_info[pos].type & TYPE_KEY)
1959   {
1960     Key key;
1961
1962     setup_info[pos].type |= TYPE_QUERY;
1963     drawSetupValue(pos);
1964     setup_info[pos].type &= ~TYPE_QUERY;
1965
1966     key = getSetupKey();
1967     if (key != KSYM_UNDEFINED)
1968       *(Key *)setup_info[pos].value = key;
1969   }
1970
1971   drawSetupValue(pos);
1972 }
1973
1974 static void DrawSetupScreen_Generic()
1975 {
1976   char *title_string = NULL;
1977   int i;
1978
1979   UnmapAllGadgets();
1980   CloseDoor(DOOR_CLOSE_2);
1981
1982   ClearWindow();
1983
1984   if (setup_mode == SETUP_MODE_MAIN)
1985   {
1986     setup_info = setup_info_main;
1987     title_string = "Setup";
1988   }
1989   else if (setup_mode == SETUP_MODE_GAME)
1990   {
1991     setup_info = setup_info_game;
1992     title_string = "Setup Game";
1993   }
1994   else if (setup_mode == SETUP_MODE_EDITOR)
1995   {
1996     setup_info = setup_info_editor;
1997     title_string = "Setup Editor";
1998   }
1999   else if (setup_mode == SETUP_MODE_GRAPHICS)
2000   {
2001     setup_info = setup_info_graphics;
2002     title_string = "Setup Graphics";
2003   }
2004   else if (setup_mode == SETUP_MODE_SOUND)
2005   {
2006     setup_info = setup_info_sound;
2007     title_string = "Setup Sound";
2008   }
2009   else if (setup_mode == SETUP_MODE_ARTWORK)
2010   {
2011     setup_info = setup_info_artwork;
2012     title_string = "Custom Artwork";
2013   }
2014   else if (setup_mode == SETUP_MODE_SHORTCUT)
2015   {
2016     setup_info = setup_info_shortcut;
2017     title_string = "Setup Shortcuts";
2018   }
2019
2020   DrawText(mSX + 16, mSY + 16, title_string, FONT_TITLE_1);
2021
2022   num_setup_info = 0;
2023   for(i=0; setup_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
2024   {
2025     void *value_ptr = setup_info[i].value;
2026     int ypos = MENU_SCREEN_START_YPOS + i;
2027     int font_nr = FONT_MENU_1;
2028
2029     /* set some entries to "unchangeable" according to other variables */
2030     if ((value_ptr == &setup.sound       && !audio.sound_available) ||
2031         (value_ptr == &setup.sound_loops && !audio.loops_available) ||
2032         (value_ptr == &setup.sound_music && !audio.music_available) ||
2033         (value_ptr == &setup.fullscreen  && !video.fullscreen_available))
2034       setup_info[i].type |= TYPE_GHOSTED;
2035
2036     if (setup_info[i].type & TYPE_STRING)
2037       font_nr = FONT_MENU_2;
2038
2039     DrawText(mSX + 32, mSY + ypos * 32, setup_info[i].text, font_nr);
2040
2041     if (setup_info[i].type & TYPE_ENTER_MENU)
2042       initCursor(i, IMG_MENU_BUTTON_RIGHT);
2043     else if (setup_info[i].type & TYPE_LEAVE_MENU)
2044       initCursor(i, IMG_MENU_BUTTON_LEFT);
2045     else if (setup_info[i].type & ~TYPE_SKIP_ENTRY)
2046       initCursor(i, IMG_MENU_BUTTON);
2047
2048     if (setup_info[i].type & TYPE_VALUE)
2049       drawSetupValue(i);
2050
2051     num_setup_info++;
2052   }
2053
2054   FadeToFront();
2055   InitAnimation();
2056   HandleSetupScreen_Generic(0,0,0,0,MB_MENU_INITIALIZE);
2057 }
2058
2059 void HandleSetupScreen_Generic(int mx, int my, int dx, int dy, int button)
2060 {
2061   static int choice_store[MAX_SETUP_MODES];
2062   int choice = choice_store[setup_mode];        /* always starts with 0 */
2063   int x = 0;
2064   int y = choice;
2065
2066   if (button == MB_MENU_INITIALIZE)
2067   {
2068     /* advance to first valid menu entry */
2069     while (choice < num_setup_info &&
2070            (setup_info[choice].type & TYPE_SKIP_ENTRY))
2071       choice++;
2072     choice_store[setup_mode] = choice;
2073
2074     drawCursor(choice, FC_RED);
2075     return;
2076   }
2077   else if (button == MB_MENU_LEAVE)
2078   {
2079     for (y=0; y<num_setup_info; y++)
2080     {
2081       if (setup_info[y].type & TYPE_LEAVE_MENU)
2082       {
2083         void (*menu_callback_function)(void) = setup_info[y].value;
2084
2085         menu_callback_function();
2086         break;  /* absolutely needed because function changes 'setup_info'! */
2087       }
2088     }
2089
2090     return;
2091   }
2092
2093   if (mx || my)         /* mouse input */
2094   {
2095     x = (mx - mSX) / 32;
2096     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
2097   }
2098   else if (dx || dy)    /* keyboard input */
2099   {
2100     if (dx)
2101     {
2102       int menu_navigation_type = (dx < 0 ? TYPE_LEAVE_MENU : TYPE_ENTER_MENU);
2103
2104       if ((setup_info[choice].type & menu_navigation_type) ||
2105           (setup_info[choice].type & TYPE_BOOLEAN_STYLE))
2106         button = MB_MENU_CHOICE;
2107     }
2108     else if (dy)
2109       y = choice + dy;
2110
2111     /* jump to next non-empty menu entry (up or down) */
2112     while (y > 0 && y < num_setup_info - 1 &&
2113            (setup_info[y].type & TYPE_SKIP_ENTRY))
2114       y += dy;
2115   }
2116
2117   if (x == 0 && y >= 0 && y < num_setup_info &&
2118       (setup_info[y].type & ~TYPE_SKIP_ENTRY))
2119   {
2120     if (button)
2121     {
2122       if (y != choice)
2123       {
2124         drawCursor(y, FC_RED);
2125         drawCursor(choice, FC_BLUE);
2126         choice = choice_store[setup_mode] = y;
2127       }
2128     }
2129     else if (!(setup_info[y].type & TYPE_GHOSTED))
2130     {
2131       if (setup_info[y].type & TYPE_ENTER_OR_LEAVE_MENU)
2132       {
2133         void (*menu_callback_function)(void) = setup_info[choice].value;
2134
2135         menu_callback_function();
2136       }
2137       else
2138       {
2139         if ((setup_info[y].type & TYPE_KEYTEXT) &&
2140             (setup_info[y + 1].type & TYPE_KEY))
2141           y++;
2142
2143         if (setup_info[y].type & TYPE_VALUE)
2144           changeSetupValue(y);
2145       }
2146     }
2147   }
2148
2149   BackToFront();
2150
2151   if (game_status == GAME_MODE_SETUP)
2152     DoAnimation();
2153 }
2154
2155 void DrawSetupScreen_Input()
2156 {
2157   ClearWindow();
2158
2159   DrawText(mSX+16, mSY+16, "Setup Input", FONT_TITLE_1);
2160
2161   initCursor(0, IMG_MENU_BUTTON);
2162   initCursor(1, IMG_MENU_BUTTON);
2163   initCursor(2, IMG_MENU_BUTTON_RIGHT);
2164   initCursor(13, IMG_MENU_BUTTON_LEFT);
2165
2166   drawCursorXY(10, 0, IMG_MENU_BUTTON_LEFT);
2167   drawCursorXY(12, 0, IMG_MENU_BUTTON_RIGHT);
2168
2169   DrawText(mSX+32, mSY+2*32, "Player:", FONT_MENU_1);
2170   DrawText(mSX+32, mSY+3*32, "Device:", FONT_MENU_1);
2171   DrawText(mSX+32, mSY+15*32, "Back",   FONT_MENU_1);
2172
2173 #if 0
2174   DeactivateJoystickForCalibration();
2175   DrawTextFCentered(SYSIZE - 20, FONT_TEXT_4,
2176                     "Joysticks deactivated on this screen");
2177 #endif
2178
2179   HandleSetupScreen_Input(0,0, 0,0, MB_MENU_INITIALIZE);
2180   FadeToFront();
2181   InitAnimation();
2182 }
2183
2184 static void setJoystickDeviceToNr(char *device_name, int device_nr)
2185 {
2186   if (device_name == NULL)
2187     return;
2188
2189   if (device_nr < 0 || device_nr >= MAX_PLAYERS)
2190     device_nr = 0;
2191
2192   if (strlen(device_name) > 1)
2193   {
2194     char c1 = device_name[strlen(device_name) - 1];
2195     char c2 = device_name[strlen(device_name) - 2];
2196
2197     if (c1 >= '0' && c1 <= '9' && !(c2 >= '0' && c2 <= '9'))
2198       device_name[strlen(device_name) - 1] = '0' + (char)(device_nr % 10);
2199   }
2200   else
2201     strncpy(device_name, getDeviceNameFromJoystickNr(device_nr),
2202             strlen(device_name));
2203 }
2204
2205 static void drawPlayerSetupInputInfo(int player_nr)
2206 {
2207   int i;
2208   static struct SetupKeyboardInfo custom_key;
2209   static struct
2210   {
2211     Key *key;
2212     char *text;
2213   } custom[] =
2214   {
2215     { &custom_key.left,  "Joystick Left"  },
2216     { &custom_key.right, "Joystick Right" },
2217     { &custom_key.up,    "Joystick Up"    },
2218     { &custom_key.down,  "Joystick Down"  },
2219     { &custom_key.snap,  "Button 1"       },
2220     { &custom_key.bomb,  "Button 2"       }
2221   };
2222   static char *joystick_name[MAX_PLAYERS] =
2223   {
2224     "Joystick1",
2225     "Joystick2",
2226     "Joystick3",
2227     "Joystick4"
2228   };
2229
2230   custom_key = setup.input[player_nr].key;
2231
2232   DrawText(mSX+11*32, mSY+2*32, int2str(player_nr +1, 1), FONT_INPUT_1_ACTIVE);
2233 #if 1
2234   DrawGraphicThruMaskExt(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
2235                          PLAYER_NR_GFX(IMG_PLAYER_1, player_nr), 0);
2236 #else
2237   DrawGraphicThruMask(8, 2, PLAYER_NR_GFX(IMG_PLAYER_1, player_nr), 0);
2238 #endif
2239
2240   if (setup.input[player_nr].use_joystick)
2241   {
2242     char *device_name = setup.input[player_nr].joy.device_name;
2243
2244     DrawText(mSX+8*32, mSY+3*32,
2245              joystick_name[getJoystickNrFromDeviceName(device_name)],
2246              FONT_VALUE_1);
2247     DrawText(mSX+32, mSY+4*32, "Calibrate", FONT_MENU_1);
2248   }
2249   else
2250   {
2251     DrawText(mSX+8*32, mSY+3*32, "Keyboard ", FONT_VALUE_1);
2252     DrawText(mSX+32,   mSY+4*32, "Customize", FONT_MENU_1);
2253   }
2254
2255   DrawText(mSX+32, mSY+5*32, "Actual Settings:", FONT_MENU_1);
2256   drawCursorXY(1, 4, IMG_MENU_BUTTON_LEFT);
2257   drawCursorXY(1, 5, IMG_MENU_BUTTON_RIGHT);
2258   drawCursorXY(1, 6, IMG_MENU_BUTTON_UP);
2259   drawCursorXY(1, 7, IMG_MENU_BUTTON_DOWN);
2260   DrawText(mSX+2*32, mSY+6*32, ":", FONT_VALUE_OLD);
2261   DrawText(mSX+2*32, mSY+7*32, ":", FONT_VALUE_OLD);
2262   DrawText(mSX+2*32, mSY+8*32, ":", FONT_VALUE_OLD);
2263   DrawText(mSX+2*32, mSY+9*32, ":", FONT_VALUE_OLD);
2264   DrawText(mSX+32, mSY+10*32, "Snap Field:", FONT_VALUE_OLD);
2265   DrawText(mSX+32, mSY+12*32, "Place Bomb:", FONT_VALUE_OLD);
2266
2267   for (i=0; i<6; i++)
2268   {
2269     int ypos = 6 + i + (i > 3 ? i-3 : 0);
2270
2271     DrawText(mSX + 3*32, mSY + ypos*32,
2272              "              ", FONT_VALUE_1);
2273     DrawText(mSX + 3*32, mSY + ypos*32,
2274              (setup.input[player_nr].use_joystick ?
2275               custom[i].text :
2276               getKeyNameFromKey(*custom[i].key)), FONT_VALUE_1);
2277   }
2278 }
2279
2280 void HandleSetupScreen_Input(int mx, int my, int dx, int dy, int button)
2281 {
2282   static int choice = 0;
2283   static int player_nr = 0;
2284   int x = 0;
2285   int y = choice;
2286   int pos_start  = SETUPINPUT_SCREEN_POS_START;
2287   int pos_empty1 = SETUPINPUT_SCREEN_POS_EMPTY1;
2288   int pos_empty2 = SETUPINPUT_SCREEN_POS_EMPTY2;
2289   int pos_end    = SETUPINPUT_SCREEN_POS_END;
2290
2291   if (button == MB_MENU_INITIALIZE)
2292   {
2293     drawPlayerSetupInputInfo(player_nr);
2294     drawCursor(choice, FC_RED);
2295     return;
2296   }
2297   else if (button == MB_MENU_LEAVE)
2298   {
2299     setup_mode = SETUP_MODE_MAIN;
2300     DrawSetupScreen();
2301     InitJoysticks();
2302   }
2303
2304   if (mx || my)         /* mouse input */
2305   {
2306     x = (mx - mSX) / 32;
2307     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
2308   }
2309   else if (dx || dy)    /* keyboard input */
2310   {
2311     if (dx && choice == 0)
2312       x = (dx < 0 ? 10 : 12);
2313     else if ((dx && choice == 1) ||
2314              (dx == +1 && choice == 2) ||
2315              (dx == -1 && choice == pos_end))
2316       button = MB_MENU_CHOICE;
2317     else if (dy)
2318       y = choice + dy;
2319
2320     if (y >= pos_empty1 && y <= pos_empty2)
2321       y = (dy > 0 ? pos_empty2 + 1 : pos_empty1 - 1);
2322   }
2323
2324   if (y == 0 && ((x == 0 && !button) || ((x == 10 || x == 12) && button)))
2325   {
2326     static unsigned long delay = 0;
2327
2328     if (!DelayReached(&delay, GADGET_FRAME_DELAY))
2329       goto out;
2330
2331     player_nr = (player_nr + (x == 10 ? -1 : +1) + MAX_PLAYERS) % MAX_PLAYERS;
2332
2333     drawPlayerSetupInputInfo(player_nr);
2334   }
2335   else if (x == 0 && y >= pos_start && y <= pos_end &&
2336            !(y >= pos_empty1 && y <= pos_empty2))
2337   {
2338     if (button)
2339     {
2340       if (y != choice)
2341       {
2342         drawCursor(y, FC_RED);
2343         drawCursor(choice, FC_BLUE);
2344         choice = y;
2345       }
2346     }
2347     else
2348     {
2349       if (y == 1)
2350       {
2351         char *device_name = setup.input[player_nr].joy.device_name;
2352
2353         if (!setup.input[player_nr].use_joystick)
2354         {
2355           int new_device_nr = (dx >= 0 ? 0 : MAX_PLAYERS - 1);
2356
2357           setJoystickDeviceToNr(device_name, new_device_nr);
2358           setup.input[player_nr].use_joystick = TRUE;
2359         }
2360         else
2361         {
2362           int device_nr = getJoystickNrFromDeviceName(device_name);
2363           int new_device_nr = device_nr + (dx >= 0 ? +1 : -1);
2364
2365           if (new_device_nr < 0 || new_device_nr >= MAX_PLAYERS)
2366             setup.input[player_nr].use_joystick = FALSE;
2367           else
2368             setJoystickDeviceToNr(device_name, new_device_nr);
2369         }
2370
2371         drawPlayerSetupInputInfo(player_nr);
2372       }
2373       else if (y == 2)
2374       {
2375         if (setup.input[player_nr].use_joystick)
2376         {
2377           InitJoysticks();
2378           CalibrateJoystick(player_nr);
2379         }
2380         else
2381           CustomizeKeyboard(player_nr);
2382       }
2383       else if (y == pos_end)
2384       {
2385         InitJoysticks();
2386
2387         setup_mode = SETUP_MODE_MAIN;
2388         DrawSetupScreen();
2389       }
2390     }
2391   }
2392
2393   BackToFront();
2394
2395   out:
2396
2397   if (game_status == GAME_MODE_SETUP)
2398     DoAnimation();
2399 }
2400
2401 void CustomizeKeyboard(int player_nr)
2402 {
2403   int i;
2404   int step_nr;
2405   boolean finished = FALSE;
2406   static struct SetupKeyboardInfo custom_key;
2407   static struct
2408   {
2409     Key *key;
2410     char *text;
2411   } customize_step[] =
2412   {
2413     { &custom_key.left,  "Move Left"  },
2414     { &custom_key.right, "Move Right" },
2415     { &custom_key.up,    "Move Up"    },
2416     { &custom_key.down,  "Move Down"  },
2417     { &custom_key.snap,  "Snap Field" },
2418     { &custom_key.bomb,  "Place Bomb" }
2419   };
2420
2421   /* read existing key bindings from player setup */
2422   custom_key = setup.input[player_nr].key;
2423
2424   ClearWindow();
2425   DrawText(mSX + 16, mSY + 16, "Keyboard Input", FONT_TITLE_1);
2426
2427   BackToFront();
2428   InitAnimation();
2429
2430   step_nr = 0;
2431   DrawText(mSX, mSY + (2+2*step_nr)*32,
2432            customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
2433   DrawText(mSX, mSY + (2+2*step_nr+1)*32,
2434            "Key:", FONT_INPUT_1_ACTIVE);
2435   DrawText(mSX + 4*32, mSY + (2+2*step_nr+1)*32,
2436            getKeyNameFromKey(*customize_step[step_nr].key), FONT_VALUE_OLD);
2437
2438   while(!finished)
2439   {
2440     if (PendingEvent())         /* got event */
2441     {
2442       Event event;
2443
2444       NextEvent(&event);
2445
2446       switch(event.type)
2447       {
2448         case EVENT_KEYPRESS:
2449           {
2450             Key key = GetEventKey((KeyEvent *)&event, FALSE);
2451
2452             if (key == KSYM_Escape || (key == KSYM_Return && step_nr == 6))
2453             {
2454               finished = TRUE;
2455               break;
2456             }
2457
2458             /* all keys configured -- wait for "Escape" or "Return" key */
2459             if (step_nr == 6)
2460               break;
2461
2462             /* press 'Enter' to keep the existing key binding */
2463             if (key == KSYM_Return)
2464               key = *customize_step[step_nr].key;
2465
2466             /* check if key already used */
2467             for (i=0; i<step_nr; i++)
2468               if (*customize_step[i].key == key)
2469                 break;
2470             if (i < step_nr)
2471               break;
2472
2473             /* got new key binding */
2474             *customize_step[step_nr].key = key;
2475             DrawText(mSX + 4*32, mSY + (2+2*step_nr+1)*32,
2476                      "             ", FONT_VALUE_1);
2477             DrawText(mSX + 4*32, mSY + (2+2*step_nr+1)*32,
2478                      getKeyNameFromKey(key), FONT_VALUE_1);
2479             step_nr++;
2480
2481             /* un-highlight last query */
2482             DrawText(mSX, mSY+(2+2*(step_nr-1))*32,
2483                      customize_step[step_nr-1].text, FONT_MENU_1);
2484             DrawText(mSX, mSY+(2+2*(step_nr-1)+1)*32,
2485                      "Key:", FONT_MENU_1);
2486
2487             /* press 'Enter' to leave */
2488             if (step_nr == 6)
2489             {
2490               DrawText(mSX + 16, mSY + 15*32+16,
2491                        "Press Enter", FONT_TITLE_1);
2492               break;
2493             }
2494
2495             /* query next key binding */
2496             DrawText(mSX, mSY+(2+2*step_nr)*32,
2497                      customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
2498             DrawText(mSX, mSY+(2+2*step_nr+1)*32,
2499                      "Key:", FONT_INPUT_1_ACTIVE);
2500             DrawText(mSX + 4*32, mSY+(2+2*step_nr+1)*32,
2501                      getKeyNameFromKey(*customize_step[step_nr].key),
2502                      FONT_VALUE_OLD);
2503           }
2504           break;
2505
2506         case EVENT_KEYRELEASE:
2507           key_joystick_mapping = 0;
2508           break;
2509
2510         default:
2511           HandleOtherEvents(&event);
2512           break;
2513       }
2514     }
2515
2516     BackToFront();
2517     DoAnimation();
2518
2519     /* don't eat all CPU time */
2520     Delay(10);
2521   }
2522
2523   /* write new key bindings back to player setup */
2524   setup.input[player_nr].key = custom_key;
2525
2526   StopAnimation();
2527   DrawSetupScreen_Input();
2528 }
2529
2530 static boolean CalibrateJoystickMain(int player_nr)
2531 {
2532   int new_joystick_xleft = JOYSTICK_XMIDDLE;
2533   int new_joystick_xright = JOYSTICK_XMIDDLE;
2534   int new_joystick_yupper = JOYSTICK_YMIDDLE;
2535   int new_joystick_ylower = JOYSTICK_YMIDDLE;
2536   int new_joystick_xmiddle, new_joystick_ymiddle;
2537
2538   int joystick_fd = joystick.fd[player_nr];
2539   int x, y, last_x, last_y, xpos = 8, ypos = 3;
2540   boolean check[3][3];
2541   int check_remaining = 3 * 3;
2542   int joy_x, joy_y;
2543   int joy_value;
2544   int result = -1;
2545
2546   if (joystick.status == JOYSTICK_NOT_AVAILABLE)
2547     return FALSE;
2548
2549   if (joystick_fd < 0 || !setup.input[player_nr].use_joystick)
2550     return FALSE;
2551
2552   ClearWindow();
2553
2554   for(y=0; y < 3; y++)
2555   {
2556     for(x=0; x < 3; x++)
2557     {
2558       DrawGraphic(xpos + x - 1, ypos + y - 1, IMG_MENU_CALIBRATE_BLUE, 0);
2559       check[x][y] = FALSE;
2560     }
2561   }
2562
2563   DrawText(mSX,      mSY +  6 * 32, " ROTATE JOYSTICK ", FONT_TITLE_1);
2564   DrawText(mSX,      mSY +  7 * 32, "IN ALL DIRECTIONS", FONT_TITLE_1);
2565   DrawText(mSX + 16, mSY +  9 * 32, "  IF ALL BALLS  ",  FONT_TITLE_1);
2566   DrawText(mSX,      mSY + 10 * 32, "   ARE YELLOW,   ", FONT_TITLE_1);
2567   DrawText(mSX,      mSY + 11 * 32, " CENTER JOYSTICK ", FONT_TITLE_1);
2568   DrawText(mSX,      mSY + 12 * 32, "       AND       ", FONT_TITLE_1);
2569   DrawText(mSX,      mSY + 13 * 32, "PRESS ANY BUTTON!", FONT_TITLE_1);
2570
2571   joy_value = Joystick(player_nr);
2572   last_x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
2573   last_y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
2574
2575   /* eventually uncalibrated center position (joystick could be uncentered) */
2576   if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
2577     return FALSE;
2578
2579   new_joystick_xmiddle = joy_x;
2580   new_joystick_ymiddle = joy_y;
2581
2582   DrawGraphic(xpos + last_x, ypos + last_y, IMG_MENU_CALIBRATE_RED, 0);
2583   BackToFront();
2584
2585   while(Joystick(player_nr) & JOY_BUTTON);      /* wait for released button */
2586   InitAnimation();
2587
2588   while(result < 0)
2589   {
2590     if (PendingEvent())         /* got event */
2591     {
2592       Event event;
2593
2594       NextEvent(&event);
2595
2596       switch(event.type)
2597       {
2598         case EVENT_KEYPRESS:
2599           switch(GetEventKey((KeyEvent *)&event, TRUE))
2600           {
2601             case KSYM_Return:
2602               if (check_remaining == 0)
2603                 result = 1;
2604               break;
2605
2606             case KSYM_Escape:
2607               result = 0;
2608               break;
2609
2610             default:
2611               break;
2612           }
2613           break;
2614
2615         case EVENT_KEYRELEASE:
2616           key_joystick_mapping = 0;
2617           break;
2618
2619         default:
2620           HandleOtherEvents(&event);
2621           break;
2622       }
2623     }
2624
2625     if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
2626       return FALSE;
2627
2628     new_joystick_xleft  = MIN(new_joystick_xleft,  joy_x);
2629     new_joystick_xright = MAX(new_joystick_xright, joy_x);
2630     new_joystick_yupper = MIN(new_joystick_yupper, joy_y);
2631     new_joystick_ylower = MAX(new_joystick_ylower, joy_y);
2632
2633     setup.input[player_nr].joy.xleft = new_joystick_xleft;
2634     setup.input[player_nr].joy.yupper = new_joystick_yupper;
2635     setup.input[player_nr].joy.xright = new_joystick_xright;
2636     setup.input[player_nr].joy.ylower = new_joystick_ylower;
2637     setup.input[player_nr].joy.xmiddle = new_joystick_xmiddle;
2638     setup.input[player_nr].joy.ymiddle = new_joystick_ymiddle;
2639
2640     CheckJoystickData();
2641
2642     joy_value = Joystick(player_nr);
2643
2644     if (joy_value & JOY_BUTTON && check_remaining == 0)
2645       result = 1;
2646
2647     x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
2648     y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
2649
2650     if (x != last_x || y != last_y)
2651     {
2652       DrawGraphic(xpos + last_x, ypos + last_y, IMG_MENU_CALIBRATE_YELLOW, 0);
2653       DrawGraphic(xpos + x,      ypos + y,      IMG_MENU_CALIBRATE_RED,    0);
2654
2655       last_x = x;
2656       last_y = y;
2657
2658       if (check_remaining > 0 && !check[x+1][y+1])
2659       {
2660         check[x+1][y+1] = TRUE;
2661         check_remaining--;
2662       }
2663
2664 #if 0
2665 #ifdef DEBUG
2666       printf("LEFT / MIDDLE / RIGHT == %d / %d / %d\n",
2667              setup.input[player_nr].joy.xleft,
2668              setup.input[player_nr].joy.xmiddle,
2669              setup.input[player_nr].joy.xright);
2670       printf("UP / MIDDLE / DOWN == %d / %d / %d\n",
2671              setup.input[player_nr].joy.yupper,
2672              setup.input[player_nr].joy.ymiddle,
2673              setup.input[player_nr].joy.ylower);
2674 #endif
2675 #endif
2676
2677     }
2678
2679     BackToFront();
2680     DoAnimation();
2681
2682     /* don't eat all CPU time */
2683     Delay(10);
2684   }
2685
2686   /* calibrated center position (joystick should now be centered) */
2687   if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
2688     return FALSE;
2689
2690   new_joystick_xmiddle = joy_x;
2691   new_joystick_ymiddle = joy_y;
2692
2693   StopAnimation();
2694
2695   DrawSetupScreen_Input();
2696
2697   /* wait until the last pressed button was released */
2698   while (Joystick(player_nr) & JOY_BUTTON)
2699   {
2700     if (PendingEvent())         /* got event */
2701     {
2702       Event event;
2703
2704       NextEvent(&event);
2705       HandleOtherEvents(&event);
2706
2707       Delay(10);
2708     }
2709   }
2710
2711   return TRUE;
2712 }
2713
2714 void CalibrateJoystick(int player_nr)
2715 {
2716   if (!CalibrateJoystickMain(player_nr))
2717   {
2718     ClearWindow();
2719
2720     DrawText(mSX + 16, mSY + 6*32, "  JOYSTICK NOT  ",  FONT_TITLE_1);
2721     DrawText(mSX,      mSY + 7*32, "    AVAILABLE    ", FONT_TITLE_1);
2722     BackToFront();
2723     Delay(2000);        /* show error message for two seconds */
2724   }
2725 }
2726
2727 void DrawSetupScreen()
2728 {
2729   DeactivateJoystick();
2730
2731   SetMainBackgroundImage(IMG_BACKGROUND_SETUP);
2732
2733   if (setup_mode == SETUP_MODE_INPUT)
2734     DrawSetupScreen_Input();
2735   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
2736     DrawChooseTree(&artwork.gfx_current);
2737   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
2738     DrawChooseTree(&artwork.snd_current);
2739   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
2740     DrawChooseTree(&artwork.mus_current);
2741   else
2742     DrawSetupScreen_Generic();
2743 }
2744
2745 void HandleSetupScreen(int mx, int my, int dx, int dy, int button)
2746 {
2747   if (setup_mode == SETUP_MODE_INPUT)
2748     HandleSetupScreen_Input(mx, my, dx, dy, button);
2749   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
2750     HandleChooseTree(mx, my, dx, dy, button, &artwork.gfx_current);
2751   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
2752     HandleChooseTree(mx, my, dx, dy, button, &artwork.snd_current);
2753   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
2754     HandleChooseTree(mx, my, dx, dy, button, &artwork.mus_current);
2755   else
2756     HandleSetupScreen_Generic(mx, my, dx, dy, button);
2757 }
2758
2759 void HandleGameActions()
2760 {
2761   if (game_status != GAME_MODE_PLAYING)
2762     return;
2763
2764   if (local_player->LevelSolved)
2765     GameWon();
2766
2767   if (AllPlayersGone && !TAPE_IS_STOPPED(tape))
2768     TapeStop();
2769
2770   GameActions();
2771
2772   BackToFront();
2773
2774 #if 1
2775   if (tape.auto_play && !tape.playing)
2776     AutoPlayTape();     /* continue automatically playing next tape */
2777 #endif
2778 }
2779
2780 /* ---------- new screen button stuff -------------------------------------- */
2781
2782 /* graphic position and size values for buttons and scrollbars */
2783 #define SC_SCROLLBUTTON_XSIZE           TILEX
2784 #define SC_SCROLLBUTTON_YSIZE           TILEY
2785
2786 #define SC_SCROLL_VERTICAL_XSIZE        SC_SCROLLBUTTON_XSIZE
2787 #define SC_SCROLL_VERTICAL_YSIZE        ((MAX_MENU_ENTRIES_ON_SCREEN - 2) * \
2788                                          SC_SCROLLBUTTON_YSIZE)
2789 #define SC_SCROLL_UP_XPOS               (SXSIZE - SC_SCROLLBUTTON_XSIZE)
2790 #define SC_SCROLL_UP_YPOS               (2 * SC_SCROLLBUTTON_YSIZE)
2791 #define SC_SCROLL_VERTICAL_XPOS         SC_SCROLL_UP_XPOS
2792 #define SC_SCROLL_VERTICAL_YPOS         (SC_SCROLL_UP_YPOS + \
2793                                          SC_SCROLLBUTTON_YSIZE)
2794 #define SC_SCROLL_DOWN_XPOS             SC_SCROLL_UP_XPOS
2795 #define SC_SCROLL_DOWN_YPOS             (SC_SCROLL_VERTICAL_YPOS + \
2796                                          SC_SCROLL_VERTICAL_YSIZE)
2797
2798 #define SC_BORDER_SIZE                  14
2799
2800 static struct
2801 {
2802   int gfx_unpressed, gfx_pressed;
2803   int x, y;
2804   int gadget_id;
2805   char *infotext;
2806 } scrollbutton_info[NUM_SCREEN_SCROLLBUTTONS] =
2807 {
2808   {
2809     IMG_MENU_BUTTON_UP, IMG_MENU_BUTTON_UP_ACTIVE,
2810     SC_SCROLL_UP_XPOS, SC_SCROLL_UP_YPOS,
2811     SCREEN_CTRL_ID_SCROLL_UP,
2812     "scroll up"
2813   },
2814   {
2815     IMG_MENU_BUTTON_DOWN, IMG_MENU_BUTTON_DOWN_ACTIVE,
2816     SC_SCROLL_DOWN_XPOS, SC_SCROLL_DOWN_YPOS,
2817     SCREEN_CTRL_ID_SCROLL_DOWN,
2818     "scroll down"
2819   }
2820 };
2821
2822 static struct
2823 {
2824 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
2825   Bitmap **gfx_unpressed, **gfx_pressed;
2826 #else
2827   int gfx_unpressed, gfx_pressed;
2828 #endif
2829   int x, y;
2830   int width, height;
2831   int type;
2832   int gadget_id;
2833   char *infotext;
2834 } scrollbar_info[NUM_SCREEN_SCROLLBARS] =
2835 {
2836   {
2837 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
2838     &scrollbar_bitmap[0], &scrollbar_bitmap[1],
2839 #else
2840     IMG_MENU_SCROLLBAR, IMG_MENU_SCROLLBAR_ACTIVE,
2841 #endif
2842     SX + SC_SCROLL_VERTICAL_XPOS, SY + SC_SCROLL_VERTICAL_YPOS,
2843     SC_SCROLL_VERTICAL_XSIZE, SC_SCROLL_VERTICAL_YSIZE,
2844     GD_TYPE_SCROLLBAR_VERTICAL,
2845     SCREEN_CTRL_ID_SCROLL_VERTICAL,
2846     "scroll level series vertically"
2847   }
2848 };
2849
2850 static void CreateScreenScrollbuttons()
2851 {
2852   struct GadgetInfo *gi;
2853   unsigned long event_mask;
2854   int i;
2855
2856   for (i=0; i<NUM_SCREEN_SCROLLBUTTONS; i++)
2857   {
2858     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
2859     int gfx_unpressed, gfx_pressed;
2860     int x, y, width, height;
2861     int gd_x1, gd_x2, gd_y1, gd_y2;
2862     int id = scrollbutton_info[i].gadget_id;
2863
2864     x = scrollbutton_info[i].x;
2865     y = scrollbutton_info[i].y;
2866
2867     event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
2868
2869     x += SX;
2870     y += SY;
2871     width = SC_SCROLLBUTTON_XSIZE;
2872     height = SC_SCROLLBUTTON_YSIZE;
2873
2874     gfx_unpressed = scrollbutton_info[i].gfx_unpressed;
2875     gfx_pressed   = scrollbutton_info[i].gfx_pressed;
2876     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
2877     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
2878     gd_x1 = graphic_info[gfx_unpressed].src_x;
2879     gd_y1 = graphic_info[gfx_unpressed].src_y;
2880     gd_x2 = graphic_info[gfx_pressed].src_x;
2881     gd_y2 = graphic_info[gfx_pressed].src_y;
2882
2883     gi = CreateGadget(GDI_CUSTOM_ID, id,
2884                       GDI_CUSTOM_TYPE_ID, i,
2885                       GDI_INFO_TEXT, scrollbutton_info[i].infotext,
2886                       GDI_X, x,
2887                       GDI_Y, y,
2888                       GDI_WIDTH, width,
2889                       GDI_HEIGHT, height,
2890                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
2891                       GDI_STATE, GD_BUTTON_UNPRESSED,
2892                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
2893                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
2894                       GDI_EVENT_MASK, event_mask,
2895                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
2896                       GDI_END);
2897
2898     if (gi == NULL)
2899       Error(ERR_EXIT, "cannot create gadget");
2900
2901     screen_gadget[id] = gi;
2902   }
2903 }
2904
2905 static void CreateScreenScrollbars()
2906 {
2907   int i;
2908
2909   for (i=0; i<NUM_SCREEN_SCROLLBARS; i++)
2910   {
2911     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
2912 #if !defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
2913     int gfx_unpressed, gfx_pressed;
2914 #endif
2915     int gd_x1, gd_x2, gd_y1, gd_y2;
2916     struct GadgetInfo *gi;
2917     int items_max, items_visible, item_position;
2918     unsigned long event_mask;
2919     int num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
2920     int id = scrollbar_info[i].gadget_id;
2921
2922     items_max = num_page_entries;
2923     items_visible = num_page_entries;
2924     item_position = 0;
2925
2926     event_mask = GD_EVENT_MOVING | GD_EVENT_OFF_BORDERS;
2927
2928 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
2929     gd_bitmap_unpressed = *scrollbar_info[i].gfx_unpressed;
2930     gd_bitmap_pressed   = *scrollbar_info[i].gfx_pressed;
2931     gd_x1 = 0;
2932     gd_y1 = 0;
2933     gd_x2 = 0;
2934     gd_y2 = 0;
2935 #else
2936     gfx_unpressed = scrollbar_info[i].gfx_unpressed;
2937     gfx_pressed   = scrollbar_info[i].gfx_pressed;
2938     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
2939     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
2940     gd_x1 = graphic_info[gfx_unpressed].src_x;
2941     gd_y1 = graphic_info[gfx_unpressed].src_y;
2942     gd_x2 = graphic_info[gfx_pressed].src_x;
2943     gd_y2 = graphic_info[gfx_pressed].src_y;
2944 #endif
2945
2946     gi = CreateGadget(GDI_CUSTOM_ID, id,
2947                       GDI_CUSTOM_TYPE_ID, i,
2948                       GDI_INFO_TEXT, scrollbar_info[i].infotext,
2949                       GDI_X, scrollbar_info[i].x,
2950                       GDI_Y, scrollbar_info[i].y,
2951                       GDI_WIDTH, scrollbar_info[i].width,
2952                       GDI_HEIGHT, scrollbar_info[i].height,
2953                       GDI_TYPE, scrollbar_info[i].type,
2954                       GDI_SCROLLBAR_ITEMS_MAX, items_max,
2955                       GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
2956                       GDI_SCROLLBAR_ITEM_POSITION, item_position,
2957                       GDI_STATE, GD_BUTTON_UNPRESSED,
2958                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
2959                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
2960                       GDI_BORDER_SIZE, SC_BORDER_SIZE, SC_BORDER_SIZE,
2961                       GDI_EVENT_MASK, event_mask,
2962                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
2963                       GDI_END);
2964
2965     if (gi == NULL)
2966       Error(ERR_EXIT, "cannot create gadget");
2967
2968     screen_gadget[id] = gi;
2969   }
2970 }
2971
2972 void CreateScreenGadgets()
2973 {
2974 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
2975   int i;
2976
2977   for (i=0; i < NUM_SCROLLBAR_BITMAPS; i++)
2978   {
2979     scrollbar_bitmap[i] = CreateBitmap(TILEX, TILEY, DEFAULT_DEPTH);
2980
2981     /* copy pointers to clip mask and GC */
2982     scrollbar_bitmap[i]->clip_mask =
2983       graphic_info[IMG_MENU_SCROLLBAR + i].clip_mask;
2984     scrollbar_bitmap[i]->stored_clip_gc =
2985       graphic_info[IMG_MENU_SCROLLBAR + i].clip_gc;
2986
2987     BlitBitmap(graphic_info[IMG_MENU_SCROLLBAR + i].bitmap,
2988                scrollbar_bitmap[i],
2989                graphic_info[IMG_MENU_SCROLLBAR + i].src_x,
2990                graphic_info[IMG_MENU_SCROLLBAR + i].src_y,
2991                TILEX, TILEY, 0, 0);
2992   }
2993 #endif
2994
2995   CreateScreenScrollbuttons();
2996   CreateScreenScrollbars();
2997 }
2998
2999 void FreeScreenGadgets()
3000 {
3001   int i;
3002
3003 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
3004   for (i=0; i < NUM_SCROLLBAR_BITMAPS; i++)
3005   {
3006     /* prevent freeing clip mask and GC twice */
3007     scrollbar_bitmap[i]->clip_mask = None;
3008     scrollbar_bitmap[i]->stored_clip_gc = None;
3009
3010     FreeBitmap(scrollbar_bitmap[i]);
3011   }
3012 #endif
3013
3014   for (i=0; i<NUM_SCREEN_GADGETS; i++)
3015     FreeGadget(screen_gadget[i]);
3016 }
3017
3018 void MapChooseTreeGadgets(TreeInfo *ti)
3019 {
3020   int num_entries = numTreeInfoInGroup(ti);
3021   int i;
3022
3023   if (num_entries <= NUM_MENU_ENTRIES_ON_SCREEN)
3024     return;
3025
3026   for (i=0; i<NUM_SCREEN_GADGETS; i++)
3027     MapGadget(screen_gadget[i]);
3028 }
3029
3030 void UnmapChooseTreeGadgets()
3031 {
3032   int i;
3033
3034   for (i=0; i<NUM_SCREEN_GADGETS; i++)
3035     UnmapGadget(screen_gadget[i]);
3036 }
3037
3038 static void HandleScreenGadgets(struct GadgetInfo *gi)
3039 {
3040   int id = gi->custom_id;
3041
3042   if (game_status != GAME_MODE_LEVELS && game_status != GAME_MODE_SETUP)
3043     return;
3044
3045   switch (id)
3046   {
3047     case SCREEN_CTRL_ID_SCROLL_UP:
3048       if (game_status == GAME_MODE_LEVELS)
3049         HandleChooseLevel(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
3050       else if (game_status == GAME_MODE_SETUP)
3051         HandleSetupScreen(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
3052       break;
3053
3054     case SCREEN_CTRL_ID_SCROLL_DOWN:
3055       if (game_status == GAME_MODE_LEVELS)
3056         HandleChooseLevel(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
3057       else if (game_status == GAME_MODE_SETUP)
3058         HandleSetupScreen(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
3059       break;
3060
3061     case SCREEN_CTRL_ID_SCROLL_VERTICAL:
3062       if (game_status == GAME_MODE_LEVELS)
3063         HandleChooseLevel(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
3064       else if (game_status == GAME_MODE_SETUP)
3065         HandleSetupScreen(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
3066       break;
3067
3068     default:
3069       break;
3070   }
3071 }