All files / src/lib/events eventCameraShake.js

76.32% Statements 29/38
53.33% Branches 8/15
50% Functions 1/2
76.32% Lines 29/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 1411x   1x 1x   1x                                         1x                                                                                                                                         1x 6x 6x 6x     6x 6x     6x 6x 6x 6x   1x 1x 1x   1x 1x 1x   2x 2x 2x   2x 2x     6x 6x       1x                  
const l10n = require("../helpers/l10n").default;
 
const id = "EVENT_CAMERA_SHAKE";
const groups = ["EVENT_GROUP_CAMERA"];
 
const autoLabel = (fetchArg, input) => {
  let direction = l10n("FIELD_HORIZONTAL");
  if (input.shakeDirection === "vertical") {
    direction = l10n("FIELD_VERTICAL");
  } else Iif (input.shakeDirection === "diagonal") {
    direction = l10n("FIELD_DIAGONAL");
  }
  Iif (input.units === "frames") {
    return l10n("EVENT_CAMERA_SHAKE_LABEL", {
      time: fetchArg("frames"),
      units: l10n("FIELD_FRAMES"),
      direction,
    });
  }
  return l10n("EVENT_CAMERA_SHAKE_LABEL", {
    time: fetchArg("time"),
    units: l10n("FIELD_SECONDS"),
    direction,
  });
};
 
const fields = [
  {
    type: "group",
    fields: [
      {
        key: "time",
        type: "number",
        label: l10n("FIELD_DURATION"),
        description: l10n("FIELD_DURATION_SHAKE_DESC"),
        min: 0,
        max: 60,
        step: 0.1,
        defaultValue: 0.5,
        unitsField: "units",
        unitsDefault: "time",
        unitsAllowed: ["time", "frames"],
        conditions: [
          {
            key: "units",
            ne: "frames",
          },
        ],
      },
      {
        key: "frames",
        label: l10n("FIELD_DURATION"),
        description: l10n("FIELD_DURATION_SHAKE_DESC"),
        type: "number",
        min: 0,
        max: 3600,
        width: "50%",
        defaultValue: 30,
        unitsField: "units",
        unitsDefault: "time",
        unitsAllowed: ["time", "frames"],
        conditions: [
          {
            key: "units",
            eq: "frames",
          },
        ],
      },
      {
        key: "shakeDirection",
        label: l10n("FIELD_MOVE_TYPE"),
        description: l10n("FIELD_MOVE_TYPE_SHAKE_DESC"),
        hideLabel: true,
        type: "moveType",
        defaultValue: "horizontal",
        flexBasis: 30,
        flexGrow: 0,
        alignBottom: true,
      },
    ],
  },
  {
    key: "magnitude",
    label: l10n("FIELD_MAGNITUDE"),
    description: l10n("FIELD_MAGNITUDE_DESC"),
    type: "value",
    min: 1,
    max: 255,
    defaultValue: {
      type: "number",
      value: 5,
    },
  },
];
 
const compile = (input, helpers) => {
  const { cameraShakeScriptValue } = helpers;
  let frames = 0;
  Iif (input.units === "frames") {
    frames = typeof input.frames === "number" ? input.frames : 30;
  } else {
    const seconds = typeof input.time === "number" ? input.time : 0.5;
    frames = Math.ceil(seconds * 60);
  }
 
  const shakeDirection = input.shakeDirection;
  let shouldShakeX = true;
  let shouldShakeY = false;
  switch (shakeDirection) {
    case "horizontal":
      shouldShakeX = true;
      shouldShakeY = false;
      break;
    case "vertical":
      shouldShakeX = false;
      shouldShakeY = true;
      break;
    case "diagonal":
      shouldShakeX = true;
      shouldShakeY = true;
      break;
    default:
      shouldShakeX = true;
      shouldShakeY = false;
  }
 
  if (frames > 0) {
    cameraShakeScriptValue(shouldShakeX, shouldShakeY, frames, input.magnitude);
  }
};
 
module.exports = {
  id,
  description: l10n("EVENT_CAMERA_SHAKE_DESC"),
  autoLabel,
  groups,
  fields,
  compile,
  waitUntilAfterInitFade: true,
};