Advertisement
DanFloyd

Foo Satellite Animation

Oct 21st, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.43 KB | None | 0 0
  1. open System.Windows.Forms
  2. open System.Drawing
  3.  
  4. type Direction =
  5.     |Up
  6.     |Down
  7.     |Right
  8.     |Left
  9.  
  10. type Animation() as this =
  11.  
  12.     inherit Control()
  13.  
  14.     let mutable posX, posY = this.Width/2 - 50, this.Height/2 - 50 // pos del satellite
  15.     let mutable dir = Down
  16.  
  17.     let timer = new Timer(Interval = 30)
  18.  
  19.     // mi serve per capire se p รจ all'interno di r
  20.     let correlate (p:Point) (r:Rectangle) =
  21.         let mutable res = true
  22.         if p.X <= r.X || p.X >= r.X + r.Width || p.Y <= r.Y || p.Y >= r.Y + this.Height then
  23.             false
  24.         else
  25.             true
  26.  
  27.     //do this.SetStyle(ControlStyles.OptimizedDoubleBuffer,true) in questo caso non serve -> niente flicker comunque
  28.     do timer.Tick.Add(fun e ->
  29.        match dir with
  30.        | Down -> posY <- posY + 5
  31.                  if posY >= this.Height/2 + 50 then dir <- Right
  32.        | Right -> posX <- posX + 5
  33.                   if posX >= this.Width/2 + 50 then dir <- Up
  34.        | Up -> posY <- posY - 5
  35.                if posY <= this.Height/2 - 50 then dir <- Left
  36.        | Left -> posX <- posX - 5
  37.                  if posX <= this.Width/2 - 50 then dir <- Down
  38.        this.Invalidate()
  39.        )
  40.  
  41.     override this.OnPaint e =
  42.         let g = e.Graphics
  43.         g.FillEllipse(Brushes.Yellow, this.Width/2 - 25, this.Height/2 - 25, 50, 50) // pivot
  44.         g.FillEllipse(Brushes.Black, posX, posY, 10, 10) // satellite
  45.         g.FillRectangle(Brushes.Green, 5, 5, 50, 20) // bottone play
  46.         g.FillRectangle(Brushes.Red, 5, 30, 50, 20) // bottone stop
  47.  
  48.     override this.OnResize e =
  49.         // ripristino
  50.         posX <- this.Width/2 - 50
  51.         posY <- this.Height/2 - 50
  52.         dir <- Down
  53.         this.Invalidate()
  54.  
  55.     override this.OnMouseDown e =
  56.         let rplay = new Rectangle(5, 5, 50, 20)
  57.         let rstop = new Rectangle(5, 30, 50, 20)
  58.         if correlate e.Location rplay then
  59.             timer.Start() // ha cliccato su play
  60.         if correlate e.Location rstop then
  61.             timer.Stop() // ... su stop
  62.         this.Invalidate()
  63.  
  64.      override this.OnKeyDown e =
  65.          match e.KeyCode with
  66.          | Keys.P -> timer.Start() //play
  67.                      this.Invalidate()
  68.          | Keys.X -> timer.Stop() //stop
  69.                      this.Invalidate()
  70.          | _ -> ()
  71.  
  72. // testing
  73. let f = new Form(Text="Animation", TopMost=true)
  74. f.Show()
  75. let a = new Animation(Dock=DockStyle.Fill)
  76. f.Controls.Add(a)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement