Jump to content
Tostm8

GUIs and Numbers

Recommended Posts

Hello,

I was wondering whether you could create a GUI where the user can input a number? Then that number would be made into a variable that can be used in a script.

 

Thanks!

Share this post


Link to post
Share on other sites

This is a quick snippet for you (with comments):

h = [] spawn {
	//--- Wait until the main display exists
	waitUntil {!isNull findDisplay 46};
	
	//--- Enables us to work with UI data types since we are storing a UI element
	disableSerialization;
	
	_display = (findDisplay 46) createDisplay "RscDisplayEmpty";
	
	//--- Create Edit box
	_ctrlEdit = _display ctrlCreate ["RscEdit", 19998];	
	
	//--- Random width, height etc. not relevant to the overall functionality of this snippet
	private _width = 20 / 108*safeZoneH /(4/3);
	private _height = 4 / 108*safeZoneH;
	private _bufferY = 13 / 108*safeZoneH;
	private _bufferX = ((safeZoneW / 2) - (_width / 2));
	
	//--- Position the Edit box in the bottom centre of the screen
	_ctrlEdit ctrlSetPosition [safeZoneX + _bufferX, safeZoneY + safeZoneH - _bufferY, _width, _height];
	_ctrlEdit ctrlSetBackgroundColor [0,0,0,1];
	_ctrlEdit ctrlCommit 0;
	
	//--- Add KeyUp Eventhandler - Fires after the release of any keyboard key
	_ctrlEdit ctrlAddEventHandler ["KeyUp", {
		params ["_ctrl", "_key"];
		
		//--- Get text from Edit box
		_text = (ctrlText _ctrl);

		//--- poor check to see if input is numeric
		_isValid = !(parseNumber _text isEqualTo 0) || (count (_text splitString "0123456789.-") isEqualTo 0); 

		//--- Check if the input text is valid
		if (_isValid) then {
			_number = parseNumber _text;
			
			hintSilent format ["Input: %1", _number];
			// [_number] execVM "script.sqf"
		} else {
			hintSilent format ["!! Invalid Input !!"];
		};
	}];
	
	//--- Set input focus to the Edit box
	ctrlSetFocus _ctrlEdit;
};

 

  • Like 2

Share this post


Link to post
Share on other sites

Legends! Thanks for the help, both of you. I didn't realise there were UI Event Handlers and that script is just what I need. Thanks again!

Share this post


Link to post
Share on other sites

New question...

 

I want the GUI to remember that I have disabled a button via "ctrlEnable [_id, false];". How would I do that since when I createDialog for the GUI it goes back to normal?

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

×