-
Notifications
You must be signed in to change notification settings - Fork 1
/
ChatOverlay.xaml.cs
166 lines (151 loc) · 5.89 KB
/
ChatOverlay.xaml.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
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Threading;
using Clipboard = System.Windows.Forms.Clipboard;
namespace ChatWheel
{
public partial class ChatOverlay : Window
{
private readonly Settings _settings;
private readonly string[] buttonColors =
{
"#39A7F2",
"#648E06",
"#FE8B1E",
"#C4771B",
"#EA5600",
"#6736C6",
"#3E5066",
"#39A7F2",
"#648E06",
"#FE8B1E"
};
private readonly DispatcherTimer hoverTimer = new DispatcherTimer();
private Point _chatWheelCenterLocation;
private PiePiece _lastChosenPie;
private Point _previousValidPos;
public ChatOverlay(Settings settings)
{
_settings = settings;
InitializeComponent();
var screen = Screen.PrimaryScreen.WorkingArea;
Left = screen.Width/2 - Width/2;
Top = screen.Height/2 - Height/2;
Canvas.SetLeft(elpsChatWheel, Width/2 - elpsChatWheel.Width/2);
Canvas.SetTop(elpsChatWheel, Height/2 - elpsChatWheel.Height/2);
_chatWheelCenterLocation = new Point(Canvas.GetLeft(elpsChatWheel) + elpsChatWheel.Width/2,
Canvas.GetTop(elpsChatWheel) + elpsChatWheel.Height/2);
hoverTimer.Tick += hoverTimer_Tick;
hoverTimer.Interval = new TimeSpan(0, 0, 0, 0, 10);
Visibility = Visibility.Hidden;
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
User32.SetWindowExTransparent(new WindowInteropHelper(this).Handle);
}
/// <summary>
/// Handles keypresses, determines what element is being highlighted
/// and send the selected element to the game window
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void hoverTimer_Tick(object sender, EventArgs e)
{
if (!Keyboard.GetKeyStates((Key) _settings.HotKey).HasFlag(KeyStates.Down))
{
if (Visibility != Visibility.Hidden)
{
//Selection has ended. Now acting
Visibility = Visibility.Hidden;
if (_lastChosenPie == null) return;
_lastChosenPie.ReactToMouseLeave();
SendKeys.SendWait("{ENTER}");
Clipboard.SetText(_lastChosenPie.FullText);
SendKeys.SendWait("^v");
SendKeys.SendWait("{ENTER}");
}
return;
}
if (Visibility == Visibility.Hidden)
{
Visibility = Visibility.Visible;
User32.SetCursorPosition(Left + _chatWheelCenterLocation.X, Top + _chatWheelCenterLocation.Y);
}
//TODO: Clean up this area
var mousePos = User32.GetMousePosition();
var mouseOffseted = mousePos;
mouseOffseted.Offset(Left*-1, Top*-1);
Console.WriteLine(mouseOffseted);
if (_previousValidPos.X == 0)
_previousValidPos = _chatWheelCenterLocation;
//Recenter if too far away
var distance = Utils.Distance2D(_chatWheelCenterLocation, mouseOffseted);
if (distance > 90)
{
User32.SetCursorPosition((int) _previousValidPos.X, (int) _previousValidPos.Y);
}
else
{
_previousValidPos = mousePos;
}
//Ignore nodes if mouse is in the center of the circle
if (distance < 30)
{
if (_lastChosenPie != null)
_lastChosenPie.ReactToMouseLeave();
_lastChosenPie = null;
return;
}
//Todo: optimize by generating a list of piepieces
foreach (var obj in chatWheelCanvas.Children)
{
if ((obj.GetType() != typeof (PiePiece))) continue;
var pie = obj as PiePiece;
if (pie.IsAngleOnControl(
Utils.FindAngleBetweenPoints
(new Point(_chatWheelCenterLocation.X, 0),
_chatWheelCenterLocation, mouseOffseted)))
{
pie.ReactToMouseEnter();
_lastChosenPie = pie;
}
else
{
pie.ReactToMouseLeave();
}
}
}
/// <summary>
/// Recreates and redraws all pie pieces
/// This could be greatly optimized.
/// </summary>
public void UpdateChatWheel()
{
hoverTimer.Stop();
chatWheelCanvas.Children.RemoveRange(1, chatWheelCanvas.Children.Count - 1);
for (var i = 0; i < _settings.PhrasesAmount; i++)
{
var piece = new PiePiece();
piece.FillColor =
new SolidColorBrush((Color) ColorConverter.ConvertFromString(buttonColors[i]));
piece.CenterX = _chatWheelCenterLocation.X;
piece.CenterY = _chatWheelCenterLocation.Y;
piece.RotationAngle = 360.0/_settings.PhrasesAmount*i;
piece.WedgeAngle = 360.0/_settings.PhrasesAmount - 2;
piece.Radius = 110;
piece.InnerRadius = 50;
piece.PieceValue = i;
piece.FullText = _settings.Phrases[i].FullPhrase;
piece.ShortText = _settings.Phrases[i].ShortPhrase;
chatWheelCanvas.Children.Add(piece);
}
hoverTimer.Start();
}
}
}