From a1d990512417b77efddaf22b715a771a306342a6 Mon Sep 17 00:00:00 2001 From: KSP-TaxiService Date: Sun, 5 Mar 2017 16:30:53 +0800 Subject: [PATCH] Remove the local data storage --- .../UI/ConstellationEditDialog.cs | 55 +++++++------- .../UI/VesselSetupDialog.cs | 76 +++++++++---------- 2 files changed, 64 insertions(+), 67 deletions(-) diff --git a/src/CommNetConstellation/UI/ConstellationEditDialog.cs b/src/CommNetConstellation/UI/ConstellationEditDialog.cs index 630eed2..2b2a164 100644 --- a/src/CommNetConstellation/UI/ConstellationEditDialog.cs +++ b/src/CommNetConstellation/UI/ConstellationEditDialog.cs @@ -4,6 +4,7 @@ using System; using UnityEngine.UI; using CommNetConstellation.CommNetLayer; +using TMPro; namespace CommNetConstellation.UI { @@ -15,8 +16,6 @@ public class ConstellationEditDialog : AbstractDialog private string description = "You are creating a new constellation."; private string actionButtonText = "Create"; - private string constellName = ""; - private short constellFreq = 0; private Color conColor = Color.white; private Constellation existingConstellation = null; @@ -25,6 +24,8 @@ public class ConstellationEditDialog : AbstractDialog private Callback creationCallback; private Callback updateCallback; + private DialogGUITextInput nameInput; + private DialogGUITextInput frequencyInput; public ConstellationEditDialog(string dialogTitle, Constellation thisConstellation, @@ -42,28 +43,34 @@ public ConstellationEditDialog(string dialogTitle, if(this.existingConstellation != null) { - this.constellName = this.existingConstellation.name; - this.constellFreq = this.existingConstellation.frequency; this.conColor = this.existingConstellation.color; - this.description = string.Format("You are editing Constellation '{0}'.", this.constellName); + this.description = string.Format("You are editing Constellation '{0}'.", this.existingConstellation.name); this.actionButtonText = "Update"; } } protected override List drawContentComponents() { + string constellName = ""; + short constellFreq = 0; + if (this.existingConstellation != null) + { + constellName = this.existingConstellation.name; + constellFreq = this.existingConstellation.frequency; + } + List listComponments = new List(); listComponments.Add(new DialogGUIHorizontalLayout(true, false, 0, new RectOffset(), TextAnchor.UpperCenter, new DialogGUIBase[] { new DialogGUILabel(this.description+"\n\n", false, false) })); DialogGUILabel nameLabel = new DialogGUILabel("Name", 52, 12); - DialogGUITextInput nameInput = new DialogGUITextInput(this.constellName, false, CNCSettings.MaxLengthName, setConstellName, 170, 25); + nameInput = new DialogGUITextInput(constellName, false, CNCSettings.MaxLengthName, setConstellName, 170, 25); DialogGUIHorizontalLayout nameGroup = new DialogGUIHorizontalLayout(true, false, 4, new RectOffset(), TextAnchor.MiddleLeft, new DialogGUIBase[] { nameLabel, nameInput }); listComponments.Add(nameGroup); DialogGUILabel freqLabel = new DialogGUILabel("Frequency", 52, 12); - DialogGUITextInput frequencyInput = new DialogGUITextInput(this.constellFreq.ToString(), false, CNCSettings.MaxDigits, setConstellFreq, 47, 25); + frequencyInput = new DialogGUITextInput(constellFreq.ToString(), false, CNCSettings.MaxDigits, setConstellFreq, 47, 25); constellationColorImage = new DialogGUIImage(new Vector2(32, 32), Vector2.zero, this.conColor, colorTexture); DialogGUIButton colorButton = new DialogGUIButton("Color", colorEditClick, null, 50, 24, false); DialogGUIHorizontalLayout freqColorGroup = new DialogGUIHorizontalLayout(true, false, 4, new RectOffset(), TextAnchor.MiddleLeft, new DialogGUIBase[] { freqLabel, frequencyInput, new DialogGUISpace(16), colorButton, constellationColorImage }); @@ -103,11 +110,6 @@ private string setConstellFreq(string newFreqStr) { throw new Exception("Frequency is in use already"); } - else - { - this.constellFreq = newFreq; - return this.constellFreq.ToString(); - } } catch (FormatException e) { @@ -132,17 +134,13 @@ private string setConstellFreq(string newFreqStr) /// private string setConstellName(string newName) { - if (newName.Trim().Length > 0) - { - this.constellName = newName; - return this.constellName; - } - else + if (newName.Trim().Length <= 0) { ScreenMessage msg = new ScreenMessage("Name cannot be empty", CNCSettings.ScreenMessageDuration, ScreenMessageStyle.UPPER_LEFT); ScreenMessages.PostScreenMessage(msg); - return newName; } + + return newName; } /// @@ -152,37 +150,40 @@ private void actionClick() { try { + short constellFreq = short.Parse(frequencyInput.uiItem.GetComponent().text); + string constellName = nameInput.uiItem.GetComponent().text; + //Check errors - if (CNCCommNetScenario.Instance.constellations.Any(x => x.frequency == this.constellFreq) && this.existingConstellation == null) + if (CNCCommNetScenario.Instance.constellations.Any(x => x.frequency == constellFreq) && this.existingConstellation == null) { throw new Exception("Frequency is in use already"); } - else if (this.constellName.Trim().Length < 1) + else if (constellName.Trim().Length < 1) { throw new Exception("Name cannot be empty"); } - else if (!Constellation.isFrequencyValid(this.constellFreq)) + else if (!Constellation.isFrequencyValid(constellFreq)) { throw new Exception("Frequency must be between 0 and " + short.MaxValue); } if (this.existingConstellation == null && creationCallback != null) { - Constellation newConstellation = new Constellation(this.constellFreq, this.constellName, this.conColor); + Constellation newConstellation = new Constellation(constellFreq, constellName, this.conColor); CNCCommNetScenario.Instance.constellations.Add(newConstellation); creationCallback(newConstellation); - string message = string.Format("New constellation '{0}' of frequency {1} is created", this.constellName, this.constellFreq); + string message = string.Format("New constellation '{0}' of frequency {1} is created", constellName, constellFreq); ScreenMessages.PostScreenMessage(new ScreenMessage(message, CNCSettings.ScreenMessageDuration, ScreenMessageStyle.UPPER_LEFT)); } else if (this.existingConstellation != null && updateCallback != null) { short prevFreq = this.existingConstellation.frequency; - this.existingConstellation.name = this.constellName; + this.existingConstellation.name = constellName; this.existingConstellation.color = this.conColor; if (this.existingConstellation.frequency != CNCSettings.Instance.PublicRadioFrequency) // this is not the public one - this.existingConstellation.frequency = this.constellFreq; + this.existingConstellation.frequency = constellFreq; List affectedVessels = CNCCommNetScenario.Instance.getCommNetVessels().FindAll(x => x.getRadioFrequency() == prevFreq); for (int i = 0; i < affectedVessels.Count; i++) @@ -190,7 +191,7 @@ private void actionClick() updateCallback(this.existingConstellation, prevFreq); - string message = string.Format("Constellation '{0}' of frequency {1} is updated", this.constellName,this.constellFreq); + string message = string.Format("Constellation '{0}' of frequency {1} is updated", constellName, constellFreq); ScreenMessages.PostScreenMessage(new ScreenMessage(message, CNCSettings.ScreenMessageDuration, ScreenMessageStyle.UPPER_LEFT)); } else diff --git a/src/CommNetConstellation/UI/VesselSetupDialog.cs b/src/CommNetConstellation/UI/VesselSetupDialog.cs index 72b3c4c..f98adc8 100644 --- a/src/CommNetConstellation/UI/VesselSetupDialog.cs +++ b/src/CommNetConstellation/UI/VesselSetupDialog.cs @@ -18,11 +18,7 @@ public class VesselSetupDialog : AbstractDialog private string description = "Something"; private Callback updateCallback; - - private short inputFreq = 0; private DialogGUITextInput frequencyInput; - - private bool membershipFlag = false; private DialogGUIToggle membershipToggle; private DialogGUIImage constellationColorImage; @@ -40,23 +36,29 @@ public VesselSetupDialog(string title, Vessel vessel, Part cmdPart, Callbackall command parts).", this.hostVessel.vesselName); + } + + protected override List drawContentComponents() + { + short inputFreq = CNCSettings.Instance.PublicRadioFrequency; + bool membershipFlag = false; + + if (this.rightClickedPart != null) // first choice + { CNConstellationModule cncModule = rightClickedPart.FindModuleImplementing(); - this.inputFreq = cncModule.radioFrequency; - this.membershipFlag = cncModule.communicationMembershipFlag; + inputFreq = cncModule.radioFrequency; + membershipFlag = cncModule.communicationMembershipFlag; } else if (this.hostVessel != null) { - this.description = string.Format("You are editing the whole vessel '{0}' (overriding all command parts).", this.hostVessel.vesselName); CNCCommNetVessel cv = hostVessel.Connection as CNCCommNetVessel; - this.inputFreq = cv.getRadioFrequency(); - this.membershipFlag = cv.getMembershipFlag(); + inputFreq = cv.getRadioFrequency(); + membershipFlag = cv.getMembershipFlag(); } - } - protected override List drawContentComponents() - { List listComponments = new List(); listComponments.Add(new DialogGUIHorizontalLayout(true, false, 0, new RectOffset(), TextAnchor.UpperCenter, new DialogGUIBase[] { new DialogGUILabel(this.description+"\n\n", false, false) })); @@ -71,7 +73,7 @@ protected override List drawContentComponents() DialogGUIHorizontalLayout constellationGroup = new DialogGUIHorizontalLayout(true, false, 4, new RectOffset(), TextAnchor.MiddleCenter, new DialogGUIBase[] { constNameLabel, constellationColorImage }); listComponments.Add(constellationGroup); - membershipToggle = new DialogGUIToggle(this.membershipFlag, "Talk to constellation members only", membershipFlagToggle); + membershipToggle = new DialogGUIToggle(membershipFlag, "Talk to constellation members only", membershipFlagToggle); listComponments.Add(membershipToggle); DialogGUIButton updateButton = new DialogGUIButton("Update", updateClick, false); @@ -94,14 +96,7 @@ private string setConstellFreq(string newFreqStr) short newFreq = short.Parse(newFreqStr); if (newFreq < 0) - { throw new Exception("Frequency cannot be negative"); - } - else - { - this.inputFreq = newFreq; - return this.inputFreq.ToString(); - } } catch (FormatException e) { @@ -126,13 +121,16 @@ private string setConstellFreq(string newFreqStr) /// private string getConstellationName() { - Constellation thisConstellation = CNCCommNetScenario.Instance.constellations.Find(x => x.frequency == inputFreq); - - if (thisConstellation != null) + try { - constellationColorImage.uiItem.GetComponent().color = thisConstellation.color; - return "Constellation: " + thisConstellation.name; - } + Constellation thisConstellation = CNCCommNetScenario.Instance.constellations.Find(x => x.frequency == short.Parse(frequencyInput.uiItem.GetComponent().text)); + + if (thisConstellation != null) + { + constellationColorImage.uiItem.GetComponent().color = thisConstellation.color; + return "Constellation: " + thisConstellation.name; + } + } catch (Exception e) { } constellationColorImage.uiItem.GetComponent().color = Color.clear; return "Constellation: Unrecognised"; @@ -145,12 +143,14 @@ private void updateClick() { try { + short inputFreq = short.Parse(frequencyInput.uiItem.GetComponent().text); + //Check errors - if (!CNCCommNetScenario.Instance.constellations.Any(x => x.frequency == this.inputFreq)) + if (!CNCCommNetScenario.Instance.constellations.Any(x => x.frequency == inputFreq)) { throw new Exception("Please choose an existing constellation"); } - else if (!Constellation.isFrequencyValid(this.inputFreq)) + else if (!Constellation.isFrequencyValid(inputFreq)) { throw new Exception("Frequency must be between 0 and "+short.MaxValue); } @@ -160,24 +160,24 @@ private void updateClick() if (this.hostVessel != null) // flight { CNCCommNetVessel cv = hostVessel.Connection as CNCCommNetVessel; - cv.updateRadioFrequency(this.inputFreq, rightClickedPart); + cv.updateRadioFrequency(inputFreq, rightClickedPart); } else // editor { CNConstellationModule cncModule = rightClickedPart.FindModuleImplementing(); - cncModule.radioFrequency = this.inputFreq; + cncModule.radioFrequency = inputFreq; } - string message = string.Format("Frequency of {0} is updated to {1}", rightClickedPart.partInfo.title, this.inputFreq); + string message = string.Format("Frequency of {0} is updated to {1}", rightClickedPart.partInfo.title, inputFreq); ScreenMessages.PostScreenMessage(new ScreenMessage(message, CNCSettings.ScreenMessageDuration, ScreenMessageStyle.UPPER_LEFT)); } else if (this.hostVessel != null) // tracking station { CNCCommNetVessel cv = hostVessel.Connection as CNCCommNetVessel; short prevFrequency = cv.getRadioFrequency(); - cv.updateRadioFrequency(this.inputFreq); + cv.updateRadioFrequency(inputFreq); - string message = string.Format("Individual frequencies of {0} are updated to {1}", this.hostVessel.GetName() ,this.inputFreq); + string message = string.Format("Individual frequencies of {0} are updated to {1}", this.hostVessel.GetName(), inputFreq); ScreenMessages.PostScreenMessage(new ScreenMessage(message, CNCSettings.ScreenMessageDuration, ScreenMessageStyle.UPPER_LEFT)); updateCallback(this.hostVessel, prevFrequency); @@ -199,12 +199,8 @@ private void updateClick() /// private void defaultClick() { - this.inputFreq = CNCSettings.Instance.PublicRadioFrequency; - updateClick(); - - // Issue: SetOptionText or text belongs to DialogGUIBase. Nothing in DialogGUITextInput to update its input field. - TMP_InputField component = frequencyInput.uiItem.GetComponent(); // TODO: eliminate the local storage - component.text = this.inputFreq.ToString(); + frequencyInput.uiItem.GetComponent().text = CNCSettings.Instance.PublicRadioFrequency.ToString(); + updateClick(); } ///