changeset 262:900adf96a915

Closes #281: Export multiple stat lines in HTML * Rebuild HTML generation to take advantage of existing columns where stat type matches table stat type * Tidy up the styling
author IBBoard <dev@ibboard.co.uk>
date Mon, 31 May 2010 15:33:41 +0000
parents b9b8b0e60c31
children 7933d852181e
files api/Exporters/WarFoundryHtmlExporter.cs
diffstat 1 files changed, 102 insertions(+), 35 deletions(-) [+]
line diff
     1.1 --- a/api/Exporters/WarFoundryHtmlExporter.cs	Sat May 29 20:03:17 2010 +0000
     1.2 +++ b/api/Exporters/WarFoundryHtmlExporter.cs	Mon May 31 15:33:41 2010 +0000
     1.3 @@ -57,8 +57,9 @@
     1.4  			metaCharset.SetAttribute("content", "text/html;charset=UTF-8");
     1.5  			head.AppendChild(metaCharset);
     1.6  			XmlElement style = doc.CreateElement("style");
     1.7 -			style.InnerText = "table, th, td { border: 1px solid #000; border-spacing: 0 }" +
     1.8 -				"table table { width: 100% }";
     1.9 +			style.InnerText = "able, th, td { border: 1px solid #000; border-spacing: 0; border-collapse: collapse; margin: 0 }\n"
    1.10 +				+"table table { width: 100%; border-width: 0; margin: -2px }\n"
    1.11 +				+"table table td { border-width:0 1px }";
    1.12  			head.AppendChild(style);
    1.13  			XmlElement body = doc.CreateElement("body");
    1.14  			html.AppendChild(body);
    1.15 @@ -102,7 +103,7 @@
    1.16  			
    1.17  			foreach (Unit unit in army.GetUnits())
    1.18  			{
    1.19 -				tables[GetFirstStatType(unit)].AppendChild(CreateUnitRow(unit, doc));
    1.20 +				CreateUnitRow(unit, tables[GetFirstStatType(unit)]);
    1.21  			}
    1.22  			
    1.23  			return DictionaryUtils.ToArray(tables);
    1.24 @@ -150,16 +151,38 @@
    1.25  			return table;
    1.26  		}
    1.27  		
    1.28 -		private XmlElement CreateUnitRow(Unit unit, XmlDocument doc)
    1.29 +		private XmlElement CreateUnitRow(Unit unit, XmlElement tableElem)
    1.30  		{
    1.31 +			XmlDocument doc = tableElem.OwnerDocument;
    1.32  			XmlElement row = doc.CreateElement("tr");
    1.33 +			tableElem.AppendChild(row);
    1.34 +			Stat[][] memberStats = unit.UnitStatsArraysWithName;
    1.35 +			string[] statTypeIDs = unit.UnitStatsArrayIDs;
    1.36 +			string defaultStatType = GetFirstStatType(statTypeIDs);
    1.37 +			int statRowCount = 0;
    1.38 +			bool hasOther = false;
    1.39 +			
    1.40 +			foreach (string statTypeID in statTypeIDs)
    1.41 +			{
    1.42 +				if (statTypeID.Equals(defaultStatType))
    1.43 +				{
    1.44 +					statRowCount++;
    1.45 +				}
    1.46 +				else if (!hasOther)
    1.47 +				{
    1.48 +					statRowCount++;
    1.49 +					hasOther = true;
    1.50 +				}
    1.51 +			}
    1.52 +			
    1.53  			XmlElement name = doc.CreateElement("td");
    1.54  			name.InnerText = unit.Name;
    1.55 +			SetRowSpan(name, statRowCount);
    1.56  			row.AppendChild(name);
    1.57 -			CreateStatsBlock(unit, row);
    1.58 +			CreateStatsBlock(row, memberStats, statTypeIDs);
    1.59  
    1.60  			StringBuilder sb = new StringBuilder();
    1.61 -			UnitEquipmentItem[] unitEquipment = unit.GetEquipment ();
    1.62 +			UnitEquipmentItem[] unitEquipment = unit.GetEquipment();
    1.63  			
    1.64  			if (unitEquipment.Length > 0)
    1.65  			{
    1.66 @@ -189,7 +212,7 @@
    1.67  						else
    1.68  						{
    1.69  							int number = UnitEquipmentUtil.GetEquipmentAmountTaken(unit, equip);
    1.70 -							amountString = GetEquipmentAmountRatioTranslation (amount, number);
    1.71 +							amountString = GetEquipmentAmountRatioTranslation(amount, number);
    1.72  						}
    1.73  					}
    1.74  					else
    1.75 @@ -213,7 +236,7 @@
    1.76  			ICollection<Ability> abilities = unit.Abilities;
    1.77  			
    1.78  			if (abilities.Count > 0)
    1.79 -			{				
    1.80 +			{
    1.81  				bool addSeparator = false;
    1.82  				
    1.83  				foreach (Ability ability in abilities)
    1.84 @@ -232,66 +255,110 @@
    1.85  				
    1.86  				sb.Append(". ");
    1.87  			}
    1.88 -						
    1.89 +			
    1.90  			XmlElement notes = doc.CreateElement("td");
    1.91  			notes.InnerText = sb.ToString();
    1.92 +			SetRowSpan(notes, statRowCount);
    1.93  			row.AppendChild(notes);
    1.94  			
    1.95  			XmlElement points = doc.CreateElement("td");
    1.96  			points.InnerText = unit.Points.ToString();
    1.97 +			SetRowSpan(points, statRowCount);
    1.98  			row.AppendChild(points);
    1.99  			
   1.100  			return row;
   1.101  		}
   1.102  		
   1.103 -		private void CreateStatsBlock(Unit unit, XmlElement unitRow)
   1.104 +		private static void SetRowSpan(XmlElement xmlElement, int statRowCount)
   1.105 +		{
   1.106 +			if (statRowCount > 1)
   1.107 +			{
   1.108 +				xmlElement.SetAttribute("rowspan", statRowCount.ToString());
   1.109 +			}
   1.110 +		}
   1.111 +		
   1.112 +		private void CreateStatsBlock(XmlElement unitRow, Stat[][] memberStats, string[] statTypeIDs)
   1.113  		{
   1.114  			XmlDocument doc = unitRow.OwnerDocument;
   1.115 -			XmlElement statsWrapperCell = doc.CreateElement("td");
   1.116 -			unitRow.AppendChild(statsWrapperCell);
   1.117 -			Stat[][] memberStats = unit.UnitStatsArraysWithName;
   1.118 -			statsWrapperCell.SetAttribute("colspan", memberStats[0].Length.ToString());
   1.119 -			string[] statTypeIDs = unit.UnitStatsArrayIDs;
   1.120  			string defaultStatType = GetFirstStatType(statTypeIDs);
   1.121 -			Dictionary<string, XmlElement> statTables = CreateEmptyStatsTables(statTypeIDs, statsWrapperCell);
   1.122 +			
   1.123 +			Stat[] defaultStatLine = memberStats[0];
   1.124 +			int defaultStatLineCount = defaultStatLine.Length;
   1.125 +			AddStatCell(defaultStatLine[0].SlotValueString, unitRow);
   1.126 +			
   1.127 +			for (int i = 1; i < defaultStatLineCount; i++)
   1.128 +			{
   1.129 +				string statText = GetDefaultStatCellText(defaultStatLine[i]);
   1.130 +				AddStatCell(statText, unitRow);
   1.131 +			}
   1.132  			
   1.133  			int statCount = statTypeIDs.Length;
   1.134  			
   1.135 -			for (int i = 0; i < statCount; i++)
   1.136 +			if (statCount > 1)
   1.137  			{
   1.138 -				Stat[] statLine = memberStats[i];
   1.139 -				string statTypeID = statTypeIDs[i];
   1.140 -				XmlElement tableElement = DictionaryUtils.GetValue(statTables, statTypeID);
   1.141 -				int statLineCount = statLine.Length;
   1.142 -				XmlElement statRow = doc.CreateElement("tr");
   1.143 -				tableElement.AppendChild(statRow);
   1.144 -				GetStatCellTextDelegate statCellTextDelegate = (statTypeID.Equals(defaultStatType) ? new GetStatCellTextDelegate(GetDefaultStatCellText) : new GetStatCellTextDelegate(GetOtherStatCellText));
   1.145 -				AddStatCell(statLine[0].SlotValueString, statRow);
   1.146 +				XmlElement unitTable = (XmlElement)unitRow.ParentNode;
   1.147 +				Dictionary<string, XmlElement> statParents = CreateStatsParentElements(statTypeIDs, unitTable);
   1.148  				
   1.149 -				for (int j = 1; j < statLineCount; j++)
   1.150 +				for (int i = 1; i < statCount; i++)
   1.151  				{
   1.152 -					string statText = statCellTextDelegate(statLine[j]);
   1.153 -					AddStatCell(statText, statRow);
   1.154 +					Stat[] statLine = memberStats[i];
   1.155 +					string statTypeID = statTypeIDs[i];
   1.156 +					XmlElement tableElement = DictionaryUtils.GetValue(statParents, statTypeID);
   1.157 +					int statLineCount = statLine.Length;
   1.158 +					XmlElement statRow = doc.CreateElement("tr");
   1.159 +					tableElement.AppendChild(statRow);
   1.160 +					GetStatCellTextDelegate statCellTextDelegate = (statTypeID.Equals(defaultStatType) ? new GetStatCellTextDelegate(GetDefaultStatCellText) : new GetStatCellTextDelegate(GetOtherStatCellText));
   1.161 +					AddStatCell(statLine[0].SlotValueString, statRow);
   1.162 +				
   1.163 +					for (int j = 1; j < statLineCount; j++)
   1.164 +					{
   1.165 +						string statText = statCellTextDelegate(statLine[j]);
   1.166 +						AddStatCell(statText, statRow);
   1.167 +					}
   1.168 +				}
   1.169 +				
   1.170 +				if (statParents.Count > 1)
   1.171 +				{
   1.172 +					AddOtherUnitStatTables(statParents, unitTable, defaultStatLineCount);
   1.173 +				}
   1.174 +			}
   1.175 +		}
   1.176 +		
   1.177 +		private static void AddOtherUnitStatTables(Dictionary<string, XmlElement> statParents, XmlElement unitTable, int defaultStatLineCount)
   1.178 +		{
   1.179 +			XmlDocument doc = unitTable.OwnerDocument;
   1.180 +			XmlElement otherStatsRow = doc.CreateElement("tr");
   1.181 +			unitTable.AppendChild(otherStatsRow);
   1.182 +			XmlElement otherStatsCell = doc.CreateElement("td");
   1.183 +			otherStatsCell.SetAttribute("colspan", defaultStatLineCount.ToString());
   1.184 +			otherStatsRow.AppendChild(otherStatsCell);
   1.185 +			
   1.186 +			foreach (XmlElement tableElem in statParents.Values)
   1.187 +			{
   1.188 +				if (tableElem != unitTable)
   1.189 +				{
   1.190 +					otherStatsCell.AppendChild(tableElem);
   1.191  				}
   1.192  			}
   1.193  		}
   1.194  
   1.195 -		private Dictionary<string, XmlElement> CreateEmptyStatsTables(string[] statTypeIDs, XmlElement containerCell)
   1.196 +		private Dictionary<string, XmlElement> CreateStatsParentElements(string[] statTypeIDs, XmlElement parentTable)
   1.197  		{
   1.198 -			Dictionary<string, XmlElement> statTables = new Dictionary<string, XmlElement>();
   1.199 -			XmlDocument doc = containerCell.OwnerDocument;
   1.200 +			Dictionary<string, XmlElement> statParents = new Dictionary<string, XmlElement>();
   1.201 +			XmlDocument doc = parentTable.OwnerDocument;
   1.202 +			string defaultStatTypeID = GetFirstStatType(statTypeIDs);
   1.203 +			statParents[defaultStatTypeID] = parentTable;
   1.204  			
   1.205  			foreach (string statTypeID in statTypeIDs)
   1.206  			{
   1.207 -				if (!statTables.ContainsKey(statTypeID))
   1.208 +				if (!statParents.ContainsKey(statTypeID))
   1.209  				{
   1.210  					XmlElement tableElement = doc.CreateElement("table");
   1.211 -					containerCell.AppendChild(tableElement);
   1.212 -					statTables[statTypeID] = tableElement;
   1.213 +					statParents[statTypeID] = tableElement;
   1.214  				}
   1.215  			}
   1.216  			
   1.217 -			return statTables;
   1.218 +			return statParents;
   1.219  		}
   1.220  
   1.221  		private string GetDefaultStatCellText(Stat stat)