//construct the tree internal static void CalculateTree() { PackingNode root = new PackingNode(new Rectangle(0, 0, atlasSize, atlasSize)); for (int i = 0; i < rawTexs.Set.Count; i++) { RawTexture raw = rawTexs.Set[i]; Rectangle r = new Rectangle(0, 0, raw.texture.Width, raw.texture.Height); PackingNode res = root.Fill(r); if (res == null) { Debug.PrintError("Texture could not be packed: " + raw.name); } else { if (raw.c == 1 && raw.r == 1) { textures.Add(raw.name, new Texture(raw.texture, res.bound)); } else if (raw.c == 0 && raw.r == 0) { tiles.Add(raw.name, new TileableTexture(raw.texture)); } else { animations.Add(raw.name, new AnimatedTexture(raw.texture, raw.c, raw.r, res.bound)); } } } }
public PackingNode(Rectangle bound) { filled = false; this.bound = bound; l = null; r = null; }
//see if the image fits this node recursivly and cut the space if needed. public PackingNode Fill(Rectangle rect) { if (rect == null) { return(null); } if (filled) { return(null); } if (rect.Width > bound.Width) { return(null); } if (rect.Height > bound.Height) { return(null); } if (rect.Width == bound.Width && rect.Height == bound.Height) { filled = true; return(this); } PackingNode temp = null; if (l != null) { temp = l.Fill(rect); } if (temp != null) { return(temp); } if (r != null) { temp = r.Fill(rect); } if (temp != null) { return(temp); } if (l != null && r != null) { return(null); } l = new PackingNode(new Rectangle()); r = new PackingNode(new Rectangle()); int diffWidth = rect.Width - rect.Width; int diffHeight = rect.Height - rect.Height; bool hor = diffWidth >= diffHeight; if (bound.Width == rect.Width) { hor = false; } if (bound.Height == rect.Height) { hor = true; } Split(rect, hor); return(l.Fill(rect)); }