Advertisement
Guest User

Untitled

a guest
Nov 6th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 5.25 KB | None | 0 0
  1. open System.Windows.Forms
  2. open System.Drawing
  3.  
  4. type W2V() =
  5.   let w2v = new Drawing2D.Matrix()
  6.   let v2w = new Drawing2D.Matrix()
  7.  
  8.   member this.Translate(tx, ty) =
  9.     w2v.Translate(tx, ty)
  10.     v2w.Translate(-tx, -ty, Drawing2D.MatrixOrder.Append)
  11.  
  12.   member this.Rotate(a) =
  13.     w2v.Rotate(a)
  14.     v2w.Rotate(-a, Drawing2D.MatrixOrder.Append)
  15.  
  16.   member this.Scale(sx, sy) =
  17.     w2v.Scale(sx, sy)
  18.     v2w.Scale(1.f/sx, 1.f/sy, Drawing2D.MatrixOrder.Append)
  19.  
  20.   member this.W2V with get() = w2v
  21.   member this.V2W with get() = v2w
  22.  
  23. let Rect2RectF (r:Rectangle) =
  24.   RectangleF(single r.X, single r.Y, single r.Width, single r.Height)
  25.  
  26. let RectF2Rect (r:RectangleF) =
  27.   Rectangle(int r.X, int r.Y, int r.Width, int r.Height)
  28.  
  29. type CoordinateType = View | World
  30.  
  31. type LWControl() =
  32.   let mutable coordinates = View
  33.  
  34.   let mutable position = PointF()
  35.   let mutable size = SizeF()
  36.  
  37.   let mutable parent : Control = null
  38.  
  39.   let mousedownevt = new Event<MouseEventArgs>()
  40.   let mousemoveevt = new Event<MouseEventArgs>()
  41.   let mouseupevt = new Event<MouseEventArgs>()
  42.  
  43.   member this.CoordinateType
  44.     with get() = coordinates
  45.     and set(v) = coordinates <- v
  46.  
  47.   member this.Position
  48.     with get() = position
  49.     and set(v) = position <- v
  50.  
  51.   member this.Size
  52.     with get() = size
  53.     and set(v) = size <- v
  54.  
  55.   member this.Parent
  56.     with get() = parent
  57.     and set(v) = parent <- v
  58.  
  59.   member this.MouseDown = mousedownevt.Publish
  60.  
  61.   abstract OnMouseDown : MouseEventArgs -> unit
  62.   default this.OnMouseDown e = mousedownevt.Trigger(e)
  63.  
  64.   abstract OnMouseMove : MouseEventArgs -> unit
  65.   default this.OnMouseMove e = mousemoveevt.Trigger(e)
  66.  
  67.   abstract OnMouseUp : MouseEventArgs -> unit
  68.   default this.OnMouseUp e = mouseupevt.Trigger(e)
  69.  
  70.   abstract OriginOnPaint : PaintEventArgs -> unit
  71.   default this.OriginOnPaint e = ()
  72.  
  73.   abstract OnPaint : PaintEventArgs -> unit
  74.   default this.OnPaint e =
  75.     let g = e.Graphics
  76.     let t = g.Transform
  77.  
  78.     // Cambio il contesto grafico in base alla posizione
  79.     let mutable new_t = g.Transform
  80.     if (this.CoordinateType = View) then
  81.       new_t <- new Drawing2D.Matrix()
  82.     new_t.Translate(this.Position.X, this.Position.Y)
  83.     g.Transform <- new_t
  84.  
  85.     // Richiamo la stampa effettiva
  86.     this.OriginOnPaint e
  87.  
  88.     // Ripristino il contesto grafico
  89.     g.Transform <- t
  90.  
  91.  
  92.   abstract OriginHitTest : PointF -> bool
  93.     default this.OriginHitTest p =
  94.     (new RectangleF(PointF(0.0f,0.0f), size)).Contains(p)
  95.  
  96.   abstract HitTest : PointF -> bool
  97.   default this.HitTest p =
  98.     // Cambio le coordinate del test
  99.     let mutable new_p = p - Size(int this.Position.X,int this.Position.Y)
  100.    
  101.     // Richiamo il test effettivo
  102.     this.OriginHitTest new_p
  103.  
  104.  
  105. type LWContainer() as this =
  106.   inherit UserControl()
  107.  
  108.   let transformPoint (m:Drawing2D.Matrix) (p:PointF) =
  109.     let pts = [| p |]
  110.     m.TransformPoints(pts)
  111.     pts.[0]
  112.  
  113.   let transform = W2V()
  114.  
  115.   let controls = ResizeArray<LWControl>()
  116.  
  117.   let scrollUp () =
  118.     transform.Translate(0.f, 10.f)
  119.     this.Invalidate()
  120.  
  121.  
  122.   member this.LWControls with get() = controls
  123.  
  124.   override this.OnMouseDown e =
  125.     let p = PointF(single e.X, single e.Y)
  126.     let controlsView = controls |> Seq.filter (fun c -> c.CoordinateType = View)
  127.     match (controlsView |> Seq.tryFind (fun c -> c.HitTest p)) with
  128.     | Some c -> c.OnMouseDown(e)
  129.     | None ->
  130.       let pw = transformPoint transform.V2W p
  131.       let controlsWorld = controls |> Seq.filter (fun c -> c.CoordinateType = World)
  132.       match (controlsWorld |> Seq.tryFind(fun c -> c.HitTest pw)) with
  133.       | Some c -> c.OnMouseDown(e)
  134.       | None -> ()
  135.  
  136.   override this.OnMouseMove e =
  137.     let p = PointF(single e.X, single e.Y)
  138.     let controlsView = controls |> Seq.filter (fun c -> c.CoordinateType = View)
  139.     match (controlsView |> Seq.tryFind (fun c -> c.HitTest p)) with
  140.     | Some c -> c.OnMouseMove(e)
  141.     | None -> ()
  142.  
  143.   override this.OnMouseUp e =
  144.     let p = PointF(single e.X, single e.Y)
  145.     let controlsView = controls |> Seq.filter (fun c -> c.CoordinateType = View)
  146.     match (controlsView |> Seq.tryFind (fun c -> c.HitTest p)) with
  147.     | Some c -> c.OnMouseUp(e)
  148.     | None -> ()
  149.  
  150.   override this.OnPaint e =
  151.     let g = e.Graphics
  152.  
  153.     let t = g.Transform
  154.  
  155.     g.Transform <- transform.W2V
  156.  
  157.     for idx in (controls.Count - 1) .. -1 .. 0 do
  158.       let c = controls.[idx]
  159.       if c.CoordinateType = World then
  160.         c.OnPaint e
  161.    
  162.     g.Transform <- t
  163.  
  164.     for idx in (controls.Count - 1) .. -1 .. 0 do
  165.       let c = controls.[idx]
  166.       if c.CoordinateType = View then
  167.         c.OnPaint e
  168.  
  169.   override this.OnKeyDown e =
  170.     match e.KeyCode with
  171.     | Keys.W -> scrollUp()
  172.     | Keys.A ->
  173.       transform.Translate(10.f, 0.f)
  174.       this.Invalidate()
  175.     | Keys.S ->
  176.       transform.Translate(0.f, -10.f)
  177.       this.Invalidate()
  178.     | Keys.D ->
  179.       transform.Translate(-10.f, 0.f)
  180.       this.Invalidate()
  181.     | Keys.Q ->
  182.       transform.Rotate(10.f)
  183.       this.Invalidate()
  184.     | Keys.E ->
  185.       transform.Rotate(-10.f)
  186.       this.Invalidate()
  187.     | Keys.Z ->
  188.       transform.Scale(1.1f, 1.1f)
  189.       this.Invalidate()
  190.     | Keys.X ->
  191.       transform.Scale(1.f/1.1f, 1.f/1.1f)
  192.       this.Invalidate()
  193.     | _ -> ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement