Skip to content

Commit e2ad574

Browse files
authored
Allow script actions to use window for parameter selection
1 parent 1caffd8 commit e2ad574

6 files changed

Lines changed: 194 additions & 13 deletions

File tree

src/TSMapEditor/CCEngine/ScriptAction.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
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;
@@ -18,7 +19,10 @@ public ScriptActionPresetOption(int value, string text)
1819

1920
public string GetOptionText()
2021
{
21-
return Value + " - " + Text;
22+
if (string.IsNullOrEmpty(Text))
23+
return Value.ToString();
24+
else
25+
return Value + " - " + Text;
2226
}
2327
}
2428

@@ -36,6 +40,7 @@ public ScriptAction(int id)
3640
public string OptionsSectionName { get; set; } = string.Empty;
3741
public TriggerParamType ParamType { get; set; } = TriggerParamType.Unknown;
3842
public List<ScriptActionPresetOption> PresetOptions { get; } = new List<ScriptActionPresetOption>(0);
43+
public bool UseWindowSelection { get; set; } = false;
3944

4045
public void ReadIniSection(IniFile iniFile, string sectionName)
4146
{
@@ -45,6 +50,7 @@ public void ReadIniSection(IniFile iniFile, string sectionName)
4550
Description = iniSection.GetStringValue(nameof(Description), Description);
4651
OptionsSectionName = iniSection.GetStringValue(nameof(OptionsSectionName), OptionsSectionName);
4752
ParamDescription = iniSection.GetStringValue(nameof(ParamDescription), ParamDescription);
53+
UseWindowSelection = iniSection.GetBooleanValue(nameof(UseWindowSelection), UseWindowSelection);
4854
if (Enum.TryParse(iniSection.GetStringValue(nameof(ParamType), "Unknown"), out TriggerParamType result))
4955
{
5056
ParamType = result;

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ $CC22=selTypeOfAction:EditorPopUpSelector
2727
$CC23=lblParameterDescription:XNALabel
2828
$CC24=tbParameterValue:EditorNumberTextBox
2929
$CC25=btnEditorPresetValues:MenuButton
30-
$CC26=lblActionDescription:XNALabel
31-
$CC27=panelActionDescription:EditorPanel
30+
$CC26=btnEditorPresetValuesWindow:EditorButton
31+
$CC27=lblActionDescription:XNALabel
32+
$CC28=panelActionDescription:EditorPanel
3233
HasCloseButton=true
3334

3435
[lblWindowDescription]
@@ -177,6 +178,13 @@ $Width=getWidth(ScriptsWindow) - getRight(tbParameterValue) - EMPTY_SPACE_SIDES
177178
$Height=getHeight(tbParameterValue)
178179
Text=v
179180

181+
[btnEditorPresetValuesWindow]
182+
$X=getX(btnEditorPresetValues)
183+
$Y=getY(btnEditorPresetValues)
184+
$Width=getWidth(btnEditorPresetValues)
185+
$Height=getHeight(btnEditorPresetValues)
186+
Text=...
187+
180188
[lblActionDescription]
181189
$X=getX(lblName)
182190
$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
@@ -225,6 +225,9 @@
225225
<None Update="Config\Default\UI\Windows\SelectHouseTypeWindow.ini">
226226
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
227227
</None>
228+
<None Update="Config\UI\Windows\SelectScriptActionPresetOptionWindow.ini">
229+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
230+
</None>
228231
<None Update="Config\Default\UI\Windows\SelectParticleSystemTypeWindow.ini">
229232
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
230233
</None>

src/TSMapEditor/UI/Windows/ScriptsWindow.cs

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@ public ScriptsWindow(WindowManager windowManager, Map map, EditorState editorSta
5252
private XNALabel lblParameterDescription;
5353
private EditorNumberTextBox tbParameterValue;
5454
private MenuButton btnEditorPresetValues;
55+
private EditorButton btnEditorPresetValuesWindow;
5556
private XNALabel lblActionDescriptionValue;
5657
private XNADropDown ddScriptColor;
5758

5859
private SelectScriptActionWindow selectScriptActionWindow;
60+
private SelectScriptActionPresetOptionWindow selectScriptActionPresetOptionWindow;
5961
private EditorContextMenu actionListContextMenu;
6062

6163
private SelectBuildingTargetWindow selectBuildingTargetWindow;
@@ -92,6 +94,7 @@ public override void Initialize()
9294
lblParameterDescription = FindChild<XNALabel>(nameof(lblParameterDescription));
9395
tbParameterValue = FindChild<EditorNumberTextBox>(nameof(tbParameterValue));
9496
btnEditorPresetValues = FindChild<MenuButton>(nameof(btnEditorPresetValues));
97+
btnEditorPresetValuesWindow = FindChild<EditorButton>(nameof(btnEditorPresetValuesWindow));
9598
lblActionDescriptionValue = FindChild<XNALabel>(nameof(lblActionDescriptionValue));
9699
ddScriptColor = FindChild<XNADropDown>(nameof(ddScriptColor));
97100

@@ -109,6 +112,9 @@ public override void Initialize()
109112
btnEditorPresetValues.ContextMenu.OptionSelected += ContextMenu_OptionSelected;
110113
btnEditorPresetValues.LeftClick += BtnEditorPresetValues_LeftClick;
111114

115+
btnEditorPresetValuesWindow.LeftClick += BtnEditorPresetValuesWindow_LeftClick;
116+
btnEditorPresetValuesWindow.Disable();
117+
112118
tbName.TextChanged += TbName_TextChanged;
113119
tbParameterValue.TextChanged += TbParameterValue_TextChanged;
114120
lbScriptTypes.SelectedIndexChanged += LbScriptTypes_SelectedIndexChanged;
@@ -143,6 +149,10 @@ public override void Initialize()
143149
var selectScriptActionDarkeningPanel = DarkeningPanel.InitializeAndAddToParentControlWithChild(WindowManager, Parent, selectScriptActionWindow);
144150
selectScriptActionDarkeningPanel.Hidden += SelectScriptActionDarkeningPanel_Hidden;
145151

152+
selectScriptActionPresetOptionWindow = new SelectScriptActionPresetOptionWindow(WindowManager, map);
153+
var selectScriptActionPresetDarkeningPanel = DarkeningPanel.InitializeAndAddToParentControlWithChild(WindowManager, Parent, selectScriptActionPresetOptionWindow);
154+
selectScriptActionPresetDarkeningPanel.Hidden += SelectScriptActionPresetDarkeningPanel_Hidden;
155+
146156
selectBuildingTargetWindow = new SelectBuildingTargetWindow(WindowManager, map);
147157
var buildingTargetWindowDarkeningPanel = DarkeningPanel.InitializeAndAddToParentControlWithChild(WindowManager, Parent, selectBuildingTargetWindow);
148158
buildingTargetWindowDarkeningPanel.Hidden += BuildingTargetWindowDarkeningPanel_Hidden;
@@ -294,6 +304,18 @@ private void BtnEditorPresetValues_LeftClick(object sender, EventArgs e)
294304
}
295305
}
296306

307+
private void BtnEditorPresetValuesWindow_LeftClick(object sender, EventArgs e)
308+
{
309+
if (editedScript == null)
310+
return;
311+
312+
if (lbActions.SelectedItem == null)
313+
return;
314+
315+
var item = selectScriptActionPresetOptionWindow.GetMatchingItem(tbParameterValue.Text);
316+
selectScriptActionPresetOptionWindow.Open(item);
317+
}
318+
297319
private void ShowScriptReferences()
298320
{
299321
if (editedScript == null)
@@ -502,6 +524,18 @@ private void SelectScriptActionDarkeningPanel_Hidden(object sender, EventArgs e)
502524
InputIgnoreTime = TimeSpan.FromSeconds(Constants.UIAccidentalClickPreventionTime);
503525
}
504526

527+
528+
private void SelectScriptActionPresetDarkeningPanel_Hidden(object sender, EventArgs e)
529+
{
530+
if (lbActions.SelectedItem == null || editedScript == null)
531+
{
532+
return;
533+
}
534+
535+
if (selectScriptActionPresetOptionWindow.SelectedObject != null)
536+
tbParameterValue.Text = selectScriptActionPresetOptionWindow.GetSelectedItemText();
537+
}
538+
505539
private void LbActions_SelectedIndexChanged(object sender, EventArgs e)
506540
{
507541
if (lbActions.SelectedItem == null || editedScript == null)
@@ -526,7 +560,23 @@ private void LbActions_SelectedIndexChanged(object sender, EventArgs e)
526560
lblParameterDescription.Text = action == null ? "Parameter:" : action.ParamDescription + ":";
527561
lblActionDescriptionValue.Text = GetActionDescriptionFromIndex(entry.Action);
528562

529-
FillPresetContextMenu(entry, action);
563+
string text = null;
564+
565+
if (action.UseWindowSelection && action.PresetOptions.Count > 0)
566+
{
567+
btnEditorPresetValues.Disable();
568+
btnEditorPresetValuesWindow.Enable();
569+
text = selectScriptActionPresetOptionWindow.FillPresetOptions(entry, action);
570+
}
571+
else
572+
{
573+
btnEditorPresetValues.Enable();
574+
btnEditorPresetValuesWindow.Disable();
575+
text = FillPresetContextMenu(entry, action);
576+
}
577+
578+
if (text != null)
579+
tbParameterValue.Text = text;
530580
}
531581

532582
private void SetParameterEntryText(ScriptActionEntry scriptActionEntry, ScriptAction action)
@@ -585,13 +635,13 @@ private string GetBuildingWithPropertyText(int argument)
585635
return GetBuildingWithPropertyText(index, property);
586636
}
587637

588-
private void FillPresetContextMenu(ScriptActionEntry entry, ScriptAction action)
638+
private string FillPresetContextMenu(ScriptActionEntry entry, ScriptAction action)
589639
{
590640
btnEditorPresetValues.ContextMenu.ClearItems();
591641

592642
if (action == null)
593643
{
594-
return;
644+
return null;
595645
}
596646

597647
action.PresetOptions.ForEach(p => btnEditorPresetValues.ContextMenu.AddItem(new XNAContextMenuItem() { Text = p.GetOptionText() }));
@@ -630,7 +680,9 @@ private void FillPresetContextMenu(ScriptActionEntry entry, ScriptAction action)
630680
fittingItem = btnEditorPresetValues.ContextMenu.Items.Find(item => item.Text.StartsWith(entry.Argument.ToString()));
631681

632682
if (fittingItem != null)
633-
tbParameterValue.Text = fittingItem.Text;
683+
return fittingItem.Text;
684+
685+
return null;
634686
}
635687

636688
private void LbScriptTypes_SelectedIndexChanged(object sender, EventArgs e) => RefreshSelectedScript();
@@ -734,9 +786,9 @@ private void EditScript(Script script)
734786
for (int i = 0; i < editedScript.Actions.Count; i++)
735787
{
736788
var actionEntry = editedScript.Actions[i];
737-
lbActions.AddItem(new XNAListBoxItem()
738-
{
739-
Text = GetActionEntryText(i, actionEntry),
789+
lbActions.AddItem(new XNAListBoxItem()
790+
{
791+
Text = GetActionEntryText(i, actionEntry),
740792
Tag = actionEntry
741793
});
742794
}
@@ -754,7 +806,7 @@ private string GetActionEntryText(int index, ScriptActionEntry entry)
754806
{
755807
ScriptAction action = GetScriptAction(entry.Action);
756808
if (action == null)
757-
return "#" + index + " - Unknown (" + entry.Argument.ToString(CultureInfo.InvariantCulture) + ")";
809+
return "#" + index + " - Unknown (" + entry.Argument.ToString(CultureInfo.InvariantCulture) + ")";
758810

759811
return "#" + index + " - " + action.Name + " (" + entry.Argument.ToString(CultureInfo.InvariantCulture) + ")";
760812
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
lbObjectList.AddItem(item);
48+
49+
if (presetOption == SelectedObject)
50+
lbObjectList.SelectedIndex = lbObjectList.Items.Count - 1;
51+
}
52+
}
53+
54+
public string FillPresetOptions(ScriptActionEntry entry, ScriptAction action)
55+
{
56+
presetOptions.Clear();
57+
presetOptions.AddRange(action.PresetOptions);
58+
59+
var fittingItem = presetOptions.Find(item => item.Text.StartsWith(entry.Argument.ToString()));
60+
61+
if (fittingItem != null)
62+
return fittingItem.Text;
63+
64+
return null;
65+
}
66+
67+
public ScriptActionPresetOption GetMatchingItem(string text)
68+
{
69+
return presetOptions.Find(item => item.GetOptionText().Equals(text, StringComparison.Ordinal));
70+
}
71+
72+
public string GetSelectedItemText()
73+
{
74+
if (SelectedObject != null)
75+
return SelectedObject.GetOptionText();
76+
77+
return string.Empty;
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)