Skip to content

Commit

Permalink
[VSE] Fix for editing controllables policy and resetting map signals …
Browse files Browse the repository at this point in the history
…policies.
  • Loading branch information
KarByc committed Aug 25, 2021
1 parent f60e2bf commit 1dac0bb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ private void OnMapChanged(ScenarioMapManager.MapMetaData mapData)
if (!controllable.IsEditableOnMap)
controllable.Dispose();
}

LoadMapControllables();
}

Expand All @@ -135,7 +136,7 @@ private void LoadMapControllables()
var scenarioControllable = mapSignal.CurrentSignalLight.gameObject.AddComponent<ScenarioControllable>();
scenarioControllable.Uid = mapSignal.UID;
var policy = new List<ControlAction>(mapSignal.DefaultControlPolicy);
scenarioControllable.Setup(source, mapSignalVariant,policy);
scenarioControllable.Setup(source, mapSignalVariant, policy);
var boxCollider = scenarioControllable.gameObject.AddComponent<BoxCollider>();
boxCollider.isTrigger = true;
var meshRenderers = scenarioControllable.GetComponentsInChildren<MeshRenderer>();
Expand All @@ -146,11 +147,12 @@ private void LoadMapControllables()
boxCollider.size = Vector3.one;
continue;
}

var bounds = meshRenderers[0].bounds;
for (var i = 1; i < meshRenderers.Length; i++)
bounds.Encapsulate(meshRenderers[i].bounds);
boxCollider.center = bounds.center-scenarioControllable.transform.position;
boxCollider.size = bounds.extents*2;
boxCollider.center = bounds.center - scenarioControllable.transform.position;
boxCollider.size = bounds.extents * 2;
}
}

Expand All @@ -162,7 +164,13 @@ private void InstanceOnScenarioReset()
for (var i = Controllables.Count - 1; i >= 0; i--)
{
var controllable = Controllables[i];
if (!controllable.IsEditableOnMap) continue;
if (!controllable.IsEditableOnMap)
{
controllable.Policy =
new List<ControlAction>(controllable.Variant.controllable.DefaultControlPolicy);
continue;
}

controllable.RemoveFromMap();
controllable.Dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ private void SetPolicy(List<ControlAction> policy)
Policy = policy;
if (policy != null)
{
while (entries.Count < policy.Count) AddPolicyEntry();
while (entries.Count > policy.Count) RemovePolicyEntry(entries[0]);
while (entries.Count < policy.Count) AddPolicyEntry(updatePolicy: false);
while (entries.Count > policy.Count) RemovePolicyEntry(entries[0], updatePolicy: false);

for (var i = 0; i < entries.Count; i++)
{
Expand Down Expand Up @@ -168,7 +168,8 @@ public void AddPolicyEntryWithUndo()
{
var newEntry = ScenarioManager.Instance.prefabsPools.GetInstance(policyEntryPrefab.gameObject)
.GetComponent<PolicyEntry>();
var undoRecord = new GenericUndo<PolicyEntry>(newEntry, "Undo adding a policy entry", RemovePolicyEntry);
var undoRecord = new GenericUndo<PolicyEntry>(newEntry, "Undo adding a policy entry",
entry => RemovePolicyEntry(entry));
ScenarioManager.Instance.GetExtension<ScenarioUndoManager>().RegisterRecord(undoRecord);
AddPolicyEntry(newEntry);
}
Expand All @@ -177,7 +178,8 @@ public void AddPolicyEntryWithUndo()
/// Add a single policy entry to this edit panel
/// </summary>
/// <param name="newEntry">Policy entry to be added</param>
private void AddPolicyEntry(PolicyEntry newEntry = null)
/// <param name="updatePolicy">Should the policy be updated</param>
private void AddPolicyEntry(PolicyEntry newEntry = null, bool updatePolicy = true)
{
if (newEntry == null)
newEntry = ScenarioManager.Instance.prefabsPools.GetInstance(policyEntryPrefab.gameObject)
Expand All @@ -186,6 +188,8 @@ private void AddPolicyEntry(PolicyEntry newEntry = null)
newEntry.Initialize(this, validActions, validStates, validActions[0], "");
newEntry.transform.SetParent(transform);
newEntry.gameObject.SetActive(true);
if (updatePolicy)
UpdatePolicy();
UnityUtilities.LayoutRebuild(transform as RectTransform);
}

Expand Down Expand Up @@ -215,12 +219,14 @@ public void RemovePolicyEntryWithUndo(PolicyEntry entry)
/// Remove the policy entry from this edit panel
/// </summary>
/// <param name="entry">Policy entry to be removed</param>
public void RemovePolicyEntry(PolicyEntry entry)
/// <param name="updatePolicy">Should the policy be updated</param>
public void RemovePolicyEntry(PolicyEntry entry, bool updatePolicy = true)
{
entry.gameObject.SetActive(false);
entries.Remove(entry);
entry.Deinitialize();
UpdatePolicy();
if (updatePolicy)
UpdatePolicy();
UnityUtilities.LayoutRebuild(transform as RectTransform);
}

Expand Down Expand Up @@ -268,6 +274,7 @@ public void PastePolicy()
pasteAction.Invoke();
return;
}

//Ask for replacing current policy
var popupData = new ConfirmationPopup.PopupData
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public void Remove()
/// <returns>Is policy valid for the given controllable</returns>
private bool Validate()
{
return !string.IsNullOrEmpty(Policy.Action) && !string.IsNullOrEmpty(Policy.Value);
return !string.IsNullOrEmpty(Policy.Action);
}
}
}

0 comments on commit 1dac0bb

Please sign in to comment.