private void ShowField(Field field) { FieldRenderer renderer = new FieldRenderer(field, null); FieldRegion region = new FieldRegion(); Pixbuf pixbuf; region.Left = region.Bottom = 0; region.Top = field.Height - 1; region.Right = field.Width - 1; pixbuf = renderer.RenderToPixbuf(region, null); if (pixbuf.Width > DEFAULT_WIDTH || pixbuf.Height > DEFAULT_HEIGHT) { int w, h; CalculateOptimalDimensions(pixbuf, out w, out h); pixbuf = pixbuf.ScaleSimple(w, h, InterpType.Tiles); } if (image == null) { image = new Gtk.Image(pixbuf); image.Show(); Add(image); } else { image.Pixbuf = pixbuf; } }
private void OnExposed(object o, ExposeEventArgs args) { Rectangle area = args.Event.Region.Clipbox; GdkWindow.DrawRectangle(Style.BackgroundGC(StateType.Normal), true, area.X, area.Y, area.Width, area.Height); if (field != null) { if (pixmap == null) { FieldRegion region; pixmap = new Pixmap(GdkWindow, (int)(field.Width * zoomLevel), (int)(field.Height * zoomLevel), -1); region = new FieldRegion(); region.Left = region.Bottom = 0; region.Right = field.Width - 1; region.Top = field.Height - 1; renderer.RenderToDrawable(pixmap, Style.BlackGC, region, null); } GdkWindow.DrawDrawable(Style.BlackGC, pixmap, area.X, area.Y, area.X, area.Y, area.Width, area.Height); if (selection != null) { renderer.RenderToDrawable(GdkWindow, Style.BlackGC, selection, selection); } } }
private void OnFieldDimensionChanged(Field field) { pixmap = null; selection = null; creatingSelection = false; QueueDraw(); }
public static FieldRegion ScreenSelectionToFieldRegion( Field field, uint zoomLevel, int x, int y, int width, int height ) { FieldRegion s = new FieldRegion(); s.Left = (uint) Math.Max(0, x / zoomLevel); s.Right = (uint) Math.Min(field.Width, (x + width) / zoomLevel); s.Top = (uint) Math.Max(0, field.Height - (y / zoomLevel) - 1); s.Bottom = (uint) Math.Min(field.Height, field.Height - ((y + height) / zoomLevel) - 1); return s; }
public static FieldRegion ScreenSelectionToFieldRegion( Field field, uint zoomLevel, int x, int y, int width, int height ) { FieldRegion s = new FieldRegion(); s.Left = (uint)Math.Max(0, x / zoomLevel); s.Right = (uint)Math.Min(field.Width, (x + width) / zoomLevel); s.Top = (uint)Math.Max(0, field.Height - (y / zoomLevel) - 1); s.Bottom = (uint)Math.Min(field.Height, field.Height - ((y + height) / zoomLevel) - 1); return(s); }
/** * Render a part of a field to a drawable. * * @param drawable The drawable to render to. * @param gc The GC to use when rendering. This GC is not * actually used for anything visible, so any GC will do. * @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 void RenderToDrawable(Drawable drawable, Gdk.GC gc, FieldRegion region, FieldRegion selection) { using (Pixbuf pixbuf = RenderToPixbuf(region, selection)) { Point screenLeftTop; screenLeftTop.X = (int) region.Left; screenLeftTop.Y = (int) region.Top; Calc.FieldPosToScreenPos(ref screenLeftTop, field, zoomLevel); pixbuf.RenderToDrawable(drawable, gc, 0, 0, screenLeftTop.X, screenLeftTop.Y, (int) (region.Width * zoomLevel), (int) (region.Height * zoomLevel), RgbDither.Normal, 0, 0); } }
private void OnFieldSelectionChanged(FieldView sender, FieldRegion selection) { if (selection != null) { selectedBlockType.Sensitive = true; if (selection.Left != selection.Right || selection.Top != selection.Bottom) { // More than 1 block has been selected. selectedCoord.Text = String.Format("({0}, {1}) - ({2}, {3})", selection.Left, selection.Top, selection.Right, selection.Bottom); BlockType type = sender.Field.GetBlock(selection.Left, selection.Top); bool same = true; for (uint x = selection.Left; x <= selection.Right && same; x++) { for (uint y = selection.Bottom; y <= selection.Top && same; y++) { same = sender.Field.GetBlock(x, y) == type; } } selectedBlockTypeChanging = true; ShowMixedType(!same, !same); if (same) { selectedBlockType.Active = (int)type; } selectedBlockTypeChanging = false; } else { // Only 1 block is selected. selectedCoord.Text = String.Format("({0}, {1})", selection.Left, selection.Top); selectedBlockTypeChanging = true; ShowMixedType(false, false); selectedBlockType.Active = (int)sender.Field.GetBlock(selection.Left, selection.Top); selectedBlockTypeChanging = false; } } else { selectedCoord.Text = "-"; selectedBlockType.Sensitive = false; } }
/** * Mark a region of the field for redrawing. * * @require field != null && region != null */ private void RedrawFieldRegion(FieldRegion region) { FieldPoint begin; begin.X = region.Left; begin.Y = region.Top; Calc.FieldPosToScreenPos(ref begin, field, zoomLevel); FieldPoint end; end.X = region.Right + 1; end.Y = region.Bottom - 1; Calc.FieldPosToScreenPos(ref end, field, zoomLevel); QueueDrawArea((int)begin.X, (int)begin.Y, (int)(end.X - begin.X), (int)(end.Y - begin.Y)); }
protected void OnSelectedBlockTypeChanged(object o, EventArgs args) { int len = Enum.GetValues(((Enum)BlockType.Walkable).GetType()).Length; // Make sure we do nothing when user selected "(Mixed)", // or when the combo boxed is being automaticallychanged. if (selectedBlockType.Active < len && !selectedBlockTypeChanging) { BlockType type = (BlockType)selectedBlockType.Active; FieldRegion selection = fieldView.Selection; fieldView.Field.BeginUpdate(); for (uint x = selection.Left; x <= selection.Right; x++) { for (uint y = selection.Bottom; y <= selection.Top; y++) { fieldView.Field.SetBlock(x, y, type); } } fieldView.Field.EndUpdate(); //OnFieldSelectionChanged(fieldView, selection); } }
private void OnButtonPress(object o, ButtonPressEventArgs args) { if (args.Event.Button == 1 && field != null) { // Create a new selection. creatingSelection = true; if (selection != null) { RedrawFieldRegion(selection); } FieldPoint p; p.X = (uint)Math.Max(0, args.Event.X); p.Y = (uint)Math.Max(0, args.Event.Y); Calc.ScreenPosToFieldPos(ref p, field, zoomLevel); selection = new FieldRegion(); selection.SetBeginPoint(p.X, p.Y); RedrawFieldRegion(selection); if (OnSelectionChanged != null) { OnSelectionChanged(this, selection); } } else if (args.Event.Button == 3 && field != null && selection != null) { // Clear the selection and re-render the previously selected part. RedrawFieldRegion(selection); creatingSelection = false; selection = null; if (OnSelectionChanged != null) { OnSelectionChanged(this, null); } } }
private void OnExposed(object o, ExposeEventArgs args) { Rectangle area = args.Event.Region.Clipbox; GdkWindow.DrawRectangle(Style.BackgroundGC(StateType.Normal), true, area.X, area.Y, area.Width, area.Height); if (field != null) { if (pixmap == null) { FieldRegion region; pixmap = new Pixmap(GdkWindow, (int) (field.Width * zoomLevel), (int) (field.Height * zoomLevel), -1); region = new FieldRegion(); region.Left = region.Bottom = 0; region.Right = field.Width - 1; region.Top = field.Height - 1; renderer.RenderToDrawable(pixmap, Style.BlackGC, region, null); } GdkWindow.DrawDrawable(Style.BlackGC, pixmap, area.X, area.Y, area.X, area.Y, area.Width, area.Height); if (selection != null) { renderer.RenderToDrawable(GdkWindow, Style.BlackGC, selection, selection); } } }
/** * 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); }
private bool WithinSelection(FieldRegion selection, uint x, uint y) { return x >= selection.Left && x <= selection.Right && y >= selection.Bottom && y <= selection.Top; }
private void OnFieldSelectionChanged(FieldView sender, FieldRegion selection) { if (selection != null) { selectedBlockType.Sensitive = true; if (selection.Left != selection.Right || selection.Top != selection.Bottom) { // More than 1 block has been selected. selectedCoord.Text = String.Format("({0}, {1}) - ({2}, {3})", selection.Left, selection.Top, selection.Right, selection.Bottom); BlockType type = sender.Field.GetBlock(selection.Left, selection.Top); bool same = true; for (uint x = selection.Left; x <= selection.Right && same; x++) { for (uint y = selection.Bottom; y <= selection.Top && same; y++) { same = sender.Field.GetBlock(x, y) == type; } } selectedBlockTypeChanging = true; ShowMixedType(!same, !same); if (same) { selectedBlockType.Active = (int) type; } selectedBlockTypeChanging = false; } else { // Only 1 block is selected. selectedCoord.Text = String.Format("({0}, {1})", selection.Left, selection.Top); selectedBlockTypeChanging = true; ShowMixedType(false, false); selectedBlockType.Active = (int) sender.Field.GetBlock(selection.Left, selection.Top); selectedBlockTypeChanging = false; } } else { selectedCoord.Text = "-"; selectedBlockType.Sensitive = false; } }
/** * Mark a region of the field for redrawing. * * @require field != null && region != null */ private void RedrawFieldRegion(FieldRegion region) { FieldPoint begin; begin.X = region.Left; begin.Y = region.Top; Calc.FieldPosToScreenPos(ref begin, field, zoomLevel); FieldPoint end; end.X = region.Right + 1; end.Y = region.Bottom - 1; Calc.FieldPosToScreenPos(ref end, field, zoomLevel); QueueDrawArea((int) begin.X, (int) begin.Y, (int) (end.X - begin.X), (int) (end.Y - begin.Y)); }
private void OnButtonPress(object o, ButtonPressEventArgs args) { if (args.Event.Button == 1 && field != null) { // Create a new selection. creatingSelection = true; if (selection != null) { RedrawFieldRegion(selection); } FieldPoint p; p.X = (uint) Math.Max(0, args.Event.X); p.Y = (uint) Math.Max(0, args.Event.Y); Calc.ScreenPosToFieldPos(ref p, field, zoomLevel); selection = new FieldRegion(); selection.SetBeginPoint(p.X, p.Y); RedrawFieldRegion(selection); if (OnSelectionChanged != null) { OnSelectionChanged(this, selection); } } else if (args.Event.Button == 3 && field != null && selection != null) { // Clear the selection and re-render the previously selected part. RedrawFieldRegion(selection); creatingSelection = false; selection = null; if (OnSelectionChanged != null) { OnSelectionChanged(this, null); } } }
private void OnFieldDimensionChanged(Field field) { pixmap = null; selection = null; creatingSelection = false; QueueDraw(); }