Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hringhorni #194

Open
wants to merge 4 commits into
base: universal
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
593 changes: 593 additions & 0 deletions WFBot.Koharu/Base.cs

Large diffs are not rendered by default.

282 changes: 282 additions & 0 deletions WFBot.Koharu/DrawingContents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
using System.Diagnostics;
using System.Drawing;
using SkiaSharp;
using Topten.RichTextKit;
using static System.Net.Mime.MediaTypeNames;

namespace WFBot.Koharu;



public sealed class MarginCommand : ComplexDrawingCommand
{
int _top;
int _left;
int _down;
int _right;
IDrawingCommand _drawingCommand;

public MarginCommand(IDrawingCommand command, int top, int left, int down, int right)
{
_drawingCommand = command;
_top = top;
_left = left;
_down = down;
_right = right;
}

public override Size Size
{
get
{
var size = _drawingCommand.Size;
size.Height += _top + _down;
size.Width += _left + _right;
return size;
}
}

public static MarginCommand Of(IDrawingCommand command, int margin) =>
new MarginCommand(command, margin, margin, margin, margin);


public override void AcquireAllDrawingCommands(List<DrawingContentWithPosition> commands, Point offset)
{
offset.X += _left;
offset.Y += _top;
ApplyCommand(commands, _drawingCommand, offset);
}
}

public sealed class RoundedRectCommand : ComplexDrawingCommand
{
IDrawingCommand _drawingCommand;
float _curvature;
Color _color;
int _margin;

public RoundedRectCommand(IDrawingCommand command, Color color, float curvature, int margin = 0)
{
_color = color;
_drawingCommand = command;
_margin = margin;
_curvature = curvature;
}

public override Size Size
{
get
{
var size = _drawingCommand.Size;
size.Height += 2 * _margin;
size.Width += 2 * _margin;
return size;
}
}

public override DrawingPriority DrawingPriority => DrawingPriority.Background;
public override void AcquireAllDrawingCommands(List<DrawingContentWithPosition> commands, Point offset)
{
ApplyCommand(commands, new RoundedRectCommandCore(_drawingCommand.Size, _color, _curvature, _margin), offset);
offset.X += _margin;
offset.Y += _margin;
ApplyCommand(commands, _drawingCommand, offset);
}
}

public sealed class RoundedRectCommandCore : IDrawingContent
{
float _curvature;

public RoundedRectCommandCore(Size size, Color color, float curvature, int margin)
{
size.Width += margin * 2;
size.Height += margin * 2;
Size = size;
Color = color;
_curvature = curvature;
}

public Color Color { get; }
public Size Size { get; }
public void DrawCore(SKCanvas canvas, Point position)
{
canvas.DrawRoundRect(position.X, position.Y, Size.Width, Size.Height, _curvature, _curvature, new SKPaint() { Color = Color.ToSkColor() });
}
}

public sealed class FillCommand : ComplexDrawingCommand
{
IDrawingCommand _drawingCommand;
Color _color;

public FillCommand(IDrawingCommand command, Color color)
{
_color = color;
_drawingCommand = command;
}

public override Size Size => _drawingCommand.Size;
public override DrawingPriority DrawingPriority => DrawingPriority.Background;
public override void AcquireAllDrawingCommands(List<DrawingContentWithPosition> commands, Point offset)
{
ApplyCommand(commands, new FillCommandCore(_drawingCommand.Size, _color), offset);
ApplyCommand(commands, _drawingCommand, offset);
}
}
public sealed class FillCommandCore : IDrawingContent
{
public FillCommandCore(Size size, Color color)
{
Size = size;
Color = color;
}

public Color Color { get; }
public Size Size { get; }
public void DrawCore(SKCanvas canvas, Point position)
{
canvas.DrawRect(position.X, position.Y, Size.Width, Size.Height, new SKPaint() { Color = Color.ToSkColor() });
}
}

class MyFontMapper : FontMapper
{
static SKTypeface font = SKTypeface.FromFile("WFConfigs/font.ttf");
public override SKTypeface TypefaceFromStyle(IStyle style, bool ignoreFontVariants)
{
return font;
}
}

public sealed class TextCommand : IDrawingContent
{
public TextCommand(string text, TextOptions options)
{
textBlock = new TextBlock();
textBlock.AddText(text, new Style()
{
TextColor = options.Color.ToSkColor(),
FontSize = options.Size,
FontWeight = options.Bold ? 400 : 700,
});

textBlock.MaxWidth = options.MaxWidth == -1 ? null : options.MaxWidth;
textBlock.FontMapper = new MyFontMapper();
size = new Size((int)textBlock.MeasuredWidth, (int)textBlock.MeasuredHeight);
//Debug.Assert(size.Height != 0);
}

//static SKCanvas measurerCanvas = SKSurface.Create(new SKImageInfo(800, 700, SKColorType.Rgba8888)).Canvas;


Size size;
public Size Size => size;

TextBlock textBlock;

public void DrawCore(SKCanvas canvas, Point position)
{
textBlock.Paint(canvas, position, new TextPaintOptions(){Edging = SKFontEdging.Antialias});
}
}

public class RichTextBuilder
{
TextBlock textBlock = new TextBlock();
TextOptions lastTextOptions;
string? lastText = null;

void Commit()
{
if (lastText != null)
{
textBlock.AddText(lastText, new Style()
{
TextColor = lastTextOptions.Color.ToSkColor(),
FontSize = lastTextOptions.Size,
FontWeight = lastTextOptions.Bold ? 400 : 700,
});
lastTextOptions = Painter<object>.textOptions;
}
}
private RichTextBuilder() {}

public static RichTextBuilder Create(int maxWidth = 1000)
{
var b = new RichTextBuilder();
b.lastTextOptions = Painter<object>.textOptions;
b.textBlock.MaxWidth = maxWidth;
return b;
}

public RichTextBuilder Text(string text)
{
Commit();
lastText = text;
return this;
}

public RichTextBuilder Bold()
{
lastTextOptions.Bold = true;
return this;
}

public RichTextBuilder Color(Color color)
{
lastTextOptions.Color = color;
return this;
}

public RichTextBuilder Size(int size)
{
lastTextOptions.Size = size;
return this;
}

public IDrawingCommand Build()
{
Commit();
return new RichTextCommand(textBlock);
}
}

public class RichTextCommand : IDrawingCommand
{
TextBlock textBlock;

public RichTextCommand(TextBlock textBlock)
{
this.textBlock = textBlock;

textBlock.FontMapper = new MyFontMapper();
size = new Size((int)textBlock.MeasuredWidth, (int)textBlock.MeasuredHeight);
}
Size size;
public Size Size => size;

public void DrawCore(SKCanvas canvas, Point position)
{
textBlock.Paint(canvas, position, new TextPaintOptions() { Edging = SKFontEdging.Antialias });
}
}

public record struct TextOptions(int Size, Color Color, bool Bold, int MaxWidth);

public sealed class ImageCommand : IDrawingContent
{
SKBitmap bitmap;

public ImageCommand(SKBitmap bitmap)
{
this.bitmap = bitmap;
}

public Size Size => Size.Of(bitmap.Width, bitmap.Height);
public void DrawCore(SKCanvas canvas, Point position)
{
canvas.DrawBitmap(bitmap, position);
}

}
7 changes: 7 additions & 0 deletions WFBot.Koharu/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
global using System;
global using System.Collections.Generic;
global using System.Linq;
global using System.Text;
global using System.Threading.Tasks;
// ReSharper disable UnusedMember.Global
global using Color = System.Drawing.Color;
62 changes: 62 additions & 0 deletions WFBot.Koharu/Models.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
namespace WFBot.Koharu;

public record InvasionData(InvasionData.WFInvasion[] Invasions)
{
public class WFInvasion
{
public string id { get; set; }
public DateTime activation { get; set; }
public string startString { get; set; }
public string node { get; set; }
public string desc { get; set; }
public RewardInfo attackerReward { get; set; }
public string attackingFaction { get; set; }
public RewardInfo defenderReward { get; set; }
public string defendingFaction { get; set; }
public bool vsInfestation { get; set; }
public int count { get; set; }
public int requiredRuns { get; set; }
public float completion { get; set; }
public bool completed { get; set; }
public string eta { get; set; }
public string[] rewardTypes { get; set; }
}

public class RewardInfo
{
public Counteditem[] countedItems { get; set; }
public int credits { get; set; }
public string asString { get; set; }
public string itemString { get; set; }
public string thumbnail { get; set; }
public int color { get; set; }
}

public class Counteditem
{
public int count { get; set; }
public string type { get; set; }
}
}


public record FissureData(List<FissureData.Fissure> Fissures, int Tier)
{
public class Fissure
{
public string id { get; set; }

Check warning on line 47 in WFBot.Koharu/Models.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'id' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 47 in WFBot.Koharu/Models.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'id' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public DateTime activation { get; set; }
public string startString { get; set; }

Check warning on line 49 in WFBot.Koharu/Models.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'startString' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 49 in WFBot.Koharu/Models.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'startString' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public DateTime expiry { get; set; }
public bool active { get; set; }
public string node { get; set; }

Check warning on line 52 in WFBot.Koharu/Models.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'node' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 52 in WFBot.Koharu/Models.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'node' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string missionType { get; set; }

Check warning on line 53 in WFBot.Koharu/Models.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'missionType' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 53 in WFBot.Koharu/Models.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'missionType' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string enemy { get; set; }

Check warning on line 54 in WFBot.Koharu/Models.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'enemy' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 54 in WFBot.Koharu/Models.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'enemy' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string tier { get; set; }
public int tierNum { get; set; }
public bool expired { get; set; }
public string eta { get; set; }
public bool isStorm { get; set; }
public bool isHard { get; set; }
}
}
Loading
Loading