-
Notifications
You must be signed in to change notification settings - Fork 4
/
DrawRadial.cs
167 lines (143 loc) · 6.36 KB
/
DrawRadial.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
using Blish_HUD;
using Blish_HUD.Controls;
using Blish_HUD.Controls.Intern;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Manlaan.Mounts.Controls
{
internal class RadialMount
{
public double AngleBegin { get; set; }
public double AngleEnd { get; set; }
public Mount Mount { get; set; }
public Texture2D Texture { get; set; }
public int ImageX { get; set; }
public int ImageY { get; set; }
public bool Selected { get; set; }
public bool Default { get; internal set; }
}
internal class DrawRadial : Control
{
private static readonly Logger Logger = Logger.GetLogger<DrawRadial>();
private readonly Helper _helper;
private List<RadialMount> RadialMounts = new List<RadialMount>();
private RadialMount SelectedMount => RadialMounts.SingleOrDefault(m => m.Selected);
public override int ZIndex { get => base.ZIndex; set => base.ZIndex = value; }
public bool IsActionCamToggledOnMount { get; private set; }
int radius = 0;
int mountIconSize = 0;
int _maxRadialDiameter = 0;
private Point SpawnPoint = default;
public DrawRadial(Helper helper)
{
Visible = false;
Padding = Thickness.Zero;
_helper = helper;
Shown += async (sender, e) => await HandleShown(sender, e);
Hidden += async (sender, e) => await HandleHidden(sender, e);
}
protected override CaptureType CapturesInput()
{
return CaptureType.Mouse;
}
protected override void Paint(SpriteBatch spriteBatch, Rectangle bounds) {
RadialMounts.Clear();
var mounts = Module._availableOrderedMounts;
Mount mountToPutInCenter = _helper.GetCenterMount();
if (mountToPutInCenter != null && mountToPutInCenter.IsAvailable)
{
if (Module._settingMountRadialRemoveCenterMount.Value)
{
mounts.Remove(mountToPutInCenter);
}
var texture = _helper.GetImgFile(mountToPutInCenter.ImageFileName);
int loc = radius;
RadialMounts.Add(new RadialMount { Texture = texture, Mount = mountToPutInCenter, ImageX = loc, ImageY = loc, Default = true });
}
double currentAngle = 0;
var partAngleStep = Math.PI * 2 / mounts.Count();
foreach (var mount in mounts)
{
var angleMid = currentAngle + partAngleStep / 2;
var angleEnd = currentAngle + partAngleStep;
var texture = _helper.GetImgFile(mount.ImageFileName);
int x = (int)Math.Round(radius + radius * Math.Cos(angleMid));
int y = (int)Math.Round(radius + radius * Math.Sin(angleMid));
RadialMounts.Add(new RadialMount
{
Texture = texture,
Mount = mount,
ImageX = x,
ImageY = y,
AngleBegin = currentAngle,
AngleEnd = angleEnd
});
currentAngle = angleEnd;
}
var mousePos = Input.Mouse.Position;
var diff = mousePos - SpawnPoint;
var angle = Math.Atan2(diff.Y, diff.X);
if (angle < 0)
{
angle += Math.PI * 2;
}
var length = new Vector2(diff.Y, diff.X).Length();
foreach (var radialMount in RadialMounts)
{
if (length < mountIconSize * Math.Sqrt(2) / 2)
{
radialMount.Selected = radialMount.Default;
}
else
{
radialMount.Selected = radialMount.AngleBegin <= angle && radialMount.AngleEnd > angle;
}
spriteBatch.DrawOnCtrl(this, radialMount.Texture, new Rectangle(radialMount.ImageX, radialMount.ImageY, mountIconSize, mountIconSize), null, Color.White * (radialMount.Selected ? 1f : Module._settingMountRadialIconOpacity.Value));
}
}
public async Task TriggerSelectedMountAsync()
{
await (SelectedMount?.Mount.DoMountAction() ?? Task.CompletedTask);
}
private async Task HandleShown(object sender, EventArgs e)
{
Logger.Debug("HandleShown entered");
var isCursorVisible = GameService.Input.Mouse.CursorIsVisible;
if (!isCursorVisible)
{
IsActionCamToggledOnMount = true;
await _helper.TriggerKeybind(Module._settingMountRadialToggleActionCameraKeyBinding);
Logger.Debug("HandleShown turned off action cam");
}
_maxRadialDiameter = Math.Min(GameService.Graphics.SpriteScreen.Width, GameService.Graphics.SpriteScreen.Height);
mountIconSize = (int)(_maxRadialDiameter / 4 * Module._settingMountRadialIconSizeModifier.Value);
radius = (int)((_maxRadialDiameter / 2 - mountIconSize / 2) * Module._settingMountRadialRadiusModifier.Value);
Size = new Point(_maxRadialDiameter, _maxRadialDiameter);
if (Module._settingMountRadialSpawnAtMouse.Value && isCursorVisible)
{
SpawnPoint = Input.Mouse.Position;
}
else
{
Mouse.SetPosition(GameService.Graphics.WindowWidth / 2, GameService.Graphics.WindowHeight / 2, true);
SpawnPoint = new Point(GameService.Graphics.SpriteScreen.Width / 2, GameService.Graphics.SpriteScreen.Height / 2);
}
Location = new Point(SpawnPoint.X - radius - mountIconSize / 2, SpawnPoint.Y - radius - mountIconSize / 2);
}
private async Task HandleHidden(object sender, EventArgs e)
{
Logger.Debug("HandleHidden entered");
if (IsActionCamToggledOnMount)
{
await _helper.TriggerKeybind(Module._settingMountRadialToggleActionCameraKeyBinding);
IsActionCamToggledOnMount = false;
Logger.Debug("HandleHidden turned back on action cam");
}
await TriggerSelectedMountAsync();
}
}
}