swarm/example/pilotmode.sw
Brent Yorgey 599225f4d6
Key input handler (#1214)
Ability to code your own input handler routines.  Closes #102 .  Fixes #1210 .

* Adds a new type `key` to represent keypresses
* Adds a primitive function `key : text -> key` which can handle usual letters, numbers, etc. as well as special keys like `"Down"` etc, as well as modifier key prefixes like `key "A-C-Del"`.  `swarm generate keys` generates a list of all recognized special key names.
* New command `installKeyHandler : text -> (key -> cmd unit) -> cmd unit` which sets the "current key handler".  The `text` value is a hint line to display in the secondary key hints menu while the handler is running.  The global shortcut `M-k` toggles the currently installed handler.
* Add a `keyboard` device to provide these commands, as well as a `key` entity (the recipe for a `keyboard` is 16 `key`s + 1 `board`).
* Add a few examples in the `examples` folder.
* Add an installed `keyboard` to the `building-bridges` challenge.
2023-04-25 16:39:59 +00:00

27 lines
1003 B
XML

def cons : a * b -> (a -> b) -> (a -> b) = \p. \k. \a.
if (a == fst p) {snd p} {k a}
end
def nil : a -> cmd unit = \a. return () end
// Suitable to use as e.g.
// installKeyHandler "(S-)←↓↑→ [Del] [g]rab [h]arvest [d]rill [s]can [b]locked [u]pload" pilot
def pilot : key -> cmd unit =
cons (key "Up", move) $
cons (key "Down", turn back) $
cons (key "Left", turn left) $
cons (key "Right", turn right) $
cons (key "S-Up", turn north) $
cons (key "S-Down", turn south) $
cons (key "S-Left", turn west) $
cons (key "S-Right", turn east) $
cons (key "Del", selfdestruct) $
cons (key "g", res <- grab; log res) $
cons (key "h", res <- harvest; log res) $
cons (key "d", res <- drill forward; case res (\_. return ()) log) $
cons (key "s", res <- scan forward; case res (\_. return ()) log) $
cons (key "b", b <- blocked; if b {log "blocked"} {log "not blocked"}) $
cons (key "u", upload base) $
nil
end