source: ps/trunk/binaries/data/mods/public/gui/hotkeys/HotkeyMetadata.js@ 25023

Last change on this file since 25023 was 25023, checked in by wraitii, 3 years ago

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

  • Property svn:eol-style set to native
File size: 1.9 KB
Line 
1/**
2 *
3 */
4class HotkeyMetadata
5{
6 constructor()
7 {
8 this.DEFAULT_CATEGORY = "other";
9 this.categories = {};
10 this.hotkeys = {};
11
12 this.parseSpec();
13 }
14
15 parseSpec()
16 {
17 let files = this.getFiles();
18 let hotkey_i = 0;
19 let categories = {
20 [this.DEFAULT_CATEGORY]: {
21 "name": markForTranslation("Other Hotkeys"),
22 "desc": markForTranslation("Other Hotkeys"),
23 }
24 };
25 for (let file of files)
26 {
27 let data = Engine.ReadJSONFile(file);
28 if (data.categories)
29 for (let cat in data.categories)
30 categories[cat] = data.categories[cat];
31 if (data.hotkeys)
32 for (let hotkey in data.hotkeys)
33 {
34 this.hotkeys[hotkey] = data.hotkeys[hotkey];
35 this.hotkeys[hotkey].order = hotkey_i++;
36 this.hotkeys[hotkey].categories = data.hotkeys[hotkey].categories || [this.DEFAULT_CATEGORY];
37 }
38 if (data.mapped_hotkeys)
39 for (let cat in data.mapped_hotkeys)
40 for (let hotkey in data.mapped_hotkeys[cat])
41 {
42 if (data.mapped_hotkeys[cat][hotkey].categories)
43 warn("Categories will be overwritten for mapped hotkey " + hotkey);
44 this.hotkeys[hotkey] = data.mapped_hotkeys[cat][hotkey];
45 this.hotkeys[hotkey].order = hotkey_i++;
46 this.hotkeys[hotkey].categories = [cat];
47 }
48 }
49 // Sort categories (JS objects are (in this case) sorted by insertion order).
50 this.categories = {};
51 let keys = Object.keys(categories).sort((a, b) => {
52 if (a === this.DEFAULT_CATEGORY || b === this.DEFAULT_CATEGORY)
53 return a === this.DEFAULT_CATEGORY ? 1 : -1;
54 if (categories[a].order === undefined || categories[b].order === undefined)
55 return categories[a].order === undefined ? -1 : 1; // Likely to keep alphabetical order.
56 return categories[a].order - categories[b].order;
57 });
58 for (let key of keys)
59 this.categories[key] = categories[key];
60 // TODO: validate that categories exist.
61 }
62
63 getFiles()
64 {
65 return Engine.ListDirectoryFiles("gui/hotkeys/spec/", "*.json");
66 }
67}
Note: See TracBrowser for help on using the repository browser.