Code cleanup
authorLiza Carvelli <liza@carvel.li>
Thu, 5 Sep 2024 22:08:43 +0000 (00:08 +0200)
committerLiza Carvelli <liza@carvel.li>
Thu, 5 Sep 2024 22:08:43 +0000 (00:08 +0200)
Questionable/Windows/QuestComponents/CreationUtilsComponent.cs

index dd095f9006a02c8b2d821488ba5a11faafddc102..8c7cb09bba2b9f1c0134f2323a92438524076766 100644 (file)
@@ -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;
@@ -146,17 +147,38 @@ internal sealed class CreationUtilsComponent
 #endif
 
         if (_targetManager.Target != null)
+        {
+            DrawTargetDetails(_targetManager.Target);
+            DrawInteractionButtons(_targetManager.Target);
+            ImGui.SameLine();
+            DrawCopyButton(_targetManager.Target);
+        }
+        else
+        {
+            DrawCopyButton();
+        }
+
+        ulong hoveredItemId = _gameGui.HoveredItem;
+        if (hoveredItemId != 0)
         {
             ImGui.Separator();
-            ImGui.Text(string.Create(CultureInfo.InvariantCulture,
-                $"Target: {_targetManager.Target.Name}  ({_targetManager.Target.ObjectKind}; {_targetManager.Target.DataId})"));
+            ImGui.Text($"Hovered Item: {hoveredItemId}");
+        }
+    }
 
-            GameObject* gameObject = (GameObject*)_targetManager.Target.Address;
+    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: {(_targetManager.Target.Position - _clientState.LocalPlayer.Position).Length():F2}"));
+                $"Distance: {(target.Position - _clientState.LocalPlayer.Position).Length():F2}"));
             ImGui.SameLine();
 
-            float verticalDistance = _targetManager.Target.Position.Y - _clientState.LocalPlayer.Position.Y;
+            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);
@@ -164,134 +186,139 @@ internal sealed class CreationUtilsComponent
                 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
+        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"))
             {
-                if (ImGui.Button("Cancel pathfinding"))
-                    _movementController.ResetPathfinding();
+                _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.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.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.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();
+        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();
+    }
 
-            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)
+    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)
             {
-                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}}"
-                                             """);
-                }
+                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 if (ImGui.IsItemClicked(ImGuiMouseButton.Right))
+            else
             {
-                if (_targetManager.Target.ObjectKind == ObjectKind.Aetheryte)
+                string interactionType = gameObject->NamePlateIconId switch
                 {
-                    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)"));
-            }
-        }
-        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)
-            {
+                    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": {{_clientState.LocalPlayer.Position.X.ToString(CultureInfo.InvariantCulture)}},
-                                             "Y": {{_clientState.LocalPlayer.Position.Y.ToString(CultureInfo.InvariantCulture)}},
-                                             "Z": {{_clientState.LocalPlayer.Position.Z.ToString(CultureInfo.InvariantCulture)}}
+                                             "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": "{{interactionType}}"
                                          """);
             }
-            else if (ImGui.IsItemClicked(ImGuiMouseButton.Right))
+        }
+        else if (ImGui.IsItemClicked(ImGuiMouseButton.Right))
+        {
+            if (target.ObjectKind == ObjectKind.Aetheryte)
             {
-                Vector3 position = _clientState.LocalPlayer!.Position;
+                EAetheryteLocation location = (EAetheryteLocation)target.DataId;
                 ImGui.SetClipboardText(string.Create(CultureInfo.InvariantCulture,
-                    $"new({position.X}f, {position.Y}f, {position.Z}f)"));
+                    $"{{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)"));
         }
+    }
 
-        ulong hoveredItemId = _gameGui.HoveredItem;
-        if (hoveredItemId != 0)
+    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.Separator();
-            ImGui.Text($"Hovered Item: {hoveredItemId}");
+            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)"));
         }
     }
 }