diff --git a/ModeSwitch.cs b/ModeSwitch.cs new file mode 100644 index 0000000..d8504da --- /dev/null +++ b/ModeSwitch.cs @@ -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? Enter; + private readonly string[] str = ["退出"]; + private readonly int mode = 0; + public ModeSwitch(string title, string[] a, Action 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 Enter) + { + _ = new ModeSwitch(title, a, Enter); + } + + + + + +} diff --git a/ModelingMusic/Music.cs b/ModelingMusic/Music.cs index 39d2b65..223663c 100644 --- a/ModelingMusic/Music.cs +++ b/ModelingMusic/Music.cs @@ -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() diff --git a/ModelingMusic/MusicDebug.cs b/ModelingMusic/MusicDebug.cs index 7f2922b..b19d38b 100644 --- a/ModelingMusic/MusicDebug.cs +++ b/ModelingMusic/MusicDebug.cs @@ -117,7 +117,6 @@ public class MusicDebug // Console.WriteLine("RMS Volume: " + rmsVolume); DrawVolumeBar(rmsVolume); Thread.Sleep(100); - // Thread.Sleep(1000); }; cap.StartRecording(); // new Thread(() => diff --git a/Program.cs b/Program.cs index e8557b3..7d7134a 100644 --- a/Program.cs +++ b/Program.cs @@ -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()