Skip to content

Commit

Permalink
Fix freeze in RPM display
Browse files Browse the repository at this point in the history
- Guard for missing transfers in ShowMenu
- Handle multi-line strings in centerString
- Add line prefix in centerString
- Pass style color as subtitle line prefix
- Move AstrogationModel.subTitle to ViewTools.ModelDescription
- Use ModelDescription in AstrogatorMenu
- Delete redundant copy of SortTransfers
- Update versions for v0.7.6
- Add screenshot of RPM error message
  • Loading branch information
HebaruSan committed Jul 23, 2017
1 parent a233f20 commit 5e3be5b
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 119 deletions.
2 changes: 1 addition & 1 deletion Astrogator.version
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"VERSION": {
"MAJOR": 0,
"MINOR": 7,
"PATCH": 5,
"PATCH": 6,
"BUILD": 0
},
"KSP_VERSION_MIN": {
Expand Down
Binary file added screenshots/rpm-error.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 1 addition & 75 deletions src/AstrogationView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace Astrogator {
using static DebugTools;
using static KerbalTools;
using static ViewTools;
using static PhysicsTools;

/// <summary>
/// A DialogGUI* object that displays our app's data.
Expand Down Expand Up @@ -127,35 +126,6 @@ private void SortClicked(SortEnum which)
resetCallback();
}

private List<TransferModel> SortTransfers(AstrogationModel m, SortEnum how, bool descend)
{
List<TransferModel> transfers = new List<TransferModel>(m.transfers);
switch (how) {
case SortEnum.Name:
transfers.Sort((a, b) =>
a?.destination?.GetName().CompareTo(b?.destination?.GetName()) ?? 0);
break;
case SortEnum.Position:
// Use the natural/default ordering in the model
break;
case SortEnum.Time:
transfers.Sort((a, b) =>
a?.ejectionBurn?.atTime?.CompareTo(b?.ejectionBurn?.atTime ?? 0) ?? 0);
break;
case SortEnum.DeltaV:
transfers.Sort((a, b) =>
a?.ejectionBurn?.totalDeltaV.CompareTo(b?.ejectionBurn?.totalDeltaV) ?? 0);
break;
default:
DbgFmt("Bad sort argument: {0}", how.ToString());
break;
}
if (descend) {
transfers.Reverse();
}
return transfers;
}

private void createRows()
{
List<TransferModel> transfers = SortTransfers(
Expand All @@ -177,50 +147,6 @@ private bool ErrorCondition {
}
}

private string subTitle {
get {
if (model != null) {
if (model.origin == null) {
return "Model's origin is null";
} else if (model.hyperbolicOrbit) {
if (model.inbound) {
return Localizer.Format(
"astrogator_inboundHyperbolicWarning",
TheName(model.origin)
);
} else {
return Localizer.Format(
"astrogator_outboundHyperbolicError",
TheName(model.origin)
);
}
} else if (model.badInclination) {
return Localizer.Format(
"astrogator_highInclinationError",
(AngleFromEquatorial(model.origin.GetOrbit().inclination * Mathf.Deg2Rad) * Mathf.Rad2Deg).ToString("0.0"),
(AstrogationModel.maxInclination * Mathf.Rad2Deg).ToString("0")
);
} else if (model.transfers.Count == 0) {
return Localizer.Format("astrogator_noTransfersError");
} else if (Landed(model.origin) || solidBodyWithoutVessel(model.origin)) {
CelestialBody b = model.origin as CelestialBody;
if (b == null) {
b = model.origin.GetOrbit().referenceBody;
}
return Localizer.Format(
"astrogator_launchSubtitle",
TheName(model.origin),
FormatSpeed(DeltaVToOrbit(b), Settings.Instance.DisplayUnits)
);
} else {
return Localizer.Format("astrogator_normalSubtitle", TheName(model.origin));
}
} else {
return "Internal error: Model not found";
}
}
}

private string getMessage()
{
if (model.ActiveEjectionBurn != null
Expand Down Expand Up @@ -355,7 +281,7 @@ public PopupDialog Show()
mainWindowAnchorMax,
new MultiOptionDialog(
Localizer.Format("astrogator_mainTitle"),
subTitle,
ModelDescription(model),
Localizer.Format("astrogator_mainTitle") + " " + versionString,
skinToUse,
offsetGeometry,
Expand Down
120 changes: 79 additions & 41 deletions src/AstrogatorMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ public class AstrogatorMenu : InternalModule {
private int rowsPerPage { get; set; }
private List<TransferModel> transfers { get; set; }

private void addTitle(StringBuilder sb, int columns)
{
sb.Append(centerString(" " + Localizer.Format("astrogator_mainTitle") + " " + versionString + " ", columns, '-'));
sb.Append(Environment.NewLine);
sb.Append(centerString(
ModelDescription(model),
columns,
' ',
styleColorString(
model.ErrorCondition
? AstrogatorErrorSkin.label
: AstrogatorSkin.label
)
));
sb.Append(Environment.NewLine);
sb.Append(Environment.NewLine);
}

private void addHeaders(StringBuilder sb)
{
bool firstCol = true;
Expand Down Expand Up @@ -262,40 +280,49 @@ private void addRow(StringBuilder sb, TransferModel m, DateTimeParts dt, bool se
/// </returns>
public string ShowMenu(int columns, int rows)
{
if ((Refresh() || cursorMoved) && transfers.Count == timeToWait.Count) {

if ((RefreshTransfers() || cursorMoved)) {
StringBuilder sb = new StringBuilder();
sb.Append(centerString(" " + Localizer.Format("astrogator_mainTitle") + " " + versionString + " ", columns, '-'));
sb.Append(Environment.NewLine);
sb.Append("[#a0a0a0ff]");
sb.Append(centerString(Localizer.Format("astrogator_normalSubtitle", TheName(model.origin)), columns));
sb.Append(Environment.NewLine);
sb.Append(Environment.NewLine);

// [#rrggbbaa]
addHeaders(sb);

// Wrap the cursor around the edges now because it only tells us dimensions here.
while (cursorTransfer < 0) {
cursorTransfer += transfers.Count;
}
while (cursorTransfer >= transfers.Count) {
cursorTransfer -= transfers.Count;
}

rowsPerPage = rows - 4;
int screenPage = cursorTransfer / rowsPerPage;
for (int t = screenPage * rowsPerPage, r = 0;
t < transfers.Count && r < rowsPerPage;
++t, ++r) {
addRow(sb, transfers[t], timeToWait[t], (cursorTransfer == t));
// Top title and subtitle
addTitle(sb, columns);

if (!ErrorCondition
&& transfers != null && timeToWait != null
&& transfers.Count == timeToWait.Count) {

addHeaders(sb);

// Wrap the cursor around the edges now because it only tells us dimensions here.
while (cursorTransfer < 0) {
cursorTransfer += transfers.Count;
}
while (cursorTransfer >= transfers.Count) {
cursorTransfer -= transfers.Count;
}

rowsPerPage = rows - 4;
int screenPage = cursorTransfer / rowsPerPage;
for (int t = screenPage * rowsPerPage, r = 0;
t < transfers.Count && r < rowsPerPage;
++t, ++r) {
addRow(sb, transfers[t], timeToWait[t], (cursorTransfer == t));
}
}
menu = sb.ToString();
cursorMoved = false;
}
return menu;
}

private bool ErrorCondition {
get {
return model == null
|| model.origin == null
|| model.transfers.Count == 0
|| model.ErrorCondition;
}
}

/// <summary>
/// Turn data loading on and off.
/// Called by RasterPropMonitor based on our cfg file to tell us we're visible or invisible.
Expand All @@ -312,10 +339,19 @@ public void PageActive(bool pageActive, int pageNumber)
}
}

private string centerString(string val, int columns, char padding = ' ')
private string centerString(string val, int columns, char padding = ' ', string linePrefix = "")
{
int numPads = columns - val.Length;
return val.PadLeft(columns - numPads/2, padding).PadRight(columns, padding);
StringBuilder sb = new StringBuilder();
string[] lines = val.Split(new char[] { '\n' });
for (int i = 0; i < lines.Length; ++i) {
int numPads = columns - lines[i].Length;
if (i > 0) {
sb.Append("\n");
}
sb.Append(linePrefix);
sb.Append(lines[i].PadLeft(columns - numPads/2, padding).PadRight(columns, padding));
}
return sb.ToString();
}

/// <summary>
Expand Down Expand Up @@ -366,27 +402,29 @@ public void ButtonRelease(int buttonNumber)
activeButton = null;
}

private bool Refresh()
private bool RefreshTransfers()
{
double now = Math.Floor(Planetarium.GetUniversalTime());
if (lastUniversalTime != now) {

transfers = SortTransfers(
model,
Settings.Instance.TransferSort,
Settings.Instance.DescendingSort
);
if (!ErrorCondition) {
transfers = SortTransfers(
model,
Settings.Instance.TransferSort,
Settings.Instance.DescendingSort
);

timeToWait = new List<DateTimeParts>();
for (int i = 0; i < transfers.Count; ++i) {
timeToWait = new List<DateTimeParts>();
for (int i = 0; i < transfers.Count; ++i) {

if (transfers[i].ejectionBurn != null && transfers[i].ejectionBurn.atTime != null) {
timeToWait.Add(new DateTimeParts((transfers[i].ejectionBurn.atTime ?? 0) - Planetarium.GetUniversalTime()));
} else {
timeToWait.Add(null);
if (transfers[i].ejectionBurn != null && transfers[i].ejectionBurn.atTime != null) {
timeToWait.Add(new DateTimeParts((transfers[i].ejectionBurn.atTime ?? 0) - Planetarium.GetUniversalTime()));
} else {
timeToWait.Add(null);
}
}

}

lastUniversalTime = now;
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("0.7.5.0")]
[assembly: AssemblyFileVersion("0.7.5.0")]
[assembly: AssemblyVersion("0.7.6.0")]
[assembly: AssemblyFileVersion("0.7.6.0")]
50 changes: 50 additions & 0 deletions src/ViewTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Astrogator {
using static KerbalTools;
using static ViewTools;
using static TooltipExtensions;
using static PhysicsTools;

/// Anything UI-related that needs to be used from multiple places.
public static class ViewTools {
Expand Down Expand Up @@ -1038,6 +1039,55 @@ public static string FormatSpeed(double speed, DisplayUnitsEnum units) {
return Localizer.Format("astrogator_speedMetric", speed.ToString("0"));
}
}

/// <summary>
/// Generate a string describing the state of a model.
/// </summary>
/// <param name="model">Model to examine</param>
/// <returns>
/// Description of error or warning if applicable,
/// otherwise "Transfers from X".
/// </returns>
public static string ModelDescription(AstrogationModel model)
{
if (model == null) {
return "Internal error: Model not found";
} else if (model.origin == null) {
return "Internal error: Model's origin is null";
} else if (model.hyperbolicOrbit) {
if (model.inbound) {
return Localizer.Format(
"astrogator_inboundHyperbolicWarning",
TheName(model.origin)
);
} else {
return Localizer.Format(
"astrogator_outboundHyperbolicError",
TheName(model.origin)
);
}
} else if (model.badInclination) {
return Localizer.Format(
"astrogator_highInclinationError",
(AngleFromEquatorial(model.origin.GetOrbit().inclination * Mathf.Deg2Rad) * Mathf.Rad2Deg).ToString("0.0"),
(AstrogationModel.maxInclination * Mathf.Rad2Deg).ToString("0")
);
} else if (model.transfers.Count == 0) {
return Localizer.Format("astrogator_noTransfersError");
} else if (Landed(model.origin) || solidBodyWithoutVessel(model.origin)) {
CelestialBody b = model.origin as CelestialBody;
if (b == null) {
b = model.origin.GetOrbit().referenceBody;
}
return Localizer.Format(
"astrogator_launchSubtitle",
TheName(model.origin),
FormatSpeed(DeltaVToOrbit(b), Settings.Instance.DisplayUnits)
);
} else {
return Localizer.Format("astrogator_normalSubtitle", TheName(model.origin));
}
}
}

/// <summary>
Expand Down

0 comments on commit 5e3be5b

Please sign in to comment.