+static void AdjustEditorScrollbar(int id)
+{
+ struct GadgetInfo *gi = level_editor_gadget[id];
+ int items_max, items_visible, item_position;
+
+ if (id == GADGET_ID_SCROLL_HORIZONTAL)
+ {
+ items_max = MAX(lev_fieldx + 2, ed_fieldx);
+ items_visible = ed_fieldx;
+ item_position = level_xpos + 1;
+ }
+ else
+ {
+ items_max = MAX(lev_fieldy + 2, ed_fieldy);
+ items_visible = ed_fieldy;
+ item_position = level_ypos + 1;
+ }
+
+ if (item_position > items_max - items_visible)
+ item_position = items_max - items_visible;
+
+ ModifyGadget(gi, GDI_SCROLLBAR_ITEMS_MAX, items_max,
+ GDI_SCROLLBAR_ITEM_POSITION, item_position, GDI_END);
+}
+
+static void ModifyEditorCounter(int counter_id, int new_value)
+{
+ int *counter_value = counterbutton_info[counter_id].value;
+ int gadget_id = counterbutton_info[counter_id].gadget_id_text;
+ struct GadgetInfo *gi = level_editor_gadget[gadget_id];
+
+ ModifyGadget(gi, GDI_NUMBER_VALUE, new_value, GDI_END);
+
+ if (counter_value != NULL)
+ *counter_value = gi->textinput.number_value;
+}
+
+static void ModifyEditorCounterLimits(int counter_id, int min, int max)
+{
+ int gadget_id = counterbutton_info[counter_id].gadget_id_text;
+ struct GadgetInfo *gi = level_editor_gadget[gadget_id];
+
+ ModifyGadget(gi, GDI_NUMBER_MIN, min, GDI_NUMBER_MAX, max, GDI_END);
+}
+
+static void ModifyEditorSelectbox(int selectbox_id, int new_value)
+{
+ int gadget_id = selectbox_info[selectbox_id].gadget_id;
+ struct GadgetInfo *gi = level_editor_gadget[gadget_id];
+ int new_index_value = setSelectboxValue(selectbox_id, new_value);
+
+ ModifyGadget(gi, GDI_SELECTBOX_INDEX, new_index_value, GDI_END);
+}
+
+static void ModifyEditorElementList()
+{
+ int i;
+
+ for (i=0; i<ED_NUM_ELEMENTLIST_BUTTONS; i++)
+ {
+ int gadget_id = GADGET_ID_ELEMENTLIST_FIRST + i;
+ struct GadgetInfo *gi = level_editor_gadget[gadget_id];
+ struct GadgetDesign *gd = &gi->deco.design;
+ int element = editor_elements[element_shift + i];
+
+ UnmapGadget(gi);
+ getMiniGraphicSource(el2edimg(element), &gd->bitmap, &gd->x, &gd->y);
+ ModifyGadget(gi, GDI_INFO_TEXT, getElementInfoText(element), GDI_END);
+ MapGadget(gi);
+ }
+}
+
+static void PickDrawingElement(int button, int element)
+{
+ if (button < 1 || button > 3)
+ return;
+
+ if (button == 1)
+ {
+ new_element1 = element;
+ DrawMiniGraphicExt(drawto,
+ DX + ED_WIN_MB_LEFT_XPOS, DY + ED_WIN_MB_LEFT_YPOS,
+ el2edimg(new_element1));
+ }
+ else if (button == 2)
+ {
+ new_element2 = element;
+ DrawMiniGraphicExt(drawto,
+ DX + ED_WIN_MB_MIDDLE_XPOS, DY + ED_WIN_MB_MIDDLE_YPOS,
+ el2edimg(new_element2));
+ }
+ else
+ {
+ new_element3 = element;
+ DrawMiniGraphicExt(drawto,
+ DX + ED_WIN_MB_RIGHT_XPOS, DY + ED_WIN_MB_RIGHT_YPOS,
+ el2edimg(new_element3));
+ }
+
+ redraw_mask |= REDRAW_DOOR_1;
+}
+
+static void RedrawDrawingElements()
+{
+ PickDrawingElement(1, new_element1);
+ PickDrawingElement(2, new_element2);
+ PickDrawingElement(3, new_element3);
+}
+
+static void DrawDrawingWindow()
+{
+ stick_element_properties_window = FALSE;
+
+ SetMainBackgroundImage(IMG_UNDEFINED);
+ ClearWindow();
+
+ UnmapLevelEditorWindowGadgets();
+ UnmapLevelEditorToolboxCustomGadgets();
+
+ AdjustDrawingAreaGadgets();
+ AdjustLevelScrollPosition();
+ AdjustEditorScrollbar(GADGET_ID_SCROLL_HORIZONTAL);
+ AdjustEditorScrollbar(GADGET_ID_SCROLL_VERTICAL);
+
+ DrawMiniLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
+
+ MapMainDrawingArea();
+ MapLevelEditorToolboxDrawingGadgets();
+}
+
+static void DrawLevelInfoWindow()
+{
+ int i;
+
+ stick_element_properties_window = FALSE;
+
+ SetMainBackgroundImage(IMG_BACKGROUND_EDITOR);
+ ClearWindow();
+ UnmapLevelEditorWindowGadgets();
+
+ DrawText(SX + ED_SETTINGS2_XPOS, SY + ED_SETTINGS1_YPOS,
+ "Level Settings", FONT_TITLE_1);
+ DrawText(SX + ED_SETTINGS2_XPOS, SY + ED_SETTINGS2_YPOS,
+ "Editor Settings", FONT_TITLE_1);
+
+ /* draw counter gadgets */
+ for (i=ED_COUNTER_ID_LEVEL_FIRST; i<=ED_COUNTER_ID_LEVEL_LAST; i++)
+ MapCounterButtons(i);
+
+ /* draw checkbutton gadgets */
+ for (i=ED_CHECKBUTTON_ID_LEVEL_FIRST; i<=ED_CHECKBUTTON_ID_LEVEL_LAST; i++)
+ MapCheckbuttonGadget(i);
+
+ /* draw radiobutton gadgets */
+ for (i=ED_RADIOBUTTON_ID_LEVEL_FIRST; i<=ED_RADIOBUTTON_ID_LEVEL_LAST; i++)
+ MapRadiobuttonGadget(i);
+
+ /* draw text input gadgets */
+ for (i=ED_TEXTINPUT_ID_LEVEL_FIRST; i<=ED_TEXTINPUT_ID_LEVEL_LAST; i++)
+ MapTextInputGadget(i);
+
+ /* draw drawing area */
+ MapDrawingArea(ED_DRAWING_ID_RANDOM_BACKGROUND);
+}
+
+static void DrawCustomContentArea()
+{
+ int id = ED_DRAWING_ID_CUSTOM_CONTENT;
+ struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[id].gadget_id];
+ int x1 = right_gadget_border[GADGET_ID_CUSTOM_DEADLINESS];
+ int x2 = right_gadget_border[GADGET_ID_CUSTOM_CONSISTENCY];
+ int x3 = right_gadget_border[GADGET_ID_CUSTOM_EXPLODE_IMPACT];
+ int xoffset = ED_DRAWINGAREA_TEXT_DISTANCE;
+
+ /* add distance for potential left text (without drawing area border) */
+ x2 += getTextWidthForGadget(drawingarea_info[id].text_left);
+
+ ModifyGadget(gi, GDI_X, MAX(x1, MAX(x2, x3)) + xoffset, GDI_END);
+
+ MapDrawingArea(ED_DRAWING_ID_CUSTOM_CONTENT);
+}
+
+static void DrawCustomChangeContentArea()
+{
+ int id = ED_DRAWING_ID_CUSTOM_CHANGE_CONTENT;
+ struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[id].gadget_id];
+ int x1 = right_gadget_border[GADGET_ID_CHANGE_USE_CONTENT];
+ int x2 = right_gadget_border[GADGET_ID_CHANGE_POWER];
+ int x3 = right_gadget_border[GADGET_ID_CHANGE_ONLY_COMPLETE];
+ int xoffset = ED_DRAWINGAREA_TEXT_DISTANCE;
+
+ ModifyGadget(gi, GDI_X, MAX(x1, MAX(x2, x3)) + xoffset, GDI_END);
+
+ MapDrawingArea(id);
+}
+
+static void DrawElementContentAreas()
+{
+ int x = SX + ED_AREA_YAMYAM_CONTENT_XPOS(3) + 4 * MINI_TILEX;
+ int y = SY + ED_AREA_YAMYAM_CONTENT_YPOS(0) + ED_BORDER_AREA_YSIZE;
+ int i;
+
+ /* display counter to choose number of element content areas */
+ MapCounterButtons(ED_COUNTER_ID_ELEMENT_CONTENT);
+
+ for (i=0; i < MAX_ELEMENT_CONTENTS; i++)
+ {
+ int id = ED_DRAWING_ID_ELEMENT_CONTENT_0 + i;
+ int font_height = getFontHeight(FONT_TEXT_1);
+
+ if (i < level.num_yamyam_contents)
+ MapDrawingArea(id);
+ else
+ {
+ UnmapDrawingArea(id);
+
+ /* delete content areas in case of reducing number of them */
+ DrawBackground(SX + drawingarea_info[id].x - MINI_TILEX / 2,
+ SY + drawingarea_info[id].y - MINI_TILEY / 2,
+ 4 * MINI_TILEX,
+ 4 * MINI_TILEX + ED_GADGET_TEXT_DISTANCE + font_height);
+ }
+ }
+
+ DrawText(x, y + 0 * MINI_TILEY, "content", FONT_TEXT_1);
+ DrawText(x, y + 1 * MINI_TILEY, "when", FONT_TEXT_1);
+ DrawText(x, y + 2 * MINI_TILEY, "smashed", FONT_TEXT_1);
+}
+
+static void DrawEnvelopeTextArea(int envelope_nr)
+{
+ int id = ED_TEXTAREA_ID_ENVELOPE_INFO;
+ struct GadgetInfo *gi = level_editor_gadget[textarea_info[id].gadget_id];
+
+ UnmapGadget(gi);
+ DrawBackground(gi->x, gi->y, gi->width, gi->height);
+
+ if (envelope_nr != -1)
+ textarea_info[id].value = level.envelope_text[envelope_nr];
+
+ ModifyGadget(gi, GDI_AREA_SIZE,
+ *counterbutton_info[ED_COUNTER_ID_ENVELOPE_XSIZE].value,
+ *counterbutton_info[ED_COUNTER_ID_ENVELOPE_YSIZE].value,
+ GDI_END);
+
+ MapTextAreaGadget(ED_TEXTAREA_ID_ENVELOPE_INFO);
+}
+
+char *getElementDescriptionFilename(int element)
+{
+ char *docs_dir = options.docs_directory;
+ char *elements_subdir = "elements";
+ static char *filename = NULL;
+ char basename[MAX_FILENAME_LEN];
+
+ if (filename != NULL)
+ free(filename);
+
+ /* 1st try: look for element description file for exactly this element */
+ sprintf(basename, "%s.txt", element_info[element].token_name);
+ filename = getPath3(docs_dir, elements_subdir, basename);
+ if (fileExists(filename))
+ return filename;
+
+ free(filename);
+
+ /* 2nd try: look for element description file for this element's class */
+ sprintf(basename, "%s.txt", element_info[element].class_name);
+ filename = getPath3(docs_dir, elements_subdir, basename);
+ if (fileExists(filename))
+ return filename;
+
+ return NULL;
+}
+
+#if 1
+static boolean PrintInfoText(char *text, int font_nr, int start_line)
+{
+ int font_height = getFontHeight(font_nr);
+ int pad_x = ED_SETTINGS_XPOS(0);
+ int pad_y = ED_SETTINGS_YPOS(0) + ED_BORDER_SIZE;
+ int sx = SX + pad_x;
+ int sy = SY + pad_y;
+ int max_lines_per_screen = (SYSIZE - pad_y) / font_height - 1;
+
+ if (start_line >= max_lines_per_screen)
+ return FALSE;
+
+ DrawText(sx, sy + start_line * font_height, text, font_nr);
+
+ return TRUE;
+}
+
+#if 1
+
+static int PrintElementDescriptionFromFile(char *filename, int start_line)
+{
+ int font_nr = FONT_TEXT_2;
+ int font_width = getFontWidth(font_nr);
+ int font_height = getFontHeight(font_nr);
+ int pad_x = ED_SETTINGS_XPOS(0);
+ int pad_y = ED_SETTINGS_YPOS(0) + ED_BORDER_SIZE;
+ int sx = SX + pad_x;
+ int sy = SY + pad_y;
+ int max_chars_per_line = (SXSIZE - 2 * pad_x) / font_width;
+ int max_lines_per_screen = (SYSIZE - pad_y) / font_height - 1;
+ int current_line = start_line;
+ char line[MAX_LINE_LEN];
+ char buffer[max_chars_per_line + 1];
+ int buffer_len;
+ FILE *file;
+
+ if (current_line >= max_lines_per_screen)
+ return 0;
+
+ if (filename == NULL)
+ return 0;
+
+ if (!(file = fopen(filename, MODE_READ)))
+ return 0;
+
+ buffer[0] = '\0';
+ buffer_len = 0;
+
+ while(!feof(file) && current_line < max_lines_per_screen)
+ {
+ char *line_ptr;
+ boolean last_line_was_empty = TRUE;
+
+ /* read next line of input file */
+ if (!fgets(line, MAX_LINE_LEN, file))
+ break;
+
+ /* skip comments (lines directly beginning with '#') */
+ if (line[0] == '#')
+ continue;
+
+ /* cut trailing newline from input line */
+ for (line_ptr = line; *line_ptr; line_ptr++)
+ {
+ if (*line_ptr == '\n' || *line_ptr == '\r')
+ {
+ *line_ptr = '\0';
+ break;
+ }
+ }
+
+ if (strlen(line) == 0) /* special case: force empty line */
+ strcpy(line, "\n");
+
+ line_ptr = line;
+
+ while (*line_ptr && current_line < max_lines_per_screen)
+ {
+ boolean buffer_filled = RenderLineToBuffer(&line_ptr,
+ buffer, &buffer_len,
+ last_line_was_empty,
+ max_chars_per_line);
+ if (buffer_filled)
+ {
+ DrawText(sx, sy + current_line * font_height, buffer, font_nr);
+ current_line++;
+
+ last_line_was_empty = (buffer_len == 0);
+
+ buffer[0] = '\0';
+ buffer_len = 0;
+ }
+ }
+ }
+
+ fclose(file);
+
+ if (buffer_len > 0 && current_line < max_lines_per_screen)
+ {
+ DrawText(sx, sy + current_line * font_height, buffer, font_nr);
+ current_line++;
+ }
+
+ return (current_line - start_line);
+}
+
+#else
+
+static int PrintElementDescriptionFromFile(char *filename, int start_line)
+{
+ int font_nr = FONT_TEXT_2;
+ int font_width = getFontWidth(font_nr);
+ int font_height = getFontHeight(font_nr);
+ int pad_x = ED_SETTINGS_XPOS(0);
+ int pad_y = ED_SETTINGS_YPOS(0) + ED_BORDER_SIZE;
+ int sx = SX + pad_x;
+ int sy = SY + pad_y;
+ int max_chars_per_line = (SXSIZE - 2 * pad_x) / font_width;
+ int max_lines_per_screen = (SYSIZE - pad_y) / font_height - 1;
+ int current_line = start_line;
+ char line[MAX_LINE_LEN];
+ char buffer[max_chars_per_line + 1];
+ int buffer_len;
+ FILE *file;
+
+ if (filename == NULL)
+ return 0;
+
+ if (!(file = fopen(filename, MODE_READ)))
+ return 0;
+
+ buffer[0] = '\0';
+ buffer_len = 0;
+
+ while(!feof(file))
+ {
+ char *line_ptr, *word_ptr;
+ boolean last_line_was_empty = TRUE;
+
+ /* read next line of input file */
+ if (!fgets(line, MAX_LINE_LEN, file))
+ break;
+
+ /* skip comments (lines directly beginning with '#') */
+ if (line[0] == '#')
+ continue;
+
+ /* cut trailing newline from input line */
+ for (line_ptr = line; *line_ptr; line_ptr++)
+ {
+ if (*line_ptr == '\n' || *line_ptr == '\r')
+ {
+ *line_ptr = '\0';
+ break;
+ }
+ }
+
+ if (strlen(line) == 0) /* special case: force empty line */
+ strcpy(line, "\n");
+
+ word_ptr = line;
+
+ while (*word_ptr)
+ {
+ boolean print_buffer = FALSE;
+ int word_len;
+
+ /* skip leading whitespaces */
+ while (*word_ptr == ' ' || *word_ptr == '\t')
+ word_ptr++;