From 819facbec8b7b92eaf5e2b7b14cee618b6ba693b Mon Sep 17 00:00:00 2001 From: unsoluble Date: Sun, 19 Sep 2021 21:04:47 +0000 Subject: [PATCH] hotkeys imp, plus localization --- lang/en.json | 23 ++++- scripts/quickscale.js | 230 +++++++++++++++++++++++++++++++----------- 2 files changed, 195 insertions(+), 58 deletions(-) diff --git a/lang/en.json b/lang/en.json index d04426a..4617325 100644 --- a/lang/en.json +++ b/lang/en.json @@ -2,5 +2,26 @@ "QSCALE.Token_Random_Range": "Token Scale Randomization Range", "QSCALE.Tile_Random_Range": "Tile Scale Randomization Range", "QSCALE.Rotation_Amount": "Maximum Rotation Amount", - "QSCALE.Rotation_Amount_Hint": "When applying random rotations, rotate +/- up to this many degrees." + "QSCALE.Rotation_Amount_Hint": "When applying random rotations, rotate +/- up to this many degrees.", + + "QSCALE.DFKEYS.Group_Name": "QuickScale Controls", + "QSCALE.DFKEYS.Reduce": "Reduce", + "QSCALE.DFKEYS.Enlarge": "Enlarge", + "QSCALE.DFKEYS.Random_Scale": "Random Scale", + "QSCALE.DFKEYS.Random_Rotation": "Random Rotation", + "QSCALE.DFKEYS.Save_Prototype": "Save Prototype", + + "QSCALE.REFERENCE.Group_Name": "QuickScale Controls", + "QSCALE.REFERENCE.Scale_Elements": "Scale Elements Down or Up", + "QSCALE.REFERENCE.Scale_Templates": "Scale Templates Down or Up", + "QSCALE.REFERENCE.Elements_Large": "Scale Elements in Large Steps", + "QSCALE.REFERENCE.Templates_Large": "Scale Templates in Large Steps", + "QSCALE.REFERENCE.Save_Prototypes": "Save Scales to Prototypes", + "QSCALE.REFERENCE.Randomize_Scales": "Randomize Element Scales", + "QSCALE.REFERENCE.Randomize_Rotations": "Randomize Element Rotations", + "QSCALE.REFERENCE.Tokens": "Tokens", + "QSCALE.REFERENCE.Templates": "Templates", + "QSCALE.REFERENCE.Tiles": "Tiles", + "QSCALE.REFERENCE.Lights": "Lights", + "QSCALE.REFERENCE.Sounds": "Sounds" } diff --git a/scripts/quickscale.js b/scripts/quickscale.js index f9c904d..70c11c8 100755 --- a/scripts/quickscale.js +++ b/scripts/quickscale.js @@ -68,49 +68,97 @@ Hooks.on('init', function () { }); }); +Hooks.once('init', function () { + // If the DF Hotkeys module is present, set it up to allow for custom keybinds. + if (game.modules.get('lib-df-hotkeys')?.active) { + Hotkeys.registerGroup({ + name: 'quickscale.qs-controls', + label: game.i18n.localize('QSCALE.DFKEYS.Group_Name'), + }); + + Hotkeys.registerShortcut({ + name: 'quickscale.reduce-key', + label: game.i18n.localize('QSCALE.DFKEYS.Reduce'), + group: 'quickscale.qs-controls', + default: { key: Hotkeys.keys.BracketLeft, alt: false, ctrl: false, shift: false }, + repeat: true, + onKeyDown: (self) => { + updateSize(QS_Reduce_Key, false); + }, + }); + + Hotkeys.registerShortcut({ + name: 'quickscale.enlarge-key', + label: game.i18n.localize('QSCALE.DFKEYS.Enlarge'), + group: 'quickscale.qs-controls', + default: { key: Hotkeys.keys.BracketRight, alt: false, ctrl: false, shift: false }, + repeat: true, + onKeyDown: (self) => { + updateSize(QS_Enlarge_Key, false); + }, + }); + + Hotkeys.registerShortcut({ + name: 'quickscale.random-scale-key', + label: game.i18n.localize('QSCALE.DFKEYS.Random_Scale'), + group: 'quickscale.qs-controls', + default: { key: Hotkeys.keys.BracketLeft, alt: false, ctrl: false, shift: true }, + repeat: true, + onKeyDown: (self) => { + handleRandomScaleKey(game.canvas.activeLayer.name, QS_Random_Scale_Key); + }, + }); + + Hotkeys.registerShortcut({ + name: 'quickscale.random-rotation-key', + label: game.i18n.localize('QSCALE.DFKEYS.Random_Rotation'), + group: 'quickscale.qs-controls', + default: { key: Hotkeys.keys.BracketRight, alt: false, ctrl: false, shift: true }, + repeat: true, + onKeyDown: (self) => { + handleRandomRotationKey(game.canvas.activeLayer.name, QS_Random_Rotate_Key); + }, + }); + + Hotkeys.registerShortcut({ + name: 'quickscale.prototype-key', + label: game.i18n.localize('QSCALE.DFKEYS.Save_Prototype'), + group: 'quickscale.qs-controls', + default: { key: Hotkeys.keys.Backslash, alt: false, ctrl: false, shift: true }, + repeat: true, + onKeyDown: (self) => { + if (game.canvas.activeLayer.name == 'TokenLayer') updatePrototype(); + }, + }); + } +}); + Hooks.on('ready', () => { - window.addEventListener('keypress', (e) => { - // Don't trigger if we're in a text entry field. - if (document.activeElement instanceof HTMLInputElement) return; - if (document.activeElement instanceof HTMLTextAreaElement) return; - if (document.activeElement.getAttribute('contenteditable') === 'true') return; - - if (e.key == QS_Reduce_Key || e.key == QS_Enlarge_Key) { - updateSize(e.key, false); - } + // Only set up our own key listener if DF Hotkeys isn't handling this. + if (!game.modules.get('lib-df-hotkeys')?.active) { + window.addEventListener('keypress', (e) => { + // Don't trigger if we're in a text entry field. + if (document.activeElement instanceof HTMLInputElement) return; + if (document.activeElement instanceof HTMLTextAreaElement) return; + if (document.activeElement.getAttribute('contenteditable') === 'true') return; + + if (e.key == QS_Reduce_Key || e.key == QS_Enlarge_Key) { + updateSize(e.key, false); + } + + const currentToolLayer = game.canvas.activeLayer.name; - const currentToolLayer = game.canvas.activeLayer.name; - - if (e.key == QS_Random_Scale_Key) { - switch (currentToolLayer) { - case 'TokenLayer': - case 'BackgroundLayer': - randomizeScale(); - break; - case 'TemplateLayer': - case 'LightingLayer': - case 'SoundsLayer': - updateSize(e.key, true); - break; + if (e.key == QS_Random_Scale_Key) { + handleRandomScaleKey(currentToolLayer, e.key); } - } - if (e.key == QS_Random_Rotate_Key) { - switch (currentToolLayer) { - case 'TokenLayer': - case 'BackgroundLayer': - randomizeRotation(); - break; - case 'TemplateLayer': - case 'LightingLayer': - case 'SoundsLayer': - updateSize(e.key, true); - break; + if (e.key == QS_Random_Rotate_Key) { + handleRandomRotationKey(currentToolLayer, e.key); } - } - if (e.key == QS_Prototype_Key && currentToolLayer == 'TokenLayer') { - updatePrototype(); - } - }); + if (e.key == QS_Prototype_Key && currentToolLayer == 'TokenLayer') { + updatePrototype(); + } + }); + } }); Hooks.on('renderSettingsConfig', () => { @@ -204,62 +252,130 @@ Hooks.on('renderControlsReference', () => { let injection = `
- QuickScale Controls + ${game.i18n.localize('QSCALE.REFERENCE.Group_Name')}
  1. -

    ${gmAuth ? 'Scale Elements Down or Up' : 'Scale Templates Down or Up'}

    +

    ${ + gmAuth + ? game.i18n.localize('QSCALE.REFERENCE.Scale_Elements') + : game.i18n.localize('QSCALE.REFERENCE.Scale_Templates') + }

    - [ ] + ${QS_Reduce_Key} + ${QS_Enlarge_Key} ${ - gmAuth ? '(Tokens, Templates, Tiles, Lights, Sounds)' : '' + gmAuth + ? '(' + + game.i18n.localize('QSCALE.REFERENCE.Tokens') + + ', ' + + game.i18n.localize('QSCALE.REFERENCE.Templates') + + ', ' + + game.i18n.localize('QSCALE.REFERENCE.Tiles') + + ', ' + + game.i18n.localize('QSCALE.REFERENCE.Lights') + + ', ' + + game.i18n.localize('QSCALE.REFERENCE.Sounds') + + ')' + : '' }
  2. -

    ${gmAuth ? 'Scale Elements in Large Steps' : 'Scale Templates in Large Steps'}

    +

    ${ + gmAuth + ? game.i18n.localize('QSCALE.REFERENCE.Elements_Large') + : game.i18n.localize('QSCALE.REFERENCE.Templates_Large') + }

    - { } - ${gmAuth ? '(Templates, Lights, Sounds)' : ''} + ${QS_Random_Scale_Key} + ${QS_Random_Rotate_Key} + ${ + gmAuth + ? '(' + + game.i18n.localize('QSCALE.REFERENCE.Templates') + + ', ' + + game.i18n.localize('QSCALE.REFERENCE.Lights') + + ', ' + + game.i18n.localize('QSCALE.REFERENCE.Sounds') + + ')' + : '' + }
  3. `; if (gmAuth) { injection += `
  4. -

    Save Scales to Prototypes

    +

    ${game.i18n.localize('QSCALE.REFERENCE.Save_Prototypes')}

    - | - (Tokens) + ${QS_Prototype_Key} + (${game.i18n.localize('QSCALE.REFERENCE.Tokens')})
  5. -

    Randomize Element Scales

    +

    ${game.i18n.localize('QSCALE.REFERENCE.Randomize_Scales')}

    - { - (Tokens, Tiles) + ${QS_Random_Scale_Key} + (${game.i18n.localize( + 'QSCALE.REFERENCE.Tokens' + )}, ${game.i18n.localize('QSCALE.REFERENCE.Tiles')})
  6. -

    Randomize Element Rotations

    +

    ${game.i18n.localize('QSCALE.REFERENCE.Randomize_Rotations')}

    - } - (Tokens, Tiles) + ${QS_Random_Rotate_Key} + (${game.i18n.localize( + 'QSCALE.REFERENCE.Tokens' + )}, ${game.i18n.localize('QSCALE.REFERENCE.Tiles')})
  7. `; } injection += `
`; - // Insert the controls at the bottom of the window. - $('#controls-reference > section > div').last().after(injection); + // Insert the controls at the bottom of the window, but only if + // DF Hotkeys isn't being used to define custom keys. + if (!game.modules.get('lib-df-hotkeys')?.active) { + $('#controls-reference > section > div').last().after(injection); + } }); +function handleRandomScaleKey(currentToolLayer, key) { + switch (currentToolLayer) { + case 'TokenLayer': + case 'BackgroundLayer': + randomizeScale(); + break; + case 'TemplateLayer': + case 'LightingLayer': + case 'SoundsLayer': + updateSize(key, true); + break; + } +} + +function handleRandomRotationKey(currentToolLayer, key) { + switch (currentToolLayer) { + case 'TokenLayer': + case 'BackgroundLayer': + randomizeRotation(); + break; + case 'TemplateLayer': + case 'LightingLayer': + case 'SoundsLayer': + updateSize(key, true); + break; + } +} + // On slider changes, save the new values into the actual inputs. function saveTokenRange(values, handle, unencoded, tap, positions, noUiSlider) { $('input[name="quickscale.token-random-min"]').val(values[0]); $('input[name="quickscale.token-random-max"]').val(values[1]); } + function saveTileRange(values, handle, unencoded, tap, positions, noUiSlider) { $('input[name="quickscale.tile-random-min"]').val(values[0]); $('input[name="quickscale.tile-random-max"]').val(values[1]);