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