449 lines
15 KiB
C#
449 lines
15 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using Avalonia;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Controls.Primitives;
|
|
using Avalonia.Input;
|
|
using Avalonia.Interactivity;
|
|
using Avalonia.Markup.Xaml;
|
|
using Avalonia.VisualTree;
|
|
using AvaloniaApp;
|
|
using ToolsForm.Manager;
|
|
using ToolsForm.Models;
|
|
using ToolsForm.ViewModels;
|
|
using ToolsForm.Views.TemporaryStorage;
|
|
|
|
namespace ToolsForm.Views;
|
|
|
|
public enum ButtonState
|
|
{
|
|
Default,
|
|
Hover,
|
|
Clicked
|
|
}
|
|
|
|
public partial class ExperimentWindow : Window
|
|
{
|
|
private ExperimentWindowViewModel ViewModel;
|
|
private bool _isHovering = false;
|
|
private bool _isClicked = false;
|
|
private ButtonState _currentState = ButtonState.Default;
|
|
|
|
public ExperimentWindow()
|
|
{
|
|
InitializeComponent();
|
|
PositionChanged += OnPositionChanged;
|
|
PointerPressed += MainWindow_PointerPressed;
|
|
PointerPressed += ExperimentWindow_PointerReleased;
|
|
Topmost = true;
|
|
|
|
ShowInTaskbar = false;
|
|
|
|
ViewModel = new ExperimentWindowViewModel();
|
|
DataContext = ViewModel; // 设置 DataContext
|
|
var experimentButton = this.FindControl<Button>("ExperimentButton");
|
|
if (experimentButton != null)
|
|
{
|
|
// experimentButton.PointerEntered += ExperimentButton_OnPointerEntered;
|
|
// experimentButton.PointerExited += ExperimentButton_OnPointerExited;
|
|
}
|
|
|
|
WindowAttachment.GetInstance().Detach();
|
|
|
|
// 控制暂存按钮状态
|
|
// var temporaryStorageButton = this.FindControl<ToggleButton>("TemporaryStorageButton5");
|
|
// if (temporaryStorageButton != null)
|
|
// {
|
|
// temporaryStorageButton.IsEnabled = WindowApi.isSubmiteBttonOpen;
|
|
// }
|
|
|
|
// 设置窗口位置为屏幕最左边,并垂直居中
|
|
var screen = Screens.Primary.WorkingArea;
|
|
var screenCenterY = (screen.Height - this.Height) / 2;
|
|
this.Position = new PixelPoint(0, (int)screenCenterY);
|
|
AppDataManager.MainType = MainType.子窗口;
|
|
|
|
//
|
|
// var ExperimentButton = this.FindControl<Button>("ExperimentButton");
|
|
// var SubmittedButton = this.FindControl<Button>("SubmittedButton");
|
|
//
|
|
// ExperimentButton.IsVisible = true;
|
|
// SubmittedButton.IsVisible = false;
|
|
//
|
|
if (AppDataManager.isUpload)
|
|
{
|
|
// var ExperimentButton = this.FindControl<Button>("ExperimentButton");
|
|
// var SubmittedButton = this.FindControl<Button>("SubmittedButton");
|
|
//
|
|
// ExperimentButton.IsVisible = false;
|
|
// SubmittedButton.IsVisible = true;
|
|
}
|
|
Console.WriteLine("----->"+WindowApi.isSubmiteBttonOpen);
|
|
|
|
if (!WindowApi.isSubmiteBttonOpen)
|
|
{
|
|
// var ExperimentButton = this.FindControl<Button>("ExperimentButton");
|
|
// var SubmittedButton = this.FindControl<Button>("SubmittedButton");
|
|
// var temporaryStorageButton = this.FindControl<ToggleButton>("TemporaryStorageButton5");
|
|
// var OverviewButton = this.FindControl<ToggleButton>("OverviewButton");
|
|
// var ReadCodeButton = this.FindControl<ToggleButton>("ReadCodeButton");
|
|
// ExperimentButton.IsVisible = false;
|
|
// SubmittedButton.IsVisible = false;
|
|
// temporaryStorageButton.IsEnabled = false;
|
|
// OverviewButton.IsEnabled = false;
|
|
// ReadCodeButton.IsEnabled = false;
|
|
}
|
|
|
|
|
|
// if (WindowApi.isSubmiteBttonOpen)
|
|
// {
|
|
// var ExperimentButton1 = this.FindControl<Button>("ExperimentButton");
|
|
// var SubmittedButton1 = this.FindControl<Button>("SubmittedButton");
|
|
// ExperimentButton.IsVisible = true;
|
|
// SubmittedButton.IsVisible = true;
|
|
// }
|
|
// else
|
|
// {
|
|
// var ExperimentButton = this.FindControl<Button>("ExperimentButton");
|
|
// var SubmittedButton = this.FindControl<Button>("SubmittedButton");
|
|
// ExperimentButton.IsVisible = false;
|
|
// SubmittedButton.IsVisible = false;
|
|
// }
|
|
}
|
|
|
|
private void MainWindow_PointerPressed(object? sender, PointerPressedEventArgs e)
|
|
{
|
|
if (e.Pointer.Type == PointerType.Mouse) BeginMoveDrag(e);
|
|
}
|
|
|
|
private void ExperimentWindow_PointerReleased(object? sender, PointerPressedEventArgs e)
|
|
{
|
|
// 在释放鼠标时,检查并调整窗口位置
|
|
var screen = Screens.Primary.WorkingArea;
|
|
if (this.Position.X < screen.X)
|
|
{
|
|
// 如果窗口超出了左边界,将其调整到左边界
|
|
this.Position = new PixelPoint(screen.X, this.Position.Y);
|
|
}
|
|
}
|
|
|
|
private void OnPositionChanged(object? sender, PixelPointEventArgs pixelPointEventArgs)
|
|
{
|
|
AppWindowManager.UpdateWindowPosition(AppWindowManager.OverviewWindow, 0, 0, MainType.子窗口);
|
|
AppWindowManager.UpdateWindowPosition(AppWindowManager.IdeWindow, 0, 0, MainType.子窗口);
|
|
AppWindowManager.UpdateWindowPosition(AppWindowManager.ResetSceneWindow, 0, 0, MainType.子窗口);
|
|
AppWindowManager.UpdateWindowPosition(AppWindowManager.TemporaryStorageWindow, 0, 0, MainType.子窗口);
|
|
AppWindowManager.UpdateWindowPosition(AppWindowManager.TemporaryStorageMessage, 0, 300, MainType.子窗口);
|
|
AppWindowManager.UpdateWindowPosition(AppWindowManager.DownCodeWindow, 0, 0, MainType.子窗口);
|
|
AppWindowManager.UpdateWindowPosition(AppWindowManager.ReadCodeWindow, 0, 0, MainType.子窗口);
|
|
AppWindowManager.UpdateWindowPosition(AppWindowManager.UploadExperimentWindow, 0, 0, MainType.子窗口);
|
|
// AppWindowManager.UpdateWindowPosition(AppWindowManager.AiWindow, 0, 0, MainType.子窗口);
|
|
}
|
|
|
|
// 打开总览窗口
|
|
public void OpenWindowToggleButton_Overview(object sender, RoutedEventArgs e)
|
|
{
|
|
ResetOtherButtons("Overview");
|
|
|
|
AppWindowManager.ToggleWindow(ref AppWindowManager.OverviewWindow, () => new OverviewWindow(), true, true, MainType.子窗口);
|
|
|
|
|
|
// if (aiwindow == null)
|
|
// {
|
|
// aiwindow = new AIWindow();
|
|
// aiwindow.Show();
|
|
// var screen = Screens.All.FirstOrDefault();
|
|
//
|
|
// // 获取屏幕的工作区域
|
|
// var screenBounds = screen.WorkingArea;
|
|
//
|
|
// if (aiwindow != null)
|
|
// {
|
|
// // X 坐标保持水平居中
|
|
// var centerX = (screenBounds.Width - aiwindow.Width) / 2 + screenBounds.X;
|
|
// // Y 坐标贴近可用工作区域的底部
|
|
// var bottomY = screenBounds.Y + screenBounds.Height - aiwindow.Height;
|
|
//
|
|
// aiwindow.Position = new PixelPoint((int)centerX, (int)bottomY);
|
|
// }
|
|
// }
|
|
// else
|
|
// {
|
|
// if (!aiwindow.IsVisible)
|
|
// {
|
|
// aiwindow.Show();
|
|
// var screen = Screens.All.FirstOrDefault();
|
|
//
|
|
// // 获取屏幕的工作区域
|
|
// var screenBounds = screen.WorkingArea;
|
|
//
|
|
// if (aiwindow != null)
|
|
// {
|
|
// // X 坐标保持水平居中
|
|
// var centerX = (screenBounds.Width - aiwindow.Width) / 2 + screenBounds.X;
|
|
// // Y 坐标贴近可用工作区域的底部
|
|
// var bottomY = screenBounds.Y + screenBounds.Height - aiwindow.Height;
|
|
//
|
|
// aiwindow.Position = new PixelPoint((int)centerX, (int)bottomY);
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// var screen = Screens.All.FirstOrDefault();
|
|
//
|
|
// // 获取屏幕的工作区域
|
|
// var screenBounds = screen.WorkingArea;
|
|
//
|
|
// if (AppWindowManager.OverviewWindow != null)
|
|
// {
|
|
// // 计算窗口居中的位置
|
|
// var centerX = (screenBounds.Width - AppWindowManager.OverviewWindow.Width) / 2 + screenBounds.X;
|
|
// var centerY = (screenBounds.Height - AppWindowManager.OverviewWindow.Height) / 2 + screenBounds.Y;
|
|
//
|
|
//
|
|
// // 设置窗口的屏幕中心位置
|
|
// AppWindowManager.OverviewWindow.Position = new PixelPoint((int)centerX, (int)centerY);
|
|
// // AppWindowManager.MainWindow.Hide();
|
|
//
|
|
// // AppWindowManager.UploadExperimentMessageWindow.Position = new PixelPoint((int)p.X - 226, (int)(p.Y - p.Y / 2));
|
|
// }
|
|
}
|
|
private Window aiwindow;
|
|
|
|
// 打开 IDE 窗口
|
|
public void AIWindowToggleButton_AI(object sender, RoutedEventArgs e)
|
|
{
|
|
ResetOtherButtons("AI");
|
|
|
|
if (aiwindow == null)
|
|
{
|
|
aiwindow = new AIWindow();
|
|
aiwindow.Show();
|
|
}
|
|
else
|
|
{
|
|
if (aiwindow.IsVisible)
|
|
{
|
|
aiwindow.Hide();
|
|
}
|
|
else
|
|
{
|
|
aiwindow.Show();
|
|
}
|
|
}
|
|
|
|
|
|
var screen = Screens.All.FirstOrDefault();
|
|
|
|
// 获取屏幕的工作区域
|
|
var screenBounds = screen.WorkingArea;
|
|
|
|
if (aiwindow != null)
|
|
{
|
|
// X 坐标保持水平居中
|
|
var centerX = (screenBounds.Width - aiwindow.Width) / 2 + screenBounds.X;
|
|
// Y 坐标贴近可用工作区域的底部
|
|
var bottomY = screenBounds.Y + screenBounds.Height - aiwindow.Height;
|
|
|
|
aiwindow.Position = new PixelPoint((int)centerX, (int)bottomY);
|
|
}
|
|
}
|
|
|
|
|
|
// 打开重置窗口
|
|
private void APIWindowButton_OnClick(object? sender, RoutedEventArgs e)
|
|
{
|
|
ResetOtherButtons("ResetScene");
|
|
// AppWindowManager.ToggleWindow(ref AppWindowManager.ResetSceneWindow, () => new ResetSceneWindow(), true);
|
|
try
|
|
{
|
|
var processStartInfo = new ProcessStartInfo(APIInterface.url)
|
|
{
|
|
UseShellExecute = true // 使用系统默认的浏览器
|
|
};
|
|
Process.Start(processStartInfo);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// 处理异常,例如输出错误信息
|
|
Console.WriteLine($"无法打开链接: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
// 打开暂存窗口
|
|
private void TemporaryStorageButton5_OnClick(object? sender, RoutedEventArgs e)
|
|
{
|
|
ResetOtherButtons("TemporaryStorage");
|
|
|
|
AppWindowManager.ToggleWindow(ref AppWindowManager.TemporaryStorageWindow, () => new TemporaryStorageWindow(), true, true, MainType.子窗口);
|
|
}
|
|
|
|
|
|
// 打开重置窗口
|
|
private void RecordingWindowButton_OnClick(object? sender, RoutedEventArgs e)
|
|
{
|
|
ResetOtherButtons("Recording");
|
|
|
|
AppWindowManager.ToggleWindow(ref AppWindowManager.RecordingWindow, () => new RecordingWindow(), true, true, MainType.子窗口);
|
|
}
|
|
|
|
|
|
// 读取窗口
|
|
private void ReadCodeButton6_OnClick(object? sender, RoutedEventArgs e)
|
|
{
|
|
ResetOtherButtons("ReadCode");
|
|
AppWindowManager.ToggleWindow(ref AppWindowManager.ReadCodeWindow, () => new ReadCodeWindow(), true, true, MainType.子窗口);
|
|
}
|
|
|
|
//切换
|
|
private void StartExperimentButton_OnClick(object? sender, RoutedEventArgs e)
|
|
{
|
|
if (aiwindow != null)
|
|
{
|
|
aiwindow.Hide();
|
|
}
|
|
|
|
|
|
AppWindowManager.ToggleWindow(ref AppWindowManager.ExperimentWindow2, () => new ExperimentWindow2(), false, false);
|
|
// var mainWindowPosition = Position;
|
|
// AppWindowManager.ExperimentWindow2.Position = new PixelPoint(mainWindowPosition.X, mainWindowPosition.Y);
|
|
|
|
AppWindowManager.CloseWindow(ref AppWindowManager.ExperimentWindow);
|
|
|
|
|
|
WindowSwitcher.GetInstance().SwitchWindow("unity");
|
|
|
|
// AppWindowManager.ExperimentWindow.Cancal();
|
|
|
|
WebSocketManager.GetInstance().SendMessage(ToolsEventEnum.HuiFu, null);
|
|
|
|
// AppDataManager.BringExeToFront(ChangeWindowEnum.unity);
|
|
}
|
|
//开始实验
|
|
|
|
private void ExperimentButton_OnPointerEntered(object? sender, PointerEventArgs e)
|
|
{
|
|
if (!_isClicked && !_isHovering)
|
|
{
|
|
_isHovering = true; // 设置悬浮状态
|
|
UpdateButtonImages((Button)sender, ButtonState.Hover);
|
|
}
|
|
}
|
|
|
|
private void ExperimentButton_OnPointerExited(object? sender, PointerEventArgs e)
|
|
{
|
|
if (_isHovering)
|
|
{
|
|
_isHovering = false; // 重置悬浮状态
|
|
UpdateButtonImages((Button)sender, ButtonState.Default);
|
|
}
|
|
}
|
|
|
|
private void UploadButton_OnClick(object? sender, RoutedEventArgs e)
|
|
{
|
|
// 当按钮被点击时,设置点击状态,并禁用悬浮状态
|
|
AppWindowManager.ToggleWindow(ref AppWindowManager.UploadExperimentWindow, () => new UploadExperimentWindow(), true, true, MainType.子窗口);
|
|
}
|
|
|
|
private void UpdateButtonImages(Button button, ButtonState state)
|
|
{
|
|
var images = new Dictionary<string, Image>();
|
|
FindImagesInVisualTree(button, images);
|
|
|
|
if (!images.TryGetValue("DefaultImage", out var defaultImage) ||
|
|
!images.TryGetValue("HoverImage", out var hoverImage) ||
|
|
!images.TryGetValue("ClickedImage", out var clickedImage))
|
|
{
|
|
Console.WriteLine("One or more images are null!");
|
|
return;
|
|
}
|
|
|
|
// 根据状态更新图像可见性
|
|
switch (state)
|
|
{
|
|
case ButtonState.Clicked:
|
|
defaultImage.IsVisible = false;
|
|
hoverImage.IsVisible = false;
|
|
clickedImage.IsVisible = true; // 显示点击状态图像
|
|
break;
|
|
|
|
case ButtonState.Hover:
|
|
if (!_isClicked) // 只有在未点击时才允许悬浮状态
|
|
{
|
|
defaultImage.IsVisible = false;
|
|
hoverImage.IsVisible = true; // 显示悬浮状态图像
|
|
clickedImage.IsVisible = false;
|
|
}
|
|
|
|
break;
|
|
|
|
case ButtonState.Default:
|
|
default:
|
|
defaultImage.IsVisible = true; // 显示默认状态图像
|
|
hoverImage.IsVisible = false;
|
|
clickedImage.IsVisible = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void FindImagesInVisualTree(Visual visual, Dictionary<string, Image> images)
|
|
{
|
|
if (visual == null) return;
|
|
|
|
foreach (var child in visual.GetVisualChildren())
|
|
{
|
|
if (child is Image img)
|
|
{
|
|
images[img.Name] = img; // 将图像添加到字典中
|
|
Console.WriteLine($"找到图像控件:{img.Name}");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine($"未找到图像控件,类型为:{child.GetType()}");
|
|
// 继续递归查找子控件
|
|
FindImagesInVisualTree(child, images);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 重置其他 ToggleButton 的状态
|
|
private void ResetOtherButtons(string excludeButton)
|
|
{
|
|
if (excludeButton != "Overview") ViewModel.IsButton1Checked = false;
|
|
if (excludeButton != "AI") ViewModel.IsButton2Checked = false;
|
|
if (excludeButton != "ResetScene") ViewModel.IsButton3Checked = false;
|
|
if (excludeButton != "Recording") ViewModel.IsButton4Checked = false;
|
|
if (excludeButton != "TemporaryStorage") ViewModel.IsButton5Checked = false;
|
|
if (excludeButton != "ReadCode") ViewModel.IsButton6Checked = false;
|
|
if (excludeButton != "StartExperiment") ViewModel.IsButton7Checked = false;
|
|
}
|
|
private bool isMiniUnity = false;
|
|
|
|
private void MiniModelButton_OnClick(object? sender, RoutedEventArgs e)
|
|
{
|
|
WindowSwitcher.GetInstance().BringUnityWindowToFront();
|
|
|
|
WebSocketManager.GetInstance().SendMessage(ToolsEventEnum.Thumbnail, null);
|
|
}
|
|
|
|
public void Submit()
|
|
{
|
|
var ExperimentButton = this.FindControl<Button>("ExperimentButton");
|
|
var SubmittedButton = this.FindControl<Button>("SubmittedButton");
|
|
|
|
ExperimentButton.IsVisible = false;
|
|
SubmittedButton.IsVisible = true;
|
|
}
|
|
|
|
// public void Cancal()
|
|
// {
|
|
// var ExperimentButton = this.FindControl<Button>("ExperimentButton");
|
|
// var SubmittedButton = this.FindControl<Button>("SubmittedButton");
|
|
//
|
|
// ExperimentButton.IsVisible = true;
|
|
// SubmittedButton.IsVisible = false;
|
|
// }
|
|
//
|
|
} |