changeset 261:b9b8b0e60c31

Re #281: Export multiple stat lines in HTML * Add initial version of multiple stat lines per unit - still needs some work Also: * Line ending cleanup
author IBBoard <dev@ibboard.co.uk>
date Sat, 29 May 2010 20:03:17 +0000
parents b72cc74a240b
children 900adf96a915
files api/Exporters/WarFoundryHtmlExporter.cs api/Factories/Xml/WarFoundryXmlFactory.cs
diffstat 2 files changed, 122 insertions(+), 44 deletions(-) [+]
line diff
     1.1 --- a/api/Exporters/WarFoundryHtmlExporter.cs	Fri May 21 19:41:02 2010 +0000
     1.2 +++ b/api/Exporters/WarFoundryHtmlExporter.cs	Sat May 29 20:03:17 2010 +0000
     1.3 @@ -21,6 +21,7 @@
     1.4  	public class WarFoundryHtmlExporter : IWarFoundryExporter
     1.5  	{
     1.6  		private static WarFoundryHtmlExporter exporter;
     1.7 +		private delegate string GetStatCellTextDelegate(Stat stat);
     1.8  		
     1.9  		public static WarFoundryHtmlExporter GetDefault()
    1.10  		{
    1.11 @@ -56,7 +57,8 @@
    1.12  			metaCharset.SetAttribute("content", "text/html;charset=UTF-8");
    1.13  			head.AppendChild(metaCharset);
    1.14  			XmlElement style = doc.CreateElement("style");
    1.15 -			style.InnerText = "table, th, td { border: 1px solid #000 }";
    1.16 +			style.InnerText = "table, th, td { border: 1px solid #000; border-spacing: 0 }" +
    1.17 +				"table table { width: 100% }";
    1.18  			head.AppendChild(style);
    1.19  			XmlElement body = doc.CreateElement("body");
    1.20  			html.AppendChild(body);
    1.21 @@ -100,11 +102,22 @@
    1.22  			
    1.23  			foreach (Unit unit in army.GetUnits())
    1.24  			{
    1.25 -				tables[unit.UnitType.StatsID].AppendChild(CreateUnitRow(unit, doc));
    1.26 +				tables[GetFirstStatType(unit)].AppendChild(CreateUnitRow(unit, doc));
    1.27  			}
    1.28  			
    1.29  			return DictionaryUtils.ToArray(tables);
    1.30  		}
    1.31 +
    1.32 +		private static string GetFirstStatType(Unit unit)
    1.33 +		{
    1.34 +			string[] unitStatIDs = unit.UnitStatsArrayIDs;
    1.35 +			return GetFirstStatType(unitStatIDs);
    1.36 +		}
    1.37 +		
    1.38 +		public static string GetFirstStatType(string[] unitStatIDs)
    1.39 +		{
    1.40 +			return unitStatIDs[0];
    1.41 +		}
    1.42  		
    1.43  		private XmlElement CreateTable(SystemStats stats, XmlDocument doc)
    1.44  		{
    1.45 @@ -115,6 +128,10 @@
    1.46  			name.InnerText = Translation.GetTranslation("armyHtmlOutputTableHeaderUnitName", "name");
    1.47  			headerRow.AppendChild(name);
    1.48  			
    1.49 +			XmlElement unitTypeName = doc.CreateElement("th");
    1.50 +			unitTypeName.InnerText = Translation.GetTranslation("armyHtmlOutputTableHeaderUnitTypeName", "type name");
    1.51 +			headerRow.AppendChild(unitTypeName);
    1.52 +			
    1.53  			foreach (StatSlot stat in stats.StatSlots)
    1.54  			{
    1.55  				XmlElement statHeader = doc.CreateElement("th");
    1.56 @@ -139,13 +156,7 @@
    1.57  			XmlElement name = doc.CreateElement("td");
    1.58  			name.InnerText = unit.Name;
    1.59  			row.AppendChild(name);
    1.60 -			
    1.61 -			foreach (Stat stat in unit.UnitStatsArray)
    1.62 -			{
    1.63 -				XmlElement statCell = doc.CreateElement("td");
    1.64 -				statCell.InnerText = stat.SlotValueString;
    1.65 -				row.AppendChild(statCell);
    1.66 -			}
    1.67 +			CreateStatsBlock(unit, row);
    1.68  
    1.69  			StringBuilder sb = new StringBuilder();
    1.70  			UnitEquipmentItem[] unitEquipment = unit.GetEquipment ();
    1.71 @@ -233,6 +244,73 @@
    1.72  			return row;
    1.73  		}
    1.74  		
    1.75 +		private void CreateStatsBlock(Unit unit, XmlElement unitRow)
    1.76 +		{
    1.77 +			XmlDocument doc = unitRow.OwnerDocument;
    1.78 +			XmlElement statsWrapperCell = doc.CreateElement("td");
    1.79 +			unitRow.AppendChild(statsWrapperCell);
    1.80 +			Stat[][] memberStats = unit.UnitStatsArraysWithName;
    1.81 +			statsWrapperCell.SetAttribute("colspan", memberStats[0].Length.ToString());
    1.82 +			string[] statTypeIDs = unit.UnitStatsArrayIDs;
    1.83 +			string defaultStatType = GetFirstStatType(statTypeIDs);
    1.84 +			Dictionary<string, XmlElement> statTables = CreateEmptyStatsTables(statTypeIDs, statsWrapperCell);
    1.85 +			
    1.86 +			int statCount = statTypeIDs.Length;
    1.87 +			
    1.88 +			for (int i = 0; i < statCount; i++)
    1.89 +			{
    1.90 +				Stat[] statLine = memberStats[i];
    1.91 +				string statTypeID = statTypeIDs[i];
    1.92 +				XmlElement tableElement = DictionaryUtils.GetValue(statTables, statTypeID);
    1.93 +				int statLineCount = statLine.Length;
    1.94 +				XmlElement statRow = doc.CreateElement("tr");
    1.95 +				tableElement.AppendChild(statRow);
    1.96 +				GetStatCellTextDelegate statCellTextDelegate = (statTypeID.Equals(defaultStatType) ? new GetStatCellTextDelegate(GetDefaultStatCellText) : new GetStatCellTextDelegate(GetOtherStatCellText));
    1.97 +				AddStatCell(statLine[0].SlotValueString, statRow);
    1.98 +				
    1.99 +				for (int j = 1; j < statLineCount; j++)
   1.100 +				{
   1.101 +					string statText = statCellTextDelegate(statLine[j]);
   1.102 +					AddStatCell(statText, statRow);
   1.103 +				}
   1.104 +			}
   1.105 +		}
   1.106 +
   1.107 +		private Dictionary<string, XmlElement> CreateEmptyStatsTables(string[] statTypeIDs, XmlElement containerCell)
   1.108 +		{
   1.109 +			Dictionary<string, XmlElement> statTables = new Dictionary<string, XmlElement>();
   1.110 +			XmlDocument doc = containerCell.OwnerDocument;
   1.111 +			
   1.112 +			foreach (string statTypeID in statTypeIDs)
   1.113 +			{
   1.114 +				if (!statTables.ContainsKey(statTypeID))
   1.115 +				{
   1.116 +					XmlElement tableElement = doc.CreateElement("table");
   1.117 +					containerCell.AppendChild(tableElement);
   1.118 +					statTables[statTypeID] = tableElement;
   1.119 +				}
   1.120 +			}
   1.121 +			
   1.122 +			return statTables;
   1.123 +		}
   1.124 +
   1.125 +		private string GetDefaultStatCellText(Stat stat)
   1.126 +		{
   1.127 +			return Translation.GetTranslation("armyHtmlExportDefaultStatCellText", "{0}", stat.SlotValueString, stat.ParentSlotName);
   1.128 +		}
   1.129 +
   1.130 +		private string GetOtherStatCellText(Stat stat)
   1.131 +		{
   1.132 +			return Translation.GetTranslation("armyHtmlExportOtherStatCellText", "{1}: {0}", stat.SlotValueString, stat.ParentSlotName);
   1.133 +		}
   1.134 +		
   1.135 +		private static void AddStatCell(string statValue, XmlElement row)
   1.136 +		{
   1.137 +			XmlElement statCell = row.OwnerDocument.CreateElement("td");
   1.138 +			statCell.InnerText = statValue;
   1.139 +			row.AppendChild(statCell);
   1.140 +		}
   1.141 +		
   1.142  		private string GetEquipmentAmountRatioTranslation (double amount, int number)
   1.143  		{
   1.144  			return Translation.GetTranslation ("armyHtmlExportEquipAmountPercentage", "{0}% ({1})", amount, number);
     2.1 --- a/api/Factories/Xml/WarFoundryXmlFactory.cs	Fri May 21 19:41:02 2010 +0000
     2.2 +++ b/api/Factories/Xml/WarFoundryXmlFactory.cs	Sat May 29 20:03:17 2010 +0000
     2.3 @@ -133,42 +133,42 @@
     2.4  
     2.5  		public override void CompleteLoading(IWarFoundryStagedLoadObject obj)
     2.6  		{			
     2.7 -			LogNotifier.DebugFormat(GetType(), "Complete loading of {0} with ID {1}", obj.GetType().Name, obj.ID);
     2.8 -
     2.9 -			if (obj is GameSystem)
    2.10 -			{
    2.11 -				CompleteLoadingGameSystem((GameSystem) obj);
    2.12 -			}
    2.13 -			else if (obj is Race)
    2.14 -			{
    2.15 -				CompleteLoadingRace((Race) obj);
    2.16 +			LogNotifier.DebugFormat(GetType(), "Complete loading of {0} with ID {1}", obj.GetType().Name, obj.ID);
    2.17 +
    2.18 +			if (obj is GameSystem)
    2.19 +			{
    2.20 +				CompleteLoadingGameSystem((GameSystem) obj);
    2.21  			}
    2.22 -		}
    2.23 -
    2.24 -		private void CompleteLoadingRace(Race race)
    2.25 -		{
    2.26 -			try
    2.27 -			{
    2.28 -				raceFactory.CompleteLoading(race);
    2.29 -			}
    2.30 -			catch (InvalidFileException ex)
    2.31 -			{
    2.32 -				WarFoundryLoader.GetDefault().RemoveRace(race);
    2.33 -				throw;
    2.34 -			}
    2.35 -		}
    2.36 -
    2.37 -		private void CompleteLoadingGameSystem(GameSystem system)
    2.38 -		{
    2.39 -			try
    2.40 -			{
    2.41 -				gameSystemFactory.CompleteLoading(system);
    2.42 -			}
    2.43 -			catch (InvalidFileException ex)
    2.44 -			{
    2.45 -				WarFoundryLoader.GetDefault().RemoveGameSystem(system);
    2.46 -				throw;
    2.47 -			}
    2.48 +			else if (obj is Race)
    2.49 +			{
    2.50 +				CompleteLoadingRace((Race) obj);
    2.51 +			}
    2.52 +		}
    2.53 +
    2.54 +		private void CompleteLoadingRace(Race race)
    2.55 +		{
    2.56 +			try
    2.57 +			{
    2.58 +				raceFactory.CompleteLoading(race);
    2.59 +			}
    2.60 +			catch (InvalidFileException ex)
    2.61 +			{
    2.62 +				WarFoundryLoader.GetDefault().RemoveRace(race);
    2.63 +				throw;
    2.64 +			}
    2.65 +		}
    2.66 +
    2.67 +		private void CompleteLoadingGameSystem(GameSystem system)
    2.68 +		{
    2.69 +			try
    2.70 +			{
    2.71 +				gameSystemFactory.CompleteLoading(system);
    2.72 +			}
    2.73 +			catch (InvalidFileException ex)
    2.74 +			{
    2.75 +				WarFoundryLoader.GetDefault().RemoveGameSystem(system);
    2.76 +				throw;
    2.77 +			}
    2.78  		}
    2.79  	}
    2.80  }
    2.81 \ No newline at end of file