forked from paradoxial/D2RAssist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Overlay.cs
281 lines (232 loc) · 10.7 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
* Copyright (C) 2021 okaygo
*
* https://github.com/misterokaygo/MapAssist/
*
* 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 GameOverlay.Drawing;
using GameOverlay.Windows;
using MapAssist.Helpers;
using MapAssist.Settings;
using MapAssist.Types;
using System;
using System.Windows.Forms;
//using WK.Libraries.HotkeyListenerNS;
using Graphics = GameOverlay.Drawing.Graphics;
namespace MapAssist
{
public class Overlay : IDisposable
{
private static readonly NLog.Logger _log = NLog.LogManager.GetCurrentClassLogger();
private readonly GraphicsWindow _window;
private GameDataReader _gameDataReader;
private GameData _gameData;
private Compositor _compositor = new Compositor();
private bool _show = true;
private static readonly object _lock = new object();
public Overlay()
{
_gameDataReader = new GameDataReader();
GameOverlay.TimerService.EnableHighPrecisionTimers();
var gfx = new Graphics() { MeasureFPS = true };
gfx.PerPrimitiveAntiAliasing = true;
gfx.TextAntiAliasing = true;
_window = new GraphicsWindow(0, 0, 1, 1, gfx) { FPS = 60, IsVisible = true };
_window.DrawGraphics += _window_DrawGraphics;
_window.DestroyGraphics += _window_DestroyGraphics;
}
private void _window_DrawGraphics(object sender, DrawGraphicsEventArgs e)
{
if (disposed) return;
var gfx = e.Graphics;
try
{
lock (_lock)
{
var (gameData, areaData, pointsOfInterest, changed) = _gameDataReader.Get();
_gameData = gameData;
if (changed)
{
_compositor.SetArea(areaData, pointsOfInterest);
}
gfx.ClearScene();
if (_compositor != null && InGame() && _compositor != null && _gameData != null)
{
UpdateLocation();
var errorLoadingAreaData = _compositor._areaData == null;
var overlayHidden = !_show ||
errorLoadingAreaData ||
(MapAssistConfiguration.Loaded.RenderingConfiguration.ToggleViaInGameMap && !_gameData.MenuOpen.Map) ||
(MapAssistConfiguration.Loaded.RenderingConfiguration.ToggleViaInGamePanels && _gameData.MenuPanelOpen > 0) ||
(MapAssistConfiguration.Loaded.RenderingConfiguration.ToggleViaInGamePanels && _gameData.MenuOpen.EscMenu) ||
Array.Exists(MapAssistConfiguration.Loaded.HiddenAreas, area => area == _gameData.Area) ||
_gameData.Area == Area.None ||
gfx.Width == 1 ||
gfx.Height == 1;
var size = MapAssistConfiguration.Loaded.RenderingConfiguration.Size;
var drawBounds = new Rectangle(0, 0, gfx.Width, gfx.Height * 0.78f);
switch (MapAssistConfiguration.Loaded.RenderingConfiguration.Position)
{
case MapPosition.TopLeft:
drawBounds = new Rectangle(PlayerIconWidth() + 40, PlayerIconWidth() + 100, 0, PlayerIconWidth() + 100 + size);
break;
case MapPosition.TopRight:
drawBounds = new Rectangle(0, 100, gfx.Width, 100 + size);
break;
}
_compositor.Init(gfx, _gameData, drawBounds);
if (!overlayHidden)
{
_compositor.DrawGamemap(gfx);
_compositor.DrawOverlay(gfx);
_compositor.DrawBuffs(gfx);
_compositor.DrawMonsterBar(gfx);
}
_compositor.DrawPlayerInfo(gfx);
var gameInfoAnchor = GameInfoAnchor(MapAssistConfiguration.Loaded.GameInfo.Position);
var nextAnchor = _compositor.DrawGameInfo(gfx, gameInfoAnchor, e, errorLoadingAreaData);
var itemLogAnchor = (MapAssistConfiguration.Loaded.ItemLog.Position == MapAssistConfiguration.Loaded.GameInfo.Position)
? nextAnchor.Add(0, GameInfoPadding())
: GameInfoAnchor(MapAssistConfiguration.Loaded.ItemLog.Position);
_compositor.DrawItemLog(gfx, itemLogAnchor);
}
}
}
catch (Exception ex)
{
_log.Error(ex);
}
}
public void Run()
{
_window.Create();
_window.Join();
}
private bool InGame()
{
return _gameData != null && _gameData.MainWindowHandle != IntPtr.Zero;
}
public void KeyDownHandler(object sender, KeyEventArgs args)
{
if (InGame() && GameManager.IsGameInForeground)
{
var keys = new Hotkey(args.Modifiers, args.KeyCode);
if (keys == new Hotkey(MapAssistConfiguration.Loaded.HotkeyConfiguration.ToggleKey))
{
_show = !_show;
}
if (keys == new Hotkey(MapAssistConfiguration.Loaded.HotkeyConfiguration.HideMapKey))
{
_show = false;
}
if (keys == new Hotkey(MapAssistConfiguration.Loaded.HotkeyConfiguration.AreaLevelKey))
{
MapAssistConfiguration.Loaded.GameInfo.ShowAreaLevel = !MapAssistConfiguration.Loaded.GameInfo.ShowAreaLevel;
}
if (keys == new Hotkey(MapAssistConfiguration.Loaded.HotkeyConfiguration.ZoomInKey))
{
var zoomLevel = MapAssistConfiguration.Loaded.RenderingConfiguration.ZoomLevel;
if (MapAssistConfiguration.Loaded.RenderingConfiguration.ZoomLevel > 0.1f)
{
MapAssistConfiguration.Loaded.RenderingConfiguration.ZoomLevel -= zoomLevel <= 1 ? 0.1 : 0.2;
MapAssistConfiguration.Loaded.RenderingConfiguration.Size +=
(int)(MapAssistConfiguration.Loaded.RenderingConfiguration.InitialSize * 0.05f);
}
}
if (keys == new Hotkey(MapAssistConfiguration.Loaded.HotkeyConfiguration.ZoomOutKey))
{
var zoomLevel = MapAssistConfiguration.Loaded.RenderingConfiguration.ZoomLevel;
if (MapAssistConfiguration.Loaded.RenderingConfiguration.ZoomLevel < 4f)
{
MapAssistConfiguration.Loaded.RenderingConfiguration.ZoomLevel += zoomLevel >= 1 ? 0.2 : 0.1;
MapAssistConfiguration.Loaded.RenderingConfiguration.Size -=
(int)(MapAssistConfiguration.Loaded.RenderingConfiguration.InitialSize * 0.05f);
}
}
if (keys == new Hotkey(MapAssistConfiguration.Loaded.HotkeyConfiguration.ExportItemsKey))
{
if (InGame())
{
ItemExport.ExportPlayerInventory(_gameData.PlayerUnit, _gameData.AllItems);
}
}
}
}
/// <summary>
/// Resize overlay to currently active screen
/// </summary>
private void UpdateLocation()
{
var rect = WindowRect();
var ultraWideMargin = UltraWideMargin();
_window.Resize((int)(rect.Left + ultraWideMargin), (int)rect.Top, (int)(rect.Right - rect.Left - ultraWideMargin * 2), (int)(rect.Bottom - rect.Top));
_window.PlaceAbove(_gameData.MainWindowHandle);
}
private Rectangle WindowRect()
{
WindowBounds rect;
WindowHelper.GetWindowClientBounds(_gameData.MainWindowHandle, out rect);
return new Rectangle(rect.Left, rect.Top, rect.Right, rect.Bottom);
}
private float UltraWideMargin()
{
var rect = WindowRect();
return (float)Math.Max(Math.Round(((rect.Width + 2) - (rect.Height + 4) * 2.1f) / 2f), 0);
}
private float PlayerIconWidth()
{
var rect = WindowRect();
return rect.Height / 20f;
}
private float GameInfoPadding()
{
var rect = WindowRect();
return rect.Height / 100f;
}
private Point GameInfoAnchor(GameInfoPosition position)
{
switch (position)
{
case GameInfoPosition.TopLeft:
var margin = _window.Height / 18f;
return new Point(PlayerIconWidth() + margin, PlayerIconWidth() + margin);
case GameInfoPosition.TopRight:
var rightMargin = _window.Width / 60f;
var topMargin = _window.Height / 35f;
return new Point(_window.Width - rightMargin, topMargin);
}
return new Point();
}
private void _window_DestroyGraphics(object sender, DestroyGraphicsEventArgs e)
{
if (_compositor != null) _compositor.Dispose();
_compositor = null;
}
~Overlay() => Dispose();
private bool disposed = false;
public void Dispose()
{
lock (_lock)
{
if (!disposed)
{
disposed = true; // This first to let GraphicsWindow.DrawGraphics know to return instantly
_window.Dispose(); // This second to dispose of GraphicsWindow
if (_compositor != null) _compositor.Dispose(); // This last so it's disposed after GraphicsWindow stops using it
}
}
}
}
}