2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
#region ================== Copyright (c) 2007 Pascal vd Heiden
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
|
|
|
|
* This program is released under GNU General Public License
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Namespaces
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using CodeImp.DoomBuilder.Data;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.ZDoom
|
|
|
|
{
|
|
|
|
public sealed class TextureStructure
|
|
|
|
{
|
|
|
|
#region ================== Constants
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Variables
|
|
|
|
|
|
|
|
// Declaration
|
2015-03-04 09:37:30 +00:00
|
|
|
private readonly string typename;
|
|
|
|
private readonly string name;
|
|
|
|
private readonly string virtualpath; //mxd
|
|
|
|
private readonly int width;
|
|
|
|
private readonly int height;
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// Properties
|
2015-03-04 09:37:30 +00:00
|
|
|
private readonly float xscale;
|
|
|
|
private readonly float yscale;
|
|
|
|
private readonly int xoffset;
|
|
|
|
private readonly int yoffset;
|
|
|
|
private readonly bool worldpanning;
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// Patches
|
2015-03-04 09:37:30 +00:00
|
|
|
private readonly List<PatchStructure> patches;
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Properties
|
|
|
|
|
2010-08-18 05:49:15 +00:00
|
|
|
public string TypeName { get { return typename; } }
|
2009-04-19 18:07:22 +00:00
|
|
|
public string Name { get { return name; } }
|
|
|
|
public int Width { get { return width; } }
|
|
|
|
public int Height { get { return height; } }
|
|
|
|
public float XScale { get { return xscale; } }
|
|
|
|
public float YScale { get { return yscale; } }
|
|
|
|
public int XOffset { get { return xoffset; } }
|
|
|
|
public int YOffset { get { return yoffset; } }
|
|
|
|
public bool WorldPanning { get { return worldpanning; } }
|
|
|
|
public ICollection<PatchStructure> Patches { get { return patches; } }
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Constructor / Disposer
|
|
|
|
|
|
|
|
// Constructor
|
2015-03-04 09:37:30 +00:00
|
|
|
internal TextureStructure(TexturesParser parser, string typename, string virtualpath)
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
|
|
|
// Initialize
|
2010-08-18 05:49:15 +00:00
|
|
|
this.typename = typename;
|
2015-03-04 09:37:30 +00:00
|
|
|
this.virtualpath = virtualpath;
|
2009-04-19 18:07:22 +00:00
|
|
|
patches = new List<PatchStructure>(4);
|
|
|
|
xscale = 0.0f;
|
|
|
|
yscale = 0.0f;
|
|
|
|
|
|
|
|
// There should be 3 tokens separated by 2 commas now:
|
|
|
|
// Name, Width, Height
|
|
|
|
|
2015-12-17 10:07:28 +00:00
|
|
|
// First token is the texture name
|
2009-04-19 18:07:22 +00:00
|
|
|
parser.SkipWhitespace(true);
|
2015-12-17 10:07:28 +00:00
|
|
|
name = parser.StripTokenQuotes(parser.ReadToken(false)); //mxd. Don't skip newline
|
2015-03-04 09:37:30 +00:00
|
|
|
|
|
|
|
//mxd. It can also be "optional" keyword.
|
|
|
|
if(name.ToLowerInvariant() == "optional")
|
|
|
|
{
|
|
|
|
parser.SkipWhitespace(true);
|
2015-12-17 10:07:28 +00:00
|
|
|
name = parser.StripTokenQuotes(parser.ReadToken(false)); //mxd. Don't skip newline
|
2015-03-04 09:37:30 +00:00
|
|
|
}
|
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
if(string.IsNullOrEmpty(name))
|
|
|
|
{
|
2015-12-17 10:07:28 +00:00
|
|
|
parser.ReportError("Expected " + typename + " name");
|
2009-04-19 18:07:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we should find a comma
|
2015-12-21 14:17:47 +00:00
|
|
|
if(!parser.NextTokenIs(",")) return; //mxd
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// Next is the texture width
|
|
|
|
parser.SkipWhitespace(true);
|
2015-12-21 14:17:47 +00:00
|
|
|
string tokenstr = parser.ReadToken();
|
2009-04-19 18:07:22 +00:00
|
|
|
if(string.IsNullOrEmpty(tokenstr) || !int.TryParse(tokenstr, NumberStyles.Integer, CultureInfo.InvariantCulture, out width))
|
|
|
|
{
|
|
|
|
parser.ReportError("Expected width in pixels");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we should find a comma again
|
2015-12-21 14:17:47 +00:00
|
|
|
if(!parser.NextTokenIs(",")) return; //mxd
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// Next is the texture height
|
|
|
|
parser.SkipWhitespace(true);
|
|
|
|
tokenstr = parser.ReadToken();
|
|
|
|
if(string.IsNullOrEmpty(tokenstr) || !int.TryParse(tokenstr, NumberStyles.Integer, CultureInfo.InvariantCulture, out height))
|
|
|
|
{
|
|
|
|
parser.ReportError("Expected height in pixels");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next token should be the beginning of the texture scope
|
2015-12-21 14:17:47 +00:00
|
|
|
if(!parser.NextTokenIs("{", false)) //mxd
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
|
|
|
parser.ReportError("Expected begin of structure");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now parse the contents of texture structure
|
2013-12-18 09:11:04 +00:00
|
|
|
bool done = false; //mxd
|
|
|
|
while(!done && parser.SkipWhitespace(true))
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
|
|
|
string token = parser.ReadToken();
|
|
|
|
token = token.ToLowerInvariant();
|
2013-12-18 09:11:04 +00:00
|
|
|
|
2015-12-17 10:07:28 +00:00
|
|
|
switch(token)
|
2014-12-03 23:15:26 +00:00
|
|
|
{
|
2013-12-18 09:11:04 +00:00
|
|
|
case "xscale":
|
2015-12-17 10:07:28 +00:00
|
|
|
if(!ReadTokenFloat(parser, token, out xscale)) return;
|
2013-12-18 09:11:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "yscale":
|
2015-12-17 10:07:28 +00:00
|
|
|
if(!ReadTokenFloat(parser, token, out yscale)) return;
|
2013-12-18 09:11:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "worldpanning":
|
|
|
|
worldpanning = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "offset":
|
|
|
|
// Read x offset
|
2015-12-17 10:07:28 +00:00
|
|
|
if(!ReadTokenInt(parser, token, out xoffset)) return;
|
2013-12-18 09:11:04 +00:00
|
|
|
|
|
|
|
// Now we should find a comma
|
2015-12-21 14:17:47 +00:00
|
|
|
if(!parser.NextTokenIs(",")) return; //mxd
|
2013-12-18 09:11:04 +00:00
|
|
|
|
|
|
|
// Read y offset
|
2015-12-17 10:07:28 +00:00
|
|
|
if(!ReadTokenInt(parser, token, out yoffset)) return;
|
2013-12-18 09:11:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "patch":
|
|
|
|
// Read patch structure
|
|
|
|
PatchStructure pt = new PatchStructure(parser);
|
2015-12-17 10:07:28 +00:00
|
|
|
if(parser.HasError) break;
|
2013-12-18 09:11:04 +00:00
|
|
|
|
|
|
|
// Add the patch
|
|
|
|
patches.Add(pt);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "}":
|
|
|
|
// Actor scope ends here,
|
|
|
|
// break out of this parse loop
|
|
|
|
done = true;
|
|
|
|
break;
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Methods
|
|
|
|
|
|
|
|
// This reads the next token and sets a floating point value, returns false when failed
|
2014-03-05 09:21:28 +00:00
|
|
|
private static bool ReadTokenFloat(TexturesParser parser, string propertyname, out float value)
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
|
|
|
// Next token is the property value to set
|
|
|
|
parser.SkipWhitespace(true);
|
|
|
|
string strvalue = parser.ReadToken();
|
|
|
|
if(!string.IsNullOrEmpty(strvalue))
|
|
|
|
{
|
|
|
|
// Try parsing as value
|
|
|
|
if(!float.TryParse(strvalue, NumberStyles.Float, CultureInfo.InvariantCulture, out value))
|
|
|
|
{
|
2016-02-15 14:06:46 +00:00
|
|
|
parser.ReportError("Expected numeric value for property \"" + propertyname + "\"");
|
2009-04-19 18:07:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-12-21 14:17:47 +00:00
|
|
|
|
|
|
|
// Success
|
|
|
|
return true;
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
2015-12-21 14:17:47 +00:00
|
|
|
|
|
|
|
// Can't find the property value!
|
2016-02-15 14:06:46 +00:00
|
|
|
parser.ReportError("Expected a value for property \"" + propertyname + "\"");
|
2015-12-21 14:17:47 +00:00
|
|
|
value = 0.0f;
|
|
|
|
return false;
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This reads the next token and sets an integral value, returns false when failed
|
2014-03-05 09:21:28 +00:00
|
|
|
private static bool ReadTokenInt(TexturesParser parser, string propertyname, out int value)
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
|
|
|
// Next token is the property value to set
|
|
|
|
parser.SkipWhitespace(true);
|
|
|
|
string strvalue = parser.ReadToken();
|
|
|
|
if(!string.IsNullOrEmpty(strvalue))
|
|
|
|
{
|
|
|
|
// Try parsing as value
|
|
|
|
if(!int.TryParse(strvalue, NumberStyles.Integer, CultureInfo.InvariantCulture, out value))
|
|
|
|
{
|
2016-02-15 14:06:46 +00:00
|
|
|
parser.ReportError("Expected integral value for property \"" + propertyname + "\"");
|
2009-04-19 18:07:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-12-21 14:17:47 +00:00
|
|
|
|
|
|
|
// Success
|
|
|
|
return true;
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
2015-12-21 14:17:47 +00:00
|
|
|
|
|
|
|
// Can't find the property value!
|
2016-02-15 14:06:46 +00:00
|
|
|
parser.ReportError("Expected a value for property \"" + propertyname + "\"");
|
2015-12-21 14:17:47 +00:00
|
|
|
value = 0;
|
|
|
|
return false;
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This makes a HighResImage texture for this texture
|
2016-03-04 08:10:56 +00:00
|
|
|
internal TEXTURESImage MakeImage()
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
|
|
|
// Determine scale for texture
|
2015-12-21 14:17:47 +00:00
|
|
|
float scalex = ((xscale == 0.0f) ? General.Map.Config.DefaultTextureScale : 1f / xscale);
|
|
|
|
float scaley = ((yscale == 0.0f) ? General.Map.Config.DefaultTextureScale : 1f / yscale);
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// Make texture
|
2016-03-04 08:10:56 +00:00
|
|
|
TEXTURESImage tex = new TEXTURESImage(name, virtualpath, width, height, scalex, scaley, worldpanning, typename == "flat");
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// Add patches
|
2015-12-21 14:17:47 +00:00
|
|
|
foreach(PatchStructure p in patches) tex.AddPatch(new TexturePatch(p));//mxd
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
return tex;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|