diff --git a/Intersect.Client.Core/Interface/Game/Bag/BagItem.cs b/Intersect.Client.Core/Interface/Game/Bag/BagItem.cs index 70592aec0f..487a705fd2 100644 --- a/Intersect.Client.Core/Interface/Game/Bag/BagItem.cs +++ b/Intersect.Client.Core/Interface/Game/Bag/BagItem.cs @@ -39,7 +39,8 @@ public partial class BagItem : SlotItem // Context Menu Handling private readonly MenuItem _withdrawContextItem; - public BagItem(BagWindow bagWindow, Base parent, int index, ContextMenu contextMenu) : base(parent, nameof(BagItem), index, contextMenu) + public BagItem(BagWindow bagWindow, Base parent, int index, ContextMenu contextMenu) + : base(parent, nameof(BagItem), index, contextMenu) { _bagWindow = bagWindow; TextureFilename = "bagitem.png"; @@ -60,15 +61,15 @@ public BagItem(BagWindow bagWindow, Base parent, int index, ContextMenu contextM LoadJsonUi(GameContentManager.UI.InGame, Graphics.Renderer.GetResolutionString()); - _contextMenu.ClearChildren(); - _withdrawContextItem = _contextMenu.AddItem(Strings.BagContextMenu.Withdraw); + contextMenu.ClearChildren(); + _withdrawContextItem = contextMenu.AddItem(Strings.BagContextMenu.Withdraw); _withdrawContextItem.Clicked += _withdrawMenuItem_Clicked; - _contextMenu.LoadJsonUi(GameContentManager.UI.InGame, Graphics.Renderer.GetResolutionString()); + contextMenu.LoadJsonUi(GameContentManager.UI.InGame, Graphics.Renderer.GetResolutionString()); } #region Context Menu - public override void OpenContextMenu() + protected override void OnContextMenuOpening(ContextMenu contextMenu) { if (Globals.BagSlots is not { Length: > 0 } bagSlots) { @@ -81,10 +82,10 @@ public override void OpenContextMenu() } // Clear the context menu and add the withdraw item with updated item name - _contextMenu.ClearChildren(); + contextMenu.ClearChildren(); _withdrawContextItem.SetText(Strings.BagContextMenu.Withdraw.ToString(item.Name)); - _contextMenu.AddChild(_withdrawContextItem); - base.OpenContextMenu(); + contextMenu.AddChild(_withdrawContextItem); + base.OnContextMenuOpening(contextMenu); } private void _withdrawMenuItem_Clicked(Base sender, MouseButtonState arguments) diff --git a/Intersect.Client.Core/Interface/Game/Bank/BankItem.cs b/Intersect.Client.Core/Interface/Game/Bank/BankItem.cs index fb4f9a4fb9..954403f6db 100644 --- a/Intersect.Client.Core/Interface/Game/Bank/BankItem.cs +++ b/Intersect.Client.Core/Interface/Game/Bank/BankItem.cs @@ -63,13 +63,13 @@ public BankItem(BankWindow bankWindow, Base parent, int index, ContextMenu conte LoadJsonUi(GameContentManager.UI.InGame, Graphics.Renderer.GetResolutionString()); - _contextMenu.ClearChildren(); - _withdrawContextItem = _contextMenu.AddItem(Strings.BankContextMenu.Withdraw); + contextMenu.ClearChildren(); + _withdrawContextItem = contextMenu.AddItem(Strings.BankContextMenu.Withdraw); _withdrawContextItem.Clicked += _withdrawMenuItem_Clicked; - _contextMenu.LoadJsonUi(GameContentManager.UI.InGame, Graphics.Renderer.GetResolutionString()); + contextMenu.LoadJsonUi(GameContentManager.UI.InGame, Graphics.Renderer.GetResolutionString()); } - public void OpenContextMenu() + protected override void OnContextMenuOpening(ContextMenu contextMenu) { if (Globals.BankSlots is not { Length: > 0 } bankSlots) { @@ -82,11 +82,11 @@ public void OpenContextMenu() } // Clear the context menu and add the withdraw item with updated item name - _contextMenu.ClearChildren(); + contextMenu.ClearChildren(); + contextMenu.AddChild(_withdrawContextItem); _withdrawContextItem.SetText(Strings.BankContextMenu.Withdraw.ToString(item.Name)); - _contextMenu.AddChild(_withdrawContextItem); - base.OpenContextMenu(); + base.OnContextMenuOpening(contextMenu); } private void _withdrawMenuItem_Clicked(Base sender, MouseButtonState arguments) diff --git a/Intersect.Client.Core/Interface/Game/Hotbar/HotBar.cs b/Intersect.Client.Core/Interface/Game/Hotbar/HotBar.cs deleted file mode 100644 index 399b5e9e34..0000000000 --- a/Intersect.Client.Core/Interface/Game/Hotbar/HotBar.cs +++ /dev/null @@ -1,79 +0,0 @@ -using Intersect.Client.Core; -using Intersect.Client.Framework.File_Management; -using Intersect.Client.Framework.GenericClasses; -using Intersect.Client.Framework.Gwen; -using Intersect.Client.Framework.Gwen.Control; -using Intersect.Client.General; - -namespace Intersect.Client.Interface.Game.Hotbar; - - -public partial class HotBarWindow -{ - - //Controls - public readonly ImagePanel HotbarWindow; - - //Item List - public readonly List Items = []; - - //Init - public HotBarWindow(Canvas gameCanvas) - { - HotbarWindow = new ImagePanel(gameCanvas, "HotbarWindow") - { - AlignmentPadding = new Padding { Top = 4, Right = 4 }, - Alignment = [Alignments.Top, Alignments.Right], - Padding = Padding.Four, - RestrictToParent = true, - TextureFilename = "hotbar.png", - TextureNinePatchMargin = Margin.Three, - ShouldCacheToTexture = true, - }; - - if (Graphics.Renderer == null) - { - return; - } - - var hotbarSlotCount = Options.Instance.Player.HotbarSlotCount; - for (var hotbarSlotIndex = 0; hotbarSlotIndex < hotbarSlotCount; hotbarSlotIndex++) - { - var hotbarItem = new HotbarItem(hotbarSlotIndex, HotbarWindow); - Items.Add(hotbarItem); - } - - HotbarWindow.SizeToChildren(); - - HotbarWindow.LoadJsonUi(GameContentManager.UI.InGame, Graphics.Renderer.GetResolutionString()); - - HotbarWindow.SizeToChildren(); - } - - public void Update() - { - if (Globals.Me == null) - { - return; - } - - foreach (var slot in Items) - { - slot.Update(); - } - } - - public FloatRect RenderBounds() - { - var rect = new FloatRect - { - X = HotbarWindow.ToCanvas(default).X, - Y = HotbarWindow.ToCanvas(default).Y, - Width = HotbarWindow.Width, - Height = HotbarWindow.Height, - }; - - return rect; - } - -} diff --git a/Intersect.Client.Core/Interface/Game/Hotbar/HotBarWindow.cs b/Intersect.Client.Core/Interface/Game/Hotbar/HotBarWindow.cs new file mode 100644 index 0000000000..a528821e29 --- /dev/null +++ b/Intersect.Client.Core/Interface/Game/Hotbar/HotBarWindow.cs @@ -0,0 +1,65 @@ +using Intersect.Client.Core; +using Intersect.Client.Framework.File_Management; +using Intersect.Client.Framework.GenericClasses; +using Intersect.Client.Framework.Gwen; +using Intersect.Client.Framework.Gwen.Control; +using Intersect.Client.General; + +namespace Intersect.Client.Interface.Game.Hotbar; + +public partial class HotBarWindow : ImagePanel +{ + public readonly List Items = []; + + public HotBarWindow(Canvas gameCanvas) : base(gameCanvas, nameof(HotBarWindow)) + { + AlignmentPadding = new Padding { Top = 4, Right = 4 }; + Alignment = [Alignments.Top, Alignments.Right]; + RestrictToParent = true; + TextureFilename = "hotbar.png"; + TextureNinePatchMargin = Margin.Three; + ShouldCacheToTexture = true; + + if (Graphics.Renderer == null) + { + return; + } + + var hotbarSlotCount = Options.Instance.Player.HotbarSlotCount; + for (var hotbarSlotIndex = 0; hotbarSlotIndex < hotbarSlotCount; hotbarSlotIndex++) + { + var hotbarItem = new HotbarItem(hotbarSlotIndex, this); + Items.Add(hotbarItem); + } + + SizeToChildren(); + LoadJsonUi(GameContentManager.UI.InGame, Graphics.Renderer.GetResolutionString()); + SizeToChildren(); + } + + public void Update() + { + if (Globals.Me == null) + { + return; + } + + foreach (var slot in Items) + { + slot.Update(); + } + } + + public FloatRect RenderBounds() + { + var rect = new FloatRect + { + X = ToCanvas(default).X, + Y = ToCanvas(default).Y, + Width = Width, + Height = Height, + }; + + return rect; + } +} diff --git a/Intersect.Client.Core/Interface/Game/Hotbar/HotbarItem.cs b/Intersect.Client.Core/Interface/Game/Hotbar/HotbarItem.cs index 034b725d73..e19115505f 100644 --- a/Intersect.Client.Core/Interface/Game/Hotbar/HotbarItem.cs +++ b/Intersect.Client.Core/Interface/Game/Hotbar/HotbarItem.cs @@ -18,16 +18,13 @@ namespace Intersect.Client.Interface.Game.Hotbar; - -public partial class HotbarItem +public partial class HotbarItem : SlotItem { private const int ItemXPadding = 4; private const int ItemYPadding = 4; - private readonly ImagePanel _contentPanel; private readonly Label _cooldownLabel; private readonly Label _equipLabel; - private readonly ImagePanel _icon; private readonly Label _keyLabel; private bool _canDrag; @@ -40,7 +37,6 @@ public partial class HotbarItem private bool _isEquipped; private bool _isFaded; private readonly Base _hotbarWindow; - private readonly int _hotbarSlotIndex; private ControlBinding? _hotKey; private Item? _inventoryItem = null; private int _inventoryItemIndex = -1; @@ -54,39 +50,31 @@ public partial class HotbarItem private bool _textureLoaded; public HotbarItem(int hotbarSlotIndex, Base hotbarWindow) + : base(hotbarWindow, $"HotbarContainer{hotbarSlotIndex}", hotbarSlotIndex, null) { - _hotbarSlotIndex = hotbarSlotIndex; _hotbarWindow = hotbarWindow; var column = hotbarSlotIndex % 10; var row = hotbarSlotIndex / 10; - _icon = new ImagePanel(hotbarWindow, $"HotbarContainer{hotbarSlotIndex}") - { - X = 4 + column * 40, - Y = 4 + row * 40, - Width = 36, - Height = 36, - Margin = new Margin(column > 0 ? 4 : 0, row > 0 ? 4 : 0, 0, 0), - RestrictToParent = true, - TextureFilename = "hotbaritem.png", - }; - - // Content Panel is layered on top of the container (shows the Item or Spell Icon). - _contentPanel = new ImagePanel(_icon, $"{nameof(HotbarIcon)}{_hotbarSlotIndex}") - { - X = 1, - Y = 1, - Width = 32, - Height = 32, - }; - _contentPanel.HoverEnter += hotbarIcon_HoverEnter; - _contentPanel.HoverLeave += hotbarIcon_HoverLeave; - _contentPanel.Clicked += hotbarIcon_Clicked; + X = 4 + column * 40; + Y = 4 + row * 40; + Width = 36; + Height = 36; + Margin = new Margin(column > 0 ? 4 : 0, row > 0 ? 4 : 0, 0, 0); + RestrictToParent = true; + TextureFilename = "hotbaritem.png"; + + // _iconImage is layered on top of the container (shows the Item or Spell Icon). + _iconImage.Name = $"{nameof(HotbarItem)}{SlotIndex}"; + _iconImage.SetPosition(1, 1); + _iconImage.HoverEnter += _iconImage_HoverEnter; + _iconImage.HoverLeave += _iconImage_HoverLeave; + _iconImage.Clicked += _iconImage_Clicked; var font = GameContentManager.Current.GetFont("sourcesansproblack"); - _equipLabel = new Label(_icon, $"EquipLabel{hotbarSlotIndex}") + _equipLabel = new Label(this, $"EquipLabel{hotbarSlotIndex}") { Alignment = [Alignments.Top, Alignments.Left], X = 26, @@ -102,7 +90,7 @@ public HotbarItem(int hotbarSlotIndex, Base hotbarWindow) TextColorOverride = Color.White, }; - _quantityLabel = new Label(_icon, $"QuantityLabel{hotbarSlotIndex}") + _quantityLabel = new Label(this, $"QuantityLabel{hotbarSlotIndex}") { Alignment = [Alignments.Top, Alignments.Right], X = 32, @@ -117,7 +105,7 @@ public HotbarItem(int hotbarSlotIndex, Base hotbarWindow) TextColorOverride = Color.White, }; - _cooldownLabel = new Label(_icon, $"CooldownLabel{hotbarSlotIndex}") + _cooldownLabel = new Label(this, $"CooldownLabel{hotbarSlotIndex}") { Alignment = [Alignments.Center], TextAlign = Pos.Center, @@ -133,7 +121,7 @@ public HotbarItem(int hotbarSlotIndex, Base hotbarWindow) TextColorOverride = Color.White, }; - _keyLabel = new Label(_icon, $"KeyLabel{hotbarSlotIndex}") + _keyLabel = new Label(this, $"KeyLabel{hotbarSlotIndex}") { Alignment = [Alignments.Bottom, Alignments.Right], X = 31, @@ -148,10 +136,13 @@ public HotbarItem(int hotbarSlotIndex, Base hotbarWindow) }; } - public ImagePanel HotbarIcon => _icon; - public void Activate() { + if (Globals.InputManager.IsMouseButtonDown(MouseButton.Right)) + { + return; + } + if (_currentId != Guid.Empty && Globals.Me != null) { if (_currentItem != null) @@ -168,7 +159,7 @@ public void Activate() } } - private void hotbarIcon_Clicked(Base sender, MouseButtonState arguments) + private void _iconImage_Clicked(Base sender, MouseButtonState arguments) { switch (arguments.MouseButton) { @@ -177,12 +168,12 @@ private void hotbarIcon_Clicked(Base sender, MouseButtonState arguments) break; case MouseButton.Right: - Globals.Me?.AddToHotbar(_hotbarSlotIndex, -1, -1); + Globals.Me?.AddToHotbar(SlotIndex, -1, -1); break; } } - private void hotbarIcon_HoverLeave(Base sender, EventArgs arguments) + private void _iconImage_HoverLeave(Base sender, EventArgs arguments) { _mouseOver = false; _mouseX = -1; @@ -200,7 +191,7 @@ private void hotbarIcon_HoverLeave(Base sender, EventArgs arguments) } } - private void hotbarIcon_HoverEnter(Base sender, EventArgs arguments) + private void _iconImage_HoverEnter(Base sender, EventArgs arguments) { if (InputHandler.MouseFocus != null || Globals.Me == null) { @@ -254,10 +245,10 @@ public FloatRect RenderBounds() { var rect = new FloatRect() { - X = HotbarIcon.ToCanvas(new Point(0, 0)).X, - Y = HotbarIcon.ToCanvas(new Point(0, 0)).Y, - Width = HotbarIcon.Width, - Height = HotbarIcon.Height + X = ToCanvas(new Point(0, 0)).X, + Y = ToCanvas(new Point(0, 0)).Y, + Width = Width, + Height = Height }; return rect; @@ -271,7 +262,7 @@ public void Update() } // Check if the label should be changed - var controlValue = Control.HotkeyOffset + _hotbarSlotIndex + 1; + var controlValue = Control.HotkeyOffset + SlotIndex + 1; ControlBinding? binding = null; if (Controls.ActiveControls.TryGetMappingFor(controlValue, out var mapping)) { @@ -311,7 +302,7 @@ public void Update() _hotKey = binding == null ? null : new ControlBinding(binding); } - var slot = Globals.Me.Hotbar[_hotbarSlotIndex]; + var slot = Globals.Me.Hotbar[SlotIndex]; var updateDisplay = _currentId != slot.ItemOrSpellId || _textureLoaded == false; // Update display if item changes or we dont have a texture for it. if (_currentId != slot.ItemOrSpellId) @@ -395,7 +386,7 @@ public void Update() //We don't know it, remove from hotbar right away! if (_spellBookItem == null) { - Globals.Me.AddToHotbar(_hotbarSlotIndex, -1, -1); + Globals.Me.AddToHotbar(SlotIndex, -1, -1); updateDisplay = true; } @@ -418,8 +409,8 @@ public void Update() { if (_currentItem != null) { - _contentPanel.Show(); - _contentPanel.Texture = Globals.ContentManager.GetTexture( + _iconImage.Show(); + _iconImage.Texture = Globals.ContentManager.GetTexture( Framework.Content.TextureType.Item, _currentItem.Icon ); @@ -457,8 +448,8 @@ public void Update() } else if (_currentSpell != null) { - _contentPanel.Show(); - _contentPanel.Texture = Globals.ContentManager.GetTexture( + _iconImage.Show(); + _iconImage.Texture = Globals.ContentManager.GetTexture( Framework.Content.TextureType.Spell, _currentSpell.Icon ); @@ -486,7 +477,7 @@ public void Update() } else { - _contentPanel.Hide(); + _iconImage.Hide(); _textureLoaded = true; _isEquipped = false; _equipLabel.IsHidden = true; @@ -498,24 +489,24 @@ public void Update() { if (_currentSpell != null) { - _contentPanel.RenderColor = new Color(60, 255, 255, 255); + _iconImage.RenderColor = new Color(60, 255, 255, 255); } if (_currentItem != null) { - _contentPanel.RenderColor = new Color(60, _currentItem.Color.R, _currentItem.Color.G, _currentItem.Color.B); + _iconImage.RenderColor = new Color(60, _currentItem.Color.R, _currentItem.Color.G, _currentItem.Color.B); } } else { if (_currentSpell != null) { - _contentPanel.RenderColor = Color.White; + _iconImage.RenderColor = Color.White; } if (_currentItem != null) { - _contentPanel.RenderColor = _currentItem.Color; + _iconImage.RenderColor = _currentItem.Color; } } } @@ -524,7 +515,7 @@ public void Update() { if (!_isDragging) { - _contentPanel.IsHidden = false; + _iconImage.IsHidden = false; var equipLabelIsHidden = _currentItem == null || !Globals.Me.IsEquipped(_inventoryItemIndex) || _inventoryItemIndex < 0; _equipLabel.IsHidden = equipLabelIsHidden; @@ -551,25 +542,25 @@ public void Update() { if (_mouseX == -1 || _mouseY == -1) { - _mouseX = InputHandler.MousePosition.X - HotbarIcon.ToCanvas(new Point(0, 0)).X; - _mouseY = InputHandler.MousePosition.Y - HotbarIcon.ToCanvas(new Point(0, 0)).Y; + _mouseX = InputHandler.MousePosition.X - ToCanvas(new Point(0, 0)).X; + _mouseY = InputHandler.MousePosition.Y - ToCanvas(new Point(0, 0)).Y; } else { var xdiff = _mouseX - (InputHandler.MousePosition.X - - HotbarIcon.ToCanvas(new Point(0, 0)).X); + ToCanvas(new Point(0, 0)).X); var ydiff = _mouseY - (InputHandler.MousePosition.Y - - HotbarIcon.ToCanvas(new Point(0, 0)).Y); + ToCanvas(new Point(0, 0)).Y); if (Math.Sqrt(Math.Pow(xdiff, 2) + Math.Pow(ydiff, 2)) > 5) { _isDragging = true; _dragIcon = new Draggable( - HotbarIcon.ToCanvas(new Point(0, 0)).X + _mouseX, - HotbarIcon.ToCanvas(new Point(0, 0)).X + _mouseY, _contentPanel.Texture, _contentPanel.RenderColor + ToCanvas(new Point(0, 0)).X + _mouseX, + ToCanvas(new Point(0, 0)).X + _mouseY, _iconImage.Texture, _iconImage.RenderColor ); //SOMETHING SHOULD BE RENDERED HERE, RIGHT? @@ -620,9 +611,9 @@ public void Update() bestIntersectIndex = hotbarSlotIndex; } - if (bestIntersectIndex > -1 && bestIntersectIndex != _hotbarSlotIndex) + if (bestIntersectIndex > -1 && bestIntersectIndex != SlotIndex) { - Globals.Me.HotbarSwap(_hotbarSlotIndex, (byte)bestIntersectIndex); + Globals.Me.HotbarSwap(SlotIndex, (byte)bestIntersectIndex); } } @@ -630,7 +621,7 @@ public void Update() } else { - _contentPanel.IsHidden = true; + _iconImage.IsHidden = true; _equipLabel.IsHidden = true; _quantityLabel.IsHidden = true; _cooldownLabel.IsHidden = true; diff --git a/Intersect.Client.Core/Interface/Game/Inventory/InventoryItem.cs b/Intersect.Client.Core/Interface/Game/Inventory/InventoryItem.cs index c75f2dc985..966f3e776f 100644 --- a/Intersect.Client.Core/Interface/Game/Inventory/InventoryItem.cs +++ b/Intersect.Client.Core/Interface/Game/Inventory/InventoryItem.cs @@ -48,7 +48,8 @@ public partial class InventoryItem : SlotItem private readonly MenuItem _actionItemMenuItem; private readonly MenuItem _dropItemMenuItem; - public InventoryItem(InventoryWindow inventoryWindow, Base parent, int index, ContextMenu contextMenu) : base(parent, nameof(InventoryItem), index, contextMenu) + public InventoryItem(InventoryWindow inventoryWindow, Base parent, int index, ContextMenu contextMenu) + : base(parent, nameof(InventoryItem), index, contextMenu) { _inventoryWindow = inventoryWindow; TextureFilename = "inventoryitem.png"; @@ -97,14 +98,14 @@ public InventoryItem(InventoryWindow inventoryWindow, Base parent, int index, Co LoadJsonUi(GameContentManager.UI.InGame, Graphics.Renderer.GetResolutionString()); - _contextMenu.ClearChildren(); - _useItemMenuItem = _contextMenu.AddItem(Strings.ItemContextMenu.Use); + contextMenu.ClearChildren(); + _useItemMenuItem = contextMenu.AddItem(Strings.ItemContextMenu.Use); _useItemMenuItem.Clicked += _useItemContextItem_Clicked; - _dropItemMenuItem = _contextMenu.AddItem(Strings.ItemContextMenu.Drop); + _dropItemMenuItem = contextMenu.AddItem(Strings.ItemContextMenu.Drop); _dropItemMenuItem.Clicked += _dropItemContextItem_Clicked; - _actionItemMenuItem = _contextMenu.AddItem(Strings.ItemContextMenu.Bank); + _actionItemMenuItem = contextMenu.AddItem(Strings.ItemContextMenu.Bank); _actionItemMenuItem.Clicked += _actionItemContextItem_Clicked; - _contextMenu.LoadJsonUi(GameContentManager.UI.InGame, Graphics.Renderer.GetResolutionString()); + contextMenu.LoadJsonUi(GameContentManager.UI.InGame, Graphics.Renderer.GetResolutionString()); if (Globals.Me is { } player) { @@ -114,10 +115,10 @@ public InventoryItem(InventoryWindow inventoryWindow, Base parent, int index, Co #region Context Menu - public override void OpenContextMenu() + protected override void OnContextMenuOpening(ContextMenu contextMenu) { // Clear out the old options since we might not show all of them - _contextMenu.ClearChildren(); + contextMenu.ClearChildren(); if (Globals.Me?.Inventory[SlotIndex] is not { } inventorySlot) { @@ -134,24 +135,24 @@ public override void OpenContextMenu() switch (descriptor.ItemType) { case ItemType.Spell: - _contextMenu.AddChild(_useItemMenuItem); + contextMenu.AddChild(_useItemMenuItem); var useItemLabel = descriptor.QuickCast ? Strings.ItemContextMenu.Cast : Strings.ItemContextMenu.Learn; _useItemMenuItem.Text = useItemLabel.ToString(descriptor.Name); break; case ItemType.Event: case ItemType.Consumable: - _contextMenu.AddChild(_useItemMenuItem); + contextMenu.AddChild(_useItemMenuItem); _useItemMenuItem.Text = Strings.ItemContextMenu.Use.ToString(descriptor.Name); break; case ItemType.Bag: - _contextMenu.AddChild(_useItemMenuItem); + contextMenu.AddChild(_useItemMenuItem); _useItemMenuItem.Text = Strings.ItemContextMenu.Open.ToString(descriptor.Name); break; case ItemType.Equipment: - _contextMenu.AddChild(_useItemMenuItem); + contextMenu.AddChild(_useItemMenuItem); var equipItemLabel = Globals.Me.MyEquipment.Contains(SlotIndex) ? Strings.ItemContextMenu.Unequip : Strings.ItemContextMenu.Equip; _useItemMenuItem.Text = equipItemLabel.ToString(descriptor.Name); break; @@ -160,33 +161,33 @@ public override void OpenContextMenu() // Set up the correct contextual additional action. if (Globals.InBag && descriptor.CanBag) { - _contextMenu.AddChild(_actionItemMenuItem); + contextMenu.AddChild(_actionItemMenuItem); _actionItemMenuItem.SetText(Strings.ItemContextMenu.Bag.ToString(descriptor.Name)); } else if (Globals.InBank && (descriptor.CanBank || descriptor.CanGuildBank)) { - _contextMenu.AddChild(_actionItemMenuItem); + contextMenu.AddChild(_actionItemMenuItem); _actionItemMenuItem.SetText(Strings.ItemContextMenu.Bank.ToString(descriptor.Name)); } else if (Globals.InTrade && descriptor.CanTrade) { - _contextMenu.AddChild(_actionItemMenuItem); + contextMenu.AddChild(_actionItemMenuItem); _actionItemMenuItem.SetText(Strings.ItemContextMenu.Trade.ToString(descriptor.Name)); } else if (Globals.GameShop != null && descriptor.CanSell) { - _contextMenu.AddChild(_actionItemMenuItem); + contextMenu.AddChild(_actionItemMenuItem); _actionItemMenuItem.SetText(Strings.ItemContextMenu.Sell.ToString(descriptor.Name)); } // Can we drop this item? if so show the user! if (descriptor.CanDrop) { - _contextMenu.AddChild(_dropItemMenuItem); + contextMenu.AddChild(_dropItemMenuItem); _dropItemMenuItem.SetText(Strings.ItemContextMenu.Drop.ToString(descriptor.Name)); } - base.OpenContextMenu(); + base.OnContextMenuOpening(contextMenu); } private void _useItemContextItem_Clicked(Base sender, MouseButtonState arguments) diff --git a/Intersect.Client.Core/Interface/Game/Shop/ShopItem.cs b/Intersect.Client.Core/Interface/Game/Shop/ShopItem.cs index 5e776ad6f3..235eedf673 100644 --- a/Intersect.Client.Core/Interface/Game/Shop/ShopItem.cs +++ b/Intersect.Client.Core/Interface/Game/Shop/ShopItem.cs @@ -1,6 +1,5 @@ using Intersect.Client.Core; using Intersect.Client.Framework.File_Management; -using Intersect.Client.Framework.Gwen; using Intersect.Client.Framework.Gwen.Control; using Intersect.Client.Framework.Gwen.Control.EventArguments; using Intersect.Client.Framework.Gwen.Input; @@ -20,7 +19,8 @@ public partial class ShopItem : SlotItem private readonly MenuItem _buyMenuItem; private ItemDescriptionWindow? _itemDescWindow; - public ShopItem(ShopWindow shopWindow, Base parent, int index, ContextMenu contextMenu) : base(parent, nameof(ShopItem), index, contextMenu) + public ShopItem(ShopWindow shopWindow, Base parent, int index, ContextMenu contextMenu) + : base(parent, nameof(ShopItem), index, contextMenu) { _shopWindow = shopWindow; _mySlot = index; @@ -33,11 +33,10 @@ public ShopItem(ShopWindow shopWindow, Base parent, int index, ContextMenu conte LoadJsonUi(GameContentManager.UI.InGame, Graphics.Renderer.GetResolutionString()); - //TODO: Is this a memory leak? - _contextMenu.ClearChildren(); - _buyMenuItem = _contextMenu.AddItem(Strings.ShopContextMenu.Buy); + contextMenu.ClearChildren(); + _buyMenuItem = contextMenu.AddItem(Strings.ShopContextMenu.Buy); _buyMenuItem.Clicked += _buyMenuItem_Clicked; - _contextMenu.LoadJsonUi(GameContentManager.UI.InGame, Graphics.Renderer.GetResolutionString()); + contextMenu.LoadJsonUi(GameContentManager.UI.InGame, Graphics.Renderer.GetResolutionString()); LoadItem(); } @@ -124,7 +123,7 @@ private void _buyMenuItem_Clicked(Base sender, Framework.Gwen.Control.EventArgum Globals.Me?.TryBuyItem(_mySlot); } - public override void OpenContextMenu() + protected override void OnContextMenuOpening(ContextMenu contextMenu) { if (Globals.GameShop is not { SellingItems.Count: > 0 } gameShop) { @@ -136,8 +135,10 @@ public override void OpenContextMenu() return; } + contextMenu.ClearChildren(); + contextMenu.AddChild(_buyMenuItem); _buyMenuItem.SetText(Strings.ShopContextMenu.Buy.ToString(item.Name)); - base.OpenContextMenu(); + base.OnContextMenuOpening(contextMenu); } public void LoadItem() diff --git a/Intersect.Client.Core/Interface/Game/SlotItem.cs b/Intersect.Client.Core/Interface/Game/SlotItem.cs index afa7202fe1..fd47cfa32f 100644 --- a/Intersect.Client.Core/Interface/Game/SlotItem.cs +++ b/Intersect.Client.Core/Interface/Game/SlotItem.cs @@ -7,9 +7,9 @@ public partial class SlotItem : ImagePanel { public readonly int SlotIndex; protected readonly ImagePanel _iconImage; - protected readonly ContextMenu _contextMenu; + protected readonly ContextMenu? _contextMenu; - public SlotItem(Base parent, string name, int index, ContextMenu contextMenu) : base(parent, name) + public SlotItem(Base parent, string name, int index, ContextMenu? contextMenu) : base(parent, name) { SlotIndex = index; @@ -32,12 +32,22 @@ public virtual void Update() { } - public virtual void OpenContextMenu() + public void OpenContextMenu() + { + if (_contextMenu is not { } contextMenu) + { + return; + } + + OnContextMenuOpening(contextMenu); + } + + protected virtual void OnContextMenuOpening(ContextMenu contextMenu) { // Display our menu... If we have anything to display. - if (_contextMenu.Children.Count > 0) + if (contextMenu.Children.Count > 0) { - _contextMenu.Open(Pos.None); + contextMenu.Open(Pos.None); } } } diff --git a/Intersect.Client.Core/Interface/Game/Spells/SpellItem.cs b/Intersect.Client.Core/Interface/Game/Spells/SpellItem.cs index a142e7bad4..67a1c75c87 100644 --- a/Intersect.Client.Core/Interface/Game/Spells/SpellItem.cs +++ b/Intersect.Client.Core/Interface/Game/Spells/SpellItem.cs @@ -61,20 +61,20 @@ public SpellItem(SpellsWindow spellWindow, Base parent, int index, ContextMenu c LoadJsonUi(GameContentManager.UI.InGame, Graphics.Renderer.GetResolutionString()); - _contextMenu.ClearChildren(); - _useSpellMenuItem = _contextMenu.AddItem(Strings.SpellContextMenu.Cast.ToString()); + contextMenu.ClearChildren(); + _useSpellMenuItem = contextMenu.AddItem(Strings.SpellContextMenu.Cast.ToString()); _useSpellMenuItem.Clicked += _useSpellMenuItem_Clicked; - _forgetSpellMenuItem = _contextMenu.AddItem(Strings.SpellContextMenu.Forget.ToString()); + _forgetSpellMenuItem = contextMenu.AddItem(Strings.SpellContextMenu.Forget.ToString()); _forgetSpellMenuItem.Clicked += _forgetSpellMenuItem_Clicked; - _contextMenu.LoadJsonUi(GameContentManager.UI.InGame, Graphics.Renderer.GetResolutionString()); + contextMenu.LoadJsonUi(GameContentManager.UI.InGame, Graphics.Renderer.GetResolutionString()); } #region Context Menu - public void OpenContextMenu() + protected override void OnContextMenuOpening(ContextMenu contextMenu) { // Clear out the old options. - _contextMenu.ClearChildren(); + contextMenu.ClearChildren(); if (Globals.Me?.Spells is not { Length: > 0 } spellSlots) { @@ -88,18 +88,17 @@ public void OpenContextMenu() } // Add our use spell option. - _contextMenu.AddChild(_useSpellMenuItem); + contextMenu.AddChild(_useSpellMenuItem); _useSpellMenuItem.SetText(Strings.SpellContextMenu.Cast.ToString(spell.Name)); // If this spell is not bound, allow users to forget it! if (!spell.Bound) { - _contextMenu.AddChild(_forgetSpellMenuItem); + contextMenu.AddChild(_forgetSpellMenuItem); _forgetSpellMenuItem.SetText(Strings.SpellContextMenu.Forget.ToString(spell.Name)); } - _contextMenu.SizeToChildren(); - _contextMenu.Open(Pos.None); + base.OnContextMenuOpening(contextMenu); } private void _useSpellMenuItem_Clicked(Base sender, MouseButtonState arguments) @@ -239,7 +238,6 @@ public override void Update() _descriptionWindow?.Dispose(); _descriptionWindow = null; - _iconImage_HoverEnter(null, null); } if (!IsDragging)