75 lines
2.4 KiB
C#
75 lines
2.4 KiB
C#
using System;
|
|
using System.Reactive.Linq;
|
|
using DynamicData;
|
|
using Intromat.Model.Compiler;
|
|
using Intromat.PersistentModel;
|
|
using Intromat.ViewModels;
|
|
using Intromat.ViewModels.Editors;
|
|
using Intromat.ViewModels.Previews;
|
|
using Intromat.Views;
|
|
using NodeNetwork.Toolkit.ValueNode;
|
|
using ReactiveUI;
|
|
using Splat;
|
|
|
|
namespace Intromat.Nodes.Code
|
|
{
|
|
public class PrintNode : ExecutionNodeBase
|
|
{
|
|
static PrintNode()
|
|
{
|
|
Locator.CurrentMutable.Register(() => new CodeGenNodeView(), typeof(IViewFor<PrintNode>));
|
|
}
|
|
|
|
public PrintNode() : base(NodeType.Code)
|
|
{
|
|
Name = "Print";
|
|
|
|
Text = new CodeGenInputViewModel<ITypedExpression<string>>(EPortType.String)
|
|
{
|
|
Name = "Text",
|
|
Editor = TextEditor
|
|
};
|
|
Inputs.Add(Text);
|
|
|
|
FlowIn.Value = FlowOutChanged
|
|
.CombineLatest(Text.ValueChanged, (flowOutChanged, textValueChanged) => (FlowOutChanged: flowOutChanged, StringExpression: textValueChanged))
|
|
.Select(v => new FunctionCall
|
|
{
|
|
FunctionName = "print",
|
|
FlowOut = FlowOut.Value,
|
|
Parameters = { v.StringExpression ?? new StringLiteralValue { Value = "" } }
|
|
});
|
|
|
|
var stringPreview = new StringPreviewViewModel();
|
|
Preview = stringPreview;
|
|
|
|
this.WhenAnyValue(vm => vm.Text.Value)
|
|
.Where(v => v != null)
|
|
.Subscribe(_ => stringPreview.Value = Text.Value.PreviewValue ?? string.Empty);
|
|
}
|
|
|
|
public StringExpressionEditorViewModel TextEditor { get; } = new();
|
|
|
|
public ValueNodeInputViewModel<ITypedExpression<string>> Text { get; }
|
|
|
|
public override NodeModelBase CreateModel()
|
|
{
|
|
return new PrintModel();
|
|
}
|
|
|
|
public override void SaveModel(NodeModelBase model)
|
|
{
|
|
base.SaveModel(model);
|
|
var print = (PrintModel)model;
|
|
print.Text = TextEditor.Value is StringLiteralValue text ? text.Value : null;
|
|
}
|
|
|
|
public override void LoadModel(NodeModelBase model)
|
|
{
|
|
base.LoadModel(model);
|
|
var print = (PrintModel)model;
|
|
if (print.Text != null)
|
|
TextEditor.Value = new StringLiteralValue { Value = print.Text };
|
|
}
|
|
}
|
|
} |