VPet/VPet-Simulator.Core/Display/ToolBar.xaml.cs

450 lines
15 KiB
C#
Raw Normal View History

2022-12-13 07:10:18 +00:00
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Timers;
using Timer = System.Timers.Timer;
2023-01-08 02:59:54 +00:00
using Panuon.WPF.UI;
using System.Windows.Threading;
2023-06-08 11:44:41 +00:00
using LinePutScript;
2023-07-01 07:46:08 +00:00
using LinePutScript.Localization.WPF;
2023-07-16 23:58:09 +00:00
using static VPet_Simulator.Core.GraphInfo;
using System.Collections.Generic;
using static VPet_Simulator.Core.GraphHelper;
using System.ComponentModel;
using System.Reflection;
2023-08-24 19:37:55 +00:00
using static VPet_Simulator.Core.Main;
2022-12-13 07:10:18 +00:00
namespace VPet_Simulator.Core
{
/// <summary>
/// ToolBar.xaml 的交互逻辑
/// </summary>
2023-01-20 17:01:10 +00:00
public partial class ToolBar : UserControl, IDisposable
2022-12-13 07:10:18 +00:00
{
Main m;
2023-05-19 08:17:51 +00:00
public Timer CloseTimer;
2022-12-13 07:10:18 +00:00
bool onFocus = false;
2023-01-20 17:01:10 +00:00
Timer closePanelTimer;
2023-01-08 02:59:54 +00:00
2022-12-13 07:10:18 +00:00
public ToolBar(Main m)
{
InitializeComponent();
this.m = m;
2023-04-04 09:32:53 +00:00
CloseTimer = new Timer()
2022-12-13 07:10:18 +00:00
{
Interval = 4000,
AutoReset = false,
Enabled = false
};
2023-04-04 09:32:53 +00:00
CloseTimer.Elapsed += Closetimer_Elapsed;
2023-01-20 17:01:10 +00:00
closePanelTimer = new Timer();
closePanelTimer.Elapsed += ClosePanelTimer_Tick;
2023-01-08 16:57:10 +00:00
m.TimeUIHandle += M_TimeUIHandle;
2023-07-17 15:38:06 +00:00
LoadWork();
2023-07-16 23:58:09 +00:00
}
public void LoadWork()
{
MenuWork.Click -= MenuWork_Click;
MenuWork.Visibility = Visibility.Visible;
MenuStudy.Click -= MenuStudy_Click;
MenuStudy.Visibility = Visibility.Visible;
2023-08-18 10:02:25 +00:00
MenuPlay.Click -= MenuPlay_Click;
2023-07-16 23:58:09 +00:00
MenuWork.Items.Clear();
MenuStudy.Items.Clear();
2023-08-18 10:02:25 +00:00
MenuPlay.Items.Clear();
2023-07-16 23:58:09 +00:00
List<Work> ws = new List<Work>();
List<Work> ss = new List<Work>();
2023-08-18 10:02:25 +00:00
List<Work> ps = new List<Work>();
2023-07-16 23:58:09 +00:00
foreach (var w in m.Core.Graph.GraphConfig.Works)
{
2023-08-18 10:02:25 +00:00
switch (w.Type)
2023-07-16 23:58:09 +00:00
{
2023-08-18 10:02:25 +00:00
case Work.WorkType.Study:
ss.Add(w);
break;
case Work.WorkType.Work:
ws.Add(w);
break;
case Work.WorkType.Play:
ps.Add(w);
break;
2023-08-24 19:37:55 +00:00
}
2023-07-16 23:58:09 +00:00
}
if (ws.Count == 0)
{
MenuWork.Visibility = Visibility.Collapsed;
}
else if (ws.Count == 1)
{
MenuWork.Click += MenuWork_Click;
wwork = ws[0];
MenuWork.Header = ws[0].NameTrans;
}
else
{
foreach (var w in ws)
{
var mi = new MenuItem()
{
Header = w.NameTrans
};
mi.Click += (s, e) => StartWork(w);
MenuWork.Items.Add(mi);
}
}
if (ss.Count == 0)
{
MenuStudy.Visibility = Visibility.Collapsed;
}
else if (ss.Count == 1)
{
MenuStudy.Click += MenuStudy_Click;
wstudy = ss[0];
MenuStudy.Header = ss[0].NameTrans;
}
else
{
foreach (var w in ss)
{
var mi = new MenuItem()
{
Header = w.NameTrans
};
mi.Click += (s, e) => StartWork(w);
MenuStudy.Items.Add(mi);
}
}
2023-08-18 10:02:25 +00:00
if (ps.Count == 0)
{
MenuPlay.Visibility = Visibility.Collapsed;
}
else if (ps.Count == 1)
{
MenuPlay.Click += MenuPlay_Click;
wplay = ps[0];
MenuPlay.Header = ps[0].NameTrans;
}
else
{
foreach (var w in ps)
{
var mi = new MenuItem()
{
Header = w.NameTrans
};
mi.Click += (s, e) => StartWork(w);
MenuPlay.Items.Add(mi);
}
}
2023-07-16 23:58:09 +00:00
}
private void MenuStudy_Click(object sender, RoutedEventArgs e)
{
StartWork(wstudy);
}
Work wwork;
Work wstudy;
2023-08-18 10:02:25 +00:00
Work wplay;
2023-08-25 21:23:30 +00:00
public Func<Work, bool> WorkCheck;
2023-07-16 23:58:09 +00:00
public void StartWork(Work work)
{
if (!m.Core.Controller.EnableFunction || m.Core.Save.Mode != GameSave.ModeType.Ill)
if (!m.Core.Controller.EnableFunction || m.Core.Save.Level >= work.LevelLimit)
if (m.State == Main.WorkingState.Work && m.StateID == m.Core.Graph.GraphConfig.Works.IndexOf(work))
m.WorkTimer.Stop();
2023-08-25 21:23:30 +00:00
else
{
if (WorkCheck != null && !WorkCheck.Invoke(work))
return;
m.WorkTimer.Start(work);
}
2023-07-16 23:58:09 +00:00
else
MessageBoxX.Show(LocalizeCore.Translate("您的桌宠等级不足{0}/{2}\n无法进行{1}", m.Core.Save.Level.ToString()
, work.NameTrans, work.LevelLimit), LocalizeCore.Translate("{0}取消", work.NameTrans));
else
MessageBoxX.Show(LocalizeCore.Translate("您的桌宠 {0} 生病啦,没法进行{1}", m.Core.Save.Name,
work.NameTrans), LocalizeCore.Translate("{0}取消", work.NameTrans));
2023-07-17 15:38:06 +00:00
Visibility = Visibility.Collapsed;
2023-07-16 23:58:09 +00:00
}
private void MenuWork_Click(object sender, RoutedEventArgs e)
{
StartWork(wwork);
2023-01-08 16:57:10 +00:00
}
2023-08-18 10:02:25 +00:00
private void MenuPlay_Click(object sender, RoutedEventArgs e)
{
StartWork(wplay);
}
2023-07-16 23:58:09 +00:00
2023-01-08 16:57:10 +00:00
private void M_TimeUIHandle(Main m)
{
if (BdrPanel.Visibility == Visibility.Visible)
{
Tlv.Text = "Lv " + m.Core.Save.Level.ToString();
tMoney.Text = "$ " + m.Core.Save.Money.ToString("N2");
2023-01-24 15:47:16 +00:00
if (m.Core.Controller.EnableFunction)
{
2023-05-09 23:16:58 +00:00
till.Visibility = m.Core.Save.Mode == GameSave.ModeType.Ill ? Visibility.Visible : Visibility.Collapsed;
2023-01-24 15:47:16 +00:00
tfun.Visibility = Visibility.Collapsed;
}
else
{
till.Visibility = Visibility.Collapsed;
tfun.Visibility = Visibility.Visible;
2023-02-01 14:16:43 +00:00
}
2023-09-13 10:06:43 +00:00
var max = m.Core.Save.LevelUpNeed();
if (max > pExp.Minimum)
{
pExp.Maximum = max;
}
2023-06-14 11:13:52 +00:00
if (m.Core.Save.Exp < 0)
{
pExp.Minimum = m.Core.Save.Exp;
}
else
{
2023-09-13 10:06:43 +00:00
var bl = m.Core.Save.Level - 2;
if (bl <= 0)
pExp.Minimum = 0;
else
pExp.Minimum = (int)(Math.Pow((bl) * 10, 2));
}
if (max < pExp.Minimum)
{
pExp.Maximum = max;
2023-06-14 11:13:52 +00:00
}
2023-09-13 10:06:43 +00:00
2023-01-08 16:57:10 +00:00
pExp.Value = m.Core.Save.Exp;
2023-06-14 11:13:52 +00:00
2023-01-08 16:57:10 +00:00
pStrength.Value = m.Core.Save.Strength;
pFeeling.Value = m.Core.Save.Feeling;
pStrengthFood.Value = m.Core.Save.StrengthFood;
pStrengthDrink.Value = m.Core.Save.StrengthDrink;
2023-07-24 01:53:55 +00:00
pStrengthFoodMax.Value = Math.Min(100, m.Core.Save.StrengthFood + m.Core.Save.StoreStrengthFood);
pStrengthDrinkMax.Value = Math.Min(100, m.Core.Save.StrengthDrink + m.Core.Save.StoreStrengthDrink);
2023-06-10 10:05:01 +00:00
if (Math.Abs(m.Core.Save.ChangeStrength) > 1)
2023-06-08 21:06:46 +00:00
tStrength.Text = $"{m.Core.Save.ChangeStrength:f1}/t";
else
tStrength.Text = $"{m.Core.Save.ChangeStrength:f2}/t";
2023-06-10 10:05:01 +00:00
if (Math.Abs(m.Core.Save.ChangeFeeling) > 1)
2023-06-08 21:06:46 +00:00
tFeeling.Text = $"{m.Core.Save.ChangeFeeling:f1}/t";
else
tFeeling.Text = $"{m.Core.Save.ChangeFeeling:f2}/t";
2023-06-10 10:05:01 +00:00
if (Math.Abs(m.Core.Save.ChangeStrengthDrink) > 1)
2023-06-08 21:06:46 +00:00
tStrengthDrink.Text = $"{m.Core.Save.ChangeStrengthDrink:f1}/t";
else
tStrengthDrink.Text = $"{m.Core.Save.ChangeStrengthDrink:f2}/t";
2023-06-10 10:05:01 +00:00
if (Math.Abs(m.Core.Save.ChangeStrengthFood) > 1)
2023-06-08 21:06:46 +00:00
tStrengthFood.Text = $"{m.Core.Save.ChangeStrengthFood:f1}/t";
else
tStrengthFood.Text = $"{m.Core.Save.ChangeStrengthFood:f2}/t";
2023-07-24 01:53:55 +00:00
2023-01-08 16:57:10 +00:00
}
2023-01-08 02:59:54 +00:00
}
private void ClosePanelTimer_Tick(object sender, EventArgs e)
{
2023-01-20 17:01:10 +00:00
Dispatcher.Invoke(() =>
2023-01-08 02:59:54 +00:00
{
2023-01-20 17:01:10 +00:00
if (BdrPanel.IsMouseOver
|| MenuPanel.IsMouseOver)
{
closePanelTimer.Stop();
return;
}
BdrPanel.Visibility = Visibility.Collapsed;
});
2022-12-13 07:10:18 +00:00
}
private void Closetimer_Elapsed(object sender, ElapsedEventArgs e)
{
if (onFocus)
{
onFocus = false;
2023-04-04 09:32:53 +00:00
CloseTimer.Start();
2022-12-13 07:10:18 +00:00
}
else
Dispatcher.Invoke(() => this.Visibility = Visibility.Collapsed);
}
2023-08-22 09:52:25 +00:00
/// <summary>
/// ToolBar显示事件
/// </summary>
public event Action EventShow;
2022-12-13 07:10:18 +00:00
public void Show()
{
2023-02-01 14:16:43 +00:00
if (m.UIGrid.Children.IndexOf(this) != m.UIGrid.Children.Count - 1)
{
2023-04-10 09:16:15 +00:00
Panel.SetZIndex(this, m.UIGrid.Children.Count);
2023-02-01 14:16:43 +00:00
}
2022-12-13 07:10:18 +00:00
Visibility = Visibility.Visible;
2023-08-22 09:52:25 +00:00
EventShow?.Invoke();
2023-04-04 09:32:53 +00:00
if (CloseTimer.Enabled)
2022-12-13 07:10:18 +00:00
onFocus = true;
else
2023-04-04 09:32:53 +00:00
CloseTimer.Start();
2022-12-13 07:10:18 +00:00
}
private void UserControl_MouseEnter(object sender, MouseEventArgs e)
{
2023-04-04 09:32:53 +00:00
CloseTimer.Enabled = false;
2022-12-13 07:10:18 +00:00
}
private void UserControl_MouseLeave(object sender, MouseEventArgs e)
{
2023-04-04 09:32:53 +00:00
CloseTimer.Start();
2022-12-13 07:10:18 +00:00
}
2022-12-28 10:03:47 +00:00
2023-01-08 02:59:54 +00:00
private void MenuPanel_Click(object sender, RoutedEventArgs e)
{
m.Core.Controller.ShowPanel();
}
2023-01-11 13:10:18 +00:00
/// <summary>
/// 窗口类型
/// </summary>
public enum MenuType
{
/// <summary>
/// 投喂
/// </summary>
Feed,
/// <summary>
/// 互动
/// </summary>
Interact,
/// <summary>
/// 自定
/// </summary>
DIY,
2023-01-23 17:31:16 +00:00
/// <summary>
/// 设置
/// </summary>
Setting,
2023-01-11 13:10:18 +00:00
}
2023-02-27 12:29:40 +00:00
/// <summary>
/// 添加按钮
/// </summary>
/// <param name="parentMenu">按钮位置</param>
/// <param name="displayName">显示名称</param>
/// <param name="clickCallback">功能</param>
2023-01-11 13:10:18 +00:00
public void AddMenuButton(MenuType parentMenu,
2023-01-03 04:21:54 +00:00
string displayName,
Action clickCallback)
{
var menuItem = new MenuItem()
{
Header = displayName,
2023-01-11 13:10:18 +00:00
HorizontalContentAlignment = HorizontalAlignment.Center
2023-01-03 04:21:54 +00:00
};
menuItem.Click += delegate
{
clickCallback?.Invoke();
};
switch (parentMenu)
{
2023-01-11 13:10:18 +00:00
case MenuType.Feed:
2023-01-03 04:21:54 +00:00
MenuFeed.Items.Add(menuItem);
break;
2023-01-11 13:10:18 +00:00
case MenuType.Interact:
MenuInteract.Items.Add(menuItem);
break;
case MenuType.DIY:
MenuDIY.Items.Add(menuItem);
break;
2023-01-23 17:31:16 +00:00
case MenuType.Setting:
MenuSetting.Items.Add(menuItem);
break;
2023-01-03 04:21:54 +00:00
}
}
2023-01-08 02:59:54 +00:00
private void PgbExperience_GeneratingPercentText(object sender, GeneratingPercentTextRoutedEventArgs e)
{
2023-06-08 21:06:46 +00:00
e.Text = $"{e.Value:f2} / {pExp.Maximum:f0}";
2023-01-08 02:59:54 +00:00
}
private void PgbStrength_GeneratingPercentText(object sender, GeneratingPercentTextRoutedEventArgs e)
{
2023-06-08 21:06:46 +00:00
e.Text = $"{e.Value:f2} / 100";
2023-01-08 02:59:54 +00:00
}
private void PgbSpirit_GeneratingPercentText(object sender, GeneratingPercentTextRoutedEventArgs e)
{
var progressBar = (ProgressBar)sender;
progressBar.Foreground = GetForeground(e.Value);
2023-06-08 21:06:46 +00:00
e.Text = $"{e.Value:f2} / 100";
2023-01-08 02:59:54 +00:00
}
private void PgbHunger_GeneratingPercentText(object sender, GeneratingPercentTextRoutedEventArgs e)
{
var progressBar = (ProgressBar)sender;
progressBar.Foreground = GetForeground(e.Value);
2023-06-08 21:06:46 +00:00
e.Text = $"{e.Value:f2} / 100";
2023-01-08 02:59:54 +00:00
}
private void PgbThirsty_GeneratingPercentText(object sender, GeneratingPercentTextRoutedEventArgs e)
{
var progressBar = (ProgressBar)sender;
progressBar.Foreground = GetForeground(e.Value);
2023-06-08 21:06:46 +00:00
e.Text = $"{e.Value:f2} / 100";
2023-01-08 16:57:10 +00:00
//if (e.Value <= 20)
//{
// tHearth.Visibility = Visibility.Visible;
//}
2023-01-08 02:59:54 +00:00
}
private Brush GetForeground(double value)
{
if (value >= 80)
{
return FindResource("SuccessProgressBarForeground") as Brush;
}
else if (value >= 50)
{
return FindResource("WarningProgressBarForeground") as Brush;
}
else
{
return FindResource("DangerProgressBarForeground") as Brush;
}
}
private void MenuPanel_MouseEnter(object sender, MouseEventArgs e)
2023-01-08 02:59:54 +00:00
{
BdrPanel.Visibility = Visibility.Visible;
2023-01-08 16:57:10 +00:00
M_TimeUIHandle(m);
2023-01-08 02:59:54 +00:00
}
2023-08-16 06:55:36 +00:00
private void MenuPanel_MouseLeave(object sender, MouseEventArgs e)
{
closePanelTimer.Start();
2023-08-16 06:55:36 +00:00
}
2023-01-11 13:10:18 +00:00
2023-01-20 17:01:10 +00:00
public void Dispose()
{
m = null;
2023-04-04 09:32:53 +00:00
CloseTimer.Dispose();
2023-01-20 17:01:10 +00:00
closePanelTimer.Dispose();
2023-01-11 13:10:18 +00:00
}
2023-01-25 04:49:18 +00:00
private void Sleep_Click(object sender, RoutedEventArgs e)
{
2023-06-01 08:16:45 +00:00
this.Visibility = Visibility.Collapsed;
2023-05-19 08:17:51 +00:00
if (m.Core.Save.Mode != GameSave.ModeType.Ill)
if (m.State == Main.WorkingState.Sleep)
2023-08-24 19:37:55 +00:00
{
m.State = WorkingState.Nomal;
2023-07-16 23:58:09 +00:00
m.Display(GraphType.Sleep, AnimatType.C_End, m.DisplayNomal);
2023-08-24 19:37:55 +00:00
}
2023-05-22 20:18:59 +00:00
else if (m.State == Main.WorkingState.Nomal)
2023-05-19 08:17:51 +00:00
m.DisplaySleep(true);
2023-05-22 20:18:59 +00:00
else
{
m.WorkTimer.Stop(() => m.DisplaySleep(true));
}
2023-05-19 08:17:51 +00:00
}
2022-12-13 07:10:18 +00:00
}
}