Skip to content

Commit

Permalink
Remove the local data storage
Browse files Browse the repository at this point in the history
  • Loading branch information
KSP-TaxiService committed Mar 5, 2017
1 parent 54a8fb2 commit a1d9905
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 67 deletions.
55 changes: 28 additions & 27 deletions src/CommNetConstellation/UI/ConstellationEditDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using UnityEngine.UI;
using CommNetConstellation.CommNetLayer;
using TMPro;

namespace CommNetConstellation.UI
{
Expand All @@ -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;

Expand All @@ -25,6 +24,8 @@ public class ConstellationEditDialog : AbstractDialog

private Callback<Constellation> creationCallback;
private Callback<Constellation, short> updateCallback;
private DialogGUITextInput nameInput;
private DialogGUITextInput frequencyInput;

public ConstellationEditDialog(string dialogTitle,
Constellation thisConstellation,
Expand All @@ -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<DialogGUIBase> drawContentComponents()
{
string constellName = "";
short constellFreq = 0;
if (this.existingConstellation != null)
{
constellName = this.existingConstellation.name;
constellFreq = this.existingConstellation.frequency;
}

List<DialogGUIBase> listComponments = new List<DialogGUIBase>();

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("<b>Name</b>", 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("<b>Frequency</b>", 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 });
Expand Down Expand Up @@ -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)
{
Expand All @@ -132,17 +134,13 @@ private string setConstellFreq(string newFreqStr)
/// </summary>
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("<color=red>Name cannot be empty</color>", CNCSettings.ScreenMessageDuration, ScreenMessageStyle.UPPER_LEFT);
ScreenMessages.PostScreenMessage(msg);
return newName;
}

return newName;
}

/// <summary>
Expand All @@ -152,45 +150,48 @@ private void actionClick()
{
try
{
short constellFreq = short.Parse(frequencyInput.uiItem.GetComponent<TMP_InputField>().text);
string constellName = nameInput.uiItem.GetComponent<TMP_InputField>().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<CNCCommNetVessel> affectedVessels = CNCCommNetScenario.Instance.getCommNetVessels().FindAll(x => x.getRadioFrequency() == prevFreq);
for (int i = 0; i < affectedVessels.Count; i++)
affectedVessels[i].updateRadioFrequency(this.existingConstellation.frequency);

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
Expand Down
76 changes: 36 additions & 40 deletions src/CommNetConstellation/UI/VesselSetupDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ public class VesselSetupDialog : AbstractDialog
private string description = "Something";

private Callback<Vessel, short> updateCallback;

private short inputFreq = 0;
private DialogGUITextInput frequencyInput;

private bool membershipFlag = false;
private DialogGUIToggle membershipToggle;

private DialogGUIImage constellationColorImage;
Expand All @@ -40,23 +36,29 @@ public VesselSetupDialog(string title, Vessel vessel, Part cmdPart, Callback<Ves
this.updateCallback = updateCallback;

if (this.rightClickedPart != null) // first choice
{
this.description = string.Format("You are editing this command part '{0}'.", this.rightClickedPart.partInfo.title);
else if (this.hostVessel != null)
this.description = string.Format("You are editing the whole vessel '{0}' (overriding <b>all</b> command parts).", this.hostVessel.vesselName);
}

protected override List<DialogGUIBase> drawContentComponents()
{
short inputFreq = CNCSettings.Instance.PublicRadioFrequency;
bool membershipFlag = false;

if (this.rightClickedPart != null) // first choice
{
CNConstellationModule cncModule = rightClickedPart.FindModuleImplementing<CNConstellationModule>();
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 <b>all</b> 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<DialogGUIBase> drawContentComponents()
{
List<DialogGUIBase> listComponments = new List<DialogGUIBase>();

listComponments.Add(new DialogGUIHorizontalLayout(true, false, 0, new RectOffset(), TextAnchor.UpperCenter, new DialogGUIBase[] { new DialogGUILabel(this.description+"\n\n", false, false) }));
Expand All @@ -71,7 +73,7 @@ protected override List<DialogGUIBase> drawContentComponents()
DialogGUIHorizontalLayout constellationGroup = new DialogGUIHorizontalLayout(true, false, 4, new RectOffset(), TextAnchor.MiddleCenter, new DialogGUIBase[] { constNameLabel, constellationColorImage });
listComponments.Add(constellationGroup);

membershipToggle = new DialogGUIToggle(this.membershipFlag, "<b>Talk to constellation members only</b>", membershipFlagToggle);
membershipToggle = new DialogGUIToggle(membershipFlag, "<b>Talk to constellation members only</b>", membershipFlagToggle);
listComponments.Add(membershipToggle);

DialogGUIButton updateButton = new DialogGUIButton("Update", updateClick, false);
Expand All @@ -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)
{
Expand All @@ -126,13 +121,16 @@ private string setConstellFreq(string newFreqStr)
/// </summary>
private string getConstellationName()
{
Constellation thisConstellation = CNCCommNetScenario.Instance.constellations.Find(x => x.frequency == inputFreq);

if (thisConstellation != null)
try
{
constellationColorImage.uiItem.GetComponent<RawImage>().color = thisConstellation.color;
return "<b>Constellation:</b> " + thisConstellation.name;
}
Constellation thisConstellation = CNCCommNetScenario.Instance.constellations.Find(x => x.frequency == short.Parse(frequencyInput.uiItem.GetComponent<TMP_InputField>().text));

if (thisConstellation != null)
{
constellationColorImage.uiItem.GetComponent<RawImage>().color = thisConstellation.color;
return "<b>Constellation:</b> " + thisConstellation.name;
}
} catch (Exception e) { }

constellationColorImage.uiItem.GetComponent<RawImage>().color = Color.clear;
return "<b>Constellation:</b> Unrecognised";
Expand All @@ -145,12 +143,14 @@ private void updateClick()
{
try
{
short inputFreq = short.Parse(frequencyInput.uiItem.GetComponent<TMP_InputField>().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);
}
Expand All @@ -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<CNConstellationModule>();
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);
Expand All @@ -199,12 +199,8 @@ private void updateClick()
/// </summary>
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<TMP_InputField>(); // TODO: eliminate the local storage
component.text = this.inputFreq.ToString();
frequencyInput.uiItem.GetComponent<TMP_InputField>().text = CNCSettings.Instance.PublicRadioFrequency.ToString();
updateClick();
}

/// <summary>
Expand Down

0 comments on commit a1d9905

Please sign in to comment.