Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open System.Windows.Forms
- open System.Drawing
- type Direction =
- |Up
- |Down
- |Right
- |Left
- type Animation() as this =
- inherit Control()
- let mutable posX, posY = this.Width/2 - 50, this.Height/2 - 50 // pos del satellite
- let mutable dir = Down
- let timer = new Timer(Interval = 30)
- // mi serve per capire se p รจ all'interno di r
- let correlate (p:Point) (r:Rectangle) =
- let mutable res = true
- if p.X <= r.X || p.X >= r.X + r.Width || p.Y <= r.Y || p.Y >= r.Y + this.Height then
- false
- else
- true
- //do this.SetStyle(ControlStyles.OptimizedDoubleBuffer,true) in questo caso non serve -> niente flicker comunque
- do timer.Tick.Add(fun e ->
- match dir with
- | Down -> posY <- posY + 5
- if posY >= this.Height/2 + 50 then dir <- Right
- | Right -> posX <- posX + 5
- if posX >= this.Width/2 + 50 then dir <- Up
- | Up -> posY <- posY - 5
- if posY <= this.Height/2 - 50 then dir <- Left
- | Left -> posX <- posX - 5
- if posX <= this.Width/2 - 50 then dir <- Down
- this.Invalidate()
- )
- override this.OnPaint e =
- let g = e.Graphics
- g.FillEllipse(Brushes.Yellow, this.Width/2 - 25, this.Height/2 - 25, 50, 50) // pivot
- g.FillEllipse(Brushes.Black, posX, posY, 10, 10) // satellite
- g.FillRectangle(Brushes.Green, 5, 5, 50, 20) // bottone play
- g.FillRectangle(Brushes.Red, 5, 30, 50, 20) // bottone stop
- override this.OnResize e =
- // ripristino
- posX <- this.Width/2 - 50
- posY <- this.Height/2 - 50
- dir <- Down
- this.Invalidate()
- override this.OnMouseDown e =
- let rplay = new Rectangle(5, 5, 50, 20)
- let rstop = new Rectangle(5, 30, 50, 20)
- if correlate e.Location rplay then
- timer.Start() // ha cliccato su play
- if correlate e.Location rstop then
- timer.Stop() // ... su stop
- this.Invalidate()
- override this.OnKeyDown e =
- match e.KeyCode with
- | Keys.P -> timer.Start() //play
- this.Invalidate()
- | Keys.X -> timer.Stop() //stop
- this.Invalidate()
- | _ -> ()
- // testing
- let f = new Form(Text="Animation", TopMost=true)
- f.Show()
- let a = new Animation(Dock=DockStyle.Fill)
- f.Controls.Add(a)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement