This repository has been archived by the owner on Jun 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simple component system for TUI apps, some basic components
- Loading branch information
Showing
19 changed files
with
1,108 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
<LangVersion>8</LangVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Commandline\Commandline.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System; | ||
using System.Drawing; | ||
using System.Threading; | ||
using CC_Functions.Commandline.TUI; | ||
|
||
namespace CLITest | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
Console.BackgroundColor = ConsoleColor.Black; | ||
Console.ForegroundColor = ConsoleColor.White; | ||
Console.Clear(); | ||
Screen screen = new Screen(40, 20); | ||
Button btn1 = new Button("Test") | ||
{ | ||
Point = new Point(2, 0) | ||
}; | ||
screen.Controls.Add(btn1); | ||
btn1.Click += (screen1, eventArgs) => | ||
{ | ||
DiffDraw.FullDraw(true); | ||
}; | ||
Label lab1 = new Label("Meem") | ||
{ | ||
Point = new Point(2, 1) | ||
}; | ||
screen.Controls.Add(lab1); | ||
screen.Controls.Add(new Label("Saas\nSoos") | ||
{ | ||
Point = new Point(2, 2) | ||
}); | ||
Button btn2 = new Button("X"); | ||
screen.Controls.Add(btn2); | ||
|
||
CheckBox box = new CheckBox("Are u gae?") | ||
{ | ||
Point = new Point(2, 3) | ||
}; | ||
screen.Controls.Add(box); | ||
box.CheckedChanged += (screen1, eventArgs) => | ||
{ | ||
lab1.Content = box.Checked ? "Sas" : "Meem"; | ||
}; | ||
|
||
TextBox tbox = new TextBox("Hello\nWorld1\n\nHow are u?") | ||
{ | ||
Size = new Size(20, 10), | ||
Point = new Point(0, 6) | ||
}; | ||
screen.Controls.Add(tbox); | ||
|
||
Slider slider = new Slider | ||
{ | ||
Point = new Point(2, 4), | ||
Size = new Size(8, 2), | ||
MaxValue = 75, | ||
StepSize = 14, | ||
MinValue = -3, | ||
Value = 7 | ||
}; | ||
screen.Controls.Add(slider); | ||
|
||
bool visible = true; | ||
btn2.Click += (screen1, eventArgs) => visible = false; | ||
screen.Close += (screen1, eventArgs) => visible = false; | ||
screen.Render(); | ||
while (visible) | ||
{ | ||
Thread.Sleep(50); | ||
screen.ReadInput(); | ||
} | ||
Console.ResetColor(); | ||
Console.Clear(); | ||
Console.WriteLine("Bye"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.1</TargetFramework> | ||
<OutputType>Library</OutputType> | ||
<RootNamespace>CC_Functions.Commandline</RootNamespace> | ||
<AssemblyName>CC_Functions.Commandline</AssemblyName> | ||
<LangVersion>8</LangVersion> | ||
<Deterministic>false</Deterministic> | ||
<PackageId>CC-Functions.Commandline</PackageId> | ||
<Title>CC-Functions.Commandline</Title> | ||
<Authors>CC24</Authors> | ||
<Description>Random pieces of code used across my projects. CLI/TUI extensions</Description> | ||
<Copyright>Copyright 2020</Copyright> | ||
<PackageProjectUrl>https://github.com/JFronny/CC-Functions</PackageProjectUrl> | ||
<RepositoryUrl>https://github.com/JFronny/CC-Functions.git</RepositoryUrl> | ||
<RepositoryType>git</RepositoryType> | ||
<AssemblyVersion>1.1.*</AssemblyVersion> | ||
<FileVersion>1.0.0.0</FileVersion> | ||
<VersionSuffix>0.0</VersionSuffix> | ||
<PackageVersion>1.1.$(VersionSuffix)</PackageVersion> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> | ||
<DocumentationFile>bin\Debug\Commandline.xml</DocumentationFile> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> | ||
<DocumentationFile>bin\Release\Commandline.xml</DocumentationFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Misc\Misc.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System.Drawing; | ||
using CC_Functions.Misc; | ||
|
||
namespace CC_Functions.Commandline.TUI | ||
{ | ||
public class Button : Control | ||
{ | ||
public string Content; | ||
public Button(string content) | ||
{ | ||
Content = content; | ||
char[,] tmp = Content.ToNDArray2D(); | ||
Size = new Size(tmp.GetLength(0), tmp.GetLength(1)); | ||
} | ||
|
||
public override Pixel[,] Render() | ||
{ | ||
char[,] inp = Content.ToNDArray2D(); | ||
inp = inp.Resize(Size.Width, Size.Height); | ||
Pixel[,] output = new Pixel[Size.Width, Size.Height]; | ||
for (int x = 0; x < Size.Width; x++) | ||
for (int y = 0; y < Size.Height; y++) | ||
output[x, y] = new Pixel(Selected ? ForeColor : BackColor, Selected ? BackColor : ForeColor, inp[x, y]); | ||
return output; | ||
} | ||
|
||
protected override void Resize(int width, int height) | ||
{ | ||
//ignored for [Render]s sake, do not use | ||
} | ||
|
||
public override bool Selectable { get; } = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using System.Drawing; | ||
using CC_Functions.Misc; | ||
|
||
namespace CC_Functions.Commandline.TUI | ||
{ | ||
public class CheckBox : Control | ||
{ | ||
public string Content; | ||
public bool Checked = false; | ||
public CheckBox(string content) | ||
{ | ||
Content = content; | ||
Input += (screen, args) => | ||
{ | ||
switch (args.Info.Key) | ||
{ | ||
case ConsoleKey.LeftArrow: | ||
case ConsoleKey.RightArrow: | ||
case ConsoleKey.Spacebar: | ||
Checked = !Checked; | ||
CheckedChanged?.Invoke(screen, args); | ||
break; | ||
} | ||
}; | ||
Click += (screen, args) => | ||
{ | ||
Checked = !Checked; | ||
CheckedChanged?.Invoke(screen, args); | ||
}; | ||
} | ||
|
||
public override Pixel[,] Render() | ||
{ | ||
char[,] inp1 = Content.ToNDArray2D(); | ||
char[,] inp = new char[inp1.GetLength(0), inp1.GetLength(1) + 4]; | ||
inp.Populate(' '); | ||
inp1.CopyTo(inp, new Point(4, 0)); | ||
inp[0, 0] = '['; | ||
inp[0, 1] = Checked ? 'X' : SpecialChars.empty; | ||
inp[0, 2] = ']'; | ||
int w = inp.GetLength(0); | ||
int h = inp.GetLength(1); | ||
Pixel[,] output = new Pixel[w, h]; | ||
for (int x = 0; x < w; x++) | ||
for (int y = 0; y < h; y++) | ||
output[x, y] = new Pixel(Selected ? ForeColor : BackColor, Selected ? BackColor : ForeColor, inp[x, y]); | ||
Size = new Size(w, h); | ||
return output; | ||
} | ||
|
||
protected override void Resize(int width, int height) | ||
{ | ||
//ignored for [Render]s sake, do not use | ||
} | ||
|
||
public override bool Selectable { get; } = true; | ||
public delegate void OnCheckedChanged(Screen screen, EventArgs e); | ||
public event OnClick CheckedChanged; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System; | ||
using System.Drawing; | ||
|
||
namespace CC_Functions.Commandline.TUI | ||
{ | ||
public abstract class Control | ||
{ | ||
private Point _point; | ||
private Size _size; | ||
public abstract Pixel[,] Render(); | ||
protected abstract void Resize(int width, int height); | ||
private void Resize(Size size) => Resize(size.Width, size.Height); | ||
|
||
public Size Size | ||
{ | ||
set | ||
{ | ||
_size = value; | ||
Resize(value); | ||
} | ||
get => _size; | ||
} | ||
|
||
public Point Point | ||
{ | ||
get => _point; | ||
set => _point = value; | ||
} | ||
|
||
public ConsoleColor ForeColor { get; set; } = Console.ForegroundColor; | ||
public ConsoleColor BackColor { get; set; } = Console.BackgroundColor; | ||
public abstract bool Selectable { get; } | ||
|
||
/// <summary> | ||
/// Called when [enter] is pressed while the control is selected | ||
/// </summary> | ||
/// <param name="screen">An instance of the calling screen</param> | ||
/// <param name="e">Args</param> | ||
public delegate void OnClick(Screen screen, EventArgs e); | ||
/// <summary> | ||
/// Called when [enter] is pressed while the control is selected | ||
/// </summary> | ||
public event OnClick Click; | ||
/// <summary> | ||
/// Called when the control is selected and unknown input is given | ||
/// </summary> | ||
/// <param name="screen">An instance of the calling screen</param> | ||
/// <param name="e">Args</param> | ||
public delegate void OnInput(Screen screen, InputEventArgs e); | ||
/// <summary> | ||
/// Called when the control is selected and unknown input is given | ||
/// </summary> | ||
public event OnInput Input; | ||
/// <summary> | ||
/// Whether the object is selected. Used internally and for drawing | ||
/// </summary> | ||
public bool Selected { get; internal set; } = false; | ||
/// <summary> | ||
/// Invokes click events | ||
/// </summary> | ||
/// <param name="screen">The calling screen</param> | ||
internal void InvokeClick(Screen screen) | ||
{ | ||
Click?.Invoke(screen, new EventArgs()); | ||
} | ||
/// <summary> | ||
/// Invokes input events | ||
/// </summary> | ||
/// <param name="screen">The calling screen</param> | ||
/// <param name="info">The input data</param> | ||
internal void InvokeInput(Screen screen, ConsoleKeyInfo info) | ||
{ | ||
Input?.Invoke(screen, new InputEventArgs(info)); | ||
} | ||
|
||
/// <summary> | ||
/// Whether the control should be rendered | ||
/// </summary> | ||
public bool Visible = true; | ||
} | ||
} |
Oops, something went wrong.