private bool TryPack(SpRect rect) { foreach (var child in children) { var gapX = child.w - rect.W; var gapY = child.h - rect.H; if (gapX >= 0 && gapY >= 0) { rect.X = child.x; rect.Y = child.y; children.Remove(child); if (gapX > 0) { children.Add(new SpMaxRects(child.x + rect.W, child.y, gapX, child.h)); } if (gapY > 0) { children.Add(new SpMaxRects(child.x, child.y + rect.H, child.w, gapY)); } Subdivide(rect); return(true); } } return(false); }
private void Subdivide(SpRect rect) { newChildrenA.Clear(); newChildrenB.Clear(); foreach (var child in children) { if (rect.X >= child.r || rect.Y >= child.t || rect.R < child.x || rect.T < child.y) { newChildrenA.Add(child); continue; } var gapL = rect.X - child.x; var gapB = rect.Y - child.y; var gapR = child.r - rect.R; var gapT = child.t - rect.T; if (gapL > 0) { newChildrenB.Add(new SpMaxRects(child.x, child.y, gapL, child.h)); } if (gapB > 0) { newChildrenB.Add(new SpMaxRects(child.x, child.y, child.w, gapB)); } if (gapR > 0) { newChildrenB.Add(new SpMaxRects(child.r - gapR, child.y, gapR, child.h)); } if (gapT > 0) { newChildrenB.Add(new SpMaxRects(child.x, child.t - gapT, child.w, gapT)); } } children.Clear(); children.AddRange(newChildrenA); children.AddRange(newChildrenB); for (var i = children.Count - 1; i >= 0; i--) { var child = children[i]; foreach (var childB in children) { if (child != childB) { if (child.x >= childB.x && child.y >= childB.y && child.r <= childB.r && child.t <= childB.t) { children.Remove(child); break; } } } } }
public SpRect GetClone() { var clone = new SpRect(); clone.Name = Name; clone.Source = Source; clone.Pixels = Pixels; clone.Border = Border; clone.Pivot = Pivot; clone.X = X; clone.Y = Y; clone.W = W; clone.H = H; return(clone); }
private static void CompileRect(List <SpRect> rects, SpSource source, SpPixels pixels, string name, Sprite sprite = null) { var newRect = new SpRect(); newRect.Name = name; newRect.Source = source; newRect.Pixels = pixels; // Read pivot and border from sprite if (sprite != null) { newRect.Pivot = SpHelper.GetSpritePivot(sprite); newRect.Border = sprite.border; } // Use default pivot and border settings else { newRect.Pivot = new Vector2(0.5f, 0.5f); newRect.Border = new Vector4(0.0f, 0.0f, 0.0f, 0.0f); } // Override the pivot? if (source.UseCustomPivot == true) { newRect.Pivot = source.CustomPivot; } // Override the border? if (source.UseCustomBorder == true) { newRect.Border = source.CustomBorder; } newRect.Trim(); rects.Add(newRect); }