Skip to content

Commit 07f0e24

Browse files
committed
Allow script actions to use window for parameter selection
1 parent dce09fd commit 07f0e24

6 files changed

Lines changed: 201 additions & 11 deletions

File tree

src/TSMapEditor/CCEngine/ScriptAction.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
1-
using Rampastring.Tools;
1+
using Microsoft.Xna.Framework;
2+
using Rampastring.Tools;
23
using System;
34
using System.Collections.Generic;
45
using TSMapEditor.Models.Enums;
56

67
namespace TSMapEditor.CCEngine
78
{
8-
public struct ScriptActionPresetOption
9+
public class ScriptActionPresetOption
910
{
1011
public int Value;
1112
public string Text;
13+
public Color? Color;
1214

13-
public ScriptActionPresetOption(int value, string text)
15+
public ScriptActionPresetOption()
16+
{
17+
}
18+
19+
public ScriptActionPresetOption(int value, string text, Color? color = null)
1420
{
1521
Value = value;
1622
Text = text;
23+
Color = color;
1724
}
1825

1926
public string GetOptionText()
2027
{
21-
return Value + " - " + Text;
28+
if (string.IsNullOrEmpty(Text))
29+
return Value.ToString();
30+
else
31+
return Value + " - " + Text;
2232
}
2333
}
2434

@@ -35,13 +45,15 @@ public ScriptAction(int id)
3545
public string ParamDescription { get; set; } = "Use 0";
3646
public TriggerParamType ParamType { get; set; } = TriggerParamType.Unknown;
3747
public List<ScriptActionPresetOption> PresetOptions { get; } = new List<ScriptActionPresetOption>(0);
48+
public bool UseWindowSelection { get; set; } = false;
3849

3950
public void ReadIniSection(IniSection iniSection)
4051
{
4152
ID = iniSection.GetIntValue("IDOverride", ID);
4253
Name = iniSection.GetStringValue(nameof(Name), Name);
4354
Description = iniSection.GetStringValue(nameof(Description), Description);
4455
ParamDescription = iniSection.GetStringValue(nameof(ParamDescription), ParamDescription);
56+
UseWindowSelection = iniSection.GetBooleanValue(nameof(UseWindowSelection), UseWindowSelection);
4557
if (Enum.TryParse(iniSection.GetStringValue(nameof(ParamType), "Unknown"), out TriggerParamType result))
4658
{
4759
ParamType = result;

src/TSMapEditor/Config/UI/Windows/ScriptsWindow.ini

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ $CC20=tbParameterValue:EditorNumberTextBox
2525
$CC21=btnEditorPresetValues:MenuButton
2626
$CC22=lblActionDescription:XNALabel
2727
$CC23=panelActionDescription:EditorPanel
28+
$CC24=btnEditorPresetValuesWindow:EditorButton
2829
HasCloseButton=true
2930

3031
[lblWindowDescription]
@@ -152,6 +153,13 @@ $Width=getWidth(ScriptsWindow) - getRight(tbParameterValue) - EMPTY_SPACE_SIDES
152153
$Height=getHeight(tbParameterValue)
153154
Text=v
154155

156+
[btnEditorPresetValuesWindow]
157+
$X=getX(btnEditorPresetValues)
158+
$Y=getY(btnEditorPresetValues)
159+
$Width=getWidth(btnEditorPresetValues)
160+
$Height=getHeight(btnEditorPresetValues)
161+
Text=...
162+
155163
[lblActionDescription]
156164
$X=getX(lblName)
157165
$Y=getBottom(tbParameterValue) + VERTICAL_SPACING
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[SelectScriptActionPresetOptionWindow]
2+
$Width=350
3+
$Height=RESOLUTION_HEIGHT - 100
4+
$CC0=lblDescription:XNALabel
5+
$CC1=tbSearch:EditorSuggestionTextBox
6+
$CC2=btnSelect:EditorButton
7+
$CC3=lbObjectList:EditorListBox
8+
9+
[lblDescription]
10+
$X=EMPTY_SPACE_SIDES
11+
$Y=EMPTY_SPACE_TOP
12+
FontIndex=1
13+
Text=Select parameter value:
14+
15+
[tbSearch]
16+
$X=EMPTY_SPACE_SIDES
17+
$Y=getBottom(lblDescription) + EMPTY_SPACE_TOP
18+
$Width=getWidth(SelectScriptActionPresetOptionWindow) - (EMPTY_SPACE_SIDES * 2)
19+
Suggestion=Search parameter values...
20+
21+
[btnSelect]
22+
$Width=100
23+
$X=(getWidth(SelectScriptActionPresetOptionWindow) - getWidth(btnSelect)) / 2
24+
$Y=getHeight(SelectScriptActionPresetOptionWindow) - EMPTY_SPACE_BOTTOM - getHeight(btnSelect)
25+
Text=Select
26+
27+
[lbObjectList]
28+
$X=EMPTY_SPACE_SIDES
29+
$Y=getBottom(tbSearch) + VERTICAL_SPACING
30+
$Width=getWidth(tbSearch)
31+
$Height=getY(btnSelect) - getY(lbObjectList) - EMPTY_SPACE_TOP
32+

src/TSMapEditor/TSMapEditor.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@
224224
<None Update="Config\UI\Windows\SelectScriptActionWindow.ini">
225225
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
226226
</None>
227+
<None Update="Config\UI\Windows\SelectScriptActionPresetOptionWindow.ini">
228+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
229+
</None>
227230
<None Update="Config\UI\Windows\SelectSoundWindow.ini">
228231
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
229232
</None>

src/TSMapEditor/UI/Windows/ScriptsWindow.cs

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ public ScriptsWindow(WindowManager windowManager, Map map, EditorState editorSta
4040
private XNALabel lblParameterDescription;
4141
private EditorNumberTextBox tbParameterValue;
4242
private MenuButton btnEditorPresetValues;
43+
private EditorButton btnEditorPresetValuesWindow;
4344
private XNALabel lblActionDescriptionValue;
4445

4546
private SelectScriptActionWindow selectScriptActionWindow;
47+
private SelectScriptActionPresetOptionWindow selectScriptActionPresetOptionWindow;
4648
private XNAContextMenu actionListContextMenu;
4749

4850
private SelectBuildingTargetWindow selectBuildingTargetWindow;
@@ -61,6 +63,7 @@ public override void Initialize()
6163
lblParameterDescription = FindChild<XNALabel>(nameof(lblParameterDescription));
6264
tbParameterValue = FindChild<EditorNumberTextBox>(nameof(tbParameterValue));
6365
btnEditorPresetValues = FindChild<MenuButton>(nameof(btnEditorPresetValues));
66+
btnEditorPresetValuesWindow = FindChild<EditorButton>(nameof(btnEditorPresetValuesWindow));
6467
lblActionDescriptionValue = FindChild<XNALabel>(nameof(lblActionDescriptionValue));
6568

6669
var presetValuesContextMenu = new XNAContextMenu(WindowManager);
@@ -69,6 +72,9 @@ public override void Initialize()
6972
btnEditorPresetValues.ContextMenu.OptionSelected += ContextMenu_OptionSelected;
7073
btnEditorPresetValues.LeftClick += BtnEditorPresetValues_LeftClick;
7174

75+
btnEditorPresetValuesWindow.LeftClick += BtnEditorPresetValuesWindow_LeftClick;
76+
btnEditorPresetValuesWindow.Disable();
77+
7278
tbName.TextChanged += TbName_TextChanged;
7379
tbParameterValue.TextChanged += TbParameterValue_TextChanged;
7480
lbScriptTypes.SelectedIndexChanged += LbScriptTypes_SelectedIndexChanged;
@@ -78,6 +84,10 @@ public override void Initialize()
7884
var selectScriptActionDarkeningPanel = DarkeningPanel.InitializeAndAddToParentControlWithChild(WindowManager, Parent, selectScriptActionWindow);
7985
selectScriptActionDarkeningPanel.Hidden += SelectScriptActionDarkeningPanel_Hidden;
8086

87+
selectScriptActionPresetOptionWindow = new SelectScriptActionPresetOptionWindow(WindowManager, map);
88+
var selectScriptActionPresetDarkeningPanel = DarkeningPanel.InitializeAndAddToParentControlWithChild(WindowManager, Parent, selectScriptActionPresetOptionWindow);
89+
selectScriptActionPresetDarkeningPanel.Hidden += SelectScriptActionPresetDarkeningPanel_Hidden;
90+
8191
selectBuildingTargetWindow = new SelectBuildingTargetWindow(WindowManager, map);
8292
var buildingTargetWindowDarkeningPanel = DarkeningPanel.InitializeAndAddToParentControlWithChild(WindowManager, Parent, selectBuildingTargetWindow);
8393
buildingTargetWindowDarkeningPanel.Hidden += BuildingTargetWindowDarkeningPanel_Hidden;
@@ -216,6 +226,18 @@ private void BtnEditorPresetValues_LeftClick(object sender, EventArgs e)
216226
}
217227
}
218228

229+
private void BtnEditorPresetValuesWindow_LeftClick(object sender, EventArgs e)
230+
{
231+
if (editedScript == null)
232+
return;
233+
234+
if (lbActions.SelectedItem == null)
235+
return;
236+
237+
var item = selectScriptActionPresetOptionWindow.GetMatchingItem(tbParameterValue.Text);
238+
selectScriptActionPresetOptionWindow.Open(item);
239+
}
240+
219241
private void BtnAddScript_LeftClick(object sender, EventArgs e)
220242
{
221243
map.Scripts.Add(new Script(map.GetNewUniqueInternalId()) { Name = "New script" });
@@ -355,6 +377,18 @@ private void SelectScriptActionDarkeningPanel_Hidden(object sender, EventArgs e)
355377
LbActions_SelectedIndexChanged(this, EventArgs.Empty);
356378
}
357379

380+
381+
private void SelectScriptActionPresetDarkeningPanel_Hidden(object sender, EventArgs e)
382+
{
383+
if (lbActions.SelectedItem == null || editedScript == null)
384+
{
385+
return;
386+
}
387+
388+
if (selectScriptActionPresetOptionWindow.SelectedObject != null)
389+
tbParameterValue.Text = selectScriptActionPresetOptionWindow.GetSelectedItemText();
390+
}
391+
358392
private void LbActions_SelectedIndexChanged(object sender, EventArgs e)
359393
{
360394
if (lbActions.SelectedItem == null || editedScript == null)
@@ -378,7 +412,23 @@ private void LbActions_SelectedIndexChanged(object sender, EventArgs e)
378412
lblParameterDescription.Text = action == null ? "Parameter:" : action.ParamDescription + ":";
379413
lblActionDescriptionValue.Text = GetActionDescriptionFromIndex(entry.Action);
380414

381-
FillPresetContextMenu(entry, action);
415+
string text = null;
416+
417+
if (action.UseWindowSelection && action.PresetOptions.Count > 0)
418+
{
419+
btnEditorPresetValues.Disable();
420+
btnEditorPresetValuesWindow.Enable();
421+
text = selectScriptActionPresetOptionWindow.FillPresetOptions(entry, action);
422+
}
423+
else
424+
{
425+
btnEditorPresetValues.Enable();
426+
btnEditorPresetValuesWindow.Disable();
427+
text = FillPresetContextMenu(entry, action);
428+
}
429+
430+
if (text != null)
431+
tbParameterValue.Text = text;
382432
}
383433

384434
private void SetParameterEntryText(ScriptActionEntry scriptActionEntry, ScriptAction action)
@@ -431,7 +481,7 @@ private string GetBuildingWithPropertyText(int argument)
431481
return GetBuildingWithPropertyText(index, property);
432482
}
433483

434-
private void FillPresetContextMenu(ScriptActionEntry entry, ScriptAction action)
484+
private string FillPresetContextMenu(ScriptActionEntry entry, ScriptAction action)
435485
{
436486
btnEditorPresetValues.ContextMenu.ClearItems();
437487

@@ -468,7 +518,9 @@ private void FillPresetContextMenu(ScriptActionEntry entry, ScriptAction action)
468518

469519
var fittingItem = btnEditorPresetValues.ContextMenu.Items.Find(item => item.Text.StartsWith(entry.Argument.ToString()));
470520
if (fittingItem != null)
471-
tbParameterValue.Text = fittingItem.Text;
521+
return fittingItem.Text;
522+
523+
return null;
472524
}
473525

474526
private void LbScriptTypes_SelectedIndexChanged(object sender, EventArgs e) => RefreshSelectedScript();
@@ -532,9 +584,9 @@ private void EditScript(Script script)
532584
for (int i = 0; i < editedScript.Actions.Count; i++)
533585
{
534586
var actionEntry = editedScript.Actions[i];
535-
lbActions.AddItem(new XNAListBoxItem()
536-
{
537-
Text = GetActionEntryText(i, actionEntry),
587+
lbActions.AddItem(new XNAListBoxItem()
588+
{
589+
Text = GetActionEntryText(i, actionEntry),
538590
Tag = actionEntry
539591
});
540592
}
@@ -546,7 +598,7 @@ private string GetActionEntryText(int index, ScriptActionEntry entry)
546598
{
547599
ScriptAction action = GetScriptAction(entry.Action);
548600
if (action == null)
549-
return "#" + index + " - Unknown (" + entry.Argument.ToString(CultureInfo.InvariantCulture) + ")";
601+
return "#" + index + " - Unknown (" + entry.Argument.ToString(CultureInfo.InvariantCulture) + ")";
550602

551603
return "#" + index + " - " + action.Name + " (" + entry.Argument.ToString(CultureInfo.InvariantCulture) + ")";
552604
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using Microsoft.Xna.Framework;
2+
using Rampastring.XNAUI;
3+
using Rampastring.XNAUI.XNAControls;
4+
using System;
5+
using System.Collections.Generic;
6+
using TSMapEditor.CCEngine;
7+
using TSMapEditor.Models;
8+
using TSMapEditor.Models.Enums;
9+
10+
namespace TSMapEditor.UI.Windows
11+
{
12+
public class SelectScriptActionPresetOptionWindow : SelectObjectWindow<ScriptActionPresetOption>
13+
{
14+
public SelectScriptActionPresetOptionWindow(WindowManager windowManager, Map map) : base(windowManager)
15+
{
16+
this.map = map;
17+
}
18+
19+
private Map map;
20+
public List<ScriptActionPresetOption> presetOptions { get; } = new List<ScriptActionPresetOption>(0);
21+
22+
public override void Initialize()
23+
{
24+
Name = nameof(SelectScriptActionPresetOptionWindow);
25+
base.Initialize();
26+
}
27+
28+
protected override void LbObjectList_SelectedIndexChanged(object sender, EventArgs e)
29+
{
30+
if (lbObjectList.SelectedItem == null)
31+
{
32+
SelectedObject = null;
33+
return;
34+
}
35+
36+
SelectedObject = (ScriptActionPresetOption)lbObjectList.SelectedItem.Tag;
37+
}
38+
39+
protected override void ListObjects()
40+
{
41+
lbObjectList.Clear();
42+
43+
foreach (ScriptActionPresetOption presetOption in presetOptions)
44+
{
45+
var item = new XNAListBoxItem() { Text = $"{presetOption.GetOptionText()}", Tag = presetOption };
46+
47+
if (presetOption.Color != null)
48+
item.TextColor = (Color)presetOption.Color;
49+
50+
lbObjectList.AddItem(item);
51+
52+
if (presetOption == SelectedObject)
53+
lbObjectList.SelectedIndex = lbObjectList.Items.Count - 1;
54+
}
55+
}
56+
57+
public string FillPresetOptions(ScriptActionEntry entry, ScriptAction action)
58+
{
59+
presetOptions.Clear();
60+
presetOptions.AddRange(action.PresetOptions);
61+
62+
var fittingItem = presetOptions.Find(item => item.Text.StartsWith(entry.Argument.ToString()));
63+
64+
if (fittingItem != null)
65+
return fittingItem.Text;
66+
67+
return null;
68+
}
69+
70+
public ScriptActionPresetOption GetMatchingItem(string text)
71+
{
72+
return presetOptions.Find(item => item.GetOptionText().Equals(text, StringComparison.Ordinal));
73+
}
74+
75+
public string GetSelectedItemText()
76+
{
77+
if (SelectedObject != null)
78+
return SelectedObject.GetOptionText();
79+
80+
return string.Empty;
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)