Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Circuit
- open System
- open System.Xml
- open UnityEngine
- open UnityEditor
- module SlotMode =
- type T
- = In
- | Out
- let In = T.In
- let Out = T.Out
- let GetName s =
- match s with
- | In -> "In"
- | Out -> "Out"
- let GetValue s =
- match s with
- | "In" -> In
- | _ -> Out
- type DataType = {
- Id : Guid
- Name : string
- Color : Color
- }
- and Slot = {
- Id : Guid
- TypeId : Guid
- Name : string
- Mode : SlotMode.T
- } with
- member x.AllowMultiple =
- false
- and NodeType = {
- Id : Guid
- Name : string
- Color : Color
- SlotsIn : Slot list
- SlotsOut : Slot list
- } with
- member x.AllowData =
- false
- member x.HasGuid (id:Guid) =
- let check (s:Slot) = s.Id = id
- x.Id = id ||
- x.SlotsIn |> List.exists check ||
- x.SlotsOut |> List.exists check
- member x.HasSlot (s:Slot) =
- let check (s':Slot) = s' = s
- match s.Mode with
- | SlotMode.In -> List.exists check x.SlotsIn
- | SlotMode.Out -> List.exists check x.SlotsOut
- and Node = {
- Id : Guid
- TypeId : Guid
- Position : Vector3
- SlotsIn : Slot list
- SlotsOut : Slot list
- } with
- member x.Enabled =
- true
- member x.HasGuid (id:Guid) =
- let check (s:Slot) = s.Id = id
- x.Id = id ||
- x.TypeId = id ||
- x.SlotsIn |> List.exists check ||
- x.SlotsOut |> List.exists check
- and LinkType = {
- Id : Guid
- Name : string
- Color : Color
- } with
- member x.AllowData =
- false
- and Link = {
- Id : Guid
- TypeId : Guid
- SourceId : Guid
- SourceSlotId : Guid
- TargetId : Guid
- TargetSlotId : Guid
- } with
- member x.Enabled =
- true
- member x.HasGuid (id:Guid) =
- x.Id = id ||
- x.TypeId = id ||
- x.SourceId = id ||
- x.SourceSlotId = id ||
- x.TargetId = id ||
- x.TargetSlotId = id
- and Context = {
- Id : Guid
- Name : string
- DataTypes : Map<Guid, DataType>
- NodeTypes : Map<Guid, NodeType>
- LinkTypes : Map<Guid, LinkType>
- } with
- member x.HasGuid (id:Guid) =
- x.Id = id ||
- x.DataTypes.ContainsKey id ||
- x.LinkTypes.ContainsKey id ||
- x.NodeTypes |> Map.exists (fun _ n -> n.HasGuid id)
- member x.DataTypesSeq = seq { for kvp in x.DataTypes do yield kvp.Value }
- member x.NodeTypesSeq = seq { for kvp in x.NodeTypes do yield kvp.Value }
- member x.LinkTypesSeq = seq { for kvp in x.LinkTypes do yield kvp.Value }
- member x.HasDataType (dt:DataType) = x.DataTypes.ContainsKey(dt.Id) && x.DataTypes.[dt.Id] = dt
- member x.HasNodeType (nt:NodeType) = x.NodeTypes.ContainsKey(nt.Id) && x.NodeTypes.[nt.Id] = nt
- member x.HasLinkType (lt:LinkType) = x.LinkTypes.ContainsKey(lt.Id) && x.LinkTypes.[lt.Id] = lt
- member x.FindDataType (id:Guid) = Map.find id x.DataTypes
- member x.FindNodeType (id:Guid) = Map.find id x.NodeTypes
- member x.FindLinkType (id:Guid) = Map.find id x.LinkTypes
- member x.FindDataType (name:string) = Map.pick (fun _ (dt:DataType) -> if dt.Name = name then Some dt else None) x.DataTypes
- member x.FindNodeType (name:string) = Map.pick (fun _ (nt:NodeType) -> if nt.Name = name then Some nt else None) x.NodeTypes
- member x.FindLinkType (name:string) = Map.pick (fun _ (lt:LinkType) -> if lt.Name = name then Some lt else None) x.LinkTypes
- member x.VerifyGuidIsAvailable (id:Guid) = if x.HasGuid id then failwith "GUID already in use"
- member x.VerifyHasDataType (dt:DataType) = if not (x.HasDataType dt) then failwith "DataType does not exist in context"
- member x.VerifyHasNodeType (nt:NodeType) = if not (x.HasNodeType nt) then failwith "NodeType does not exist in context"
- member x.VerifyHasLinkType (lt:LinkType) = if not (x.HasLinkType lt) then failwith "LinkType does not exist in context"
- and Canvas = {
- Id : Guid
- Context : Context
- Name : string
- Nodes : Map<Guid, Node>
- Links : Map<Guid, Link>
- } with
- member x.HasGuid (id:Guid) =
- x.Id = id ||
- x.Nodes |> Map.exists (fun _ n -> n.HasGuid id) ||
- x.Links |> Map.exists (fun _ l -> l.HasGuid id) ||
- x.Context.HasGuid id
- member x.NodesSeq = seq { for kvp in x.Nodes do yield kvp.Value }
- member x.LinksSeq = seq { for kvp in x.Links do yield kvp.Value }
- member x.HasNode (n:Node) = x.Nodes.ContainsKey n.Id && x.Nodes.[n.Id] = n
- member x.HasLink (l:Link) = x.Links.ContainsKey l.Id && x.Links.[l.Id] = l
- member x.VerifyGuidIsAvailable (id:Guid) = if x.HasGuid id then failwith "GUID already in use"
- member x.VerifyHasNode (n:Node) = if not (x.HasNode n) then failwith "Node does not exist in canvas"
- member x.VerifyHasLink (l:Link) = if not (x.HasLink l) then failwith "Link does not exist in canvas"
- member x.VerifyNodeHasSlot (n:Node) (s:Slot) = if not (x.NodeHasSlot n s) then failwith "Slot does not exist on node"
- member x.NodeHasLink (n:Node) (s:Slot) =
- x.VerifyHasNode n
- x.VerifyNodeHasSlot n s
- match s.Mode with
- | SlotMode.In -> Map.exists (fun k l -> l.TargetId = n.Id && l.TargetSlotId = s.Id) x.Links
- | SlotMode.Out -> Map.exists (fun k l -> l.SourceId = n.Id && l.SourceSlotId = s.Id) x.Links
- member x.NodeHasSlot (n:Node) (s:Slot) =
- x.VerifyHasNode n
- let nt = x.Context.FindNodeType n.TypeId
- match nt.HasSlot s, s.Mode with
- | true , _ -> true
- | false, SlotMode.In -> n.SlotsIn |> List.existsItem s
- | false, SlotMode.Out -> n.SlotsOut |> List.existsItem s
- [<AbstractClass>]
- type ContextProvider () =
- static let sync = obj()
- static let mutable contexts = Map.empty<Guid, Context>
- static member LoadContext (id:Guid) : Context =
- lock sync (fun () ->
- if contexts.IsEmpty then
- for typ in Reflection.GetAllSubClasses(typeof<ContextProvider>) do
- let obj = Activator.CreateInstance(typ) :?> ContextProvider
- let ctx = obj.CreateContext()
- contexts <- contexts.Add(ctx.Id, ctx)
- match contexts.TryFind id with
- | Some ctx -> ctx
- | None -> failwithf "Unknown context %A" id)
- abstract member CreateContext : unit -> Context
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement