Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2014
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 6.12 KB | None | 0 0
  1. namespace Circuit
  2.  
  3. open System
  4. open UnityEngine
  5. open UnityEditor
  6.  
  7. module private GUI =
  8.  
  9.   open Utils
  10.   open EditorUtils
  11.  
  12.   let mutable private mouseUp : (unit -> unit) option array = [| None; None; None |]
  13.   let mutable private mouseMove : (Vector2 -> unit) option array = [| None; None; None |]
  14.   let mutable private mouseAllow : bool = true
  15.  
  16.   let private MouseHandlers b up down =
  17.     match mouseUp.[b] with
  18.     | None -> ()
  19.     | Some _ -> logWarn (sprintf "Mouse button %i got it's up callback overriden" b)
  20.    
  21.     match mouseMove.[b] with
  22.     | None -> ()
  23.     | Some _ -> logWarn (sprintf "Mouse button %i got it's move callback overriden" b)
  24.  
  25.     mouseUp.[b] <- up
  26.     mouseMove.[b] <- down
  27.  
  28.   let MouseState allow (f:unit -> unit) =
  29.     let mouseAllow' = mouseAllow
  30.    //let mouseMove' = mouseMove
  31.     //let mouseUp' = mouseUp
  32.  
  33.     mouseAllow <- allow
  34.     //mouseMove <- move
  35.     //mouseUp <- up
  36.  
  37.     try
  38.       f()
  39.  
  40.     finally
  41.       mouseAllow <- mouseAllow'
  42.      //mouseMove <- mouseMove'
  43.       //mouseUp <- mouseUp'
  44.  
  45.   let MouseDown b down =
  46.     if mouseAllow then
  47.       let ev = Event.current
  48.  
  49.       if ev.``type`` = EventType.MouseDown && ev.button = b then
  50.         down()
  51.         ev.Use()
  52.         MouseHandlers b None None
  53.  
  54.   let MouseUp b up =
  55.     if mouseAllow then
  56.       let ev = Event.current
  57.  
  58.       if ev.``type`` = EventType.MouseUp && ev.button = b then
  59.         up()
  60.         ev.Use()
  61.         MouseHandlers b None None
  62.      
  63.   let MouseScroll scroll =
  64.     if mouseAllow then
  65.       let ev = Event.current
  66.  
  67.       if ev.``type`` = EventType.ScrollWheel then
  68.         scroll ev.delta
  69.         ev.Use()
  70.  
  71.   let MouseDrag b move =
  72.     if mouseAllow then
  73.       let ev = Event.current
  74.  
  75.       if ev.``type`` = EventType.MouseDrag && ev.button = b then
  76.         move ev.delta
  77.         ev.Use()
  78.  
  79.   let MouseDownUp b down up =
  80.     if mouseAllow then
  81.       let ev = Event.current
  82.  
  83.       if ev.``type`` = EventType.MouseDown && ev.button = b then
  84.         down()
  85.         ev.Use()
  86.         MouseHandlers b (Some up) None
  87.  
  88.   let MouseDownUpMove b down up move =
  89.     if mouseAllow then
  90.       let ev = Event.current
  91.  
  92.       if ev.``type`` = EventType.MouseDown && ev.button = b then
  93.         down()
  94.         ev.Use()
  95.         MouseHandlers b (Some up) (Some move)
  96.        
  97.   let TextureButton rect style color =
  98.     GUI.color <- color
  99.  
  100.     let result =
  101.       if mouseAllow then
  102.         GUI.Button(rect, "", style)
  103.  
  104.       else
  105.         GUI.DrawTexture(rect, style.normal.background)
  106.         false
  107.  
  108.     GUI.color <- Color.white
  109.     result
  110.  
  111.   let KeyUp (kc:KeyCode) f =
  112.     let ev = Event.current
  113.  
  114.     if ev.``type`` = EventType.KeyUp && ev.keyCode = kc then
  115.       f()
  116.      
  117.   let KeyDown (kc:KeyCode) f =
  118.     let ev = Event.current
  119.  
  120.     if ev.``type`` = EventType.KeyDown && ev.keyCode = kc then
  121.       f()
  122.  
  123.   let MouseHandleEvents () =
  124.     if mouseAllow then
  125.       let ev = Event.current
  126.       let b = ev.button
  127.  
  128.       match ev.``type`` with
  129.       | EventType.MouseDrag ->
  130.         match mouseMove.[b] with
  131.         | None -> ()
  132.         | Some f ->
  133.           f ev.delta
  134.           ev.Use()
  135.  
  136.       | EventType.MouseUp ->
  137.         match mouseUp.[b] with
  138.         | None -> ()
  139.         | Some f ->
  140.           f()
  141.           ev.Use()
  142.  
  143.         mouseUp.[b] <- None
  144.         mouseMove.[b] <- None
  145.  
  146.       | _ ->
  147.         ()
  148.  
  149.   let ZoomBegin (w, h, z) =
  150.     GUI.EndGroup()
  151.     GUI.BeginGroup(new Rect(0.0f, 21.0f / z, w / z, h / z))
  152.     GUI.matrix <- Matrix4x4.Scale(new Vector3(z, z, 1.0f)) * GUI.matrix
  153.  
  154.   let ZoomEnd (w, h) =
  155.     GUI.matrix <- Matrix4x4.identity
  156.     GUI.EndGroup()
  157.     GUI.BeginGroup(new Rect(0.0f, 21.0f, w, h))
  158.  
  159.   let rec DrawLine (from:float2) (to':float2) (tangent:float32) (width:float32) (clr:Color) =
  160.    if from.x > to'.x then
  161.       DrawLine to' from tangent width clr
  162.  
  163.    else
  164.      let tangent = min tangent ((from.x-to'.x) * 0.45f)
  165.       let fromPos = from
  166.       let fromTan = fromPos + new float2(-tangent, 0.0f)
  167.       let toPos = to'
  168.      let toTan = toPos + new float2(tangent, 0.0f)
  169.      
  170.      Handles.DrawBezier(
  171.        new float3(fromPos.x, fromPos.y, 0.0f),
  172.        new float3(toPos.x, toPos.y, 0.0f),
  173.        new float3(fromTan.x, fromTan.y, 0.0f),
  174.        new float3(toTan.x, toTan.y, 0.0f),
  175.        clr, null, width
  176.      )
  177.  
  178.  let Group (r:Rect, f) =
  179.    GUI.BeginGroup(r)
  180.    f (int r.width) (int r.height)
  181.    GUI.EndGroup()
  182.  
  183.  let Toolbox (x, y, w, h, c, s, f) =
  184.    // main group
  185.    GUI.BeginGroup(rect x y w h)
  186.  
  187.    // background
  188.    GUI.color <- c
  189.    GUI.Box(rect 0 0 w h, "", s)
  190.    GUI.color <- Color.white
  191.    
  192.    // content
  193.    f w h
  194.  
  195.    // end main group
  196.    GUI.EndGroup()
  197.  
  198.  let Grid (r:Rect, s:int, sizeh:int, m:int, items:'a list, pos:Vector2, draw) =
  199.     GUI.BeginGroup(r)
  200.  
  201.     let s = f s
  202.     let m = f m
  203.     let w = r.width
  204.     let h = r.height
  205.     let l = f items.Length
  206.  
  207.     // how many nodes we can have in one row with fraction
  208.     let n = max 1.0f ((w - m) / (s + m))
  209.  
  210.     // how many whole nodes we can have in one row
  211.     let y = floor n
  212.    
  213.     // how much extra space we got left over
  214.     let x = n - y
  215.    
  216.     // how much extra space we need to add
  217.     let xs = ((w - m) * (x / n)) / y
  218.  
  219.     // final size of each element
  220.     let s = s + xs
  221.  
  222.     // calculate scroll height
  223.     let sh = (ceil (l / y)) * ((f sizeh) + m)
  224.  
  225.     // scroll position rect
  226.     let sr = rect 0 0 (int (w + 20.0f)) (int h)
  227.  
  228.     // scroll view rect
  229.     let sv = rect 0 0 (int w) (int sh)
  230.  
  231.     // draw our scrollview
  232.     let pos = GUI.BeginScrollView(sr, pos, sv)
  233.  
  234.     let mutable r = 0
  235.     let mutable c = 0
  236.  
  237.     for i in items do
  238.  
  239.       if c = int n then
  240.         c <- 0
  241.         r <- r + 1
  242.  
  243.       let x_pos = m + ((f c) * (s + m)) |> int
  244.       let y_pos = (f r) * ((f sizeh) + m) |> int
  245.  
  246.       let size = s |> int
  247.  
  248.       // item group
  249.       GUI.BeginGroup(rect x_pos y_pos size sizeh)
  250.  
  251.       // draw item
  252.       draw i size sizeh
  253.  
  254.       // end item group
  255.       GUI.EndGroup()
  256.  
  257.       // step column
  258.       c <- c + 1
  259.    
  260.     GUI.EndScrollView()
  261.     GUI.EndGroup()
  262.  
  263.     pos
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement