103 lines
3.0 KiB
ObjectPascal
103 lines
3.0 KiB
ObjectPascal
unit Myc.Ast.ViewModel;
|
|
|
|
interface
|
|
|
|
uses
|
|
System.SysUtils,
|
|
System.Types,
|
|
System.Generics.Collections,
|
|
Myc.Ast.Nodes;
|
|
|
|
type
|
|
// An enum to identify the specific type of an AST node without relying on RTTI or GUIDs.
|
|
// This allows the renderer to depend only on the ViewModel layer.
|
|
TAstNodeType = (
|
|
antUndefined,
|
|
antConstant,
|
|
antIdentifier,
|
|
antBinaryExpression,
|
|
antUnaryExpression,
|
|
antIfExpression,
|
|
antTernaryExpression,
|
|
antLambdaExpression,
|
|
antFunctionCall,
|
|
antBlockExpression,
|
|
antVariableDeclaration,
|
|
antAssignment,
|
|
antIndexer,
|
|
antMemberAccess,
|
|
antCreateSeries,
|
|
antAddSeriesItem,
|
|
antSeriesLength
|
|
);
|
|
|
|
// A unique, stable identifier for a visual node instance.
|
|
TViewModelID = NativeInt;
|
|
|
|
// Defines how a view model displays its children.
|
|
TVisualizationMode = (vmSyntactic, vmSemantic);
|
|
|
|
// Metadata tied to a logical IAstNode. All visual instances of this node will share this data.
|
|
TLogicalMetadata = record
|
|
// The concrete type of the IAstNode, used for type-safe operations in the renderer.
|
|
NodeType: TAstNodeType;
|
|
Description: String;
|
|
end;
|
|
|
|
// Metadata tied to a specific visual instance of a node, identified by its TViewModelID.
|
|
TVisualInstanceMetadata = record
|
|
PositionOverride: TPointF;
|
|
IsCollapsed: Boolean;
|
|
VisualizationMode: TVisualizationMode;
|
|
end;
|
|
|
|
// The ViewModel for a node in the visual graph.
|
|
TVisualNodeViewModel = class
|
|
private
|
|
FId: TViewModelID;
|
|
FNode: IAstNode;
|
|
FLogicalMetadata: TLogicalMetadata;
|
|
FParents: TList<TVisualNodeViewModel>;
|
|
FChildren: TList<TVisualNodeViewModel>;
|
|
public
|
|
constructor Create(const ANode: IAstNode; const AId: TViewModelID; const ALogicalMetadata: TLogicalMetadata);
|
|
destructor Destroy; override;
|
|
|
|
procedure AddChild(const AChild: TVisualNodeViewModel);
|
|
|
|
property Id: TViewModelID read FId;
|
|
property Node: IAstNode read FNode;
|
|
property LogicalMetadata: TLogicalMetadata read FLogicalMetadata;
|
|
property Children: TList<TVisualNodeViewModel> read FChildren;
|
|
property Parents: TList<TVisualNodeViewModel> read FParents;
|
|
end;
|
|
|
|
implementation
|
|
|
|
{ TVisualNodeViewModel }
|
|
|
|
constructor TVisualNodeViewModel.Create(const ANode: IAstNode; const AId: TViewModelID; const ALogicalMetadata: TLogicalMetadata);
|
|
begin
|
|
inherited Create;
|
|
FNode := ANode;
|
|
FId := AId;
|
|
FLogicalMetadata := ALogicalMetadata;
|
|
FParents := TList<TVisualNodeViewModel>.Create;
|
|
FChildren := TList<TVisualNodeViewModel>.Create;
|
|
end;
|
|
|
|
destructor TVisualNodeViewModel.Destroy;
|
|
begin
|
|
FParents.Free;
|
|
FChildren.Free;
|
|
inherited;
|
|
end;
|
|
|
|
procedure TVisualNodeViewModel.AddChild(const AChild: TVisualNodeViewModel);
|
|
begin
|
|
FChildren.Add(AChild);
|
|
AChild.FParents.Add(Self);
|
|
end;
|
|
|
|
end.
|