OoliteProject / oolite

The main Oolite repository.
https://www.oolite.space
543 stars 70 forks source link

Adding ability to define additional keys on a mission screen #438

Closed phkb closed 10 months ago

phkb commented 11 months ago

Presently, the only key a mission screen can watch for is the "enter" key, when the player presses it on a choice. This limits what can be done with UI design.

This PR aims to correct this by allowing any key (including function keys or shift/ctrl/alt combinations) to be registered on a mission screen, with the callback function being called when one is pressed. Two changes are needed for a mission screen to make use of this feature.

  1. Add the "registerKeys" property to the options dictionary. This property lists all the desired keys for the mission screen in a dictionary object. For example:
    this.$openScreen = function () {
    var opts = {
        screenID: "myScreenID",
        title: "Sample Title",
        message: "A sample screen.",
        choices: {"default":{"text":"Press ESC to exit"}},
        registerKeys: {"escape":[{key:"esc"}]}
    };
    mission.runScreen(opts, this.$myScreenHandler.bind(this), this);
    }
  2. Add the "keyPress" parameter to the callback function. This parameter is in addition to the existing "choice" parameter. It will contain the dictionary key name for the registered key. For example:
    this.$myScreenHandler = function(choice, keyPress) {
    if (keyPress != "escape") this.$openScreen(); 
    }

I've put together 3 examples demonstrating the new functionality: https://app.box.com/s/hinwftyta6krpensmygi1ydcbd2e9xw7 Uses the arrow keys to enter a 4 digit code. https://app.box.com/s/cxdeap7fnlakui57vyoebdyelsh247iz Allows character entry to search for systems on the chart. https://app.box.com/s/w2405pvgs2dlxq22y154ubwjx0vhxchr Recreates the F3F3 Shipyard, responding to the arrow up/down keys to change the displayed ship.

phkb commented 10 months ago

If there’s no other issues I’ll merge this tomorrow. Let me if you’d like me to hold fire.