1
0
Fork 0
mirror of https://github.com/Nioux/AideDeJeu.git synced 2025-10-29 06:26:02 +00:00

Ajout de la plupart des attributs d'un monstre

This commit is contained in:
Yan Maniez 2018-04-30 22:57:41 +02:00
parent 5f3ad39729
commit aa88df78ad
6 changed files with 264 additions and 71 deletions

View file

@ -1,4 +1,5 @@
using System;
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
@ -31,4 +32,55 @@ namespace AideDeJeu.Tools
return null;
}
}
public class HtmlNodeToFormattedStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var node = value as HtmlNode;
if (node != null)
{
var fs = new FormattedString();
FormatedTextHelpers.HtmlNodeToFormatedString(node, fs);
return fs;
}
else
{
return null;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
public class HtmlNodesToFormattedStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var nodes = value as IEnumerable<HtmlNode>;
if (nodes != null)
{
var fs = new FormattedString();
foreach (var node in nodes)
{
FormatedTextHelpers.HtmlNodeToFormatedString(node, fs);
fs.Spans.Add(new Span() { Text = "\r\n" });
}
return fs;
}
else
{
return null;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
}

View file

@ -10,43 +10,95 @@ namespace AideDeJeu.Tools
{
public static class FormatedTextHelpers
{
public static void HtmlToFormatedString(HtmlNode parentNode, FormattedString fs, FontAttributes attributes = FontAttributes.None)
public static void HtmlNodesToFormatedString(HtmlNodeCollection nodes, FormattedString fs, FontAttributes attributes = FontAttributes.None)
{
foreach (var node in parentNode.ChildNodes)
if (nodes != null)
{
if (node.NodeType == HtmlNodeType.Text)
foreach (var node in nodes)
{
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);
HtmlNodeToFormatedString(node, fs, attributes);
}
}
}
public static void HtmlNodeToFormatedString(HtmlNode node, FormattedString fs, FontAttributes attributes = FontAttributes.None)
{
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")
{
HtmlNodesToFormatedString(node.ChildNodes, fs, attributes | FontAttributes.Bold);
}
else if (node.NodeType == HtmlNodeType.Element && node.Name == "em")
{
HtmlNodesToFormatedString(node.ChildNodes, fs, attributes | FontAttributes.Italic);
}
else if (node.NodeType == HtmlNodeType.Element)
{
HtmlNodesToFormatedString(node.ChildNodes, fs, attributes);
}
}
//public static void HtmlToFormatedString(HtmlNode parentNode, FormattedString fs, FontAttributes attributes = FontAttributes.None)
//{
// if (parentNode.NodeType == HtmlNodeType.Element)
// {
// 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);
// }
// }
// }
// else if (parentNode.NodeType == HtmlNodeType.Text)
// {
// }
//}
public class FontData
{
public double FontSize { get; set; }

View file

@ -25,6 +25,9 @@ namespace AideDeJeu.ViewModels
OnPropertyChanged(nameof(Speed));
OnPropertyChanged(nameof(SavingThrows));
OnPropertyChanged(nameof(Skills));
OnPropertyChanged(nameof(DamageResistances));
OnPropertyChanged(nameof(DamageImmunities));
OnPropertyChanged(nameof(ConditionImmunities));
OnPropertyChanged(nameof(Senses));
OnPropertyChanged(nameof(Languages));
OnPropertyChanged(nameof(Challenge));
@ -84,7 +87,7 @@ namespace AideDeJeu.ViewModels
var fd = FormatedTextHelpers.FontData.FromResource("content");
var fdb = FormatedTextHelpers.FontData.FromResource("contentbold");
var fs = new FormattedString();
fs.Spans.Add(new Span() { Text = "Jets de sauvegarde : ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor });
fs.Spans.Add(new Span() { Text = "Jets de sauvegarde ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor });
fs.Spans.Add(new Span() { Text = Item.SavingThrows, FontFamily = fd.FontFamily, FontAttributes = fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor });
return fs;
}
@ -97,12 +100,54 @@ namespace AideDeJeu.ViewModels
var fd = FormatedTextHelpers.FontData.FromResource("content");
var fdb = FormatedTextHelpers.FontData.FromResource("contentbold");
var fs = new FormattedString();
fs.Spans.Add(new Span() { Text = "Compétences : ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor });
fs.Spans.Add(new Span() { Text = "Compétence ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor });
fs.Spans.Add(new Span() { Text = Item.Skills, FontFamily = fd.FontFamily, FontAttributes = fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor });
return fs;
}
}
public FormattedString DamageImmunities
{
get
{
var fd = FormatedTextHelpers.FontData.FromResource("content");
var fdb = FormatedTextHelpers.FontData.FromResource("contentbold");
var fs = new FormattedString();
fs.Spans.Add(new Span() { Text = "Immunité contre les dégâts ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor });
fs.Spans.Add(new Span() { Text = Item.DamageImmunities, FontFamily = fd.FontFamily, FontAttributes = fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor });
return fs;
}
}
public FormattedString ConditionImmunities
{
get
{
var fd = FormatedTextHelpers.FontData.FromResource("content");
var fdb = FormatedTextHelpers.FontData.FromResource("contentbold");
var fs = new FormattedString();
fs.Spans.Add(new Span() { Text = "Immunité contre les états ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor });
fs.Spans.Add(new Span() { Text = Item.ConditionImmunities, FontFamily = fd.FontFamily, FontAttributes = fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor });
return fs;
}
}
public FormattedString DamageResistances
{
get
{
var fd = FormatedTextHelpers.FontData.FromResource("content");
var fdb = FormatedTextHelpers.FontData.FromResource("contentbold");
var fs = new FormattedString();
fs.Spans.Add(new Span() { Text = "Résistance aux dégâts ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor });
fs.Spans.Add(new Span() { Text = Item.DamageResistances, FontFamily = fd.FontFamily, FontAttributes = fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor });
return fs;
}
}
public FormattedString Senses
{
get
@ -110,7 +155,7 @@ namespace AideDeJeu.ViewModels
var fd = FormatedTextHelpers.FontData.FromResource("content");
var fdb = FormatedTextHelpers.FontData.FromResource("contentbold");
var fs = new FormattedString();
fs.Spans.Add(new Span() { Text = "Sens : ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor });
fs.Spans.Add(new Span() { Text = "Sens ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor });
fs.Spans.Add(new Span() { Text = Item.Senses, FontFamily = fd.FontFamily, FontAttributes = fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor });
return fs;
}
@ -123,7 +168,7 @@ namespace AideDeJeu.ViewModels
var fd = FormatedTextHelpers.FontData.FromResource("content");
var fdb = FormatedTextHelpers.FontData.FromResource("contentbold");
var fs = new FormattedString();
fs.Spans.Add(new Span() { Text = "Langues : ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor });
fs.Spans.Add(new Span() { Text = "Langues ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor });
fs.Spans.Add(new Span() { Text = Item.Languages, FontFamily = fd.FontFamily, FontAttributes = fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor });
return fs;
}
@ -136,12 +181,14 @@ namespace AideDeJeu.ViewModels
var fd = FormatedTextHelpers.FontData.FromResource("content");
var fdb = FormatedTextHelpers.FontData.FromResource("contentbold");
var fs = new FormattedString();
fs.Spans.Add(new Span() { Text = "Puissance : ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor });
fs.Spans.Add(new Span() { Text = "Dangerosité ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor });
fs.Spans.Add(new Span() { Text = Item.Challenge, FontFamily = fd.FontFamily, FontAttributes = fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor });
return fs;
}
}
//public FormattedString Description
//{
// get

View file

@ -34,7 +34,7 @@ namespace AideDeJeu.ViewModels
var fs = new FormattedString();
if (Item?.DescriptionDiv != null)
{
FormatedTextHelpers.HtmlToFormatedString(Item?.DescriptionDiv, fs, FontAttributes.None);
FormatedTextHelpers.HtmlNodeToFormatedString(Item?.DescriptionDiv, fs, FontAttributes.None);
}
return fs;
}

View file

@ -8,6 +8,8 @@
<ContentPage.Resources>
<ResourceDictionary>
<tools:NullToFalseConverter x:Key="NullToFalseConverter" />
<tools:HtmlNodeToFormattedStringConverter x:Key="HtmlNodeToFormattedStringConverter" />
<tools:HtmlNodesToFormattedStringConverter x:Key="HtmlNodesToFormattedStringConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<ScrollView Orientation="Vertical" BackgroundColor="#fdf1dc">
@ -15,9 +17,9 @@
<skia:SKCanvasView PaintSurface="PaintHeaderBar" HorizontalOptions="FillAndExpand" HeightRequest="8" />
<Label Text="{Binding Item.Name}" Style="{StaticResource Key=subsubsection}" />
<Label Text="{Binding Item.Name}" Style="{StaticResource Key=subsection}" />
<Label Text=" " />
<!--<Label Text=" " />-->
<Label FormattedText="{Binding TypeSizeAlignment}" />
@ -62,6 +64,9 @@
<Label FormattedText="{Binding SavingThrows}" IsVisible="{Binding Item.SavingThrows, Converter={StaticResource NullToFalseConverter}}" Style="{StaticResource Key=content}" />
<Label FormattedText="{Binding Skills}" IsVisible="{Binding Item.Skills, Converter={StaticResource NullToFalseConverter}}" Style="{StaticResource Key=content}" />
<Label FormattedText="{Binding DamageResistances}" IsVisible="{Binding Item.DamageResistances, Converter={StaticResource NullToFalseConverter}}" Style="{StaticResource Key=content}" />
<Label FormattedText="{Binding DamageImmunities}" IsVisible="{Binding Item.DamageImmunities, Converter={StaticResource NullToFalseConverter}}" Style="{StaticResource Key=content}" />
<Label FormattedText="{Binding ConditionImmunities}" IsVisible="{Binding Item.ConditionImmunities, Converter={StaticResource NullToFalseConverter}}" Style="{StaticResource Key=content}" />
<Label FormattedText="{Binding Senses}" IsVisible="{Binding Item.Senses, Converter={StaticResource NullToFalseConverter}}" Style="{StaticResource Key=content}" />
<Label FormattedText="{Binding Languages}" IsVisible="{Binding Item.Languages, Converter={StaticResource NullToFalseConverter}}" Style="{StaticResource Key=content}" />
<!--<Label FormattedText="{Binding Power}" Style="{StaticResource Key=content}" />-->
@ -69,14 +74,28 @@
<skia:SKCanvasView PaintSurface="PaintRedBar" HorizontalOptions="FillAndExpand" HeightRequest="8"/>
<Label FormattedText="{Binding Item.SpecialFeatures, Converter={StaticResource HtmlNodesToFormattedStringConverter}}" />
<Label Text="Actions" Style="{StaticResource Key=subsubsection}" IsVisible="{Binding Item.Actions, Converter={StaticResource NullToFalseConverter}}" />
<Label FormattedText="{Binding Item.Actions, Converter={StaticResource HtmlNodesToFormattedStringConverter}}" IsVisible="{Binding Item.Actions, Converter={StaticResource NullToFalseConverter}}" />
<Label Text="Actions légendaires" Style="{StaticResource Key=subsubsection}" IsVisible="{Binding Item.LegendaryActions, Converter={StaticResource NullToFalseConverter}}" />
<Label FormattedText="{Binding Item.LegendaryActions, Converter={StaticResource HtmlNodesToFormattedStringConverter}}" IsVisible="{Binding Item.LegendaryActions, Converter={StaticResource NullToFalseConverter}}" />
<!--<ListView ItemsSource="{Binding Item.SpecialFeatures}">
<ListView.ItemTemplate>
<DataTemplate>
<Label BindingContext="{Binding}" FormattedText="{Binding Converter={StaticResource HtmlNodeToFormattedStringConverter}}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>-->
<skia:SKCanvasView PaintSurface="PaintHeaderBar" HorizontalOptions="FillAndExpand" HeightRequest="8" />
<Label Text="{Binding Item.Description}" Style="{StaticResource Key=content}" />
<Label Text="{Binding Item.Picture}" Style="{StaticResource Key=content}" />
<!--<Label Text="{Binding Item.Picture}" Style="{StaticResource Key=content}" />-->
<Label Text="{Binding Item.Legendary}" Style="{StaticResource Key=content}" />
<Label Text="{Binding Item.Source}" Style="{StaticResource Key=content}" />
<skia:SKCanvasView PaintSurface="PaintHeaderBar" HorizontalOptions="FillAndExpand" HeightRequest="8" />
<Image Source="{Binding Image}" />
<Image Source="{Binding Item.Picture}" />
</StackLayout>
</ScrollView>

View file

@ -28,12 +28,19 @@ namespace AideDeJeuLib.Monsters
public string Charisma { get; set; }
public string SavingThrows { get; set; }
public string Skills { get; set; }
public string DamageImmunities { get; set; }
public string ConditionImmunities { get; set; }
public string DamageResistances { get; set; }
public string Senses { get; set; }
public string Languages { get; set; }
public string Challenge { get; set; }
public string Description { get; set; }
public string Picture { get; set; }
public List<HtmlNode> SpecialFeatures { get; set; }
public List<HtmlNode> Actions { get; set; }
public List<HtmlNode> LegendaryActions { get; set; }
public static Monster FromHtml(HtmlNode divBloc)
@ -66,51 +73,67 @@ namespace AideDeJeuLib.Monsters
monster.SavingThrows = divRed?.SelectSingleNode("strong[contains(text(),'Jets de sauvegarde')]")?.NextSibling?.InnerText;
monster.Skills = divRed?.SelectSingleNode("strong[contains(text(),'Compétences')]")?.NextSibling?.InnerText;
monster.DamageResistances = divRed?.SelectSingleNode("strong[contains(text(),'Résistances aux dégâts')]")?.NextSibling?.InnerText;
monster.DamageImmunities = divRed?.SelectSingleNode("strong[contains(text(),'Immunités aux dégâts')]")?.NextSibling?.InnerText;
monster.ConditionImmunities = divRed?.SelectSingleNode("strong[contains(text(),'Immunités aux conditions')]")?.NextSibling?.InnerText;
monster.Senses = divRed?.SelectSingleNode("strong[contains(text(),'Sens')]")?.NextSibling?.InnerText;
monster.Languages = divRed?.SelectSingleNode("strong[contains(text(),'Langues')]")?.NextSibling?.InnerText;
monster.Power = divRed?.SelectSingleNode("strong[contains(text(),'Puissance')]")?.NextSibling?.InnerText;
monster.Challenge = divRed?.SelectSingleNode("strong[contains(text(),'Puissance')]")?.NextSibling?.InnerText;
List<string> actions = new List<string>();
List<string> beforeActions = null;
List<string> commonActions = null;
List<string> legendaryActions = null;
var p = divSansSerif.SelectSingleNode("p");
while(p != null)
List<HtmlNode> nodes = new List<HtmlNode>();
List<HtmlNode> specialFeatures = null;
List<HtmlNode> actions = null;
List<HtmlNode> legendaryActions = null;
var node = divSansSerif.SelectSingleNode("p");
while(node != null)
{
if(p.NodeType == HtmlNodeType.Element && p.Name == "p")
if(node.NodeType == HtmlNodeType.Element && node.Name == "div")
{
actions.Add(p.InnerText);
}
else if(p.NodeType == HtmlNodeType.Element && p.Name == "div")
{
if(p.InnerText == "ACTIONS")
if(node.InnerText == "ACTIONS")
{
beforeActions = actions;
actions = new List<string>();
specialFeatures = nodes;
nodes = new List<HtmlNode>();
}
else if (p.InnerText == "ACTIONS LÉGENDAIRES")
else if (node.InnerText == "ACTIONS LÉGENDAIRES")
{
commonActions = actions;
actions = new List<string>();
actions = nodes;
nodes = new List<HtmlNode>();
}
}
p = p.NextSibling;
}
if(commonActions == null)
{
if(beforeActions == null)
{
beforeActions = actions;
}
else
{
commonActions = actions;
nodes.Add(node);
}
node = node.NextSibling;
}
if(actions == null)
{
if(specialFeatures == null)
{
specialFeatures = nodes;
}
else
{
actions = nodes;
}
}
else
{
legendaryActions = actions;
legendaryActions = nodes;
}
monster.SpecialFeatures = specialFeatures;
monster.Actions = actions;
monster.LegendaryActions = legendaryActions;
var divDescription = divBloc?.SelectSingleNode("div[contains(@class,'description')]");
monster.Description = divDescription?.InnerText;
var divSource = divBloc?.SelectSingleNode("div[contains(@class,'source')]");
monster.Source = divSource?.InnerText;
var img = divBloc?.SelectSingleNode("div[contains(@class,'center')]/img[contains(@class,'picture')]");
monster.Picture = img?.GetAttributeValue("src", null);
return monster;
}