Skip to content

Commit

Permalink
Add button for checking the references of a local variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Rampastring committed Sep 2, 2023
1 parent b7967ee commit 7e317e6
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/TSMapEditor/Config/UI/Windows/LocalVariablesWindow.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ $CC2=lbLocalVariables:EditorListBox
$CC3=lblName:XNALabel
$CC4=tbName:EditorTextBox
$CC5=chkInitialState:XNACheckBox
$Width=getRight(tbName) + HORIZONTAL_SPACING
$Height=getBottom(lbLocalVariables) + VERTICAL_SPACING
$CC6=btnViewVariableUsages:EditorButton
$Width=getRight(tbName) + EMPTY_SPACE_SIDES
$Height=getBottom(lbLocalVariables) + EMPTY_SPACE_BOTTOM
HasCloseButton=true

[lblLocalVariables]
Expand All @@ -18,7 +19,7 @@ Text=Local Variables:
[btnNewLocalVariable]
$X=EMPTY_SPACE_SIDES
$Y=getBottom(lblLocalVariables) + EMPTY_SPACE_TOP
$Width=150
$Width=200
Text=New Local Variable

[lbLocalVariables]
Expand All @@ -35,10 +36,16 @@ Text=Name:
[tbName]
$X=getRight(lblName) + HORIZONTAL_SPACING
$Y=getY(lblName) - 1
$Width=150
$Width=200

[chkInitialState]
$X=getX(lblName)
$Y=getBottom(tbName) + VERTICAL_SPACING
Text='Set/on/true' on scenario start

[btnViewVariableUsages]
$X=getX(lblName)
$Y=getBottom(chkInitialState) + EMPTY_SPACE_TOP
$Width=getRight(tbName) - getX(btnViewVariableUsages)
Text=View Variable Usages

12 changes: 12 additions & 0 deletions src/TSMapEditor/Misc/ListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,17 @@ public static void Swap<T>(this List<T> list, int index1, int index2)
{
(list[index1], list[index2]) = (list[index2], list[index1]);
}

/// <summary>
/// Fetches an element at the given index.
/// If the element is out of bounds, returns null.
/// </summary>
public static T GetElementIfInRange<T>(this List<T> list, int index)
{
if (index < 0 || index >= list.Count)
return default;

return list[index];
}
}
}
97 changes: 97 additions & 0 deletions src/TSMapEditor/UI/Windows/LocalVariablesWindow.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rampastring.Tools;
using Rampastring.XNAUI;
using Rampastring.XNAUI.XNAControls;
using TSMapEditor.Misc;
using TSMapEditor.Models;
using TSMapEditor.Models.Enums;
using TSMapEditor.UI.Controls;

namespace TSMapEditor.UI.Windows
Expand Down Expand Up @@ -31,6 +37,8 @@ public override void Initialize()
chkInitialState = FindChild<XNACheckBox>(nameof(chkInitialState));

FindChild<EditorButton>("btnNewLocalVariable").LeftClick += BtnNewLocalVariable_LeftClick;
FindChild<EditorButton>("btnViewVariableUsages").LeftClick += BtnViewVariableUsages_LeftClick;


lbLocalVariables.SelectedIndexChanged += LbLocalVariables_SelectedIndexChanged;
}
Expand All @@ -43,6 +51,95 @@ private void BtnNewLocalVariable_LeftClick(object sender, EventArgs e)
lbLocalVariables.ScrollToBottom();
}

private void BtnViewVariableUsages_LeftClick(object sender, EventArgs e)
{
if (editedLocalVariable == null)
{
EditorMessageBox.Show(WindowManager, "Select a variable", "Please select a variable first.", MessageBoxButtons.OK);
return;
}

var list = new List<string>();

map.Triggers.ForEach(trigger =>
{
foreach (var action in trigger.Actions)
{
var actionType = map.EditorConfig.TriggerActionTypes.GetElementIfInRange(action.ActionIndex);
if (actionType == null)
continue;
for (int i = 0; i < actionType.Parameters.Length; i++)
{
var parameter = actionType.Parameters[i];
if (parameter.TriggerParamType == TriggerParamType.LocalVariable)
{
if (Conversions.IntFromString(action.Parameters[i], -1) == editedLocalVariable.Index)
{
list.Add($"Trigger action of '{trigger.Name}' ({trigger.ID})");
break;
}
}
}
}
foreach (var triggerEvent in trigger.Conditions)
{
var eventType = map.EditorConfig.TriggerEventTypes.GetElementIfInRange(triggerEvent.ConditionIndex);
if (eventType == null)
continue;
if (eventType.P1Type == TriggerParamType.LocalVariable)
{
if (triggerEvent.Parameter1 == editedLocalVariable.Index)
{
list.Add($"Trigger event of '{trigger.Name}' ({trigger.ID})");
}
}
if (eventType.P2Type == TriggerParamType.LocalVariable)
{
if (triggerEvent.Parameter2 == editedLocalVariable.Index)
{
list.Add($"Trigger event of '{trigger.Name}' ({trigger.ID})");
}
}
}
});

map.Scripts.ForEach(script =>
{
foreach (var scriptAction in script.Actions)
{
var scriptActionType = map.EditorConfig.ScriptActions.GetElementIfInRange(scriptAction.Action);
if (scriptActionType == null)
continue;
if (scriptActionType.ParamType == TriggerParamType.LocalVariable &&
scriptAction.Argument == editedLocalVariable.Index)
{
list.Add($"Script action of '{script.Name}' ({script.ININame})");
}
}
});


if (list.Count == 0)
{
EditorMessageBox.Show(WindowManager, "No usages found",
$"No triggers or scripts make use of the selected local variable '{editedLocalVariable.Name}'", MessageBoxButtons.OK);
}
else
{
EditorMessageBox.Show(WindowManager,
"Local Variable Usages",
$"The following usages were found for the selected local variable '{editedLocalVariable.Name}':" + Environment.NewLine + Environment.NewLine +
string.Join(Environment.NewLine, list.Select(e => "- " + e)),
MessageBoxButtons.OK);
}
}

private void LbLocalVariables_SelectedIndexChanged(object sender, EventArgs e)
{
tbName.TextChanged -= TbName_TextChanged;
Expand Down

0 comments on commit 7e317e6

Please sign in to comment.