-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Set Local Variable block
- Loading branch information
1 parent
fc9678b
commit d8763a2
Showing
11 changed files
with
208 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<XmlLayout> | ||
<Styles> | ||
<Style id="set-local-variable" color="Instruction" format="set local variable |var| to (0)" tooltip="Creates a local variable with the given value." /> | ||
</Styles> | ||
<Categories> | ||
<Category name="Variables"> | ||
<SetLocalVariable style="set-local-variable"> | ||
<Constant number="0" /> | ||
</SetLocalVariable> | ||
</Category> | ||
</Categories> | ||
</XmlLayout> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using HarmonyLib; | ||
using ModApi.Craft.Program; | ||
using ModApi.Craft.Program.Instructions; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Assets.Scripts.Blocks | ||
{ | ||
[Serializable] | ||
public class SetLocalVariableInstruction : ProgramInstruction | ||
{ | ||
[ProgramNodeProperty] | ||
private string _variable = "var"; | ||
|
||
public string VariableName | ||
{ | ||
get => _variable; | ||
set => _variable = value; | ||
} | ||
|
||
public override ProgramInstruction Execute(IThreadContext context) | ||
{ | ||
ExpressionResult variableValue = GetExpression(0).Evaluate(context); | ||
context.CreateLocalVariable(VariableName).Value.Set(variableValue); | ||
|
||
return base.Execute(context); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Assets.Scripts.Blocks; | ||
using Assets.Scripts.Vizzy.UI; | ||
using Assets.Scripts.Vizzy.UI.Elements; | ||
using HarmonyLib; | ||
using ModApi.Craft.Program; | ||
using ModApi.Ui; | ||
using UnityEngine; | ||
|
||
namespace Assets.Scripts.Patches | ||
{ | ||
[HarmonyPatch(typeof(VariableElementScript))] | ||
internal static class VariableElementScriptPatches | ||
{ | ||
[HarmonyPostfix] | ||
[HarmonyPatch("OnPointerClick")] | ||
private static void OnPointerClick(VariableElementScript __instance) | ||
{ | ||
if (__instance.Parent?.Node is SetLocalVariableInstruction setLocalVarInstruction) | ||
{ | ||
VariableExpression variableExpression = AccessTools.Field(typeof(VariableElementScript), "_expression").GetValue(__instance) as VariableExpression; | ||
InputDialogScript variableNameInputDialog = VizzyUIController.CreateVariableNameInputDialog(variableExpression.VariableName); | ||
variableNameInputDialog.MessageText = "RENAME LOCAL VARIABLE"; | ||
variableNameInputDialog.OkayClicked += dialog => | ||
{ | ||
if (string.IsNullOrWhiteSpace(dialog.InputText)) | ||
return; | ||
|
||
string oldName = variableExpression.VariableName; | ||
variableExpression.VariableName = dialog.InputText; | ||
setLocalVarInstruction.VariableName = dialog.InputText; | ||
|
||
AccessTools.Method(typeof(VariableElementScript), "RenameLocalVariableInScope").Invoke(__instance, new object[] { setLocalVarInstruction, oldName, setLocalVarInstruction.VariableName }); | ||
|
||
foreach (VariableElementScript varScript in __instance.VizzyUI.ProgramTransform.GetComponentsInChildren<VariableElementScript>()) | ||
{ | ||
varScript.Parent?.LayoutElement(); | ||
} | ||
|
||
dialog.Close(); | ||
}; | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Scripts/Patches/VariableElementScriptPatches.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using Assets.Scripts.Vizzy.UI; | ||
using HarmonyLib; | ||
using ModApi.Common.Extensions; | ||
using System; | ||
using System.Linq; | ||
using System.Xml.Linq; | ||
using UnityEngine; | ||
|
||
namespace Assets.Scripts.Patches | ||
{ | ||
[HarmonyPatch(typeof(VizzyToolbox))] | ||
internal static class VizzyToolboxPatches | ||
{ | ||
[HarmonyPrefix] | ||
[HarmonyPatch(MethodType.Constructor)] | ||
[HarmonyPatch(new Type[] { typeof(XElement), typeof(bool) })] | ||
private static void OnConstructor(XElement xml) | ||
{ | ||
XElement blocksXml = XElement.Parse(Game.Instance.UserInterface.ResourceDatabase.GetResource<TextAsset>("Vizzy Studio/Blocks").text); | ||
XElement toolboxStyles = xml.Element("Styles"); | ||
foreach (XElement style in blocksXml.Element("Styles").Elements()) | ||
{ | ||
toolboxStyles.Add(style); | ||
} | ||
|
||
XElement toolboxCategories = xml.Element("Categories"); | ||
foreach (XElement category in blocksXml.Element("Categories").Elements()) | ||
{ | ||
XElement toolboxCategory = toolboxCategories.Elements().FirstOrDefault(e => e.GetStringAttribute("name") == category.GetStringAttribute("name")); | ||
if (toolboxCategory != null) | ||
{ | ||
foreach (XElement block in category.Elements()) | ||
{ | ||
//#TODO: Setting as first is specific to "SetLocalVariable". Add an attribute to specify position | ||
toolboxCategory.AddFirst(block); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.