优化控制台选择

This commit is contained in:
muqing 2024-08-28 08:56:27 +08:00
parent ef88b0ae8d
commit 06ecb9ab09
4 changed files with 91 additions and 88 deletions

81
ModeSwitch.cs Normal file
View File

@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Debug;
public class ModeSwitch
{
public event Action<int>? Enter;
private readonly string[] str = ["退出"];
private readonly int mode = 0;
public ModeSwitch(string title, string[] a, Action<int> Enter)
{
this.Enter = Enter;
str = a;
Console.WriteLine(title);
for (int i = 0; i < str.Length; i++)
{
Console.WriteLine($"{i}.{str[i]}");
}
do
{
Console.SetCursorPosition(CalculateActualLength($"{mode}.{str[mode]}") + 1, mode + 1);
Console.Write('>');
var key = Console.ReadKey(true);
Console.SetCursorPosition(CalculateActualLength($"{mode}.{str[mode]}") + 1, mode + 1);
Console.Write(' ');
if (key.Key >= ConsoleKey.D0 && key.Key <= ConsoleKey.D9)
{
int v1 = int.Parse(key.KeyChar.ToString());
if (v1 >= 0 && v1 < str.Length)
mode = v1;
}
else
switch (key.Key)
{
case ConsoleKey.UpArrow:
mode--;
break;
case ConsoleKey.DownArrow:
mode++;
break;
case ConsoleKey.Enter:
Enter?.Invoke(mode);
return;
default:
break;
}
mode = int.Max(0, int.Min(str.Length - 1, mode));
} while (true);
//Console.CursorVisible = false;
}
private static int CalculateActualLength(string text)
{
Encoding encoding = Encoding.UTF8;
int length = 0;
foreach (char c in text)
{
byte[] bytes = encoding.GetBytes(c.ToString());
if (bytes.Length > 1) // 汉字或宽字符
length += 2;
else
length += 1;
}
return length;
}
public static void New(string title, string[] a, Action<int> Enter)
{
_ = new ModeSwitch(title, a, Enter);
}
}

View File

@ -11,53 +11,12 @@ public class Music
public Music()
{
Console.Clear();
Program.printfLine("模拟音游 1.0 by ");
string[] str = ["0 退出", "1.启动", "2.测试下落", "3.指定字符串", "4.随机指定字符串下落", "5.扬声器节奏"];
// Program.printfLine("1.启动 2.测试下落 3.指定字符串 4.随机指定字符串下落 5.扬声器节奏 0.退出");
foreach (var aa in str)
{
Program.printfLine(aa);
}
Console.SetCursorPosition(0, mode + 1);
do
{
var v = Console.ReadKey(true);
// 检查是否为数字
if (v.Key >= ConsoleKey.D0 && v.Key <= ConsoleKey.D9)
{
int v1 = int.Parse(v.KeyChar.ToString());
if (v1 >= 0 && v1 < str.Length)
mode = v1;
}
else
{
switch (v.Key)
{
case ConsoleKey.UpArrow:
mode = mode == 0 ? mode : mode - 1;
break;
case ConsoleKey.DownArrow:
mode = mode == str.Length - 1 ? mode : mode + 1;
break;
case ConsoleKey.Enter:
if (mode == 0)
{
//退出程序
Environment.Exit(0);
return;
}
goto Start;
break;
}
}
// Console.WriteLine("无效输入请确保您输入的是一个数字0-9。");
Console.SetCursorPosition(0, mode + 1);
} while (true);
Start:
string[] str = ["退出", "启动", "测试下落", "指定字符串", "随机指定字符串下落", "扬声器节奏"];
ModeSwitch.New("模拟音游 1.0 by ", str, (a) =>
{
mode = a;
Init().GetAwaiter().GetResult(); // 启动
}
});
}
private async Task Init()

View File

@ -117,7 +117,6 @@ public class MusicDebug
// Console.WriteLine("RMS Volume: " + rmsVolume);
DrawVolumeBar(rmsVolume);
Thread.Sleep(100);
// Thread.Sleep(1000);
};
cap.StartRecording();
// new Thread(() =>

View File

@ -49,47 +49,11 @@ public class Program
// new Debug.MinLong.KongLong();
// return;
Console.Clear();
int y = 0;
printfLine("请选择你要执行的操作:");
string[] str = ["0 退出", "1 抽卡", "2 铁锈位置搜索引擎", "3 音乐调试", "4 小龙快跑"];
for (int i = 0; i < str.Length; i++)
Console.CursorVisible = false;
string[] str = ["退出", "抽卡", "铁锈位置搜索引擎", "音乐调试", "小龙快跑"];
var modeSwitch = new ModeSwitch("请选择你要执行的操作:", str, (a) =>
{
printfLine(str[i]);
}
Console.SetCursorPosition(0, y + 1);
do
{
var v = Console.ReadKey(true);
if (v.Key >= ConsoleKey.D0 && v.Key <= ConsoleKey.D9)
{
int v1 = int.Parse(v.KeyChar.ToString());
if (v1 >= 0 && v1 < str.Length)
{
y = v1;
}
}
else
switch (v.Key)
{
case ConsoleKey.UpArrow:
//设置光标位置
y = y == 0 ? y : y - 1;
break;
case ConsoleKey.DownArrow:
y = y + 1 == str.Length ? y : y + 1;
break;
case ConsoleKey.Enter:
goto Start;
}
// Console.WriteLine(v.Key);
Console.SetCursorPosition(0, y + 1);
// {
} while (true);
// Console.WriteLine("请选择你要执行的操作:");
Start:
{
switch (y)
switch (a)
{
case 0:
Environment.Exit(0);
@ -102,12 +66,12 @@ public class Program
break;
case 3:
_ = new Music();
return;
break;
default:
printfLine("还没有这个功能");
break;
}
}
});
}
public static void KeyEndThread()