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

cleanup shop window/item #2623

Merged
merged 19 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
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
10 changes: 4 additions & 6 deletions Intersect.Client.Core/Interface/Game/GameInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,7 @@ public void NotifyCloseShop()

public void OpenShop()
{
mShopWindow?.Close();

mShopWindow = new ShopWindow(GameCanvas);
mShopWindow = new ShopWindow(GameCanvas) { DeleteOnClose = true };
mShouldOpenShop = false;
}

Expand Down Expand Up @@ -392,7 +390,7 @@ public void Update(TimeSpan elapsed, TimeSpan total)
GameMenu.OpenInventory();
}

if (mShopWindow != null && (!mShopWindow.IsVisible() || mShouldCloseShop))
if (mShopWindow != null && (!mShopWindow.IsVisibleInTree || mShouldCloseShop))
{
CloseShop();
}
Expand Down Expand Up @@ -521,7 +519,7 @@ public void Draw(TimeSpan elapsed, TimeSpan total)
private void CloseShop()
{
Globals.GameShop = null;
mShopWindow?.Close();
mShopWindow?.Hide();
mShopWindow = null;
PacketSender.SendCloseShop();
}
Expand Down Expand Up @@ -585,7 +583,7 @@ public bool CloseAllWindows()
closedWindows = true;
}

if (mShopWindow != null && mShopWindow.IsVisible())
if (mShopWindow is { IsVisibleInTree: true })
{
CloseShop();
closedWindows = true;
Expand Down
211 changes: 121 additions & 90 deletions Intersect.Client.Core/Interface/Game/Shop/ShopItem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Intersect.Client.Framework.GenericClasses;
using Intersect.Client.Framework.Graphics;
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;
Expand All @@ -13,57 +14,114 @@

namespace Intersect.Client.Interface.Game.Shop;


public partial class ShopItem
public partial class ShopItem : ImagePanel
{
private readonly int _mySlot;
private readonly ShopWindow _shopWindow;
public ImagePanel _iconImage;
private readonly ContextMenu _contextMenu;
private readonly MenuItem _buyMenuItem;
private ItemDescriptionWindow? _itemDescWindow;

public ShopItem(ShopWindow shopWindow, Base parent, int index) : base(parent, nameof(ShopItem))
{
_shopWindow = shopWindow;
_mySlot = index;

public ImagePanel Container;
MinimumSize = new Point(34, 34);
Margin = new Margin(4, 4, 4, 4);
MouseInputEnabled = true;
TextureFilename = "shopitem.png";

private int mCurrentItem = -2;
_iconImage = new ImagePanel(this, "ShopItemIcon")
{
MinimumSize = new Point(32, 32),
MouseInputEnabled = true,
Alignment = [Alignments.Center],
HoverSound = "octave-tap-resonant.wav",
};
_iconImage.HoverEnter += _iconImage_HoverEnter;
_iconImage.HoverLeave += _iconImage_HoverLeave;
_iconImage.Clicked += _iconImage_RightClicked;
_iconImage.DoubleClicked += _iconImage_DoubleClicked;

private ItemDescriptionWindow mDescWindow;
LoadJsonUi(GameContentManager.UI.InGame, Graphics.Renderer.GetResolutionString());

private bool mIsEquipped;
// Generate our context menu with basic options.
// TODO: Refactor so shop only has 1 context menu shared between all items
_contextMenu = new ContextMenu(Interface.CurrentInterface.Root, "ShopContextMenu")
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a // TODO: Refactor so shop only has 1 context menu shared between all items here

IsVisibleInParent = false,
IconMarginDisabled = true,
ItemFont = GameContentManager.Current.GetFont(name: "sourcesansproblack"),
ItemFontSize = 10,
};

//Mouse Event Variables
private bool mMouseOver;
//TODO: Is this a memory leak?
_contextMenu.ClearChildren();
_buyMenuItem = _contextMenu.AddItem(Strings.ShopContextMenu.Buy);
_buyMenuItem.Clicked += _buyMenuItem_Clicked;
_contextMenu.LoadJsonUi(GameContentManager.UI.InGame, Graphics.Renderer.GetResolutionString());

private int mMouseX = -1;
LoadItem();
}

private int mMouseY = -1;
private void _iconImage_HoverEnter(Base sender, EventArgs arguments)
{
if (InputHandler.MouseFocus != null)
{
return;
}

//Slot info
private int mMySlot;
if (Globals.InputManager.IsMouseButtonDown(MouseButton.Left))
{
return;
}

//Textures
private IGameRenderTexture mSfTex;
if (_itemDescWindow != default)
{
_itemDescWindow.Dispose();
_itemDescWindow = default;
}

//Drag/Drop References
private ShopWindow mShopWindow;
if (Globals.GameShop is not { SellingItems.Count: > 0 } gameShop)
{
return;
}

public ImagePanel Pnl;
if (!ItemBase.TryGet(gameShop.SellingItems[_mySlot].CostItemId, out var item))
{
return;
}

public ShopItem(ShopWindow shopWindow, int index)
{
mShopWindow = shopWindow;
mMySlot = index;
}
if (gameShop.SellingItems[_mySlot].Item != default)
{
ItemProperties itemProperty = new ItemProperties()
{
StatModifiers = item.StatsGiven,
};

public void Setup()
{
Pnl = new ImagePanel(Container, "ShopItemIcon");
Pnl.HoverEnter += pnl_HoverEnter;
Pnl.HoverLeave += pnl_HoverLeave;
Pnl.Clicked += Pnl_RightClicked;
Pnl.DoubleClicked += Pnl_DoubleClicked;
_itemDescWindow = new ItemDescriptionWindow(
item: gameShop.SellingItems[_mySlot].Item,
amount: 1,
x: _shopWindow.X,
y: _shopWindow.Y,
itemProperties: itemProperty,
valueLabel: Strings.Shop.Costs.ToString(gameShop.SellingItems[_mySlot].CostItemQuantity, item.Name)
);
}
}

private void Pnl_DoubleClicked(Base sender, MouseButtonState arguments)
private void _iconImage_HoverLeave(Base sender, EventArgs arguments)
{
Globals.Me?.TryBuyItem(mMySlot);
if (_itemDescWindow != null)
{
_itemDescWindow.Dispose();
_itemDescWindow = null;
}
}

private void Pnl_RightClicked(Base sender, MouseButtonState arguments)
private void _iconImage_RightClicked(Base sender, MouseButtonState arguments)
{
if (arguments.MouseButton != MouseButton.Right)
{
Expand All @@ -72,90 +130,63 @@ private void Pnl_RightClicked(Base sender, MouseButtonState arguments)

if (ClientConfiguration.Instance.EnableContextMenus)
{
mShopWindow.OpenContextMenu(mMySlot);
OpenContextMenu(_mySlot);
}
else
{
Pnl_DoubleClicked(sender, arguments);
_iconImage_DoubleClicked(sender, arguments);
}
}

public void LoadItem()
private void _iconImage_DoubleClicked(Base sender, MouseButtonState arguments)
{
var item = ItemBase.Get(Globals.GameShop.SellingItems[mMySlot].ItemId);
if (item != null)
{
var itemTex = Globals.ContentManager.GetTexture(Framework.Content.TextureType.Item, item.Icon);
if (itemTex != null)
{
Pnl.Texture = itemTex;
Pnl.RenderColor = item.Color;
}
}
Globals.Me?.TryBuyItem(_mySlot);
}

private void _buyMenuItem_Clicked(Base sender, Framework.Gwen.Control.EventArguments.MouseButtonState arguments)
{
Globals.Me?.TryBuyItem(_mySlot);
}


void pnl_HoverLeave(Base sender, EventArgs arguments)
protected override void Dispose(bool disposing)
{
mMouseOver = false;
mMouseX = -1;
mMouseY = -1;
if (mDescWindow != null)
{
mDescWindow.Dispose();
mDescWindow = null;
}
_contextMenu?.Close();
base.Dispose(disposing);
}

void pnl_HoverEnter(Base sender, EventArgs arguments)
public void OpenContextMenu(int slot)
{
if (InputHandler.MouseFocus != null)
if (Globals.GameShop is not { SellingItems.Count: > 0 } gameShop)
{
return;
}

if (Globals.InputManager.IsMouseButtonDown(MouseButton.Left))
if (!ItemBase.TryGet(gameShop.SellingItems[slot].ItemId, out var item))
{
return;
}

if (mDescWindow != null)
_buyMenuItem.SetText(Strings.ShopContextMenu.Buy.ToString(item.Name));
_contextMenu.Open(Pos.None);
}

public void LoadItem()
{
if (Globals.GameShop is not { SellingItems.Count: > 0 } gameShop)
{
mDescWindow.Dispose();
mDescWindow = null;
return;
}

var item = ItemBase.Get(Globals.GameShop.SellingItems[mMySlot].CostItemId);
if (item != null && Globals.GameShop.SellingItems[mMySlot].Item != null)
if (!ItemBase.TryGet(gameShop.SellingItems[_mySlot].ItemId, out var itemDescriptor))
{
ItemProperties itemProperty = new ItemProperties()
{
StatModifiers = item.StatsGiven,
};

mDescWindow = new ItemDescriptionWindow(
Globals.GameShop.SellingItems[mMySlot].Item, 1, mShopWindow.X, mShopWindow.Y, itemProperty, "",
Strings.Shop.Costs.ToString(Globals.GameShop.SellingItems[mMySlot].CostItemQuantity, item.Name)
);
return;
}
}

public FloatRect RenderBounds()
{
var rect = new FloatRect()
var itemTex = Globals.ContentManager?.GetTexture(Framework.Content.TextureType.Item, itemDescriptor.Icon);
if (itemTex != null)
{
X = Pnl.ToCanvas(new Point(0, 0)).X,
Y = Pnl.ToCanvas(new Point(0, 0)).Y,
Width = Pnl.Width,
Height = Pnl.Height
};

return rect;
}

public void Update()
{
_iconImage.Texture = itemTex;
_iconImage.RenderColor = itemDescriptor.Color;
}
}

}
Loading
Loading