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;
|
|
|
|
using System.Globalization;
|
|
|
|
using CodeImp.DoomBuilder.Data;
|
|
|
|
using System.IO;
|
2013-03-18 13:52:27 +00:00
|
|
|
using CodeImp.DoomBuilder.Rendering;
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.ZDoom
|
|
|
|
{
|
|
|
|
public sealed class PatchStructure
|
|
|
|
{
|
|
|
|
#region ================== Constants
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Variables
|
|
|
|
|
|
|
|
// Declaration
|
|
|
|
private string name;
|
|
|
|
private int offsetx;
|
|
|
|
private int offsety;
|
|
|
|
private bool flipx;
|
|
|
|
private bool flipy;
|
|
|
|
private float alpha;
|
2013-03-18 13:52:27 +00:00
|
|
|
private int rotation; //mxd
|
|
|
|
private TexturePathRenderStyle renderStyle; //mxd
|
|
|
|
private PixelColor blendColor; //mxd
|
|
|
|
private TexturePathBlendStyle blendStyle; //mxd
|
|
|
|
private float tintAmmount; //mxd
|
|
|
|
private static string[] renderStyles = { "copy", "translucent", "add", "subtract", "reversesubtract", "modulate", "copyalpha", "copynewalpha", "overlay" }; //mxd
|
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Properties
|
|
|
|
|
|
|
|
public string Name { get { return name; } }
|
|
|
|
public int OffsetX { get { return offsetx; } }
|
|
|
|
public int OffsetY { get { return offsety; } }
|
|
|
|
public bool FlipX { get { return flipx; } }
|
|
|
|
public bool FlipY { get { return flipy; } }
|
|
|
|
public float Alpha { get { return alpha; } }
|
2013-03-18 13:52:27 +00:00
|
|
|
public int Rotation { get { return rotation; } } //mxd
|
|
|
|
public TexturePathRenderStyle RenderStyle { get { return renderStyle; } } //mxd
|
|
|
|
public TexturePathBlendStyle BlendStyle { get { return blendStyle; } }
|
|
|
|
public float TintAmmount { get { return tintAmmount; } }
|
|
|
|
public PixelColor BlendColor { get { return blendColor; } }//mxd
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Constructor / Disposer
|
|
|
|
|
|
|
|
// Constructor
|
|
|
|
internal PatchStructure(TexturesParser parser)
|
|
|
|
{
|
|
|
|
// Initialize
|
|
|
|
alpha = 1.0f;
|
2013-03-18 13:52:27 +00:00
|
|
|
renderStyle = TexturePathRenderStyle.Copy;//mxd
|
|
|
|
blendStyle = TexturePathBlendStyle.None; //mxd
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// There should be 3 tokens separated by 2 commas now:
|
|
|
|
// Name, Width, Height
|
|
|
|
|
|
|
|
// First token is the class name
|
|
|
|
parser.SkipWhitespace(true);
|
2015-12-17 10:07:28 +00:00
|
|
|
name = parser.StripTokenQuotes(parser.ReadToken(false)); //mxd. Don't skip newline
|
2009-04-19 18:07:22 +00:00
|
|
|
if(string.IsNullOrEmpty(name))
|
|
|
|
{
|
|
|
|
parser.ReportError("Expected patch name");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-25 11:52:01 +00:00
|
|
|
//mxd
|
|
|
|
name = name.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
|
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
// 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 patch 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 offsetx))
|
|
|
|
{
|
|
|
|
parser.ReportError("Expected offset 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 patch height
|
|
|
|
parser.SkipWhitespace(true);
|
|
|
|
tokenstr = parser.ReadToken();
|
|
|
|
if(string.IsNullOrEmpty(tokenstr) || !int.TryParse(tokenstr, NumberStyles.Integer, CultureInfo.InvariantCulture, out offsety))
|
|
|
|
{
|
|
|
|
parser.ReportError("Expected offset in pixels");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-21 14:17:47 +00:00
|
|
|
// Next token is the beginning of the texture scope. If not, then the patch info ends here.
|
|
|
|
if(!parser.NextTokenIs("{", false)) return; //mxd
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// 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-28 15:01:53 +00:00
|
|
|
switch(token)
|
2014-12-03 23:15:26 +00:00
|
|
|
{
|
2013-12-18 09:11:04 +00:00
|
|
|
case "flipx":
|
|
|
|
flipx = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "flipy":
|
|
|
|
flipy = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "alpha":
|
2015-12-28 15:01:53 +00:00
|
|
|
if(!ReadTokenFloat(parser, token, out alpha)) return;
|
2013-12-18 09:11:04 +00:00
|
|
|
alpha = General.Clamp(alpha, 0.0f, 1.0f);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "rotate":
|
2015-12-17 10:07:28 +00:00
|
|
|
if(!ReadTokenInt(parser, token, out rotation)) return;
|
2013-12-18 09:11:04 +00:00
|
|
|
rotation = rotation % 360; //Coalesce multiples
|
2015-12-17 10:07:28 +00:00
|
|
|
if(rotation < 0) rotation += 360; //Force positive
|
2013-12-18 09:11:04 +00:00
|
|
|
|
2015-12-17 10:07:28 +00:00
|
|
|
if(rotation != 0 && rotation != 90 && rotation != 180 && rotation != 270)
|
2014-12-03 23:15:26 +00:00
|
|
|
{
|
2015-12-21 14:17:47 +00:00
|
|
|
parser.LogWarning("Unsupported rotation (" + rotation + ") in patch '" + name + "'");
|
2013-12-18 09:11:04 +00:00
|
|
|
rotation = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "style": //mxd
|
2014-02-21 14:42:12 +00:00
|
|
|
string s;
|
2015-12-17 10:07:28 +00:00
|
|
|
if(!ReadTokenString(parser, token, out s)) return;
|
2013-12-18 09:11:04 +00:00
|
|
|
int index = Array.IndexOf(renderStyles, s.ToLowerInvariant());
|
|
|
|
renderStyle = index == -1 ? TexturePathRenderStyle.Copy : (TexturePathRenderStyle) index;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "blend": //mxd
|
2014-02-21 14:42:12 +00:00
|
|
|
int val;
|
2015-12-17 10:07:28 +00:00
|
|
|
if(!ReadTokenColor(parser, token, out val)) return;
|
2013-12-18 09:11:04 +00:00
|
|
|
blendColor = PixelColor.FromInt(val);
|
|
|
|
|
2013-03-18 13:52:27 +00:00
|
|
|
parser.SkipWhitespace(false);
|
2013-12-18 09:11:04 +00:00
|
|
|
token = parser.ReadToken();
|
|
|
|
|
2015-12-17 10:07:28 +00:00
|
|
|
if(token == ",") //read tint ammount
|
2014-12-03 23:15:26 +00:00
|
|
|
{
|
2013-12-18 09:11:04 +00:00
|
|
|
parser.SkipWhitespace(false);
|
2015-12-17 10:07:28 +00:00
|
|
|
if(!ReadTokenFloat(parser, token, out tintAmmount)) return;
|
2013-12-18 09:11:04 +00:00
|
|
|
tintAmmount = General.Clamp(tintAmmount, 0.0f, 1.0f);
|
|
|
|
blendStyle = TexturePathBlendStyle.Tint;
|
2014-12-03 23:15:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-18 09:11:04 +00:00
|
|
|
blendStyle = TexturePathBlendStyle.Blend;
|
|
|
|
// Rewind so this structure can be read again
|
2015-02-19 13:50:17 +00:00
|
|
|
parser.DataStream.Seek(-token.Length, SeekOrigin.Current);
|
2013-12-18 09:11:04 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "}":
|
|
|
|
// Patch 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))
|
|
|
|
{
|
|
|
|
parser.ReportError("Expected numeric value for property '" + propertyname + "'");
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-18 09:11:04 +00:00
|
|
|
// Success
|
|
|
|
return true;
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
2013-12-18 09:11:04 +00:00
|
|
|
|
|
|
|
// Can't find the property value!
|
|
|
|
parser.ReportError("Expected a value for property '" + propertyname + "'");
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
parser.ReportError("Expected integral value for property '" + propertyname + "'");
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-18 09:11:04 +00:00
|
|
|
|
|
|
|
// Success
|
|
|
|
return true;
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
2013-12-18 09:11:04 +00:00
|
|
|
|
|
|
|
// Can't find the property value!
|
|
|
|
parser.ReportError("Expected a value for property '" + propertyname + "'");
|
|
|
|
value = 0;
|
|
|
|
return false;
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
|
2013-03-18 13:52:27 +00:00
|
|
|
//mxd. This reads the next token and sets a string value, returns false when failed
|
2014-12-03 23:15:26 +00:00
|
|
|
private static bool ReadTokenString(TexturesParser parser, string propertyname, out string value)
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
parser.SkipWhitespace(true);
|
|
|
|
value = parser.StripTokenQuotes(parser.ReadToken());
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
if(string.IsNullOrEmpty(value))
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
// Can't find the property value!
|
|
|
|
parser.ReportError("Expected a value for property '" + propertyname + "'");
|
|
|
|
return false;
|
|
|
|
}
|
2015-12-17 10:07:28 +00:00
|
|
|
|
2013-03-18 13:52:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//mxd. This reads the next token and sets a PixelColor value, returns false when failed
|
2014-12-03 23:15:26 +00:00
|
|
|
private static bool ReadTokenColor(TexturesParser parser, string propertyname, out int value)
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
parser.SkipWhitespace(true);
|
|
|
|
string strvalue = parser.StripTokenQuotes(parser.ReadToken());
|
|
|
|
value = 0;
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
if(string.IsNullOrEmpty(strvalue))
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
// Can't find the property value!
|
|
|
|
parser.ReportError("Expected a value for property '" + propertyname + "'");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
if(strvalue[0] != '#')
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
parser.ReportError("Expected color value for property '" + propertyname + "'");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try parsing as value
|
2014-12-03 23:15:26 +00:00
|
|
|
if(!int.TryParse(strvalue.Remove(0, 1), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out value))
|
|
|
|
{
|
2013-03-18 13:52:27 +00:00
|
|
|
parser.ReportError("Expected color value for property '" + propertyname + "'");
|
|
|
|
return false;
|
2015-12-17 10:07:28 +00:00
|
|
|
}
|
|
|
|
|
2013-03-18 13:52:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|