aboutsummaryrefslogtreecommitdiffstats
path: root/WarframeClock/ClockOverlay.cs
blob: acdd2f7c8d5dd19e03835b1f34cb0c283adb6ba3 (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using System;
using System.Linq;
using System.Threading;
using SharpDX;
using SharpDX.Direct2D1;
using SharpDX.DirectWrite;
using SharpDX.DXGI;
using SharpDX.Mathematics.Interop;
using AlphaMode = SharpDX.Direct2D1.AlphaMode;
using Color = System.Drawing.Color;

namespace WarframeClock
{
    class ClockOverlay : IDisposable
    {
        private readonly string targetProcessName;
        private Clock clock;
        private IntPtr targetWindowHandle;
        private IntPtr handle;
        private bool isVisible;
        private bool isTopMost;
        private Native.Rect parentBounds;

        private WindowRenderTarget device;
        private SharpDX.Direct2D1.Factory _factory;
        private SharpDX.DirectWrite.Factory _fontFactory;
        private static readonly Color gdiTransparent = Color.Transparent;
        private readonly RawColor4 transparent = new RawColor4(gdiTransparent.R, gdiTransparent.G, gdiTransparent.B,
            gdiTransparent.A);
        private TextFormat textFont;
        private SolidColorBrush textBrush;

        public ClockOverlay(Clock clock, string targetProcessName)
        {
            this.clock = clock;
            this.targetProcessName = targetProcessName;

            SetupOverlayWindow();
        }

        ~ClockOverlay()
        {
            Dispose();
        }

        private void SetupOverlayWindow()
        {
            isVisible = false;
            isTopMost = false;

            parentBounds = new Native.Rect()
            {
                Top = 0,
                Left = 0,
                Right = Native.GetSystemMetrics(0),
                Bottom = Native.GetSystemMetrics(1),
            };

            handle = Native.CreateWindowEx(
                0x8000000 /* WS_EX_NOACTIVATE */
                | 0x80000 /* WS_EX_LAYERED */
                | 0x80 /* WS_EX_TOOLWINDOW */
                | 0x20 /* WS_EX_TRANSPARENT */,
                "Static",
                "",
                0x8000000 /* WS_DISABLED */
                | 0x80000000 /* WS_POPUP */,
                parentBounds.Left,
                parentBounds.Top,
                parentBounds.Right - parentBounds.Left,
                parentBounds.Bottom - parentBounds.Top,
                IntPtr.Zero,
                IntPtr.Zero,
                IntPtr.Zero,
                IntPtr.Zero);
            if (handle == IntPtr.Zero)
            {
                throw new Exception("Could not create OverlayWindow");
            }
            Native.SetLayeredWindowAttributes(handle, 0, 255, 0x00000002 /* LWA_ALPHA */);

            // Setup Direct*
            _factory = new SharpDX.Direct2D1.Factory();
            _fontFactory = new SharpDX.DirectWrite.Factory();

            var targetProperties = new HwndRenderTargetProperties
            {
                Hwnd = handle,
                PixelSize = new Size2(
                    parentBounds.Right - parentBounds.Left,
                    parentBounds.Bottom - parentBounds.Top),
                PresentOptions = PresentOptions.None
            };

            var prop = new RenderTargetProperties(RenderTargetType.Hardware,
                new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied), 0, 0, RenderTargetUsage.None,
                FeatureLevel.Level_DEFAULT);
            device = new WindowRenderTarget(_factory, prop, targetProperties);

            textBrush = new SolidColorBrush(device, new RawColor4(0xff, 0xff, 0xff, 0x7f));
            textFont = new TextFormat(_fontFactory, "Segoe UI", FontWeight.Normal, FontStyle.Normal, 12);
        }

        public void Dispose()
        {
            textBrush.Dispose();
            textFont.Dispose();
            _fontFactory.Dispose();
            _factory.Dispose();
            device.Dispose();

            Native.DestroyWindow(handle);
        }

        public void Start()
        {
            while (true)
            {
                Update();
                Thread.Sleep(100);
            }
        }

        private void Update()
        {
            GetTargetWindow();
            if (targetWindowHandle == IntPtr.Zero)
            {
                if (isVisible)
                {
                    isVisible = false;
                    Native.ShowWindow(handle, 0);
                }
                return;
            }

            // Set top-most flag if the target window is foreground. Move to
            // behind the current foreground window otherwise.
            var currentActiveWindow = Native.GetForegroundWindow();
            if (currentActiveWindow == targetWindowHandle != isTopMost || !isVisible)
            {
                isTopMost = currentActiveWindow == targetWindowHandle;
                var after = isTopMost ? (IntPtr)(-1 /* HWND_TOPMOST */) : currentActiveWindow;
                Console.WriteLine($"ClockOverlay: Overlay is inserted after: {after}");
                Native.SetWindowPos(handle, after, 0, 0, 0, 0, 0x0001 /* SWP_NOSIZE */ | 0x0002 /* SWP_NOMOVE */);
            }

            if (!isVisible)
            {
                isVisible = true;
                Native.ShowWindow(handle, 5);
            }

            Native.Rect newBounds;
            if (Native.GetWindowRect(targetWindowHandle, out newBounds) == 0)
            {
                targetWindowHandle = IntPtr.Zero;
                return;
            }
            if (parentBounds.Left != newBounds.Left || parentBounds.Right != newBounds.Right ||
                parentBounds.Top != newBounds.Top || parentBounds.Bottom != newBounds.Bottom)
            {
                parentBounds = newBounds;

                var width = parentBounds.Right - parentBounds.Left;
                var height = parentBounds.Bottom - parentBounds.Top;

                Native.SetWindowPos(handle, IntPtr.Zero, parentBounds.Left, parentBounds.Top, width, height,
                    0x0004 /* SWP_NOZORDER */);

                device.Resize(new Size2(width, height));

                Native.RawMargin _margin;
                _margin.cxLeftWidth = parentBounds.Left;
                _margin.cxRightWidth = width;
                _margin.cyBottomHeight = height;
                _margin.cyTopHeight = parentBounds.Top;
                Native.DwmExtendFrameIntoClientArea(handle, ref _margin);
            }

            // Render text
            device.BeginDraw();
            device.Clear(transparent);

            var infoText = $"{clock.CetusExpiryString()}  {clock.VallisExpiryString()}";
            device.DrawText(infoText, textFont,
                new RawRectangleF(30, parentBounds.Bottom - parentBounds.Top - 50, float.MaxValue, float.MaxValue),
                textBrush);

            device.EndDraw();
        }

        private void GetTargetWindow()
        {
            if (targetWindowHandle == IntPtr.Zero || Native.IsWindow(targetWindowHandle) == 0)
            {
                targetWindowHandle = IntPtr.Zero;

                var process = System.Diagnostics.Process.GetProcessesByName(targetProcessName).FirstOrDefault();
                if (process == null)
                {
                    return;
                }

                try
                {
                    targetWindowHandle = process.MainWindowHandle;
                    if (targetWindowHandle != IntPtr.Zero)
                    {
                        Console.WriteLine($"ClockOverlay: Found a process with name={targetProcessName}; " +
                            $"MainWindowHandle={targetWindowHandle}");
                    }
                }
                catch (Exception)
                {
                    // Ignore errors; maybe the process is just exiting. Let's retry during the next cycle.
                }
            }
        }
    }
}