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