Add support for custom thing categories

This commit is contained in:
MascaraSnake 2017-07-07 21:43:44 +02:00
parent e7306d016d
commit 93fc8fb78f
4 changed files with 66 additions and 11 deletions

View file

@ -1773,9 +1773,10 @@ namespace CodeImp.DoomBuilder.Data
if (objects.Count > 0)
{
ThingCategory cat = new ThingCategory(null, "customthings", "Custom Things");
foreach (KeyValuePair<string,SRB2Object> o in objects)
{
string catname = ZDTextParser.StripQuotes(o.Value.category);
ThingCategory cat = GetSRB2ThingCategory(thingcategories, new string[] { ZDTextParser.StripQuotes(o.Value.category) });
ThingTypeInfo t = new ThingTypeInfo(cat, o.Value);
cat.AddThing(t);
// Check if we can find this thing in our existing collection
@ -1791,8 +1792,6 @@ namespace CodeImp.DoomBuilder.Data
thingtypes.Add(t.Index, t);
}
}
thingcategories.Add(cat);
}
return objects.Count;
}
@ -1842,9 +1841,51 @@ namespace CodeImp.DoomBuilder.Data
return cat;
}
// This loads Decorate data from a specific file or lump name
private void LoadDecorateFromLocation(DecorateParser parser, string location)
private static ThingCategory GetSRB2ThingCategory(List<ThingCategory> categories, string[] catnames)
{
// Find the category to put the actor in
ThingCategory cat = null;
string catname = catnames[0].ToLowerInvariant().Trim();
if (string.IsNullOrEmpty(catname)) catname = "customthings";
// First search by Title...
foreach (ThingCategory c in categories)
{
if (c.Title.ToLowerInvariant() == catname) cat = c;
}
//...then - by Name
if (cat == null)
{
foreach (ThingCategory c in categories)
{
if (c.Name.ToLowerInvariant() == catname) cat = c;
}
}
// Make the category if needed
if (cat == null)
{
string cattitle = catnames[0].Trim();
if (string.IsNullOrEmpty(cattitle)) cattitle = "Custom Things";
cat = new ThingCategory(null, catname, cattitle);
categories.Add(cat); // ^.^
}
// Still have subcategories?
if (catnames.Length > 1)
{
string[] remainingnames = new string[catnames.Length - 1];
Array.Copy(catnames, 1, remainingnames, 0, remainingnames.Length);
return GetThingCategory(cat, cat.Children, remainingnames);
}
return cat;
}
// This loads Decorate data from a specific file or lump name
private void LoadDecorateFromLocation(DecorateParser parser, string location)
{
//General.WriteLogLine("Including DECORATE resource '" + location + "'...");
Dictionary<string, Stream> decostreams = currentreader.GetDecorateData(location);

View file

@ -88,6 +88,7 @@ namespace CodeImp.DoomBuilder.SRB2
string objname = token.Substring(9).TrimEnd(new char[] { ']' });
string name = objname;
string sprite = DataManager.INTERNAL_PREFIX + "unknownthing";
string category = "";
string[] states = new string[8];
int mapThingNum = -1;
int radius = 0;
@ -121,7 +122,7 @@ namespace CodeImp.DoomBuilder.SRB2
case "$Name":
SkipWhitespace(true);
token = ReadLine();
name = token;
name = ZDTextParser.StripQuotes(token);
break;
case "$Sprite":
SkipWhitespace(true);
@ -135,6 +136,11 @@ namespace CodeImp.DoomBuilder.SRB2
}
LogWarning("The sprite \"" + token + "\" assigned by the \"$sprite\" property does not exist");
return false;
case "$Category":
SkipWhitespace(true);
token = ReadLine();
category = token;
break;
case "doomednum":
if (!ReadParameter(out token, out finished)) return false;
if (!int.TryParse(token, NumberStyles.Integer, CultureInfo.InvariantCulture, out mapThingNum))
@ -231,7 +237,7 @@ namespace CodeImp.DoomBuilder.SRB2
if (mapThingNum > 0)
{
SRB2Object o = new SRB2Object(name, sprite, states, mapThingNum, radius, height);
SRB2Object o = new SRB2Object(name, sprite, category, states, mapThingNum, radius, height);
if (objects.ContainsKey(objname))
objects[objname] = o;
else

View file

@ -139,6 +139,7 @@ namespace CodeImp.DoomBuilder.SRB2
{
if (objname == null) return false;
string name = objname;
string category = "";
string sprite = DataManager.INTERNAL_PREFIX + "unknownthing";
string[] states = new string[8];
int mapThingNum = -1;
@ -163,7 +164,12 @@ namespace CodeImp.DoomBuilder.SRB2
}
if (line.StartsWith("#$Name "))
{
name = line.Substring(7);
name = ZDTextParser.StripQuotes(line.Substring(7));
continue;
}
if (line.StartsWith("#$Category "))
{
category = line.Substring(11);
continue;
}
if (line.StartsWith("#")) continue;
@ -230,7 +236,7 @@ namespace CodeImp.DoomBuilder.SRB2
}
if (mapThingNum > 0)
{
SRB2Object o = new SRB2Object(name, sprite, states, mapThingNum, radius, height);
SRB2Object o = new SRB2Object(name, sprite, category, states, mapThingNum, radius, height);
if (objects.ContainsKey(objname))
objects[objname] = o;
else

View file

@ -34,6 +34,7 @@ namespace CodeImp.DoomBuilder.SRB2
public readonly string name;
public readonly string sprite;
public readonly string category;
public readonly string[] states;
public readonly int mapThingNum;
public readonly int radius;
@ -44,10 +45,11 @@ namespace CodeImp.DoomBuilder.SRB2
#region ================== Constructor / Disposer
// Constructor
internal SRB2Object(string name, string sprite, string[] states, int mapThingNum, int radius, int height)
internal SRB2Object(string name, string sprite, string category, string[] states, int mapThingNum, int radius, int height)
{
this.name = name;
this.sprite = sprite;
this.category = category;
this.states = states;
this.mapThingNum = mapThingNum;
this.radius = radius;