Jump to content
cybercoco

Create a progress bar depending on a var's value

Recommended Posts

I get the counter from 0 to 100 % in ~46 secs

Then if I do it again without restarting the scenario, I get 20sec ???!!!

 

0.01 x 200 is ~ 2 seconds, keep in mind that sleep is not 100% accurate.

 

If you want it to take longer, then increase the counter.

Share this post


Link to post
Share on other sites

This code lasts longer than expected. I would like a total duration of 20 sec (the duration of the animation played at the same time). How ?

with uiNamespace do {
  my_awesome_progressBar = findDisplay 46 ctrlCreate ["RscProgress", -1];
  my_awesome_progressBar ctrlSetPosition [ 0.345, 0.3 ];
  my_awesome_progressBar progressSetPosition 0;
  my_awesome_progressBar ctrlCommit 0;
  my_awesome_progressBar ctrlSetTextColor [0.4,0.804,0,1];
};

_counter = 200;
for "_i" from 0 to _counter do {
  (uiNamespace getVariable "my_awesome_progressBar") progressSetPosition (_i/_counter);

  //hintSilent format ["%1%2", _i * 100 /_counter, "%"];
    with uiNamespace do {
        x = safezoneX + 0 * safezoneW;
        y = safezoneY + 0 * safezoneH;
        w = safezoneW * 0.8;
        h = safezoneH * 0.8;
            my_awesome_text = findDisplay 46 ctrlCreate ["RscStructuredText", -1];
            my_awesome_text ctrlSetPosition [x,y,w,h];
            my_awesome_text ctrlCommit 0;
            my_awesome_text ctrlSetStructuredText parseText format["%1%2", round(_i * 100 /_counter), "%"];
    };
  sleep 0.1;
  ctrlDelete (uiNamespace getVariable "my_awesome_text");
};

ctrlDelete (uiNamespace getVariable "my_awesome_progressBar");


Share this post


Link to post
Share on other sites
with uiNamespace do {
  my_awesome_progressBar = findDisplay 46 ctrlCreate ["RscProgress", -1];
  my_awesome_progressBar ctrlSetPosition [ 0.345, 0.3 ];
  my_awesome_progressBar progressSetPosition 0;
  my_awesome_progressBar ctrlCommit 0;
  my_awesome_progressBar ctrlSetTextColor [0.4,0.804,0,1];
};

_counter = 200;
_startTime = time;
for "_i" from 1 to _counter do {
  (uiNamespace getVariable "my_awesome_progressBar") progressSetPosition (_i/_counter);

  hintSilent format ["%1%2", _i * 100 /_counter, "%"]; // debugg
  sleep 0.1; // interval
};

_time = time - _startTime;
hintSilent format ["Time: %1",_time];

ctrlDelete (uiNamespace getVariable "my_awesome_progressBar");

I added another line to show you the time it takes. Sleep becomes really unreliable the higher the counter number is. I'd try to reduce the counter and increase the sleep time. Though that will cause the progress bar to appear more laggy. You'd need to make workaround for that.

Share this post


Link to post
Share on other sites

Well, I thought you tested his aswell.

Share this post


Link to post
Share on other sites

I'm saying that because his code doesn't utilise the sleep cmmand. So it won't be desync,and will be 20 sec, right ?

Share this post


Link to post
Share on other sites

Yeah, I looked at larrow snippet and it should be exactly 20 seconds. Extremly clever what larrow did there, took me a few seconds to figure it out.

Share this post


Link to post
Share on other sites

Here it is :

with uiNamespace do {
    my_awesome_progressBar = findDisplay 46 ctrlCreate ["RscProgress", -1];
    my_awesome_progressBar ctrlSetPosition [ 0, 0.3 ];
    my_awesome_progressBar progressSetPosition 0;
    my_awesome_progressBar ctrlCommit 0;

// that's where it begins !
    [ "TIMER", "onEachFrame", {
        params[ "_start", "_end" ];
        _progress = linearConversion[ _start, _end, time, 0, 1 ];
        (uiNamespace getVariable "my_awesome_progressBar") progressSetPosition _progress;
        if ( _progress > 1 ) then {
            [ "TIMER", "onEachFrame" ] call BIS_fnc_removeStackedEventHandler;
        };
    }, [ time, time + 20 ] ] call BIS_fnc_addStackedEventHandler; // does this set the maximum ?
};

Quite frankly, i can't understand how he did it ! Is he using time has the interval for the loop ?

Share this post


Link to post
Share on other sites

Quite frankly, i can't understand how he did it ! Is he using time has the interval for the loop ?

Yes.

This [ time, time + 20 ] is the start and end, so now(time) end in 20 seconds from now.

Then onEachFrame it works out much time has passed and converts it to a 0-1 value..

_progress = linearConversion[ _start, _end, time, 0, 1 ];

So where is time currently between start and end as a range of 0-1.

 

Maybe it would look clearer if written like this..

start = time;
lastFor = 20;

onEachFrame {
    _currentPos = time - start;
    _progress = _currentPos / lastFor;
    (uiNamespace getVariable "my_awesome_progressBar") progressSetPosition _progress;
    if ( _progress > 1 ) then {
        //Remove OEF as we have reached the end
        onEachFrame {};
    };
};
but instead is done in a stacked event handler so as not to destroy any other OEFs that maybe running.

Share this post


Link to post
Share on other sites

 

Yes.

This [ time, time + 20 ] is the start and end, so now(time) end in 20 seconds from now.

Then onEachFrame it works out much time has passed and converts it to a 0-1 value..

_progress = linearConversion[ _start, _end, time, 0, 1 ];

So where is time currently between start and end as a range of 0-1.

 

Maybe it would look clearer if written like this..

start = time;
lastFor = 20;

onEachFrame {
    _currentPos = time - start;
    _progress = _currentPos / lastFor;
    (uiNamespace getVariable "my_awesome_progressBar") progressSetPosition _progress;
    if ( _progress > 1 ) then {
        //Remove OEF as we have reached the end
        onEachFrame {};
    };
};
but instead is done in a stacked event handler so as not to destroy any other OEFs that maybe running.

 

Thank you very much, I understand now with this code version.

Share this post


Link to post
Share on other sites

1°) Is it supposed to delete itself after 20 sec ? (Because it doesn't...)

 

2°) I've tried changing the size of the bar with some modif, but they don't work :

with uiNamespace do {
    my_awesome_progressBar = findDisplay 46 ctrlCreate ["RscProgress", -1];
    my_awesome_progressBar ctrlSetPosition [ 0, 0.3 ];
    my_awesome_progressBar progressSetPosition 0;
    my_awesome_progressBar ctrlCommit 0;

    [ "TIMER", "onEachFrame", {
        params[ "_start", "_end" ];
        _progress = linearConversion[ _start, _end, time, 0, 5 ]; // here
        (uiNamespace getVariable "my_awesome_progressBar") progressSetPosition _progress;
        if ( _progress > 5 ) then { //and there
            [ "TIMER", "onEachFrame" ] call BIS_fnc_removeStackedEventHandler;
        };
    }, [ time, time + 20 ] ] call BIS_fnc_addStackedEventHandler;
};

Also tried with ctrlSetScale

my_awesome_progressBar ctrlSetScale 2;

But it has no effect on the bar !

 

Thanks

Share this post


Link to post
Share on other sites

1.) It's not

2.) Use

my_awesome_progressBar ctrlCommit 0;

after every command like ctrlSetScale or ctrlSetPosition (which you can also use to resize)

Share this post


Link to post
Share on other sites

1°)

//  What's the use of :
call BIS_fnc_removeStackedEventHandler
// Made it worked with :
ctrlDelete (uiNamespace getVariable "my_awesome_progressBar");

-----------------------------------------------------------------------------------------------------------------------------------------

 

2°) ctrlSetScale with ctrlCommit works great, thanks !

 

my_awesome_progressBar ctrlCommit 0;

after every command like ctrlSetScale or ctrlSetPosition (which you can also use to resize)

 

You mean resizing with ctrlSetPosition ? Like that ?

controlName ctrlSetPosition [x, y, w, h]

Share this post


Link to post
Share on other sites
call BIS_fnc_removeStackedEventHandler

It removes the "onEachFrame" eventHandler, which would otherwise continue to run even after the progress bar is filled.

You mean resizing with ctrlSetPosition ? Like that ?

yes!

Share this post


Link to post
Share on other sites
You mean resizing with ctrlSetPosition ? Like that ?

yes!

All right !

 

 

 I tried to output the % process, it works with hint format. However it doesn't work with another dialog (an "embeded" one showing the text).

with uiNamespace do {
	my_awesome_progressBar = findDisplay 46 ctrlCreate ["RscProgress", -1];
	my_awesome_progressBar ctrlSetPosition [ 0.345, 0.3 ];
	my_awesome_progressBar progressSetPosition 0;
	my_awesome_progressBar ctrlCommit 0;
	
	
    [ "TIMER", "onEachFrame", {
        params[ "_start", "_end" ];
        _progress = linearConversion[ _start, _end, time, 0, 1 ];
        (uiNamespace getVariable "my_awesome_progressBar") progressSetPosition _progress;
		
		//hintsilent format ["%1%2", round(100*_progress), "%"];
		
                // Not working !!!
		with uiNamespace do {
				my_awesome_text = findDisplay 46 ctrlCreate ["RscStructuredText", -1];
				my_awesome_text ctrlSetPosition [0,0,1,0.1];
				my_awesome_text ctrlCommit 0;
				my_awesome_text ctrlSetStructuredText parseText format ["%1%2", round(100*_progress), "%"];
			};
		sleep 0.1;
	        ctrlDelete (uiNamespace getVariable "my_awesome_text");
		
		if ( _progress > 1 ) then {
            [ "TIMER", "onEachFrame" ] call BIS_fnc_removeStackedEventHandler;
			ctrlDelete (uiNamespace getVariable "my_awesome_progressBar");
			hint "FINISHED";
        };
    }, [ time, time + 20 ] ] call BIS_fnc_addStackedEventHandler;
};

Share this post


Link to post
Share on other sites

Another test working, but I can't manage to delete the my_Awesome_Text with ctrlDelete ...

with uiNamespace do {
	my_awesome_progressBar = findDisplay 46 ctrlCreate ["RscProgress", -1];
	my_awesome_progressBar ctrlSetPosition [ 0.345, 0.3 ];
	my_awesome_progressBar progressSetPosition 0;
	my_awesome_progressBar ctrlCommit 0;
	
	
    [ "TIMER", "onEachFrame", {
        params[ "_start", "_end" ];
        _progress = linearConversion[ _start, _end, time, 0, 1 ];
        (uiNamespace getVariable "my_awesome_progressBar") progressSetPosition _progress;
		
		with uiNamespace do {
			my_awesome_text = findDisplay 46 ctrlCreate ["RscStructuredText", -1];
			my_awesome_text ctrlSetPosition [ 0.345, 0.3 ]; //ctrlSetPosition [0,0,1,0.1]; //[0.5,0.1]; //
			my_awesome_text ctrlCommit 0;
			my_awesome_text ctrlSetStructuredText parseText format["%1%2", round(100*_progress), "%"];
		};
		// ctrlDelete (uiNamespace getVariable "my_awesome_text");	
		if ( _progress > 1 ) then {
            [ "TIMER", "onEachFrame" ] call BIS_fnc_removeStackedEventHandler;
			ctrlDelete (uiNamespace getVariable "my_awesome_progressBar");
			hint "FINISHED";
		
        };
    }, [ time, time + 20 ] ] call BIS_fnc_addStackedEventHandler;
};

Basically, this code outputs a % value and repeat the progress to 100%, the values overlay on each other !

Share this post


Link to post
Share on other sites

No need to create and destroy a UI element every frame. Just make it once like you do the progress bar, update its text each frame and delete it once finished.

with uiNamespace do {
	my_awesome_progressBar = findDisplay 46 ctrlCreate ["RscProgress", -1];
	my_awesome_progressBar ctrlSetPosition [ 0.345, 0.3 ];
	my_awesome_progressBar progressSetPosition 0;
	my_awesome_progressBar ctrlCommit 0;
	
	my_awesome_text = findDisplay 46 ctrlCreate ["RscStructuredText", -1];
	my_awesome_text ctrlSetPosition [ 0.345, 0.3 ];
	my_awesome_text ctrlCommit 0;
};

[ "TIMER", "onEachFrame", {
    params[ "_start", "_end" ];
    _progress = linearConversion[ _start, _end, time, 0, 1 ];
    (uiNamespace getVariable "my_awesome_progressBar") progressSetPosition _progress;
	(uiNamespace getVariable "my_awesome_text") ctrlSetStructuredText parseText format["%1%2", round(100*_progress), "%"];

	if ( _progress > 1 ) then {
		[ "TIMER", "onEachFrame" ] call BIS_fnc_removeStackedEventHandler;
		ctrlDelete (uiNamespace getVariable "my_awesome_progressBar");
		ctrlDelete (uiNamespace getVariable "my_awesome_text");
		hint "FINISHED";
	};
}, [ time, time + 20 ] ] call BIS_fnc_addStackedEventHandler;

Share this post


Link to post
Share on other sites
_test = "hello";

with uiNamespace do {
	my_awesome_progressBar = findDisplay 46 ctrlCreate ["RscProgress", -1];
	my_awesome_progressBar ctrlSetPosition [ 0.345, 0.3 ];
	my_awesome_progressBar progressSetPosition 0;
	my_awesome_progressBar ctrlCommit 0;
	
	my_awesome_text = findDisplay 46 ctrlCreate ["RscStructuredText", -1];
	my_awesome_text ctrlSetPosition [ 0.345, 0.3 ];
	my_awesome_text ctrlCommit 0;
};

[ "TIMER", "onEachFrame", {
    params[ "_start", "_end" ];
    _progress = linearConversion[ _start, _end, time, 0, 1 ];
    (uiNamespace getVariable "my_awesome_progressBar") progressSetPosition _progress;
	(uiNamespace getVariable "my_awesome_text") ctrlSetStructuredText parseText format["%1%2", round(100*_progress), "%"];

	if ( _progress > 1 ) then {
		[ "TIMER", "onEachFrame" ] call BIS_fnc_removeStackedEventHandler;
		ctrlDelete (uiNamespace getVariable "my_awesome_progressBar");
		ctrlDelete (uiNamespace getVariable "my_awesome_text");
		/////
                hint format ["%1", _test]; // HERE !
                /////
	};
}, [ time, time + 20 ] ] call BIS_fnc_addStackedEventHandler;

How can I present the var as a param and make the hint format work properly ?

Share this post


Link to post
Share on other sites

_test = "hello";

with uiNamespace do {

my_awesome_progressBar = findDisplay 46 ctrlCreate ["RscProgress", -1];

my_awesome_progressBar ctrlSetPosition [ 0.345, 0.3 ];

my_awesome_progressBar progressSetPosition 0;

my_awesome_progressBar ctrlCommit 0;

my_awesome_text = findDisplay 46 ctrlCreate ["RscStructuredText", -1];

my_awesome_text ctrlSetPosition [ 0.345, 0.3 ];

my_awesome_text ctrlCommit 0;

};

[ "TIMER", "onEachFrame", {

params[ "_start", "_end", "_test" ]; //<<<<< retrieve passed _test

_progress = linearConversion[ _start, _end, time, 0, 1 ];

(uiNamespace getVariable "my_awesome_progressBar") progressSetPosition _progress;

(uiNamespace getVariable "my_awesome_text") ctrlSetStructuredText parseText format["%1%2", round(100*_progress), "%"];

if ( _progress > 1 ) then {

[ "TIMER", "onEachFrame" ] call BIS_fnc_removeStackedEventHandler;

ctrlDelete (uiNamespace getVariable "my_awesome_progressBar");

ctrlDelete (uiNamespace getVariable "my_awesome_text");

hint format ["%1", _test];

};

}, [ time, time + 20, _test ] ] call BIS_fnc_addStackedEventHandler; //<<<<<< send _test into OEF code

  • Like 1

Share this post


Link to post
Share on other sites
On 1/24/2016 at 9:21 PM, Larrow said:

 


_test = "hello";

with uiNamespace do {

my_awesome_progressBar = findDisplay 46 ctrlCreate ["RscProgress", -1];

my_awesome_progressBar ctrlSetPosition [ 0.345, 0.3 ];

my_awesome_progressBar progressSetPosition 0;

my_awesome_progressBar ctrlCommit 0;

my_awesome_text = findDisplay 46 ctrlCreate ["RscStructuredText", -1];

my_awesome_text ctrlSetPosition [ 0.345, 0.3 ];

my_awesome_text ctrlCommit 0;

};

[ "TIMER", "onEachFrame", {

params[ "_start", "_end", "_test" ]; //<<<<< retrieve passed _test

_progress = linearConversion[ _start, _end, time, 0, 1 ];

(uiNamespace getVariable "my_awesome_progressBar") progressSetPosition _progress;

(uiNamespace getVariable "my_awesome_text") ctrlSetStructuredText parseText format["%1%2", round(100*_progress), "%"];

if ( _progress > 1 ) then {

[ "TIMER", "onEachFrame" ] call BIS_fnc_removeStackedEventHandler;

ctrlDelete (uiNamespace getVariable "my_awesome_progressBar");

ctrlDelete (uiNamespace getVariable "my_awesome_text");

hint format ["%1", _test];

};

}, [ time, time + 20, _test ] ] call BIS_fnc_addStackedEventHandler; //<<<<<< send _test into OEF code

 

Hello! Sorry for necroing this but:

 

How can I create this same progress bar but in reverse?

 

(It starts at 100% and then gets to 0% in 20 seconds then disappears)?

Share this post


Link to post
Share on other sites
55 minutes ago, LSValmont said:

How can I create this same progress bar but in reverse?

 

(It starts at 100% and then gets to 0% in 20 seconds then disappears)?

Just reverse the linearConversion, either swap _start and _end OR 0 and 1.

_progress = linearConversion[ _start, _end, time, 1, 0 ]; 

And change the if statement to <= 0

  • Like 1

Share this post


Link to post
Share on other sites

Using the new MEH instead.

with uiNamespace do {
	my_awesome_progressBar = findDisplay 46 ctrlCreate ["RscProgress", -1];
	my_awesome_progressBar ctrlSetPosition [ 0.345, 0.3 ];
	my_awesome_progressBar progressSetPosition 1;
	my_awesome_progressBar ctrlCommit 0;
  
	my_awesome_text = findDisplay 46 ctrlCreate ["RscStructuredText", -1];
	my_awesome_text ctrlSetPosition [ 0.345, 0.3 ];
	my_awesome_text ctrlCommit 0;
};

//           Start, End,       Text on finished
TAG_timer = [ time, time + 20, "Timer finished" ];

addMissionEventHandler [ "EachFrame", {
	TAG_timer params[ "_start", "_end", "_text" ];
	
	_progress = linearConversion[ _start, _end, time, 1, 0 ];
	
	(uiNamespace getVariable "my_awesome_progressBar") progressSetPosition _progress;
	(uiNamespace getVariable "my_awesome_text") ctrlSetStructuredText parseText format["%1%2", round(100*_progress), "%"];
	
	if ( _progress < 0 ) then {
		removeMissionEventHandler[ "EachFrame", _thisEventHandler ];
		ctrlDelete (uiNamespace getVariable "my_awesome_progressBar");
		ctrlDelete (uiNamespace getVariable "my_awesome_text");
		hint _text;
	};
}];

 

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
11 minutes ago, Larrow said:

Using the new MEH instead.

Spoiler




with uiNamespace do {
	my_awesome_progressBar = findDisplay 46 ctrlCreate ["RscProgress", -1];
	my_awesome_progressBar ctrlSetPosition [ 0.345, 0.3 ];
	my_awesome_progressBar progressSetPosition 1;
	my_awesome_progressBar ctrlCommit 0;
  
	my_awesome_text = findDisplay 46 ctrlCreate ["RscStructuredText", -1];
	my_awesome_text ctrlSetPosition [ 0.345, 0.3 ];
	my_awesome_text ctrlCommit 0;
};

//           Start, End,       Text on finished
TAG_timer = [ time, time + 20, "Timer finished" ];

addMissionEventHandler [ "EachFrame", {
	TAG_timer params[ "_start", "_end", "_text" ];
	
	_progress = linearConversion[ _start, _end, time, 1, 0 ];
	
	(uiNamespace getVariable "my_awesome_progressBar") progressSetPosition _progress;
	(uiNamespace getVariable "my_awesome_text") ctrlSetStructuredText parseText format["%1%2", round(100*_progress), "%"];
	
	if ( _progress < 0 ) then {
		removeMissionEventHandler[ "EachFrame", _thisEventHandler ];
		ctrlDelete (uiNamespace getVariable "my_awesome_progressBar");
		ctrlDelete (uiNamespace getVariable "my_awesome_text");
		hint format ["%1", _text];
	};
}];


 

 

 

Thank you very much Larrow.

 

Works like a charm!

  • Like 2

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×