Skip to content

Commit

Permalink
fix crash when open target context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
WeylonSantana committed Feb 22, 2025
1 parent a682b84 commit cabb5f3
Showing 1 changed file with 30 additions and 46 deletions.
76 changes: 30 additions & 46 deletions Intersect.Client.Core/Interface/Game/TargetContextMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@
using Intersect.Client.Framework.Gwen;
using Intersect.Client.Framework.Gwen.Control;
using Intersect.Client.Framework.Gwen.Control.EventArguments;
using Intersect.Client.Framework.Gwen.ControlInternal;
using Intersect.Client.Framework.Gwen.Input;
using Intersect.Client.Framework.Input;
using Intersect.Client.General;
using Intersect.Client.Localization;
using Intersect.Client.Networking;
using Intersect.Enums;
using Intersect.Framework.Core;
using Intersect.Utilities;

namespace Intersect.Client.Interface.Game;

/// <summary>
/// The GUI class for the Target Context Menu that pop up on-screen during gameplay.
/// </summary>
public sealed partial class TargetContextMenu : Framework.Gwen.Control.Menu
public sealed partial class TargetContextMenu : ContextMenu
{
private readonly MenuItem _targetNameMenuItem;
private readonly MenuDivider _nameDivider;
private readonly MenuItem _tradeMenuItem;
private readonly MenuItem _partyMenuItem;
private readonly MenuItem _friendMenuItem;
Expand All @@ -39,8 +40,13 @@ public TargetContextMenu(Canvas gameCanvas) : base(gameCanvas, nameof(TargetCont
_me = Globals.Me;

_targetNameMenuItem = AddItem(string.Empty);
_targetNameMenuItem.MouseInputEnabled = false;
AddDivider();
_nameDivider = new MenuDivider(this)
{
Dock = Pos.Top,
Margin = new Margin(IconMarginDisabled ? 0 : 24, 0, 4, 0),
Height = 1,
MinimumSize = new Point(0, 1),
};

_tradeMenuItem = AddItem(Strings.EntityContextMenu.Trade);
_tradeMenuItem.Clicked += tradeRequest_Clicked;
Expand All @@ -58,6 +64,7 @@ public TargetContextMenu(Canvas gameCanvas) : base(gameCanvas, nameof(TargetCont
_privateMessageMenuItem.Clicked += privateMessageRequest_Clicked;

LoadJsonUi(GameContentManager.UI.InGame, Graphics.Renderer?.GetResolutionString());
_buildContextMenu();
}

public void ToggleHidden(object? target)
Expand Down Expand Up @@ -119,11 +126,9 @@ public void ToggleHidden(object? target)
}
}


if (IsHidden)
{
TryShowTargetButton(shouldShowTargetNameMenuItem);
TryShowGuildButton();
_buildContextMenu(shouldShowTargetNameMenuItem);
SizeToChildren();
Open(Pos.None);
SetPosition(newX, newY);
Expand All @@ -134,27 +139,33 @@ public void ToggleHidden(object? target)
}
}

private void TryShowTargetButton(bool shouldShow)
private void _buildContextMenu(bool shouldShowTargetName = false)
{
_targetNameMenuItem.SetText(shouldShow ? _entity.Name : string.Empty);
ClearChildren();

if (shouldShow)
if (shouldShowTargetName)
{
var indexOf = IndexOf(_targetNameMenuItem);
AddChild(_targetNameMenuItem);
_targetNameMenuItem.SetText(_entity?.Name ?? string.Empty);
_targetNameMenuItem.MouseInputEnabled = false;
AddChild(_nameDivider);
}

if (indexOf > 0)
{
RemoveAt(indexOf);
}
AddChild(_tradeMenuItem);
AddChild(_partyMenuItem);
AddChild(_friendMenuItem);

if (indexOf != 0)
if (_entity is Player player && player != _me && string.IsNullOrWhiteSpace(player.Guild))
{
if (_me?.GuildRank?.Permissions?.Invite ?? false)
{
Insert(0, _targetNameMenuItem);
AddChild(_guildMenuItem);
}
}
else

if (_entity is Player plyr && plyr != _me)
{
Remove(_targetNameMenuItem);
AddChild(_privateMessageMenuItem);
}
}

Expand Down Expand Up @@ -247,31 +258,4 @@ void privateMessageRequest_Clicked(Base sender, MouseButtonState arguments)

Interface.GameUi.SetChatboxText($"/pm {_entity.Name} ");
}

void TryShowGuildButton()
{
var shouldShow = false;
if (_entity is Player plyr && _entity != _me && string.IsNullOrWhiteSpace(plyr.Guild))
{
if (_me?.GuildRank?.Permissions?.Invite ?? false)
{
shouldShow = true;
}
}

if (shouldShow)
{
if (!Children.Contains(_guildMenuItem))
{
AddChild(_guildMenuItem);
}
}
else
{
if (Children.Contains(_guildMenuItem))
{
Remove(_guildMenuItem);
}
}
}
}

0 comments on commit cabb5f3

Please sign in to comment.