/** * Construct this Field object from another Field object. */ protected void ConstructFromField(Field field) { Resize(field.Width, field.Height); for (uint x = 0; x < width; x++) { for (uint y = 0; y < height; y++) { SetBlock(x, y, field.GetBlock(x, y)); } } }
/** * Render a part of a field to a Gdk.Pixbuf. * * @param region The region of the field to render. * @param selection The region of the field that is selected, or null. * This region will be rendered with a different color. * @require Both region and selection must be within the current field's bounds. */ public Pixbuf RenderToPixbuf(FieldRegion region, FieldRegion selection) { Point screenLeftTop; uint width, height; byte[] pixels; /* * Create a pixel buffer which will hold the region we're * going to render. */ screenLeftTop.X = (int) region.Left; screenLeftTop.Y = (int) region.Top; Calc.FieldPosToScreenPos(ref screenLeftTop, field, zoomLevel); width = region.Width * zoomLevel; height = region.Height * zoomLevel; pixels = new byte[width * height * CHANNELS]; /* * Go through each block in the region and render it * to the pixel buffer. */ for (uint y = region.Bottom; y <= region.Top; y++) { for (uint x = region.Left; x <= region.Right; x++) { Point p; Color color; p.X = (int) x; p.Y = (int) y; Calc.FieldPosToScreenPos(ref p, field, zoomLevel); if (selection != null && WithinSelection(selection, x, y)) { color = colors.GetSelectionColor(field.GetBlock(x, y)); } else { color = colors.GetColor(field.GetBlock(x, y)); } DrawFilledRect(pixels, (uint) (p.X - screenLeftTop.X), (uint) (p.Y - screenLeftTop.Y), zoomLevel, zoomLevel, width, (byte) (color.Red / 256), (byte) (color.Green / 256), (byte) (color.Blue / 256)); } } /* * Render the selection border. */ if (selection != null) { Point p1, p2; Color color; p1.X = (int) selection.Left; p1.Y = (int) selection.Top; p2.X = (int) selection.Right; p2.Y = (int) selection.Bottom; Calc.FieldPosToScreenPos(ref p1, field, zoomLevel); Calc.FieldPosToScreenPos(ref p2, field, zoomLevel); p1.X -= screenLeftTop.X; p1.Y -= screenLeftTop.Y; p2.X -= screenLeftTop.X; p2.Y -= screenLeftTop.Y; color = colors.SelectionBorderColor; DrawOpenRect(pixels, (uint) p1.X, (uint) p1.Y, (uint) (p2.X - p1.X + zoomLevel - 1), (uint) (p2.Y - p1.Y + zoomLevel - 1), width, (byte) (color.Red / 256), (byte) (color.Green / 256), (byte) (color.Blue / 256)); } return new Pixbuf(pixels, Colorspace.Rgb, CHANNELS == 4, 8, (int) width, (int) height, (int) (width * CHANNELS), null); }