Skip to content

Commit

Permalink
Merge pull request #2540 from ivan-mogilko/362--globalvarsarray
Browse files Browse the repository at this point in the history
Editor: support arrays on Global Variables panel
  • Loading branch information
ivan-mogilko authored Oct 7, 2024
2 parents af9a08a + 62b6f24 commit 93b50b6
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 28 deletions.
25 changes: 23 additions & 2 deletions Editor/AGS.Editor/Components/GlobalVariablesComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,17 @@ private string GenerateGlobalVariablesScriptModule(IList<GlobalVariable> variabl

foreach (GlobalVariable variable in variables)
{
string declaration = variable.Type + " " + variable.Name;
string declaration = $"{variable.Type} {variable.Name}";
switch (variable.ArrayType)
{
case VariableArrayType.Array:
declaration = declaration + $"[{variable.ArraySize}]";
break;
case VariableArrayType.DynamicArray:
declaration = declaration + $"[]";
break;
}

if (((variable.Type == "int") ||
(variable.Type == "bool") ||
(variable.Type == "float")) &&
Expand Down Expand Up @@ -91,7 +101,18 @@ private void UpdateScriptHeader()
StringBuilder sb = new StringBuilder();
foreach (GlobalVariable variable in _agsEditor.CurrentGame.GlobalVariables.ToList())
{
sb.AppendLine("import " + variable.Type + " " + variable.Name + ";");
switch (variable.ArrayType)
{
case VariableArrayType.None:
sb.AppendLine($"import {variable.Type} {variable.Name};");
break;
case VariableArrayType.Array:
sb.AppendLine($"import {variable.Type} {variable.Name}[{variable.ArraySize}];");
break;
case VariableArrayType.DynamicArray:
sb.AppendLine($"import {variable.Type} {variable.Name}[];");
break;
}
}
_scriptHeader.Text = sb.ToString();
AutoComplete.ConstructCache(_scriptHeader, null);
Expand Down
111 changes: 92 additions & 19 deletions Editor/AGS.Editor/GUI/GlobalVariableDialog.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions Editor/AGS.Editor/GUI/GlobalVariableDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public GlobalVariableDialog(GlobalVariable variable, Game game, IEnumerable<APIT
}
}

cmbArray.SelectedIndex = (int)variable.ArrayType;
udArraySize.Value = MathExtra.Clamp(variable.ArraySize, 1, Int32.MaxValue);

Utilities.CheckLabelWidthsOnForm(this);
}

Expand Down Expand Up @@ -91,6 +94,8 @@ private void btnOK_Click(object sender, EventArgs e)
_variable.Name = txtName.Text;
_variable.DefaultValue = txtDefaultValue.Text;
_variable.Type = cmbType.SelectedItem.ToString();
_variable.ArrayType = (VariableArrayType)cmbArray.SelectedIndex;
_variable.ArraySize = _variable.ArrayType == VariableArrayType.Array ? (int)udArraySize.Value : 0;
this.DialogResult = DialogResult.OK;
this.Close();
}
Expand All @@ -108,6 +113,25 @@ private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
}
}

private void cmbArray_SelectedIndexChanged(object sender, EventArgs e)
{
VariableArrayType arrType = (VariableArrayType)cmbArray.SelectedIndex;
if (arrType == VariableArrayType.None)
{
label5.Enabled = false;
udArraySize.Enabled = false;
txtDefaultValue.Enabled = ((GlobalVariableType)cmbType.SelectedItem).CanHaveDefaultValue;
}
else
{
label5.Enabled = arrType == VariableArrayType.Array;
udArraySize.Enabled = arrType == VariableArrayType.Array;
// TODO: maybe support array initialization? although it's not convenient to do with a single textbox
txtDefaultValue.Enabled = false;
txtDefaultValue.Text = string.Empty;
}
}

private class GlobalVariableType
{
public string TypeName;
Expand Down
4 changes: 2 additions & 2 deletions Editor/AGS.Editor/GUI/GlobalVariableDialog.resx
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="label2.Text" xml:space="preserve">
<value>Choose the type of the variable here. This depends on what sort of information you want to store in the variable. If you just want to store a number, choose "int". To store a string of text, choose "String".</value>
Expand Down
24 changes: 19 additions & 5 deletions Editor/AGS.Editor/Panes/GlobalVariablesEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,27 @@ private void PopulateScriptTypeList()

private ListViewItem CreateListItemFromVariable(GlobalVariable variable)
{
ListViewItem newItem = new ListViewItem(new string[] { variable.Name, variable.Type, variable.DefaultValue });
newItem.Tag = variable;
ListViewItem newItem = new ListViewItem(new string[] { string.Empty, string.Empty, string.Empty });
FillListItemFromVariable(newItem, variable);
return newItem;
}

private void FillListItemFromVariable(ListViewItem item, GlobalVariable variable)
{
string varType;
switch (variable.ArrayType)
{
case VariableArrayType.Array: varType = $"{variable.Type}[{variable.ArraySize}]"; break;
case VariableArrayType.DynamicArray: varType = $"{variable.Type}[]"; break;
default: varType = variable.Type; break;
}

item.SubItems[0].Text = variable.Name;
item.SubItems[1].Text = varType;
item.SubItems[2].Text = variable.DefaultValue;
item.Tag = variable;
}

private void UpdateListItemFromVariableObject(ListViewItem listItem)
{
GlobalVariable selectedWord = ((GlobalVariable)listItem.Tag);
Expand Down Expand Up @@ -169,9 +185,7 @@ private void EditSelectedVariable(GlobalVariable variable)
_variables.VariableRenamed(variable, nameWas);
}

lvwWords.SelectedItems[0].SubItems[0].Text = variable.Name;
lvwWords.SelectedItems[0].SubItems[1].Text = variable.Type;
lvwWords.SelectedItems[0].SubItems[2].Text = variable.DefaultValue;
FillListItemFromVariable(lvwWords.SelectedItems[0], variable);
OnGlobalVariableChanged();
}
}
Expand Down
Loading

0 comments on commit 93b50b6

Please sign in to comment.