Skip to content

Commit

Permalink
Handle item equip from inventory via double-click
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanmoffat committed Mar 27, 2022
1 parent bacb8b2 commit 8701325
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 38 deletions.
4 changes: 3 additions & 1 deletion EOLib/PacketHandlers/ItemEquipHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ protected bool HandlePaperdollPacket(IPacket packet, bool itemUnequipped)
none: () => new InventoryItem(itemId, amount));

_characterInventoryRepository.ItemInventory.RemoveWhere(x => x.ItemID == itemId);
_characterInventoryRepository.ItemInventory.Add(updatedItem);

if (updatedItem.Amount > 0)
_characterInventoryRepository.ItemInventory.Add(updatedItem);
}
else
{
Expand Down
64 changes: 42 additions & 22 deletions EndlessClient/HUD/Inventory/InventoryPanelItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
using EOLib.Localization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Diagnostics;
using XNAControls;

namespace EndlessClient.HUD.Inventory
{
public class InventoryPanelItem : XNAControl
{
private readonly IItemStringService _itemStringService;
private readonly IStatusLabelSetter _statusLabelSetter;
private readonly InventoryPanel _inventoryPanel;
private readonly EIFRecord _data;
Expand All @@ -24,7 +26,9 @@ public class InventoryPanelItem : XNAControl
private readonly XNALabel _nameLabel;

private int _slot;
private IInventoryItem _item;

private readonly Stopwatch _clickTimer;
private int _recentClicks;

public int Slot
{
Expand All @@ -36,31 +40,28 @@ public int Slot
}
}

public IInventoryItem InventoryItem
public IInventoryItem InventoryItem { get; set; }

public string Text
{
get => _item;
get => _nameLabel.Text;
set
{
_item = value;
_nameLabel.Text = _itemStringService.GetStringForMapDisplay(_data, _item.Amount);
_nameLabel.Text = value;
_nameLabel.ResizeBasedOnText(16, 9);
}
}

public InventoryPanelItem(
INativeGraphicsManager nativeGraphicsManager,
IItemStringService itemStringService,
IStatusLabelSetter statusLabelSetter,
InventoryPanel inventoryPanel, int slot, IInventoryItem inventoryItem, EIFRecord data)
public event EventHandler<EIFRecord> DoubleClick;

public InventoryPanelItem(InventoryPanel inventoryPanel, int slot, IInventoryItem inventoryItem, EIFRecord data)
{
_itemStringService = itemStringService;
_statusLabelSetter = statusLabelSetter;
_inventoryPanel = inventoryPanel;
_data = data;
Slot = slot;
_item = inventoryItem;
InventoryItem = inventoryItem;
_data = data;

_itemGraphic = nativeGraphicsManager.TextureFromResource(GFXTypes.Items, 2 * data.Graphic, transparent: true);
_itemGraphic = inventoryPanel.NativeGraphicsManager.TextureFromResource(GFXTypes.Items, 2 * data.Graphic, transparent: true);
_highlightBackground = new Texture2D(Game.GraphicsDevice, 1, 1);
_highlightBackground.SetData(new[] { Color.FromNonPremultiplied(200, 200, 200, 60) });

Expand All @@ -71,18 +72,16 @@ public InventoryPanelItem(
TextAlign = LabelAlignment.MiddleCenter,
ForeColor = ColorConstants.LightGrayText,
BackColor = Color.FromNonPremultiplied(30, 30, 30, 160),
Text = _itemStringService.GetStringForMapDisplay(data, inventoryItem.Amount)
Text = string.Empty
};

OnMouseEnter += (_, _) =>
{
_nameLabel.Visible = true;
_statusLabelSetter.SetStatusLabel(EOResourceID.STATUS_LABEL_TYPE_ITEM, _nameLabel.Text);
};
OnMouseEnter += (_, _) => _nameLabel.Visible = true;
OnMouseLeave += (_, _) => _nameLabel.Visible = false;

var (slotWidth, slotHeight) = _data.Size.GetDimensions();
SetSize(slotWidth * 26, slotHeight * 26);

_clickTimer = new Stopwatch();
}

public override void Initialize()
Expand All @@ -96,6 +95,27 @@ public override void Initialize()

protected override void OnUpdateControl(GameTime gameTime)
{
if (_recentClicks > 0 && _clickTimer.Elapsed.TotalSeconds > 1)
{
_clickTimer.Restart();
_recentClicks--;
}

if (MouseOver && MouseOverPreviously)
{
if (CurrentMouseState.LeftButton == ButtonState.Released && PreviousMouseState.LeftButton == ButtonState.Pressed)
{
_clickTimer.Restart();
_recentClicks++;

if (_recentClicks == 2)
{
DoubleClick?.Invoke(this, _data);
_recentClicks = 0;
}
}
}

base.OnUpdateControl(gameTime);
}

Expand Down
16 changes: 12 additions & 4 deletions EndlessClient/HUD/Panels/HudPanelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class HudPanelFactory : IHudPanelFactory

private readonly INativeGraphicsManager _nativeGraphicsManager;
private readonly IInGameDialogActions _inGameDialogActions;
private readonly ICharacterActions _characterActions;
private readonly IContentProvider _contentProvider;
private readonly IHudControlProvider _hudControlProvider;
private readonly INewsProvider _newsProvider;
Expand All @@ -33,7 +34,8 @@ public class HudPanelFactory : IHudPanelFactory
private readonly ICharacterProvider _characterProvider;
private readonly ICharacterInventoryProvider _characterInventoryProvider;
private readonly IExperienceTableProvider _experienceTableProvider;
private readonly IEIFFileProvider _eifFileProvider;
private readonly IPubFileProvider _pubFileProvider;
private readonly IPaperdollProvider _paperdollProvider;
private readonly IEOMessageBoxFactory _messageBoxFactory;
private readonly ITrainingController _trainingController;
private readonly IFriendIgnoreListService _friendIgnoreListService;
Expand All @@ -43,6 +45,7 @@ public class HudPanelFactory : IHudPanelFactory

public HudPanelFactory(INativeGraphicsManager nativeGraphicsManager,
IInGameDialogActions inGameDialogActions,
ICharacterActions characterActions,
IContentProvider contentProvider,
IHudControlProvider hudControlProvider,
INewsProvider newsProvider,
Expand All @@ -51,7 +54,8 @@ public HudPanelFactory(INativeGraphicsManager nativeGraphicsManager,
ICharacterProvider characterProvider,
ICharacterInventoryProvider characterInventoryProvider,
IExperienceTableProvider experienceTableProvider,
IEIFFileProvider eifFileProvider,
IPubFileProvider pubFileProvider,
IPaperdollProvider paperdollProvider,
IEOMessageBoxFactory messageBoxFactory,
ITrainingController trainingController,
IFriendIgnoreListService friendIgnoreListService,
Expand All @@ -61,6 +65,7 @@ public HudPanelFactory(INativeGraphicsManager nativeGraphicsManager,
{
_nativeGraphicsManager = nativeGraphicsManager;
_inGameDialogActions = inGameDialogActions;
_characterActions = characterActions;
_contentProvider = contentProvider;
_hudControlProvider = hudControlProvider;
_newsProvider = newsProvider;
Expand All @@ -69,7 +74,8 @@ public HudPanelFactory(INativeGraphicsManager nativeGraphicsManager,
_characterProvider = characterProvider;
_characterInventoryProvider = characterInventoryProvider;
_experienceTableProvider = experienceTableProvider;
_eifFileProvider = eifFileProvider;
_pubFileProvider = pubFileProvider;
_paperdollProvider = paperdollProvider;
_messageBoxFactory = messageBoxFactory;
_trainingController = trainingController;
_friendIgnoreListService = friendIgnoreListService;
Expand All @@ -92,13 +98,15 @@ public InventoryPanel CreateInventoryPanel()
{
return new InventoryPanel(_nativeGraphicsManager,
_inGameDialogActions,
_characterActions,
_statusLabelSetter,
_itemStringService,
_inventoryService,
_playerInfoProvider,
_characterProvider,
_paperdollProvider,
_characterInventoryProvider,
_eifFileProvider) { DrawOrder = HUD_CONTROL_LAYER };
_pubFileProvider) { DrawOrder = HUD_CONTROL_LAYER };
}

public ActiveSpellsPanel CreateActiveSpellsPanel()
Expand Down
Loading

0 comments on commit 8701325

Please sign in to comment.