forked from super-jenius/Untold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BrowserTier.as
214 lines (195 loc) · 6.66 KB
/
BrowserTier.as
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Web Browser Tier
// Opens browser and completes tier when browser is closed.
import com.GameInterface.Browser.Browser;
class BrowserTier extends BaseTier
{
public var m_URL:String;
public var m_BrowserTitle:String;
public var m_HideAddress:Boolean;
public var m_Width:Number;
public var m_Height:Number;
public var m_TrackURL:Boolean;
public var m_Response:String;
public var m_ResponseTier:BrowserResponseTier;
public function LoadXML(tierNode:XMLNode)
{
//ULog.Info("BrowserTier.LoadXML()");
this.SetURL(tierNode.attributes.url, tierNode.attributes.browserTitle, (tierNode.attributes.hideAddress == "true"),
Number(tierNode.attributes.width), Number(tierNode.attributes.height));
// Can require correct response before advancing to next tier
m_Response = tierNode.attributes.response;
if (m_Response) {
m_ResponseTier = m_Mission.AddTier("response", "The previous tier must be completed successfully before you can move on to the next tier.\n\nPress the Back button to reopen the window.");
SetURLTracking(true);
onURLChanged = CheckResponse;
}
}
public function SetURL(url:String, browserTitle:String, hideAddress:Boolean, width:Number, height:Number)
{
//ULog.Info("BrowserTier.SetURL(): title=" + browserTitle);
m_URL = USUtils.TextMerge(url, true);
m_BrowserTitle = browserTitle;
m_HideAddress = hideAddress;
m_Width = width;
m_Height = height;
}
public function SetURLTracking(trackURL:Boolean)
{
//ULog.Info("BrowserTier.SetURLTracking()");
m_TrackURL = trackURL;
}
public function StartTier()
{
ULog.Info("BrowserTier.StartTier() title=" + m_BrowserTitle);
var url = m_URL;
// If size specified, defer loading until browser resized
if (m_Width || m_Height)
{
//url = "about:blank";
url = "data:,";
}
com.GameInterface.DistributedValueBase.SetDValue("WebBrowserStartURL", url);
com.GameInterface.DistributedValueBase.SetDValue("web_browser", true);
this.AdjustBrowser(this, m_URL, m_BrowserTitle, m_HideAddress, m_Width, m_Height);
if (m_TrackURL == true) {
this.TrackURL();
}
}
public function AdjustBrowser(thisTier)
{
_root.webbrowser.m_Window._visible = false;
if (_root.webbrowser.m_Window.m_Content.m_History.length > 0)
{
ULog.Info("BrowserTier.AdjustBrowser()");
var browserWindow = _root.webbrowser.m_Window;
var loader = browserWindow.m_Content.m_Loader;
browserWindow._visible = false;
if (thisTier.m_BrowserTitle)
{
browserWindow.SetTitle(thisTier.m_BrowserTitle);
}
if (thisTier.m_HideAddress)
{
browserWindow.m_Content.m_AddressBar._visible = false;
browserWindow.m_Content.m_BackButton._visible = false;
browserWindow.m_Content.m_ForwardButton._visible = false;
}
// Changing the size is a pain
// You have to change individual elements, or else things get scaled and skewed.
var width = thisTier.m_Width;
var height = thisTier.m_Height;
if (width || height)
{
var visibleRect = Stage["visibleRect"];
if (width)
{
width = Math.min(width, visibleRect.width - 10); // don't exceed resolution
var widthDiff = loader._width - width;
loader._width = width;
browserWindow.m_Background._width -= widthDiff;
browserWindow.m_DropShadow._width -= widthDiff;
browserWindow.m_CloseButton._x -= widthDiff;
}
if (height)
{
height = Math.min(height, visibleRect.height - 80); // don't exceed resolution
var heightDiff = loader._height - height;
loader._height = height;
browserWindow.m_Background._height -= heightDiff;
browserWindow.m_DropShadow._height -= heightDiff;
}
com.GameInterface.DistributedValueBase.SetDValue("WebBrowserStartURL", thisTier.m_URL);
browserWindow.m_Content.configUI();
// Fix scaling after reloaded
loader._xscale = 100;
loader._yscale = 100;
// Center window at new size
_x = visibleRect.x;
_y = visibleRect.y;
browserWindow._x = Math.round((visibleRect.width / 2) - (browserWindow.m_Background._width / 2));
browserWindow._y = Math.round((visibleRect.height / 2) - (browserWindow.m_Background._height / 2));
}
browserWindow._visible = true;
// End tier when browser closed
//browserWindow.SignalClose.Connect(thisTier.EndTier, thisTier);
thisTier.CheckBrowser(thisTier);
}
else
{
// May take a bit for the browser to load. Keep trying until available.
setTimeout(thisTier.AdjustBrowser, 10, thisTier);
}
}
// SignalClose doesn't fire if browser closed via keyboard (Esc, B), so check if it is still open
public function CheckBrowser(thisTier)
{
if (_root.webbrowser.m_Window) {
setTimeout(thisTier.CheckBrowser, 100, thisTier);
}
else {
thisTier.EndTier();
}
}
// Track when URL changes
// This is the only way the browser can send data back to TSW
public function TrackURL()
{
var browserSignal = _root.webbrowser.m_Window.m_Content.m_Browser.SignalStartLoadingURL;
if (browserSignal) {
ULog.Info("BrowserTier.TrackURL(): Tracking SignalStartLoadingURL");
browserSignal.Connect(onURLChanged, this);
//_root.webbrowser.m_Window.m_Content.m_Browser.SignalStartLoadingURL.Connect(onURLChanged, this);
}
else {
// Browser takes a bit to load, so retry until it is loaded
// Use _global.setTimeout to avoid scoping issues
_global.setTimeout(this, "TrackURL", 100);
}
}
// Event fires when URL changed
public function onURLChanged(url)
{
ULog.Info("BrowserTier.onURLChanged(): " + url);
}
// Check browser response
public function CheckResponse(url)
{
// Formatted as data URL + expected string
var correctResponse = "data:," + m_Response.toLowerCase();
if (url.toLowerCase() == correctResponse) {
// Set response tier complete
m_ResponseTier.m_Complete = true;
// Close browser
com.GameInterface.DistributedValueBase.SetDValue("WebBrowserStartURL", "");
com.GameInterface.DistributedValueBase.SetDValue("web_browser", false);
// End tier
EndTier();
}
}
public function AbortTier()
{
ULog.Info("BrowserTier.AbortTier()");
// Close browser
com.GameInterface.DistributedValueBase.SetDValue("WebBrowserStartURL", "");
com.GameInterface.DistributedValueBase.SetDValue("web_browser", false);
}
public function ConvertToXML()
{
var tierXML:String = super.ConvertToXML(true);
tierXML += 'url="' + m_URL + '" ';
if (m_BrowserTitle) {
tierXML += 'browserTitle="' + m_BrowserTitle.split("\n").join("\\n") + '" ';
}
if (m_HideAddress) {
tierXML += 'hideAddress="' + m_HideAddress.toString() + '" ';
}
if (m_Width) {
tierXML += 'width="' + m_Width.toString() + '" ';
}
if (m_Height) {
tierXML += 'height="' + m_Height.toString() + '" ';
}
tierXML += ' />\n'
return tierXML;
}
}