/// <summary> /// store bitmap into texture sheet /// </summary> /// <param name="key">unique key for item</param> /// <param name="data">item bitmap data</param> /// <param name="rect">rectangle used in data bitmap</param> /// <returns>true on success</returns> public virtual bool Store(string key, Bitmap data, Rectangle rect) { if (data.Height > m_height || data.Width > m_width) { throw new ArgumentException("texture size is too large for this packer!"); } // add to packer ILTextureData item; if (m_items.TryGetValue(key, out item)) { item.Height = data.Height; item.Width = data.Width; // todo: remove item from packer & from texture sheet (somehow...[?]) } else { item = new ILTextureData(rect.Height, rect.Width); m_items.Add(key, item); } Node node = m_root.Insert(item); if (node == null) { return(false); } item.TextureRectangle = RectangleF.FromLTRB((0.5f + node.Rect.Left) / m_width, (0.5f + node.Rect.Top) / m_height, (node.Rect.Right - 0.5f) / m_width, (node.Rect.Bottom - 0.5f) / m_height); Store(data, rect, node.Rect); return(true); }
/// <summary> /// try to fetch item by key /// </summary> /// <param name="key">unique key</param> /// <param name="item">[output] item found</param> /// <returns>true: item was found, false otherwise</returns> public virtual bool TryGetTextureItem(string key, out ILTextureData item) { if (m_items.ContainsKey(key)) { item = m_items[key]; return(true); } item = null; return(false); }
/// <summary> /// fetch texture item from storage /// </summary> /// <param name="key"></param> /// <returns></returns> public virtual ILTextureData Get(string key) { ILTextureData ret = null; if (!m_items.TryGetValue(key, out ret)) { return(null); //throw new ArgumentException("no texture item has been found for key: " + key); } return(ret); }
public Node Insert(ILTextureData item) { if (!this.Leaf) { // Recurse towards left child, and if that fails, towards the right. Node new_node = left.Insert(item); return(new_node ?? right.Insert(item)); } else { // We have recursed to a leaf. // If it is not empty go back. if (use_count != 0) { return(null); } // If this leaf is too small go back. if (rect.Width < item.Width || rect.Height < item.Height) { return(null); } // If this leaf is the right size, insert here. if (rect.Width == item.Width && rect.Height == item.Height) { use_count = 1; return(this); } // This leaf is too large, split it up. We'll decide which way to split // by checking the width and height difference between this rectangle and // out item's bounding box. If the width difference is larger, we'll split // horizontaly, else verticaly. left = new Node(); right = new Node(); int dw = this.rect.Width - item.Width + 1; int dh = this.rect.Height - item.Height + 1; if (dw > dh) { left.rect = new Rectangle(rect.Left, rect.Top, item.Width, rect.Height); right.rect = new Rectangle(rect.Left + item.Width, rect.Top, rect.Width - item.Width, rect.Height); } else { left.rect = new Rectangle(rect.Left, rect.Top, rect.Width, item.Height); right.rect = new Rectangle(rect.Left, rect.Top + item.Height, rect.Width, rect.Height - item.Height); } return(left.Insert(item)); } }
public bool TryGetTextureItem(string key, out ILTextureData item) { lock (m_lock) { foreach (ILTextureStorage storage in m_textureSheets) { if (storage.TryGetTextureItem(key, out item)) { return(true); } } item = null; return(false); } }
public bool TryGetTextureItem (string key, out ILTextureData item) { lock (m_lock) { foreach (ILTextureStorage storage in m_textureSheets) { if (storage.TryGetTextureItem(key, out item)) { return true; } } item = null; return false; } }
public Node Insert(ILTextureData item) { if (!this.Leaf) { // Recurse towards left child, and if that fails, towards the right. Node new_node = left.Insert(item); return new_node ?? right.Insert(item); } else { // We have recursed to a leaf. // If it is not empty go back. if (use_count != 0) return null; // If this leaf is too small go back. if (rect.Width < item.Width || rect.Height < item.Height) return null; // If this leaf is the right size, insert here. if (rect.Width == item.Width && rect.Height == item.Height) { use_count = 1; return this; } // This leaf is too large, split it up. We'll decide which way to split // by checking the width and height difference between this rectangle and // out item's bounding box. If the width difference is larger, we'll split // horizontaly, else verticaly. left = new Node(); right = new Node(); int dw = this.rect.Width - item.Width + 1; int dh = this.rect.Height - item.Height + 1; if (dw > dh) { left.rect = new Rectangle(rect.Left, rect.Top, item.Width, rect.Height); right.rect = new Rectangle(rect.Left + item.Width, rect.Top, rect.Width - item.Width, rect.Height); } else { left.rect = new Rectangle(rect.Left, rect.Top, rect.Width, item.Height); right.rect = new Rectangle(rect.Left, rect.Top + item.Height, rect.Width, rect.Height - item.Height); } return left.Insert(item); } }
/// <summary> /// store bitmap into texture sheet /// </summary> /// <param name="key">unique key for item</param> /// <param name="data">item bitmap data</param> /// <param name="bmpRect">used rectangle in data bitmap</param> public virtual bool Store (string key, Bitmap data, RectangleF bmpRect) { if (bmpRect.Height > m_height || bmpRect.Width > m_width) throw new ArgumentException("texture size is too large for this packer!"); // add to packer ILTextureData item; Node node; Rectangle itemRect = Rectangle.Ceiling(bmpRect); if (m_items.TryGetValue(key,out item)) { item.Height = itemRect.Height; item.Width = itemRect.Width; // todo: remove item from packer & from texture sheet (somehow...[?]) node = m_root.Insert(item); if (node == null) return false; } else { item = new ILTextureData(itemRect.Height, itemRect.Width); node = m_root.Insert(item); if (node == null) return false; m_items.Add(key, item); } item.TextureRectangle = RectangleF.FromLTRB( (0.5f + node.Rect.Left) / m_width, (0.5f + node.Rect.Top) / m_height, (node.Rect.Right - 0.5f) / m_width, (node.Rect.Bottom - 0.5f) /m_height); //item.TextureRectangle = RectangleF.FromLTRB( (0f + node.Rect.Left) / m_width, // (0f + node.Rect.Top) / m_height, // (node.Rect.Right - 0f) / m_width, // (node.Rect.Bottom - 0f) /m_height); Store(data,bmpRect,node.Rect); return true; }
/// <summary> /// try to fetch item by key /// </summary> /// <param name="key">unique key</param> /// <param name="item">[output] item found</param> /// <returns>true: item was found, false otherwise</returns> public virtual bool TryGetTextureItem(string key, out ILTextureData item) { if (m_items.ContainsKey(key)) { item = m_items[key]; return true; } item = null; return false; }