Advertisement
Guest User

Untitled

a guest
Feb 27th, 2014
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 5.99 KB | None | 0 0
  1. namespace Circuit
  2.  
  3. open System
  4. open System.Xml
  5. open UnityEngine
  6. open UnityEditor
  7.  
  8. module SlotMode =
  9.   type T
  10.     = In
  11.     | Out
  12.    
  13.   let In = T.In
  14.   let Out = T.Out
  15.  
  16.   let GetName s =
  17.     match s with
  18.     | In -> "In"
  19.     | Out -> "Out"
  20.  
  21.   let GetValue s =
  22.     match s with
  23.     | "In" -> In
  24.     | _ -> Out
  25.  
  26. type DataType = {
  27.   Id : Guid
  28.   Name : string
  29.   Color : Color
  30. }
  31.  
  32. and Slot = {
  33.   Id : Guid
  34.   TypeId : Guid
  35.   Name : string
  36.   Mode : SlotMode.T
  37. } with
  38.   member x.AllowMultiple =
  39.     false
  40.  
  41. and NodeType = {
  42.   Id : Guid
  43.   Name : string
  44.   Color : Color
  45.   SlotsIn : Slot list
  46.   SlotsOut : Slot list
  47. } with
  48.   member x.AllowData =
  49.     false
  50.  
  51.   member x.HasGuid (id:Guid) =
  52.     let check (s:Slot) = s.Id = id
  53.     x.Id = id ||
  54.     x.SlotsIn |> List.exists check ||
  55.     x.SlotsOut |> List.exists check
  56.    
  57.   member x.HasSlot (s:Slot) =
  58.     let check (s':Slot) = s' = s
  59.     match s.Mode with
  60.     | SlotMode.In -> List.exists check x.SlotsIn
  61.     | SlotMode.Out -> List.exists check x.SlotsOut
  62.    
  63. and Node = {
  64.   Id : Guid
  65.   TypeId : Guid
  66.   Position : Vector3
  67.   SlotsIn : Slot list
  68.   SlotsOut : Slot list
  69. } with
  70.   member x.Enabled =
  71.     true
  72.  
  73.   member x.HasGuid (id:Guid) =
  74.     let check (s:Slot) = s.Id = id
  75.     x.Id = id ||
  76.     x.TypeId = id ||
  77.     x.SlotsIn |> List.exists check ||
  78.     x.SlotsOut |> List.exists check  
  79.  
  80. and LinkType = {
  81.   Id : Guid
  82.   Name : string
  83.   Color : Color
  84. } with
  85.   member x.AllowData =
  86.     false
  87.  
  88. and Link = {
  89.   Id : Guid
  90.   TypeId : Guid
  91.   SourceId : Guid
  92.   SourceSlotId : Guid
  93.   TargetId : Guid
  94.   TargetSlotId : Guid
  95. } with
  96.   member x.Enabled =
  97.     true
  98.  
  99.   member x.HasGuid (id:Guid) =
  100.     x.Id = id ||
  101.     x.TypeId = id ||
  102.     x.SourceId = id ||
  103.     x.SourceSlotId = id ||
  104.     x.TargetId = id ||
  105.     x.TargetSlotId = id
  106.  
  107. and Context = {
  108.   Id : Guid
  109.   Name : string
  110.   DataTypes : Map<Guid, DataType>
  111.   NodeTypes : Map<Guid, NodeType>
  112.   LinkTypes : Map<Guid, LinkType>
  113. } with
  114.   member x.HasGuid (id:Guid) =
  115.     x.Id = id ||
  116.     x.DataTypes.ContainsKey id ||
  117.     x.LinkTypes.ContainsKey id ||
  118.     x.NodeTypes |> Map.exists (fun _ n -> n.HasGuid id)
  119.  
  120.   member x.DataTypesSeq = seq { for kvp in x.DataTypes do yield kvp.Value }
  121.   member x.NodeTypesSeq = seq { for kvp in x.NodeTypes do yield kvp.Value }
  122.   member x.LinkTypesSeq = seq { for kvp in x.LinkTypes do yield kvp.Value }
  123.  
  124.   member x.HasDataType (dt:DataType) = x.DataTypes.ContainsKey(dt.Id) && x.DataTypes.[dt.Id] = dt
  125.   member x.HasNodeType (nt:NodeType) = x.NodeTypes.ContainsKey(nt.Id) && x.NodeTypes.[nt.Id] = nt
  126.   member x.HasLinkType (lt:LinkType) = x.LinkTypes.ContainsKey(lt.Id) && x.LinkTypes.[lt.Id] = lt
  127.  
  128.   member x.FindDataType (id:Guid) = Map.find id x.DataTypes
  129.   member x.FindNodeType (id:Guid) = Map.find id x.NodeTypes
  130.   member x.FindLinkType (id:Guid) = Map.find id x.LinkTypes
  131.  
  132.   member x.FindDataType (name:string) = Map.pick (fun _ (dt:DataType) -> if dt.Name = name then Some dt else None) x.DataTypes
  133.   member x.FindNodeType (name:string) = Map.pick (fun _ (nt:NodeType) -> if nt.Name = name then Some nt else None) x.NodeTypes
  134.   member x.FindLinkType (name:string) = Map.pick (fun _ (lt:LinkType) -> if lt.Name = name then Some lt else None) x.LinkTypes
  135.  
  136.   member x.VerifyGuidIsAvailable (id:Guid) = if x.HasGuid id then failwith "GUID already in use"
  137.   member x.VerifyHasDataType (dt:DataType) = if not (x.HasDataType dt) then failwith "DataType does not exist in context"
  138.   member x.VerifyHasNodeType (nt:NodeType) = if not (x.HasNodeType nt) then failwith "NodeType does not exist in context"
  139.   member x.VerifyHasLinkType (lt:LinkType) = if not (x.HasLinkType lt) then failwith "LinkType does not exist in context"
  140.  
  141. and Canvas = {
  142.   Id : Guid
  143.   Context : Context
  144.   Name : string
  145.   Nodes : Map<Guid, Node>
  146.   Links : Map<Guid, Link>
  147. } with
  148.   member x.HasGuid (id:Guid) =  
  149.     x.Id = id ||
  150.     x.Nodes |> Map.exists (fun _ n -> n.HasGuid id) ||
  151.     x.Links |> Map.exists (fun _ l -> l.HasGuid id) ||
  152.     x.Context.HasGuid id
  153.  
  154.   member x.NodesSeq = seq { for kvp in x.Nodes do yield kvp.Value }
  155.   member x.LinksSeq = seq { for kvp in x.Links do yield kvp.Value }
  156.  
  157.   member x.HasNode (n:Node) = x.Nodes.ContainsKey n.Id && x.Nodes.[n.Id] = n
  158.   member x.HasLink (l:Link) = x.Links.ContainsKey l.Id && x.Links.[l.Id] = l
  159.  
  160.   member x.VerifyGuidIsAvailable (id:Guid) = if x.HasGuid id then failwith "GUID already in use"
  161.   member x.VerifyHasNode (n:Node) = if not (x.HasNode n) then failwith "Node does not exist in canvas"
  162.   member x.VerifyHasLink (l:Link) = if not (x.HasLink l) then failwith "Link does not exist in canvas"
  163.   member x.VerifyNodeHasSlot (n:Node) (s:Slot) = if not (x.NodeHasSlot n s) then failwith "Slot does not exist on node"
  164.  
  165.   member x.NodeHasLink (n:Node) (s:Slot) =
  166.     x.VerifyHasNode n
  167.     x.VerifyNodeHasSlot n s
  168.  
  169.     match s.Mode with
  170.     | SlotMode.In -> Map.exists (fun k l -> l.TargetId = n.Id && l.TargetSlotId = s.Id) x.Links
  171.     | SlotMode.Out -> Map.exists (fun k l -> l.SourceId = n.Id && l.SourceSlotId = s.Id) x.Links
  172.  
  173.   member x.NodeHasSlot (n:Node) (s:Slot) =
  174.     x.VerifyHasNode n
  175.  
  176.     let nt = x.Context.FindNodeType n.TypeId
  177.  
  178.     match nt.HasSlot s, s.Mode with
  179.     | true , _   -> true
  180.     | false, SlotMode.In  -> n.SlotsIn |> List.existsItem s
  181.     | false, SlotMode.Out -> n.SlotsOut |> List.existsItem s
  182.  
  183. [<AbstractClass>]
  184. type ContextProvider () =
  185.  
  186.   static let sync = obj()
  187.   static let mutable contexts = Map.empty<Guid, Context>
  188.  
  189.   static member LoadContext (id:Guid) : Context =
  190.     lock sync (fun () ->
  191.       if contexts.IsEmpty then
  192.         for typ in Reflection.GetAllSubClasses(typeof<ContextProvider>) do
  193.           let obj = Activator.CreateInstance(typ) :?> ContextProvider
  194.           let ctx = obj.CreateContext()
  195.           contexts <- contexts.Add(ctx.Id, ctx)
  196.          
  197.       match contexts.TryFind id with
  198.       | Some ctx -> ctx
  199.       | None -> failwithf "Unknown context %A" id)
  200.  
  201.   abstract member CreateContext : unit -> Context
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement