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