From e1eae80d13d166cd5e95e1c1abc4e9608ca303f3 Mon Sep 17 00:00:00 2001 From: Yan Maniez Date: Wed, 25 Apr 2018 12:41:22 +0200 Subject: [PATCH] =?UTF-8?q?Utilisation=20g=C3=A9n=C3=A9ralis=C3=A9e=20des?= =?UTF-8?q?=20FormattedText?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AideDeJeu/Tools/FormatedTextHelpers.cs | 151 +++++++++++++ .../ViewModels/SpellDetailViewModel.cs | 205 ++++++------------ .../AideDeJeu/Views/SpellDetailPage.xaml | 25 ++- 3 files changed, 237 insertions(+), 144 deletions(-) create mode 100644 AideDeJeu/AideDeJeu/Tools/FormatedTextHelpers.cs diff --git a/AideDeJeu/AideDeJeu/Tools/FormatedTextHelpers.cs b/AideDeJeu/AideDeJeu/Tools/FormatedTextHelpers.cs new file mode 100644 index 00000000..9202c9a6 --- /dev/null +++ b/AideDeJeu/AideDeJeu/Tools/FormatedTextHelpers.cs @@ -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(fontFamilySetter); + var platformColorSetter = ResolveSetterToStruct(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(string key) where T : class + { + var resource = Application.Current.Resources[key]; + return resource as OnPlatform; + } + static T ResolveSetterToClass(Setter setter) where T : class + { + if (setter?.Value is DynamicResource) + { + var res = setter.Value as DynamicResource; + return ResolveOnPlatformToClass(res.Key); + } + else + { + return setter?.Value as T; + } + } + + static Nullable ResolveOnPlatformToStruct(string key) where T : struct + { + var resource = Application.Current.Resources[key]; + return resource as OnPlatform>; + } + static Nullable ResolveSetterToStruct(Setter setter) where T : struct + { + if (setter?.Value is DynamicResource) + { + var res = setter.Value as DynamicResource; + return ResolveOnPlatformToStruct(res.Key); + } + else + { + return setter?.Value as T?; + } + } + } + } +} diff --git a/AideDeJeu/AideDeJeu/ViewModels/SpellDetailViewModel.cs b/AideDeJeu/AideDeJeu/ViewModels/SpellDetailViewModel.cs index 2db57cd4..0f81975c 100644 --- a/AideDeJeu/AideDeJeu/ViewModels/SpellDetailViewModel.cs +++ b/AideDeJeu/AideDeJeu/ViewModels/SpellDetailViewModel.cs @@ -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(fontFamilySetter); - var platformColorSetter = ResolveSetterToStruct(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(string key) where T : class - { - var resource = Application.Current.Resources[key]; - return resource as OnPlatform; - } - static T ResolveSetterToClass(Setter setter) where T : class - { - if (setter?.Value is DynamicResource) - { - var res = setter.Value as DynamicResource; - return ResolveOnPlatformToClass(res.Key); - } - else - { - return setter?.Value as T; - } - } - - static Nullable ResolveOnPlatformToStruct(string key) where T : struct - { - var resource = Application.Current.Resources[key]; - return resource as OnPlatform>; - } - static Nullable ResolveSetterToStruct(Setter setter) where T : struct - { - if(setter?.Value is DynamicResource) - { - var res = setter.Value as DynamicResource; - return ResolveOnPlatformToStruct(res.Key); - } - else - { - return setter?.Value as T?; - } - } - } } diff --git a/AideDeJeu/AideDeJeu/Views/SpellDetailPage.xaml b/AideDeJeu/AideDeJeu/Views/SpellDetailPage.xaml index 349c3882..2df23263 100644 --- a/AideDeJeu/AideDeJeu/Views/SpellDetailPage.xaml +++ b/AideDeJeu/AideDeJeu/Views/SpellDetailPage.xaml @@ -9,31 +9,36 @@