forked from qwer0123456/D2RAssist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOverlay.cs
176 lines (151 loc) · 5.93 KB
/
Overlay.cs
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
/**
* Copyright (C) 2021 okaygo
*
* https://github.com/misterokaygo/D2RAssist/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
**/
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Newtonsoft.Json;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Net.Http;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using D2RAssist.Types;
using D2RAssist.Helpers;
namespace D2RAssist
{
public partial class Overlay : Form
{
// Move to windows external
private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
private const UInt32 SWP_NOSIZE = 0x0001;
private const UInt32 SWP_NOMOVE = 0x0002;
private const UInt32 TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE;
private Screen _screen;
public Overlay()
{
InitializeComponent();
}
private void Overlay_Load(object sender, EventArgs e)
{
this.Opacity = Settings.Map.Opacity;
Timer MapUpdateTimer = new Timer();
MapUpdateTimer.Interval = Settings.Map.UpdateTime;
MapUpdateTimer.Tick += new EventHandler(MapUpdateTimer_Tick);
MapUpdateTimer.Start();
if (Settings.Map.AlwaysOnTop)
{
uint initialStyle = (uint)WindowsExternal.GetWindowLongPtr(this.Handle, -20);
WindowsExternal.SetWindowLong(this.Handle, -20, initialStyle | 0x80000 | 0x20);
WindowsExternal.SetWindowPos(this.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
}
mapOverlay.Location = new Point(0, 0);
mapOverlay.Width = this.Width;
mapOverlay.Height = this.Height;
mapOverlay.BackColor = Color.Transparent;
}
private async void MapUpdateTimer_Tick(object sender, EventArgs e)
{
Timer timer = sender as Timer;
timer.Stop();
Globals.CurrentGameData = GameMemory.GetGameData();
if (Globals.CurrentGameData != null)
{
bool shouldUpdateMap = (Globals.CurrentGameData.MapSeed != 0 && Globals.MapData == null) ||
(Globals.CurrentGameData.AreaId != Globals.LastGameData?.AreaId &&
Globals.CurrentGameData.AreaId != 0 ||
Globals.CurrentGameData.Difficulty != Globals.LastGameData?.Difficulty);
if (Globals.LastGameData?.MapSeed != Globals.CurrentGameData.MapSeed && Globals.CurrentGameData.MapSeed != 0)
{
await MapApi.CreateNewSession();
}
if (shouldUpdateMap)
{
Globals.MapData = await MapApi.GetMapData(Globals.CurrentGameData.AreaId);
}
Globals.LastGameData = Globals.CurrentGameData;
if (ShouldHideMap())
{
mapOverlay.Hide();
}
else
{
mapOverlay.Show();
mapOverlay.Refresh();
}
}
timer.Start();
}
private bool ShouldHideMap()
{
if (Globals.CurrentGameData.MapSeed == 0 || !D2RProcessInForeground())
{
return true;
}
if (Settings.Map.HideInTown && Globals.CurrentGameData.AreaId.IsTown())
{
return true;
}
if (Settings.Map.ToggleViaInGameMap)
{
// Hide the map if the ingame map is hidden
return !Globals.CurrentGameData.MapShown;
}
return false;
}
private bool D2RProcessInForeground()
{
IntPtr activeWindowHandle = WindowsExternal.GetForegroundWindow();
return activeWindowHandle == Globals.CurrentGameData.MainWindowHandle;
}
private void MapOverlay_Paint(object sender, PaintEventArgs e)
{
// Handle race condition where mapData hasn't been received yet.
if (Globals.MapData == null || Globals.MapData.mapRows[0].Length == 0)
{
return;
}
UpdateLocation();
Bitmap gameMap = MapRenderer.FromMapData(Globals.MapData);
Point anchor = new Point(0, 0);
switch (Settings.Map.MapPosition) {
case MapPosition.TopRight:
anchor = new Point(_screen.WorkingArea.Width - gameMap.Width, 0);
break;
case MapPosition.TopLeft:
anchor = new Point(0, 0);
break;
}
e.Graphics.DrawImage(gameMap, anchor);
}
/// <summary>
/// Update the location and size of the form relative to the D2R window location.
/// </summary>
private void UpdateLocation()
{
this.WindowState = FormWindowState.Normal;
_screen = Screen.FromHandle(Globals.CurrentGameData.MainWindowHandle);
this.Location = new Point(_screen.WorkingArea.X, _screen.WorkingArea.Y);
this.Size = new Size(_screen.WorkingArea.Width, _screen.WorkingArea.Height);
}
}
}