Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix crash when open target context menu #2618

Merged
merged 2 commits into from
Feb 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 28 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,11 @@ 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,
MinimumSize = new Point(0, 1),
};

_tradeMenuItem = AddItem(Strings.EntityContextMenu.Trade);
_tradeMenuItem.Clicked += tradeRequest_Clicked;
Expand All @@ -58,6 +62,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 +124,9 @@ public void ToggleHidden(object? target)
}
}


if (IsHidden)
{
TryShowTargetButton(shouldShowTargetNameMenuItem);
TryShowGuildButton();
BuildContextMenu(shouldShowTargetNameMenuItem);
SizeToChildren();
Open(Pos.None);
SetPosition(newX, newY);
Expand All @@ -134,28 +137,34 @@ 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)
AddChild(_tradeMenuItem);
AddChild(_partyMenuItem);
AddChild(_friendMenuItem);

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

if (indexOf != 0)
if (player != _me)
{
Insert(0, _targetNameMenuItem);
AddChild(_privateMessageMenuItem);
}
}
else
{
Remove(_targetNameMenuItem);
}
}

void invite_Clicked(Base sender, MouseButtonState arguments)
Expand Down Expand Up @@ -247,31 +256,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);
}
}
}
}
Loading