/// <summary> /// Tiles the at position. /// </summary> /// <param name="x">The x.</param> /// <param name="y">The y.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <returns>A TileMaskCollection.</returns> public TileMaskCollection TilesAtPosition(int x, int y, int width, int height) { TileMaskCollection find = new TileMaskCollection(BytesPerColor) { tiles = tiles.Where((t) => { if (t.X + t.Width < x + 1) { return(false); } if (x + width < t.X + 1) { return(false); } if (t.Y + t.Height < y + 1) { return(false); } if (y + height < t.Y + 1) { return(false); } return(true); }).ToList() }; find.Sort(); find.UpdateContainer(); return(find); }
public override void AddingAtPosition(int x, int y, TileMaskCollection selection) { if (selection == null) { return; } selection.MoveTo(x, y); int drx = selection.Left; int dry = selection.Top; int drr = drx + selection.Width; int drb = dry + selection.Height; int cz = CellSize; drx /= cz; drx *= cz; dry /= cz; dry *= cz; drr += (drr % cz); drb += (drb % cz); int w = drr - drx; int h = drb - dry; layer1.DrawRectangle(drx, dry, w, h, BackgroundColor); layers[SelectedLayer].AddTiles(selection); ITileCollection col = layers[SelectedLayer].TilesOnArea(drx, dry, w, h); drawTileMaskCollection(col); UpdateLayer2(); updateOffsetAndCopyLenght(drx, dry, drr, drb); }
/// <summary> /// Tiles the on area. /// </summary> /// <param name="x">The x.</param> /// <param name="y">The y.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <returns>An ITileCollection.</returns> public ITileCollection TilesOnArea(int x, int y, int width, int height) { uint px = (uint)(x / CellSize); uint py = (uint)(y / CellSize); int w = width / CellSize; int h = height / CellSize; if (width < 2 && height < 2) { PositionKey pk = new PositionKey(px, py); if (tiles.ContainsKey(pk)) { TileMaskCollection ret = new TileMaskCollection(BytesPerColor); foreach (TileMask selT in tiles[pk]) { ret.Add(selT); } return(ret); } else { return(null); } } IEnumerable filtered = tiles.Where(kvp => { if (kvp.Key.X < px) { return(false); } if (kvp.Key.Y < py) { return(false); } if (kvp.Key.X >= w + px) { return(false); } if (kvp.Key.Y >= h + py) { return(false); } return(true); }); TileMaskCollection t = new TileMaskCollection(BytesPerColor); foreach (KeyValuePair <PositionKey, List <TileMask> > kvp in filtered) { foreach (TileMask selT in kvp.Value) { t.Add(selT); } } return(t); }
/// <summary> /// Fusions the. /// </summary> /// <param name="c1">The c1.</param> /// <param name="c2">The c2.</param> /// <returns>A SpriteTileMaskCollection.</returns> public static TileMaskCollection Fusion(TileMaskCollection c1, TileMaskCollection c2) { if (c1 == null) { throw new ArgumentNullException(nameof(c1)); } if (c2 == null) { throw new ArgumentNullException(nameof(c2)); } c1.Sort(); c2.Sort(); var vals1 = c1.tiles; var vals2 = c2.tiles; TileMaskCollection ret = new TileMaskCollection(c1.BytesPerColor); int total = vals1.Count + vals2.Count; var enum1 = vals1.GetEnumerator(); var enum2 = vals2.GetEnumerator(); enum1.MoveNext(); enum2.MoveNext(); for (int i = 0; i < total; i++) { if (enum2.Current == null || (enum1.Current != null && enum1.Current.Z <= enum2.Current.Z)) { ret.tiles.Add(enum1.Current); enum1.MoveNext(); } else if (enum2.Current != null) { ret.tiles.Add(enum2.Current); enum2.MoveNext(); } } ret.sorted = true; ret.Left = Math.Min(c1.Left, c2.Left); if (ret.Left < 0) { ret.Left = Math.Max(c1.Left, c2.Left); } ret.Right = Math.Max(c1.Right, c2.Right); ret.Top = Math.Min(c1.Top, c2.Top); if (ret.Top < 0) { ret.Top = Math.Max(c1.Top, c2.Top); } ret.Bottom = Math.Max(c1.Bottom, c2.Bottom); return(ret); }
/// <summary> /// Clones the. /// </summary> /// <returns>A SpriteTileMaskCollection.</returns> public TileMaskCollection Clone() { TileMaskCollection col = new TileMaskCollection(BytesPerColor); foreach (TileMask t in tiles) { col.Add(t.Clone()); } return(col); }
/// <summary> /// Finds the by area. /// </summary> /// <param name="x">The x.</param> /// <param name="width">The width.</param> /// <param name="y">The y.</param> /// <param name="height">The height.</param> public TileMaskCollection FindByArea(int x, int y, int width, int height) { if (selection == null) { selection = new TileMaskCollection(BytesPerColor); } selection.tiles = TilesAtPosition(x, y, width, height).tiles; selection.sorted = false; selection.UpdateContainer(); OnSelectionChanged?.Invoke(this, selection); return(selection); }
/// <summary> /// Clears the selection. /// </summary> public void ClearSelection() { if (selection == null) { selection = new TileMaskCollection(BytesPerColor); } selection.tiles.Clear(); selection.Left = -1; selection.Top = -1; selection.Right = -1; selection.Bottom = -1; OnSelectionClear?.Invoke(this); }
/// <summary> /// Selects the. /// </summary> /// <param name="x">The x.</param> /// <param name="y">The y.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> public ITileCollection SelectTiles(int x, int y, int width, int height) { TileMaskCollection sel = GetCollection(); if (width < 2 && height < 2) { return(sel.FindByPosition(x, y)); } else { return(sel.FindByArea(x, y, width, height)); } }
/// <summary> /// Adds the collection. /// </summary> /// <param name="col">The col.</param> public void AddCollection(TileMaskCollection col) { ClearSelection(); if (col != null) { col.Sort(); foreach (TileMask t in col.tiles) { Add(t); } } RequireRefresh = true; OnCollectionAdded?.Invoke(this, col); }
/// <summary> /// Moves the tiles. /// </summary> /// <param name="x">The x.</param> /// <param name="y">The y.</param> /// <returns>A bool.</returns> public bool MoveTiles(int x, int y) { if (selection == null) { return(false); } TileMaskCollection sel = selection; RemoveTiles(); sel.MoveTo(x, y); AddTiles(sel); selection = sel; return(true); }
/// <summary> /// Moves the selection. /// </summary> /// <param name="newX">The new x.</param> /// <param name="newY">The new y.</param> public bool MoveSelection(int newX, int newY) { if (selection == null) { selection = new TileMaskCollection(BytesPerColor); } if (selection.Left == newX && selection.Top == newY) { return(false); } bool b = selection.MoveTo(newX, newY); UpdateContainer(); RequireRefresh = true; OnSelectionChanged?.Invoke(this, selection); return(b); }
/// <summary> /// Removes the selection. /// </summary> public void RemoveSelection() { if (selection == null) { selection = new TileMaskCollection(BytesPerColor); } if (selection.Left > 0) { foreach (TileMask t in selection.tiles) { tiles.Remove(t); } UpdateContainer(); RequireRefresh = true; ClearSelection(); } }
/// <summary> /// Fusions the. /// </summary> /// <param name="args">The args.</param> /// <returns>A SpriteTileMaskCollection.</returns> public static TileMaskCollection Fusion(params TileMaskCollection[] args) { if (args.Length == 1) { return(args[0]); } if (args.Length == 2) { return(Fusion(args[0], args[1])); } Parallel.ForEach(args, col => { col.Sort(); }); TileMaskCollection[] objs = new TileMaskCollection[(args.Length >> 1) + (args.Length % 2)]; TileMaskCollection[] res = args; TileMaskCollection[] aux = null; while (objs.Length > 1) { Parallel.For(0, res.Length >> 1, i => { int j = i << 1; objs[i] = Fusion(res[j], res[j + 1]); }); if (res.Length % 2 == 1) { objs[objs.Length - 1] = res[res.Length - 1]; } aux = objs; objs = new TileMaskCollection[(res.Length >> 1) + (res.Length % 2)]; res = aux; } return(objs[0]); }
/// <summary> /// Removes the tiles. /// </summary> public void RemoveTiles() { if (selection == null) { return; } uint px; uint py; PositionKey pk; List <TileMask> tml; foreach (TileMask t in selection.GetEnumerable()) { px = (uint)t.X; py = (uint)t.Y; pk = new PositionKey(px, py); tml = tiles[pk]; tml.Remove(t); } selection = null; }
/// <summary> /// Selects the tiles. /// </summary> /// <param name="x">The x.</param> /// <param name="y">The y.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <returns>An ITileCollection.</returns> public ITileCollection SelectTiles(int x, int y, int width, int height) { selection = (TileMaskCollection)TilesOnArea(x, y, width, height); return(selection); }