VOID_CareerTracker: waitToResize relocated to updateTable.
VOID_CareerTracker: waitToResize relocated to updateTable.

// VOID_CareerTracker © 2015 toadicus // VOID_CareerTracker © 2015 toadicus
// //
// This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a // This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a
// copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ // copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/
   
using KSP; using KSP;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
using ToadicusTools; using ToadicusTools;
using UnityEngine; using UnityEngine;
   
namespace VOID.VOID_CareerTracker namespace VOID.VOID_CareerTracker
{ {
[VOID_Scenes(GameScenes.SPACECENTER, GameScenes.FLIGHT, GameScenes.EDITOR)] [VOID_Scenes(GameScenes.SPACECENTER, GameScenes.FLIGHT, GameScenes.EDITOR)]
[VOID_GameModes(Game.Modes.CAREER)] [VOID_GameModes(Game.Modes.CAREER)]
public class VOID_CareerTracker : VOID_WindowModule public class VOID_CareerTracker : VOID_WindowModule
{ {
private static VCTScenario Tracker private static VCTScenario Tracker
{ {
get get
{ {
return VCTScenario.Instance; return VCTScenario.Instance;
} }
} }
   
private int trackerCount; private int trackerCount;
private bool trackerChanged; private bool trackerChanged;
   
private Table ledgerTable; private Table ledgerTable;
   
private Table.Column<string> timeStampCol; private Table.Column<string> timeStampCol;
private Table.Column<string> reasonCol; private Table.Column<string> reasonCol;
private Table.Column<string> fundsDeltaCol; private Table.Column<string> fundsDeltaCol;
private Table.Column<double> fundsTotalCol; private Table.Column<double> fundsTotalCol;
private Table.Column<string> scienceDeltaCol; private Table.Column<string> scienceDeltaCol;
private Table.Column<float> scienceTotalCol; private Table.Column<float> scienceTotalCol;
private Table.Column<string> repDeltaCol; private Table.Column<string> repDeltaCol;
private Table.Column<float> repTotalCol; private Table.Column<float> repTotalCol;
   
private Vector2 scrollViewPos; private Vector2 scrollViewPos;
private float scrollWidth; private float scrollWidth;
   
private bool clearTable; private bool clearTable;
private short waitToResize; private short waitToResize;
   
[AVOID_SaveValue("IncludeFunds")] [AVOID_SaveValue("IncludeFunds")]
private VOID_SaveValue<bool> includeFunds; private VOID_SaveValue<bool> includeFunds;
   
[AVOID_SaveValue("IncludeScience")] [AVOID_SaveValue("IncludeScience")]
private VOID_SaveValue<bool> includeScience; private VOID_SaveValue<bool> includeScience;
   
[AVOID_SaveValue("IncludeReputation")] [AVOID_SaveValue("IncludeReputation")]
private VOID_SaveValue<bool> includeReputation; private VOID_SaveValue<bool> includeReputation;
   
protected override bool timeToUpdate protected override bool timeToUpdate
{ {
get get
{ {
return ( return (
(this.core.UpdateTimer - this.lastUpdate) > (this.core.UpdatePeriod * 2d) || (this.core.UpdateTimer - this.lastUpdate) > (this.core.UpdatePeriod * 2d) ||
(this.lastUpdate > this.core.UpdateTimer) (this.lastUpdate > this.core.UpdateTimer)
); );
} }
} }
   
public bool IncludeFunds public bool IncludeFunds
{ {
get get
{ {
return this.includeFunds.value; return this.includeFunds.value;
} }
private set private set
{ {
if (value != this.includeFunds && this.timeToUpdate) if (value != this.includeFunds && this.timeToUpdate)
{ {
this.clearTable = true; this.clearTable = true;
this.includeFunds.value = value; this.includeFunds.value = value;
} }
} }
} }
   
public bool IncludeScience public bool IncludeScience
{ {
get get
{ {
return this.includeScience.value; return this.includeScience.value;
} }
private set private set
{ {
if (value != this.includeScience && this.timeToUpdate) if (value != this.includeScience && this.timeToUpdate)
{ {
this.clearTable = true; this.clearTable = true;
this.includeScience.value = value; this.includeScience.value = value;
} }
} }
} }
   
public bool IncludeReputation public bool IncludeReputation
{ {
get get
{ {
return this.includeReputation.value; return this.includeReputation.value;
} }
private set private set
{ {
if (value != this.includeReputation && this.timeToUpdate) if (value != this.includeReputation && this.timeToUpdate)
{ {
this.clearTable = true; this.clearTable = true;
this.includeReputation.value = value; this.includeReputation.value = value;
} }
} }
} }
   
public VOID_CareerTracker() : base() public VOID_CareerTracker() : base()
{ {
this.Name = "Transaction Log"; this.Name = "Transaction Log";
   
this.defHeight = 320f; this.defHeight = 320f;
   
this.ledgerTable = new Table(); this.ledgerTable = new Table();
   
this.timeStampCol = new Table.Column<string>("Date", 20f); this.timeStampCol = new Table.Column<string>("Date", 20f);
this.ledgerTable.Add(this.timeStampCol); this.ledgerTable.Add(this.timeStampCol);
   
this.reasonCol = new Table.Column<string>("Reason", 20f); this.reasonCol = new Table.Column<string>("Reason", 20f);
this.ledgerTable.Add(this.reasonCol); this.ledgerTable.Add(this.reasonCol);
   
this.fundsDeltaCol = new Table.Column<string>("ΔFunds", 20f); this.fundsDeltaCol = new Table.Column<string>("ΔFunds", 20f);
this.ledgerTable.Add(this.fundsDeltaCol); this.ledgerTable.Add(this.fundsDeltaCol);
   
this.fundsTotalCol = new Table.Column<double>("Funds", 20f); this.fundsTotalCol = new Table.Column<double>("Funds", 20f);
this.fundsTotalCol.Format = "#,##0.00"; this.fundsTotalCol.Format = "#,##0.00";
this.ledgerTable.Add(this.fundsTotalCol); this.ledgerTable.Add(this.fundsTotalCol);
   
this.scienceDeltaCol = new Table.Column<string>("ΔScience", 20f); this.scienceDeltaCol = new Table.Column<string>("ΔScience", 20f);
this.ledgerTable.Add(this.scienceDeltaCol); this.ledgerTable.Add(this.scienceDeltaCol);
   
this.scienceTotalCol = new Table.Column<float>("Science", 20f); this.scienceTotalCol = new Table.Column<float>("Science", 20f);
this.scienceTotalCol.Format = "#,##0"; this.scienceTotalCol.Format = "#,##0";
this.ledgerTable.Add(this.scienceTotalCol); this.ledgerTable.Add(this.scienceTotalCol);
   
this.repDeltaCol = new Table.Column<string>("ΔReputation", 20f); this.repDeltaCol = new Table.Column<string>("ΔReputation", 20f);
this.ledgerTable.Add(this.repDeltaCol); this.ledgerTable.Add(this.repDeltaCol);
   
this.repTotalCol = new Table.Column<float>("Reputation", 20f); this.repTotalCol = new Table.Column<float>("Reputation", 20f);
this.repTotalCol.Format = "#,##0"; this.repTotalCol.Format = "#,##0";
this.ledgerTable.Add(this.repTotalCol); this.ledgerTable.Add(this.repTotalCol);
   
this.scrollViewPos = Vector2.zero; this.scrollViewPos = Vector2.zero;
this.scrollWidth = 0f; this.scrollWidth = 0f;
   
this.waitToResize = 5; this.waitToResize = 5;
   
this.includeFunds = (VOID_SaveValue<bool>)true; this.includeFunds = (VOID_SaveValue<bool>)true;
this.includeScience = (VOID_SaveValue<bool>)true; this.includeScience = (VOID_SaveValue<bool>)true;
this.includeReputation = (VOID_SaveValue<bool>)true; this.includeReputation = (VOID_SaveValue<bool>)true;
   
this.core.onSkinChanged += this.onSkinChangedHandler; this.core.onSkinChanged += this.onSkinChangedHandler;
   
this.onSkinChangedHandler(null); this.onSkinChangedHandler(null);
} }
   
private void onSkinChangedHandler(object sender) private void onSkinChangedHandler(object sender)
{ {
this.clearTable = true; this.clearTable = true;
   
this.ledgerTable.ApplyHeaderStyle(VOID_Styles.labelCenterBold); this.ledgerTable.ApplyHeaderStyle(VOID_Styles.labelCenterBold);
this.ledgerTable.ApplyCellStyle(VOID_Styles.labelRight); this.ledgerTable.ApplyCellStyle(VOID_Styles.labelRight);
   
this.timeStampCol.CellStyle = VOID_Styles.labelCenter; this.timeStampCol.CellStyle = VOID_Styles.labelCenter;
this.reasonCol.CellStyle = VOID_Styles.labelCenter; this.reasonCol.CellStyle = VOID_Styles.labelCenter;
} }
   
public override void ModuleWindow(int id) public override void ModuleWindow(int id)
{ {
if (this.timeToUpdate) if (this.timeToUpdate)
{ {
int currentTrackerCount = Tracker.TransactionList.Count; int currentTrackerCount = Tracker.TransactionList.Count;
   
this.trackerChanged = (this.trackerCount != currentTrackerCount); this.trackerChanged = (this.trackerCount != currentTrackerCount);
   
if (trackerChanged) if (trackerChanged)
{ {
this.trackerCount = currentTrackerCount; this.trackerCount = currentTrackerCount;
} }
   
if (this.clearTable) if (this.clearTable)
{ {
this.ledgerTable.ClearTable(); this.ledgerTable.ClearTable();
   
this.ledgerTable.Add(this.timeStampCol); this.ledgerTable.Add(this.timeStampCol);
this.ledgerTable.Add(this.reasonCol); this.ledgerTable.Add(this.reasonCol);
   
if (this.IncludeFunds) if (this.IncludeFunds)
{ {
this.ledgerTable.Add(this.fundsDeltaCol); this.ledgerTable.Add(this.fundsDeltaCol);
this.ledgerTable.Add(this.fundsTotalCol); this.ledgerTable.Add(this.fundsTotalCol);
} }
   
if (this.IncludeScience) if (this.IncludeScience)
{ {
this.ledgerTable.Add(this.scienceDeltaCol); this.ledgerTable.Add(this.scienceDeltaCol);
this.ledgerTable.Add(this.scienceTotalCol); this.ledgerTable.Add(this.scienceTotalCol);
} }
   
if (this.IncludeReputation) if (this.IncludeReputation)
{ {
this.ledgerTable.Add(this.repDeltaCol); this.ledgerTable.Add(this.repDeltaCol);
this.ledgerTable.Add(this.repTotalCol); this.ledgerTable.Add(this.repTotalCol);
} }
   
this.clearTable = false; this.clearTable = false;
   
this.waitToResize = 5;  
   
this.updateTable(); this.updateTable();
} }
else if (trackerChanged) else if (trackerChanged)
{ {
this.ledgerTable.ClearColumns(); this.ledgerTable.ClearColumns();
   
this.waitToResize = 5;  
   
this.updateTable(); this.updateTable();
} }
} }
   
RectOffset padding = GUI.skin.scrollView.padding; RectOffset padding = GUI.skin.scrollView.padding;
   
GUILayout.BeginVertical(GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(false)); GUILayout.BeginVertical(GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(false));
   
GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(false)); GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(false));
   
this.IncludeFunds = GUILayout.Toggle( this.IncludeFunds = GUILayout.Toggle(
this.IncludeFunds, this.IncludeFunds,
"Funds", "Funds",
GUI.skin.button, GUI.skin.button,
GUILayout.ExpandWidth(true) GUILayout.ExpandWidth(true)
); );
   
this.IncludeScience = GUILayout.Toggle( this.IncludeScience = GUILayout.Toggle(
this.IncludeScience, this.IncludeScience,
"Science", "Science",
GUI.skin.button, GUI.skin.button,
GUILayout.ExpandWidth(true) GUILayout.ExpandWidth(true)
); );
   
this.IncludeReputation = GUILayout.Toggle( this.IncludeReputation = GUILayout.Toggle(
this.IncludeReputation, this.IncludeReputation,
"Reputation", "Reputation",
GUI.skin.button, GUI.skin.button,
GUILayout.ExpandWidth(true) GUILayout.ExpandWidth(true)
); );
   
GUILayout.EndHorizontal(); GUILayout.EndHorizontal();
   
GUILayout.EndVertical(); GUILayout.EndVertical();
   
padding.bottom = padding.top = 0; padding.bottom = padding.top = 0;
   
if (this.IncludeFunds | this.IncludeScience | this.IncludeReputation) if (this.IncludeFunds | this.IncludeScience | this.IncludeReputation)
{ {
this.ledgerTable.RenderHeader(true); this.ledgerTable.RenderHeader(true);
   
GUILayout.BeginVertical(GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true)); GUILayout.BeginVertical(GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
   
this.scrollViewPos = GUILayout.BeginScrollView( this.scrollViewPos = GUILayout.BeginScrollView(
this.scrollViewPos, this.scrollViewPos,
false, false,
true, true,
GUIStyle.none, GUIStyle.none,
GUI.skin.verticalScrollbar, GUI.skin.verticalScrollbar,
GUILayout.MinWidth(this.scrollWidth), GUILayout.MinWidth(this.scrollWidth),
GUILayout.ExpandWidth(true), GUILayout.ExpandWidth(true),
GUILayout.ExpandHeight(true) GUILayout.ExpandHeight(true)
); );
   
this.ledgerTable.Render(false); this.ledgerTable.Render(false);
   
if (this.waitToResize >= 0) if (this.waitToResize >= 0)
{ {
if (this.waitToResize == 0) if (this.waitToResize == 0)
{ {
this.scrollWidth = this.ledgerTable.Width + 60f; this.scrollWidth = this.ledgerTable.Width + 60f;
this.defWidth = this.ledgerTable.Width; this.defWidth = this.ledgerTable.Width;
} }
   
this.waitToResize--; this.waitToResize--;
} }
   
GUILayout.EndScrollView(); GUILayout.EndScrollView();
   
GUILayout.EndVertical(); GUILayout.EndVertical();
} }
else else
{ {
GUILayout.Label("Select a filter option to display transactions."); GUILayout.Label("Select a filter option to display transactions.");
} }
   
base.ModuleWindow(id); base.ModuleWindow(id);
} }
   
public override void DrawConfigurables() public override void DrawConfigurables()
{ {
if (GUILayout.Button("Write Transaction Database to CSV")) if (GUILayout.Button("Write Transaction Database to CSV"))
{ {
string baseName = string.Format( string baseName = string.Format(
"{0}-{1}.csv", "{0}-{1}.csv",
HighLogic.CurrentGame.Title, HighLogic.CurrentGame.Title,
(int)Planetarium.GetUniversalTime() (int)Planetarium.GetUniversalTime()
); );
   
var file = KSP.IO.File.Create<VOID_CareerTracker>(baseName, null); var file = KSP.IO.File.Create<VOID_CareerTracker>(baseName, null);
   
UTF8Encoding enc = new UTF8Encoding(true); UTF8Encoding enc = new UTF8Encoding(true);
   
byte[] lineBytes = enc.GetPreamble(); byte[] lineBytes = enc.GetPreamble();
   
file.Write(lineBytes, 0, lineBytes.Length); file.Write(lineBytes, 0, lineBytes.Length);
   
string transLine = string.Format( string transLine = string.Format(
"{0}, {1}, \"{2}\", \"{3}\", \"{4}\"\n", "{0}, {1}, \"{2}\", \"{3}\", \"{4}\"\n",
"TimeStamp", "TimeStamp",
"Reason", "Reason",
"Funds Delta", "Funds Delta",
"Science Delta", "Science Delta",
"Reputation Delta" "Reputation Delta"
); );
   
lineBytes = enc.GetBytes(transLine); lineBytes = enc.GetBytes(transLine);
   
file.Write(lineBytes, 0, lineBytes.Length); file.Write(lineBytes, 0, lineBytes.Length);
   
foreach (CurrencyTransaction trans in Tracker.TransactionList) foreach (CurrencyTransaction trans in Tracker.TransactionList)
{ {
transLine = string.Format( transLine = string.Format(
"{0}, \"{1}\", {2}, {3}, {4}\n", "{0}, \"{1}\", {2}, {3}, {4}\n",
trans.TimeStamp.ToString("F2"), trans.TimeStamp.ToString("F2"),
Enum.GetName(typeof(TransactionReasons), trans.Reason), Enum.GetName(typeof(TransactionReasons), trans.Reason),
trans.FundsDelta.ToString("F2"), trans.FundsDelta.ToString("F2"),
trans.ScienceDelta.ToString("F2"), trans.ScienceDelta.ToString("F2"),
trans.ReputationDelta.ToString("F2") trans.ReputationDelta.ToString("F2")
); );
   
lineBytes = enc.GetBytes(transLine); lineBytes = enc.GetBytes(transLine);
   
file.Write(lineBytes, 0, lineBytes.Length); file.Write(lineBytes, 0, lineBytes.Length);
} }
} }
} }
   
private void updateTable() private void updateTable()
{ {
   
double aggregateFunds = Tracker.CurrentFunds; double aggregateFunds = Tracker.CurrentFunds;
float aggregateScience = Tracker.CurrentScience; float aggregateScience = Tracker.CurrentScience;
float aggregateReputation = Tracker.CurrentReputation; float aggregateReputation = Tracker.CurrentReputation;
   
for (int i = Tracker.TransactionList.Count - 1; i >= 0; i--) for (int i = Tracker.TransactionList.Count - 1; i >= 0; i--)
{ {
CurrencyTransaction trans = Tracker.TransactionList[i]; CurrencyTransaction trans = Tracker.TransactionList[i];
   
bool skipTrans = true; bool skipTrans = true;
   
if (this.IncludeFunds && trans.FundsDelta != 0f) if (this.IncludeFunds && trans.FundsDelta != 0f)
{ {
skipTrans = false; skipTrans = false;
} }
   
if (this.IncludeScience && trans.ScienceDelta != 0f) if (this.IncludeScience && trans.ScienceDelta != 0f)
{ {
skipTrans = false; skipTrans = false;
} }
   
if (this.IncludeReputation && trans.ReputationDelta != 0f) if (this.IncludeReputation && trans.ReputationDelta != 0f)
{ {
skipTrans = false; skipTrans = false;
} }
   
if (skipTrans) if (skipTrans)
{ {
continue; continue;
} }
   
this.timeStampCol.Add(VOID_Tools.FormatDate(trans.TimeStamp)); this.timeStampCol.Add(VOID_Tools.FormatDate(trans.TimeStamp));
   
this.reasonCol.Add(Enum.GetName(typeof(TransactionReasons), trans.Reason)); this.reasonCol.Add(Enum.GetName(typeof(TransactionReasons), trans.Reason));
   
this.fundsDeltaCol.Add(VOID_CareerStatus.formatDelta(trans.FundsDelta, "#,##0.00")); this.fundsDeltaCol.Add(VOID_CareerStatus.formatDelta(trans.FundsDelta, "#,##0.00"));
this.fundsTotalCol.Add(aggregateFunds); this.fundsTotalCol.Add(aggregateFunds);
aggregateFunds -= (double)trans.FundsDelta; aggregateFunds -= (double)trans.FundsDelta;
   
this.scienceDeltaCol.Add(VOID_CareerStatus.formatDelta(trans.ScienceDelta, "#,##0")); this.scienceDeltaCol.Add(VOID_CareerStatus.formatDelta(trans.ScienceDelta, "#,##0"));
this.scienceTotalCol.Add(aggregateScience); this.scienceTotalCol.Add(aggregateScience);
aggregateScience -= trans.ScienceDelta; aggregateScience -= trans.ScienceDelta;
   
this.repDeltaCol.Add(VOID_CareerStatus.formatDelta(trans.ReputationDelta, "#,##0")); this.repDeltaCol.Add(VOID_CareerStatus.formatDelta(trans.ReputationDelta, "#,##0"));
this.repTotalCol.Add(aggregateReputation); this.repTotalCol.Add(aggregateReputation);
aggregateReputation -= trans.ReputationDelta; aggregateReputation -= trans.ReputationDelta;
   
  this.waitToResize = 5;
} }
} }
} }
} }