From cdcb2f8f7a1b87c8076239965d89ccb9f0a4bccd Mon Sep 17 00:00:00 2001 From: Wojciech Figat Date: Thu, 18 Nov 2021 11:21:30 +0100 Subject: [PATCH] Fixes and improvements --- .../CustomEditors/CustomEditorPresenter.cs | 2 +- .../Editors/FlaxObjectRefEditor.cs | 4 ++-- .../Editor/CustomEditors/Editors/TypeEditor.cs | 6 +++--- .../Values/ReadOnlyValueContainer.cs | 2 +- .../SourceCodeEditing/CodeEditingModule.cs | 3 +-- Source/Editor/Scripting/ScriptType.cs | 5 +++++ Source/Editor/Scripting/TypeUtils.cs | 2 +- Source/Editor/Surface/Archetypes/Tools.cs | 4 ++-- Source/Editor/Surface/SurfaceNode.cs | 2 +- Source/Editor/Utilities/VariantUtils.cs | 11 ++++++++++- .../Animations/Graph/AnimGroup.Animation.cpp | 10 ++-------- .../MaterialGenerator.Material.cpp | 9 +-------- Source/Engine/Visject/ShaderGraph.h | 2 +- Source/Engine/Visject/VisjectGraph.cpp | 15 --------------- Source/Engine/Visject/VisjectGraph.h | 17 +++++++++++++++-- 15 files changed, 46 insertions(+), 48 deletions(-) diff --git a/Source/Editor/CustomEditors/CustomEditorPresenter.cs b/Source/Editor/CustomEditors/CustomEditorPresenter.cs index de2291dea..27ec01025 100644 --- a/Source/Editor/CustomEditors/CustomEditorPresenter.cs +++ b/Source/Editor/CustomEditors/CustomEditorPresenter.cs @@ -143,7 +143,7 @@ namespace FlaxEditor.CustomEditors } else { - var type = new ScriptType(typeof(object)); + var type = ScriptType.Object; if (selection.HasDifferentTypes == false) type = TypeUtils.GetObjectType(selection[0]); Editor = CustomEditorsUtil.CreateEditor(type, false); diff --git a/Source/Editor/CustomEditors/Editors/FlaxObjectRefEditor.cs b/Source/Editor/CustomEditors/Editors/FlaxObjectRefEditor.cs index bad441d20..c6b3fbe64 100644 --- a/Source/Editor/CustomEditors/Editors/FlaxObjectRefEditor.cs +++ b/Source/Editor/CustomEditors/Editors/FlaxObjectRefEditor.cs @@ -47,7 +47,7 @@ namespace FlaxEditor.CustomEditors.Editors { if (_type == value) return; - if (value == ScriptType.Null || (value.Type != typeof(Object) && !value.IsSubclassOf(new ScriptType(typeof(Object))))) + if (value == ScriptType.Null || (value.Type != typeof(Object) && !value.IsSubclassOf(ScriptType.Object))) throw new ArgumentException(string.Format("Invalid type for FlaxObjectRefEditor. Input type: {0}", value != ScriptType.Null ? value.TypeName : "null")); _type = value; @@ -132,7 +132,7 @@ namespace FlaxEditor.CustomEditors.Editors public FlaxObjectRefPickerControl() : base(0, 0, 50, 16) { - _type = new ScriptType(typeof(Object)); + _type = ScriptType.Object; } private bool IsValid(Object obj) diff --git a/Source/Editor/CustomEditors/Editors/TypeEditor.cs b/Source/Editor/CustomEditors/Editors/TypeEditor.cs index 8d32d6c3e..0c8a22007 100644 --- a/Source/Editor/CustomEditors/Editors/TypeEditor.cs +++ b/Source/Editor/CustomEditors/Editors/TypeEditor.cs @@ -119,7 +119,7 @@ namespace FlaxEditor.CustomEditors.Editors public TypePickerControl() : base(0, 0, 50, 16) { - _type = new ScriptType(typeof(object)); + _type = ScriptType.Object; } private bool IsValid(ScriptType obj) @@ -400,9 +400,9 @@ namespace FlaxEditor.CustomEditors.Editors { _element.CustomControl.ValueChanged += () => SetValue(_element.CustomControl.Value.Type); - if (_element.CustomControl.Type == new ScriptType(typeof(object))) + if (_element.CustomControl.Type == ScriptType.Object) { - _element.CustomControl.Type = Values.Type.Type != typeof(object) || Values[0] == null ? new ScriptType(typeof(object)) : TypeUtils.GetObjectType(Values[0]); + _element.CustomControl.Type = Values.Type.Type != typeof(object) || Values[0] == null ? ScriptType.Object : TypeUtils.GetObjectType(Values[0]); } } } diff --git a/Source/Editor/CustomEditors/Values/ReadOnlyValueContainer.cs b/Source/Editor/CustomEditors/Values/ReadOnlyValueContainer.cs index c87c0d446..218532343 100644 --- a/Source/Editor/CustomEditors/Values/ReadOnlyValueContainer.cs +++ b/Source/Editor/CustomEditors/Values/ReadOnlyValueContainer.cs @@ -17,7 +17,7 @@ namespace FlaxEditor.CustomEditors /// /// The initial value. public ReadOnlyValueContainer(object value) - : base(ScriptMemberInfo.Null, new ScriptType(typeof(object))) + : base(ScriptMemberInfo.Null, ScriptType.Object) { Add(value); } diff --git a/Source/Editor/Modules/SourceCodeEditing/CodeEditingModule.cs b/Source/Editor/Modules/SourceCodeEditing/CodeEditingModule.cs index 30f1a64a1..bcc6bcae3 100644 --- a/Source/Editor/Modules/SourceCodeEditing/CodeEditingModule.cs +++ b/Source/Editor/Modules/SourceCodeEditing/CodeEditingModule.cs @@ -8,7 +8,6 @@ using FlaxEditor.Options; using FlaxEditor.Scripting; using FlaxEngine; using FlaxEngine.GUI; -using Object = System.Object; namespace FlaxEditor.Modules.SourceCodeEditing { @@ -21,7 +20,7 @@ namespace FlaxEditor.Modules.SourceCodeEditing private sealed class CachedVisualScriptPropertyTypesCollection : CachedTypesCollection { public CachedVisualScriptPropertyTypesCollection(int capacity, Func checkAssembly) - : base(capacity, new ScriptType(typeof(Object)), CheckFunc, checkAssembly) + : base(capacity, ScriptType.Object, CheckFunc, checkAssembly) { } diff --git a/Source/Editor/Scripting/ScriptType.cs b/Source/Editor/Scripting/ScriptType.cs index 10956e9ba..f339c6f10 100644 --- a/Source/Editor/Scripting/ScriptType.cs +++ b/Source/Editor/Scripting/ScriptType.cs @@ -698,6 +698,11 @@ namespace FlaxEditor.Scripting /// public static readonly ScriptType Void = new ScriptType(typeof(void)); + /// + /// A that is System.Object. + /// + public static readonly ScriptType Object = new ScriptType(typeof(object)); + /// /// Gets the type of the script as . /// diff --git a/Source/Editor/Scripting/TypeUtils.cs b/Source/Editor/Scripting/TypeUtils.cs index aaa0cc3db..f197ab61c 100644 --- a/Source/Editor/Scripting/TypeUtils.cs +++ b/Source/Editor/Scripting/TypeUtils.cs @@ -64,7 +64,7 @@ namespace FlaxEditor.Scripting Utilities.Utils.InitDefaultValues(value); return value; } - if (new ScriptType(typeof(object)).IsAssignableFrom(type)) + if (ScriptType.Object.IsAssignableFrom(type)) return null; if (type.CanCreateInstance) { diff --git a/Source/Editor/Surface/Archetypes/Tools.cs b/Source/Editor/Surface/Archetypes/Tools.cs index 143a0557d..596a7999c 100644 --- a/Source/Editor/Surface/Archetypes/Tools.cs +++ b/Source/Editor/Surface/Archetypes/Tools.cs @@ -855,7 +855,7 @@ namespace FlaxEditor.Surface.Archetypes { _picker = new TypePickerControl { - Type = new ScriptType(typeof(object)), + Type = ScriptType.Object, Bounds = new Rectangle(FlaxEditor.Surface.Constants.NodeMarginX, FlaxEditor.Surface.Constants.NodeMarginY + FlaxEditor.Surface.Constants.NodeHeaderSize, 140, 16), Parent = this, }; @@ -1515,7 +1515,7 @@ namespace FlaxEditor.Surface.Archetypes { TypeID = 26, Title = "Cast Value", - Create = (id, context, arch, groupArch) => new CastNode(id, context, arch, groupArch, new ScriptType(typeof(object))), + Create = (id, context, arch, groupArch) => new CastNode(id, context, arch, groupArch, ScriptType.Object), Description = "Tries to cast the object to a given type. Returns null if fails.", Flags = NodeFlags.VisualScriptGraph, Size = new Vector2(200, 60), diff --git a/Source/Editor/Surface/SurfaceNode.cs b/Source/Editor/Surface/SurfaceNode.cs index a3e1e6ddf..aa4fb4eb7 100644 --- a/Source/Editor/Surface/SurfaceNode.cs +++ b/Source/Editor/Surface/SurfaceNode.cs @@ -317,7 +317,7 @@ namespace FlaxEditor.Surface public Box AddBox(bool isOut, int id, int yLevel, string text, ScriptType type, bool single, int valueIndex = -1) { if (type == ScriptType.Null) - type = new ScriptType(typeof(object)); + type = ScriptType.Object; // Try to reuse box var box = GetBox(id); diff --git a/Source/Editor/Utilities/VariantUtils.cs b/Source/Editor/Utilities/VariantUtils.cs index 7e92bb849..ac793aa57 100644 --- a/Source/Editor/Utilities/VariantUtils.cs +++ b/Source/Editor/Utilities/VariantUtils.cs @@ -288,7 +288,7 @@ namespace FlaxEditor.Utilities case VariantType.Matrix: return new ScriptType(typeof(Matrix)); case VariantType.Array: return new ScriptType(typeof(object[])); case VariantType.Dictionary: return new ScriptType(typeof(Dictionary)); - case VariantType.ManagedObject: return new ScriptType(typeof(object)); + case VariantType.ManagedObject: return ScriptType.Object; case VariantType.Blob: return new ScriptType(typeof(byte[])); default: throw new ArgumentOutOfRangeException($"Unknown Variant Type {variantType} without typename."); } @@ -541,6 +541,15 @@ namespace FlaxEditor.Utilities stream.Write(int.MaxValue); stream.WriteStrAnsi(type.FullName, 77); break; + case VariantType.Array: + if (type != typeof(object[])) + { + stream.Write(int.MaxValue); + stream.WriteStrAnsi(type.FullName, 77); + } + else + stream.Write(0); + break; default: stream.Write(0); break; diff --git a/Source/Engine/Animations/Graph/AnimGroup.Animation.cpp b/Source/Engine/Animations/Graph/AnimGroup.Animation.cpp index 5b1f1a401..f12f5570e 100644 --- a/Source/Engine/Animations/Graph/AnimGroup.Animation.cpp +++ b/Source/Engine/Animations/Graph/AnimGroup.Animation.cpp @@ -587,13 +587,7 @@ void AnimGraphExecutor::ProcessGroupAnimation(Box* boxBase, Node* nodeBase, Valu { // Animation Output case 1: - { - if (box->HasConnection()) - value = eatBox(nodeBase, box->FirstConnection()); - else - value = Value::Null; - break; - } + value = tryGetValue(box, Value::Null); // Animation case 2: { @@ -1637,7 +1631,7 @@ void AnimGraphExecutor::ProcessGroupAnimation(Box* boxBase, Node* nodeBase, Valu // Evaluate the function output context.GraphStack.Push((Graph*)data.Graph); - value = functionOutputBox && functionOutputBox->HasConnection() ? eatBox(nodeBase, functionOutputBox->FirstConnection()) : Value::Zero; + value = tryGetValue(functionOutputBox, Value::Zero); context.GraphStack.Pop(); break; } diff --git a/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp b/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp index c506034fe..ad3026764 100644 --- a/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp +++ b/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp @@ -56,10 +56,8 @@ void MaterialGenerator::ProcessGroupMaterial(Box* box, Node* node, Value& value) } // Screen Size case 7: - { value = Value(VariantType::Vector2, box->ID == 0 ? TEXT("ScreenSize.xy") : TEXT("ScreenSize.zw")); break; - } // Custom code case 8: { @@ -331,7 +329,7 @@ void MaterialGenerator::ProcessGroupMaterial(Box* box, Node* node, Value& value) // Evaluate the function output _graphStack.Push(graph); - value = functionOutputBox && functionOutputBox->HasConnection() ? eatBox(node, functionOutputBox->FirstConnection()) : Value::Zero; + value = tryGetValue(functionOutputBox, Value::Zero); _graphStack.Pop(); break; } @@ -400,7 +398,6 @@ void MaterialGenerator::ProcessGroupMaterial(Box* box, Node* node, Value& value) case 30: { const auto inValue = tryGetValue(node->GetBox(0), 0, Value::Zero); - value = writeLocal(inValue.Type, String::Format(TEXT("ddx({0})"), inValue.Value), node); break; } @@ -408,7 +405,6 @@ void MaterialGenerator::ProcessGroupMaterial(Box* box, Node* node, Value& value) case 31: { const auto inValue = tryGetValue(node->GetBox(0), 0, Value::Zero); - value = writeLocal(inValue.Type, String::Format(TEXT("ddy({0})"), inValue.Value), node); break; } @@ -416,7 +412,6 @@ void MaterialGenerator::ProcessGroupMaterial(Box* box, Node* node, Value& value) case 32: { const auto inValue = tryGetValue(node->GetBox(0), 0, Value::Zero); - value = writeLocal(ValueType::Float, String::Format(TEXT("sign({0})"), inValue.Value), node); break; } @@ -424,7 +419,6 @@ void MaterialGenerator::ProcessGroupMaterial(Box* box, Node* node, Value& value) case 33: { const auto inValue = tryGetValue(node->GetBox(0), 0, Value::Zero); - value = writeLocal(ValueType::Bool, String::Format(TEXT("any({0})"), inValue.Value), node); break; } @@ -432,7 +426,6 @@ void MaterialGenerator::ProcessGroupMaterial(Box* box, Node* node, Value& value) case 34: { const auto inValue = tryGetValue(node->GetBox(0), 0, Value::Zero); - value = writeLocal(ValueType::Bool, String::Format(TEXT("all({0})"), inValue.Value), node); break; } diff --git a/Source/Engine/Visject/ShaderGraph.h b/Source/Engine/Visject/ShaderGraph.h index f73be6506..dee42e988 100644 --- a/Source/Engine/Visject/ShaderGraph.h +++ b/Source/Engine/Visject/ShaderGraph.h @@ -48,7 +48,7 @@ public: public: - ShaderGraphBox* FirstConnection() const + FORCE_INLINE ShaderGraphBox* FirstConnection() const { return (ShaderGraphBox*)Connections[0]; } diff --git a/Source/Engine/Visject/VisjectGraph.cpp b/Source/Engine/Visject/VisjectGraph.cpp index 63a262d11..b0093fc7a 100644 --- a/Source/Engine/Visject/VisjectGraph.cpp +++ b/Source/Engine/Visject/VisjectGraph.cpp @@ -1323,18 +1323,3 @@ void VisjectExecutor::ProcessGroupCollections(Box* box, Node* node, Value& value } } } - -VisjectExecutor::Value VisjectExecutor::tryGetValue(Box* box, int32 defaultValueBoxIndex, const Value& defaultValue) -{ - const auto parentNode = box->GetParent(); - if (box->HasConnection()) - return eatBox(parentNode, box->FirstConnection()); - if (parentNode->Values.Count() > defaultValueBoxIndex) - return Value(parentNode->Values[defaultValueBoxIndex]); - return defaultValue; -} - -VisjectExecutor::Value VisjectExecutor::tryGetValue(Box* box, const Value& defaultValue) -{ - return box && box->HasConnection() ? eatBox(box->GetParent(), box->FirstConnection()) : defaultValue; -} diff --git a/Source/Engine/Visject/VisjectGraph.h b/Source/Engine/Visject/VisjectGraph.h index 4cbe47b2e..e03807a2e 100644 --- a/Source/Engine/Visject/VisjectGraph.h +++ b/Source/Engine/Visject/VisjectGraph.h @@ -262,6 +262,19 @@ protected: virtual Value eatBox(Node* caller, Box* box) = 0; virtual Graph* GetCurrentGraph() const = 0; - Value tryGetValue(Box* box, int32 defaultValueBoxIndex, const Value& defaultValue); - Value tryGetValue(Box* box, const Value& defaultValue); + + FORCE_INLINE Value tryGetValue(Box* box, int32 defaultValueBoxIndex, const Value& defaultValue) + { + const auto parentNode = box->GetParent(); + if (box->HasConnection()) + return eatBox(parentNode, box->FirstConnection()); + if (parentNode->Values.Count() > defaultValueBoxIndex) + return Value(parentNode->Values[defaultValueBoxIndex]); + return defaultValue; + } + + FORCE_INLINE Value tryGetValue(Box* box, const Value& defaultValue) + { + return box && box->HasConnection() ? eatBox(box->GetParent(), box->FirstConnection()) : defaultValue; + } };