forked from paradoxial/D2RAssist
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOverlay.cs
389 lines (329 loc) · 16.5 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
using GameOverlay.Drawing;
using GameOverlay.Windows;
using MapAssist.Helpers;
using MapAssist.Settings;
using MapAssist.Structs;
using MapAssist.Types;
using System;
using System.Linq;
using System.Windows.Forms;
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 DCTrack _dcTrack;
private Compositor _compositor = new Compositor();
private Point _mouseRelativePos;
private static ConfigEditor _configEditor;
private (MapPosition, bool) _lastMapConfiguration;
private bool _show = true;
private static readonly object _lock = new object();
private bool frameDone = true;
public Overlay(ConfigEditor configEditor)
{
_configEditor = configEditor;
_gameDataReader = new GameDataReader();
_dcTrack = new DCTrack();
GameOverlay.TimerService.EnableHighPrecisionTimers();
var gfx = new Graphics() { MeasureFPS = true };
gfx.PerPrimitiveAntiAliasing = true;
gfx.TextAntiAliasing = true;
_window = new GraphicsWindow(0, 0, 1, 1, gfx) { FPS = MapAssistConfiguration.Loaded.RenderingConfiguration.FPS, IsVisible = true };
_window.DrawGraphics += _window_DrawGraphics;
_window.DestroyGraphics += _window_DestroyGraphics;
}
private void _window_DrawGraphics(object sender, DrawGraphicsEventArgs e)
{
if (disposed) return;
if (!frameDone) return;
frameDone = false;
var gfx = e.Graphics;
try
{
lock (_lock)
{
(MapPosition, bool) MapConfiguration()
{
return (MapAssistConfiguration.Loaded.RenderingConfiguration.Position, MapAssistConfiguration.Loaded.RenderingConfiguration.OverlayMode);
}
var (gameData, areaData, changed) = _gameDataReader.Get();
_gameData = gameData;
if (changed || _lastMapConfiguration != MapConfiguration())
{
_compositor.SetArea(areaData);
_lastMapConfiguration = MapConfiguration();
}
gfx.ClearScene();
if (_compositor != null && _gameData != null && InGame())
{
UpdateLocation();
if (gfx.Width > 0 && gfx.Height > 0)
{
var errorLoadingAreaData = _compositor._areaData == null;
var overlayHidden = !_show ||
errorLoadingAreaData ||
(MapAssistConfiguration.Loaded.RenderingConfiguration.ToggleViaInGameMap && !_gameData.MenuOpen.Map) ||
(MapAssistConfiguration.Loaded.RenderingConfiguration.ToggleViaInGamePanels && _gameData.MenuOpen.IsAnyMenuOpen()) ||
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 height = MapAssistConfiguration.Loaded.RenderingConfiguration.OverlayMode ? size / 2 : size;
var x_offset = MapAssistConfiguration.Loaded.RenderingConfiguration.PositionX;
var y_offset = MapAssistConfiguration.Loaded.RenderingConfiguration.PositionY;
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() + 60, 0, PlayerIconWidth() + 60 + height); // Right will be reset inside compositor
drawBounds.Left += x_offset;
drawBounds.Top += y_offset;
break;
case MapPosition.TopRight:
drawBounds = new Rectangle(0, PlayerIconWidth() + 60, gfx.Width, PlayerIconWidth() + 60 + height); // Left will be reset inside compositor
drawBounds.Right += x_offset;
drawBounds.Top += y_offset;
break;
}
_compositor.Init(gfx, _gameData, _dcTrack, drawBounds);
if (!overlayHidden)
{
if (!errorLoadingAreaData)
{
_compositor.DrawGamemap(gfx);
_compositor.DrawOverlay(gfx);
}
_compositor.DrawBuffs(gfx, _mouseRelativePos);
_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);
if (Program.isPrecompiled) _compositor.DrawWatermark(gfx);
}
}
}
}
catch (Exception ex)
{
_log.Error(ex);
}
frameDone = true;
}
public void Run()
{
_window.Create();
_window.Join();
}
private bool InGame()
{
return _gameData != null && _gameData.MainWindowHandle != IntPtr.Zero;
}
public void MouseMoveHandler(object sender, MouseEventArgs args)
{
if (GameManager.IsGameInForeground && InGame())
{
_mouseRelativePos = new Point(args.X - _window.X, args.Y - _window.Y);
}
}
public void KeyDownHandler(object sender, KeyEventArgs args)
{
var keys = new Hotkey(args.Modifiers, args.KeyCode);
if (GameManager.IsGameInForeground && (_gameData == null || !_gameData.MenuOpen.Chat))
{
if (InGame())
{
if (keys == new Hotkey(MapAssistConfiguration.Loaded.HotkeyConfiguration.ToggleKey))
{
MapAssistConfiguration.Loaded.RenderingConfiguration.Offset = new Point(0, 0);
_show = !_show;
}
else if (keys == new Hotkey(MapAssistConfiguration.Loaded.HotkeyConfiguration.HideMapKey))
{
_show = false;
}
else if (keys == new Hotkey(MapAssistConfiguration.Loaded.HotkeyConfiguration.MapPositionsKey))
{
MapAssistConfiguration.Loaded.RenderingConfiguration.Offset = new Point(0, 0);
var position = MapAssistConfiguration.Loaded.RenderingConfiguration.Position;
MapAssistConfiguration.Loaded.RenderingConfiguration.Position = Enum.GetValues(typeof(MapPosition))
.Cast<MapPosition>().Concat(new[] { default(MapPosition) })
.SkipWhile(e => !position.Equals(e)).Skip(1).First();
}
else 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);
}
}
else 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);
}
}
else if (keys == new Hotkey(MapAssistConfiguration.Loaded.HotkeyConfiguration.ExportItemsKey))
{
ItemExport.ExportPlayerInventory(_gameData.PlayerUnit, _gameData.AllItems);
}
var offsetMoveBy = 32; // Brute forced to match in game movements
if (keys == new Hotkey("Up"))
{
MapAssistConfiguration.Loaded.RenderingConfiguration.Offset = MapAssistConfiguration.Loaded.RenderingConfiguration.Offset.Add(0, offsetMoveBy);
}
else if (keys == new Hotkey("Down"))
{
MapAssistConfiguration.Loaded.RenderingConfiguration.Offset = MapAssistConfiguration.Loaded.RenderingConfiguration.Offset.Add(0, -offsetMoveBy);
}
else if (keys == new Hotkey("Left"))
{
MapAssistConfiguration.Loaded.RenderingConfiguration.Offset = MapAssistConfiguration.Loaded.RenderingConfiguration.Offset.Add(offsetMoveBy, 0);
}
else if (keys == new Hotkey("Right"))
{
MapAssistConfiguration.Loaded.RenderingConfiguration.Offset = MapAssistConfiguration.Loaded.RenderingConfiguration.Offset.Add(-offsetMoveBy, 0);
}
}
if (keys == new Hotkey(MapAssistConfiguration.Loaded.HotkeyConfiguration.ToggleConfigKey))
{
if (_configEditor.Visible)
{
_configEditor.Close();
}
else
{
_configEditor.ShowDialog();
}
}
if (keys == new Hotkey(MapAssistConfiguration.Loaded.HotkeyConfiguration.ShareRoomKey))
{
if (InGame())
{
Clipboard.SetText("我在房间:" + _gameData.Session.GameName + " 密码:" + _gameData.Session.GamePass);
}
}
if (keys == new Hotkey(MapAssistConfiguration.Loaded.HC.ESCKey))
{
try
{
WindowsExternal.SetForegroundWindow(_gameData.MainWindowHandle);
WindowsExternal.SendEscapeKey(_gameData.MainWindowHandle);
var windowRect = WindowsExternal.GetWindowRect(_gameData.MainWindowHandle);
var width = windowRect.Right - windowRect.Left;
var height = windowRect.Bottom - windowRect.Top;
WindowsExternal.LeftMouseClick(windowRect.Left + width / 2, windowRect.Top + height / 20 * 9);
}
catch
{
}
}
}
if (keys == new Hotkey(MapAssistConfiguration.Loaded.HotkeyConfiguration.CopyRoomKey))
{
Clipboard.SetText(_gameData.Session.GameName);
}
// out of game
if (keys == new Hotkey(MapAssistConfiguration.Loaded.HotkeyConfiguration.AddRoomKey))
{
var roomName = MapAssistConfiguration.Loaded.HotkeyConfiguration.RoomTemplete;
var head = roomName.Split('[')[0];
var tail = roomName.Split(']')[1];
var num = int.Parse(roomName.Split('[')[1].Split(']')[0]) + 1;
roomName = head + num.ToString() + tail;
var roomTemplete = head + "[" + num.ToString() + "]" + tail;
try
{
Clipboard.SetText(roomName);
WindowsExternal.SendPasteKey(GameManager.MainWindowHandle);
MapAssistConfiguration.Loaded.HotkeyConfiguration.RoomTemplete = roomTemplete;
}
catch
{
_log.Error("copy and paste failed");
}
}
}
/// <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
}
}
}
}
}