Skip to content

Commit

Permalink
Extract ItemNameColorService for getting colors for item name labels …
Browse files Browse the repository at this point in the history
…in inventory/map
  • Loading branch information
ethanmoffat committed Mar 28, 2022
1 parent 5d01f6e commit b7e3540
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 19 deletions.
4 changes: 2 additions & 2 deletions EndlessClient/HUD/Inventory/InventoryPanelItem.cs
Expand Up @@ -81,7 +81,7 @@ public string Text
public event EventHandler<EIFRecord> DoubleClick;
public event EventHandler<ItemDragCompletedEventArgs> DoneDragging;

public InventoryPanelItem(InventoryPanel inventoryPanel, int slot, IInventoryItem inventoryItem, EIFRecord data)
public InventoryPanelItem(IItemNameColorService itemNameColorService, InventoryPanel inventoryPanel, int slot, IInventoryItem inventoryItem, EIFRecord data)
{
_inventoryPanel = inventoryPanel;
Slot = slot;
Expand All @@ -97,7 +97,7 @@ public InventoryPanelItem(InventoryPanel inventoryPanel, int slot, IInventoryIte
Visible = false,
AutoSize = false,
TextAlign = LabelAlignment.MiddleCenter,
ForeColor = ColorConstants.LightGrayText,
ForeColor = itemNameColorService.GetColorForInventoryDisplay(Data),
BackColor = Color.FromNonPremultiplied(30, 30, 30, 160),
Text = string.Empty
};
Expand Down
37 changes: 37 additions & 0 deletions EndlessClient/HUD/ItemNameColorService.cs
@@ -0,0 +1,37 @@
using AutomaticTypeMapper;
using EOLib.Graphics;
using EOLib.IO;
using EOLib.IO.Pub;
using Microsoft.Xna.Framework;

namespace EndlessClient.HUD
{
[AutoMappedType]
public class ItemNameColorService : IItemNameColorService
{
public Color GetColorForInventoryDisplay(EIFRecord itemData) => GetColor(itemData, ColorConstants.LightGrayText);

public Color GetColorForMapDisplay(EIFRecord itemData) => GetColor(itemData, Color.White);

private static Color GetColor(EIFRecord itemData, Color defaultColor)
{
switch (itemData.Special)
{
case ItemSpecial.Lore:
case ItemSpecial.Unique:
return Color.FromNonPremultiplied(0xff, 0xf0, 0xa5, 0xff);
case ItemSpecial.Rare:
return Color.FromNonPremultiplied(0xf5, 0xc8, 0x9c, 0xff);
}

return defaultColor;
}
}

public interface IItemNameColorService
{
Color GetColorForMapDisplay(EIFRecord itemData);

Color GetColorForInventoryDisplay(EIFRecord itemData);
}
}
4 changes: 4 additions & 0 deletions EndlessClient/HUD/Panels/HudPanelFactory.cs
Expand Up @@ -41,6 +41,7 @@ public class HudPanelFactory : IHudPanelFactory
private readonly IFriendIgnoreListService _friendIgnoreListService;
private readonly IStatusLabelSetter _statusLabelSetter;
private readonly IItemStringService _itemStringService;
private readonly IItemNameColorService _itemNameColorService;
private readonly IInventoryService _inventoryService;

public HudPanelFactory(INativeGraphicsManager nativeGraphicsManager,
Expand All @@ -61,6 +62,7 @@ public class HudPanelFactory : IHudPanelFactory
IFriendIgnoreListService friendIgnoreListService,
IStatusLabelSetter statusLabelSetter,
IItemStringService itemStringService,
IItemNameColorService itemNameColorService,
IInventoryService inventoryService)
{
_nativeGraphicsManager = nativeGraphicsManager;
Expand All @@ -81,6 +83,7 @@ public class HudPanelFactory : IHudPanelFactory
_friendIgnoreListService = friendIgnoreListService;
_statusLabelSetter = statusLabelSetter;
_itemStringService = itemStringService;
_itemNameColorService = itemNameColorService;
_inventoryService = inventoryService;
}

Expand All @@ -101,6 +104,7 @@ public InventoryPanel CreateInventoryPanel()
_characterActions,
_statusLabelSetter,
_itemStringService,
_itemNameColorService,
_inventoryService,
_playerInfoProvider,
_characterProvider,
Expand Down
5 changes: 4 additions & 1 deletion EndlessClient/HUD/Panels/InventoryPanel.cs
Expand Up @@ -32,6 +32,7 @@ public class InventoryPanel : XNAPanel, IHudPanel
private readonly ICharacterActions _characterActions;
private readonly IStatusLabelSetter _statusLabelSetter;
private readonly IItemStringService _itemStringService;
private readonly IItemNameColorService _itemNameColorService;
private readonly IInventoryService _inventoryService;
private readonly IPlayerInfoProvider _playerInfoProvider;
private readonly ICharacterProvider _characterProvider;
Expand All @@ -58,6 +59,7 @@ public class InventoryPanel : XNAPanel, IHudPanel
ICharacterActions characterActions,
IStatusLabelSetter statusLabelSetter,
IItemStringService itemStringService,
IItemNameColorService itemNameColorService,
IInventoryService inventoryService,
IPlayerInfoProvider playerInfoProvider,
ICharacterProvider characterProvider,
Expand All @@ -69,6 +71,7 @@ public class InventoryPanel : XNAPanel, IHudPanel
_characterActions = characterActions;
_statusLabelSetter = statusLabelSetter;
_itemStringService = itemStringService;
_itemNameColorService = itemNameColorService;
_inventoryService = inventoryService;
_playerInfoProvider = playerInfoProvider;
_characterProvider = characterProvider;
Expand Down Expand Up @@ -186,7 +189,7 @@ protected override void OnUpdateControl(GameTime gameTime)
{
_inventoryService.SetSlots(_usedSlots, slot, itemData.Size);
var newItem = new InventoryPanelItem(this, slot, item, itemData);
var newItem = new InventoryPanelItem(_itemNameColorService, this, slot, item, itemData);
newItem.Initialize();
newItem.SetParentControl(this);
newItem.Text = _itemStringService.GetStringForMapDisplay(itemData, item.Amount);
Expand Down
@@ -1,6 +1,7 @@
using AutomaticTypeMapper;
using EndlessClient.Controllers;
using EndlessClient.Dialogs;
using EndlessClient.HUD;
using EndlessClient.Input;
using EOLib.Domain.Character;
using EOLib.Domain.Item;
Expand All @@ -18,6 +19,7 @@ public class MouseCursorRendererFactory : IMouseCursorRendererFactory
private readonly IRenderOffsetCalculator _renderOffsetCalculator;
private readonly IMapCellStateProvider _mapCellStateProvider;
private readonly IItemStringService _itemStringService;
private readonly IItemNameColorService _itemNameColorService;
private readonly IEIFFileProvider _eifFileProvider;
private readonly ICurrentMapProvider _currentMapProvider;
private readonly IMapInteractionController _mapInteractionController;
Expand All @@ -30,6 +32,7 @@ public class MouseCursorRendererFactory : IMouseCursorRendererFactory
IRenderOffsetCalculator renderOffsetCalculator,
IMapCellStateProvider mapCellStateProvider,
IItemStringService itemStringService,
IItemNameColorService itemNameColorService,
IEIFFileProvider eifFileProvider,
ICurrentMapProvider currentMapProvider,
IMapInteractionController mapInteractionController,
Expand All @@ -42,6 +45,7 @@ public class MouseCursorRendererFactory : IMouseCursorRendererFactory
_renderOffsetCalculator = renderOffsetCalculator;
_mapCellStateProvider = mapCellStateProvider;
_itemStringService = itemStringService;
_itemNameColorService = itemNameColorService;
_eifFileProvider = eifFileProvider;
_currentMapProvider = currentMapProvider;
_mapInteractionController = mapInteractionController;
Expand All @@ -57,6 +61,7 @@ public IMouseCursorRenderer Create()
_renderOffsetCalculator,
_mapCellStateProvider,
_itemStringService,
_itemNameColorService,
_eifFileProvider,
_currentMapProvider,
_mapInteractionController,
Expand Down
21 changes: 5 additions & 16 deletions EndlessClient/Rendering/MouseCursorRenderer.cs
@@ -1,5 +1,6 @@
using EndlessClient.Controllers;
using EndlessClient.Dialogs;
using EndlessClient.HUD;
using EndlessClient.Input;
using EOLib;
using EOLib.Domain.Character;
Expand Down Expand Up @@ -40,6 +41,7 @@ private enum CursorIndex
private readonly IRenderOffsetCalculator _renderOffsetCalculator;
private readonly IMapCellStateProvider _mapCellStateProvider;
private readonly IItemStringService _itemStringService;
private readonly IItemNameColorService _itemNameColorService;
private readonly IEIFFileProvider _eifFileProvider;
private readonly ICurrentMapProvider _currentMapProvider;
private readonly IMapInteractionController _mapInteractionController;
Expand All @@ -63,6 +65,7 @@ private enum CursorIndex
IRenderOffsetCalculator renderOffsetCalculator,
IMapCellStateProvider mapCellStateProvider,
IItemStringService itemStringService,
IItemNameColorService itemNameColorService,
IEIFFileProvider eifFileProvider,
ICurrentMapProvider currentMapProvider,
IMapInteractionController mapInteractionController,
Expand All @@ -75,6 +78,7 @@ private enum CursorIndex
_renderOffsetCalculator = renderOffsetCalculator;
_mapCellStateProvider = mapCellStateProvider;
_itemStringService = itemStringService;
_itemNameColorService = itemNameColorService;
_eifFileProvider = eifFileProvider;
_currentMapProvider = currentMapProvider;
_mapInteractionController = mapInteractionController;
Expand Down Expand Up @@ -219,7 +223,7 @@ private void UpdateMapItemLabel(Option<IItem> item)
_mapItemText.Visible = true;
_mapItemText.Text = text;
_mapItemText.ResizeBasedOnText();
_mapItemText.ForeColor = GetColorForMapDisplay(data);
_mapItemText.ForeColor = _itemNameColorService.GetColorForMapDisplay(data);
//relative to cursor DrawPosition, since this control is a parent of MapItemText
_mapItemText.DrawPosition = new Vector2(_drawArea.X + 32 - _mapItemText.ActualWidth / 2f,
Expand Down Expand Up @@ -280,21 +284,6 @@ private void UpdateCursorIndexForTileSpec(TileSpec tileSpec)
}
}

//todo: extract this into a service (also used by inventory)
private static Color GetColorForMapDisplay(EIFRecord record)
{
switch (record.Special)
{
case ItemSpecial.Lore:
case ItemSpecial.Unique:
return Color.FromNonPremultiplied(0xff, 0xf0, 0xa5, 0xff);
case ItemSpecial.Rare:
return Color.FromNonPremultiplied(0xf5, 0xc8, 0x9c, 0xff);
}

return Color.White;
}

private async Task CheckForClicks(IMapCellState cellState)
{
var currentMouseState = _userInputProvider.CurrentMouseState;
Expand Down

0 comments on commit b7e3540

Please sign in to comment.