-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.sqf
More file actions
274 lines (196 loc) · 10.1 KB
/
init.sqf
File metadata and controls
274 lines (196 loc) · 10.1 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// NO EDIT BELOW HERE
#define OS_DEBUGCONSOLE 0
#define OS_FREEZEUNFREEZE 0
ded_CytechOSInstances = createHashMap;
ded_CytechOSGeneration = hashValue diag_tickTime;
// Create a new OS (This must be executed on every player's machine
ded_fnc_CreateOS = {
params ["_osName", "_screenVariableName", "_keyboardVariableName"];
diag_log ["create", _this];
private _osData = ded_CytechOSInstances getOrDefault [_osName, createHashMap, true];
_osData set ["name", _osName];
_osData set ["screen", (missionNamespace getVariable _screenVariableName)];
_osData set ["keyboard", (missionNamespace getVariable _keyboardVariableName)];
_osData set ["stateInfo", "{}"];
_osData set ["stateHandlers", []];
_osData set ["powered", false];
(_osData get "keyboard") addAction ["Open Controls", {
(_this select 3) params ["_osData"];
private _display = (findDisplay 46) createDisplay "RscCytechDisplayControl";
_display setVariable ["osData", _osData];
private _ctrl = (_display displayCtrl 1337);
#if OS_DEBUGCONSOLE
_ctrl ctrlWebBrowserAction ["OpenDevConsole"];
[_ctrl] spawn {Sleep 1; (_this#0) ctrlWebBrowserAction ["LoadFile", "CytechUI\index.html"];};
#else
_ctrl ctrlWebBrowserAction ["LoadFile", "CytechUI\index.html"];
#endif
_ctrl ctrlAddEventHandler ["JSDialog", {
params ["_ctrl", "_isConfirmDialog", "_message"];
private _display = ctrlParent _ctrl;
private _osData = _display getVariable "osData";
[_osData get "name", _message] call ded_fnc_OnJSAlert;
true; // We need to tell it that we handled the "dialog", by returning true or false.
}];
// Wait for the browser to actually be loaded
_ctrl ctrlAddEventHandler ["PageLoaded", {
params ["_ctrl"];
private _display = ctrlParent _ctrl;
private _osData = _display getVariable "osData";
diag_log ["PageLoaded", _osData get "name", diag_frameNo];
_ctrl ctrlWebBrowserAction ["ExecJS", format ["UIUpdateState(%1)", _osData get "stateInfo"]];
}];
}, _osData, 1.5, true, true, "", format["ded_CytechOSInstances get '%1' get 'powered'", _osName]];
#if OS_FREEZEUNFREEZE
(_osData get "keyboard") addAction ["Freeze", {
(_this select 3) params ["_osData"];
private _ctrl = _osData getOrDefault ["ctrl", controlNull];
if (!isNull _ctrl) then {
// UI is open, tell it about new state
_ctrl ctrlWebBrowserAction ["StopBrowser"];
[(ctrlParent _ctrl) getVariable "ded_p2"] call CBA_fnc_removePerFrameHandler;
(ctrlParent _ctrl) setVariable ["ded_p2", -1];
};
}, _osData, 1.5, true, true, "", format["ded_CytechOSInstances get '%1' get 'powered'", _osName]];
(_osData get "keyboard") addAction ["UnFreeze", {
(_this select 3) params ["_osData"];
private _ctrl = _osData getOrDefault ["ctrl", controlNull];
if (!isNull _ctrl) then {
// UI is open, tell it about new state
_ctrl ctrlWebBrowserAction ["OpenDevConsole"];
_ctrl ctrlWebBrowserAction ["ResumeBrowser"]; // This will trigger the "PageLoaded" eventhandler again, which will fill in our state
(ctrlParent _ctrl) setVariable ["ded_p2", -2]; // Pretend we have a eventhandler, so that the PageLoad doesn't add it back. We will add it manually once the page is stable
_ctrl setVariable ["lastDraw", diag_frameNo];
_ctrl ctrlAddEventHandler ["Draw", {
params ["_ctrl"];
_ctrl setVariable ["lastDraw", diag_frameNo]; // Remember when the last draw happened
}];
[{
// Wait until the last draw, was more than 60 frames ago. So we wait until the image is "stable"
(((_this select 0) getVariable "lastDraw") + 60) < diag_frameNo
//#TODO handle the control becoming null because the UI was closed
},
{
// No draw for 60 frames, its probably stable, resume updating the ui2tex
private _display = ctrlParent (_this select 0);
private _p2 = [{displayUpdate (_this#0)}, 0.1, _display] call CBA_fnc_addPerFrameHandler;
_display setVariable ["ded_p2", _p2];
}, [_ctrl]] call CBA_fnc_waitUntilAndExecute;
};
}, _osData, 1.5, true, true, "", format["ded_CytechOSInstances get '%1' get 'powered'", _osName]];
#endif // OS_FREEZEUNFREEZE
};
ded_fnc_SetOSPowerOn = {
params ["_osName"];
private _osData = ded_CytechOSInstances get _osName;
_osData set ["powered", true];
(_osData get "screen") setObjectTexture [0, format["#(rgb,2048,1024,1)ui(RscCytechDisplay,%1_%s)", _osName, ded_CytechOSGeneration]];
};
// Internal function
ded_fnc_SetOSState = {
params ["_osName", "_newState"];
_osData = ded_CytechOSInstances getOrDefault [_osName, createHashMap, true];
_osData set ["stateInfo", _newState];
private _ctrl = _osData getOrDefault ["ctrl", controlNull];
if (!isNull _ctrl) then {
// UI is open, tell it about new state
_ctrl ctrlWebBrowserAction ["ExecJS", format ["UIUpdateState(%1)", _osData get "stateInfo"]];
};
private _stateMap = fromJson _newState;
{ _stateMap call _x; } forEach (_osData getOrDefault ["stateHandlers", []]);
};
ded_fnc_OnJSAlert = {
params ["_osName", "_content"];
// Tell everyone about new state. Including ourselves, because this is only possible on interactive UI
if ((_content select [0,5]) == "state") then {
[_osName, _content select [5]] remoteExec ["ded_fnc_SetOSState", 0];
};
};
// Only for ui2tex variant
ded_fnc_OSDisplayLoad = {
diag_log ["load", _this, diag_frameNo];
params ["_display", "_osName"];
_osName = _osName select [0, _osName find "_"];
private _osData = ded_CytechOSInstances getOrDefault [_osName, createHashMap, true];
_display setVariable ["osData", _osData];
private _ctrl = (_display displayCtrl 1337);
_osData set ["ctrl", _ctrl];
#if OS_DEBUGCONSOLE
_ctrl ctrlWebBrowserAction ["OpenDevConsole"];
[_ctrl] spawn {Sleep 1; (_this#0) ctrlWebBrowserAction ["LoadFile", "CytechUI\index.html"];};
#else
_ctrl ctrlWebBrowserAction ["LoadFile", "CytechUI\index.html"];
#endif
_ctrl ctrlAddEventHandler ["JSDialog", {
params ["_ctrl", "_isConfirmDialog", "_message"];
private _display = ctrlParent _ctrl;
private _osData = _display getVariable "osData";
[_osData get "name", _message] call ded_fnc_OnJSAlert;
true; // We need to tell it that we handled the "dialog", by returning true or false.
}];
_ctrl ctrlAddEventHandler ["PageLoaded", {
params ["_ctrl"];
private _display = ctrlParent _ctrl;
private _osData = _display getVariable "osData";
diag_log ["PageLoaded", _osData get "name", diag_frameNo, _ctrl];
_ctrl ctrlWebBrowserAction ["ExecJS", format ["UIUpdateState(%1)", _osData get "stateInfo"]];
// Regular updating
if ((_display getVariable ["ded_p2", -1]) isEqualTo -1) then {
private _p2 = [{displayUpdate (_this#0)}, 0.1, _display] call CBA_fnc_addPerFrameHandler;
_display setVariable ["ded_p2", _p2];
};
}];
_display displayAddEventHandler ["Unload", {
params ["_display", "_exitCode"];
[_display getVariable "ded_p2"] call CBA_fnc_removePerFrameHandler;
}];
};
// NO EDIT ABOVE HERE
// Register all OS's, each one is a separate computer, with separate screen and keyboard objects
// Keyboard can be the same object as the screen, which will make the screen be the input device (like a touch screen)
// First parameter is the name of the OS, which is referenced again to turn power on and handle state changes
["OS1", "Display_1_Screen", "Display_1_Keyboard"] call ded_fnc_CreateOS;
// Turn on the power, must be done once. Cannot be turned off again.
// !Important! The system will only actually boot, if its in view of atleast on player. Only if the screen is visible!
["OS1"] call ded_fnc_SetOSPowerOn;
// Register eventhandlers for the OS.
// Eventhandlers are called globally, on all players
// The first call to the eventhandler, will be after bootup ("page" member will be set to "mainsystems")
(ded_CytechOSInstances get "OS1" get "stateHandlers") pushBack {
params ["_newState"];
// Something about this OS's state changed
private _sys1IsEnabled = _newState getOrDefault ["sys1", false];
private _sys2IsEnabled = _newState getOrDefault ["sys2", false];
private _sys3IsEnabled = _newState getOrDefault ["sys3", false];
private _sys4IsEnabled = _newState getOrDefault ["sys4", false];
// Do something with the system states, in this case color some indicators
private _colorOn = "#(argb,8,8,3)color(0,1,0,1,co)";
private _colorOff = "#(argb,8,8,3)color(1,0,0,1,co)";
indicator1 setObjectTexture [0, [_colorOff, _colorOn] select _sys1IsEnabled];
indicator2 setObjectTexture [0, [_colorOff, _colorOn] select _sys2IsEnabled];
indicator3 setObjectTexture [0, [_colorOff, _colorOn] select _sys3IsEnabled];
indicator4 setObjectTexture [0, [_colorOff, _colorOn] select _sys4IsEnabled];
// System 3 on OS1, controls the toxic zone trigger example
OS1_ToxicZone1 enableSimulation !_sys3IsEnabled;
diag_log ["newState", _newState];
};
// Second OS example, it controls different indicators
["OS2", "Display_1_Screen_1", "Display_1_Keyboard_1"] call ded_fnc_CreateOS;
["OS2"] call ded_fnc_SetOSPowerOn;
(ded_CytechOSInstances get "OS2" get "stateHandlers") pushBack {
params ["_newState"];
// Something about this OS's state changed
private _sys1IsEnabled = _newState getOrDefault ["sys1", false];
private _sys2IsEnabled = _newState getOrDefault ["sys2", false];
private _sys3IsEnabled = _newState getOrDefault ["sys3", false];
private _sys4IsEnabled = _newState getOrDefault ["sys4", false];
// Do something with the system states, in this case color some indicators
private _colorOn = "#(argb,8,8,3)color(0.5,0.5,0,1,co)";
private _colorOff = "#(argb,8,8,3)color(0,0,1,1,co)";
indicator1_1 setObjectTexture [0, [_colorOff, _colorOn] select _sys1IsEnabled];
indicator2_1 setObjectTexture [0, [_colorOff, _colorOn] select _sys2IsEnabled];
indicator3_1 setObjectTexture [0, [_colorOff, _colorOn] select _sys3IsEnabled];
indicator4_1 setObjectTexture [0, [_colorOff, _colorOn] select _sys4IsEnabled];
diag_log ["newState", _newState];
};
[] execVM "toxicAreaTrigger.sqf";