From c8fed9d278f949cc816b601c7a832be5383a3b86 Mon Sep 17 00:00:00 2001 From: Liza Carvelli Date: Fri, 6 Sep 2024 00:08:43 +0200 Subject: [PATCH] Code cleanup --- .../QuestComponents/CreationUtilsComponent.cs | 295 ++++++++++-------- 1 file changed, 161 insertions(+), 134 deletions(-) diff --git a/Questionable/Windows/QuestComponents/CreationUtilsComponent.cs b/Questionable/Windows/QuestComponents/CreationUtilsComponent.cs index dd095f900..8c7cb09bb 100644 --- a/Questionable/Windows/QuestComponents/CreationUtilsComponent.cs +++ b/Questionable/Windows/QuestComponents/CreationUtilsComponent.cs @@ -4,6 +4,7 @@ using System.Globalization; using System.Numerics; using Dalamud.Game.ClientState.Conditions; using Dalamud.Game.ClientState.Objects; +using Dalamud.Game.ClientState.Objects.Types; using Dalamud.Game.Text; using Dalamud.Interface; using Dalamud.Interface.Colors; @@ -147,144 +148,14 @@ internal sealed class CreationUtilsComponent if (_targetManager.Target != null) { - ImGui.Separator(); - ImGui.Text(string.Create(CultureInfo.InvariantCulture, - $"Target: {_targetManager.Target.Name} ({_targetManager.Target.ObjectKind}; {_targetManager.Target.DataId})")); - - GameObject* gameObject = (GameObject*)_targetManager.Target.Address; - ImGui.Text(string.Create(CultureInfo.InvariantCulture, - $"Distance: {(_targetManager.Target.Position - _clientState.LocalPlayer.Position).Length():F2}")); + DrawTargetDetails(_targetManager.Target); + DrawInteractionButtons(_targetManager.Target); ImGui.SameLine(); - - float verticalDistance = _targetManager.Target.Position.Y - _clientState.LocalPlayer.Position.Y; - string verticalDistanceText = string.Create(CultureInfo.InvariantCulture, $"Y: {verticalDistance:F2}"); - if (Math.Abs(verticalDistance) >= MovementController.DefaultVerticalInteractionDistance) - ImGui.TextColored(ImGuiColors.DalamudOrange, verticalDistanceText); - else - ImGui.Text(verticalDistanceText); - - ImGui.SameLine(); - ImGui.Text($"QM: {gameObject->NamePlateIconId}"); - - ImGui.BeginDisabled(!_movementController.IsNavmeshReady || _gameFunctions.IsOccupied()); - if (!_movementController.IsPathfinding) - { - if (ImGuiComponents.IconButtonWithText(FontAwesomeIcon.Bullseye, "To Target")) - { - _movementController.NavigateTo(EMovementType.DebugWindow, _targetManager.Target.DataId, - _targetManager.Target.Position, - fly: _condition[ConditionFlag.Mounted] && _gameFunctions.IsFlyingUnlockedInCurrentZone(), - sprint: true); - } - } - else - { - if (ImGui.Button("Cancel pathfinding")) - _movementController.ResetPathfinding(); - } - - ImGui.EndDisabled(); - - ImGui.SameLine(); - ImGui.BeginDisabled(!_questData.IsIssuerOfAnyQuest(_targetManager.Target.DataId)); - bool showQuests = ImGuiComponents.IconButton(FontAwesomeIcon.MapMarkerAlt); - if (ImGui.IsItemHovered()) - ImGui.SetTooltip("Show all Quests starting with your current target."); - if (showQuests) - _questSelectionWindow.OpenForTarget(_targetManager.Target); - - ImGui.EndDisabled(); - - ImGui.BeginDisabled(_gameFunctions.IsOccupied()); - ImGui.SameLine(); - bool interact = ImGuiComponents.IconButton(FontAwesomeIcon.MousePointer); - if (ImGui.IsItemHovered()) - ImGui.SetTooltip("Interact with your current target."); - if (interact) - { - ulong result = TargetSystem.Instance()->InteractWithObject( - (GameObject*)_targetManager.Target.Address, false); - _logger.LogInformation("XXXXX Interaction Result: {Result}", result); - } - ImGui.EndDisabled(); - ImGui.SameLine(); - - bool copy = ImGuiComponents.IconButton(FontAwesomeIcon.Copy); - if (ImGui.IsItemHovered()) - ImGui.SetTooltip( - "Left click: Copy target position as JSON.\nRight click: Copy target position as C# code."); - if (copy) - { - var target = _targetManager.Target; - if (target.ObjectKind == ObjectKind.GatheringPoint) - { - ImGui.SetClipboardText($$""" - "DataId": {{target.DataId}}, - "Position": { - "X": {{target.Position.X.ToString(CultureInfo.InvariantCulture)}}, - "Y": {{target.Position.Y.ToString(CultureInfo.InvariantCulture)}}, - "Z": {{target.Position.Z.ToString(CultureInfo.InvariantCulture)}} - } - """); - } - else - { - string interactionType = gameObject->NamePlateIconId switch - { - 71201 or 71211 or 71221 or 71231 or 71341 or 71351 => "AcceptQuest", - 71202 or 71212 or 71222 or 71232 or 71342 or 71352 => "AcceptQuest", // repeatable - 71205 or 71215 or 71225 or 71235 or 71345 or 71355 => "CompleteQuest", - _ => "Interact", - }; - ImGui.SetClipboardText($$""" - "DataId": {{target.DataId}}, - "Position": { - "X": {{target.Position.X.ToString(CultureInfo.InvariantCulture)}}, - "Y": {{target.Position.Y.ToString(CultureInfo.InvariantCulture)}}, - "Z": {{target.Position.Z.ToString(CultureInfo.InvariantCulture)}} - }, - "TerritoryId": {{_clientState.TerritoryType}}, - "InteractionType": "{{interactionType}}" - """); - } - } - else if (ImGui.IsItemClicked(ImGuiMouseButton.Right)) - { - if (_targetManager.Target.ObjectKind == ObjectKind.Aetheryte) - { - EAetheryteLocation location = (EAetheryteLocation)_targetManager.Target.DataId; - ImGui.SetClipboardText(string.Create(CultureInfo.InvariantCulture, - $"{{EAetheryteLocation.{location}, new({_targetManager.Target.Position.X}f, {_targetManager.Target.Position.Y}f, {_targetManager.Target.Position.Z}f)}},")); - } - else - ImGui.SetClipboardText(string.Create(CultureInfo.InvariantCulture, - $"new({_targetManager.Target.Position.X}f, {_targetManager.Target.Position.Y}f, {_targetManager.Target.Position.Z}f)")); - } + DrawCopyButton(_targetManager.Target); } else { - bool copy = ImGuiComponents.IconButton(FontAwesomeIcon.Copy); - if (ImGui.IsItemHovered()) - ImGui.SetTooltip( - "Left click: Copy your position as JSON.\nRight click: Copy your position as C# code."); - if (copy) - { - ImGui.SetClipboardText($$""" - "Position": { - "X": {{_clientState.LocalPlayer.Position.X.ToString(CultureInfo.InvariantCulture)}}, - "Y": {{_clientState.LocalPlayer.Position.Y.ToString(CultureInfo.InvariantCulture)}}, - "Z": {{_clientState.LocalPlayer.Position.Z.ToString(CultureInfo.InvariantCulture)}} - }, - "TerritoryId": {{_clientState.TerritoryType}}, - "InteractionType": "" - """); - } - else if (ImGui.IsItemClicked(ImGuiMouseButton.Right)) - { - Vector3 position = _clientState.LocalPlayer!.Position; - ImGui.SetClipboardText(string.Create(CultureInfo.InvariantCulture, - $"new({position.X}f, {position.Y}f, {position.Z}f)")); - } + DrawCopyButton(); } ulong hoveredItemId = _gameGui.HoveredItem; @@ -294,4 +165,160 @@ internal sealed class CreationUtilsComponent ImGui.Text($"Hovered Item: {hoveredItemId}"); } } + + private unsafe void DrawTargetDetails(IGameObject target) + { + ImGui.Separator(); + ImGui.Text(string.Create(CultureInfo.InvariantCulture, + $"Target: {target.Name} ({target.ObjectKind}; {target.DataId})")); + + if (_clientState.LocalPlayer != null) + { + ImGui.Text(string.Create(CultureInfo.InvariantCulture, + $"Distance: {(target.Position - _clientState.LocalPlayer.Position).Length():F2}")); + ImGui.SameLine(); + + float verticalDistance = target.Position.Y - _clientState.LocalPlayer.Position.Y; + string verticalDistanceText = string.Create(CultureInfo.InvariantCulture, $"Y: {verticalDistance:F2}"); + if (Math.Abs(verticalDistance) >= MovementController.DefaultVerticalInteractionDistance) + ImGui.TextColored(ImGuiColors.DalamudOrange, verticalDistanceText); + else + ImGui.Text(verticalDistanceText); + + ImGui.SameLine(); + } + + GameObject* gameObject = (GameObject*)target.Address; + ImGui.Text($"QM: {gameObject->NamePlateIconId}"); + } + + private unsafe void DrawInteractionButtons(IGameObject target) + { + ImGui.BeginDisabled(!_movementController.IsNavmeshReady || _gameFunctions.IsOccupied()); + if (!_movementController.IsPathfinding) + { + if (ImGuiComponents.IconButtonWithText(FontAwesomeIcon.Bullseye, "To Target")) + { + _movementController.NavigateTo(EMovementType.DebugWindow, target.DataId, + target.Position, + fly: _condition[ConditionFlag.Mounted] && _gameFunctions.IsFlyingUnlockedInCurrentZone(), + sprint: true); + } + } + else + { + if (ImGui.Button("Cancel pathfinding")) + _movementController.ResetPathfinding(); + } + + ImGui.EndDisabled(); + + ImGui.SameLine(); + ImGui.BeginDisabled(!_questData.IsIssuerOfAnyQuest(target.DataId)); + bool showQuests = ImGuiComponents.IconButton(FontAwesomeIcon.MapMarkerAlt); + if (ImGui.IsItemHovered()) + ImGui.SetTooltip("Show all Quests starting with your current target."); + if (showQuests) + _questSelectionWindow.OpenForTarget(_targetManager.Target); + + ImGui.EndDisabled(); + + ImGui.BeginDisabled(_gameFunctions.IsOccupied()); + ImGui.SameLine(); + bool interact = ImGuiComponents.IconButton(FontAwesomeIcon.MousePointer); + if (ImGui.IsItemHovered()) + ImGui.SetTooltip("Interact with your current target."); + if (interact) + { + ulong result = TargetSystem.Instance()->InteractWithObject( + (GameObject*)target.Address, false); + _logger.LogInformation("XXXXX Interaction Result: {Result}", result); + } + + ImGui.EndDisabled(); + } + + private unsafe void DrawCopyButton(IGameObject target) + { + GameObject* gameObject = (GameObject*)target.Address; + bool copy = ImGuiComponents.IconButton(FontAwesomeIcon.Copy); + if (ImGui.IsItemHovered()) + ImGui.SetTooltip( + "Left click: Copy target position as JSON.\nRight click: Copy target position as C# code."); + if (copy) + { + if (target.ObjectKind == ObjectKind.GatheringPoint) + { + ImGui.SetClipboardText($$""" + "DataId": {{target.DataId}}, + "Position": { + "X": {{target.Position.X.ToString(CultureInfo.InvariantCulture)}}, + "Y": {{target.Position.Y.ToString(CultureInfo.InvariantCulture)}}, + "Z": {{target.Position.Z.ToString(CultureInfo.InvariantCulture)}} + } + """); + } + else + { + string interactionType = gameObject->NamePlateIconId switch + { + 71201 or 71211 or 71221 or 71231 or 71341 or 71351 => "AcceptQuest", + 71202 or 71212 or 71222 or 71232 or 71342 or 71352 => "AcceptQuest", // repeatable + 71205 or 71215 or 71225 or 71235 or 71345 or 71355 => "CompleteQuest", + _ => "Interact", + }; + ImGui.SetClipboardText($$""" + "DataId": {{target.DataId}}, + "Position": { + "X": {{target.Position.X.ToString(CultureInfo.InvariantCulture)}}, + "Y": {{target.Position.Y.ToString(CultureInfo.InvariantCulture)}}, + "Z": {{target.Position.Z.ToString(CultureInfo.InvariantCulture)}} + }, + "TerritoryId": {{_clientState.TerritoryType}}, + "InteractionType": "{{interactionType}}" + """); + } + } + else if (ImGui.IsItemClicked(ImGuiMouseButton.Right)) + { + if (target.ObjectKind == ObjectKind.Aetheryte) + { + EAetheryteLocation location = (EAetheryteLocation)target.DataId; + ImGui.SetClipboardText(string.Create(CultureInfo.InvariantCulture, + $"{{EAetheryteLocation.{location}, new({target.Position.X}f, {target.Position.Y}f, {target.Position.Z}f)}},")); + } + else + ImGui.SetClipboardText(string.Create(CultureInfo.InvariantCulture, + $"new({target.Position.X}f, {target.Position.Y}f, {target.Position.Z}f)")); + } + } + + private void DrawCopyButton() + { + if (_clientState.LocalPlayer == null) + return; + + bool copy = ImGuiComponents.IconButton(FontAwesomeIcon.Copy); + if (ImGui.IsItemHovered()) + ImGui.SetTooltip( + "Left click: Copy your position as JSON.\nRight click: Copy your position as C# code."); + if (copy) + { + ImGui.SetClipboardText($$""" + "Position": { + "X": {{_clientState.LocalPlayer.Position.X.ToString(CultureInfo.InvariantCulture)}}, + "Y": {{_clientState.LocalPlayer.Position.Y.ToString(CultureInfo.InvariantCulture)}}, + "Z": {{_clientState.LocalPlayer.Position.Z.ToString(CultureInfo.InvariantCulture)}} + }, + "TerritoryId": {{_clientState.TerritoryType}}, + "InteractionType": "" + """); + } + else if (ImGui.IsItemClicked(ImGuiMouseButton.Right)) + { + Vector3 position = _clientState.LocalPlayer!.Position; + ImGui.SetClipboardText(string.Create(CultureInfo.InvariantCulture, + $"new({position.X}f, {position.Y}f, {position.Z}f)")); + } + } }