diff --git a/boot/helicopter.ks b/boot/helicopter.ks index c3ea3a0..3ce6dc2 100644 --- a/boot/helicopter.ks +++ b/boot/helicopter.ks @@ -2,10 +2,15 @@ runoncepath("0:/lib/boot"). parameter debug is false. +local compiled is List( + "/lib/stabilize_helicopter", + "/lib/ui" +). + Bootstrap( "/boot/helicopter", "/prog/helicopter", -list("/lib/stabilize_helicopter"), -list("/lib/ui"), +compiled, +List(), // no copied files debug ). diff --git a/boot/rocket.ks b/boot/rocket.ks index 63ae57c..05b6bc4 100644 --- a/boot/rocket.ks +++ b/boot/rocket.ks @@ -7,11 +7,11 @@ local compiled is list( "/lib/navigation", "/lib/node", "/lib/stabilize_rocket", + "/lib/ui", "/lib/throttle" ). local copied is list( - "/lib/ui", "/lib/sensors" ). diff --git a/lib/ui.ks b/lib/ui.ks index 15232c0..4b1a5d8 100644 --- a/lib/ui.ks +++ b/lib/ui.ks @@ -1,14 +1,53 @@ -function makeRow { +function MakeRow { parameter p. return p:AddHLayout(). } -function makeButton { +function MakeButton { parameter p. parameter l. - parameter f. + parameter f is {}. local b is p:AddButton(l). set b:onClick to f. return b. } + +function MakeMenu { + parameter stack. // the stack where menu stuff goes + parameter btn. // the button that should show this menu + parameter options. + parameter execLabel. + parameter callback. + parameter preback is { parameter nil. }. + + local top is stack:AddVLayout(). + local sbox is top:AddScrollBox(). + + local optionList is Lex(). + for option in options { + local row is MakeRow(sbox). + local name is option[0]. + local type is option[1]. + local field is 0. + if type = "SCALAR" { + row:AddLabel(name). + set field to row:AddTextField(option[2]). + } else if type = "BOOL" { + set field to row:AddCheckBox(name, option[2]). + } else if type = "RO" { + row:AddLabel(name). + set field to row:AddLabel(option[2]). + } else { + print "WARNING: Unsupported type passed to MakeMenu". + } + optionList:Add(name, field). + } + + set btn:onClick to { + preback(optionList). + stack:ShowOnly(top). + }. + + set top:AddButton(execLabel):onClick to callback:Bind(optionList). +} diff --git a/prog/helicopter.ks b/prog/helicopter.ks index 56a2e7f..3aa1236 100644 --- a/prog/helicopter.ks +++ b/prog/helicopter.ks @@ -4,6 +4,9 @@ // * Main throttle controls collective. runoncepath("lib/stabilize_helicopter"). +runoncepath("lib/ui"). + +clearguis(). global done is false. on AG9 { @@ -11,22 +14,18 @@ on AG9 { return true. } -clearguis(). +// Top-level elements. +local iface is gui(250, 300). +set iface:X to 200. +set iface:Y to 700. +local top is iface:AddVLayout(). +local rows is List(MakeRow(top)). -// Main UI. -local interface is gui(250, 300). -set interface:X to 200. -set interface:Y to 700. - -// Main menu elements in here. -local y is interface:AddVLayout(). -local x is y:AddHLayout(). - -set x:AddButton("HOVER"):onClick to { +set rows[0]:AddButton("HOVER"):onClick to { Hover(). }. -set x:AddButton("LAND"):onClick to { +set rows[0]:AddButton("LAND"):onClick to { // todo: consider a more elaborate landing script that adjusts // velocity as a function of altitude. when SHIP:STATUS = "LANDED" then { @@ -38,6 +37,6 @@ set x:AddButton("LAND"):onClick to { Hover(-5). }. -interface:show(). +iface:show(). wait until false. diff --git a/prog/rocket.ks b/prog/rocket.ks index 052050d..2f60901 100644 --- a/prog/rocket.ks +++ b/prog/rocket.ks @@ -15,16 +15,16 @@ on AG9 { return true. } -// Main UI. +// Top-level menu structure. local iface is gui(250, 300). set iface:X to 200. set iface:Y to 700. - -// Main menu elements in here. local top is iface:AddVLayout(). -local row is makeRow(top). +local rows is list(MakeRow(top), MakeRow(top), MakeRow(top)). +local stk is iface:AddStack(). -local btn is row:AddButton("TERM"). +// Buttons and menus. +local btn is rows[0]:AddButton("TERM"). set btn:TOGGLE to true. set btn:ONTOGGLE to { parameter d. @@ -36,131 +36,91 @@ set btn:ONTOGGLE to { } }. -makeButton(row, "CONF", { stk:ShowOnly(panes["conf"]). }). - -set row to makeRow(top). - -makeButton(row, "HOVER", { Hover(). }). -makeButton(row, "LAND", { Land(). }). - -set row to makeRow(top). - -makeButton(row, "LNCH", { stk:ShowOnly(panes["launch"]). }). - -makeButton(row, "NODE", { - if HASNODE { - set nodeDV:TEXT to NEXTNODE:DELTAV:MAG:ToString. - set nodeBT:TEXT to BurnTime(NEXTNODE:DELTAV:MAG):ToString. - } else { - set nodeDV:TEXT to "No Node". - set nodeBT:TEXT to "No Node". +MakeMenu( + stk, + MakeButton(rows[0], "CONF"), + List( + List("Torque Epsilon Max", "SCALAR", "0.001"), + List("Max Stopping Time", "SCALAR", "2.0") + ), + "Apply", + { + parameter options. + set STEERINGMANAGER:TORQUEEPSILONMAX to options["Torque Epsilon Max"]:TEXT:ToNumber(). + set STEERINGMANAGER:MAXSTOPPINGTIME to options["Max Stopping Time"]:TEXT:ToNumber(). } - stk:ShowOnly(panes["node"]). -}). +). -makeButton(row, "TWR", { stk:ShowOnly(panes["twr"]). }). +MakeButton(rows[1], "HOVER", { Hover(). }). +MakeButton(rows[1], "LAND", { Land(). }). -local stk is iface:AddStack(). -local panes is Lex(). - -// Build launch menu -local top is stk:AddVLayout(). -panes:Add("launch", top). - -local box is top:AddScrollBox(). - -set row to makeRow(box). -row:AddLabel("Target Apoapsis"). -local targetApo is row:AddTextField("80000"). // todo: config params should go into a nested lexicon - -set row to makeRow(box). -row:AddLabel("Atmo TWR"). -local targetTWR is row:AddTextField("1.6"). - -set row to makeRow(box). -row:AddLabel("Initial Pitch"). -local initialPitch is row:AddTextField("85"). - -set row to makeRow(box). -row:AddLabel("Minimum Pitch"). -local minimumPitch is row:AddTextField("5"). - -set row to makeRow(box). -row:AddLabel("Gravity Turn Alt"). -local gravTurnStart is row:AddTextField("8000"). - -set row to makeRow(box). -row:AddLabel("Gravity Turn Pitch"). -local gravPitch is row:AddTextField("75"). - -makeButton(top, "Execute", { - Launch( - targetApo:TEXT:ToNumber(), - targetTWR:TEXT:ToNumber(), - gravTurnStart:TEXT:ToNumber(), - gravPitch:TEXT:ToNumber(), - initialPitch:TEXT:ToNumber(), - minimumPitch:TEXT:ToNumber() - ). -}). -// End Launch Menu - -// Build conf menu -local top is stk:AddVLayout(). -panes:Add("conf", top). - -set box to top:AddScrollbox(). - -set row to makeRow(box). -row:AddLabel("Torque Epsilon Max"). -local maxEpsilon is row:AddTextField("0.001"). - -set row to makeRow(box). -row:AddLabel("Max Stopping Time"). -local maxStopTime is row:AddTextField("2.0"). - -makeButton(top, "Apply", { - set STEERINGMANAGER:TORQUEEPSILONMAX to maxEpsilon:TEXT:ToNumber(). - set STEERINGMANAGER:MAXSTOPPINGTIME to maxStopTime:TEXT:ToNumber(). -}). -// end conf menu - -// twr menu -local top is stk:AddVLayout(). -panes:Add("twr", top). - -set row to makeRow(top). -row:AddLabel("Target TWR"). -local twrLock is row:AddTextField("1.6"). - -makeButton(top, "Lock TWR", { - local done is false. - on AG9 { - set done to true. +MakeMenu( + stk, + MakeButton(rows[2], "LNCH"), + List( + List("Target Apoapsis", "SCALAR", "80000"), + List("Atmo TWR", "SCALAR", "1.8"), + List("Initial Pitch", "SCALAR", "85"), + List("Minimum Pitch", "SCALAR", "5"), + List("Gravity Turn Alt", "SCALAR", "8000"), + List("Gravity Turn Pitch", "SCALAR", "75") + ), + "Execute", + { + parameter options. + Launch( + options["Target Apoapsis"]:TEXT:ToNumber(), + options["Atmo TWR"]:TEXT:ToNumber(), + options["Gravity Turn Alt"]:TEXT:ToNumber(), + options["Gravity Turn Pitch"]:TEXT:ToNumber(), + options["Initial Pitch"]:TEXT:ToNumber(), + options["Minimum Pitch"]:TEXT:ToNumber() + ). } +). - print "Locking throttle to target TWR.". - local tgt is twrLock:TEXT:ToNumber(). - lock THROTTLE to ThrottleToTWR(tgt). - wait until done. - print "Throttle unlocked.". -}). -// end twr menu +MakeMenu( + stk, + MakeButton(rows[2], "NODE"), + List( + List("Node dV", "RO", "0"), + List("Node Burn Time", "RO", "0") + ), + "Execute", + { + parameter options. + ExecNode(). + }, + { + parameter options. + if HASNODE { + set options["Node dV"]:TEXT to NEXTNODE:DELTAV:MAG:ToString. + set options["Node Burn Time"]:TEXT to BurnTime(NEXTNODE:DELTAV:MAG):ToString. + } else { + set options["Node dV"]:TEXT to "No Node". + set options["Node Burn Time"]:TEXT to "No Node". + } + } +). -// node menu -local top is stk:AddVLayout(). -panes:Add("node", top). - -set row to makeRow(top). -row:AddLabel("Node dV"). -local nodeDV is row:AddLabel(). - -set row to makeRow(top). -row:AddLabel("Node Burn Time"). -local nodeBT is row:AddLabel(). - -makeButton(top, "Execute", { ExecNode(). }). -// end node menu +MakeMenu( + stk, + MakeButton(rows[2], "TWR"), + List( + List("Target TWR", "SCALAR", "1.6") + ), + "Lock TWR", + { + parameter options. + local done is false. + print "Locking throttle to target TWR.". + local tgt is options["Target TWR"]:TEXT:ToNumber(). + lock THROTTLE to ThrottleToTWR(tgt). + wait until done. + print "Throttle unlocked.". + set done to false. + } +). iface:show(). wait until false.