-
Notifications
You must be signed in to change notification settings - Fork 28
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
|
||
public ScriptActionPresetOption(int value, string text) | ||
public ScriptActionPresetOption() | ||
{ | ||
} | ||
Comment on lines
+15
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} | ||
|
||
|
@@ -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; | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Since this exists visually in the same space as |
||||||||||||||||||
HasCloseButton=true | ||||||||||||||||||
|
||||||||||||||||||
[lblWindowDescription] | ||||||||||||||||||
|
@@ -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 | ||||||||||||||||||
|
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 | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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; | ||||||||||||||||
|
@@ -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); | ||||||||||||||||
|
@@ -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; | ||||||||||||||||
|
@@ -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; | ||||||||||||||||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
|
||||||||||||||||
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" }); | ||||||||||||||||
|
@@ -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) | ||||||||||||||||
|
@@ -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) | ||||||||||||||||
|
@@ -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(); | ||||||||||||||||
|
||||||||||||||||
|
@@ -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(); | ||||||||||||||||
|
@@ -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 | ||||||||||||||||
}); | ||||||||||||||||
} | ||||||||||||||||
|
@@ -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) + ")"; | ||||||||||||||||
} | ||||||||||||||||
|
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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; | ||||||||||
} | ||||||||||
} | ||||||||||
} |
There was a problem hiding this comment.
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.