Files
Terminal.Gui/UICatalog/Scenarios/Animation.cs
BDisp ea24de3a27 Fixes #2482. Refactor Redraw - Non-virtual with the right set of virtual OnXXX methods. (#2577)
* Fixes #2482. Refactor Redraw - Non-virtual with the right set of virtual OnXXX methods.

* Change documentation comments.

* Fixes #2575 - TableView to use interface instead of System.Data.DataTable (#2576)

* WIP: Add ITableDataSource

* WIP: Refactor TableView

* WIP: Port CSVEditor

* WIP: Port TableEditor

* WIP: Port MultiColouredTable scenario

* Fix bug of adding duplicate column styles

* Update tests to use DataTableSource

* Tidy up

* Add EnumerableTableDataSource<T>

* Add test for EnumerableTableDataSource

* Add test for EnumerableTableDataSource

* Add code example to xmldoc

* Add ProcessTable scenario

* Rename ITableDataSource to ITableSource and update docs

* Rename EnumerableTableDataSource to EnumerableTableSource

* Fixed Frame != Bounds; changed UICat Scenarios list to use tableview!

* Fix scroll resetting in ProcessTable scenario

* Fix unit tests by setting Frame to same as Bounds

* Document why we have to measure our data for use with TableView

---------

Co-authored-by: Tig Kindel <tig@users.noreply.github.com>

* Fixes #2582 - Refactors FileDialog for cleaner data model (#2583)

* WIP: Add ITableDataSource

* WIP: Refactor TableView

* WIP: Port CSVEditor

* WIP: Port TableEditor

* WIP: Port MultiColouredTable scenario

* Fix bug of adding duplicate column styles

* Update tests to use DataTableSource

* Tidy up

* Add EnumerableTableDataSource<T>

* Add test for EnumerableTableDataSource

* Add test for EnumerableTableDataSource

* Add code example to xmldoc

* Add ProcessTable scenario

* Rename ITableDataSource to ITableSource and update docs

* Rename EnumerableTableDataSource to EnumerableTableSource

* Fixed Frame != Bounds; changed UICat Scenarios list to use tableview!

* Fix scroll resetting in ProcessTable scenario

* Fix unit tests by setting Frame to same as Bounds

* Document why we have to measure our data for use with TableView

* WIP: Simplify FileDialogs use of TableView

* WIP start migrating sorter

* WIP new filedialog table source mostly working

* WIP remove sorter class

* Refactor GetOrderByValue to be adjacent to GetColumnValue

* Fix collection navigator back so it ignores icon

* Fix unit tests

* Tidy up

* Fix UseColors

* Add test for UseColors

---------

Co-authored-by: Tig Kindel <tig@users.noreply.github.com>

* Fixes #2196. TextView: Setting Text places cursor at beginning, unlike TextField (#2572)

* Fixes #2196. TextView: Setting Text places cursor at beginning, unlike TextField

* Change all private members to have the _prefix.

* Renamed local member to prevLayoutStyle.

* Helper function for SetNeedsDisplay.

* Fixes #2569. Borders scenarios needed to be refactored. (#2570)

* Fixes #2569. Borders scenarios needed to be refactored.

* Fix border title with width equal to 4 and thickness top grater than 1.

* Improves border manipulation on borders scenarios.

* Prevents null value on the margin, border and padding thickness on the border scenarios.

* Remove NStack using dependence and fix prior commit.

* Refactoring the Frames scenario.

* Deleted borders scenarios.

* I did not realize that it was changed to SetSubViewNeedsDisplay.

* Re-layout; fixed colorpicker; fixed radio group

* Remove Thickness.IsEmpty as requested.

* Change the Frames scenario as requested.

---------

Co-authored-by: Tig Kindel <tig@users.noreply.github.com>

* Builds CollectionNavigator support into UI Catalog for TableView (#2584)

* Builds collectionnav support into UI cat for TableView

* Fixes keyboard mapping

* MultiSelect = false for TableView

* MultiSelect = false doesn't unbind ctrl-a

* Fixes #2581 Refactor CollectionNavigator so it supports TableView (#2586)

* Refactor CollectionNavigator to a base and a collection implementation

* Refactor CollectionNavigatorBase to look for first match smartly

* Add TableCollectionNavigator

* Make TableCollectionNavigator a core part of TableView

* Fix bad merge

* Added tests for tableview collection navigator

* Add FileDialogCollectionNavigator which ignores . and directory separator prefixes on file names

* whitespace fixes

---------

Co-authored-by: Tig <tig@users.noreply.github.com>

* Resolving merge conflicts.

* Fix merge errors.

* Fix merge errors.

* Add Command.Accept and snap to the selected glyph when ShowHorizontalScrollIndicator change to true.

* Reformat.

* Reformat again.

---------

Co-authored-by: Thomas Nind <31306100+tznind@users.noreply.github.com>
Co-authored-by: Tig Kindel <tig@users.noreply.github.com>
2023-05-04 13:17:59 +02:00

221 lines
6.3 KiB
C#

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.ColorSpaces;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Terminal.Gui;
using Attribute = Terminal.Gui.Attribute;
namespace UICatalog.Scenarios {
[ScenarioMetadata (Name: "Animation", Description: "Demonstration of how to render animated images with threading.")]
[ScenarioCategory ("Colors")]
public class Animation : Scenario {
private bool isDisposed;
public override void Setup ()
{
base.Setup ();
var imageView = new ImageView () {
Width = Dim.Fill (),
Height = Dim.Fill () - 2,
};
Win.Add (imageView);
var lbl = new Label ("Image by Wikiscient") {
Y = Pos.AnchorEnd (2)
};
Win.Add (lbl);
var lbl2 = new Label ("https://commons.wikimedia.org/wiki/File:Spinning_globe.gif") {
Y = Pos.AnchorEnd (1)
};
Win.Add (lbl2);
var dir = new DirectoryInfo (Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location));
var f = new FileInfo (
Path.Combine (dir.FullName, "Scenarios", "Spinning_globe_dark_small.gif"));
if (!f.Exists) {
MessageBox.ErrorQuery ("Could not find gif", "Could not find " + f.FullName, "Ok");
return;
}
imageView.SetImage (Image.Load<Rgba32> (File.ReadAllBytes (f.FullName)));
Task.Run (() => {
while (!isDisposed) {
// When updating from a Thread/Task always use Invoke
Application.MainLoop.Invoke (() => {
imageView.NextFrame ();
imageView.SetNeedsDisplay ();
});
Task.Delay (100).Wait ();
}
});
}
protected override void Dispose (bool disposing)
{
isDisposed = true;
base.Dispose (disposing);
}
// This is a C# port of https://github.com/andraaspar/bitmap-to-braille by Andraaspar
/// <summary>
/// Renders an image as unicode Braille.
/// </summary>
public class BitmapToBraille {
public const int CHAR_WIDTH = 2;
public const int CHAR_HEIGHT = 4;
const string CHARS = " ⠁⠂⠃⠄⠅⠆⠇⡀⡁⡂⡃⡄⡅⡆⡇⠈⠉⠊⠋⠌⠍⠎⠏⡈⡉⡊⡋⡌⡍⡎⡏⠐⠑⠒⠓⠔⠕⠖⠗⡐⡑⡒⡓⡔⡕⡖⡗⠘⠙⠚⠛⠜⠝⠞⠟⡘⡙⡚⡛⡜⡝⡞⡟⠠⠡⠢⠣⠤⠥⠦⠧⡠⡡⡢⡣⡤⡥⡦⡧⠨⠩⠪⠫⠬⠭⠮⠯⡨⡩⡪⡫⡬⡭⡮⡯⠰⠱⠲⠳⠴⠵⠶⠷⡰⡱⡲⡳⡴⡵⡶⡷⠸⠹⠺⠻⠼⠽⠾⠿⡸⡹⡺⡻⡼⡽⡾⡿⢀⢁⢂⢃⢄⢅⢆⢇⣀⣁⣂⣃⣄⣅⣆⣇⢈⢉⢊⢋⢌⢍⢎⢏⣈⣉⣊⣋⣌⣍⣎⣏⢐⢑⢒⢓⢔⢕⢖⢗⣐⣑⣒⣓⣔⣕⣖⣗⢘⢙⢚⢛⢜⢝⢞⢟⣘⣙⣚⣛⣜⣝⣞⣟⢠⢡⢢⢣⢤⢥⢦⢧⣠⣡⣢⣣⣤⣥⣦⣧⢨⢩⢪⢫⢬⢭⢮⢯⣨⣩⣪⣫⣬⣭⣮⣯⢰⢱⢲⢳⢴⢵⢶⢷⣰⣱⣲⣳⣴⣵⣶⣷⢸⢹⢺⢻⢼⢽⢾⢿⣸⣹⣺⣻⣼⣽⣾⣿";
public int WidthPixels { get; }
public int HeightPixels { get; }
public Func<int, int, bool> PixelIsLit { get; }
public BitmapToBraille (int widthPixels, int heightPixels, Func<int, int, bool> pixelIsLit)
{
WidthPixels = widthPixels;
HeightPixels = heightPixels;
PixelIsLit = pixelIsLit;
}
public string GenerateImage ()
{
int imageHeightChars = (int)Math.Ceiling ((double)HeightPixels / CHAR_HEIGHT);
int imageWidthChars = (int)Math.Ceiling ((double)WidthPixels / CHAR_WIDTH);
var result = new StringBuilder ();
for (int y = 0; y < imageHeightChars; y++) {
for (int x = 0; x < imageWidthChars; x++) {
int baseX = x * CHAR_WIDTH;
int baseY = y * CHAR_HEIGHT;
int charIndex = 0;
int value = 1;
for (int charX = 0; charX < CHAR_WIDTH; charX++) {
for (int charY = 0; charY < CHAR_HEIGHT; charY++) {
int bitmapX = baseX + charX;
int bitmapY = baseY + charY;
bool pixelExists = bitmapX < WidthPixels && bitmapY < HeightPixels;
if (pixelExists && PixelIsLit (bitmapX, bitmapY)) {
charIndex += value;
}
value *= 2;
}
}
result.Append (CHARS [charIndex]);
}
result.Append ('\n');
}
return result.ToString ().TrimEnd ();
}
}
class ImageView : View {
private int frameCount;
private int currentFrame = 0;
private Image<Rgba32> [] fullResImages;
private Image<Rgba32> [] matchSizes;
private string [] brailleCache;
Rect oldSize = Rect.Empty;
internal void SetImage (Image<Rgba32> image)
{
frameCount = image.Frames.Count;
fullResImages = new Image<Rgba32> [frameCount];
matchSizes = new Image<Rgba32> [frameCount];
brailleCache = new string [frameCount];
for (int i = 0; i < frameCount - 1; i++) {
fullResImages [i] = image.Frames.ExportFrame (0);
}
fullResImages [frameCount - 1] = image;
this.SetNeedsDisplay ();
}
public void NextFrame ()
{
currentFrame = (currentFrame + 1) % frameCount;
}
public override void OnDrawContent (Rect contentArea)
{
base.OnDrawContent (contentArea);
if (oldSize != Bounds) {
// Invalidate cached images now size has changed
matchSizes = new Image<Rgba32> [frameCount];
brailleCache = new string [frameCount];
oldSize = Bounds;
}
var imgScaled = matchSizes [currentFrame];
var braille = brailleCache [currentFrame];
if (imgScaled == null) {
var imgFull = fullResImages [currentFrame];
// keep aspect ratio
var newSize = Math.Min (Bounds.Width, Bounds.Height);
// generate one
matchSizes [currentFrame] = imgScaled = imgFull.Clone (
x => x.Resize (
newSize * BitmapToBraille.CHAR_HEIGHT,
newSize * BitmapToBraille.CHAR_HEIGHT));
}
if (braille == null) {
brailleCache [currentFrame] = braille = GetBraille (matchSizes [currentFrame]);
}
var lines = braille.Split ('\n');
for (int y = 0; y < lines.Length; y++) {
var line = lines [y];
for (int x = 0; x < line.Length; x++) {
AddRune (x, y, line [x]);
}
}
}
private string GetBraille (Image<Rgba32> img)
{
var braille = new BitmapToBraille (
img.Width,
img.Height,
(x, y) => IsLit (img, x, y));
return braille.GenerateImage ();
}
private bool IsLit (Image<Rgba32> img, int x, int y)
{
var rgb = img [x, y];
return rgb.R + rgb.G + rgb.B > 50;
}
}
}
}