Skip to content

Commit e41a46c

Browse files
committed
more refactor and cleaning
1 parent 7b40b3e commit e41a46c

File tree

3 files changed

+142
-49
lines changed

3 files changed

+142
-49
lines changed

Intersect.Client.Core/Interface/Game/Shop/ShopItem.cs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public ShopItem(ShopWindow shopWindow, Base parent, int index) : base(parent, na
2828
_shopWindow = shopWindow;
2929
_mySlot = index;
3030

31-
MinimumSize = new Point(34, 35);
31+
MinimumSize = new Point(34, 34);
3232
Margin = new Margin(4, 4, 4, 4);
3333
MouseInputEnabled = true;
3434
TextureFilename = "shopitem.png";
@@ -48,18 +48,15 @@ public ShopItem(ShopWindow shopWindow, Base parent, int index) : base(parent, na
4848
LoadJsonUi(GameContentManager.UI.InGame, Graphics.Renderer.GetResolutionString());
4949

5050
// Generate our context menu with basic options.
51+
// TODO: Refactor so shop only has 1 context menu shared between all items
5152
_contextMenu = new ContextMenu(Interface.CurrentInterface.Root, "ShopContextMenu")
5253
{
5354
IsHidden = true,
5455
IconMarginDisabled = true,
56+
ItemFont = GameContentManager.Current.GetFont(name: "sourcesansproblack"),
57+
ItemFontSize = 10,
5558
};
5659

57-
var font = GameContentManager.Current.GetFont(name: "sourcesansproblack");
58-
if (font != default)
59-
{
60-
_contextMenu.SetItemFont(font, 10);
61-
}
62-
6360
//TODO: Is this a memory leak?
6461
_contextMenu.ClearChildren();
6562
_buyMenuItem = _contextMenu.AddItem(Strings.ShopContextMenu.Buy);
@@ -87,30 +84,30 @@ private void _iconImage_HoverEnter(Base sender, EventArgs arguments)
8784
_itemDescWindow = default;
8885
}
8986

90-
if (Globals.GameShop == default || Globals.GameShop.SellingItems.Count == 0)
87+
if (Globals.GameShop is not { SellingItems.Count: > 0 } gameShop)
9188
{
9289
return;
9390
}
9491

95-
if (!ItemBase.TryGet(Globals.GameShop.SellingItems[_mySlot].CostItemId, out var item))
92+
if (!ItemBase.TryGet(gameShop.SellingItems[_mySlot].CostItemId, out var item))
9693
{
9794
return;
9895
}
9996

100-
if (Globals.GameShop.SellingItems[_mySlot].Item != default)
97+
if (gameShop.SellingItems[_mySlot].Item != default)
10198
{
10299
ItemProperties itemProperty = new ItemProperties()
103100
{
104101
StatModifiers = item.StatsGiven,
105102
};
106103

107104
_itemDescWindow = new ItemDescriptionWindow(
108-
item: Globals.GameShop.SellingItems[_mySlot].Item,
105+
item: gameShop.SellingItems[_mySlot].Item,
109106
amount: 1,
110107
x: _shopWindow.X,
111108
y: _shopWindow.Y,
112109
itemProperties: itemProperty,
113-
valueLabel: Strings.Shop.Costs.ToString(Globals.GameShop.SellingItems[_mySlot].CostItemQuantity, item.Name)
110+
valueLabel: Strings.Shop.Costs.ToString(gameShop.SellingItems[_mySlot].CostItemQuantity, item.Name)
114111
);
115112
}
116113
}
@@ -183,16 +180,21 @@ public void OpenContextMenu(int slot)
183180

184181
public void LoadItem()
185182
{
186-
if (Globals.GameShop == default || !ItemBase.TryGet(Globals.GameShop.SellingItems[_mySlot].ItemId, out var item))
183+
if (Globals.GameShop is not { SellingItems.Count: > 0 } gameShop)
184+
{
185+
return;
186+
}
187+
188+
if (!ItemBase.TryGet(gameShop.SellingItems[_mySlot].ItemId, out var itemDescriptor))
187189
{
188190
return;
189191
}
190192

191-
var itemTex = Globals.ContentManager?.GetTexture(Framework.Content.TextureType.Item, item.Icon);
193+
var itemTex = Globals.ContentManager?.GetTexture(Framework.Content.TextureType.Item, itemDescriptor.Icon);
192194
if (itemTex != null)
193195
{
194196
_iconImage.Texture = itemTex;
195-
_iconImage.RenderColor = item.Color;
197+
_iconImage.RenderColor = itemDescriptor.Color;
196198
}
197199
}
198200
}

Intersect.Client.Core/Interface/Game/Shop/ShopWindow.cs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public ShopWindow(Canvas gameCanvas) : base(gameCanvas, Globals.GameShop?.Name ?
1818
Interface.InputBlockingComponents.Add(this);
1919

2020
Alignment = [Alignments.Center];
21-
MinimumSize = new Point(x: 442, y: 469);
21+
MinimumSize = new Point(x: 435, y: 469);
2222
IsResizable = false;
2323
IsClosable = true;
2424

@@ -41,30 +41,27 @@ protected override void EnsureInitialized()
4141

4242
private void InitItemContainer()
4343
{
44-
if (Globals.GameShop == default || Globals.GameShop.SellingItems.Count == 0)
44+
if (Globals.GameShop is not { SellingItems.Count: > 0 } gameShop)
4545
{
4646
return;
4747
}
4848

49-
for (var i = 0; i < Globals.GameShop.SellingItems.Count; i++)
49+
float containerInnerWidth = _slotContainer.InnerPanel.InnerWidth;
50+
for (var slotIndex = 0; slotIndex < gameShop.SellingItems.Count; slotIndex++)
5051
{
51-
_items.Add(new ShopItem(this, _slotContainer, i));
52+
var slotContainer = new ShopItem(this, _slotContainer, slotIndex);
53+
_items.Add(slotContainer);
5254

53-
var xPadding = _items[i].Margin.Left + _items[i].Margin.Right;
54-
var yPadding = _items[i].Margin.Top + _items[i].Margin.Bottom;
55+
var outerSize = slotContainer.OuterBounds.Size;
56+
var itemsPerRow = (int)(containerInnerWidth / outerSize.X);
5557

56-
var itemWidthWithPadding = _items[i].Width + xPadding;
57-
var itemHeightWithPadding = _items[i].Height + yPadding;
58+
var column = slotIndex % itemsPerRow;
59+
var row = slotIndex / itemsPerRow;
5860

59-
var itemsPerRow = _slotContainer.Width / itemWidthWithPadding;
61+
var xPosition = column * outerSize.X + slotContainer.Margin.Left;
62+
var yPosition = row * outerSize.Y + slotContainer.Margin.Top;
6063

61-
var column = i % itemsPerRow;
62-
var row = i / itemsPerRow;
63-
64-
var xPosition = column * itemWidthWithPadding + xPadding;
65-
var yPosition = row * itemHeightWithPadding + yPadding;
66-
67-
_items[i].SetPosition(xPosition, yPosition);
64+
slotContainer.SetPosition(xPosition, yPosition);
6865
}
6966
}
7067
}

Intersect.Client.Framework/Gwen/Control/Menu.cs

Lines changed: 112 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Collections;
21
using Intersect.Client.Framework.File_Management;
32
using Intersect.Client.Framework.Graphics;
43
using Intersect.Client.Framework.Gwen.ControlInternal;
@@ -20,12 +19,7 @@ public partial class Menu : ScrollControl
2019

2120
private bool mDisableIconMargin;
2221

23-
private IFont? mItemFont;
24-
private int _itemFontSize;
25-
2622
//Menu Item Stuff
27-
private string mItemFontInfo;
28-
2923
protected Color mItemHoverTextColor;
3024

3125
protected Color mItemNormalTextColor;
@@ -46,6 +40,66 @@ public Menu(Base parent, string? name = default) : base(parent, name)
4640
DeleteOnClose = false;
4741
}
4842

43+
#region Font Handling
44+
45+
private IFont? _itemFont;
46+
47+
public IFont? ItemFont
48+
{
49+
get => _itemFont;
50+
set => SetItemFont(value, value?.Name);
51+
}
52+
53+
private string? _itemFontName;
54+
55+
public string? ItemFontName
56+
{
57+
get => _itemFontName;
58+
set => SetItemFont(GameContentManager.Current.GetFont(_itemFontName), _itemFontName);
59+
}
60+
61+
private int _itemFontSize;
62+
63+
public int ItemFontSize
64+
{
65+
get => _itemFontSize;
66+
set
67+
{
68+
if (value == _itemFontSize)
69+
{
70+
return;
71+
}
72+
73+
var oldValue = _itemFontSize;
74+
_itemFontSize = value;
75+
OnFontSizeChanged(this, value, oldValue);
76+
}
77+
}
78+
79+
private void SetItemFont(IFont? itemFont, string? itemFontName)
80+
{
81+
var oldValue = _itemFont;
82+
_itemFont = itemFont;
83+
_itemFontName = itemFontName;
84+
85+
if (itemFont != oldValue)
86+
{
87+
OnFontChanged(this, itemFont, oldValue);
88+
}
89+
}
90+
91+
protected virtual void OnFontChanged(Base sender, IFont? newFont, IFont? oldFont)
92+
{
93+
UpdateItemStyles();
94+
}
95+
96+
protected virtual void OnFontSizeChanged(Base sender, int newSize, int oldSize)
97+
{
98+
UpdateItemStyles();
99+
}
100+
101+
#endregion
102+
49103
internal override bool IsMenuComponent => true;
50104

51105
public bool IconMarginDisabled
@@ -343,14 +397,21 @@ public override bool SizeToChildren(SizeToChildrenArgs args)
343397
serializedProperties.Add("BackgroundTemplate", mBackgroundTemplateFilename);
344398
serializedProperties.Add("ItemTextColor", Color.ToString(mItemNormalTextColor));
345399
serializedProperties.Add("ItemHoveredTextColor", Color.ToString(mItemHoverTextColor));
346-
serializedProperties.Add("ItemFont", mItemFontInfo);
400+
serializedProperties.Add(nameof(ItemFontName), ItemFontName);
401+
serializedProperties.Add(nameof(ItemFontSize), ItemFontSize);
347402

348403
return base.FixJson(serializedProperties);
349404
}
350405

351-
public override void LoadJson(JToken obj, bool isRoot = default)
406+
public override void LoadJson(JToken token, bool isRoot = default)
352407
{
353-
base.LoadJson(obj);
408+
base.LoadJson(token, isRoot);
409+
410+
if (token is not JObject obj)
411+
{
412+
return;
413+
}
414+
354415
if (obj["BackgroundTemplate"] != null)
355416
{
356417
SetBackgroundTemplate(
@@ -370,12 +431,46 @@ public override void LoadJson(JToken obj, bool isRoot = default)
370431
mItemHoverTextColor = Color.FromString((string)obj["ItemHoveredTextColor"]);
371432
}
372433

373-
if (obj["ItemFont"] != null && obj["ItemFont"].Type != JTokenType.Null)
434+
string? itemFontName = null;
435+
int? itemFontSize = null;
436+
437+
if (obj.TryGetValue(nameof(ItemFont), out var tokenItemFont) &&
438+
tokenItemFont is JValue { Type: JTokenType.String } valueItemFont)
439+
{
440+
var stringItemFont = valueItemFont.Value<string>()?.Trim();
441+
if (!string.IsNullOrWhiteSpace(stringItemFont))
442+
{
443+
var parts = stringItemFont.Split(',');
444+
itemFontName = parts.FirstOrDefault();
445+
446+
if (parts.Length > 1 && int.TryParse(parts[1], out var size))
447+
{
448+
itemFontSize = size;
449+
}
450+
}
451+
}
452+
453+
if (obj.TryGetValue(nameof(ItemFontName), out var tokenItemFontName) &&
454+
tokenItemFontName is JValue { Type: JTokenType.String } valueItemFontName)
455+
{
456+
itemFontName = valueItemFontName.Value<string>();
457+
}
458+
459+
if (obj.TryGetValue(nameof(ItemFontSize), out var tokenItemFontSize) &&
460+
tokenItemFontSize is JValue { Type: JTokenType.Integer } valueItemFontSize)
461+
{
462+
itemFontSize = valueItemFontSize.Value<int>();
463+
}
464+
465+
if (itemFontSize.HasValue)
466+
{
467+
ItemFontSize = itemFontSize.Value;
468+
}
469+
470+
itemFontName = itemFontName?.Trim();
471+
if (!string.IsNullOrWhiteSpace(itemFontName))
374472
{
375-
var fontArr = ((string)obj["ItemFont"]).Split(',');
376-
mItemFontInfo = (string)obj["ItemFont"];
377-
_itemFontSize = int.Parse(fontArr[1]);
378-
mItemFont = GameContentManager.Current.GetFont(fontArr[0]);
473+
ItemFontName = itemFontName;
379474
}
380475

381476
UpdateItemStyles();
@@ -386,9 +481,9 @@ private void UpdateItemStyles()
386481
var menuItems = Children.OfType<MenuItem>().ToArray();
387482
foreach (var item in menuItems)
388483
{
389-
if (mItemFont != null)
484+
if (_itemFont != null)
390485
{
391-
item.Font = mItemFont;
486+
item.Font = _itemFont;
392487
}
393488

394489
item.FontSize = _itemFontSize;
@@ -415,9 +510,8 @@ public void SetBackgroundTemplate(IGameTexture texture, string fileName)
415510

416511
public void SetItemFont(IFont font, int fontSize)
417512
{
418-
mItemFont = font;
513+
_itemFont = font;
419514
_itemFontSize = fontSize;
420-
mItemFontInfo = $"{font.Name},{fontSize}";
421515
UpdateItemStyles();
422516
}
423517

0 commit comments

Comments
 (0)