From b7e3540ea38fc7c381428a8f12647c934dc17980 Mon Sep 17 00:00:00 2001 From: Ethan Moffat Date: Sun, 27 Mar 2022 18:52:32 -0700 Subject: [PATCH] Extract ItemNameColorService for getting colors for item name labels in inventory/map --- .../HUD/Inventory/InventoryPanelItem.cs | 4 +- EndlessClient/HUD/ItemNameColorService.cs | 37 +++++++++++++++++++ EndlessClient/HUD/Panels/HudPanelFactory.cs | 4 ++ EndlessClient/HUD/Panels/InventoryPanel.cs | 5 ++- .../Factories/MouseCursorRendererFactory.cs | 5 +++ .../Rendering/MouseCursorRenderer.cs | 21 +++-------- 6 files changed, 57 insertions(+), 19 deletions(-) create mode 100644 EndlessClient/HUD/ItemNameColorService.cs diff --git a/EndlessClient/HUD/Inventory/InventoryPanelItem.cs b/EndlessClient/HUD/Inventory/InventoryPanelItem.cs index 6c4d830f9..f91bdc04a 100644 --- a/EndlessClient/HUD/Inventory/InventoryPanelItem.cs +++ b/EndlessClient/HUD/Inventory/InventoryPanelItem.cs @@ -81,7 +81,7 @@ public string Text public event EventHandler DoubleClick; public event EventHandler 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; @@ -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 }; diff --git a/EndlessClient/HUD/ItemNameColorService.cs b/EndlessClient/HUD/ItemNameColorService.cs new file mode 100644 index 000000000..d37a3217c --- /dev/null +++ b/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); + } +} diff --git a/EndlessClient/HUD/Panels/HudPanelFactory.cs b/EndlessClient/HUD/Panels/HudPanelFactory.cs index 6c14ed107..7100cd0eb 100644 --- a/EndlessClient/HUD/Panels/HudPanelFactory.cs +++ b/EndlessClient/HUD/Panels/HudPanelFactory.cs @@ -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, @@ -61,6 +62,7 @@ public class HudPanelFactory : IHudPanelFactory IFriendIgnoreListService friendIgnoreListService, IStatusLabelSetter statusLabelSetter, IItemStringService itemStringService, + IItemNameColorService itemNameColorService, IInventoryService inventoryService) { _nativeGraphicsManager = nativeGraphicsManager; @@ -81,6 +83,7 @@ public class HudPanelFactory : IHudPanelFactory _friendIgnoreListService = friendIgnoreListService; _statusLabelSetter = statusLabelSetter; _itemStringService = itemStringService; + _itemNameColorService = itemNameColorService; _inventoryService = inventoryService; } @@ -101,6 +104,7 @@ public InventoryPanel CreateInventoryPanel() _characterActions, _statusLabelSetter, _itemStringService, + _itemNameColorService, _inventoryService, _playerInfoProvider, _characterProvider, diff --git a/EndlessClient/HUD/Panels/InventoryPanel.cs b/EndlessClient/HUD/Panels/InventoryPanel.cs index b867bce02..c00381980 100644 --- a/EndlessClient/HUD/Panels/InventoryPanel.cs +++ b/EndlessClient/HUD/Panels/InventoryPanel.cs @@ -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; @@ -58,6 +59,7 @@ public class InventoryPanel : XNAPanel, IHudPanel ICharacterActions characterActions, IStatusLabelSetter statusLabelSetter, IItemStringService itemStringService, + IItemNameColorService itemNameColorService, IInventoryService inventoryService, IPlayerInfoProvider playerInfoProvider, ICharacterProvider characterProvider, @@ -69,6 +71,7 @@ public class InventoryPanel : XNAPanel, IHudPanel _characterActions = characterActions; _statusLabelSetter = statusLabelSetter; _itemStringService = itemStringService; + _itemNameColorService = itemNameColorService; _inventoryService = inventoryService; _playerInfoProvider = playerInfoProvider; _characterProvider = characterProvider; @@ -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); diff --git a/EndlessClient/Rendering/Factories/MouseCursorRendererFactory.cs b/EndlessClient/Rendering/Factories/MouseCursorRendererFactory.cs index eb9084944..19c444b9e 100644 --- a/EndlessClient/Rendering/Factories/MouseCursorRendererFactory.cs +++ b/EndlessClient/Rendering/Factories/MouseCursorRendererFactory.cs @@ -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; @@ -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; @@ -30,6 +32,7 @@ public class MouseCursorRendererFactory : IMouseCursorRendererFactory IRenderOffsetCalculator renderOffsetCalculator, IMapCellStateProvider mapCellStateProvider, IItemStringService itemStringService, + IItemNameColorService itemNameColorService, IEIFFileProvider eifFileProvider, ICurrentMapProvider currentMapProvider, IMapInteractionController mapInteractionController, @@ -42,6 +45,7 @@ public class MouseCursorRendererFactory : IMouseCursorRendererFactory _renderOffsetCalculator = renderOffsetCalculator; _mapCellStateProvider = mapCellStateProvider; _itemStringService = itemStringService; + _itemNameColorService = itemNameColorService; _eifFileProvider = eifFileProvider; _currentMapProvider = currentMapProvider; _mapInteractionController = mapInteractionController; @@ -57,6 +61,7 @@ public IMouseCursorRenderer Create() _renderOffsetCalculator, _mapCellStateProvider, _itemStringService, + _itemNameColorService, _eifFileProvider, _currentMapProvider, _mapInteractionController, diff --git a/EndlessClient/Rendering/MouseCursorRenderer.cs b/EndlessClient/Rendering/MouseCursorRenderer.cs index acd7d0434..f5bca6a43 100644 --- a/EndlessClient/Rendering/MouseCursorRenderer.cs +++ b/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; @@ -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; @@ -63,6 +65,7 @@ private enum CursorIndex IRenderOffsetCalculator renderOffsetCalculator, IMapCellStateProvider mapCellStateProvider, IItemStringService itemStringService, + IItemNameColorService itemNameColorService, IEIFFileProvider eifFileProvider, ICurrentMapProvider currentMapProvider, IMapInteractionController mapInteractionController, @@ -75,6 +78,7 @@ private enum CursorIndex _renderOffsetCalculator = renderOffsetCalculator; _mapCellStateProvider = mapCellStateProvider; _itemStringService = itemStringService; + _itemNameColorService = itemNameColorService; _eifFileProvider = eifFileProvider; _currentMapProvider = currentMapProvider; _mapInteractionController = mapInteractionController; @@ -219,7 +223,7 @@ private void UpdateMapItemLabel(Option 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, @@ -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;