Skip to content

Commit 933abde

Browse files
committed
making items having 1 context menu shared between all items
1 parent 4028031 commit 933abde

File tree

5 files changed

+41
-31
lines changed

5 files changed

+41
-31
lines changed

Intersect.Client.Core/Interface/Game/Inventory/InventoryItem.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public partial class InventoryItem : SlotItem
4646
private readonly MenuItem _actionItemMenuItem;
4747
private readonly MenuItem _dropItemMenuItem;
4848

49-
public InventoryItem(InventoryWindow inventoryWindow, Base parent, int index) : base(parent, nameof(InventoryItem), index, "InventoryContextMenu")
49+
public InventoryItem(InventoryWindow inventoryWindow, Base parent, int index, ContextMenu contextMenu) : base(parent, nameof(InventoryItem), index, contextMenu)
5050
{
5151
_inventoryWindow = inventoryWindow;
5252
TextureFilename = "inventoryitem.png";
@@ -184,11 +184,7 @@ public override void OpenContextMenu()
184184
_dropItemMenuItem.SetText(Strings.ItemContextMenu.Drop.ToString(descriptor.Name));
185185
}
186186

187-
// Display our menu... If we have anything to display.
188-
if (_contextMenu.Children.Count > 0)
189-
{
190-
_contextMenu.Open(Pos.None);
191-
}
187+
base.OpenContextMenu();
192188
}
193189

194190
private void _useItemContextItem_Clicked(Base sender, MouseButtonState arguments)

Intersect.Client.Core/Interface/Game/Inventory/InventoryWindow.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ namespace Intersect.Client.Interface.Game.Inventory;
1212
public partial class InventoryWindow : Window
1313
{
1414
public List<SlotItem> Items { get; set; } = [];
15-
1615
private readonly ScrollControl _slotContainer;
16+
private readonly ContextMenu _contextMenu;
1717

1818
public InventoryWindow(Canvas gameCanvas) : base(gameCanvas, Strings.Inventory.Title, false, nameof(InventoryWindow))
1919
{
@@ -32,6 +32,14 @@ public InventoryWindow(Canvas gameCanvas) : base(gameCanvas, Strings.Inventory.T
3232
OverflowX = OverflowBehavior.Auto,
3333
OverflowY = OverflowBehavior.Scroll,
3434
};
35+
36+
_contextMenu = new ContextMenu(Interface.CurrentInterface.Root, "InventoryContextMenu")
37+
{
38+
IsVisibleInParent = false,
39+
IconMarginDisabled = true,
40+
ItemFont = GameContentManager.Current.GetFont(name: "sourcesansproblack"),
41+
ItemFontSize = 10,
42+
};
3543
}
3644

3745
protected override void EnsureInitialized()
@@ -75,7 +83,7 @@ private void InitItemContainer()
7583
{
7684
for (var slotIndex = 0; slotIndex < Options.Instance.Player.MaxInventory; slotIndex++)
7785
{
78-
Items.Add(new InventoryItem(this, _slotContainer, slotIndex));
86+
Items.Add(new InventoryItem(this, _slotContainer, slotIndex, _contextMenu));
7987
}
8088

8189
PopulateSlotContainer.Populate(_slotContainer, Items);
@@ -88,6 +96,7 @@ public override void Hide()
8896
return;
8997
}
9098

99+
_contextMenu?.Close();
91100
base.Hide();
92101
}
93102

Intersect.Client.Core/Interface/Game/Shop/ShopItem.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public partial class ShopItem : SlotItem
2020
private readonly MenuItem _buyMenuItem;
2121
private ItemDescriptionWindow? _itemDescWindow;
2222

23-
public ShopItem(ShopWindow shopWindow, Base parent, int index) : base(parent, nameof(ShopItem), index, "ShopContextMenu")
23+
public ShopItem(ShopWindow shopWindow, Base parent, int index, ContextMenu contextMenu) : base(parent, nameof(ShopItem), index, contextMenu)
2424
{
2525
_shopWindow = shopWindow;
2626
_mySlot = index;
@@ -106,7 +106,7 @@ private void _iconImage_RightClicked(Base sender, MouseButtonState arguments)
106106

107107
if (ClientConfiguration.Instance.EnableContextMenus)
108108
{
109-
_openContextMenu(_mySlot);
109+
OpenContextMenu();
110110
}
111111
else
112112
{
@@ -124,20 +124,20 @@ private void _buyMenuItem_Clicked(Base sender, Framework.Gwen.Control.EventArgum
124124
Globals.Me?.TryBuyItem(_mySlot);
125125
}
126126

127-
private void _openContextMenu(int slot)
127+
public override void OpenContextMenu()
128128
{
129129
if (Globals.GameShop is not { SellingItems.Count: > 0 } gameShop)
130130
{
131131
return;
132132
}
133133

134-
if (!ItemDescriptor.TryGet(gameShop.SellingItems[slot].ItemId, out var item))
134+
if (!ItemDescriptor.TryGet(gameShop.SellingItems[SlotIndex].ItemId, out var item))
135135
{
136136
return;
137137
}
138138

139139
_buyMenuItem.SetText(Strings.ShopContextMenu.Buy.ToString(item.Name));
140-
_contextMenu.Open(Pos.None);
140+
base.OpenContextMenu();
141141
}
142142

143143
public void LoadItem()

Intersect.Client.Core/Interface/Game/Shop/ShopWindow.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public partial class ShopWindow : Window
1212
{
1313
private readonly List<SlotItem> _items = [];
1414
private readonly ScrollControl _slotContainer;
15+
private readonly ContextMenu _contextMenu;
1516

1617
public ShopWindow(Canvas gameCanvas) : base(gameCanvas, Globals.GameShop?.Name ?? Strings.Shop.Title, false, nameof(ShopWindow))
1718
{
@@ -32,6 +33,14 @@ public ShopWindow(Canvas gameCanvas) : base(gameCanvas, Globals.GameShop?.Name ?
3233
OverflowX = OverflowBehavior.Auto,
3334
OverflowY = OverflowBehavior.Scroll,
3435
};
36+
37+
_contextMenu = new ContextMenu(Interface.CurrentInterface.Root, "ShopContextMenu")
38+
{
39+
IsVisibleInParent = false,
40+
IconMarginDisabled = true,
41+
ItemFont = GameContentManager.Current.GetFont(name: "sourcesansproblack"),
42+
ItemFontSize = 10,
43+
};
3544
}
3645

3746
protected override void EnsureInitialized()
@@ -49,9 +58,15 @@ private void InitItemContainer()
4958

5059
for (var slotIndex = 0; slotIndex < gameShop.SellingItems.Count; slotIndex++)
5160
{
52-
_items.Add(new ShopItem(this, _slotContainer, slotIndex));
61+
_items.Add(new ShopItem(this, _slotContainer, slotIndex, _contextMenu));
5362
}
5463

5564
PopulateSlotContainer.Populate(_slotContainer, _items);
5665
}
66+
67+
public override void Hide()
68+
{
69+
_contextMenu?.Close();
70+
base.Hide();
71+
}
5772
}
Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using Intersect.Client.Framework.File_Management;
21
using Intersect.Client.Framework.Gwen;
32
using Intersect.Client.Framework.Gwen.Control;
43

@@ -10,7 +9,7 @@ public partial class SlotItem : ImagePanel
109
protected readonly ImagePanel _iconImage;
1110
protected readonly ContextMenu _contextMenu;
1211

13-
public SlotItem(Base parent, string name, int index, string contextMenuName) : base(parent, name)
12+
public SlotItem(Base parent, string name, int index, ContextMenu contextMenu) : base(parent, name)
1413
{
1514
SlotIndex = index;
1615

@@ -26,15 +25,7 @@ public SlotItem(Base parent, string name, int index, string contextMenuName) : b
2625
HoverSound = "octave-tap-resonant.wav",
2726
};
2827

29-
// Generate our context menu with basic options.
30-
// TODO: Refactor so shop only has 1 context menu shared between all items
31-
_contextMenu = new ContextMenu(Interface.CurrentInterface.Root, contextMenuName)
32-
{
33-
IsVisibleInParent = false,
34-
IconMarginDisabled = true,
35-
ItemFont = GameContentManager.Current.GetFont(name: "sourcesansproblack"),
36-
ItemFontSize = 10,
37-
};
28+
_contextMenu = contextMenu;
3829
}
3930

4031
public virtual void Update()
@@ -43,11 +34,10 @@ public virtual void Update()
4334

4435
public virtual void OpenContextMenu()
4536
{
46-
}
47-
48-
protected override void Dispose(bool disposing)
49-
{
50-
_contextMenu?.Close();
51-
base.Dispose(disposing);
37+
// Display our menu... If we have anything to display.
38+
if (_contextMenu.Children.Count > 0)
39+
{
40+
_contextMenu.Open(Pos.None);
41+
}
5242
}
5343
}

0 commit comments

Comments
 (0)