1
0
forked from liza/Questionable
Questionable/GatheringPathRenderer/Windows/EditorWindow.cs

266 lines
10 KiB
C#
Raw Normal View History

2024-08-03 01:21:11 +00:00
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Numerics;
using Dalamud.Game.ClientState.Objects;
using Dalamud.Game.ClientState.Objects.Enums;
using Dalamud.Game.ClientState.Objects.Types;
2024-08-03 15:26:49 +00:00
using Dalamud.Interface.Colors;
2024-08-03 01:21:11 +00:00
using Dalamud.Interface.Windowing;
using Dalamud.Plugin.Services;
using ImGuiNET;
2024-11-19 14:57:15 +00:00
using Lumina.Excel.Sheets;
2024-08-03 01:21:11 +00:00
using Questionable.Model.Gathering;
namespace GatheringPathRenderer.Windows;
internal sealed class EditorWindow : Window
{
private readonly RendererPlugin _plugin;
private readonly EditorCommands _editorCommands;
private readonly IDataManager _dataManager;
private readonly ITargetManager _targetManager;
private readonly IClientState _clientState;
2024-08-03 09:17:20 +00:00
private readonly IObjectTable _objectTable;
2024-08-03 01:21:11 +00:00
private readonly Dictionary<Guid, LocationOverride> _changes = [];
private IGameObject? _target;
2024-08-03 09:17:20 +00:00
private (RendererPlugin.GatheringLocationContext Context, GatheringNode Node, GatheringLocation Location)?
_targetLocation;
2024-08-03 01:21:11 +00:00
public EditorWindow(RendererPlugin plugin, EditorCommands editorCommands, IDataManager dataManager,
2024-08-03 09:17:20 +00:00
ITargetManager targetManager, IClientState clientState, IObjectTable objectTable)
: base("Gathering Path Editor###QuestionableGatheringPathEditor",
ImGuiWindowFlags.NoFocusOnAppearing | ImGuiWindowFlags.NoNavFocus | ImGuiWindowFlags.AlwaysAutoResize)
2024-08-03 01:21:11 +00:00
{
_plugin = plugin;
_editorCommands = editorCommands;
_dataManager = dataManager;
_targetManager = targetManager;
_clientState = clientState;
2024-08-03 09:17:20 +00:00
_objectTable = objectTable;
2024-08-03 01:21:11 +00:00
SizeConstraints = new WindowSizeConstraints
{
MinimumSize = new Vector2(300, 100),
2024-08-03 01:21:11 +00:00
};
RespectCloseHotkey = false;
2024-08-03 09:17:20 +00:00
ShowCloseButton = false;
AllowPinning = false;
AllowClickthrough = false;
2024-08-03 01:21:11 +00:00
}
public override void Update()
{
if (!_clientState.IsLoggedIn || _clientState.LocalPlayer == null)
{
_target = null;
_targetLocation = null;
return;
}
2024-08-03 01:21:11 +00:00
_target = _targetManager.Target;
var gatheringLocations = _plugin.GetLocationsInTerritory(_clientState.TerritoryType);
2024-09-19 23:46:13 +00:00
var location = gatheringLocations.ToList().SelectMany(context =>
2024-08-03 01:21:11 +00:00
context.Root.Groups.SelectMany(group =>
group.Nodes.SelectMany(node => node.Locations
.Select(location =>
{
float distance;
if (_target != null)
distance = Vector3.Distance(location.Position, _target.Position);
else
distance = Vector3.Distance(location.Position, _clientState.LocalPlayer.Position);
return new { Context = context, Node = node, Location = location, Distance = distance };
})
.Where(location => location.Distance < (_target == null ? 3f : 0.1f)))))
.MinBy(x => x.Distance);
2024-08-03 15:26:49 +00:00
if (_target != null && _target.ObjectKind != ObjectKind.GatheringPoint)
2024-08-03 01:21:11 +00:00
{
2024-08-03 09:17:20 +00:00
_target = null;
2024-08-03 01:21:11 +00:00
_targetLocation = null;
return;
}
2024-08-03 15:26:49 +00:00
if (location == null)
{
_targetLocation = null;
return;
}
2024-08-11 19:22:19 +00:00
_target ??= _objectTable
.Where(x => x.ObjectKind == ObjectKind.GatheringPoint && x.DataId == location.Node.DataId)
.Select(x => new
{
Object = x,
Distance = Vector3.Distance(location.Location.Position, _clientState.LocalPlayer.Position)
})
.Where(x => x.Distance < 3f)
.OrderBy(x => x.Distance)
.Select(x => x.Object)
.FirstOrDefault();
2024-08-03 09:17:20 +00:00
_targetLocation = (location.Context, location.Node, location.Location);
2024-08-03 01:21:11 +00:00
}
public override bool DrawConditions()
{
return _target != null || _targetLocation != null;
}
public override void Draw()
{
if (_target != null && _targetLocation != null)
{
2024-08-03 09:17:20 +00:00
var context = _targetLocation.Value.Context;
var node = _targetLocation.Value.Node;
var location = _targetLocation.Value.Location;
2024-08-03 01:21:11 +00:00
ImGui.Text(context.File.Directory?.Name ?? string.Empty);
ImGui.Indent();
ImGui.Text(context.File.Name);
ImGui.Unindent();
ImGui.Text(
$"{_target.DataId} +{node.Locations.Count - 1} / {location.InternalId.ToString().Substring(0, 4)}");
2024-08-03 01:21:11 +00:00
ImGui.Text(string.Create(CultureInfo.InvariantCulture, $"{location.Position:G}"));
if (!_changes.TryGetValue(location.InternalId, out LocationOverride? locationOverride))
{
locationOverride = new LocationOverride();
_changes[location.InternalId] = locationOverride;
}
int minAngle = locationOverride.MinimumAngle ?? location.MinimumAngle.GetValueOrDefault();
2024-08-11 19:22:19 +00:00
int maxAngle = locationOverride.MaximumAngle ?? location.MaximumAngle.GetValueOrDefault();
if (ImGui.DragIntRange2("Angle", ref minAngle, ref maxAngle, 5, -360, 360))
2024-08-03 01:21:11 +00:00
{
locationOverride.MinimumAngle = minAngle;
2024-08-11 19:22:19 +00:00
locationOverride.MaximumAngle = maxAngle;
2024-08-03 01:21:11 +00:00
_plugin.Redraw();
}
2024-08-11 19:22:19 +00:00
float minDistance = locationOverride.MinimumDistance ?? location.CalculateMinimumDistance();
float maxDistance = locationOverride.MaximumDistance ?? location.CalculateMaximumDistance();
if (ImGui.DragFloatRange2("Distance", ref minDistance, ref maxDistance, 0.1f, 1f, 3f))
2024-08-03 01:21:11 +00:00
{
2024-08-11 19:22:19 +00:00
locationOverride.MinimumDistance = minDistance;
locationOverride.MaximumDistance = maxDistance;
2024-08-03 01:21:11 +00:00
_plugin.Redraw();
}
bool unsaved = locationOverride.NeedsSave();
2024-08-03 15:26:49 +00:00
ImGui.BeginDisabled(!unsaved);
if (unsaved)
ImGui.PushStyleColor(ImGuiCol.Button, ImGuiColors.DalamudRed);
2024-08-03 01:21:11 +00:00
if (ImGui.Button("Save"))
{
2024-08-11 19:22:19 +00:00
if (locationOverride is { MinimumAngle: not null, MaximumAngle: not null })
{
location.MinimumAngle = locationOverride.MinimumAngle ?? location.MinimumAngle;
location.MaximumAngle = locationOverride.MaximumAngle ?? location.MaximumAngle;
}
if (locationOverride is { MinimumDistance: not null, MaximumDistance: not null })
{
location.MinimumDistance = locationOverride.MinimumDistance;
location.MaximumDistance = locationOverride.MaximumDistance;
}
2024-08-03 01:21:11 +00:00
_plugin.Save(context.File, context.Root);
}
2024-08-03 15:26:49 +00:00
if (unsaved)
ImGui.PopStyleColor();
2024-08-03 09:17:20 +00:00
2024-08-03 01:21:11 +00:00
ImGui.SameLine();
if (ImGui.Button("Reset"))
{
_changes[location.InternalId] = new LocationOverride();
_plugin.Redraw();
}
2024-08-03 09:17:20 +00:00
2024-08-03 01:21:11 +00:00
ImGui.EndDisabled();
2024-08-03 09:17:20 +00:00
List<IGameObject> nodesInObjectTable = _objectTable
.Where(x => x.ObjectKind == ObjectKind.GatheringPoint && x.DataId == _target.DataId)
.ToList();
List<IGameObject> missingLocations = nodesInObjectTable
.Where(x => !node.Locations.Any(y => Vector3.Distance(x.Position, y.Position) < 0.1f))
.ToList();
if (missingLocations.Count > 0)
{
if (ImGui.Button("Add missing locations"))
{
foreach (var missing in missingLocations)
_editorCommands.AddToExistingGroup(context.Root, missing);
_plugin.Save(context.File, context.Root);
}
}
2024-08-03 01:21:11 +00:00
}
else if (_target != null)
{
2024-11-19 14:57:15 +00:00
var gatheringPoint = _dataManager.GetExcelSheet<GatheringPoint>().GetRowOrDefault(_target.DataId);
2024-08-03 01:21:11 +00:00
if (gatheringPoint == null)
return;
var locationsInTerritory = _plugin.GetLocationsInTerritory(_clientState.TerritoryType).ToList();
2024-11-19 14:57:15 +00:00
var location = locationsInTerritory.SingleOrDefault(x => x.Id == gatheringPoint.Value.GatheringPointBase.RowId);
2024-08-03 01:21:11 +00:00
if (location != null)
{
var targetFile = location.File;
var root = location.Root;
if (ImGui.Button("Add to closest group"))
{
_editorCommands.AddToExistingGroup(root, _target);
_plugin.Save(targetFile, root);
}
ImGui.BeginDisabled(root.Groups.Any(group => group.Nodes.Any(node => node.DataId == _target.DataId)));
ImGui.SameLine();
if (ImGui.Button("Add as new group"))
{
_editorCommands.AddToNewGroup(root, _target);
_plugin.Save(targetFile, root);
}
2024-08-03 09:17:20 +00:00
2024-08-03 01:21:11 +00:00
ImGui.EndDisabled();
}
else
{
2024-11-19 14:57:15 +00:00
if (ImGui.Button($"Create location ({gatheringPoint.Value.GatheringPointBase.RowId})"))
2024-08-03 01:21:11 +00:00
{
2024-11-19 14:57:15 +00:00
var (targetFile, root) = _editorCommands.CreateNewFile(gatheringPoint.Value, _target);
2024-08-03 01:21:11 +00:00
_plugin.Save(targetFile, root);
}
}
}
}
public bool TryGetOverride(Guid internalId, out LocationOverride? locationOverride)
=> _changes.TryGetValue(internalId, out locationOverride);
}
2024-08-03 09:17:20 +00:00
internal sealed class LocationOverride
2024-08-03 01:21:11 +00:00
{
public int? MinimumAngle { get; set; }
public int? MaximumAngle { get; set; }
public float? MinimumDistance { get; set; }
public float? MaximumDistance { get; set; }
public bool IsCone()
{
return MinimumAngle != null && MaximumAngle != null && MinimumAngle != MaximumAngle;
}
public bool NeedsSave()
{
2024-08-11 19:22:19 +00:00
return (MinimumAngle != null && MaximumAngle != null) || (MinimumDistance != null && MaximumDistance != null);
}
2024-08-03 01:21:11 +00:00
}