-
Notifications
You must be signed in to change notification settings - Fork 0
/
Popup.cs
170 lines (145 loc) · 6.2 KB
/
Popup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using XRLCore = XRL.Core.XRLCore;
using XRLPopup = XRL.UI.Popup;
using TextBlock = XRL.UI.TextBlock;
using static HarmonyInjector.Tools;
using static HarmonyInjector.Constants;
namespace HarmonyInjector
{
/// <summary>
/// Provides basic support to display popups, especially in the main-menu and from
/// the UI-thread. If being called from the game-thread while the game is running,
/// the normal popup infrastructure will be used automatically.
/// </summary>
public static class Popup
{
/// <summary>
/// Whether a popup is showing or is queued to show.
/// An exception is thrown if a request to show a popup occurs while one is already showing.
/// </summary>
public static bool Showing => showPending || ViewManager.ActiveView?.Name.StartsWith("Popup:") == true;
/// <summary>
/// Whether the game-state is in a place where a popup can be shown to the player properly.
/// </summary>
public static bool CanDoPopup
{
get
{
if (Showing) return false;
if (UseRegularPopups) return true;
if (GameManager.OverlayUIEnabled) return true;
return false;
}
}
private static global::GameManager GameManager => global::GameManager.Instance;
private static QupKit.ViewManager ViewManager => QupKit.ViewManager.Instance;
private static bool UseRegularPopups
{
get
{
// If we're on the game-thread and the game is running, we should be
// able to show a dialog through the normal means fine.
if (!OnGameThread) return false;
if (GameManager.CurrentGameView != "Game") return false;
return XRLCore.Core?.Game?.Running == true;
}
}
/// <summary>
/// Shows a simple dialog with an `Ok` button to the player.
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="onOk">The action to execute when the dialog is dismissed.</param>
/// <exception cref="InvalidOperationException">
/// When `Showing` is `true`; another popup is already being shown.
/// </exception>
public static void ShowMessage(string message, Action onOk = null)
{
if (Showing)
throw new InvalidOperationException("Another popup is already being shown.");
if (onOk == null) onOk = NoopFn;
if (UseRegularPopups)
{
XRLPopup.Show(message);
onOk();
}
else if (GameManager.OverlayUIEnabled)
{
// If on the UI-thread, then we have to manage the popup manually.
DoShow(() => {
var previousView = ViewManager.ActiveView?.Name;
Popup_MessageBox.Buttons = 1;
Popup_MessageBox.Default = 1;
Popup_MessageBox.Button1 = "Ok";
Popup_MessageBox.Text = new TextBlock(message, 80, 5000, false).GetRTFBlock();
Popup_MessageBox.Title = String.Empty;
Popup_MessageBox.action1 = HandleDismiss(onOk, previousView);
return "Popup:MessageBox";
});
}
else onOk();
}
private static void DoShow(Func<string> configurePopup)
{
showPending = true;
// Defer to next UI loop.
QueueUITask(() => {
// The `WaitFor` is for popups created while `XRLCore` is still initializing.
// This ensures the popup shows only after the main-menu is completely setup.
WaitFor(() => GameIsReady, () => {
// Show the popup...
ViewManager.SetActiveView(configurePopup(), bHideOldView: false);
showPending = false;
// ...and block the game-thread while it is shown.
QueueGameTask(() => { BlockWhile(() => Showing); });
});
});
}
private static Action HandleDismiss(Action action, string previousView) => () => {
// The action can be called more than once under certain circumstances.
// Got to prevent re-running if it has already been dismissed.
if (!Showing) return;
ViewManager.SetActiveView(previousView);
action();
};
private static bool showPending = false;
/// <summary>
/// Provides access to fields on the internal `Popup_MessageBox` class.
/// </summary>
private static class Popup_MessageBox
{
public static int Buttons
{
get { return (int)GetField(nameof(Buttons)); }
set { SetField(nameof(Buttons), value); }
}
public static int Default
{
get { return (int)GetField(nameof(Default)); }
set { SetField(nameof(Default), value); }
}
public static string Button1
{
get { return (string)GetField(nameof(Button1)); }
set { SetField(nameof(Button1), value); }
}
public static string Text
{
get { return (string)GetField(nameof(Text)); }
set { SetField(nameof(Text), value); }
}
public static string Title
{
get { return (string)GetField(nameof(Title)); }
set { SetField(nameof(Title), value); }
}
public static Action action1
{
get { return (Action)GetField(nameof(action1)); }
set { SetField(nameof(action1), value); }
}
private static object GetField(string name) { return type.GetField(name).GetValue(null); }
private static void SetField(string name, object value) { type.GetField(name).SetValue(null, value); }
private static readonly Type type = Injector.GetUnityType("Popup_MessageBox");
}
}
}