-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathfnc_notify.sqf
229 lines (180 loc) · 7.26 KB
/
fnc_notify.sqf
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
#include "script_component.hpp"
/* ----------------------------------------------------------------------------
Function: CBA_fnc_notify
Description:
Display a text message. Multiple incoming messages are queued.
Parameters:
_content - Notifications content (lines). <ARRAY>
_line1 - First content line. <ARRAY>
_line2 - Second content line. <ARRAY>
...
_lineN - N-th content line (may be passed directly if only 1 line is required). <ARRAY>
_text - Text to display or path to .paa or .jpg image (may be passed directly if only text is required). <STRING, NUMBER>
_size - Text or image size multiplier. (optional, default: 1) <NUMBER>
_color - RGB or RGBA color (range 0-1). (optional, default: [1, 1, 1, 1]) <ARRAY>
_skippable - Skip or overwrite this notification if another entered the queue. (optional, default: false) <BOOL>
_sound - Play notification sound when hint is displayed (optional, default: false) <BOOL>
Examples:
(begin example)
"Banana" call CBA_fnc_notify;
["Banana", 1.5, [1, 1, 0, 1], true, true] call CBA_fnc_notify;
[["Banana", 1.5, [1, 1, 0, 1]], ["Not Apple"], true, true] call CBA_fnc_notify;
(end)
Returns:
Nothing
Authors:
commy2
---------------------------------------------------------------------------- */
#define FADE_IN_TIME 0.2
#define FADE_OUT_TIME 1
if (canSuspend) exitWith {
[CBA_fnc_notify, _this] call CBA_fnc_directCall;
};
if (!hasInterface) exitWith {};
// compose structured text
if !(_this isEqualType []) then {
_this = [_this];
};
if !(_this select 0 isEqualType []) then {
// Handle booleans at index 3-4
_this = [_this select [0, 3]] + (_this select [3, 2]);
};
// Handle boolean parameters
private _composition = [];
(_this select {_x isEqualType true}) params [
["_skippable", false],
["_sound", false]
];
FILTER(_this,!(_x isEqualType true));
{
// Line
_composition pushBack lineBreak;
_x params [["_text", "", ["", 0]], ["_size", 1, [0]], ["_color", [], [[]], [3,4]]];
if (_text isEqualType 0) then {
_text = str _text;
};
_size = _size * 0.55 / (getResolution select 5);
_color params [
["_r", 1, [0]],
["_g", 1, [0]],
["_b", 1, [0]],
["_a", 1, [0]]
];
_color = [_r, _g, _b, _a] call BIS_fnc_colorRGBAtoHTML;
private _isImage = toLower _text select [count _text - 4] in [".paa", ".jpg"];
if (_isImage) then {
_composition pushBack parseText format ["<img align='center' size='%2' color='%3' image='%1'/>", _text, _size, _color];
} else {
_composition pushBack parseText format ["<t align='center' size='%2' color='%3'>%1</t>", _text, _size, _color];
};
} forEach _this;
_composition deleteAt 0;
private _notification = [_composition, _skippable, _sound];
// add the queue
if (isNil QGVAR(notifyQueue)) then {
GVAR(notifyQueue) = [];
};
GVAR(notifyQueue) pushBack _notification;
private _fnc_popQueue = {
params ["_controls", "_fnc_processQueue"];
GVAR(notifyQueue) deleteAt 0;
private _notification = GVAR(notifyQueue) select 0;
if (!isNil "_notification") then {
_notification call _fnc_processQueue;
} else {
// fade out
{
_x ctrlSetFade 1;
_x ctrlCommit (FADE_OUT_TIME);
} forEach _controls;
};
};
private _fnc_processQueue = {
params ["_composition", "_skippable", "_sound"];
// Notification is skippable and the queue is not empty - skip to the next one immediately
if (_skippable && {count GVAR(notifyQueue) > 1}) exitWith {
LOG("Skipped queue process");
[[], _fnc_processQueue] call _fnc_popQueue;
};
if _sound then {
LOG("Played sound");
playSound "hint";
};
QGVAR(notify) cutRsc ["RscTitleDisplayEmpty", "PLAIN", 0, true];
private _display = uiNamespace getVariable "RscTitleDisplayEmpty";
private _vignette = _display displayCtrl 1202;
_vignette ctrlShow false;
private _background = _display ctrlCreate ["RscText", -1];
_background ctrlSetBackgroundColor [0,0,0,0.25];
private _text = _display ctrlCreate ["RscStructuredText", -1];
_text ctrlSetStructuredText composeText _composition;
private _controls = [_background, _text];
private _left = profileNamespace getVariable ['TRIPLES(IGUI,GVAR(notify),x)', NOTIFY_DEFAULT_X];
private _top = profileNamespace getVariable ['TRIPLES(IGUI,GVAR(notify),y)', NOTIFY_DEFAULT_Y];
private _width = profileNamespace getVariable ['TRIPLES(IGUI,GVAR(notify),w)', NOTIFY_MIN_WIDTH];
private _height = profileNamespace getVariable ['TRIPLES(IGUI,GVAR(notify),h)', NOTIFY_MIN_HEIGHT];
_width = ctrlTextWidth _text max _width;
// need to set this before reading the text height, to get the correct amount of auto line breaks
_text ctrlSetPosition [0, 0, _width, _height];
_text ctrlCommit 0;
private _textHeight = ctrlTextHeight _text;
_height = _textHeight max _height;
// ensure the box not going off screen
private _right = _left + _width;
private _bottom = _top + _height;
private _leftEdge = safezoneX;
private _rightEdge = safezoneW + safezoneX;
private _topEdge = safezoneY;
private _bottomEdge = safezoneH + safezoneY;
if (_right > _rightEdge) then {
_left = _left - (_right - _rightEdge);
};
if (_left < _leftEdge) then {
_left = _left + (_leftEdge - _left);
};
if (_bottom > _bottomEdge) then {
_top = _top - (_bottom - _bottomEdge);
};
if (_top < _topEdge) then {
_top = _top + (_topEdge - _top);
};
_background ctrlSetPosition [_left, _top, _width, _height];
if (_textHeight < _height) then {
_top = _top + (_height - _textHeight) / 2;
};
_text ctrlSetPosition [_left, _top, _width, _textHeight];
// fade in
{
_x ctrlSetFade 1;
_x ctrlCommit 0;
_x ctrlSetFade 0;
_x ctrlCommit (FADE_IN_TIME);
} forEach _controls;
// pop queue - waitUntilAndExecute for skippable, waitAndExecute for non-skippable for less wasted condition checking
TRACE_3("Pop wait",_composition,_skippable,GVAR(notifyQueue));
if (_skippable) then {
[{
// Wait for another notification to be added
count GVAR(notifyQueue) > 1
}, {
// Condition met - Show next notification immediately
LOG("Skipped queue process");
params ["_fnc_popQueue", "_controls", "_fnc_processQueue"];
[_controls, _fnc_processQueue] call _fnc_popQueue;
}, [_fnc_popQueue, _controls, _fnc_processQueue], GVAR(notifyLifetime), {
// Timeout - Normally move to next notification
LOG("Normal queue process");
params ["_fnc_popQueue", "_controls", "_fnc_processQueue"];
[_controls, _fnc_processQueue] call _fnc_popQueue;
}] call CBA_fnc_waitUntilAndExecute;
} else {
[{
params ["_fnc_popQueue", "_controls", "_fnc_processQueue"];
[_controls, _fnc_processQueue] call _fnc_popQueue;
}, [_fnc_popQueue, _controls, _fnc_processQueue], GVAR(notifyLifetime)] call CBA_fnc_waitAndExecute;
};
};
if (count GVAR(notifyQueue) isEqualTo 1) then {
_notification call _fnc_processQueue;
};
nil