public void TestUndo() { Map map1 = new Map(1); map1.Width = 100; map1.Height = 100; Tile[] tiles1 = new[] { new Tile { TileSetId = 1, TileId = 2 }, new Tile { TileSetId = 3, TileId = 4 }, new Tile { TileSetId = 5, TileId = 6 }, new Tile { TileSetId = 7, TileId = 8 }, }; Tile[] tiles2 = new[] { new Tile { TileSetId = 101, TileId = 102 }, new Tile { TileSetId = 103, TileId = 104 }, new Tile { TileSetId = 105, TileId = 106 }, new Tile { TileSetId = 107, TileId = 108 }, }; Command command1 = map1.CreateSettingTilesCommand(1, 2, 3, SelectedTiles.Picker(tiles1, 2, 2), 0, 1); Assert.AreEqual(new Tile { TileSetId = 0, TileId = 0 }, map1.GetTile(0, 0, 0)); Assert.AreEqual(new Tile { TileSetId = 0, TileId = 0 }, map1.GetTile(0, 1, 0)); Assert.AreEqual(new Tile { TileSetId = 0, TileId = 0 }, map1.GetTile(0, 0, 1)); Assert.AreEqual(new Tile { TileSetId = 0, TileId = 0 }, map1.GetTile(0, 1, 1)); command1.Do(); Assert.AreEqual(tiles1[2], map1.GetTile(1, 2, 3)); Assert.AreEqual(tiles1[3], map1.GetTile(1, 3, 3)); Assert.AreEqual(tiles1[0], map1.GetTile(1, 2, 4)); Assert.AreEqual(tiles1[1], map1.GetTile(1, 3, 4)); Command command2 = map1.CreateSettingTilesCommand(1, 2, 3, SelectedTiles.Picker(tiles2, 2, 2), 1, 0); command2.Do(); Assert.AreEqual(tiles2[1], map1.GetTile(1, 2, 3)); Assert.AreEqual(tiles2[0], map1.GetTile(1, 3, 3)); Assert.AreEqual(tiles2[3], map1.GetTile(1, 2, 4)); Assert.AreEqual(tiles2[2], map1.GetTile(1, 3, 4)); command2.Undo(); Assert.AreEqual(tiles1[2], map1.GetTile(1, 2, 3)); Assert.AreEqual(tiles1[3], map1.GetTile(1, 3, 3)); Assert.AreEqual(tiles1[0], map1.GetTile(1, 2, 4)); Assert.AreEqual(tiles1[1], map1.GetTile(1, 3, 4)); command1.Undo(); Assert.AreEqual(new Tile { TileSetId = 0, TileId = 0 }, map1.GetTile(0, 0, 0)); Assert.AreEqual(new Tile { TileSetId = 0, TileId = 0 }, map1.GetTile(0, 1, 0)); Assert.AreEqual(new Tile { TileSetId = 0, TileId = 0 }, map1.GetTile(0, 0, 1)); Assert.AreEqual(new Tile { TileSetId = 0, TileId = 0 }, map1.GetTile(0, 1, 1)); }
public void TestJson() { Map map1 = new Map(1); map1.Width = 100; map1.Height = 200; map1.CreateSettingTilesCommand(0, 1, 2, SelectedTiles.Single(new Tile { TileSetId = 3, TileId = 4 }), 0, 0).Do(); map1.CreateSettingTilesCommand(1, 5, 6, SelectedTiles.Single(new Tile { TileSetId = 7, TileId = 8 }), 0, 0).Do(); JToken token = map1.ToJson(); Assert.AreEqual(map1.Width, token["Width"].Value<int>()); Assert.AreEqual(map1.Height, token["Height"].Value<int>()); Assert.AreEqual(2, token["Tiles"].Count()); byte[] bytes1 = Convert.FromBase64String(token["Tiles"][0].Value<string>()); Assert.AreEqual(map1.Width * map1.Height * 4, bytes1.Length); Assert.AreEqual(3, bytes1[(1 + 2 * map1.Width) * 4]); Assert.AreEqual(0, bytes1[(1 + 2 * map1.Width) * 4 + 1]); Assert.AreEqual(4, bytes1[(1 + 2 * map1.Width) * 4 + 2]); Assert.AreEqual(0, bytes1[(1 + 2 * map1.Width) * 4 + 3]); byte[] bytes2 = Convert.FromBase64String(token["Tiles"][1].Value<string>()); Assert.AreEqual(map1.Width * map1.Height * 4, bytes2.Length); Assert.AreEqual(7, bytes2[(5 + 6 * map1.Width) * 4]); Assert.AreEqual(0, bytes2[(5 + 6 * map1.Width) * 4 + 1]); Assert.AreEqual(8, bytes2[(5 + 6 * map1.Width) * 4 + 2]); Assert.AreEqual(0, bytes2[(5 + 6 * map1.Width) * 4 + 3]); Map map2 = new Map(2); Assert.AreEqual(Map.MinWidth, map2.Width); Assert.AreEqual(Map.MinHeight, map2.Height); map2.LoadJson(token); Assert.AreEqual(map1.Width, map2.Width); Assert.AreEqual(map1.Height, map2.Height); Assert.AreEqual(map1.GetTile(0, 0, 0), map2.GetTile(0, 0, 0)); Assert.AreEqual(map1.GetTile(0, 1, 2), map2.GetTile(0, 1, 2)); Assert.AreEqual(map1.GetTile(1, 5, 6), map2.GetTile(1, 5, 6)); }
public Node(int id, Map map, bool isExpanded) { this.Id = id; this.Map = map; this.IsExpanded = isExpanded; this.Children = new List<Node>(); }
private void AddNodeFromJson(Node parentNode, JObject json) { int id = json.Value<int>("Id"); Map map = new Map(id); map.LoadJson(json["Map"]); Node node = new Node(id, map, json.Value<bool>("IsExpanded")); parentNode.Children.Add(node); node.Parent = parentNode; foreach (JObject childJson in json["Children"]) { this.AddNodeFromJson(node, childJson); } }
public bool TryGetMap(int id, out Map map) { map = null; Node node; if (this.TryGetNode(id, out node) && node.Parent != null) { map = node.Map; } return map != null; }
public void AdjustScrollBars(EditorState editorState, Map map, int gridSize) { if (map != null) { Point offset = editorState.GetMapOffset(map.Id); int hMax = map.Width * gridSize - this.HScrollBar.Width; if (0 < hMax) { this.HScrollBar.Enabled = true; this.HScrollBar.Minimum = 0; this.HScrollBar.Maximum = hMax + this.HScrollBar.Width - 1; this.HScrollBar.SmallChange = 32; // TODO this.HScrollBar.LargeChange = this.HScrollBar.Width; Debug.Assert(this.HScrollBar.LargeChange == this.HScrollBar.Width); this.HScrollBar.Value = Math.Min(Math.Max(0, -offset.X), hMax); } else { this.HScrollBar.Enabled = false; this.HScrollBar.Value = 0; } int vMax = map.Height * gridSize - this.VScrollBar.Height; if (0 < vMax) { this.VScrollBar.Enabled = true; this.VScrollBar.Minimum = 0; this.VScrollBar.Maximum = vMax + this.VScrollBar.Height - 1; this.VScrollBar.SmallChange = 32; // TODO this.VScrollBar.LargeChange = this.VScrollBar.Height; Debug.Assert(this.VScrollBar.LargeChange == this.VScrollBar.Height); this.VScrollBar.Value = Math.Min(Math.Max(0, -offset.Y), vMax); } else { this.VScrollBar.Enabled = false; this.VScrollBar.Value = 0; } editorState.SetMapOffset(map.Id, new Point { X = -this.HScrollBar.Value, Y = -this.VScrollBar.Value, }); } else { this.HScrollBar.Enabled = false; this.VScrollBar.Enabled = false; this.HScrollBar.Value = 0; this.VScrollBar.Value = 0; } }
public void Update(EditorState editorState, TileSetCollection tileSetCollection, Map map, int gridSize, Rectangle rect) { if (editorState == null || map == null) { return; } Debug.Assert(this.DeviceContext != IntPtr.Zero); Point offset = editorState.GetMapOffset(map.Id); int offscreenWidth = this.Size.Width; int offscreenHeight = this.Size.Height; Size offscreenSize = this.Size; int tileGridSize = gridSize; int tileStartI = Math.Max(-offset.X / tileGridSize, 0); int tileEndI = Math.Min((offscreenWidth - offset.X) / tileGridSize + 1, map.Width); int tileStartJ = Math.Max(-offset.Y / tileGridSize, 0); int tileEndJ = Math.Min((offscreenHeight - offset.Y) / tileGridSize + 1, map.Height); using (Graphics g = Graphics.FromHdc(this.DeviceContext)) { if (0 < offscreenHeight - map.Height * gridSize) { NativeMethods.RECT win32Rect = new NativeMethods.RECT { Left = 0, Right = offscreenWidth, Top = map.Height * gridSize, Bottom = offscreenHeight, }; NativeMethods.FillRect(this.DeviceContext, ref win32Rect, (IntPtr)(NativeMethods.COLOR_BTNFACE + 1)); } if (0 < offscreenWidth - map.Width * gridSize) { NativeMethods.RECT win32Rect = new NativeMethods.RECT { Left = map.Width * gridSize, Right = offscreenWidth, Top = 0, Bottom = map.Height * gridSize, }; NativeMethods.FillRect(this.DeviceContext, ref win32Rect, (IntPtr)(NativeMethods.COLOR_BTNFACE + 1)); } Dictionary<Bitmap, BitmapData> bdHash = null; try { bdHash = new Dictionary<Bitmap, BitmapData>(); LayerMode layerMode = editorState.LayerMode; ScaleMode scaleMode = editorState.ScaleMode; int reductionRatio = 0; switch (scaleMode) { case ScaleMode.Scale2: reductionRatio = 1; break; case ScaleMode.Scale4: reductionRatio = 2; break; case ScaleMode.Scale8: reductionRatio = 4; break; } int halfTileGridSize = tileGridSize >> 1; for (int layer = -1; layer < 2; layer++) { byte alpha = 255; if (layerMode == LayerMode.Layer1 && layer == 1) { alpha = 128; } for (int j = tileStartJ; j < tileEndJ; j++) { int y = j * tileGridSize + offset.Y; for (int i = tileStartI; i < tileEndI; i++) { int x = i * tileGridSize + offset.X; if (0 <= layer) { Tile tile = map.GetTile(layer, i, j); if (tileSetCollection.ContainsId(tile.TileSetId)) { int tileId = tile.TileId; TileSet tileSet = tileSetCollection.GetItem(tile.TileSetId); Bitmap bitmap = tileSet.GetBitmap(scaleMode); BitmapData srcBD; if (!bdHash.ContainsKey(bitmap)) { srcBD = bdHash[bitmap] = bitmap.LockBits(new Rectangle { Size = bitmap.Size, }, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); } else { srcBD = bdHash[bitmap]; } int srcX = (tileId % Util.PaletteHorizontalCount) * tileGridSize; int srcY = (tileId / Util.PaletteHorizontalCount) * tileGridSize; Util.DrawBitmap(this.Pixels, offscreenSize, x, y, tileGridSize, tileGridSize, srcBD, srcX, srcY, alpha); } } else { if (scaleMode == ScaleMode.Scale1) { this.DrawRectOffscreen(new Rectangle { X = x, Y = y, Width = halfTileGridSize, Height = halfTileGridSize, }, 0x00, 0x00, 0x80); this.DrawRectOffscreen(new Rectangle { X = x + halfTileGridSize, Y = y, Width = halfTileGridSize, Height = halfTileGridSize, }, 0x00, 0x00, 0x40); this.DrawRectOffscreen(new Rectangle { X = x, Y = y + halfTileGridSize, Width = halfTileGridSize, Height = halfTileGridSize, }, 0x00, 0x00, 0x40); this.DrawRectOffscreen(new Rectangle { X = x + halfTileGridSize, Y = y + halfTileGridSize, Width = halfTileGridSize, Height = halfTileGridSize, }, 0x00, 0x00, 0x80); } else { byte blue = (byte)(((i / reductionRatio + j / reductionRatio) % 2) == 0 ? 0x80 : 0x40); this.DrawRectOffscreen(new Rectangle { X = x, Y = y, Width = tileGridSize, Height = tileGridSize, }, 0x00, 0x00, blue); } } } } if (layerMode == LayerMode.Event) { int yMin = tileStartJ * tileGridSize + offset.Y; int yMax = tileEndJ * tileGridSize + offset.Y; for (int i = tileStartI; i < tileEndI; i++) { int x1 = i * tileGridSize + offset.X; int x2 = i * tileGridSize + offset.X + tileGridSize - 1; this.DrawGrayLineOnOffscreen(x1, x1, yMin, yMax); this.DrawGrayLineOnOffscreen(x2, x2, yMin, yMax); } int xMin = tileStartI * tileGridSize + offset.X; int xMax = tileEndI * tileGridSize + offset.X; for (int j = tileStartJ; j < tileEndJ; j++) { int y1 = j * tileGridSize + offset.Y; int y2 = j * tileGridSize + offset.Y + tileGridSize - 1; this.DrawGrayLineOnOffscreen(xMin, xMax, y1, y1); this.DrawGrayLineOnOffscreen(xMin, xMax, y2, y2); } } if (editorState.LayerMode == LayerMode.Layer2 && layer == 0) { this.DarkenOffscreen(new Rectangle { X = tileStartI * tileGridSize + offset.X, Y = tileStartJ * tileGridSize + offset.Y, Width = (tileEndI - tileStartI) * tileGridSize, Height = (tileEndJ - tileStartJ) * tileGridSize, }); } } } finally { if (bdHash != null) { foreach (var pair in bdHash) { pair.Key.UnlockBits(pair.Value); } bdHash.Clear(); bdHash = null; } } } }
public void Update(EditorState editorState, TileSetCollection tileSetCollection, Map map, int gridSize) { Rectangle rect = new Rectangle(new Point(0, 0), this.Size); this.Update(editorState, tileSetCollection, map, gridSize, rect); }