Kaynağa Gözat

Added option to text slice node for not using a length

CPKreuz 2 hafta önce
ebeveyn
işleme
78e606f561

+ 13 - 4
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/Text/SliceTextNode.cs

@@ -7,6 +7,8 @@ public class SliceTextNode : Node
 {
     public OutputProperty<string> SlicedText { get; }
     
+    public InputProperty<bool> UseLength { get; }
+    
     public InputProperty<string> Text { get; }
     
     public InputProperty<int> Index { get; }
@@ -17,6 +19,7 @@ public class SliceTextNode : Node
     {
         SlicedText = CreateOutput("SlicedText", "TEXT", string.Empty);
         Text = CreateInput("Text", "TEXT", string.Empty);
+        UseLength = CreateInput("UseLength", "TEXT_SLICE_USE_LENGTH", true);
         
         Index = CreateInput("Index", "INDEX_START_AT", 0)
             .WithRules(x => x.Min(0));
@@ -27,12 +30,18 @@ public class SliceTextNode : Node
 
     protected override void OnExecute(RenderContext context)
     {
-        var textLength = Text.Value.Length;
+        var text = Text.Value;
+        var startIndex = Math.Clamp(Index.Value, 0, text.Length);
+
+        if (!UseLength.Value)
+        {
+            SlicedText.Value = text.Substring(startIndex);
+            return;
+        }
 
-        var startIndex = Math.Clamp(Index.Value, 0, textLength);
-        var length = Math.Clamp(Length.Value, 0, textLength - startIndex);
+        var length = Math.Clamp(Length.Value, 0, text.Length - startIndex);
         
-        SlicedText.Value = Text.Value.Substring(startIndex, length);
+        SlicedText.Value = text.Substring(startIndex, length);
     }
 
     public override Node CreateCopy() =>

+ 2 - 1
src/PixiEditor/Data/Localization/Languages/en.json

@@ -1126,5 +1126,6 @@
   "SEARCH_TEXT": "Search text",
   "TEXT_INFO_NODE": "Text info",
   "TEXT_LENGTH": "Length",
-  "TEXT_LINE_COUNT": "Line count"
+  "TEXT_LINE_COUNT": "Line count",
+  "TEXT_SLICE_USE_LENGTH": "Use length"
 }

+ 18 - 1
src/PixiEditor/ViewModels/Document/Nodes/Text/SliceTextNodeViewModel.cs

@@ -1,7 +1,24 @@
 using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes.Text;
+using PixiEditor.Models.Events;
+using PixiEditor.Models.Handlers;
 using PixiEditor.ViewModels.Nodes;
 
 namespace PixiEditor.ViewModels.Document.Nodes.Text;
 
 [NodeViewModel("SLICE_TEXT_NODE", "SHAPE", null)]
-internal class SliceTextNodeViewModel : NodeViewModel<SliceTextNode>;
+internal class SliceTextNodeViewModel : NodeViewModel<SliceTextNode>
+{
+    private NodePropertyViewModel<bool> _useLengthProperty;
+    private NodePropertyViewModel _lengthProperty;
+    
+    public override void OnInitialized()
+    {
+        _useLengthProperty = FindInputProperty<bool>("UseLength");
+        _lengthProperty = FindInputProperty("Length");
+        
+        _useLengthProperty.ValueChanged += UseLengthValueChanged;
+    }
+
+    private void UseLengthValueChanged(INodePropertyHandler property, NodePropertyValueChangedArgs args) =>
+        _lengthProperty.IsVisible = _useLengthProperty.Value;
+}