Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow script actions to use window for parameter selection #176

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/TSMapEditor/CCEngine/ScriptAction.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
using Rampastring.Tools;
using Microsoft.Xna.Framework;
using Rampastring.Tools;
using System;
using System.Collections.Generic;
using TSMapEditor.Models.Enums;

namespace TSMapEditor.CCEngine
{
public struct ScriptActionPresetOption
public class ScriptActionPresetOption
{
public int Value;
public string Text;
public Color? Color;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can see, there is currently no way to actually assign this a value? The INI parsing code for preset options is unchanged.


public ScriptActionPresetOption(int value, string text)
public ScriptActionPresetOption()
{
}
Comment on lines +15 to +17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I'm missing something, this constructor is never used, so it can be removed.


public ScriptActionPresetOption(int value, string text, Color? color = null)
{
Value = value;
Text = text;
Color = color;
}

public string GetOptionText()
{
return Value + " - " + Text;
if (string.IsNullOrEmpty(Text))
return Value.ToString();
else
return Value + " - " + Text;
}
}

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

public void ReadIniSection(IniSection iniSection)
{
ID = iniSection.GetIntValue("IDOverride", ID);
Name = iniSection.GetStringValue(nameof(Name), Name);
Description = iniSection.GetStringValue(nameof(Description), Description);
ParamDescription = iniSection.GetStringValue(nameof(ParamDescription), ParamDescription);
UseWindowSelection = iniSection.GetBooleanValue(nameof(UseWindowSelection), UseWindowSelection);
if (Enum.TryParse(iniSection.GetStringValue(nameof(ParamType), "Unknown"), out TriggerParamType result))
{
ParamType = result;
Expand Down
8 changes: 8 additions & 0 deletions src/TSMapEditor/Config/UI/Windows/ScriptsWindow.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ $CC20=tbParameterValue:EditorNumberTextBox
$CC21=btnEditorPresetValues:MenuButton
$CC22=lblActionDescription:XNALabel
$CC23=panelActionDescription:EditorPanel
$CC24=btnEditorPresetValuesWindow:EditorButton
Comment on lines 25 to +28
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$CC21=btnEditorPresetValues:MenuButton
$CC22=lblActionDescription:XNALabel
$CC23=panelActionDescription:EditorPanel
$CC24=btnEditorPresetValuesWindow:EditorButton
$CC21=btnEditorPresetValues:MenuButton
$CC22=btnEditorPresetValuesWindow:EditorButton
$CC23=lblActionDescription:XNALabel
$CC24=panelActionDescription:EditorPanel

Since this exists visually in the same space as btnEditorPresetValues, and the [btnEditorPresetValuesWindow] section has been placed under the [btnEditorPresetValues] section, it would be logical for this to also be defined right below btnEditorPresetValues in the list.

HasCloseButton=true

[lblWindowDescription]
Expand Down Expand Up @@ -152,6 +153,13 @@ $Width=getWidth(ScriptsWindow) - getRight(tbParameterValue) - EMPTY_SPACE_SIDES
$Height=getHeight(tbParameterValue)
Text=v

[btnEditorPresetValuesWindow]
$X=getX(btnEditorPresetValues)
$Y=getY(btnEditorPresetValues)
$Width=getWidth(btnEditorPresetValues)
$Height=getHeight(btnEditorPresetValues)
Text=...

[lblActionDescription]
$X=getX(lblName)
$Y=getBottom(tbParameterValue) + VERTICAL_SPACING
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[SelectScriptActionPresetOptionWindow]
$Width=350
$Height=RESOLUTION_HEIGHT - 100
$CC0=lblDescription:XNALabel
$CC1=tbSearch:EditorSuggestionTextBox
$CC2=btnSelect:EditorButton
$CC3=lbObjectList:EditorListBox

[lblDescription]
$X=EMPTY_SPACE_SIDES
$Y=EMPTY_SPACE_TOP
FontIndex=1
Text=Select parameter value:

[tbSearch]
$X=EMPTY_SPACE_SIDES
$Y=getBottom(lblDescription) + EMPTY_SPACE_TOP
$Width=getWidth(SelectScriptActionPresetOptionWindow) - (EMPTY_SPACE_SIDES * 2)
Suggestion=Search parameter values...

[btnSelect]
$Width=100
$X=(getWidth(SelectScriptActionPresetOptionWindow) - getWidth(btnSelect)) / 2
$Y=getHeight(SelectScriptActionPresetOptionWindow) - EMPTY_SPACE_BOTTOM - getHeight(btnSelect)
Text=Select

[lbObjectList]
$X=EMPTY_SPACE_SIDES
$Y=getBottom(tbSearch) + VERTICAL_SPACING
$Width=getWidth(tbSearch)
$Height=getY(btnSelect) - getY(lbObjectList) - EMPTY_SPACE_TOP

3 changes: 3 additions & 0 deletions src/TSMapEditor/TSMapEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@
<None Update="Config\UI\Windows\SelectScriptActionWindow.ini">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Config\UI\Windows\SelectScriptActionPresetOptionWindow.ini">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Config\UI\Windows\SelectSoundWindow.ini">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
66 changes: 59 additions & 7 deletions src/TSMapEditor/UI/Windows/ScriptsWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ public ScriptsWindow(WindowManager windowManager, Map map, EditorState editorSta
private XNALabel lblParameterDescription;
private EditorNumberTextBox tbParameterValue;
private MenuButton btnEditorPresetValues;
private EditorButton btnEditorPresetValuesWindow;
private XNALabel lblActionDescriptionValue;

private SelectScriptActionWindow selectScriptActionWindow;
private SelectScriptActionPresetOptionWindow selectScriptActionPresetOptionWindow;
private XNAContextMenu actionListContextMenu;

private SelectBuildingTargetWindow selectBuildingTargetWindow;
Expand All @@ -61,6 +63,7 @@ public override void Initialize()
lblParameterDescription = FindChild<XNALabel>(nameof(lblParameterDescription));
tbParameterValue = FindChild<EditorNumberTextBox>(nameof(tbParameterValue));
btnEditorPresetValues = FindChild<MenuButton>(nameof(btnEditorPresetValues));
btnEditorPresetValuesWindow = FindChild<EditorButton>(nameof(btnEditorPresetValuesWindow));
lblActionDescriptionValue = FindChild<XNALabel>(nameof(lblActionDescriptionValue));

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

btnEditorPresetValuesWindow.LeftClick += BtnEditorPresetValuesWindow_LeftClick;
btnEditorPresetValuesWindow.Disable();

tbName.TextChanged += TbName_TextChanged;
tbParameterValue.TextChanged += TbParameterValue_TextChanged;
lbScriptTypes.SelectedIndexChanged += LbScriptTypes_SelectedIndexChanged;
Expand All @@ -78,6 +84,10 @@ public override void Initialize()
var selectScriptActionDarkeningPanel = DarkeningPanel.InitializeAndAddToParentControlWithChild(WindowManager, Parent, selectScriptActionWindow);
selectScriptActionDarkeningPanel.Hidden += SelectScriptActionDarkeningPanel_Hidden;

selectScriptActionPresetOptionWindow = new SelectScriptActionPresetOptionWindow(WindowManager, map);
var selectScriptActionPresetDarkeningPanel = DarkeningPanel.InitializeAndAddToParentControlWithChild(WindowManager, Parent, selectScriptActionPresetOptionWindow);
selectScriptActionPresetDarkeningPanel.Hidden += SelectScriptActionPresetDarkeningPanel_Hidden;

selectBuildingTargetWindow = new SelectBuildingTargetWindow(WindowManager, map);
var buildingTargetWindowDarkeningPanel = DarkeningPanel.InitializeAndAddToParentControlWithChild(WindowManager, Parent, selectBuildingTargetWindow);
buildingTargetWindowDarkeningPanel.Hidden += BuildingTargetWindowDarkeningPanel_Hidden;
Expand Down Expand Up @@ -216,6 +226,18 @@ private void BtnEditorPresetValues_LeftClick(object sender, EventArgs e)
}
}

private void BtnEditorPresetValuesWindow_LeftClick(object sender, EventArgs e)
{
if (editedScript == null)
return;

if (lbActions.SelectedItem == null)
return;
Comment on lines +231 to +235
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (editedScript == null)
return;
if (lbActions.SelectedItem == null)
return;
if (editedScript == null || lbActions.SelectedItem == null)
return;


var item = selectScriptActionPresetOptionWindow.GetMatchingItem(tbParameterValue.Text);
selectScriptActionPresetOptionWindow.Open(item);
}

private void BtnAddScript_LeftClick(object sender, EventArgs e)
{
map.Scripts.Add(new Script(map.GetNewUniqueInternalId()) { Name = "New script" });
Expand Down Expand Up @@ -355,6 +377,18 @@ private void SelectScriptActionDarkeningPanel_Hidden(object sender, EventArgs e)
LbActions_SelectedIndexChanged(this, EventArgs.Empty);
}


private void SelectScriptActionPresetDarkeningPanel_Hidden(object sender, EventArgs e)
{
if (lbActions.SelectedItem == null || editedScript == null)
{
return;
}

if (selectScriptActionPresetOptionWindow.SelectedObject != null)
tbParameterValue.Text = selectScriptActionPresetOptionWindow.GetSelectedItemText();
}

private void LbActions_SelectedIndexChanged(object sender, EventArgs e)
{
if (lbActions.SelectedItem == null || editedScript == null)
Expand All @@ -378,7 +412,23 @@ private void LbActions_SelectedIndexChanged(object sender, EventArgs e)
lblParameterDescription.Text = action == null ? "Parameter:" : action.ParamDescription + ":";
lblActionDescriptionValue.Text = GetActionDescriptionFromIndex(entry.Action);

FillPresetContextMenu(entry, action);
string text = null;

if (action.UseWindowSelection && action.PresetOptions.Count > 0)
{
btnEditorPresetValues.Disable();
btnEditorPresetValuesWindow.Enable();
text = selectScriptActionPresetOptionWindow.FillPresetOptions(entry, action);
}
else
{
btnEditorPresetValues.Enable();
btnEditorPresetValuesWindow.Disable();
text = FillPresetContextMenu(entry, action);
}

if (text != null)
tbParameterValue.Text = text;
}

private void SetParameterEntryText(ScriptActionEntry scriptActionEntry, ScriptAction action)
Expand Down Expand Up @@ -431,7 +481,7 @@ private string GetBuildingWithPropertyText(int argument)
return GetBuildingWithPropertyText(index, property);
}

private void FillPresetContextMenu(ScriptActionEntry entry, ScriptAction action)
private string FillPresetContextMenu(ScriptActionEntry entry, ScriptAction action)
{
btnEditorPresetValues.ContextMenu.ClearItems();

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

var fittingItem = btnEditorPresetValues.ContextMenu.Items.Find(item => item.Text.StartsWith(entry.Argument.ToString()));
if (fittingItem != null)
tbParameterValue.Text = fittingItem.Text;
return fittingItem.Text;

return null;
}

private void LbScriptTypes_SelectedIndexChanged(object sender, EventArgs e) => RefreshSelectedScript();
Expand Down Expand Up @@ -532,9 +584,9 @@ private void EditScript(Script script)
for (int i = 0; i < editedScript.Actions.Count; i++)
{
var actionEntry = editedScript.Actions[i];
lbActions.AddItem(new XNAListBoxItem()
{
Text = GetActionEntryText(i, actionEntry),
lbActions.AddItem(new XNAListBoxItem()
{
Text = GetActionEntryText(i, actionEntry),
Tag = actionEntry
});
}
Expand All @@ -546,7 +598,7 @@ private string GetActionEntryText(int index, ScriptActionEntry entry)
{
ScriptAction action = GetScriptAction(entry.Action);
if (action == null)
return "#" + index + " - Unknown (" + entry.Argument.ToString(CultureInfo.InvariantCulture) + ")";
return "#" + index + " - Unknown (" + entry.Argument.ToString(CultureInfo.InvariantCulture) + ")";

return "#" + index + " - " + action.Name + " (" + entry.Argument.ToString(CultureInfo.InvariantCulture) + ")";
}
Expand Down
83 changes: 83 additions & 0 deletions src/TSMapEditor/UI/Windows/SelectScriptActionPresetOptionWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using Microsoft.Xna.Framework;
using Rampastring.XNAUI;
using Rampastring.XNAUI.XNAControls;
using System;
using System.Collections.Generic;
using TSMapEditor.CCEngine;
using TSMapEditor.Models;
using TSMapEditor.Models.Enums;

namespace TSMapEditor.UI.Windows
{
public class SelectScriptActionPresetOptionWindow : SelectObjectWindow<ScriptActionPresetOption>
{
public SelectScriptActionPresetOptionWindow(WindowManager windowManager, Map map) : base(windowManager)
{
this.map = map;
}

private Map map;
public List<ScriptActionPresetOption> presetOptions { get; } = new List<ScriptActionPresetOption>(0);

public override void Initialize()
{
Name = nameof(SelectScriptActionPresetOptionWindow);
base.Initialize();
}

protected override void LbObjectList_SelectedIndexChanged(object sender, EventArgs e)
{
if (lbObjectList.SelectedItem == null)
{
SelectedObject = null;
return;
}

SelectedObject = (ScriptActionPresetOption)lbObjectList.SelectedItem.Tag;
}

protected override void ListObjects()
{
lbObjectList.Clear();

foreach (ScriptActionPresetOption presetOption in presetOptions)
{
var item = new XNAListBoxItem() { Text = $"{presetOption.GetOptionText()}", Tag = presetOption };

if (presetOption.Color != null)
item.TextColor = (Color)presetOption.Color;
Comment on lines +47 to +48
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (presetOption.Color != null)
item.TextColor = (Color)presetOption.Color;
if (presetOption.Color.HasValue)
item.TextColor = presetOption.Color.Value;

Not really a complaint, I just prefer this syntax when working with nullable value types.


lbObjectList.AddItem(item);

if (presetOption == SelectedObject)
lbObjectList.SelectedIndex = lbObjectList.Items.Count - 1;
}
}

public string FillPresetOptions(ScriptActionEntry entry, ScriptAction action)
{
presetOptions.Clear();
presetOptions.AddRange(action.PresetOptions);

var fittingItem = presetOptions.Find(item => item.Text.StartsWith(entry.Argument.ToString()));

if (fittingItem != null)
return fittingItem.Text;

return null;
}

public ScriptActionPresetOption GetMatchingItem(string text)
{
return presetOptions.Find(item => item.GetOptionText().Equals(text, StringComparison.Ordinal));
}

public string GetSelectedItemText()
{
if (SelectedObject != null)
return SelectedObject.GetOptionText();

return string.Empty;
}
}
}
Loading