using System.Collections.ObjectModel; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using Hk4eGm.Services; using Hk4eGm.UI.Pages; namespace Hk4eGm; public partial class MainWindow : Window { public AppConfig Config { get; } public DataStore Data { get; } public MuipClient Muip { get; } public SshService Ssh { get; } public MySqlService Db { get; } private readonly Dictionary _pages = new(); private readonly List<(string Key, StackPanel Item)> _nav = new(); private readonly ObservableCollection _log = new(); public MainWindow() { InitializeComponent(); Config = new AppConfig(); Data = new DataStore(); Muip = new MuipClient(Config); Ssh = new SshService(Config); Db = new MySqlService(Config); BuildNav(); BuildPages(); ShowPage("item"); AddLog($"hk4e-GM C# 复刻版启动 · 配置 {AppConfig.ConfigPath}"); } // ---------------------------------------------------------------- 导航 private void BuildNav() { var items = new (string Key, string Text, string Glyph)[] { ("item", "物品", "\uE7B8"), ("weapon", "武器", "\uE7FC"), ("avatar", "角色", "\uE77B"), ("artifact", "圣遗物", "\uE735"), ("generate", "生成", "\uE8B7"), ("quest", "任务", "\uE8A5"), ("misc", "其它", "\uE71D"), ("template", "模板", "\uE8FD"), ("start", "应用", "\uE71D"), ("log", "日志", "\uE7C3"), ("about", "关于", "\uE946"), }; foreach (var (key, text, glyph) in items) AddNavItem(key, text, glyph, 0); AddNavItem("settings", "设置", "\uE713", 0); NavPanel.Children.Add(new Border { Height = 4 }); } private void AddNavItem(string key, string text, string glyph, double top) { var panel = new StackPanel { Height = 60, Cursor = System.Windows.Input.Cursors.Hand, Background = Brushes.Transparent }; var icon = new TextBlock { Text = glyph, FontFamily = new FontFamily("Segoe MDL2 Assets"), FontSize = 16, Foreground = new SolidColorBrush(Color.FromRgb(0xC7, 0xC7, 0xC7)), HorizontalAlignment = HorizontalAlignment.Center, Margin = new Thickness(0, 6, 0, 0), }; var label = new TextBlock { Text = text, FontSize = 12, HorizontalAlignment = HorizontalAlignment.Center, Foreground = new SolidColorBrush(Color.FromRgb(0xC7, 0xC7, 0xC7)), Margin = new Thickness(0, 4, 0, 0), }; panel.Children.Add(icon); panel.Children.Add(label); panel.MouseLeftButtonUp += (_, _) => ShowPage(key); panel.MouseEnter += (_, _) => { if (!IsPageActive(key)) panel.Background = new SolidColorBrush(Color.FromRgb(0x28, 0x28, 0x28)); }; panel.MouseLeave += (_, _) => { if (!IsPageActive(key)) panel.Background = Brushes.Transparent; }; NavPanel.Children.Add(panel); _nav.Add((key, panel)); } private bool IsPageActive(string key) => _pages.TryGetValue(key, out var page) && ReferenceEquals(PageHost.Content, page); private void ShowPage(string key) { if (!_pages.TryGetValue(key, out var page)) return; PageHost.Content = page; foreach (var (k, panel) in _nav) { var active = k == key; panel.Background = active ? new SolidColorBrush(Color.FromRgb(0x25, 0x25, 0x25)) : Brushes.Transparent; foreach (var child in panel.Children.OfType()) child.Foreground = new SolidColorBrush(active ? Color.FromRgb(0x80, 0xB9, 0xEE) : Color.FromRgb(0xC7, 0xC7, 0xC7)); } } // ---------------------------------------------------------------- 页面 private void BuildPages() { var ctx = new PageContext(this); _pages["item"] = new ItemPage(ctx); _pages["weapon"] = new WeaponPage(ctx); _pages["avatar"] = new AvatarPage(ctx); _pages["artifact"] = new ArtifactPage(ctx); _pages["generate"] = new GeneratePage(ctx); _pages["quest"] = new QuestPage(ctx); _pages["misc"] = new MiscPage(ctx); _pages["template"] = new TemplatePage(ctx); _pages["start"] = new StartPage(ctx); _pages["log"] = new LogPage(ctx); _pages["about"] = new AboutPage(ctx); _pages["settings"] = new SettingsPage(ctx); } // ---------------------------------------------------------------- 服务 public void Log(string message) { AddLog(message); if (_pages.TryGetValue("log", out var p) && p is LogPage lp) lp.Append(message); } private void AddLog(string message) => _log.Add($"{DateTime.Now:HH:mm:ss} {message}"); public void Toast(string message) => Log(message); public void RunBackground(Func work, Action? done = null) { _ = Task.Run(async () => { Exception? error = null; try { await work(); } catch (Exception ex) { error = ex; Log(ex.Message); } Dispatcher.Invoke(() => done?.Invoke(error)); }); } public void Exec(string command, string? uid = null) { if (string.IsNullOrWhiteSpace(command)) { Toast("指令为空"); return; } Log($"执行:{command}"); RunBackground(async () => { var result = await Muip.ExecAsync(command, uid); Dispatcher.Invoke(() => { Log($"→ {(result.Message.Length > 0 ? result.Message : "succ")}"); Config.Set("MUIP", "是否连通", result.Ok ? "true" : "false"); }); }); } } public sealed class PageContext { public MainWindow Window { get; } public AppConfig Config => Window.Config; public DataStore Data => Window.Data; public MuipClient Muip => Window.Muip; public SshService Ssh => Window.Ssh; public MySqlService Db => Window.Db; public PageContext(MainWindow window) => Window = window; public void Log(string message) => Window.Log(message); public void Toast(string message) => Window.Toast(message); public void Exec(string command) => Window.Exec(command); public void RunBackground(Func work, Action? done = null) => Window.RunBackground(work, done); }