为选择对话框增加倒计时关闭功能 (#20)

This commit is contained in:
milimoe 2023-10-26 08:54:16 +08:00 committed by GitHub
parent 0882e526e8
commit 72f81cf5f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 236 additions and 184 deletions

View File

@ -203,14 +203,17 @@ namespace Milimoe.FunGame.Desktop.Controller
if (ServerMessage.Length > 0) room = ServerMessage.GetParam<Room>(0) ?? General.HallInstance; if (ServerMessage.Length > 0) room = ServerMessage.GetParam<Room>(0) ?? General.HallInstance;
if (room.Roomid == "-1") if (room.Roomid == "-1")
{ {
Main.ShowMessage(ShowMessageType.General, "暂时无法找到符合条件的房间。", "匹配房间"); Main.ShowMessage(ShowMessageType.General, "暂时无法找到符合条件的房间。", "匹配房间", 10);
Main.UpdateUI(MainInvokeType.MatchRoom, StartMatchState.Success, room); Main.UpdateUI(MainInvokeType.MatchRoom, StartMatchState.Success, room);
Main.UpdateUI(MainInvokeType.MatchRoom, StartMatchState.Cancel); Main.UpdateUI(MainInvokeType.MatchRoom, StartMatchState.Cancel);
return;
} }
else if (Main.ShowMessage(ShowMessageType.YesNo, "已找到符合条件的房间,是否加入?", "匹配房间", 10) == MessageResult.Yes) else if (Main.ShowMessage(ShowMessageType.YesNo, "已找到符合条件的房间,是否加入?", "匹配房间", 10) == MessageResult.Yes)
{ {
Main.UpdateUI(MainInvokeType.MatchRoom, StartMatchState.Success, room); Main.UpdateUI(MainInvokeType.MatchRoom, StartMatchState.Success, room);
} return;
}
Main.UpdateUI(MainInvokeType.MatchRoom, StartMatchState.Cancel);
} }
} }
} }

View File

@ -32,25 +32,25 @@ namespace Milimoe.FunGame.Desktop.Library.Component
switch (type) switch (type)
{ {
case ShowMessageType.General: case ShowMessageType.General:
result = Library.Component.ShowMessage.Message(msg, title, autoclose); result = Component.ShowMessage.Message(msg, title, autoclose);
break; break;
case ShowMessageType.Tip: case ShowMessageType.Tip:
result = Library.Component.ShowMessage.TipMessage(msg, "", autoclose); result = Component.ShowMessage.TipMessage(msg, "", autoclose);
break; break;
case ShowMessageType.Warning: case ShowMessageType.Warning:
result = Library.Component.ShowMessage.WarningMessage(msg, "", autoclose); result = Component.ShowMessage.WarningMessage(msg, "", autoclose);
break; break;
case ShowMessageType.Error: case ShowMessageType.Error:
result = Library.Component.ShowMessage.ErrorMessage(msg, "", autoclose); result = Component.ShowMessage.ErrorMessage(msg, "", autoclose);
break; break;
case ShowMessageType.YesNo: case ShowMessageType.YesNo:
result = Library.Component.ShowMessage.YesNoMessage(msg, title); result = Component.ShowMessage.YesNoMessage(msg, title, autoclose);
break; break;
case ShowMessageType.OKCancel: case ShowMessageType.OKCancel:
result = Library.Component.ShowMessage.OKCancelMessage(msg, title); result = Component.ShowMessage.OKCancelMessage(msg, title, autoclose);
break; break;
case ShowMessageType.RetryCancel: case ShowMessageType.RetryCancel:
result = Library.Component.ShowMessage.RetryCancelMessage(msg, title); result = Component.ShowMessage.RetryCancelMessage(msg, title, autoclose);
break; break;
default: default:
break; break;

View File

@ -3,11 +3,14 @@ using Milimoe.FunGame.Core.Library.Constant;
namespace Milimoe.FunGame.Desktop.Library.Component namespace Milimoe.FunGame.Desktop.Library.Component
{ {
/// <summary>
/// ShowMessage提供三个按钮三种按钮组合方式(单一,是否,是否+取消),提供自动关闭弹窗的功能。
/// </summary>
public partial class ShowMessage : GeneralForm public partial class ShowMessage : GeneralForm
{ {
private MessageResult MessageResult = MessageResult.Cancel; private MessageResult MessageResult = MessageResult.Cancel;
private readonly MessageResult AutoResult = MessageResult.OK;
private string InputResult = ""; private string InputResult = "";
private readonly int AutoClose = 0;
private const string TITLE_TIP = "提示"; private const string TITLE_TIP = "提示";
private const string TITLE_WARNING = "警告"; private const string TITLE_WARNING = "警告";
@ -18,43 +21,144 @@ namespace Milimoe.FunGame.Desktop.Library.Component
private const string BUTTON_NO = "否"; private const string BUTTON_NO = "否";
private const string BUTTON_RETRY = "重试"; private const string BUTTON_RETRY = "重试";
/// <summary>
/// 弹出单一按钮(确定)的弹窗
/// </summary>
/// <param name="msg"></param>
/// <param name="title"></param>
/// <param name="autoclose"></param>
/// <returns></returns>
public static MessageResult Message(string msg, string title, int autoclose = 0)
{
MessageResult result = new ShowMessage(title, msg, autoclose, MessageButtonType.OK, BUTTON_OK).MessageResult;
return result;
}
/// <summary>
/// 弹出单一按钮(确定)的弹窗,标题默认为“提示”
/// </summary>
/// <param name="msg"></param>
/// <param name="title"></param>
/// <param name="autoclose"></param>
/// <returns></returns>
public static MessageResult TipMessage(string msg, string title = "", int autoclose = 0)
{
MessageResult result = new ShowMessage(title == "" ? TITLE_TIP : title, msg, autoclose, MessageButtonType.OK, BUTTON_OK).MessageResult;
return result;
}
/// <summary>
/// 弹出单一按钮(确定)的弹窗,标题默认为“警告”
/// </summary>
/// <param name="msg"></param>
/// <param name="title"></param>
/// <param name="autoclose"></param>
/// <returns></returns>
public static MessageResult WarningMessage(string msg, string title = "", int autoclose = 0)
{
MessageResult result = new ShowMessage(title == "" ? TITLE_WARNING : title, msg, autoclose, MessageButtonType.OK, BUTTON_OK).MessageResult;
return result;
}
/// <summary>
/// 弹出单一按钮(确定)的弹窗,标题默认为“错误”
/// </summary>
/// <param name="msg"></param>
/// <param name="title"></param>
/// <param name="autoclose"></param>
/// <returns></returns>
public static MessageResult ErrorMessage(string msg, string title = "", int autoclose = 0)
{
MessageResult result = new ShowMessage(title == "" ? TITLE_ERROR : title, msg, autoclose, MessageButtonType.OK, BUTTON_OK).MessageResult;
return result;
}
/// <summary>
/// 弹出选择(是,否)的弹窗
/// </summary>
/// <param name="msg"></param>
/// <param name="title"></param>
/// <param name="autoclose"></param>
/// <returns></returns>
public static MessageResult YesNoMessage(string msg, string title, int autoclose = 0)
{
MessageResult result = new ShowMessage(title, msg, autoclose, MessageButtonType.YesNo, BUTTON_CANCEL, BUTTON_YES, BUTTON_NO).MessageResult;
return result;
}
/// <summary>
/// 弹出选择(确定,取消)的弹窗
/// </summary>
/// <param name="msg"></param>
/// <param name="title"></param>
/// <param name="autoclose"></param>
/// <returns></returns>
public static MessageResult OKCancelMessage(string msg, string title, int autoclose = 0)
{
MessageResult result = new ShowMessage(title, msg, autoclose, MessageButtonType.OKCancel, BUTTON_CANCEL, BUTTON_OK, BUTTON_CANCEL).MessageResult;
return result;
}
/// <summary>
/// 弹出选择(重试,取消)的弹窗
/// </summary>
/// <param name="msg"></param>
/// <param name="title"></param>
/// <param name="autoclose"></param>
/// <returns></returns>
public static MessageResult RetryCancelMessage(string msg, string title, int autoclose = 0)
{
MessageResult result = new ShowMessage(title, msg, autoclose, MessageButtonType.RetryCancel, BUTTON_CANCEL, BUTTON_RETRY, BUTTON_CANCEL).MessageResult;
return result;
}
/// <summary>
/// 弹出具有输入框的弹窗
/// </summary>
/// <param name="msg"></param>
/// <param name="title"></param>
/// <returns>输入框内的文本</returns>
public static string InputMessage(string msg, string title)
{
string result = new ShowMessage(title, msg, 0, MessageButtonType.Input).InputResult;
return result;
}
/// <summary>
/// 弹出具有输入框的弹窗,多增加了(点击的按钮)返回值,可用于判断是否取消操作
/// </summary>
/// <param name="msg"></param>
/// <param name="title"></param>
/// <param name="cancel">点击的按钮</param>
/// <returns>输入框内的文本</returns>
public static string InputMessageCancel(string msg, string title, out MessageResult cancel)
{
ShowMessage window = new(title, msg, 0, MessageButtonType.Input);
string result = window.InputResult;
cancel = window.MessageResult;
return result;
}
/// <summary> /// <summary>
/// 构造方法 /// 构造方法
/// </summary> /// </summary>
/// /// <param name="objs">参数数组,按下面的注释传参,不要乱传</param> /// <param name="title">标题</param>
private ShowMessage(params object[] objs) /// <param name="msg">信息</param>
/// <param name="autoclose">自动关闭倒计时(秒)</param>
/// <param name="type">按钮类型(组合类型)</param>
/// <param name="mid">中间按钮的文本</param>
/// <param name="left">左边按钮的文本</param>
/// <param name="right">右边按钮的文本</param>
private ShowMessage(string title = "", string msg = "", int autoclose = 0, MessageButtonType type = MessageButtonType.OK, string mid = "", string left = "", string right = "")
{ {
InitializeComponent(); InitializeComponent();
Opacity = 0.85; // 透明度 Opacity = 0.85; // 透明度
if (objs != null) // 标题
{ Title.Text = title;
/** Text = title;
* objs: // 信息
* 0 = title MsgText.Text = msg;
* 1 = msg // 按钮类型(组合类型)
* 2 = autoclose(second)
* 3 = button type
* 4 = mid text
* 5 = left text
* 6 = right text
*/
int length = objs.Length;
if (length > 0 && objs[0] != null)
{
Title.Text = (string)objs[0];
Text = Title.Text;
}
if (length > 1 && objs[1] != null)
{
MsgText.Text = (string)objs[1];
}
if (length > 2 && objs[2] != null)
{
AutoClose = (int)objs[2];
}
if (length > 3 && objs[3] != null)
{
MessageButtonType type = (MessageButtonType)objs[3];
switch (type) switch (type)
{ {
case MessageButtonType.OK: case MessageButtonType.OK:
@ -73,6 +177,7 @@ namespace Milimoe.FunGame.Desktop.Library.Component
LeftButton.Visible = true; LeftButton.Visible = true;
RightButton.Visible = true; RightButton.Visible = true;
MidButton.Visible = false; MidButton.Visible = false;
AutoResult = MessageResult.Cancel;
break; break;
case MessageButtonType.YesNo: case MessageButtonType.YesNo:
LeftButton.Text = BUTTON_YES; LeftButton.Text = BUTTON_YES;
@ -82,6 +187,7 @@ namespace Milimoe.FunGame.Desktop.Library.Component
LeftButton.Visible = true; LeftButton.Visible = true;
RightButton.Visible = true; RightButton.Visible = true;
MidButton.Visible = false; MidButton.Visible = false;
AutoResult = MessageResult.No;
break; break;
case MessageButtonType.RetryCancel: case MessageButtonType.RetryCancel:
LeftButton.Text = BUTTON_RETRY; LeftButton.Text = BUTTON_RETRY;
@ -91,6 +197,7 @@ namespace Milimoe.FunGame.Desktop.Library.Component
LeftButton.Visible = true; LeftButton.Visible = true;
RightButton.Visible = true; RightButton.Visible = true;
MidButton.Visible = false; MidButton.Visible = false;
AutoResult = MessageResult.Cancel;
break; break;
case MessageButtonType.Input: case MessageButtonType.Input:
InputButton.Text = BUTTON_OK; InputButton.Text = BUTTON_OK;
@ -99,24 +206,28 @@ namespace Milimoe.FunGame.Desktop.Library.Component
MidButton.Visible = false; MidButton.Visible = false;
InputText.Visible = true; InputText.Visible = true;
InputButton.Visible = true; InputButton.Visible = true;
AutoResult = MessageResult.Cancel;
break; break;
} }
if (length > 4 && objs[4] != null) MidButton.Text = (string)objs[4]; // 按钮文本
if (length > 5 && objs[5] != null) LeftButton.Text = (string)objs[5]; MidButton.Text = mid;
if (length > 6 && objs[6] != null) RightButton.Text = (string)objs[6]; LeftButton.Text = left;
RightButton.Text = right;
// 自动关闭倒计时
TaskUtility.NewTask(async () => await AutoClose(autoclose));
ShowDialog();
} }
}
else /// <summary>
/// 自动关闭窗口的实现
/// </summary>
/// <param name="autoclose"></param>
/// <returns></returns>
private async Task AutoClose(int autoclose)
{ {
MessageResult = MessageResult.Cancel; if (autoclose > 0)
Dispose();
}
if (AutoClose > 0)
{
TaskUtility.NewTask(async () =>
{ {
string msg = MsgText.Text; string msg = MsgText.Text;
int s = AutoClose;
await Task.Run(() => await Task.Run(() =>
{ {
while (IsHandleCreated) while (IsHandleCreated)
@ -124,19 +235,17 @@ namespace Milimoe.FunGame.Desktop.Library.Component
break; break;
} }
}); });
BeginInvoke(() => ChangeSecond(msg, s)); ChangeSecond(msg, autoclose);
while (!Disposing) while (!Disposing)
{ {
if (s > 0) await Task.Delay(1000); if (autoclose > 0) await Task.Delay(1000);
else break; else break;
s--; autoclose--;
BeginInvoke(() => ChangeSecond(msg, s)); ChangeSecond(msg, autoclose);
} }
MessageResult = MessageResult.OK; MessageResult = AutoResult;
Close(); Close();
});
} }
ShowDialog();
} }
/// <summary> /// <summary>
@ -157,71 +266,11 @@ namespace Milimoe.FunGame.Desktop.Library.Component
Dispose(); Dispose();
} }
public static MessageResult Message(string msg, string title, int autoclose = 0) /// <summary>
{ /// 修改倒计时显示
object[] objs = { title, msg, autoclose, MessageButtonType.OK, BUTTON_OK }; /// </summary>
MessageResult result = new ShowMessage(objs).MessageResult; /// <param name="msg"></param>
return result; /// <param name="s"></param>
}
public static MessageResult TipMessage(string msg, string title = "", int autoclose = 0)
{
object[] objs = { title == "" ? TITLE_TIP : title, msg, autoclose, MessageButtonType.OK, BUTTON_OK };
MessageResult result = new ShowMessage(objs).MessageResult;
return result;
}
public static MessageResult WarningMessage(string msg, string title = "", int autoclose = 0)
{
object[] objs = { title == "" ? TITLE_WARNING : title, msg, autoclose, MessageButtonType.OK, BUTTON_OK };
MessageResult result = new ShowMessage(objs).MessageResult;
return result;
}
public static MessageResult ErrorMessage(string msg, string title = "", int autoclose = 0)
{
object[] objs = { title == "" ? TITLE_ERROR : title, msg, autoclose, MessageButtonType.OK, BUTTON_OK };
MessageResult result = new ShowMessage(objs).MessageResult;
return result;
}
public static MessageResult YesNoMessage(string msg, string title)
{
object[] objs = { title, msg, 0, MessageButtonType.YesNo, BUTTON_CANCEL, BUTTON_YES, BUTTON_NO };
MessageResult result = new ShowMessage(objs).MessageResult;
return result;
}
public static MessageResult OKCancelMessage(string msg, string title)
{
object[] objs = { title, msg, 0, MessageButtonType.OKCancel, BUTTON_CANCEL, BUTTON_OK, BUTTON_CANCEL };
MessageResult result = new ShowMessage(objs).MessageResult;
return result;
}
public static MessageResult RetryCancelMessage(string msg, string title)
{
object[] objs = { title, msg, 0, MessageButtonType.RetryCancel, BUTTON_CANCEL, BUTTON_RETRY, BUTTON_CANCEL };
MessageResult result = new ShowMessage(objs).MessageResult;
return result;
}
public static string InputMessage(string msg, string title)
{
object[] objs = { title, msg, 0, MessageButtonType.Input };
string result = new ShowMessage(objs).InputResult;
return result;
}
public static string InputMessageCancel(string msg, string title, out MessageResult cancel)
{
object[] objs = { title, msg, 0, MessageButtonType.Input };
ShowMessage window = new(objs);
string result = window.InputResult;
cancel = window.MessageResult;
return result;
}
private void ChangeSecond(string msg, int s) private void ChangeSecond(string msg, int s)
{ {
MsgText.Text = msg + "\n[ " + s + " 秒后自动关闭 ]"; MsgText.Text = msg + "\n[ " + s + " 秒后自动关闭 ]";