Skip to content

Commit

Permalink
Sort rows by clicking headers
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Feb 7, 2017
1 parent cd0f955 commit aa6f1f0
Show file tree
Hide file tree
Showing 7 changed files with 330 additions and 128 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ Click the wrench to open and close the settings panel:

- Useful numbers for launches/landed
- Return-to-LKO burns from Mun and Minmus
- Allow low-inclination retrograde orbits
- Represent starting point with ITargetable
- Split CalculateEjectionBurn into transfer versus ejection functions

## Building

Expand Down
5 changes: 3 additions & 2 deletions src/AstrogationModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ private void CreateTransfers(CelestialBody body, Vessel vessel)
DbgFmt("Fabricating transfers");

bool foundTarget = false;
int discoveryOrder = 0;

CelestialBody origin = StartBody(body, vessel);

Expand All @@ -162,7 +163,7 @@ private void CreateTransfers(CelestialBody body, Vessel vessel)
CelestialBody satellite = b.orbitingBodies[i];
if (satellite != toSkip) {
DbgFmt("Plotting transfer to {0}", satellite.theName);
transfers.Add(new TransferModel(origin, satellite, vessel));
transfers.Add(new TransferModel(origin, satellite, vessel, ++discoveryOrder));
DbgFmt("Finalized transfer to {0}", satellite.theName);

if (satellite == FlightGlobals.fetch.VesselTarget as CelestialBody) {
Expand All @@ -180,7 +181,7 @@ private void CreateTransfers(CelestialBody body, Vessel vessel)
if (!foundTarget
&& FlightGlobals.ActiveVessel != null
&& FlightGlobals.fetch.VesselTarget != null) {
transfers.Insert(0, new TransferModel(origin, FlightGlobals.fetch.VesselTarget, vessel));
transfers.Insert(0, new TransferModel(origin, FlightGlobals.fetch.VesselTarget, vessel, -1));
}

DbgFmt("Shipping completed transfers");
Expand Down
96 changes: 79 additions & 17 deletions src/AstrogationView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ private bool ShowSettings {
}
}

private void toggleSettingsVisible()
{
ShowSettings = !ShowSettings;
resetCallback();
}

/// <summary>
/// The user-facing name for this mod.
/// Use Astrogator.Name for filenames, internal representations, CKAN, etc.
Expand All @@ -87,27 +93,37 @@ private bool ShowSettings {
/// <summary>
/// UI object representing the top row of the table
/// </summary>
private static DialogGUIHorizontalLayout ColumnHeaders { get; set; }
private DialogGUIHorizontalLayout ColumnHeaders { get; set; }

private void toggleSettingsVisible()
private string columnSortIndicator(ColumnDefinition col)
{
ShowSettings = !ShowSettings;
resetCallback();
return col.sortKey != Settings.Instance.TransferSort ? ""
: Settings.Instance.DescendingSort ? " ↓"
: " ↑";
}

private void createHeaders()
{
if (ColumnHeaders == null) {
ColumnHeaders = new DialogGUIHorizontalLayout();
for (int i = 0; i < Columns.Length; ++i) {
ColumnDefinition col = Columns[i];
// Skip columns that require an active vessel if we don't have one
if (!col.vesselSpecific || model.vessel != null) {
int width = 0;
for (int span = 0; span < col.headerColSpan; ++span) {
width += Columns[i + span].width;
}
if (width > 0) {
ColumnHeaders = new DialogGUIHorizontalLayout();
for (int i = 0; i < Columns.Length; ++i) {
ColumnDefinition col = Columns[i];
// Skip columns that require an active vessel if we don't have one
if (!col.vesselSpecific || model.vessel != null) {
float width = 0;
for (int span = 0; span < col.headerColSpan; ++span) {
width += Columns[i + span].width;
}
if (width > 0) {
// Add in the spacing gaps that got left out from colspanning
width += (col.headerColSpan - 1) * spacing;
if (col.header != "") {
ColumnHeaders.AddChild(headerButton(
col.header + columnSortIndicator(col),
col.headerStyle, "Sort", width, rowHeight, () => {
SortClicked(col.sortKey);
}
));
} else {
ColumnHeaders.AddChild(LabelWithStyleAndSize(col.header, col.headerStyle, width, rowHeight));
}
}
Expand All @@ -116,10 +132,56 @@ private void createHeaders()
AddChild(ColumnHeaders);
}

private void SortClicked(SortEnum which)
{
if (Settings.Instance.TransferSort == which) {
Settings.Instance.DescendingSort = !Settings.Instance.DescendingSort;
} else {
Settings.Instance.TransferSort = which;
Settings.Instance.DescendingSort = false;
}
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:
transfers.Sort((a, b) =>
a?.DiscoveryOrder.CompareTo(b?.DiscoveryOrder) ?? 0);
break;
case SortEnum.Time:
transfers.Sort((a, b) =>
a?.ejectionBurn?.atTime.CompareTo(b?.ejectionBurn?.atTime) ?? 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()
{
for (int i = 0; i < model.transfers.Count; ++i) {
AddChild(new TransferView(model.transfers[i]));
List<TransferModel> transfers = SortTransfers(
model,
Settings.Instance.TransferSort,
Settings.Instance.DescendingSort
);
for (int i = 0; i < transfers.Count; ++i) {
AddChild(new TransferView(transfers[i]));
}
}

Expand Down
14 changes: 9 additions & 5 deletions src/Astrogator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,10 @@ private bool SituationChanged()

private void OnSituationChanged()
{
StartLoadingModel(FlightGlobals.ActiveVessel?.mainBody, FlightGlobals.ActiveVessel);
ResetView();
if (model != null && view != null) {
StartLoadingModel(FlightGlobals.ActiveVessel?.mainBody, FlightGlobals.ActiveVessel);
ResetView();
}
}

/// <summary>
Expand All @@ -423,9 +425,11 @@ public void SOIChanged(CelestialBody newBody)
{
DbgFmt("Entered {0}'s sphere of influence", newBody.theName);

// The old list no longer applies because reachable bodies depend on current SOI
StartLoadingModel(newBody, FlightGlobals.ActiveVessel);
ResetView();
if (model != null && view != null) {
// The old list no longer applies because reachable bodies depend on current SOI
StartLoadingModel(newBody, FlightGlobals.ActiveVessel);
ResetView();
}
}

/// <summary>
Expand Down
41 changes: 41 additions & 0 deletions src/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ private const string
MainWindowVisibleKey = "MainWindowVisible",

ShowSettingsKey = "ShowSettings",
TransferSortKey = "TransferSort",
DescendingSortKey = "DescendingSort",

GeneratePlaneChangeBurnsKey = "GeneratePlaneChangeBurns",
AddPlaneChangeDeltaVKey = "AddPlaneChangeDeltaV",
Expand Down Expand Up @@ -275,6 +277,45 @@ public bool AutoEditPlaneChangeNode {
}
}

/// <summary>
/// How to sort the table.
/// </summary>
public SortEnum TransferSort {
get {
SortEnum ts = SortEnum.Position;
string savedValue = "";
if (config.TryGetValue(TransferSortKey, ref savedValue)) {
ts = ParseEnum<SortEnum>(savedValue, SortEnum.Position);
}
return ts;
}
set {
if (config.HasValue(TransferSortKey)) {
config.SetValue(TransferSortKey, value.ToString());
} else {
config.AddValue(TransferSortKey, value.ToString());
}
}
}

/// <summary>
/// True if the sort should be largest value at the top, false otherwise.
/// </summary>
public bool DescendingSort {
get {
bool descend = false;
config.TryGetValue(DescendingSortKey, ref descend);
return descend;
}
set {
if (config.HasValue(DescendingSortKey)) {
config.SetValue(DescendingSortKey, value);
} else {
config.AddValue(DescendingSortKey, value);
}
}
}

}

}
8 changes: 7 additions & 1 deletion src/TransferModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ public class TransferModel {
/// <summary>
/// Construct a model object.
/// </summary>
public TransferModel(CelestialBody org, ITargetable dest, Vessel v)
public TransferModel(CelestialBody org, ITargetable dest, Vessel v, int order)
{
origin = org;
destination = dest;
vessel = v;
DiscoveryOrder = order;
}

/// <summary>
Expand Down Expand Up @@ -52,6 +53,11 @@ public TransferModel(CelestialBody org, ITargetable dest, Vessel v)
/// </summary>
public BurnModel planeChangeBurn { get; private set; }

/// <summary>
/// Number representing the position of this row when sorted by position.
/// </summary>
public int DiscoveryOrder { get; private set; }

private BurnModel GenerateEjectionBurn(Orbit currentOrbit)
{
if (currentOrbit == null || destination == null) {
Expand Down
Loading

0 comments on commit aa6f1f0

Please sign in to comment.