public bool IsValid(PointInt32 point)
        {
            if (_selectionVisibility != Visibility.Visible)
                return true;

            return (Rectangle.Contains(point));
        }
Example #2
0
 public Chest(PointInt32 location)
     : this()
 {
     _Location = location;
     for (int i = 0; i < MaxItems; i++)
     {
         _Items.Add(new Item(0,"[empty]"));
     }
 }
 public void SetRectangle(PointInt32 p1, PointInt32 p2)
 {
     int x = p1.X < p2.X ? p1.X : p2.X;
     int y = p1.Y < p2.Y ? p1.Y : p2.Y;
     int width = Math.Abs(p2.X - p1.X) + 1;
     int height = Math.Abs(p2.Y - p1.Y) + 1;
     Rectangle = new Int32Rect(x, y, width, height);
     SelectionVisibility = Visibility.Visible;
 }
        private RectI TileViewportRect()
        {
            var vm = (WorldViewModel)DataContext;
            var partView = (ScrollViewer)FindName("WorldScrollViewer");

            var xy   = new PointInt32((int)(partView.HorizontalOffset / vm.Zoom), (int)(partView.VerticalOffset / vm.Zoom));
            var size = new SizeInt32 ((int)(partView.ActualWidth      / vm.Zoom), (int)(partView.ActualHeight   / vm.Zoom));
            return new RectI(xy, size);
        }
        public static IEnumerable<PointInt32> DrawLine(PointInt32 begin, PointInt32 end)
        {
            int y0 = begin.Y;
            int x0 = begin.X;
            int y1 = end.Y;
            int x1 = end.X;

            // Bresenham's Line Algorithm - Pure int version (very fast and compact)
            // http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm#Simplification
            /*
            int dx  = Math.Abs(x1 - x0);
            int dy  = Math.Abs(y1 - y0);
            int sx  = x0 < x1 ? 1 : -1;
            int sy  = y0 < y1 ? 1 : -1;
            int err = dx - dy;

            yield return new PointInt32(x0, y0);
            while (x0 != x1 || y0 != y1) {
                int e2 = err * 2;
                if (e2 > -dy) {
                    err -= dy;
                    x0  += sx;
                }
                if (e2 <  dx) {
                    err += dx;
                    y0  += sy;
                }
                yield return new PointInt32(x0, y0);
            }
            */

            // EFLA Variation E - (even faster)
            // http://www.simppa.fi/blog/extremely-fast-line-algorithm-as3-optimized/
            // (AS3 version with Abs re-added, thus simplifying the bit shifting from EFLA-E)
            int shortLen = y1 - y0;
            int longLen  = x1 - x0;
            bool yLonger = false;

            if (Math.Abs(shortLen) > Math.Abs(longLen)) {
                shortLen ^= longLen;
                longLen  ^= shortLen;
                shortLen ^= longLen;
                yLonger = true;
            }

            int inc = longLen < 0 ? -1 : 1;
            // using double here for accuracy, since some lines from one end of a world to another could get quite long...
            double multDiff = longLen == 0 ? shortLen : (double)shortLen / (double)longLen;

            if (yLonger) {
                for (int i = 0; i != longLen + inc; i += inc) { yield return new PointInt32(x0 + (int)(i * multDiff), y0 + i); }
            }
            else {
                for (int i = 0; i != longLen + inc; i += inc) { yield return new PointInt32(x0 + i, y0 + (int)(i * multDiff)); }
            }
        }
 private void ScrollToTile(PointInt32 tile)
 {
     var vm = (WorldViewModel)DataContext;
     double zoom = vm.Zoom;
     var partView = (ScrollViewer)FindName("WorldScrollViewer");
     if (partView != null)
     {
         partView.ScrollToHorizontalOffset((tile.X * zoom) - (partView.ActualWidth / 2.0));
         partView.ScrollToVerticalOffset((tile.Y * zoom) - (partView.ActualHeight / 2.0));
     }
 }
        public static bool Contains(this Int32Rect rect, PointInt32 point)
        {
            int xabs = (point.X - rect.X);
            int yabs = (point.Y - rect.Y);

            if (xabs < 0)
                return false;

            if (yabs < 0)
                return false;

            return xabs < rect.Width && yabs < rect.Height;
        }
Example #8
0
 public void SetData(BytePixels src, PointInt32 xy)
 {
     src.PutData(null, this, xy);
 }
 public void UpdateWorldImage(PointInt32 loc, bool isRenderedLayer = false)
 {
     UpdateWorldImage(new RectI(loc, loc), isRenderedLayer, null);
 }
Example #10
0
 private void PasteClipboard(PointInt32 anchor)
 {
     _clipboardMan.PasteBufferIntoWorld(_world, _clipboardMan.Buffer, anchor);
     _renderer.UpdateWorldImage(new Int32Rect(anchor.X, anchor.Y, _clipboardMan.Buffer.Size.W, _clipboardMan.Buffer.Size.H));
 }
Example #11
0
        private void CheckDirectionandDraw(TileMouseEventArgs e)
        {
            PointInt32 p = e.Tile;
            // Free range draw only
            /* if (_isLeftDown && !_isRightDown) {
                DrawLine(p);
                _startPoint = p;
            } */

            if (_isRightDown)
            {
                if (_isLeftDown)
                    p.X = _startPoint.X;
                else
                    p.Y = _startPoint.Y;

                DrawLine(p);
                _startPoint = p;
            }
            else if (_isLeftDown)
            {
                DrawLine(p);
                _startPoint = p;
            }
        }
Example #12
0
 public void SetPixel(PointInt32 xy, Color c)
 {
     SetPixel(Size.W * xy.Y + xy.X, c);
 }
Example #13
0
 public void SetData(RectI? rect, byte[] src, int width, int height, PointInt32 xy)
 {
     var bp = new BytePixels(width, height, src);
     bp.PutData(rect, this, xy);
 }
Example #14
0
 public byte[] GetPixel(PointInt32 xy)
 {
     return(GetPixel(Size.W * xy.Y + xy.X));
 }
Example #15
0
 public Color GetColor(PointInt32 xy)
 {
     return(GetColor(Size.W * xy.Y + xy.X));
 }
Example #16
0
 public void SetPixel(PointInt32 xy, byte[] b)
 {
     SetPixel(Size.W * xy.Y + xy.X, b);
 }
Example #17
0
 public bool Equals(PointInt32 p)
 {
     return(MatchFields(this, p));
 }
Example #18
0
 private static bool MatchFields(PointInt32 a, PointInt32 m)
 {
     return(a.X == m.X && a.Y == m.Y);
 }
Example #19
0
 public PointInt32(PointInt32 p)
 {
     _x = p.X;
     _y = p.Y;
 }
Example #20
0
 public void SetData(RectI?rect, BytePixels src, PointInt32 xy)
 {
     src.PutData(rect, this, xy);
 }
Example #21
0
 public void PutData(BytePixels dest, PointInt32 xy)
 {
     PutData(null, dest, xy);
 }
Example #22
0
 public void SetData(RectI? rect, BytePixels src, PointInt32 xy)
 {
     src.PutData(rect, this, xy);
 }
Example #23
0
 public RectI(int left, int right, int top, int bottom)
 {
     _topLeft = new PointInt32(left, top);
     _bottomRight = new PointInt32(right, bottom);
 }
Example #24
0
 public void SetPixel(PointInt32 xy, byte[] b)
 {
     SetPixel(Size.W * xy.Y + xy.X, b);
 }
Example #25
0
        public override bool MoveTool(TileMouseEventArgs e)
        {
            WriteableBitmap bmp;

            if (_startPoint != null) _endPoint = e.Tile;
            CheckDirectionandDraw(e);
            ToolAnchorMode mode = ToolAnchorMode.Center;

            // Line draw preview
            if (_isRightDown && _startPoint != null && _endPoint != null) {
                var sp = (PointInt32)_startPoint;
                var ep = (PointInt32)_endPoint;
                var delta = ep - sp;
                var rect = new RectI(new PointInt32(), new SizeInt32(Math.Abs(delta.X) + 1, Math.Abs(delta.Y) + 1));

                // figure out exactly which PreviewMode
                mode = 0;
                if      (delta.X <  0) mode += (int)ToolAnchorModeParts.Left;
                else if (delta.X == 0) mode += (int)ToolAnchorModeParts.Center;
                else if (delta.X >  0) mode += (int)ToolAnchorModeParts.Right;

                if      (delta.Y <  0) mode += (int)ToolAnchorModeParts.Top;
                else if (delta.Y == 0) mode += (int)ToolAnchorModeParts.Middle;
                else if (delta.Y >  0) mode += (int)ToolAnchorModeParts.Bottom;

                // which direction to draw the line
                var linePnts = new PointInt32[2];
                switch (mode) {
                    case ToolAnchorMode.TopLeft:     linePnts = new[] { rect.BottomRight, rect.TopLeft }; break;
                    case ToolAnchorMode.TopRight:    linePnts = new[] { rect.BottomLeft, rect.TopRight }; break;
                    case ToolAnchorMode.BottomLeft:  linePnts = new[] { rect.TopRight, rect.BottomLeft }; break;
                    case ToolAnchorMode.BottomRight: linePnts = new[] { rect.TopLeft, rect.BottomRight }; break;
                    default:  // has middle or center, order doesn't matter
                        linePnts = new[] { rect.TopLeft, rect.BottomRight };
                        break;
                }

                bmp = new WriteableBitmap(
                    rect.W,
                    rect.H,
                    96,
                    96,
                    System.Windows.Media.PixelFormats.Bgra32,
                    null);

                bmp.Clear();
                foreach (PointInt32 p in WorldRenderer.DrawLine(linePnts[0], linePnts[1])) {
                    if (_selection.IsValid(p)) bmp.SetPixel(p.X, p.Y, previewColor);
                }
            }
            // Single dot
            else {
                bmp = new WriteableBitmap(
                    1,
                    1,
                    96,
                    96,
                    System.Windows.Media.PixelFormats.Bgra32,
                    null);

                bmp.Clear();
                bmp.SetPixel(0, 0, previewColor);
            }

            _properties.Image = bmp;
            _properties.PreviewMode = mode;

            return false;
        }
Example #26
0
        public override bool PressTool(TileMouseEventArgs e)
        {
            tilesChecked = new bool[_world.Header.WorldBounds.Total];

            if (!_isRightDown && !_isLeftDown)
                _startPoint = e.Tile;

            if ((_properties.Height > 0 && _properties.Width > 0) &&
                (_lastUsedSize.Width != _properties.Width || _lastUsedSize.Height != _properties.Height))
                _lastUsedSize = new SizeInt32(_properties.Width, _properties.Height);

            CheckDirectionandDraw(e);
            _isLeftDown = (e.LeftButton == MouseButtonState.Pressed);
            _isRightDown = (e.RightButton == MouseButtonState.Pressed);
            return true;
        }
Example #27
0
 public Sign(string text, PointInt32 location)
 {
     _Text = text;
     _Location = location;
 }
Example #28
0
        private void DrawLine(PointInt32 endPoint)
        {
            foreach (PointInt32 p in WorldRenderer.DrawLine(_startPoint, endPoint))
            {
                if (_selection.SelectionVisibility == Visibility.Visible)
                {
                    // if selection is active, and point is not inside, skip point
                    if (!_selection.Rectangle.Contains(p))
                        continue;
                }

                // center
                int x0 = p.X - _properties.Offset.X;
                int y0 = p.Y - _properties.Offset.Y;

                if (_properties.BrushShape == ToolBrushShape.Square)
                {

                    if (_properties.IsOutline)
                    {
                        TilePicker outline = Utility.DeepCopy(_tilePicker);
                        outline.Wall.IsActive = false;

                        // eraise a center section
                        TilePicker eraser = Utility.DeepCopy(_tilePicker);
                        eraser.IsEraser = true;
                        eraser.Wall.IsActive = false; // don't erase the wall for the interrior

                        TilePicker wall = Utility.DeepCopy(_tilePicker);
                        wall.Tile.IsActive = false;

                        var interior = new Int32Rect(x0 + _properties.OutlineThickness,
                                                     y0 + _properties.OutlineThickness,
                                                     _properties.Width - (_properties.OutlineThickness * 2),
                                                     _properties.Height - (_properties.OutlineThickness * 2));

                        // Erase center
                        FillRectangle(interior, ref eraser);
                        // Fill center
                        if (wall.Wall.IsActive)
                            FillRectangle(interior, ref wall);
                        // Draw outline
                        if (outline.Tile.IsActive)
                        {
                            for (int i = 0; i < _properties.OutlineThickness; i++)
                            {
                                DrawRectangle(new Int32Rect(x0 + i, y0 + i, _properties.Width - (i * 2) - 1, _properties.Height - (i * 2) - 1), ref outline);
                            }
                        }
                    }
                    else
                    {
                        FillRectangle(new Int32Rect(x0, y0, _properties.Width, _properties.Height), ref _tilePicker);
                    }
                }
                else if (_properties.BrushShape == ToolBrushShape.Round)
                {

                    if (_properties.IsOutline)
                    {
                        TilePicker outline = Utility.DeepCopy(_tilePicker);
                        outline.Wall.IsActive = false;

                        // eraise a center section
                        TilePicker eraser = Utility.DeepCopy(_tilePicker);
                        eraser.IsEraser = true;
                        eraser.Wall.IsActive = false; // don't erase the wall for the interrior

                        TilePicker wall = Utility.DeepCopy(_tilePicker);
                        wall.Tile.IsActive = false;

                        // Draw outline
                        if (outline.Tile.IsActive)
                        {
                            FillEllipse(x0, y0, x0 + _properties.Width, y0 + _properties.Height, ref outline);
                        }

                        // Erase center
                        FillEllipse(x0 + _properties.OutlineThickness,
                                           y0 + _properties.OutlineThickness,
                                           x0 + _properties.Width - _properties.OutlineThickness,
                                           y0 + _properties.Height - _properties.OutlineThickness, ref eraser);
                        // Fill center
                        if (wall.Wall.IsActive)
                        {
                            FillEllipse(x0 + _properties.OutlineThickness,
                                           y0 + _properties.OutlineThickness,
                                           x0 + _properties.Width - _properties.OutlineThickness,
                                           y0 + _properties.Height - _properties.OutlineThickness, ref wall);
                        }

                        eraser = null;
                    }
                    else
                    {
                        FillEllipse(x0, y0, x0 + _properties.Width, y0 + _properties.Height, ref _tilePicker);
                    }
                }
                if (_properties.IsOutline)
                    _renderer.UpdateWorldImage(new Int32Rect(x0, y0, _properties.Width + 1, _properties.Height + 1));
            }
        }
Example #29
0
        private void PlaceBrownChest(PointInt32 p)
        {
            // anchor at top-right of chest
            int x = p.X;
            int y = p.Y;

            // Validate free space
            if (!_world.Tiles[x, y].IsActive && !_world.Tiles[x + 1, y].IsActive && !_world.Tiles[x, y + 1].IsActive && !_world.Tiles[x + 1, y + 1].IsActive)
            {
                // Validate floor    WorldSettings.Tiles[].IsSolid
                if ((_world.Tiles[x, y + 2].IsActive && (WorldSettings.Tiles[_world.Tiles[x, y + 2].Type].IsSolid)) &&
                    (_world.Tiles[x + 1, y + 2].IsActive && (WorldSettings.Tiles[_world.Tiles[x + 1, y + 2].Type].IsSolid)))
                {
                    // Place Chest
                    var tp = new TilePicker();
                    SetTileSprite(new PointInt32(x, y), new PointShort(0, 0), 21);
                    SetTileSprite(new PointInt32(x, y + 1), new PointShort(0, 18), 21);
                    SetTileSprite(new PointInt32(x + 1, y), new PointShort(18, 0), 21);
                    SetTileSprite(new PointInt32(x + 1, y + 1), new PointShort(18, 18), 21);
                }
            }
        }
Example #30
0
 private void SetTileSprite(PointInt32 point, PointShort frame, byte type)
 {
     var curTile = _world.Tiles[point.X, point.Y];
     if (curTile != null)
     {
         curTile.IsActive = true;
         curTile.Type = type;
         curTile.Frame = frame;
         _renderer.UpdateWorldImage(point);
     }
 }
Example #31
0
 public RectI(PointInt32 topLeft, SizeInt32 size)
 {
     _topLeft     = topLeft;
     _bottomRight = new PointInt32(size.Width + topLeft.X - 1, size.Height + topLeft.Y - 1);
 }
Example #32
0
        private void DrawLine(PointInt32 endPoint)
        {
            var startPoint = (PointInt32)_startPoint;
            foreach (PointInt32 p in WorldRenderer.DrawLine(startPoint, endPoint))
            {
                if (_selection.IsValid(p))
                {
                    int x = p.X;
                    int y = p.Y;
                    if (HistMan.SaveHistory)
                        HistMan.AddTileToBuffer(x, y, ref _world.Tiles[x, y]);
                    _world.SetTileXY(ref x, ref y, ref _tilePicker, ref _selection);
                    _renderer.UpdateWorldImage(p);
                }
            }

            // Make sure the actual corners are defined
            /* if (_selection.IsValid(startPoint) bmp.SetPixel(startPoint.X, startPoint.Y, previewColor);
            if (_selection.IsValid(endPoint)   bmp.SetPixel(endPoint.X, endPoint.Y, previewColor); */
        }
Example #33
0
 public void SetPixel(PointInt32 xy, Color c)
 {
     SetPixel(Size.W * xy.Y + xy.X, c);
 }
Example #34
0
 public override bool PressTool(TileMouseEventArgs e)
 {
     if (e.LeftButton == MouseButtonState.Pressed)
         _startselection = e.Tile;
     if (e.RightButton == MouseButtonState.Pressed && e.LeftButton == MouseButtonState.Released)
     {
         _selection.Deactive();
     }
     return true;
 }
Example #35
0
 public Color GetColor(PointInt32 xy)
 {
     return GetColor(Size.W * xy.Y + xy.X);
 }
Example #36
0
        private void PlaceSprite(PointInt32 location, byte type, PointShort size, PointShort upperLeft = new PointShort())
        {
            for (int x = 0; x < size.X; x++)
            {
                for (int y = 0; y < size.Y; y++)
                {
                    var curTile = _world.Tiles[location.X + x, location.Y + y];
                    curTile.IsActive = true;
                    curTile.Type = type;
                    curTile.Frame = new PointShort((short)(upperLeft.X + (x * 18)), (short)(upperLeft.Y + (y * 18)));
                    _renderer.UpdateWorldImage(new PointInt32(location.X + x, location.Y + y));
                }
            }

            if (type == 21)
                _world.Chests.Add(new Chest { Location = location });
            else if (type == 55 || type == 85)
                _world.Signs.Add(new Sign{Location =  location});
        }
Example #37
0
 public byte[] GetPixel(PointInt32 xy)
 {
     return GetPixel(Size.W * xy.Y + xy.X);
 }
Example #38
0
 public RectI(RectI rect)
 {
     _topLeft     = new PointInt32(rect.TopLeft);
     _bottomRight = new PointInt32(rect.BottomRight);
 }
Example #39
0
 public void PutData(BytePixels dest, PointInt32 xy)
 {
     PutData(null, dest, xy);
 }
        private void ViewportMouseWheel(object sender, MouseWheelEventArgs e)
        {
            var cargs = new TileMouseEventArgs
                            {
                                Tile = GetTileAtPixel(e.GetPosition((IInputElement)sender)),
                                LeftButton = e.LeftButton,
                                RightButton = e.RightButton,
                                MiddleButton = e.MiddleButton,
                                WheelDelta = e.Delta
                            };

            var vm = (WorldViewModel)DataContext;
            var partView = (ScrollViewer)FindName("WorldScrollViewer");

            double initialZoom = vm.Zoom;
            var initialScrollPosition = new Point(partView.HorizontalOffset, partView.VerticalOffset);
            var initialCenterTile =
                new PointInt32((int)(partView.HorizontalOffset / initialZoom + (partView.ActualWidth / 2) / initialZoom),
                               (int)(partView.VerticalOffset / initialZoom + (partView.ActualHeight / 2) / initialZoom));

            if (vm.MouseWheelCommand.CanExecute(cargs))
                vm.MouseWheelCommand.Execute(cargs);

            double finalZoom = vm.Zoom;
            //var finalScrollPosition = new Point(partView.HorizontalOffset, partView.VerticalOffset);
            double zoomRatio = 1 - finalZoom / initialZoom;
            var scaleCenterTile = new PointInt32(
                (int)(initialCenterTile.X - ((cargs.Tile.X - initialCenterTile.X) * zoomRatio)),
                (int)(initialCenterTile.Y - ((cargs.Tile.Y - initialCenterTile.Y) * zoomRatio)));
            ScrollToTile(scaleCenterTile);

            ViewportRerender(sender, null);  // scroll/zoom change needs a re-render
        }
Example #41
0
        /// <summary>Puts a copy of 2D pixel data in a BytePixels object, specifying a source rectangle and a destination X,Y point.</summary>
        /// <param name="rect">The section of the pixel data to copy. <paramref name="null"/> indicates the data will be copied from the entire object.</param>
        /// <param name="dest">The destination BytePixels object.</param>
        /// <param name="xy">The top-left corner of the destination.</param>
        public void PutData(RectI? rect, BytePixels dest, PointInt32 xy)
        {
            RectI r;
            if (rect != null) r = (RectI)rect;
            else              r = new RectI(new PointInt32(), this.Size);

            var sOfs = (r.Y * this.Size.W) + r.X;
            sOfs *= Bpp;

            var dOfs = (xy.Y * dest.Size.W) + xy.X;
            dOfs *= Bpp;  // note: we're not factoring for Bpp mismatches here...

            var dataLeft = r.Size.Total * Bpp;
            for (int y = 0; y < r.Height; y++)
            {
                Array.Copy(_data, sOfs, dest.Data, dOfs, r.Width * Bpp);
                sOfs += this.Size.W * Bpp;
                dOfs += dest.Size.W * Bpp;
            }
        }
Example #42
0
 public void SetData(BytePixels src, PointInt32 xy)
 {
     src.PutData(null, this, xy);
 }
Example #43
0
        public void SetData(RectI?rect, byte[] src, int width, int height, PointInt32 xy)
        {
            var bp = new BytePixels(width, height, src);

            bp.PutData(rect, this, xy);
        }