|
@@ -22,101 +22,133 @@
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
******************************************************************************/
|
|
|
-
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.IO;
|
|
|
|
|
|
namespace Spine {
|
|
|
- abstract public class BaseAtlas {
|
|
|
- List<AtlasPage> pages = new List<AtlasPage>();
|
|
|
- List<AtlasRegion> regions = new List<AtlasRegion>();
|
|
|
+ public class Atlas {
|
|
|
+ public Format Format;
|
|
|
+ public TextureFilter MinFilter;
|
|
|
+ public TextureFilter MagFilter;
|
|
|
+ public TextureWrap UWrap;
|
|
|
+ public TextureWrap VWrap;
|
|
|
+ public int TextureWidth;
|
|
|
+ public int TextureHeight;
|
|
|
+ public List<AtlasRegion> Regions;
|
|
|
+ public Object Texture;
|
|
|
+
|
|
|
+ public Atlas (String path, Object texture, int textureWidth, int textureHeight) {
|
|
|
+ using (Stream input = new FileStream(path, FileMode.Open, FileAccess.Read)) {
|
|
|
+ try {
|
|
|
+ initialize(input, texture, textureWidth, textureHeight);
|
|
|
+ } catch (Exception ex) {
|
|
|
+ throw new Exception("Error reading atlas file: " + path, ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public Atlas (Stream input, Object texture, int textureWidth, int textureHeight) {
|
|
|
+ initialize(input, texture, textureWidth, textureHeight);
|
|
|
+ }
|
|
|
|
|
|
- abstract protected AtlasPage NewAtlasPage (String path);
|
|
|
+ private void initialize (Stream input, Object texture, int textureWidth, int textureHeight) {
|
|
|
+ TextureWidth = textureWidth;
|
|
|
+ TextureHeight = textureHeight;
|
|
|
+ Texture = texture;
|
|
|
|
|
|
- public void load (StreamReader reader, String imagesDir) {
|
|
|
+ Regions = new List<AtlasRegion>();
|
|
|
+ float invTexWidth = 1f / textureWidth;
|
|
|
+ float invTexHeight = 1f / textureHeight;
|
|
|
String[] tuple = new String[4];
|
|
|
- AtlasPage page = null;
|
|
|
+
|
|
|
+ StreamReader reader = new StreamReader(input);
|
|
|
+ // Skip to first page entry.
|
|
|
while (true) {
|
|
|
String line = reader.ReadLine();
|
|
|
- if (line == null) break;
|
|
|
if (line.Trim().Length == 0)
|
|
|
- page = null;
|
|
|
- else if (page == null) {
|
|
|
- page = NewAtlasPage(Path.Combine(imagesDir, line));
|
|
|
-
|
|
|
- page.Format = (Format)Enum.Parse(typeof(Format), readValue(reader), false);
|
|
|
-
|
|
|
- readTuple(reader, tuple);
|
|
|
- page.MinFilter = (TextureFilter)Enum.Parse(typeof(TextureFilter), tuple[0]);
|
|
|
- page.MagFilter = (TextureFilter)Enum.Parse(typeof(TextureFilter), tuple[1]);
|
|
|
-
|
|
|
- String direction = readValue(reader);
|
|
|
- page.UWrap = TextureWrap.ClampToEdge;
|
|
|
- page.VWrap = TextureWrap.ClampToEdge;
|
|
|
- if (direction == "x")
|
|
|
- page.UWrap = TextureWrap.Repeat;
|
|
|
- else if (direction == "y")
|
|
|
- page.VWrap = TextureWrap.Repeat;
|
|
|
- else if (direction == "xy")
|
|
|
- page.UWrap = page.VWrap = TextureWrap.Repeat;
|
|
|
-
|
|
|
- pages.Add(page);
|
|
|
-
|
|
|
- } else {
|
|
|
- AtlasRegion region = new AtlasRegion();
|
|
|
- region.Name = line;
|
|
|
- region.Page = page;
|
|
|
-
|
|
|
- region.Rotate = Boolean.Parse(readValue(reader));
|
|
|
-
|
|
|
- readTuple(reader, tuple);
|
|
|
- int x = int.Parse(tuple[0]);
|
|
|
- int y = int.Parse(tuple[1]);
|
|
|
-
|
|
|
- readTuple(reader, tuple);
|
|
|
- int width = int.Parse(tuple[0]);
|
|
|
- int height = int.Parse(tuple[1]);
|
|
|
-
|
|
|
- float invTexWidth = 1f / page.GetTextureWidth();
|
|
|
- float invTexHeight = 1f / page.GetTextureHeight();
|
|
|
- region.U = x * invTexWidth;
|
|
|
- region.V = y * invTexHeight;
|
|
|
- region.U2 = (x + width) * invTexWidth;
|
|
|
- region.V2 = (y + height) * invTexHeight;
|
|
|
- region.Width = Math.Abs(width);
|
|
|
- region.Height = Math.Abs(height);
|
|
|
-
|
|
|
- if (readTuple(reader, tuple) == 4) { // split is optional
|
|
|
- region.Splits = new int[] {int.Parse(tuple[0]), int.Parse(tuple[1]),
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ reader.ReadLine(); // Skip first page name.
|
|
|
+
|
|
|
+ Format = (Format)Enum.Parse(typeof(Format), readValue(reader), false);
|
|
|
+
|
|
|
+ readTuple(reader, tuple);
|
|
|
+ MinFilter = (TextureFilter)Enum.Parse(typeof(TextureFilter), tuple[0]);
|
|
|
+ MagFilter = (TextureFilter)Enum.Parse(typeof(TextureFilter), tuple[1]);
|
|
|
+
|
|
|
+ String direction = readValue(reader);
|
|
|
+ UWrap = TextureWrap.ClampToEdge;
|
|
|
+ VWrap = TextureWrap.ClampToEdge;
|
|
|
+ if (direction == "x")
|
|
|
+ UWrap = TextureWrap.Repeat;
|
|
|
+ else if (direction == "y")
|
|
|
+ VWrap = TextureWrap.Repeat;
|
|
|
+ else if (direction == "xy")
|
|
|
+ UWrap = VWrap = TextureWrap.Repeat;
|
|
|
+
|
|
|
+ while (true) {
|
|
|
+ String line = reader.ReadLine();
|
|
|
+ if (line == null || line.Trim().Length == 0) break;
|
|
|
+
|
|
|
+ AtlasRegion region = new AtlasRegion();
|
|
|
+ region.Atlas = this;
|
|
|
+ region.Name = line;
|
|
|
+
|
|
|
+ region.Rotate = Boolean.Parse(readValue(reader));
|
|
|
+
|
|
|
+ readTuple(reader, tuple);
|
|
|
+ int x = int.Parse(tuple[0]);
|
|
|
+ int y = int.Parse(tuple[1]);
|
|
|
+
|
|
|
+ readTuple(reader, tuple);
|
|
|
+ int width = int.Parse(tuple[0]);
|
|
|
+ int height = int.Parse(tuple[1]);
|
|
|
+
|
|
|
+ region.U = x * invTexWidth;
|
|
|
+ region.V = y * invTexHeight;
|
|
|
+ region.U2 = (x + width) * invTexWidth;
|
|
|
+ region.V2 = (y + height) * invTexHeight;
|
|
|
+ region.Width = Math.Abs(width);
|
|
|
+ region.Height = Math.Abs(height);
|
|
|
+
|
|
|
+ if (readTuple(reader, tuple) == 4) { // split is optional
|
|
|
+ region.Splits = new int[] {int.Parse(tuple[0]), int.Parse(tuple[1]),
|
|
|
int.Parse(tuple[2]), int.Parse(tuple[3])};
|
|
|
|
|
|
- if (readTuple(reader, tuple) == 4) { // pad is optional, but only present with splits
|
|
|
- region.Pads = new int[] {int.Parse(tuple[0]), int.Parse(tuple[1]),
|
|
|
+ if (readTuple(reader, tuple) == 4) { // pad is optional, but only present with splits
|
|
|
+ region.Pads = new int[] {int.Parse(tuple[0]), int.Parse(tuple[1]),
|
|
|
int.Parse(tuple[2]), int.Parse(tuple[3])};
|
|
|
|
|
|
- readTuple(reader, tuple);
|
|
|
- }
|
|
|
+ readTuple(reader, tuple);
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- region.OriginalWidth = int.Parse(tuple[0]);
|
|
|
- region.OriginalHeight = int.Parse(tuple[1]);
|
|
|
+ region.OriginalWidth = int.Parse(tuple[0]);
|
|
|
+ region.OriginalHeight = int.Parse(tuple[1]);
|
|
|
|
|
|
- readTuple(reader, tuple);
|
|
|
- region.OffsetX = int.Parse(tuple[0]);
|
|
|
- region.OffsetY = int.Parse(tuple[1]);
|
|
|
+ readTuple(reader, tuple);
|
|
|
+ region.OffsetX = int.Parse(tuple[0]);
|
|
|
+ region.OffsetY = int.Parse(tuple[1]);
|
|
|
|
|
|
- region.Index = int.Parse(readValue(reader));
|
|
|
+ region.Index = int.Parse(readValue(reader));
|
|
|
|
|
|
- regions.Add(region);
|
|
|
- }
|
|
|
+ Regions.Add(region);
|
|
|
+ }
|
|
|
+
|
|
|
+ while (true) {
|
|
|
+ String line = reader.ReadLine();
|
|
|
+ if (line == null)
|
|
|
+ break;
|
|
|
+ if (line.Trim().Length != 0) throw new Exception("An atlas with multiple images is not supported.");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
static String readValue (StreamReader reader) {
|
|
|
String line = reader.ReadLine();
|
|
|
int colon = line.IndexOf(':');
|
|
|
- if (colon == -1) throw new Exception("Invalid line: " + line);
|
|
|
+ if (colon == -1)
|
|
|
+ throw new Exception("Invalid line: " + line);
|
|
|
return line.Substring(colon + 1).Trim();
|
|
|
}
|
|
|
|
|
@@ -124,12 +156,14 @@ namespace Spine {
|
|
|
static int readTuple (StreamReader reader, String[] tuple) {
|
|
|
String line = reader.ReadLine();
|
|
|
int colon = line.IndexOf(':');
|
|
|
- if (colon == -1) throw new Exception("Invalid line: " + line);
|
|
|
+ if (colon == -1)
|
|
|
+ throw new Exception("Invalid line: " + line);
|
|
|
int i = 0, lastMatch = colon + 1;
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
int comma = line.IndexOf(',', lastMatch);
|
|
|
if (comma == -1) {
|
|
|
- if (i == 0) throw new Exception("Invalid line: " + line);
|
|
|
+ if (i == 0)
|
|
|
+ throw new Exception("Invalid line: " + line);
|
|
|
break;
|
|
|
}
|
|
|
tuple[i] = line.Substring(lastMatch, comma - lastMatch).Trim();
|
|
@@ -143,8 +177,9 @@ namespace Spine {
|
|
|
* should be cached rather than calling this method multiple times.
|
|
|
* @return The region, or null. */
|
|
|
public AtlasRegion FindRegion (String name) {
|
|
|
- for (int i = 0, n = regions.Count; i < n; i++)
|
|
|
- if (regions[i].Name == name) return regions[i];
|
|
|
+ for (int i = 0, n = Regions.Count; i < n; i++)
|
|
|
+ if (Regions[i].Name == name)
|
|
|
+ return Regions[i];
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
@@ -175,19 +210,8 @@ namespace Spine {
|
|
|
Repeat
|
|
|
}
|
|
|
|
|
|
- abstract public class AtlasPage {
|
|
|
- public Format Format;
|
|
|
- public TextureFilter MinFilter;
|
|
|
- public TextureFilter MagFilter;
|
|
|
- public TextureWrap UWrap;
|
|
|
- public TextureWrap VWrap;
|
|
|
-
|
|
|
- abstract public int GetTextureWidth ();
|
|
|
- abstract public int GetTextureHeight ();
|
|
|
- }
|
|
|
-
|
|
|
public class AtlasRegion {
|
|
|
- public AtlasPage Page;
|
|
|
+ public Atlas Atlas;
|
|
|
public float U, V;
|
|
|
public float U2, V2;
|
|
|
public int Width, Height;
|