aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.WinForms/Viruses/WindowsEverywhere.cs
blob: df73d58525f6c5b0a9e7187a48e4a107c0750188 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShiftOS.Engine;

namespace ShiftOS.WinForms.Viruses
{
    [Virus("windows_everywhere", "Windows Everywhere", "Makes the windows dance around the screen if the user has the WM Free Placement upgrade. Speed depends on threatlevel.")]
    [RequiresUpgrade("wm_free_placement")]
    public class WindowsEverywhere : IVirus
    {
        private System.Windows.Forms.Timer timer = null;
        private int ThreatLevel = 1;
        private Dictionary<string, System.Drawing.Point> Velocities = null;


        public void Infect(int threatlevel)
        {
            Velocities = new Dictionary<string, System.Drawing.Point>();
            ThreatLevel = threatlevel;
            timer = new System.Windows.Forms.Timer();
            timer.Interval = 50;
            timer.Tick += (o, a) =>
            {
                foreach (var win in AppearanceManager.OpenForms)
                {
                    var border = (win as WindowBorder);
                    var loc = border.Location;
                    var velocity = new System.Drawing.Point(1, 1);
                    string vKey = border.GetType().Name + "_" + border.GetHashCode();
                    if (!Velocities.ContainsKey(vKey))
                    {
                        Velocities.Add(vKey, velocity);
                    }
                    else
                    {
                        velocity = Velocities[vKey];
                    }

                    //Calculate proper velocity.
                    if (border.Top <= 0)
                    {
                        velocity.Y = 1;
                    }
                    if (border.Top + border.Height >= System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height)
                    {
                        velocity.Y = -1;
                    }
                    if (border.Left <= 0)
                    {
                        velocity.X = 1;
                    }
                    if (border.Left + border.Width >= System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width)
                    {
                        velocity.X = -1;
                    }
                    //save velocity in memory.
                    Velocities[vKey] = velocity;
                    //convert using threatlevel

                    var velocityX = velocity.X * (threatlevel * 4);
                    var velocityY = velocity.Y * (threatlevel * 4);

                    loc.X += velocityX;
                    loc.Y += velocityY;

                    border.Location = loc;
                }
            };
            timer.Start();
        }

        public void Disinfect()
        {
            timer.Stop();
            Velocities.Clear();
            ThreatLevel = 0;
            timer = null;
        }
    }
}