Skip to content
This repository has been archived by the owner on Jun 19, 2020. It is now read-only.

Commit

Permalink
Simple component system for TUI apps, some basic components
Browse files Browse the repository at this point in the history
  • Loading branch information
JFronny committed May 22, 2020
1 parent b73bcce commit 11a5287
Show file tree
Hide file tree
Showing 19 changed files with 1,108 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .idea/.idea.CC-Functions/.idea/projectSettingsUpdater.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions CC-Functions.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Misc", "Misc\Misc.csproj",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "W32.Test", "W32.Test\W32.Test.csproj", "{6121A6D3-7C73-4EDE-A5A9-09DC52150824}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commandline", "Commandline\Commandline.csproj", "{08C16A0B-A69D-4229-BC6D-F6949CA7BE76}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CLITest", "CLITest\CLITest.csproj", "{3FAB0713-3021-4C6A-BF4A-ABBD542634A6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,6 +31,14 @@ Global
{6121A6D3-7C73-4EDE-A5A9-09DC52150824}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6121A6D3-7C73-4EDE-A5A9-09DC52150824}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6121A6D3-7C73-4EDE-A5A9-09DC52150824}.Release|Any CPU.Build.0 = Release|Any CPU
{08C16A0B-A69D-4229-BC6D-F6949CA7BE76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{08C16A0B-A69D-4229-BC6D-F6949CA7BE76}.Debug|Any CPU.Build.0 = Debug|Any CPU
{08C16A0B-A69D-4229-BC6D-F6949CA7BE76}.Release|Any CPU.ActiveCfg = Release|Any CPU
{08C16A0B-A69D-4229-BC6D-F6949CA7BE76}.Release|Any CPU.Build.0 = Release|Any CPU
{3FAB0713-3021-4C6A-BF4A-ABBD542634A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3FAB0713-3021-4C6A-BF4A-ABBD542634A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3FAB0713-3021-4C6A-BF4A-ABBD542634A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3FAB0713-3021-4C6A-BF4A-ABBD542634A6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
13 changes: 13 additions & 0 deletions CLITest/CLITest.csproj
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>
79 changes: 79 additions & 0 deletions CLITest/Program.cs
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");
}
}
}
36 changes: 36 additions & 0 deletions Commandline/Commandline.csproj
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>
34 changes: 34 additions & 0 deletions Commandline/TUI/Button.cs
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;
}
}
61 changes: 61 additions & 0 deletions Commandline/TUI/CheckBox.cs
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;
}
}
81 changes: 81 additions & 0 deletions Commandline/TUI/Control.cs
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;
}
}

0 comments on commit 11a5287

Please sign in to comment.