From 4127e2f2a5b1b42b702b1fbd50d486b1ddb68f64 Mon Sep 17 00:00:00 2001 From: KSP-TaxiService Date: Thu, 29 Jun 2017 22:04:05 +0800 Subject: [PATCH] Reorganise the management tools of the vessel --- .../CommNetConstellation.csproj | 2 +- .../UI/VesselMgtTools/AbstractMgtTool.cs | 47 -------- .../UI/VesselMgtTools/AntennaTool.cs | 25 ++-- .../VesselMgtTools/ToolContentManagement.cs | 111 ++++++++++++++++++ .../UI/VesselMgtTools/UpdateListTool.cs | 15 +-- .../UI/VesselMgtTools/VanillaFreqTool.cs | 10 +- .../UI/VesselSetupDialog.cs | 44 ++----- 7 files changed, 143 insertions(+), 111 deletions(-) delete mode 100644 src/CommNetConstellation/UI/VesselMgtTools/AbstractMgtTool.cs create mode 100644 src/CommNetConstellation/UI/VesselMgtTools/ToolContentManagement.cs diff --git a/src/CommNetConstellation/CommNetConstellation.csproj b/src/CommNetConstellation/CommNetConstellation.csproj index 20c4fc6..d080023 100644 --- a/src/CommNetConstellation/CommNetConstellation.csproj +++ b/src/CommNetConstellation/CommNetConstellation.csproj @@ -73,8 +73,8 @@ - + diff --git a/src/CommNetConstellation/UI/VesselMgtTools/AbstractMgtTool.cs b/src/CommNetConstellation/UI/VesselMgtTools/AbstractMgtTool.cs deleted file mode 100644 index 4eb1c7a..0000000 --- a/src/CommNetConstellation/UI/VesselMgtTools/AbstractMgtTool.cs +++ /dev/null @@ -1,47 +0,0 @@ -using CommNet; -using CommNetConstellation.CommNetLayer; -using System.Collections.Generic; -using UnityEngine; - -namespace CommNetConstellation.UI.VesselMgtTools -{ - public abstract class AbstractMgtTool - { - protected List initialFrequencies; - protected CNCCommNetVessel cncVessel; - protected List antennas; - protected Callback updateCallback; - - protected string codename; - public string toolName; - protected UIStyle style; - - public AbstractMgtTool(CommNetVessel thisVessel, string uniqueCodename, string toolName, Callback updateCallback = null) - { - if (!(thisVessel is CNCCommNetVessel)) - CNCLog.Error("Vessel '{0}''s connection is not of type CNCCommNetVessel!", thisVessel.Vessel.vesselName); - - this.cncVessel = (CNCCommNetVessel)thisVessel; - this.initialFrequencies = this.cncVessel.getFrequencies(); - this.antennas = this.cncVessel.getAllAntennaInfo(); - - this.codename = uniqueCodename+"_mgttool"; - this.toolName = toolName; - this.updateCallback = updateCallback; - - style = new UIStyle(); - style.alignment = TextAnchor.MiddleLeft; - style.fontStyle = FontStyle.Normal; - style.normal = new UIStyleState(); - style.normal.textColor = Color.white; - } - - public abstract List getContentComponents(); - public abstract void run();//any use case? - - public virtual void prerun()//any use case? - { - //do nothing - } - } -} diff --git a/src/CommNetConstellation/UI/VesselMgtTools/AntennaTool.cs b/src/CommNetConstellation/UI/VesselMgtTools/AntennaTool.cs index 29666df..e2cd98a 100644 --- a/src/CommNetConstellation/UI/VesselMgtTools/AntennaTool.cs +++ b/src/CommNetConstellation/UI/VesselMgtTools/AntennaTool.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using CommNet; using UnityEngine; using CommNetConstellation.CommNetLayer; @@ -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() { 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 getContentComponents() @@ -31,7 +36,7 @@ public override List 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("({1})", UIUtils.colorToHex(Constellation.getColor(antennaInfo.frequency)), antennaInfo.frequency), style); frequencyLabel.size = new Vector2(60, 32); @@ -46,19 +51,13 @@ public override List 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); @@ -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 } } } diff --git a/src/CommNetConstellation/UI/VesselMgtTools/ToolContentManagement.cs b/src/CommNetConstellation/UI/VesselMgtTools/ToolContentManagement.cs new file mode 100644 index 0000000..b9d5394 --- /dev/null +++ b/src/CommNetConstellation/UI/VesselMgtTools/ToolContentManagement.cs @@ -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 initialFrequencies; + protected CNCCommNetVessel cncVessel; + protected List antennas; + protected List actionCallbacks; + + public ToolContentManagement toolManagement { get; set; } + + public string codename; + public string toolName; + + public AbstractMgtTool(CommNetVessel thisVessel, string uniqueCodename, string toolName, List 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 getContentComponents(); + public virtual void precompute() { } + public virtual void cleanup() { } + } + + public class ToolContentManagement + { + protected DialogGUIVerticalLayout toolContentLayout; + protected List tools; + private AbstractMgtTool currentTool; + + public ToolContentManagement() + { + this.tools = new List(); + this.currentTool = null; + } + + public void add(AbstractMgtTool newTool) + { + this.tools.Add(newTool); + newTool.toolManagement = this; + } + + public void clear() + { + this.tools.Clear(); + } + + public List getLayoutContents() + { + List layout = new List(); + + layout.Add(new DialogGUILabel("Management tools", 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); + } + } +} diff --git a/src/CommNetConstellation/UI/VesselMgtTools/UpdateListTool.cs b/src/CommNetConstellation/UI/VesselMgtTools/UpdateListTool.cs index 83f8ad6..72a95d4 100644 --- a/src/CommNetConstellation/UI/VesselMgtTools/UpdateListTool.cs +++ b/src/CommNetConstellation/UI/VesselMgtTools/UpdateListTool.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using CommNet; using UnityEngine; using static CommNetConstellation.CommNetLayer.CNCCommNetVessel; @@ -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 getContentComponents() @@ -49,11 +55,6 @@ public override List getContentComponents() return layout; } - public override void run() - { - throw new NotImplementedException(); - } - private void ListOperationSelected(bool b, FrequencyListOperation operation) { if (b) diff --git a/src/CommNetConstellation/UI/VesselMgtTools/VanillaFreqTool.cs b/src/CommNetConstellation/UI/VesselMgtTools/VanillaFreqTool.cs index 4e2b088..df1f94f 100644 --- a/src/CommNetConstellation/UI/VesselMgtTools/VanillaFreqTool.cs +++ b/src/CommNetConstellation/UI/VesselMgtTools/VanillaFreqTool.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using CommNet; using UnityEngine; @@ -7,7 +6,7 @@ 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() { updateFreqRowsCallback }) { } @@ -20,10 +19,5 @@ public override List getContentComponents() return layout; } - - public override void run() - { - throw new NotImplementedException(); - } } } diff --git a/src/CommNetConstellation/UI/VesselSetupDialog.cs b/src/CommNetConstellation/UI/VesselSetupDialog.cs index c644446..7594491 100644 --- a/src/CommNetConstellation/UI/VesselSetupDialog.cs +++ b/src/CommNetConstellation/UI/VesselSetupDialog.cs @@ -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 @@ -19,11 +15,8 @@ public class VesselSetupDialog : AbstractDialog private string description = "Something"; private Callback updateCallback; - private DialogGUIVerticalLayout frequencyRowLayout; - private DialogGUIVerticalLayout toolContentLayout; - private List tools; - + private ToolContentManagement toolMgt; private static readonly Texture2D colorTexture = UIUtils.loadImage("colorDisplay"); public VesselSetupDialog(string title, Vessel vessel, Callback updateCallback) : base("vesselEdit", @@ -37,14 +30,14 @@ public VesselSetupDialog(string title, Vessel vessel, Callback 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(); + 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(); } @@ -64,10 +57,10 @@ protected override List drawContentComponents() CNCCommNetVessel cncVessel = (CNCCommNetVessel)this.hostVessel.Connection; List 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("\nActive frequencies", false, false)); + listComponments.Add(new DialogGUILabel("Active frequencies", 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++) @@ -79,19 +72,7 @@ protected override List drawContentComponents() listComponments.Add(new DialogGUIScrollList(Vector2.one, false, true, frequencyRowLayout)); //tools - listComponments.Add(new DialogGUILabel("\nManagement tools", false, false)); - DialogGUIBase[] buttons = new DialogGUIBase[this.tools.Count+1]; - for (int i=0; i contents) - { - deregisterLayoutComponents(toolContentLayout); - toolContentLayout.AddChildren(contents.ToArray()); - registerLayoutComponents(toolContentLayout); - } } }