Skip to content

Commit cabb5f3

Browse files
committed
fix crash when open target context menu
1 parent a682b84 commit cabb5f3

File tree

1 file changed

+30
-46
lines changed

1 file changed

+30
-46
lines changed

Intersect.Client.Core/Interface/Game/TargetContextMenu.cs

Lines changed: 30 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,24 @@
55
using Intersect.Client.Framework.Gwen;
66
using Intersect.Client.Framework.Gwen.Control;
77
using Intersect.Client.Framework.Gwen.Control.EventArguments;
8+
using Intersect.Client.Framework.Gwen.ControlInternal;
89
using Intersect.Client.Framework.Gwen.Input;
910
using Intersect.Client.Framework.Input;
1011
using Intersect.Client.General;
1112
using Intersect.Client.Localization;
1213
using Intersect.Client.Networking;
1314
using Intersect.Enums;
1415
using Intersect.Framework.Core;
15-
using Intersect.Utilities;
1616

1717
namespace Intersect.Client.Interface.Game;
1818

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

4142
_targetNameMenuItem = AddItem(string.Empty);
42-
_targetNameMenuItem.MouseInputEnabled = false;
43-
AddDivider();
43+
_nameDivider = new MenuDivider(this)
44+
{
45+
Dock = Pos.Top,
46+
Margin = new Margin(IconMarginDisabled ? 0 : 24, 0, 4, 0),
47+
Height = 1,
48+
MinimumSize = new Point(0, 1),
49+
};
4450

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

6066
LoadJsonUi(GameContentManager.UI.InGame, Graphics.Renderer?.GetResolutionString());
67+
_buildContextMenu();
6168
}
6269

6370
public void ToggleHidden(object? target)
@@ -119,11 +126,9 @@ public void ToggleHidden(object? target)
119126
}
120127
}
121128

122-
123129
if (IsHidden)
124130
{
125-
TryShowTargetButton(shouldShowTargetNameMenuItem);
126-
TryShowGuildButton();
131+
_buildContextMenu(shouldShowTargetNameMenuItem);
127132
SizeToChildren();
128133
Open(Pos.None);
129134
SetPosition(newX, newY);
@@ -134,27 +139,33 @@ public void ToggleHidden(object? target)
134139
}
135140
}
136141

137-
private void TryShowTargetButton(bool shouldShow)
142+
private void _buildContextMenu(bool shouldShowTargetName = false)
138143
{
139-
_targetNameMenuItem.SetText(shouldShow ? _entity.Name : string.Empty);
144+
ClearChildren();
140145

141-
if (shouldShow)
146+
if (shouldShowTargetName)
142147
{
143-
var indexOf = IndexOf(_targetNameMenuItem);
148+
AddChild(_targetNameMenuItem);
149+
_targetNameMenuItem.SetText(_entity?.Name ?? string.Empty);
150+
_targetNameMenuItem.MouseInputEnabled = false;
151+
AddChild(_nameDivider);
152+
}
144153

145-
if (indexOf > 0)
146-
{
147-
RemoveAt(indexOf);
148-
}
154+
AddChild(_tradeMenuItem);
155+
AddChild(_partyMenuItem);
156+
AddChild(_friendMenuItem);
149157

150-
if (indexOf != 0)
158+
if (_entity is Player player && player != _me && string.IsNullOrWhiteSpace(player.Guild))
159+
{
160+
if (_me?.GuildRank?.Permissions?.Invite ?? false)
151161
{
152-
Insert(0, _targetNameMenuItem);
162+
AddChild(_guildMenuItem);
153163
}
154164
}
155-
else
165+
166+
if (_entity is Player plyr && plyr != _me)
156167
{
157-
Remove(_targetNameMenuItem);
168+
AddChild(_privateMessageMenuItem);
158169
}
159170
}
160171

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

248259
Interface.GameUi.SetChatboxText($"/pm {_entity.Name} ");
249260
}
250-
251-
void TryShowGuildButton()
252-
{
253-
var shouldShow = false;
254-
if (_entity is Player plyr && _entity != _me && string.IsNullOrWhiteSpace(plyr.Guild))
255-
{
256-
if (_me?.GuildRank?.Permissions?.Invite ?? false)
257-
{
258-
shouldShow = true;
259-
}
260-
}
261-
262-
if (shouldShow)
263-
{
264-
if (!Children.Contains(_guildMenuItem))
265-
{
266-
AddChild(_guildMenuItem);
267-
}
268-
}
269-
else
270-
{
271-
if (Children.Contains(_guildMenuItem))
272-
{
273-
Remove(_guildMenuItem);
274-
}
275-
}
276-
}
277261
}

0 commit comments

Comments
 (0)