Files
Spectrum/Spectrum/Renderer/Renderer.MovementPreviewWindow.cs
T

191 lines
8.5 KiB
C#

using ImGuiNET;
using Spectrum.Input;
using System.Numerics;
namespace Spectrum
{
partial class Renderer
{
private bool _showMovementPreview = false;
private Point _previewStartPoint = new(100, 100);
private Point _previewEndPoint = new(400, 300);
private readonly List<(Point point, double speed)> _previewPath = [];
private int UpdatePreviewInterval = 10; // ms
private DateTime _lastPreviewUpdate = DateTime.MinValue;
private bool _isDragging = false;
private bool _isMovingStart = false;
private void RenderMovementPreview()
{
if (!_showMovementPreview)
return;
var config = mainConfig.Data;
ImGui.SetNextWindowSize(new Vector2(500, 500), ImGuiCond.Always);
ImGui.SetNextWindowPos(new Vector2(screenSize.width / 2 - 300, screenSize.height / 2 - 250), ImGuiCond.FirstUseEver);
if (!ImGui.Begin("Movement Path Preview", ref _showMovementPreview, ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoDocking | ImGuiWindowFlags.NoTitleBar))
{
ImGui.End();
return;
}
ImGui.TextUnformatted("Movement Path Preview");
ImGui.SameLine(ImGui.GetContentRegionAvail().X - 14);
if (ImGui.Button("X", new Vector2(22, 22)))
{
_showMovementPreview = false;
ImGui.End();
return;
}
var boundingBox = new Rectangle(_previewEndPoint.X - 20, _previewEndPoint.Y - 40, 40, 80);
if ((DateTime.Now - _lastPreviewUpdate).TotalMilliseconds > UpdatePreviewInterval)
{
_lastPreviewUpdate = DateTime.Now;
_previewPath.Clear();
MovementPaths.ResetWindMouse();
Point current = _previewStartPoint;
int steps = 0;
int maxSteps = 10000;
_previewPath.Add((current, 0.0));
while (true)
{
double distance = Math.Sqrt(Math.Pow(_previewEndPoint.X - current.X, 2) + Math.Pow(_previewEndPoint.Y - current.Y, 2));
if (distance < 2.0 || steps++ >= maxSteps)
break;
bool isInBox = boundingBox.Contains(current);
Point nextPoint = InputManager.CalculateMovement(current, _previewEndPoint, config.Sensitivity, config.AimMovementType, isInBox);
if (config.EmaSmoothening)
nextPoint = MovementPaths.EmaSmoothing(current, nextPoint, config.EmaSmootheningFactor);
double speed = Math.Sqrt(Math.Pow(nextPoint.X - current.X, 2) + Math.Pow(nextPoint.Y - current.Y, 2));
_previewPath.Add((nextPoint, speed));
current = nextPoint;
}
if (_previewPath.Count > 0)
{
var lastPoint = _previewPath[^1].point;
double finalDistance = Math.Sqrt(Math.Pow(_previewEndPoint.X - lastPoint.X, 2) + Math.Pow(_previewEndPoint.Y - lastPoint.Y, 2));
if (finalDistance > 2.0)
_previewPath.Add((_previewEndPoint, finalDistance));
}
}
var drawList = ImGui.GetWindowDrawList();
Vector2 canvasPos = ImGui.GetCursorScreenPos();
canvasPos.Y += 5;
Vector2 canvasSize = ImGui.GetContentRegionAvail();
Vector2 size = new(canvasSize.X, canvasSize.Y - 60);
drawList.AddRectFilled(canvasPos, size + canvasPos, ImGui.GetColorU32(new Vector4(0.12f, 0.12f, 0.14f, 1.0f)), 3f);
drawList.AddRect(canvasPos, size + canvasPos, ImGui.GetColorU32(new Vector4(0.17f, 0.17f, 0.20f, 1.0f)), 3f);
drawList.PushClipRect(canvasPos, canvasPos + size, true);
drawList.AddRect(
new Vector2(boundingBox.X + canvasPos.X, boundingBox.Top + canvasPos.Y),
new Vector2(boundingBox.X + boundingBox.Width + canvasPos.X, boundingBox.Y + boundingBox.Height + canvasPos.Y),
ImGui.GetColorU32(new Vector4(0, 0, 1, 1)), 0.0f, ImDrawFlags.None, 2.0f);
if (_previewPath.Count > 1)
{
double maxSpeed = _previewPath.Max(p => p.speed);
for (int i = 1; i < _previewPath.Count; i++)
{
var (prevPoint, prevSpeed) = _previewPath[i - 1];
var (currPoint, currSpeed) = _previewPath[i];
Vector2 p1 = new Vector2(canvasPos.X + prevPoint.X, canvasPos.Y + prevPoint.Y);
Vector2 p2 = new Vector2(canvasPos.X + currPoint.X, canvasPos.Y + currPoint.Y);
double avgSpeed = (prevSpeed + currSpeed) / 2.0;
float normalizedSpeed = (float)(avgSpeed / maxSpeed);
Vector4 color;
if (normalizedSpeed < 0.33f) // green -> yellow
color = Vector4.Lerp(new Vector4(0, 1, 0, 1), new Vector4(1, 1, 0, 1), normalizedSpeed / 0.33f);
else if (normalizedSpeed < 0.66f) // yellow -> orange
color = Vector4.Lerp(new Vector4(1, 1, 0, 1), new Vector4(1, 0.5f, 0, 1), (normalizedSpeed - 0.33f) / 0.33f);
else // orange -> red
color = Vector4.Lerp(new Vector4(1, 0.5f, 0, 1), new Vector4(1, 0, 0, 1), (normalizedSpeed - 0.66f) / 0.34f);
drawList.AddLine(p1, p2, ImGui.GetColorU32(color), 2.0f);
}
}
drawList.AddCircleFilled(new Vector2(canvasPos.X + _previewStartPoint.X, canvasPos.Y + _previewStartPoint.Y), 5.0f, ImGui.GetColorU32(new Vector4(0, 1, 0, 1)));
drawList.AddCircleFilled(new Vector2(canvasPos.X + _previewEndPoint.X, canvasPos.Y + _previewEndPoint.Y), 5.0f, ImGui.GetColorU32(new Vector4(1, 0, 0, 1)));
drawList.PopClipRect();
ImGui.InvisibleButton("canvas", size);
var io = ImGui.GetIO();
var mousePos = io.MousePos;
bool isMouseInCanvas = mousePos.X >= canvasPos.X && mousePos.X <= canvasPos.X + size.X &&
mousePos.Y >= canvasPos.Y && mousePos.Y <= canvasPos.Y + size.Y;
bool isMouseDown = ImGui.IsMouseDown(ImGuiMouseButton.Left);
bool isMouseClicked = ImGui.IsMouseClicked(ImGuiMouseButton.Left);
bool isMouseReleased = ImGui.IsMouseReleased(ImGuiMouseButton.Left);
if (!_isDragging && isMouseInCanvas && isMouseClicked)
{
Vector2 localMousePos = mousePos - canvasPos;
float distToStart = Vector2.Distance(localMousePos, new Vector2(_previewStartPoint.X, _previewStartPoint.Y));
float distToEnd = Vector2.Distance(localMousePos, new Vector2(_previewEndPoint.X, _previewEndPoint.Y));
if (distToStart < 10.0f)
{
_isMovingStart = true;
_isDragging = true;
}
else if (distToEnd < 10.0f)
{
_isMovingStart = false;
_isDragging = true;
}
else
{
_isDragging = false;
_isMovingStart = false;
}
}
if (_isDragging && isMouseDown)
{
Vector2 localMousePos = mousePos - canvasPos;
if (localMousePos.X < 15) localMousePos.X = 15;
if (localMousePos.Y < 15) localMousePos.Y = 15;
if (localMousePos.X > size.X - 15) localMousePos.X = size.X - 15;
if (localMousePos.Y > size.Y - 15) localMousePos.Y = size.Y - 15;
if (_isMovingStart)
{
_previewStartPoint = new Point((int)localMousePos.X, (int)localMousePos.Y);
}
else
{
_previewEndPoint = new Point((int)localMousePos.X, (int)localMousePos.Y);
}
}
if (_isDragging && isMouseReleased)
{
_isDragging = false;
}
ImGui.Dummy(new(ImGui.GetContentRegionAvail().X, 10));
int interval = UpdatePreviewInterval;
if (ImGuiExtensions.SliderFill("Update Interval (ms)", ref interval, 1, 100))
UpdatePreviewInterval = interval;
ImGui.End();
}
}
}