mirror of
				https://github.com/Nioux/AideDeJeu.git
				synced 2025-10-31 07:26:09 +00:00 
			
		
		
		
	Utilisation généralisée des FormattedText
This commit is contained in:
		
							parent
							
								
									f741c229a2
								
							
						
					
					
						commit
						e1eae80d13
					
				
					 3 changed files with 237 additions and 144 deletions
				
			
		
							
								
								
									
										151
									
								
								AideDeJeu/AideDeJeu/Tools/FormatedTextHelpers.cs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										151
									
								
								AideDeJeu/AideDeJeu/Tools/FormatedTextHelpers.cs
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,151 @@ | |||
| using HtmlAgilityPack; | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using System.Text; | ||||
| using Xamarin.Forms; | ||||
| using Xamarin.Forms.Internals; | ||||
| 
 | ||||
| namespace AideDeJeu.Tools | ||||
| { | ||||
|     public static class FormatedTextHelpers | ||||
|     { | ||||
|         public static void HtmlToFormatedString(HtmlNode parentNode, FormattedString fs, FontAttributes attributes = FontAttributes.None) | ||||
|         { | ||||
|             foreach (var node in parentNode.ChildNodes) | ||||
|             { | ||||
|                 if (node.NodeType == HtmlNodeType.Text) | ||||
|                 { | ||||
|                     var resname = "content"; | ||||
|                     if (attributes.HasFlag(FontAttributes.Bold)) | ||||
|                     { | ||||
|                         resname += "bold"; | ||||
|                     } | ||||
|                     if (attributes.HasFlag(FontAttributes.Italic)) | ||||
|                     { | ||||
|                         resname += "ital"; | ||||
|                     } | ||||
|                     var fd = FontData.FromResource(resname); | ||||
|                     fs.Spans.Add(new Span() { FontFamily = fd.FontFamily, FontAttributes = attributes | fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor, Text = node.InnerText }); | ||||
|                 } | ||||
|                 else if (node.NodeType == HtmlNodeType.Element && node.Name == "br") | ||||
|                 { | ||||
|                     fs.Spans.Add(new Span() { Text = "\r\n" }); | ||||
|                 } | ||||
|                 else if (node.NodeType == HtmlNodeType.Element && node.Name == "strong") | ||||
|                 { | ||||
|                     HtmlToFormatedString(node, fs, attributes | FontAttributes.Bold); | ||||
|                 } | ||||
|                 else if (node.NodeType == HtmlNodeType.Element && node.Name == "em") | ||||
|                 { | ||||
|                     HtmlToFormatedString(node, fs, attributes | FontAttributes.Italic); | ||||
|                 } | ||||
|                 else if (node.NodeType == HtmlNodeType.Element) | ||||
|                 { | ||||
|                     HtmlToFormatedString(node, fs, attributes); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         public class FontData | ||||
|         { | ||||
|             public double FontSize { get; set; } | ||||
|             public FontAttributes FontAttributes { get; set; } | ||||
|             public Color TextColor { get; set; } | ||||
|             public string FontFamily { get; set; } | ||||
| 
 | ||||
|             public static FontData DefaultValues() | ||||
|             { | ||||
|                 return new FontData | ||||
|                 { | ||||
|                     FontSize = (double)Label.FontSizeProperty.DefaultValue, | ||||
|                     FontAttributes = (FontAttributes)Label.FontAttributesProperty.DefaultValue, | ||||
|                     TextColor = (Color)Label.TextColorProperty.DefaultValue, | ||||
|                     FontFamily = Label.FontFamilyProperty.DefaultValue.ToString() | ||||
|                 }; | ||||
|             } | ||||
| 
 | ||||
|             public static FontData FromResource(string resourceName) | ||||
|             { | ||||
|                 var resource = Application.Current.Resources[resourceName]; | ||||
|                 if (resource == null) | ||||
|                 { | ||||
|                     return DefaultValues(); | ||||
|                 } | ||||
|                 var style = (Style)resource; | ||||
| 
 | ||||
|                 var data = new FontData(); | ||||
|                 var colorSetter = style.Setters.FirstOrDefault(x => x.Property == Label.TextColorProperty); | ||||
|                 var attrSetter = style.Setters.FirstOrDefault(x => x.Property == Label.FontAttributesProperty); | ||||
|                 var fontSizeSetter = style.Setters.FirstOrDefault(x => x.Property == Label.FontSizeProperty); | ||||
|                 var fontFamilySetter = style.Setters.FirstOrDefault(x => x.Property == Label.FontFamilyProperty); | ||||
|                 var platformFontFamilySetter = ResolveSetterToClass<string>(fontFamilySetter); | ||||
|                 var platformColorSetter = ResolveSetterToStruct<Color>(colorSetter); | ||||
| 
 | ||||
|                 if (platformColorSetter.HasValue) | ||||
|                 { | ||||
|                     data.TextColor = platformColorSetter.Value; | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     data.TextColor = colorSetter?.Value as Color? ?? (Color)Label.TextColorProperty.DefaultValue; | ||||
|                 } | ||||
|                 data.FontSize = fontSizeSetter?.Value as double? ?? (double)Label.FontSizeProperty.DefaultValue; | ||||
| 
 | ||||
| 
 | ||||
|                 if (platformFontFamilySetter != null) | ||||
|                 { | ||||
|                     data.FontFamily = platformFontFamilySetter; | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     data.FontFamily = fontFamilySetter != null && fontFamilySetter.Value != null | ||||
|                         ? fontFamilySetter.Value.ToString() | ||||
|                         : Label.FontFamilyProperty.DefaultValue?.ToString(); | ||||
|                 } | ||||
| 
 | ||||
|                 data.FontAttributes = attrSetter?.Value != null | ||||
|                     ? (FontAttributes)Enum.Parse(typeof(FontAttributes), attrSetter.Value.ToString()) | ||||
|                     : (FontAttributes)Label.FontAttributesProperty.DefaultValue; | ||||
| 
 | ||||
|                 return data; | ||||
|             } | ||||
| 
 | ||||
|             static T ResolveOnPlatformToClass<T>(string key) where T : class | ||||
|             { | ||||
|                 var resource = Application.Current.Resources[key]; | ||||
|                 return resource as OnPlatform<T>; | ||||
|             } | ||||
|             static T ResolveSetterToClass<T>(Setter setter) where T : class | ||||
|             { | ||||
|                 if (setter?.Value is DynamicResource) | ||||
|                 { | ||||
|                     var res = setter.Value as DynamicResource; | ||||
|                     return ResolveOnPlatformToClass<T>(res.Key); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     return setter?.Value as T; | ||||
|                 } | ||||
|             } | ||||
| 
 | ||||
|             static Nullable<T> ResolveOnPlatformToStruct<T>(string key) where T : struct | ||||
|             { | ||||
|                 var resource = Application.Current.Resources[key]; | ||||
|                 return resource as OnPlatform<Nullable<T>>; | ||||
|             } | ||||
|             static Nullable<T> ResolveSetterToStruct<T>(Setter setter) where T : struct | ||||
|             { | ||||
|                 if (setter?.Value is DynamicResource) | ||||
|                 { | ||||
|                     var res = setter.Value as DynamicResource; | ||||
|                     return ResolveOnPlatformToStruct<T>(res.Key); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     return setter?.Value as T?; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | @ -7,6 +7,7 @@ using AideDeJeuLib; | |||
| using HtmlAgilityPack; | ||||
| using Xamarin.Forms; | ||||
| using Xamarin.Forms.Internals; | ||||
| using AideDeJeu.Tools; | ||||
| 
 | ||||
| namespace AideDeJeu.ViewModels | ||||
| { | ||||
|  | @ -16,7 +17,16 @@ namespace AideDeJeu.ViewModels | |||
|         public Spell Item | ||||
|         { | ||||
|             get { return _Item; } | ||||
|             set { SetProperty(ref _Item, value); OnPropertyChanged(nameof(Description)); } | ||||
|             set | ||||
|             { | ||||
|                 SetProperty(ref _Item, value); | ||||
|                 OnPropertyChanged(nameof(Description)); | ||||
|                 OnPropertyChanged(nameof(TypeLevel)); | ||||
|                 OnPropertyChanged(nameof(CastingTime)); | ||||
|                 OnPropertyChanged(nameof(Range)); | ||||
|                 OnPropertyChanged(nameof(Components)); | ||||
|                 OnPropertyChanged(nameof(Duration)); | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         public FormattedString Description | ||||
|  | @ -26,48 +36,75 @@ namespace AideDeJeu.ViewModels | |||
|                 var fs = new FormattedString(); | ||||
|                 if (Item?.DescriptionDiv != null) | ||||
|                 { | ||||
|                     HtmlToFormatedString(Item?.DescriptionDiv, fs, FontAttributes.None); | ||||
|                     FormatedTextHelpers.HtmlToFormatedString(Item?.DescriptionDiv, fs, FontAttributes.None); | ||||
|                 } | ||||
|                 return fs; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         void HtmlToFormatedString(HtmlNode parentNode, FormattedString fs, FontAttributes attributes) | ||||
|         public FormattedString TypeLevel | ||||
|         { | ||||
|             foreach (var node in parentNode.ChildNodes) | ||||
|             get | ||||
|             { | ||||
|                 if (node.NodeType == HtmlNodeType.Text) | ||||
|                 { | ||||
|                     var resname = "content"; | ||||
|                     if (attributes.HasFlag(FontAttributes.Bold)) | ||||
|                     { | ||||
|                         resname += "bold"; | ||||
|                     } | ||||
|                     if (attributes.HasFlag(FontAttributes.Italic)) | ||||
|                     { | ||||
|                         resname += "ital"; | ||||
|                     } | ||||
|                     var fd = FontData.FromResource(resname); | ||||
|                     fs.Spans.Add(new Span() { FontFamily = fd.FontFamily, FontAttributes = attributes | fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor, Text = node.InnerText }); | ||||
|                 } | ||||
|                 else if (node.NodeType == HtmlNodeType.Element && node.Name == "br") | ||||
|                 { | ||||
|                     fs.Spans.Add(new Span() { Text = "\r\n" }); | ||||
|                 } | ||||
|                 else if (node.NodeType == HtmlNodeType.Element && node.Name == "strong") | ||||
|                 { | ||||
|                     HtmlToFormatedString(node, fs, attributes | FontAttributes.Bold); | ||||
|                 } | ||||
|                 else if (node.NodeType == HtmlNodeType.Element && node.Name == "em") | ||||
|                 { | ||||
|                     HtmlToFormatedString(node, fs, attributes | FontAttributes.Italic); | ||||
|                 } | ||||
|                 else if (node.NodeType == HtmlNodeType.Element) | ||||
|                 { | ||||
|                     HtmlToFormatedString(node, fs, attributes); | ||||
|                 } | ||||
|                 var fd = FormatedTextHelpers.FontData.FromResource("contentital"); | ||||
|                 var fs = new FormattedString(); | ||||
|                 fs.Spans.Add(new Span() { Text = string.Format("{0} de niveau {1}", Item.Type, Item.Level), FontFamily = fd.FontFamily, FontAttributes = fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor}); | ||||
|                 return fs; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         public FormattedString CastingTime | ||||
|         { | ||||
|             get | ||||
|             { | ||||
|                 var fd = FormatedTextHelpers.FontData.FromResource("content"); | ||||
|                 var fdb = FormatedTextHelpers.FontData.FromResource("contentbold"); | ||||
|                 var fs = new FormattedString(); | ||||
|                 fs.Spans.Add(new Span() { Text = "Durée d'incantation : ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor }); | ||||
|                 fs.Spans.Add(new Span() { Text = Item.CastingTime, FontFamily = fd.FontFamily, FontAttributes = fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor }); | ||||
|                 return fs; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         public FormattedString Range | ||||
|         { | ||||
|             get | ||||
|             { | ||||
|                 var fd = FormatedTextHelpers.FontData.FromResource("content"); | ||||
|                 var fdb = FormatedTextHelpers.FontData.FromResource("contentbold"); | ||||
|                 var fs = new FormattedString(); | ||||
|                 fs.Spans.Add(new Span() { Text = "Portée : ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor }); | ||||
|                 fs.Spans.Add(new Span() { Text = Item.Range, FontFamily = fd.FontFamily, FontAttributes = fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor }); | ||||
|                 return fs; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         public FormattedString Components | ||||
|         { | ||||
|             get | ||||
|             { | ||||
|                 var fd = FormatedTextHelpers.FontData.FromResource("content"); | ||||
|                 var fdb = FormatedTextHelpers.FontData.FromResource("contentbold"); | ||||
|                 var fs = new FormattedString(); | ||||
|                 fs.Spans.Add(new Span() { Text = "Composantes : ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor }); | ||||
|                 fs.Spans.Add(new Span() { Text = Item.Components, FontFamily = fd.FontFamily, FontAttributes = fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor }); | ||||
|                 return fs; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         public FormattedString Duration | ||||
|         { | ||||
|             get | ||||
|             { | ||||
|                 var fd = FormatedTextHelpers.FontData.FromResource("content"); | ||||
|                 var fdb = FormatedTextHelpers.FontData.FromResource("contentbold"); | ||||
|                 var fs = new FormattedString(); | ||||
|                 fs.Spans.Add(new Span() { Text = "Durée : ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor }); | ||||
|                 fs.Spans.Add(new Span() { Text = Item.Duration, FontFamily = fd.FontFamily, FontAttributes = fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor }); | ||||
|                 return fs; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         public Command LoadItemCommand { get; set; } | ||||
| 
 | ||||
|         public SpellDetailViewModel(Spell item = null) | ||||
|  | @ -107,104 +144,4 @@ namespace AideDeJeu.ViewModels | |||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|     public class FontData | ||||
|     { | ||||
|         public double FontSize { get; set; } | ||||
|         public FontAttributes FontAttributes { get; set; } | ||||
|         public Color TextColor { get; set; } | ||||
|         public string FontFamily { get; set; } | ||||
| 
 | ||||
|         public static FontData DefaultValues() | ||||
|         { | ||||
|             return new FontData | ||||
|             { | ||||
|                 FontSize = (double)Label.FontSizeProperty.DefaultValue, | ||||
|                 FontAttributes = (FontAttributes)Label.FontAttributesProperty.DefaultValue, | ||||
|                 TextColor = (Color)Label.TextColorProperty.DefaultValue, | ||||
|                 FontFamily = Label.FontFamilyProperty.DefaultValue.ToString() | ||||
|             }; | ||||
|         } | ||||
| 
 | ||||
|         public static FontData FromResource(string resourceName) | ||||
|         { | ||||
|             var resource = Application.Current.Resources[resourceName]; | ||||
|             if (resource == null) | ||||
|             { | ||||
|                 return DefaultValues(); | ||||
|             } | ||||
|             var style = (Style)resource; | ||||
| 
 | ||||
|             var data = new FontData(); | ||||
|             var colorSetter = style.Setters.FirstOrDefault(x => x.Property == Label.TextColorProperty); | ||||
|             var attrSetter = style.Setters.FirstOrDefault(x => x.Property == Label.FontAttributesProperty); | ||||
|             var fontSizeSetter = style.Setters.FirstOrDefault(x => x.Property == Label.FontSizeProperty); | ||||
|             var fontFamilySetter = style.Setters.FirstOrDefault(x => x.Property == Label.FontFamilyProperty); | ||||
|             var platformFontFamilySetter = ResolveSetterToClass<string>(fontFamilySetter); | ||||
|             var platformColorSetter = ResolveSetterToStruct<Color>(colorSetter); | ||||
| 
 | ||||
|             if (platformColorSetter.HasValue) | ||||
|             { | ||||
|                 data.TextColor = platformColorSetter.Value; | ||||
|             } | ||||
|             else | ||||
|             {  | ||||
|                 data.TextColor = colorSetter?.Value as Color? ?? (Color)Label.TextColorProperty.DefaultValue; | ||||
|             } | ||||
|             data.FontSize = fontSizeSetter?.Value as double? ?? (double)Label.FontSizeProperty.DefaultValue; | ||||
| 
 | ||||
|              | ||||
|             if (platformFontFamilySetter != null) | ||||
|             { | ||||
|                 data.FontFamily = platformFontFamilySetter; | ||||
|             } | ||||
|             else | ||||
|             {  | ||||
|                 data.FontFamily = fontFamilySetter != null && fontFamilySetter.Value != null | ||||
|                     ? fontFamilySetter.Value.ToString() | ||||
|                     : Label.FontFamilyProperty.DefaultValue?.ToString(); | ||||
|             } | ||||
| 
 | ||||
|             data.FontAttributes = attrSetter?.Value != null | ||||
|                 ? (FontAttributes)Enum.Parse(typeof(FontAttributes), attrSetter.Value.ToString()) | ||||
|                 : (FontAttributes)Label.FontAttributesProperty.DefaultValue; | ||||
| 
 | ||||
|             return data; | ||||
|         } | ||||
| 
 | ||||
|         static T ResolveOnPlatformToClass<T>(string key) where T : class | ||||
|         { | ||||
|             var resource = Application.Current.Resources[key]; | ||||
|             return resource as OnPlatform<T>; | ||||
|         } | ||||
|         static T ResolveSetterToClass<T>(Setter setter) where T : class | ||||
|         { | ||||
|             if (setter?.Value is DynamicResource) | ||||
|             { | ||||
|                 var res = setter.Value as DynamicResource; | ||||
|                 return ResolveOnPlatformToClass<T>(res.Key); | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 return setter?.Value as T; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         static Nullable<T> ResolveOnPlatformToStruct<T>(string key) where T : struct | ||||
|         { | ||||
|             var resource = Application.Current.Resources[key]; | ||||
|             return resource as OnPlatform<Nullable<T>>; | ||||
|         } | ||||
|         static Nullable<T> ResolveSetterToStruct<T>(Setter setter) where T : struct | ||||
|         { | ||||
|             if(setter?.Value is DynamicResource) | ||||
|             { | ||||
|                 var res = setter.Value as DynamicResource; | ||||
|                 return ResolveOnPlatformToStruct<T>(res.Key); | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 return setter?.Value as T?; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -9,31 +9,36 @@ | |||
| 
 | ||||
|             <Label Text=" " /> | ||||
| 
 | ||||
|             <StackLayout Orientation="Horizontal"> | ||||
|             <Label FormattedText="{Binding TypeLevel}" /> | ||||
|             <!--<StackLayout Orientation="Horizontal"> | ||||
|                 <Label Text="{Binding Item.Type}" Style="{StaticResource Key=contentital}" /> | ||||
|                 <Label Text="{Binding Item.Level, StringFormat='de niveau {0}'}" Style="{StaticResource Key=contentital}" /> | ||||
|             </StackLayout> | ||||
|             </StackLayout>--> | ||||
| 
 | ||||
|             <StackLayout Orientation="Horizontal"> | ||||
|             <Label FormattedText="{Binding CastingTime}" /> | ||||
|             <!--<StackLayout Orientation="Horizontal"> | ||||
|                 <Label Text="Durée d'incantation :" LineBreakMode="NoWrap" Style="{StaticResource Key=contentbold}" /> | ||||
|                 <Label Text="{Binding Item.CastingTime}" Style="{StaticResource Key=content}" /> | ||||
|             </StackLayout> | ||||
|             </StackLayout>--> | ||||
| 
 | ||||
| 
 | ||||
|             <StackLayout Orientation="Horizontal"> | ||||
|             <Label FormattedText="{Binding Range}" /> | ||||
|             <!--<StackLayout Orientation="Horizontal"> | ||||
|                 <Label Text="Portée :" LineBreakMode="NoWrap" Style="{StaticResource Key=contentbold}" /> | ||||
|                 <Label Text="{Binding Item.Range}" Style="{StaticResource Key=content}" /> | ||||
|             </StackLayout> | ||||
|             </StackLayout>--> | ||||
| 
 | ||||
|             <StackLayout Orientation="Horizontal"> | ||||
|             <Label FormattedText="{Binding Components}" /> | ||||
|             <!--<StackLayout Orientation="Horizontal"> | ||||
|                 <Label Text="Composantes :" LineBreakMode="NoWrap" Style="{StaticResource Key=contentbold}" /> | ||||
|                 <Label Text="{Binding Item.Components}" Style="{StaticResource Key=content}" /> | ||||
|             </StackLayout> | ||||
|             </StackLayout>--> | ||||
| 
 | ||||
|             <StackLayout Orientation="Horizontal"> | ||||
|             <Label FormattedText="{Binding Duration}" /> | ||||
|             <!--<StackLayout Orientation="Horizontal"> | ||||
|                 <Label Text="Durée :" LineBreakMode="NoWrap" Style="{StaticResource Key=contentbold}" /> | ||||
|                 <Label Text="{Binding Item.Duration}" Style="{StaticResource Key=content}" /> | ||||
|             </StackLayout> | ||||
|             </StackLayout>--> | ||||
| 
 | ||||
|             <Label Text=" " /> | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Yan Maniez
						Yan Maniez