conditional form?

  • abief
  • abief's Avatar Offline Autor
  • Premium Mitglied
  • Premium Mitglied
  • I'm here, there and elsewhere!
  • Beiträge: 95
  • Dank erhalten: 19

conditional form? wurde erstellt von abief

Posted 04 Apr. 2018 06:41 #1
Hi,

can somebody provide a sample on how to build a conditional form?

My code:

local function setupForm(formId)
-- load sensors
sensorsAvailable = {}
local available = system.getSensors();
local formSensors={}
local formSensorsIndex=-1
for index,sensor in ipairs(available) do
if(sensor.param == 0 and
(string.sub(sensor.label, 1, 3) == _MUI or
string.sub(sensor.label, 1, 5) == _MEZON)
) then
formSensors[#formSensors+1] = sensor.label
sensorsAvailable[#sensorsAvailable+1] = sensor
if(sensor.id==sensorId ) then
formSensorsIndex=#sensorsAvailable
end
end
end

-- form title
form.setTitle(locale.SetupFormTitle);
-- spacer
form.addSpacer(318,7);
-- sensor (MUI or MEZON)
form.addRow(2);
form.addLabel({label = locale.SetupFormSelectSensorLabel, width = 200});
form. addSelectbox(formSensors, formSensorsIndex, true, sensorChanged);
-- only for MEZON, we add the gearbox ratio
if (sensorMode == _MEZ) then
form.addRow(2);
form.addLabel({label = locale.SetupFormGearBoxValueLabel, width = 200});
form.addIntbox(revolutionsGearboxRatio, 100, 1000, 100, 0, 1, gearboxRatioChanged);
end
-- battery
form.addRow(2);
form.addLabel({label = locale.SetupFormSelectBatteryLabel, width = 200});
form.addIntbox(batteryCapacity, 0, 32000, 0, 0, 10, batteryChanged);
-- spacer
form.addSpacer(318,7);
-- gates
form.addRow(3);
form.addLabel({label = locale.SetupFormRedGateLabel, width = 150});
form.addIntbox(redGate, 0, 100, _REDGATE, 0, 1, redGateChanged);
form.addAudioFilebox(alarmRedGate, alarmRedGateChanged);
form.addRow(3);
form.addLabel({label = locale.SetupFormAmberGateLabel, width = 150});
form.addIntbox(amberGate, 0, 100, _AMBERGATE, 0, 1, amberGateChanged);
form.addAudioFilebox(alarmAmberGate, alarmAmberGateChanged);
form.addRow(3);
form.addLabel({label = locale.SetupFormBlueGateLabel, width = 150});
form.addIntbox(blueGate, 0, 100, _BLUEGATE, 0, 1, blueGateChanged);
form.addAudioFilebox(alarmBlueGate, alarmBlueGateChanged);
collectgarbage();
end

-- print Configuration form
local function printForm(formId)
end

My idea is that if sensorMode is Mezon, then the gearbox ratio parameter shows up, if not, this gearbox ratio is not used.

but I'm not able to make this field to disappear when sensorMode is not mezon.

Suggestions??

Thanks,
Field ready: Taborca (3.4m), Ocelot (1.6m), Alula (0.6m).
Factory line: FVT-3 (1m), A6M Zero (1.6m), Tiger Moth (2.2m), Grumann Goose (2m) and Beechcraft D18 (1.5m).
Design phase: Szd-20x Wampir II (7.5m), Rutan Quickie (1.6m), both parked.
Dreaming: Weird push-pull called Double Trouble (2.4m)
von abief

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

  • TeroS
  • TeroS's Avatar Offline
  • Platinum Mitglied
  • Platinum Mitglied
  • RC-Thoughts
  • Beiträge: 527
  • Dank erhalten: 370

TeroS antwortete auf conditional form?

Posted 07 Apr. 2018 11:44 #2
Please put the code inside code tags so it's more readable.
Code:
[code type="" ] [/code ]
(Remove spaces before the "]", or more simply, use the <> button in editors buttonline above)

There have been issues with this forums colors with code tag, I hope they will get it fixed sometime...

But an answer, you should look in reinit function and use some if-statements.
Last Edit:07 Apr. 2018 11:45 von TeroS
Letzte Änderung: 07 Apr. 2018 11:45 von TeroS.

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

  • TeroS
  • TeroS's Avatar Offline
  • Platinum Mitglied
  • Platinum Mitglied
  • RC-Thoughts
  • Beiträge: 527
  • Dank erhalten: 370

TeroS antwortete auf conditional form?

Posted 07 Apr. 2018 11:47 #3
So here's your code:
Code:
local function setupForm(formId) -- load sensors sensorsAvailable = {} local available = system.getSensors(); local formSensors={} local formSensorsIndex=-1 for index,sensor in ipairs(available) do if(sensor.param == 0 and (string.sub(sensor.label, 1, 3) == _MUI or string.sub(sensor.label, 1, 5) == _MEZON) ) then formSensors[#formSensors+1] = sensor.label sensorsAvailable[#sensorsAvailable+1] = sensor if(sensor.id==sensorId ) then formSensorsIndex=#sensorsAvailable end end end -- form title form.setTitle(locale.SetupFormTitle); -- spacer form.addSpacer(318,7); -- sensor (MUI or MEZON) form.addRow(2); form.addLabel({label = locale.SetupFormSelectSensorLabel, width = 200}); form. addSelectbox(formSensors, formSensorsIndex, true, sensorChanged); -- only for MEZON, we add the gearbox ratio if (sensorMode == _MEZ) then form.addRow(2); form.addLabel({label = locale.SetupFormGearBoxValueLabel, width = 200}); form.addIntbox(revolutionsGearboxRatio, 100, 1000, 100, 0, 1, gearboxRatioChanged); end -- battery form.addRow(2); form.addLabel({label = locale.SetupFormSelectBatteryLabel, width = 200}); form.addIntbox(batteryCapacity, 0, 32000, 0, 0, 10, batteryChanged); -- spacer form.addSpacer(318,7); -- gates form.addRow(3); form.addLabel({label = locale.SetupFormRedGateLabel, width = 150}); form.addIntbox(redGate, 0, 100, _REDGATE, 0, 1, redGateChanged); form.addAudioFilebox(alarmRedGate, alarmRedGateChanged); form.addRow(3); form.addLabel({label = locale.SetupFormAmberGateLabel, width = 150}); form.addIntbox(amberGate, 0, 100, _AMBERGATE, 0, 1, amberGateChanged); form.addAudioFilebox(alarmAmberGate, alarmAmberGateChanged); form.addRow(3); form.addLabel({label = locale.SetupFormBlueGateLabel, width = 150}); form.addIntbox(blueGate, 0, 100, _BLUEGATE, 0, 1, blueGateChanged); form.addAudioFilebox(alarmBlueGate, alarmBlueGateChanged); collectgarbage(); end -- print Configuration form local function printForm(formId) end

With a quick look I don't see why that would not work with some adjustment. Where and when do you set the "sensorMode" to "_MEZ"? Have a look in LUA API for "form.reinit" :)
Last Edit:07 Apr. 2018 11:52 von TeroS
Letzte Änderung: 07 Apr. 2018 11:52 von TeroS.
Folgende Benutzer bedankten sich: abief

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

  • abief
  • abief's Avatar Offline Autor
  • Premium Mitglied
  • Premium Mitglied
  • I'm here, there and elsewhere!
  • Beiträge: 95
  • Dank erhalten: 19

abief antwortete auf conditional form?

Posted 07 Apr. 2018 12:17 #4
Ok, I solved it myself.

I'm not sure if this is the right solution, but it works.

In my case, the reason why I need the form to add a parameter when I use a MEZON sensor, to determine the gearbox setting.

Basically, in the setup form I want to be able to switch from



to



I have a variable, called sensorMode, that determines if the sensor is a MUI or a MEZON. If Mezon is active, there is a revolutions parameter that I need to determine and apply the gearbox reduction in order to obtain the actual propeller revolutions.

first, I added a local variable at the start of the lua code:
Code:
local previousSensorMode = sensorMode; -- to change the form

second, I slightly change the code in my setup form. so to skip the gearbox field if sensor mode is not MEZON...
Code:
-- only for MEZON, we add the gearbox ratio if (sensorMode == _MEZ) then form.addRow(2); form.addLabel({label = locale.SetupFormGearBoxValueLabel, width = 200}); form.addIntbox(revolutionsGearboxRatio, 100, 1000, 100, 2, 1, gearboxRatioChanged); form.addSpacer(318,7); end

now, as this code is present in the setupform function, it's only called once, when we setup the form.

So, I added some code also the the sensorChanged function (that is, the code associated to the change of sensors between MUI and MEZON).
Code:
local function sensorChanged(value) previousSensorMode = sensorMode; -- some more code here end

this way I know what was the previous value of sensormode.

Lastly, I add a function to my code, and that is the printform

I change my definition of the setup of the form by adding the proper calls
Code:
system.registerForm(1, MENU_APPS, locale.AppName, setupForm, nil, printForm);

and I complete the printform function.

this is called as the loop, every 22ms...
Code:
-- print Configuration form local function printForm() if (sensorMode ~= previousSensorMode) then previousSensorMode = sensorMode; form.reinit(); end end

the important line is
Code:
form.reinit();

this line basically respawns the initform code, so the form is effectively regenerated.

hope it helps
Field ready: Taborca (3.4m), Ocelot (1.6m), Alula (0.6m).
Factory line: FVT-3 (1m), A6M Zero (1.6m), Tiger Moth (2.2m), Grumann Goose (2m) and Beechcraft D18 (1.5m).
Design phase: Szd-20x Wampir II (7.5m), Rutan Quickie (1.6m), both parked.
Dreaming: Weird push-pull called Double Trouble (2.4m)
Last Edit:07 Apr. 2018 12:27 von abief

Anhänge:

Letzte Änderung: 07 Apr. 2018 12:27 von abief.

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

  • abief
  • abief's Avatar Offline Autor
  • Premium Mitglied
  • Premium Mitglied
  • I'm here, there and elsewhere!
  • Beiträge: 95
  • Dank erhalten: 19

abief antwortete auf conditional form?

Posted 07 Apr. 2018 12:28 #5

TeroS wrote: So here's your code:

--

With a quick look I don't see why that would not work with some adjustment. Where and when do you set the "sensorMode" to "_MEZ"? Have a look in LUA API for "form.reinit" :)


thanks, we arrived to the same conclusion!
Field ready: Taborca (3.4m), Ocelot (1.6m), Alula (0.6m).
Factory line: FVT-3 (1m), A6M Zero (1.6m), Tiger Moth (2.2m), Grumann Goose (2m) and Beechcraft D18 (1.5m).
Design phase: Szd-20x Wampir II (7.5m), Rutan Quickie (1.6m), both parked.
Dreaming: Weird push-pull called Double Trouble (2.4m)
von abief

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Moderatoren: ThornIG-Modellbau
Ladezeit der Seite: 0.980 Sekunden