+ if (from_y > to_y)
+ swap_numbers(&from_y, &to_y);
+
+ brush_width = to_x - from_x + 1;
+ brush_height = to_y - from_y + 1;
+
+ from_lx = from_x + level_xpos;
+ from_ly = from_y + level_ypos;
+
+ for (y=0; y<brush_height; y++)
+ for (x=0; x<brush_width; x++)
+ brush_buffer[x][y] = Feld[from_lx + x][from_ly + y];
+
+ delete_old_brush = FALSE;
+ }
+ else if (mode == CB_BRUSH_TO_CURSOR || mode == CB_DELETE_OLD_CURSOR ||
+ mode == CB_BRUSH_TO_LEVEL)
+ {
+ int cursor_x = (mode == CB_DELETE_OLD_CURSOR ? last_cursor_x : from_x);
+ int cursor_y = (mode == CB_DELETE_OLD_CURSOR ? last_cursor_y : from_y);
+ int cursor_from_x = cursor_x - brush_width / 2;
+ int cursor_from_y = cursor_y - brush_height / 2;
+ int border_from_x = cursor_x, border_from_y = cursor_y;
+ int border_to_x = cursor_x, border_to_y = cursor_y;
+
+ if (mode != CB_DELETE_OLD_CURSOR && delete_old_brush)
+ CopyBrushExt(0, 0, 0, 0, CB_DELETE_OLD_CURSOR);
+
+ if (!IN_LEV_FIELD(cursor_x + level_xpos, cursor_y + level_ypos))
+ {
+ delete_old_brush = FALSE;
+ return;
+ }
+
+ for (y=0; y<brush_height; y++)
+ {
+ for (x=0; x<brush_width; x++)
+ {
+ int sx = cursor_from_x + x;
+ int sy = cursor_from_y + y;
+ int lx = sx + level_xpos;
+ int ly = sy + level_ypos;
+ int element = (mode == CB_DELETE_OLD_CURSOR ? -1 : brush_buffer[x][y]);
+ boolean change_level = (mode == CB_BRUSH_TO_LEVEL);
+
+ if (IN_LEV_FIELD(lx, ly) &&
+ sx >=0 && sx < ED_FIELDX && sy >=0 && sy < ED_FIELDY)
+ {
+ if (sx < border_from_x)
+ border_from_x = sx;
+ else if (sx > border_to_x)
+ border_to_x = sx;
+ if (sy < border_from_y)
+ border_from_y = sy;
+ else if (sy > border_to_y)
+ border_to_y = sy;
+
+ DrawLineElement(sx, sy, element, change_level);
+ }
+ }
+ }
+
+ /*
+ printf("%d, %d - %d, %d in level and screen\n",
+ border_from_x, border_from_y, border_to_x, border_to_y);
+ */
+
+ if (mode != CB_DELETE_OLD_CURSOR)
+ DrawAreaBorder(border_from_x, border_from_y, border_to_x, border_to_y);
+
+ if (mode == CB_BRUSH_TO_LEVEL)
+ CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
+
+ last_cursor_x = cursor_x;
+ last_cursor_y = cursor_y;
+ delete_old_brush = TRUE;
+ }
+}
+
+static void CopyAreaToBrush(int from_x, int from_y, int to_x, int to_y)
+{
+ CopyBrushExt(from_x, from_y, to_x, to_y, CB_AREA_TO_BRUSH);
+}
+
+static void CopyBrushToLevel(int x, int y)
+{
+ CopyBrushExt(x, y, 0, 0, CB_BRUSH_TO_LEVEL);
+}
+
+static void CopyBrushToCursor(int x, int y)
+{
+ CopyBrushExt(x, y, 0, 0, CB_BRUSH_TO_CURSOR);
+}
+
+static void DeleteBrushFromCursor()
+{
+ CopyBrushExt(0, 0, 0, 0, CB_DELETE_OLD_CURSOR);
+}
+
+static void FloodFill(int from_x, int from_y, int fill_element)
+{
+ int i,x,y;
+ int old_element;
+ static int check[4][2] = { {-1,0}, {0,-1}, {1,0}, {0,1} };
+ static int safety = 0;
+
+ /* check if starting field still has the desired content */
+ if (Feld[from_x][from_y] == fill_element)
+ return;
+
+ safety++;
+
+ if (safety > lev_fieldx*lev_fieldy)
+ Error(ERR_EXIT, "Something went wrong in 'FloodFill()'. Please debug.");
+
+ old_element = Feld[from_x][from_y];
+ Feld[from_x][from_y] = fill_element;
+
+ for(i=0;i<4;i++)
+ {
+ x = from_x + check[i][0];
+ y = from_y + check[i][1];
+
+ if (IN_LEV_FIELD(x,y) && Feld[x][y] == old_element)
+ FloodFill(x, y, fill_element);
+ }
+
+ safety--;
+}
+
+/* values for DrawLevelText() modes */
+#define TEXT_INIT 0
+#define TEXT_SETCURSOR 1
+#define TEXT_WRITECHAR 2
+#define TEXT_BACKSPACE 3
+#define TEXT_NEWLINE 4
+#define TEXT_END 5
+
+static void DrawLevelText(int sx, int sy, char letter, int mode)
+{
+ static short delete_buffer[MAX_LEV_FIELDX];
+ static int start_sx, start_sy;
+ static int last_sx, last_sy;
+ static boolean typing = FALSE;
+ int letter_element = EL_CHAR_ASCII0 + letter;
+ int lx, ly;
+
+ /* map lower case letters to upper case and convert special characters */
+ if (letter >= 'a' && letter <= 'z')
+ letter_element = EL_CHAR_ASCII0 + letter + (int)('A' - 'a');
+ else if (letter == 'ä' || letter == 'Ä')
+ letter_element = EL_CHAR_AE;
+ else if (letter == 'ö' || letter == 'Ö')
+ letter_element = EL_CHAR_OE;
+ else if (letter == 'ü' || letter == 'Ü')
+ letter_element = EL_CHAR_UE;
+ else if (letter == '^')
+ letter_element = EL_CHAR_COPY;
+ else
+ letter_element = EL_CHAR_ASCII0 + letter;
+
+ if (mode != TEXT_INIT)
+ {
+ if (!typing)
+ return;
+
+ if (mode != TEXT_SETCURSOR)
+ {
+ sx = last_sx;
+ sy = last_sy;
+ }
+
+ lx = last_sx + level_xpos;
+ ly = last_sy + level_ypos;
+ }
+
+ switch (mode)
+ {
+ case TEXT_INIT:
+ if (typing)
+ DrawLevelText(0, 0, 0, TEXT_END);
+
+ typing = TRUE;
+ start_sx = last_sx = sx;
+ start_sy = last_sy = sy;
+ DrawLevelText(sx, sy, 0, TEXT_SETCURSOR);
+ break;
+
+ case TEXT_SETCURSOR:
+ DrawMiniElement(last_sx, last_sy, Feld[lx][ly]);
+ DrawAreaBorder(sx, sy, sx, sy);
+ last_sx = sx;
+ last_sy = sy;
+ break;
+
+ case TEXT_WRITECHAR:
+ if (letter_element >= EL_CHAR_START && letter_element <= EL_CHAR_END)
+ {
+ delete_buffer[sx - start_sx] = Feld[lx][ly];
+ Feld[lx][ly] = letter_element;
+
+ if (sx + 1 < ED_FIELDX && lx + 1 < lev_fieldx)
+ DrawLevelText(sx + 1, sy, 0, TEXT_SETCURSOR);
+ else if (sy + 1 < ED_FIELDY && ly + 1 < lev_fieldy)
+ DrawLevelText(start_sx, sy + 1, 0, TEXT_SETCURSOR);
+ else
+ DrawLevelText(0, 0, 0, TEXT_END);
+ }
+ break;
+
+ case TEXT_BACKSPACE:
+ if (sx > start_sx)
+ {
+ Feld[lx - 1][ly] = delete_buffer[sx - start_sx - 1];
+ DrawMiniElement(sx - 1, sy, new_element3);
+ DrawLevelText(sx - 1, sy, 0, TEXT_SETCURSOR);
+ }
+ break;
+
+ case TEXT_NEWLINE:
+ if (sy + 1 < ED_FIELDY - 1 && ly + 1 < lev_fieldy - 1)
+ DrawLevelText(start_sx, sy + 1, 0, TEXT_SETCURSOR);
+ else
+ DrawLevelText(0, 0, 0, TEXT_END);
+ break;
+
+ case TEXT_END:
+ CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
+ DrawMiniElement(sx, sy, Feld[lx][ly]);
+ typing = FALSE;
+ break;
+
+ default:
+ break;
+ }
+}
+
+static void SetTextCursor(int unused_sx, int unused_sy, int sx, int sy,
+ int element, boolean change_level)
+{
+ int lx = sx + level_xpos;
+ int ly = sy + level_ypos;
+
+ if (element == -1)
+ DrawMiniElement(sx, sy, Feld[lx][ly]);
+ else
+ DrawAreaBorder(sx, sy, sx, sy);
+}
+
+static void CopyLevelToUndoBuffer(int mode)
+{
+ static boolean accumulated_undo = FALSE;
+ boolean new_undo_buffer_position = TRUE;
+ int x, y;
+
+ switch (mode)
+ {
+ case UNDO_IMMEDIATE:
+ accumulated_undo = FALSE;
+ break;
+
+ case UNDO_ACCUMULATE:
+ if (accumulated_undo)
+ new_undo_buffer_position = FALSE;
+ accumulated_undo = TRUE;
+ break;
+
+ default:
+ break;
+ }
+
+ if (new_undo_buffer_position)
+ {
+ /* new position in undo buffer ring */
+ undo_buffer_position = (undo_buffer_position + 1) % NUM_UNDO_STEPS;
+
+ if (undo_buffer_steps < NUM_UNDO_STEPS - 1)
+ undo_buffer_steps++;
+ }
+
+ for(x=0; x<lev_fieldx; x++)
+ for(y=0; y<lev_fieldy; y++)
+ UndoBuffer[undo_buffer_position][x][y] = Feld[x][y];
+#if 0
+#ifdef DEBUG
+ printf("level saved to undo buffer\n");
+#endif
+#endif