Skip to content

Commit

Permalink
Merge pull request #72 from Jack15678/dev
Browse files Browse the repository at this point in the history
调试界面v1
  • Loading branch information
Ador-N authored Dec 24, 2024
2 parents 5fd9246 + 5a3e817 commit c12fd83
Show file tree
Hide file tree
Showing 17 changed files with 735 additions and 0 deletions.
File renamed without changes.
20 changes: 20 additions & 0 deletions interface/AvaloniaUI_debug_interface/debug_interface/App.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!-- App.axaml -->

<Application
x:Class="debug_interface.App"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:debug_interface"
xmlns:views="using:YourAppNamespace.Views"
RequestedThemeVariant="Light">
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->

<Application.DataTemplates>
<local:ViewLocator />
</Application.DataTemplates>

<Application.Styles>

<FluentTheme />
</Application.Styles>
</Application>
80 changes: 80 additions & 0 deletions interface/AvaloniaUI_debug_interface/debug_interface/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Data.Core;
using Avalonia.Data.Core.Plugins;
using Avalonia.Markup.Xaml;
using debug_interface.ViewModels;
using debug_interface.Views;
using System.Linq;

namespace debug_interface
{
/// <summary>
/// 应用程序的后置代码,负责初始化和启动主窗口。
/// </summary>
public partial class App : Application
{
//GPT GENERATED
//public override void Initialize()
//{
// AvaloniaXamlLoader.Load(this);
//}

//public override void OnFrameworkInitializationCompleted()
//{
// if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
// {
// // 创建主窗口的ViewModel实例
// var mainWindowViewModel = new MainWindowViewModel();

// // 设置主窗口及其数据上下文
// desktop.MainWindow = new MainWindow
// {
// DataContext = mainWindowViewModel
// };
// }

// base.OnFrameworkInitializationCompleted();
//}


public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}

public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
// Avoid duplicate validations from both Avalonia and the CommunityToolkit.
// More info: https://docs.avaloniaui.net/docs/guides/development-guides/data-validation#manage-validationplugins
DisableAvaloniaDataAnnotationValidation();

var mainWindowViewModel = new MainWindowViewModel();

desktop.MainWindow = new MainWindow
{
DataContext = new MainWindowViewModel(),
};
}

base.OnFrameworkInitializationCompleted();
}

private void DisableAvaloniaDataAnnotationValidation()
{
// Get an array of plugins to remove
var dataValidationPluginsToRemove =
BindingPlugins.DataValidators.OfType<DataAnnotationsValidationPlugin>().ToArray();

// remove each entry found
foreach (var plugin in dataValidationPluginsToRemove)
{
BindingPlugins.DataValidators.Remove(plugin);
}
}


}
}
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions interface/AvaloniaUI_debug_interface/debug_interface/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Avalonia;
using System;

namespace debug_interface
{
internal sealed class Program
{
// Initialization code. Don't use any Avalonia, third-party APIs or any
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
[STAThread]
public static void Main(string[] args) => BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);

// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.WithInterFont()
.LogToTrace();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Avalonia.Controls;
using Avalonia.Controls.Templates;
using debug_interface.ViewModels;
using System;

namespace debug_interface
{
public class ViewLocator : IDataTemplate
{

public Control? Build(object? param)
{
if (param is null)
return null;

var name = param.GetType().FullName!.Replace("ViewModel", "View", StringComparison.Ordinal);
var type = Type.GetType(name);

if (type != null)
{
return (Control)Activator.CreateInstance(type)!;
}

return new TextBlock { Text = "Not Found: " + name };
}

public bool Match(object? data)
{
return data is ViewModelBase;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace debug_interface.ViewModels
{
public partial class CharacterViewModel : ViewModelBase
{
[ObservableProperty]
private string name = "角色"; // 默认角色名,可由服务器更新

[ObservableProperty]
private int hp = 1000; // 血量,可由服务器动态更新

// 主动状态(单一)
// 可能值: "空置","开采","攻击","释放技能","建造","移动"
[ObservableProperty]
private string activeState = "空置";
//public string ActiveState
//{
// get => activeState;
// set
// {
// if (value == activeState) return;
// activeState = value;
// OnPropertyChanged(nameof(DisplayStates));
// }
//}
// 被动状态(可叠加)
// 可能值: "致盲","击退","定身","隐身" 等,可由服务器控制增减
public ObservableCollection<string> PassiveStates { get; }

// 装备清单:用名称+数量表示。如 {"小血瓶":2, "鞋子":1, "大护盾":1, "净化药水":3}
// 为了方便绑定,用 ObservableCollection 来存储装备条目,每个条目包含Name和Count
public ObservableCollection<EquipmentItem> EquipmentInventory { get; }

// 状态选项列表和装备选项列表已不再使用ComboBox选择,而是纯展示。
// 如果需要仍保留可由服务器更新,但这里不会再用于交互。

// 构造函数
public CharacterViewModel()
{
PassiveStates = new ObservableCollection<string>();
EquipmentInventory = new ObservableCollection<EquipmentItem>();
}

// 用于UI展示状态字符串
// 格式: (主动:xx 被动:xx yy zz)
public string DisplayStates
{
get
{
var sb = new StringBuilder();
sb.Append("(主动:").Append(ActiveState).Append(" 被动:");
if (PassiveStates.Count > 0)
sb.Append(string.Join(" ", PassiveStates));
else
sb.Append("无");
sb.Append(")");
return sb.ToString();
}
}

// 用于UI展示装备字符串
// 格式: 装备: 小血瓶x2 鞋子x1 大护盾x1 净化药水x3
public string DisplayEquipments
{
get
{
if (EquipmentInventory.Count == 0)
{
return "装备:无";
}
var parts = EquipmentInventory.Select(e => $"{e.Name}x{e.Count}");
return "装备:" + string.Join(" ", parts);
}
}

// 当ActiveState或PassiveStates变化后,通知UI更新 DisplayStates
partial void OnActiveStateChanged(string oldValue, string newValue)
{
OnPropertyChanged(nameof(DisplayStates));
}

// 如果被动状态列表更新,需要调用OnPropertyChanged(nameof(DisplayStates))
// 可以在服务器更新逻辑中调用。

// 同理,当EquipmentInventory变化时,需要更新DisplayEquipments
// 可以在添加/移除装备后调用OnPropertyChanged(nameof(DisplayEquipments))
}

// 定义装备类
public class EquipmentItem
{
public string Name { get; set; }
public int Count { get; set; }
public EquipmentItem(string name, int count)
{
Name = name;
Count = count;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// ViewModels/MainWindowViewModel.cs
using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;
using System;
using System.Timers;

namespace debug_interface.ViewModels
{
public partial class MainWindowViewModel : ViewModelBase
{
public ObservableCollection<CharacterViewModel> RedTeamCharacters { get; }
public ObservableCollection<CharacterViewModel> BlueTeamCharacters { get; }

[ObservableProperty]
private string someBuildingInfo = "红方建筑信息...";

[ObservableProperty]
private string anotherBuildingInfo = "蓝方建筑信息...";

[ObservableProperty]
private string currentTime = DateTime.Now.ToString("HH:mm:ss");

[ObservableProperty]
private int redScore = 0;

[ObservableProperty]
private int blueScore = 0;

[ObservableProperty]
private bool isBlueView = true;

public bool IsRedView
{
get => !IsBlueView;
set => IsBlueView = !value;
}

[ObservableProperty]
private string gameLog = "地图...";

private Timer _timer;

public MainWindowViewModel()
{
RedTeamCharacters = new ObservableCollection<CharacterViewModel>();
BlueTeamCharacters = new ObservableCollection<CharacterViewModel>();

// 初始化角色,给每个角色不同的数据以示例可变性
for (int i = 0; i < 6; i++)
{
var redChar = new CharacterViewModel()
{
Name = "红方角色" + (i + 1),
Hp = 1000 * (i + 1),
ActiveState = i % 2 == 0 ? "攻击" : "移动",
};
redChar.PassiveStates.Add("致盲");
if (i % 3 == 0) redChar.PassiveStates.Add("定身");
redChar.EquipmentInventory.Add(new EquipmentItem("小血瓶", 2));
if (i % 2 == 0)
redChar.EquipmentInventory.Add(new EquipmentItem("大护盾", 1));

RedTeamCharacters.Add(redChar);

var blueChar = new CharacterViewModel()
{
Name = "蓝方角色" + (i + 1),
Hp = 1500 + i * 500,
ActiveState = "空置",
};
blueChar.PassiveStates.Add("隐身");
//blueChar.PassiveStates.Remove("致盲");
blueChar.EquipmentInventory.Add(new EquipmentItem("净化药水", 3));
if (i % 2 == 1)
blueChar.EquipmentInventory.Add(new EquipmentItem("鞋子", 1));

BlueTeamCharacters.Add(blueChar);
}

_timer = new Timer(1000);
_timer.Elapsed += Timer_Elapsed;
_timer.Start();
}

private void Timer_Elapsed(object? sender, ElapsedEventArgs e)
{
CurrentTime = DateTime.Now.ToString("HH:mm:ss");
Avalonia.Threading.Dispatcher.UIThread.Post(() =>
{
OnPropertyChanged(nameof(CurrentTime));
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using CommunityToolkit.Mvvm.ComponentModel;

namespace debug_interface.ViewModels
{
public class ViewModelBase : ObservableObject
{
}
}
Loading

0 comments on commit c12fd83

Please sign in to comment.