Skip to content

Commit

Permalink
Reorganise the management tools of the vessel
Browse files Browse the repository at this point in the history
  • Loading branch information
KSP-TaxiService committed Jun 29, 2017
1 parent 3e93a94 commit 4127e2f
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 111 deletions.
2 changes: 1 addition & 1 deletion src/CommNetConstellation/CommNetConstellation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@
<Compile Include="UI\ConstellationEditDialog.cs" />
<Compile Include="UI\GroundStationEditDialog.cs" />
<Compile Include="UI\UIUtils.cs" />
<Compile Include="UI\VesselMgtTools\AbstractMgtTool.cs" />
<Compile Include="UI\VesselMgtTools\AntennaTool.cs" />
<Compile Include="UI\VesselMgtTools\ToolContentManagement.cs" />
<Compile Include="UI\VesselMgtTools\UpdateListTool.cs" />
<Compile Include="UI\VesselMgtTools\VanillaFreqTool.cs" />
<Compile Include="UI\VesselSetupDialog.cs" />
Expand Down
47 changes: 0 additions & 47 deletions src/CommNetConstellation/UI/VesselMgtTools/AbstractMgtTool.cs

This file was deleted.

25 changes: 12 additions & 13 deletions src/CommNetConstellation/UI/VesselMgtTools/AntennaTool.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using CommNet;
using UnityEngine;
using CommNetConstellation.CommNetLayer;
Expand All @@ -9,9 +8,15 @@ namespace CommNetConstellation.UI.VesselMgtTools
public class AntennaTool : AbstractMgtTool
{
private DialogGUIVerticalLayout toggleAntennaColumn;
private UIStyle style;

public AntennaTool(CommNetVessel thisVessel, Callback updateFreqRowsCallback) : base(thisVessel, "antenna", "Antennas", updateFreqRowsCallback)
public AntennaTool(CommNetVessel thisVessel, Callback updateFreqRowsCallback) : base(thisVessel, "antenna", "Antennas", new List<Callback>() { updateFreqRowsCallback })
{
this.style = new UIStyle();
this.style.alignment = TextAnchor.MiddleLeft;
this.style.fontStyle = FontStyle.Normal;
this.style.normal = new UIStyleState();
this.style.normal.textColor = Color.white;
}

public override List<DialogGUIBase> getContentComponents()
Expand All @@ -31,7 +36,7 @@ public override List<DialogGUIBase> getContentComponents()
{
CNCAntennaPartInfo antennaInfo = antennas[i];

DialogGUIToggle toggleBtn = new DialogGUIToggle(antennaInfo.inUse, "", delegate (bool b) { vesselAntennaSelected(b, antennaInfo.GUID); updateCallback(); }, 20, 32);
DialogGUIToggle toggleBtn = new DialogGUIToggle(antennaInfo.inUse, "", delegate (bool b) { vesselAntennaSelected(b, antennaInfo.GUID); actionCallbacks[0](); }, 20, 32);
DialogGUILabel nameLabel = new DialogGUILabel(antennaInfo.name, style); nameLabel.size = new Vector2(160, 32);
DialogGUILabel comPowerLabel = new DialogGUILabel(string.Format("Com power: {0:0.00}", UIUtils.RoundToNearestMetricFactor(antennaInfo.antennaPower)), style); comPowerLabel.size = new Vector2(120, 32);
DialogGUILabel frequencyLabel = new DialogGUILabel(string.Format("(<color={0}>{1}</color>)", UIUtils.colorToHex(Constellation.getColor(antennaInfo.frequency)), antennaInfo.frequency), style); frequencyLabel.size = new Vector2(60, 32);
Expand All @@ -46,19 +51,13 @@ public override List<DialogGUIBase> getContentComponents()

layout.Add(new DialogGUIHorizontalLayout(true, false, 0, new RectOffset(), TextAnchor.MiddleLeft, new DialogGUIBase[] { toggleAntennaColumn, nameColumn, frequencyColumn, comPowerColumn, combinableColumn }));

DialogGUIButton deselectButton = new DialogGUIButton("Deselect all", delegate { toggleAllAntennas(false); updateCallback(); }, false);
DialogGUIButton selectButton = new DialogGUIButton("Select all", delegate { toggleAllAntennas(true); updateCallback(); }, false);
//DialogGUIButton buildButton = new DialogGUIButton("Build List", delegate { cncVessel.rebuildFreqList(); refreshFrequencyRows(); }, false);
DialogGUIButton deselectButton = new DialogGUIButton("Deselect all", delegate { toggleAllAntennas(false); actionCallbacks[0](); }, false);
DialogGUIButton selectButton = new DialogGUIButton("Select all", delegate { toggleAllAntennas(true); actionCallbacks[0](); }, false);
layout.Add(new DialogGUIHorizontalLayout(true, false, 0, new RectOffset(), TextAnchor.MiddleLeft, new DialogGUIBase[] { selectButton, deselectButton }));

return layout;
}

public override void run()
{
throw new NotImplementedException();
}

private void vesselAntennaSelected(bool useState, uint antennaGUID)
{
cncVessel.toggleAntenna(antennaGUID, useState);
Expand All @@ -72,7 +71,7 @@ private void toggleAllAntennas(bool state)
for (int i = 0; i < allAntennas.Count; i++)
vesselAntennaSelected(state, allAntennas[i].GUID);

//displayContent(Tool.SELECT_ANTENNAS);
this.selfRefresh(); //TODO: force retrieve antenna data
}
}
}
111 changes: 111 additions & 0 deletions src/CommNetConstellation/UI/VesselMgtTools/ToolContentManagement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using CommNet;
using CommNetConstellation.CommNetLayer;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace CommNetConstellation.UI.VesselMgtTools
{
public abstract class AbstractMgtTool
{
protected List<short> initialFrequencies;
protected CNCCommNetVessel cncVessel;
protected List<CNCAntennaPartInfo> antennas;
protected List<Callback> actionCallbacks;

public ToolContentManagement toolManagement { get; set; }

public string codename;
public string toolName;

public AbstractMgtTool(CommNetVessel thisVessel, string uniqueCodename, string toolName, List<Callback> actionCallbacks = null)
{
if (!(thisVessel is CNCCommNetVessel))
{
CNCLog.Error("Vessel '{0}''s connection is not of type CNCCommNetVessel!", thisVessel.Vessel.vesselName);
return;
}

this.cncVessel = (CNCCommNetVessel)thisVessel;
this.initialFrequencies = this.cncVessel.getFrequencies();
this.antennas = this.cncVessel.getAllAntennaInfo();

this.codename = uniqueCodename + "_mgttool";
this.toolName = toolName;
this.actionCallbacks = actionCallbacks;
}

protected void selfRefresh()
{
this.toolManagement.selectTool(this.codename);
}

public abstract List<DialogGUIBase> getContentComponents();
public virtual void precompute() { }
public virtual void cleanup() { }
}

public class ToolContentManagement
{
protected DialogGUIVerticalLayout toolContentLayout;
protected List<AbstractMgtTool> tools;
private AbstractMgtTool currentTool;

public ToolContentManagement()
{
this.tools = new List<AbstractMgtTool>();
this.currentTool = null;
}

public void add(AbstractMgtTool newTool)
{
this.tools.Add(newTool);
newTool.toolManagement = this;
}

public void clear()
{
this.tools.Clear();
}

public List<DialogGUIBase> getLayoutContents()
{
List<DialogGUIBase> layout = new List<DialogGUIBase>();

layout.Add(new DialogGUILabel("<b>Management tools</b>", false, false));
DialogGUIBase[] buttons = new DialogGUIBase[this.tools.Count];
for (int i = 0; i < this.tools.Count; i++)
{
AbstractMgtTool thisTool = this.tools[i];
buttons[i] = new DialogGUIButton(thisTool.toolName, delegate { selectTool(thisTool.codename); }, 50, 32, false);
}
layout.Add(new DialogGUIHorizontalLayout(true, false, 0, new RectOffset(), TextAnchor.MiddleLeft, buttons));

//Tool content
toolContentLayout = new DialogGUIVerticalLayout(10, 100, 4, new RectOffset(5, 25, 5, 5), TextAnchor.UpperLeft, new DialogGUIBase[] { new DialogGUIContentSizer(ContentSizeFitter.FitMode.Unconstrained, ContentSizeFitter.FitMode.PreferredSize, true) });
layout.Add(new DialogGUIScrollList(Vector2.one, false, true, toolContentLayout));

return layout;
}

public void selectTool(string toolCodename)
{
if(this.currentTool != null)
this.currentTool.cleanup();

AbstractDialog.deregisterLayoutComponents(toolContentLayout);

if ((this.currentTool = this.tools.Find(x => x.codename.Equals(toolCodename))) == null)
{
toolContentLayout.AddChildren(new DialogGUIBase[] { }); // empty
}
else
{
toolContentLayout.AddChildren(this.currentTool.getContentComponents().ToArray());
this.currentTool.precompute();
}

AbstractDialog.registerLayoutComponents(toolContentLayout);
}
}
}
15 changes: 8 additions & 7 deletions src/CommNetConstellation/UI/VesselMgtTools/UpdateListTool.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using CommNet;
using UnityEngine;
using static CommNetConstellation.CommNetLayer.CNCCommNetVessel;
Expand All @@ -8,8 +7,15 @@ namespace CommNetConstellation.UI.VesselMgtTools
{
public class UpdateListTool : AbstractMgtTool
{
private UIStyle style;

public UpdateListTool(CommNetVessel thisVessel) : base(thisVessel, "updatelist", "Update List")
{
this.style = new UIStyle();
this.style.alignment = TextAnchor.MiddleLeft;
this.style.fontStyle = FontStyle.Normal;
this.style.normal = new UIStyleState();
this.style.normal.textColor = Color.white;
}

public override List<DialogGUIBase> getContentComponents()
Expand Down Expand Up @@ -49,11 +55,6 @@ public override List<DialogGUIBase> getContentComponents()
return layout;
}

public override void run()
{
throw new NotImplementedException();
}

private void ListOperationSelected(bool b, FrequencyListOperation operation)
{
if (b)
Expand Down
10 changes: 2 additions & 8 deletions src/CommNetConstellation/UI/VesselMgtTools/VanillaFreqTool.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using CommNet;
using UnityEngine;

namespace CommNetConstellation.UI.VesselMgtTools
{
public class VanillaFreqTool : AbstractMgtTool
{
public VanillaFreqTool(CommNetVessel thisVessel) : base(thisVessel, "vanilla", "Vanilla")
public VanillaFreqTool(CommNetVessel thisVessel, Callback updateFreqRowsCallback) : base(thisVessel, "vanilla", "Vanilla", new List<Callback>() { updateFreqRowsCallback })
{
}

Expand All @@ -20,10 +19,5 @@ public override List<DialogGUIBase> getContentComponents()

return layout;
}

public override void run()
{
throw new NotImplementedException();
}
}
}
44 changes: 9 additions & 35 deletions src/CommNetConstellation/UI/VesselSetupDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Linq;
using TMPro;
using static CommNetConstellation.CommNetLayer.CNCCommNetVessel;
using CommNetConstellation.UI.VesselMgtTools;

namespace CommNetConstellation.UI
Expand All @@ -19,11 +15,8 @@ public class VesselSetupDialog : AbstractDialog
private string description = "Something";

private Callback<Vessel> updateCallback;

private DialogGUIVerticalLayout frequencyRowLayout;
private DialogGUIVerticalLayout toolContentLayout;
private List<AbstractMgtTool> tools;

private ToolContentManagement toolMgt;
private static readonly Texture2D colorTexture = UIUtils.loadImage("colorDisplay");

public VesselSetupDialog(string title, Vessel vessel, Callback<Vessel> updateCallback) : base("vesselEdit",
Expand All @@ -37,14 +30,14 @@ public VesselSetupDialog(string title, Vessel vessel, Callback<Vessel> updateCa
this.hostVessel = vessel;
this.updateCallback = updateCallback;
this.description = string.Format("The frequency list of this vessel '{0}' is used to communicate with other vessels.", this.hostVessel.vesselName);
this.tools = new List<AbstractMgtTool>();

this.toolMgt = new ToolContentManagement();
UpdateListTool updateTool = new UpdateListTool(this.hostVessel.connection);
this.tools.Add(updateTool);
this.toolMgt.add(updateTool);
AntennaTool antennaTool = new AntennaTool(this.hostVessel.connection, refreshFrequencyRows);
this.tools.Add(antennaTool);
VanillaFreqTool vanillaTool = new VanillaFreqTool(this.hostVessel.connection);
this.tools.Add(vanillaTool);
this.toolMgt.add(antennaTool);
VanillaFreqTool vanillaTool = new VanillaFreqTool(this.hostVessel.connection, refreshFrequencyRows);
this.toolMgt.add(vanillaTool);

this.GetInputLocks();
}
Expand All @@ -64,10 +57,10 @@ protected override List<DialogGUIBase> drawContentComponents()
CNCCommNetVessel cncVessel = (CNCCommNetVessel)this.hostVessel.Connection;
List<short> vesselFrequencyList = cncVessel.getFrequencies();

listComponments.Add(new DialogGUIHorizontalLayout(true, false, 0, new RectOffset(), TextAnchor.UpperCenter, new DialogGUIBase[] { new DialogGUILabel(this.description + "\n", false, false) }));
listComponments.Add(new DialogGUIHorizontalLayout(true, false, 0, new RectOffset(), TextAnchor.UpperCenter, new DialogGUIBase[] { new DialogGUILabel(this.description + "\n\n", false, false) }));

//frequency list
listComponments.Add(new DialogGUILabel("\n<b>Active frequencies</b>", false, false));
listComponments.Add(new DialogGUILabel("<b>Active frequencies</b>", false, false));
DialogGUIBase[] frequencyRows = new DialogGUIBase[vesselFrequencyList.Count + 1];
frequencyRows[0] = new DialogGUIContentSizer(ContentSizeFitter.FitMode.Unconstrained, ContentSizeFitter.FitMode.PreferredSize, true);
for (int i = 0; i < vesselFrequencyList.Count; i++)
Expand All @@ -79,19 +72,7 @@ protected override List<DialogGUIBase> drawContentComponents()
listComponments.Add(new DialogGUIScrollList(Vector2.one, false, true, frequencyRowLayout));

//tools
listComponments.Add(new DialogGUILabel("\n<b>Management tools</b>", false, false));
DialogGUIBase[] buttons = new DialogGUIBase[this.tools.Count+1];
for (int i=0; i<this.tools.Count; i++)
{
AbstractMgtTool thisTool = this.tools[i];
buttons[i] = new DialogGUIButton(thisTool.toolName, delegate { displayContent(thisTool.getContentComponents()); }, 50, 32, false);
}
buttons[this.tools.Count] = new DialogGUILabel("More coming tools soon!");
listComponments.Add(new DialogGUIHorizontalLayout(true, false, 0, new RectOffset(), TextAnchor.MiddleLeft, buttons));

//Tool content
toolContentLayout = new DialogGUIVerticalLayout(10, 100, 4, new RectOffset(5, 25, 5, 5), TextAnchor.UpperLeft, new DialogGUIBase[]{ new DialogGUIContentSizer(ContentSizeFitter.FitMode.Unconstrained, ContentSizeFitter.FitMode.PreferredSize, true) });
listComponments.Add(new DialogGUIScrollList(Vector2.one, false, true, toolContentLayout));
listComponments.AddRange(this.toolMgt.getLayoutContents());

return listComponments;
}
Expand Down Expand Up @@ -122,12 +103,5 @@ private void refreshFrequencyRows()

registerLayoutComponents(frequencyRowLayout);
}

private void displayContent(List<DialogGUIBase> contents)
{
deregisterLayoutComponents(toolContentLayout);
toolContentLayout.AddChildren(contents.ToArray());
registerLayoutComponents(toolContentLayout);
}
}
}

0 comments on commit 4127e2f

Please sign in to comment.