2023-09-12 18:58:10 +00:00
#include "..\script_component.hpp"
2023-11-11 14:22:17 +00:00
#include "\a3\ui_f\hpp\defineDIKCodes.inc"
2015-01-11 16:42:31 +00:00
/*
2015-03-20 22:46:21 +00:00
* Author: commy2, Glowbal, PabstMirror
* Draw progress bar and execute given function if succesful.
* Finish/Failure/Conditional are all passed [_args, _elapsedTime, _totalTime, _errorCode]
*
2015-09-18 23:50:17 +00:00
* Arguments:
2017-06-08 13:31:51 +00:00
* 0: Total Time (in game "time" seconds) <NUMBER>
* 1: Arguments, passed to condition, fail and finish <ARRAY>
* 2: On Finish: Code called or STRING raised as event. <CODE, STRING>
* 3: On Failure: Code called or STRING raised as event. <CODE, STRING>
2023-11-11 14:22:17 +00:00
* 4: Localized Title <STRING> (default: "")
* 5: Code to check each frame <CODE> (default: {true})
* 6: Exceptions for checking ace_common_fnc_canInteractWith <ARRAY> (default: [])
* 7: Create progress bar as dialog, this blocks user input <BOOL> (default: true)
2015-03-20 22:46:21 +00:00
*
2015-09-18 23:50:17 +00:00
* Return Value:
2017-06-08 13:31:51 +00:00
* None
2015-03-20 22:46:21 +00:00
*
* Example:
* [5, [], {Hint "Finished!"}, {hint "Failure!"}, "My Title"] call ace_common_fnc_progressBar
2015-09-18 23:50:17 +00:00
*
* Public: Yes
2015-03-20 22:46:21 +00:00
*/
2015-01-11 16:42:31 +00:00
2023-11-11 14:22:17 +00:00
params ["_totalTime", "_args", "_onFinish", "_onFail", ["_localizedTitle", ""], ["_condition", {true}], ["_exceptions", []], ["_dialog", true]];
2015-09-18 23:50:17 +00:00
2015-12-14 12:08:19 +00:00
private _player = ACE_player;
2015-01-26 06:24:33 +00:00
//Open Dialog and set the title
2015-01-11 16:42:31 +00:00
closeDialog 0;
2023-11-11 14:22:17 +00:00
if (_dialog) then {
createDialog QGVAR(ProgressBar_Dialog);
} else {
QGVAR(progressBarDisplay) cutRsc [QGVAR(ProgressBar_Display), "PLAIN"];
};
2015-09-18 23:50:17 +00:00
2023-01-24 19:57:10 +00:00
private _display = uiNamespace getVariable QGVAR(dlgProgress);
2023-02-14 03:57:32 +00:00
// Ensure CBA keybindings are hooked into the display
_display call (uiNamespace getVariable "CBA_events_fnc_initDisplayCurator");
// Hide cursor by using custom transparent cursor
2023-11-11 14:22:17 +00:00
if (_dialog) then {
private _map = _display displayCtrl 101;
_map ctrlMapCursor ["", QGVAR(blank)];
} else { // Add key handler for ESC to cancel
[DIK_ESCAPE, [false, false, false], {
QGVAR(progressBarDisplay) cutText ["", "PLAIN"];
[QGVAR(progressBarKeyHandler), "keydown"] call CBA_fnc_removeKeyHandler;
true
}, "keydown", QGVAR(progressBarKeyHandler)] call CBA_fnc_addKeyHandler;
};
2023-01-24 19:57:10 +00:00
2015-01-26 06:24:33 +00:00
(uiNamespace getVariable QGVAR(ctrlProgressBarTitle)) ctrlSetText _localizedTitle;
2015-01-11 16:42:31 +00:00
2015-04-15 17:55:16 +00:00
//Adjust position based on user setting:
2015-12-14 12:08:19 +00:00
private _ctrlPos = ctrlPosition (uiNamespace getVariable QGVAR(ctrlProgressBarTitle));
2016-05-07 20:14:56 +00:00
_ctrlPos set [1, ((0 + 29 * GVAR(settingProgressBarLocation)) * ((((safezoneW / safezoneH) min 1.2) / 1.2) / 25) + (safezoneY + (safezoneH - (((safezoneW / safezoneH) min 1.2) / 1.2))/2))];
2015-04-17 14:51:43 +00:00
(uiNamespace getVariable QGVAR(ctrlProgressBG)) ctrlSetPosition _ctrlPos;
(uiNamespace getVariable QGVAR(ctrlProgressBG)) ctrlCommit 0;
2015-04-15 17:55:16 +00:00
(uiNamespace getVariable QGVAR(ctrlProgressBar)) ctrlSetPosition _ctrlPos;
(uiNamespace getVariable QGVAR(ctrlProgressBar)) ctrlCommit 0;
2015-04-17 14:51:43 +00:00
(uiNamespace getVariable QGVAR(ctrlProgressBarTitle)) ctrlSetPosition _ctrlPos;
(uiNamespace getVariable QGVAR(ctrlProgressBarTitle)) ctrlCommit 0;
2015-12-14 12:08:19 +00:00
[{
2023-11-11 14:22:17 +00:00
(_this select 0) params ["_args", "_onFinish", "_onFail", "_condition", "_player", "_startTime", "_totalTime", "_exceptions", "_title", "_dialog"];
2015-02-22 13:38:48 +00:00
2016-03-02 10:01:39 +00:00
private _elapsedTime = CBA_missionTime - _startTime;
2015-12-14 12:08:19 +00:00
private _errorCode = -1;
2015-02-14 19:06:55 +00:00
2015-03-22 13:05:31 +00:00
// this does not check: target fell unconscious, target died, target moved inside vehicle / left vehicle, target moved outside of players range, target moves at all.
2015-03-20 22:46:21 +00:00
if (isNull (uiNamespace getVariable [QGVAR(ctrlProgressBar), controlNull])) then {
_errorCode = 1;
2015-01-26 06:24:33 +00:00
} else {
2015-05-31 07:23:36 +00:00
if (ACE_player != _player || !alive _player) then {
2015-03-20 22:46:21 +00:00
_errorCode = 2;
2015-01-28 02:51:12 +00:00
} else {
2015-09-18 23:50:17 +00:00
if !([_args, _elapsedTime, _totalTime, _errorCode] call _condition) then {
2015-03-20 22:46:21 +00:00
_errorCode = 3;
} else {
2015-09-18 23:50:17 +00:00
if !([_player, objNull, _exceptions] call EFUNC(common,canInteractWith)) then {
2015-03-20 22:46:21 +00:00
_errorCode = 4;
} else {
2023-11-11 14:22:17 +00:00
if (!_dialog && {dialog}) then {
_errorCode = 5;
} else {
if (_elapsedTime >= _totalTime) then {
_errorCode = 0;
};
2015-03-20 22:46:21 +00:00
};
};
};
2015-01-26 06:24:33 +00:00
};
};
2015-01-11 16:42:31 +00:00
2015-03-20 22:46:21 +00:00
if (_errorCode != -1) then {
//Error or Success, close dialog and remove PFEH
2015-01-11 16:42:31 +00:00
2015-03-20 22:46:21 +00:00
//Only close dialog if it's the progressBar:
if (!isNull (uiNamespace getVariable [QGVAR(ctrlProgressBar), controlNull])) then {
2023-11-11 14:22:17 +00:00
if (_dialog) then {
closeDialog 0;
} else {
QGVAR(progressBarDisplay) cutText ["", "PLAIN"];
// Remove key handler for non-dialog bar
[QGVAR(progressBarKeyHandler), "keydown"] call CBA_fnc_removeKeyHandler;
};
2015-03-20 22:46:21 +00:00
};
2015-09-18 23:50:17 +00:00
[_this select 1] call CBA_fnc_removePerFrameHandler;
2015-03-20 22:46:21 +00:00
if (_errorCode == 0) then {
2015-11-20 17:40:31 +00:00
if (_onFinish isEqualType "") then {
2016-05-22 15:29:05 +00:00
[_onFinish, [_args, _elapsedTime, _totalTime, _errorCode]] call CBA_fnc_localEvent;
2015-03-20 22:46:21 +00:00
} else {
[_args, _elapsedTime, _totalTime, _errorCode] call _onFinish;
};
} else {
2015-11-20 17:40:31 +00:00
if (_onFail isEqualType "") then {
2016-05-22 15:29:05 +00:00
[_onFail, [_args, _elapsedTime, _totalTime, _errorCode]] call CBA_fnc_localEvent;
2015-03-20 22:46:21 +00:00
} else {
[_args, _elapsedTime, _totalTime, _errorCode] call _onFail;
};
};
2015-01-26 06:24:33 +00:00
} else {
2015-03-20 22:46:21 +00:00
//Update Progress Bar (ratio of elepased:total)
2021-10-10 16:49:41 +00:00
private _ratio = _elapsedTime / _totalTime;
(uiNamespace getVariable QGVAR(ctrlProgressBar)) progressSetPosition _ratio;
switch (GVAR(progressBarInfo)) do {
case 0: {};
case 1: {
(uiNamespace getVariable QGVAR(ctrlProgressBarTitle)) ctrlSetText (_title + format [" (%1", floor (_ratio * 100)] + "%)");
};
case 2: {
(uiNamespace getVariable QGVAR(ctrlProgressBarTitle)) ctrlSetText (_title + " " + format [localize LSTRING(TimeLeft), ceil (_totalTime - _elapsedTime)]);
};
};
2015-01-11 16:42:31 +00:00
};
2023-11-11 14:22:17 +00:00
}, 0, [_args, _onFinish, _onFail, _condition, _player, CBA_missionTime, _totalTime, _exceptions, _localizedTitle, _dialog]] call CBA_fnc_addPerFrameHandler;