Changeset 25023

Timestamp:
Mar 6, 2021, 7:11:41 PM (3 years ago)
Author:
wraitii
Message:

Customize hotkey categories & ordering, prepare for translation.

  • adds JSON files to specify the human-readable categorization, name and description of hotkeys.
  • clean up code

Translation support via messages.json not yet added - the strings need another pass and that avoids translators doing redundant work. See D3641

Refs #5867, Refs #5996

Differential Revision: https://code.wildfiregames.com/D3596

Location:
ps/trunk/binaries/data/mods/public/gui/hotkeys
Files:
8 added
3 edited

Legend:

Unmodified
Added
Removed
  • ps/trunk/binaries/data/mods/public/gui/hotkeys/HotkeyPicker.js

    r24215 r25023  
    55class HotkeyPicker
    66{
    7     constructor(onClose, name, combinations)
     7    constructor(onClose, name, combinations)
    88    {
     9
    910        this.name = name;
    1011        this.combinations = combinations;
     
    1415        this.enteringInput = -1;
    1516
    16         Engine.GetGUIObjectByName("hotkeyPickerTitle").caption = translate(this.name);
     17        if (this.metadata.hotkeys[name])
     18        {
     19            Engine.GetGUIObjectByName("hotkeyPickerTitle").caption = translate(this.metadata.hotkeys[name].name);
     20            Engine.GetGUIObjectByName("hotkeyPickerDescHotkey").caption = translate(this.metadata.hotkeys[name].desc);
     21        }
     22        else
     23        {
     24            Engine.GetGUIObjectByName("hotkeyPickerTitle").caption = this.name;
     25            Engine.GetGUIObjectByName("hotkeyPickerDescHotkey").hidden = true;
     26        }
    1727
    1828        this.setupCombinations();
     
    4252        {
    4353            let s = Engine.GetGUIObjectByName("combination[" + i + "]").size;
    44             s.top = +i * 60 + 90;
    45             s.bottom = +i * 60 + 120;
     54            s.top = +i * 60 + 0;
     55            s.bottom = +i * 60 + 10;
    4656            Engine.GetGUIObjectByName("combination[" + i + "]").size = s;
    4757            Engine.GetGUIObjectByName("combNb[" + i + "]").caption = sprintf(translate("#%i"), i);
  • ps/trunk/binaries/data/mods/public/gui/hotkeys/HotkeysPage.js

    r24793 r25023  
    11class HotkeysPage
    22{
    3     constructor()
     3    constructor()
    44    {
     5
     6
    57        Engine.GetGUIObjectByName("hotkeyList").onMouseLeftDoubleClickItem = () => {
    68            let idx = Engine.GetGUIObjectByName("hotkeyList").selected;
    79            let picker = new HotkeyPicker(
     10
    811                this.onHotkeyPicked.bind(this),
    912                Engine.GetGUIObjectByName("hotkeyList").list_data[idx],
     
    1114            );
    1215        };
     16
     17
    1318        Engine.GetGUIObjectByName("hotkeyFilter").onSelectionChange = () => this.setupHotkeyList();
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
    1431
    1532        Engine.GetGUIObjectByName("hotkeyTextFilter").onTextEdit = () => this.setupHotkeyList();
     
    3047    }
    3148
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
    3276    setupFilters()
    3377    {
     
    3579        let names = [];
    3680        for (let cat in this.categories)
    37             names.push(this.categories[cat].label);
     81            names.push(t);
    3882        dropdown.list = [translate("All Hotkeys")].concat(names);
    3983        dropdown.list_data = [-1].concat(Object.keys(this.categories));
     
    4690        hotkeyList.selected = -1;
    4791        let textFilter = Engine.GetGUIObjectByName("hotkeyTextFilter").caption;
     92
     93
    4894        let dropdown = Engine.GetGUIObjectByName("hotkeyFilter");
    4995        if (dropdown.selected && dropdown.selected !== 0)
    50         {
    51             let category = this.categories[dropdown.list_data[dropdown.selected]];
    52             // This is inefficient but it seems fast enough.
    53             let hotkeys = category.hotkeys.filter(x => translate(x[0]).indexOf(textFilter) !== -1);
    54             hotkeyList.list_name = hotkeys.map(x => translate(x[0]));
    55             hotkeyList.list_mapping = hotkeys.map(x => formatHotkeyCombinations(x[1]));
    56             hotkeyList.list = hotkeys.map(() => 0);
    57             hotkeyList.list_data = hotkeys.map(x => x[0]);
    58         }
     96            hotkeys = this.categories[dropdown.list_data[dropdown.selected]].hotkeys;
     97        else
     98            hotkeys = Object.values(this.categories).map(x => x.hotkeys).flat();
     99        hotkeys = hotkeys.filter(x => {
     100            return x.indexOf(textFilter) !== -1 ||
     101                translate(this.metadata.hotkeys[x]?.name || x).indexOf(textFilter) !== -1;
     102        });
     103
     104        hotkeyList.list_name = hotkeys.map(x => translate(this.metadata.hotkeys[x]?.name || x));
     105        hotkeyList.list_mapping = hotkeys.map(x => formatHotkeyCombinations(this.hotkeys[x]));
     106        hotkeyList.list = hotkeys.map(() => 0);
     107        hotkeyList.list_data = hotkeys.map(x => x);
     108    }
     109
     110    onHoverChange()
     111    {
     112        let hotkeyList = Engine.GetGUIObjectByName("hotkeyList");
     113        if (hotkeyList.hovered === -1)
     114            hotkeyList.tooltip = "";
    59115        else
    60116        {
    61             // TODO SM62+ : refactor using flat()
    62             let flattened = [];
    63             for (let cat in this.categories)
    64                 flattened = flattened.concat(this.categories[cat].hotkeys);
    65             flattened = flattened.filter(x => translate(x[0]).indexOf(textFilter) !== -1);
    66             hotkeyList.list_name = flattened.map(x => translate(x[0]));
    67             hotkeyList.list_mapping = flattened.map(x => formatHotkeyCombinations(x[1]));
    68             hotkeyList.list = flattened.map(() => 0);
    69             hotkeyList.list_data = flattened.map(x => x[0]);
     117            let hotkey = hotkeyList.list_data[hotkeyList.hovered];
     118            let tooltip = this.metadata.hotkeys[hotkey]?.desc || markForTranslation("No tooltip available");
     119            hotkeyList.tooltip = translate(tooltip);
    70120        }
    71121    }
     
    93143        this.saveButton.enabled = true;
    94144        this.setupHotkeyList();
    95     }
    96 
    97     setupHotkeyData()
    98     {
    99         let hotkeydata = Engine.GetHotkeyMap();
    100         this.hotkeys = hotkeydata;
    101 
    102         let categories = {
    103             "other": {
    104                 "label": translate("Other hotkeys"),
    105                 "hotkeys": []
    106             }
    107         };
    108         let n_categories = 1;
    109         for (let hotkeyName in this.hotkeys)
    110         {
    111             let category = "other";
    112             let firstdot = hotkeyName.indexOf('.');
    113             if (firstdot !== -1)
    114                 category = hotkeyName.substr(0, firstdot);
    115             if (!(category in categories))
    116             {
    117                 if (n_categories > 18)
    118                     category = "other";
    119                 categories[category] = {
    120                     "label": category,
    121                     "hotkeys": []
    122                 };
    123             }
    124             categories[category].hotkeys.push([hotkeyName, this.hotkeys[hotkeyName]]);
    125         }
    126         // Remove categories that are too small to deserve a tab.
    127         for (let cat of Object.keys(categories))
    128             if (categories[cat].hotkeys.length < 3)
    129             {
    130                 categories.other.hotkeys = categories.other.hotkeys.concat(categories[cat].hotkeys);
    131                 delete categories[cat];
    132             }
    133         for (let cat in categories)
    134             categories[cat].hotkeys = categories[cat].hotkeys.sort();
    135 
    136         this.categories = categories;
    137145    }
    138146
     
    178186
    179187
    180 function init(data)
     188function init()
    181189{
    182     let hotkeyPage = new HotkeysPage(data);
     190    let hotkeyPage = new HotkeysPage();
    183191}
  • ps/trunk/binaries/data/mods/public/gui/hotkeys/hotkeys.xml

    r24215 r25023  
    6767        <object type="image"
    6868            style="ModernDialog"
    69             size="50%-300 50%-190 50%+300 50%+190"
     69            size="50%-300 50%-"
    7070        >
    7171            <object name="hotkeyPickerTitle" style="ModernLabelText" type="text" size="50%-128 -16 50%+128 16">
     
    7373            </object>
    7474
    75             <object name="hotkeyPickerDesc"  style="ModernLabelText" type="text" size="8 20 100%-8 66">
     75            <object name="hotkeyPickerDescHotkey" style="ModernLabelText" type="text" size="8 30 100%-8 60">
     76                <translatableAttribute id="caption">Hotkey Description</translatableAttribute>
     77            </object>
     78
     79            <object name="hotkeyPickerDesc"  style="ModernLabelText" type="text" size="8 60 100%-8 120">
    7680                <translatableAttribute id="caption">Click on any mapping to modify it.\n You may have up to 4 different hotkeys.</translatableAttribute>
    7781            </object>
Note: See TracChangeset for help on using the changeset viewer.