1- using System . Collections ;
21using Intersect . Client . Framework . File_Management ;
32using Intersect . Client . Framework . Graphics ;
43using 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