Example #1
0
        public void ChangeSize(int newWidth, int newHeight, bool anchorLeft, bool anchorTop)
        {
            TileTemplate[] newUpperTiles = new TileTemplate[newWidth * newHeight];
            TileTemplate[] newLowerTiles = new TileTemplate[newWidth * newHeight];

            TileTemplate[] source, target;
            int            oldWidth  = this.Width;
            int            oldHeight = this.Height;

            foreach (bool isUpper in new bool[] { true, false })
            {
                source = isUpper ? this.TilesUpper : this.TilesLower;
                target = isUpper ? newUpperTiles : newLowerTiles;
                int offsetX = anchorLeft ? 0 : newWidth - this.Width;
                int offsetY = anchorTop ? 0 : newHeight - this.Height;
                int tx, ty;
                for (int y = 0; y < this.Height; ++y)
                {
                    ty = y + offsetY;
                    for (int x = 0; x < this.Width; ++x)
                    {
                        tx = x + offsetX;
                        if (tx < 0 || tx >= newWidth || ty < 0 || ty >= newHeight)
                        {
                        }
                        else
                        {
                            target[ty * newWidth + tx] = source[y * oldWidth + x];
                        }
                    }
                }
            }

            this.Width  = newWidth;
            this.Height = newHeight;

            this.TilesLower = newLowerTiles;
            this.TilesUpper = newUpperTiles;

            this.IsDirty = true;
            MainWindow.Instance.UpdateTitle();
            MainWindow.Instance.InvalidateDrawing();
        }
 public void SetActiveTile(TileTemplate template)
 {
     this.ActiveTile = template;
 }
        public void InvalidateDrawing()
        {
            int xStart = (this.drawBeginPixelX - this.cameraX) >> 4;
            int xEnd   = (this.drawEndPixelX - this.cameraX) >> 4;
            int yStart = (this.drawBeginPixelY - this.cameraY) >> 4;
            int yEnd   = (this.drawEndPixelY - this.cameraY) >> 4;

            int t;

            if (xEnd < xStart)
            {
                t      = xEnd;
                xEnd   = xStart;
                xStart = t;
            }

            if (yEnd < yStart)
            {
                t      = yEnd;
                yEnd   = yStart;
                yStart = t;
            }

            int width  = this.ActiveModel.Width;
            int height = this.ActiveModel.Height;

            if (xStart < 0)
            {
                xStart = 0;
            }
            if (yStart < 0)
            {
                yStart = 0;
            }
            if (xEnd >= width)
            {
                xEnd = width - 1;
            }
            if (yEnd >= height)
            {
                yEnd = height - 1;
            }

            if (drawThis[0] == null || drawThis[0].Length != width * height)
            {
                drawThis[0] = new TileTemplate[width * height];
                drawThis[1] = new TileTemplate[width * height];
            }

            for (int i = width * height - 1; i >= 0; --i)
            {
                drawThis[0][i] = this.ActiveModel.TilesLower[i];
                drawThis[1][i] = this.ActiveModel.TilesUpper[i];
            }

            if (this.isMouseDown && !this.panMode)
            {
                TileTemplate[] layer = this.isUpperActive ? drawThis[1] : drawThis[0];
                for (int y = yStart; y <= yEnd; ++y)
                {
                    for (int x = xStart; x <= xEnd; ++x)
                    {
                        layer[y * width + x] = this.isEraseMode ? null : this.ActiveTile;
                    }
                }
            }

            this.RedrawTheWholeDamnThing();
        }
 public void Update(TileTemplate[] activePalette)
 {
     this.listbox.ItemsSource = activePalette;
     this.listbox.SelectionChanged += (sender, e) => { MainWindow.Instance.SetActiveTile(this.listbox.SelectedItem as TileTemplate); };
 }
Example #5
0
        public Model Parse()
        {
            Dictionary <string, string> values = new Dictionary <string, string>();

            string[] lines = System.IO.File.ReadAllText(this.filename).Replace("\r\n", "\n").Replace('\r', '\n').Split('\n');
            foreach (string line in lines)
            {
                string[] parts = line.Split(':');
                if (parts.Length >= 2)
                {
                    string key = parts[0];
                    if (key.Length > 0 && key[0] == '#')
                    {
                        key = key.Substring(1);
                        string value = parts[1];
                        for (int i = 2; i < parts.Length; ++i)
                        {
                            value += ':' + parts[i];
                        }

                        values[key] = value;
                    }
                }
            }

            int width  = int.Parse(values["width"]);
            int height = int.Parse(values["height"]);             // let it crash if this is wrong.

            Model model = new Model(width, height);

            TileTemplate[] topLayer    = new TileTemplate[width * height];
            TileTemplate[] bottomLayer = new TileTemplate[width * height];

            string[] tileIdsUpper = values["upper"].Split(',');
            string[] tileIdsLower = values["lower"].Split(',');

            for (int i = width * height - 1; i >= 0; --i)
            {
                string tileId = tileIdsUpper[i].Trim();
                if (tileId.Length > 0)
                {
                    topLayer[i] = this.tileLookup[tileId];
                }

                tileId = tileIdsLower[i].Trim();
                if (tileId.Length > 0)
                {
                    bottomLayer[i] = this.tileLookup[tileId];
                }
            }

            // TODO: other fields

            model.SetTiles(topLayer, true);
            model.SetTiles(bottomLayer, false);

            model.SetRawFileData(values);

            model.Path         = filename;
            model.IsSideScroll = !values.ContainsKey("view") || values["view"].Trim().ToLowerInvariant() != "over";

            return(model);
        }
        public Model Parse()
        {
            Dictionary<string, string> values = new Dictionary<string, string>();
            string[] lines = System.IO.File.ReadAllText(this.filename).Replace("\r\n", "\n").Replace('\r', '\n').Split('\n');
            foreach (string line in lines)
            {
                string[] parts = line.Split(':');
                if (parts.Length >= 2)
                {
                    string key = parts[0];
                    if (key.Length > 0 && key[0] == '#')
                    {
                        key = key.Substring(1);
                        string value = parts[1];
                        for (int i = 2; i < parts.Length; ++i)
                        {
                            value += ':' + parts[i];
                        }

                        values[key] = value;
                    }
                }
            }

            int width = int.Parse(values["width"]);
            int height = int.Parse(values["height"]); // let it crash if this is wrong.

            Model model = new Model(width, height);

            TileTemplate[] topLayer = new TileTemplate[width * height];
            TileTemplate[] bottomLayer = new TileTemplate[width * height];

            string[] tileIdsUpper = values["upper"].Split(',');
            string[] tileIdsLower = values["lower"].Split(',');

            for (int i = width * height - 1; i >= 0; --i)
            {
                string tileId = tileIdsUpper[i].Trim();
                if (tileId.Length > 0)
                {
                    topLayer[i] = this.tileLookup[tileId];
                }

                tileId = tileIdsLower[i].Trim();
                if (tileId.Length > 0)
                {
                    bottomLayer[i] = this.tileLookup[tileId];
                }
            }

            // TODO: other fields

            model.SetTiles(topLayer, true);
            model.SetTiles(bottomLayer, false);

            model.SetRawFileData(values);

            model.Path = filename;
            model.IsSideScroll = !values.ContainsKey("view") || values["view"].Trim().ToLowerInvariant() != "over";

            return model;
        }
 public void SetActiveTile(TileTemplate template)
 {
     this.ActiveTile = template;
 }
        public void InvalidateDrawing()
        {
            int xStart = (this.drawBeginPixelX - this.cameraX) >> 4;
            int xEnd = (this.drawEndPixelX - this.cameraX) >> 4;
            int yStart = (this.drawBeginPixelY - this.cameraY) >> 4;
            int yEnd = (this.drawEndPixelY - this.cameraY) >> 4;

            int t;

            if (xEnd < xStart)
            {
                t = xEnd;
                xEnd = xStart;
                xStart = t;
            }

            if (yEnd < yStart)
            {
                t = yEnd;
                yEnd = yStart;
                yStart = t;
            }

            int width = this.ActiveModel.Width;
            int height = this.ActiveModel.Height;

            if (xStart < 0) xStart = 0;
            if (yStart < 0) yStart = 0;
            if (xEnd >= width) xEnd = width - 1;
            if (yEnd >= height) yEnd = height - 1;

            if (drawThis[0] == null || drawThis[0].Length != width * height)
            {
                drawThis[0] = new TileTemplate[width * height];
                drawThis[1] = new TileTemplate[width * height];
            }

            for (int i = width * height - 1; i >= 0; --i)
            {
                drawThis[0][i] = this.ActiveModel.TilesLower[i];
                drawThis[1][i] = this.ActiveModel.TilesUpper[i];
            }

            if (this.isMouseDown && !this.panMode)
            {
                TileTemplate[] layer = this.isUpperActive ? drawThis[1] : drawThis[0];
                for (int y = yStart; y <= yEnd; ++y)
                {
                    for (int x = xStart; x <= xEnd; ++x)
                    {
                        layer[y * width + x] = this.isEraseMode ? null : this.ActiveTile;
                    }
                }
            }

            this.RedrawTheWholeDamnThing();
        }