File size: 11,145 Bytes
8086c81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
(window["webpackJsonpGUI"] = window["webpackJsonpGUI"] || []).push([["addon-entry-default-costume-editor-color"],{

/***/ "./src/addons/addons/default-costume-editor-color/_runtime_entry.js":
/*!**************************************************************************!*\
  !*** ./src/addons/addons/default-costume-editor-color/_runtime_entry.js ***!
  \**************************************************************************/
/*! exports provided: resources */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "resources", function() { return resources; });
/* harmony import */ var _userscript_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./userscript.js */ "./src/addons/addons/default-costume-editor-color/userscript.js");
/* generated by pull.js */

const resources = {
  "userscript.js": _userscript_js__WEBPACK_IMPORTED_MODULE_0__["default"]
};

/***/ }),

/***/ "./src/addons/addons/default-costume-editor-color/userscript.js":
/*!**********************************************************************!*\
  !*** ./src/addons/addons/default-costume-editor-color/userscript.js ***!
  \**********************************************************************/
/*! exports provided: default */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony default export */ __webpack_exports__["default"] = (async function (_ref) {
  let {
    addon,
    console,
    msg
  } = _ref;
  // We don"t *need* to wait for the costume editor to be opened, but redux updates take a non-zero
  // amount of CPU time so let's delay that for as long as possible.
  await addon.tab.traps.getPaper();
  const hexComponent = str => Math.round(+str).toString(16).toUpperCase().padStart(2, "0");
  const parseColor = color => {
    if (color === null) {
      return null;
    }
    if (typeof color === "string") {
      // TW natively supports hex color codes with or without transparency
      if (color.startsWith("#")) {
        return color.substring(0, 9).toUpperCase();
      }
      // Sometimes paper gives us rgb() colors which have to be converted to hex
      const rgbMatch = color.match(/^rgb\((\d+)\s*,(\d+)\s*,(\d+)\)$/);
      if (rgbMatch) {
        const [_, r, g, b] = rgbMatch;
        return "#".concat(hexComponent(r)).concat(hexComponent(g)).concat(hexComponent(b));
      }
      // It can also give us rgba() colors
      const rgbaMatch = color.match(/^rgba\((\d+)\s*,(\d+)\s*,(\d+),([\d.]+)\)$/);
      if (rgbaMatch) {
        const [_, r, g, b, a] = rgbaMatch;
        return "#".concat(hexComponent(r)).concat(hexComponent(g)).concat(hexComponent(b)).concat(hexComponent(a * 255));
      }
    }
    console.log("Could not normalize color", color);
    return null;
  };
  const parseColorStyleColor = color => {
    if (color === MIXED) return MIXED;
    return parseColor(color);
  };

  // Special value Scratch uses as color when objects with different colors are selected
  // https://github.com/LLK/scratch-paint/blob/6733e20b56f52d139f9885952a57c7da012a542f/src/helper/style-path.js#L10
  const MIXED = "scratch-paint/style-path/mixed";
  const SCRATCH_DEFAULT_FILL = parseColor("#9966FF");
  const SCRATCH_DEFAULT_STROKE = parseColor("#000000");
  const TOOL_INFO = Object.assign(Object.create(null), {
    // Tool names and gradient info defined in https://github.com/LLK/scratch-paint/blob/develop/src/lib/modes.js
    // Search for activateTool() in matching file in https://github.com/LLK/scratch-paint/tree/develop/src/containers
    BRUSH: {
      resetsFill: true
    },
    ERASER: {},
    LINE: {
      resetsStroke: true,
      requiresNonZeroStrokeWidth: true,
      supportsGradient: true
    },
    FILL: {
      resetsFill: true,
      supportsGradient: true
    },
    SELECT: {
      supportsGradient: true
    },
    RESHAPE: {
      supportsGradient: true
    },
    OVAL: {
      resetsFill: true,
      resetsStroke: true,
      supportsGradient: true
    },
    RECT: {
      resetsFill: true,
      resetsStroke: true,
      supportsGradient: true
    },
    TEXT: {
      resetsFill: true,
      resetsStroke: true
    },
    BIT_BRUSH: {
      resetsFill: true
    },
    BIT_LINE: {
      resetsFill: true,
      requiresNonZeroStrokeWidth: true
    },
    BIT_OVAL: {
      resetsFill: true,
      resetsStroke: true,
      supportsGradient: true
    },
    BIT_RECT: {
      resetsFill: true,
      resetsStroke: true,
      supportsGradient: true
    },
    BIT_TEXT: {
      resetsFill: true,
      resetsStroke: true
    },
    BIT_FILL: {
      resetsFill: true,
      supportsGradient: true
    },
    BIT_ERASER: {},
    BIT_SELECT: {
      supportsGradient: true
    }
  });
  const getToolInfo = () => TOOL_INFO[addon.tab.redux.state.scratchPaint.mode];
  class ColorStyleReducerWrapper {
    constructor(reduxPropertyName, primaryAction, secondaryAction, gradientTypeAction) {
      this.reduxPropertyName = reduxPropertyName;
      this.primaryAction = primaryAction;
      this.secondaryAction = secondaryAction;
      this.gradientTypeAction = gradientTypeAction;
    }
    get() {
      let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : addon.tab.redux.state;
      return state.scratchPaint.color[this.reduxPropertyName];
    }
    set(newColor) {
      const state = this.get();
      const newPrimary = parseColorStyleColor(newColor.primary);
      if (state.primary !== newPrimary) {
        addon.tab.redux.dispatch({
          type: this.primaryAction,
          color: newPrimary
        });
      }
      const toolInfo = getToolInfo();
      const toolSupportsGradient = toolInfo && toolInfo.supportsGradient;
      if (toolSupportsGradient) {
        const newSecondary = parseColorStyleColor(newColor.secondary);
        if (state.secondary !== newSecondary) {
          addon.tab.redux.dispatch({
            type: this.secondaryAction,
            color: newSecondary
          });
        }
        if (state.gradientType !== newColor.gradientType) {
          addon.tab.redux.dispatch({
            type: this.gradientTypeAction,
            gradientType: newColor.gradientType
          });
        }
      }
    }
  }
  const fillStyle = new ColorStyleReducerWrapper("fillColor", "scratch-paint/fill-style/CHANGE_FILL_COLOR", "scratch-paint/fill-style/CHANGE_FILL_COLOR_2", "scratch-paint/fill-style/CHANGE_FILL_GRADIENT_TYPE");
  const strokeStyle = new ColorStyleReducerWrapper("strokeColor", "scratch-paint/stroke-style/CHANGE_STROKE_COLOR", "scratch-paint/stroke-style/CHANGE_STROKE_COLOR_2", "scratch-paint/stroke-style/CHANGE_STROKE_GRADIENT_TYPE");
  const simpleHexColor = hex => ({
    primary: hex,
    secondary: null,
    gradientType: "SOLID"
  });
  let defaultFillColor;
  let defaultStrokeColor;
  let defaultStrokeWidth;
  const setDefaultColorsToSettings = () => {
    defaultFillColor = simpleHexColor(parseColor(addon.settings.get("fill")));
    defaultStrokeColor = simpleHexColor(parseColor(addon.settings.get("stroke")));
    defaultStrokeWidth = addon.settings.get("strokeSize");
  };
  setDefaultColorsToSettings();
  const applyFillColor = () => {
    fillStyle.set(defaultFillColor);
  };
  const applyStrokeColor = () => {
    strokeStyle.set(defaultStrokeColor);
  };
  const applyStrokeWidth = mustBeNonZero => {
    let width = defaultStrokeWidth;
    if (width === 0 && mustBeNonZero) {
      width = 1;
    }
    if (addon.tab.redux.state.scratchPaint.color.strokeWidth !== width) {
      addon.tab.redux.dispatch({
        type: "scratch-paint/stroke-width/CHANGE_STROKE_WIDTH",
        strokeWidth: width
      });
    }
  };
  if (!addon.self.disabled) {
    applyFillColor();
    applyStrokeColor();
    applyStrokeWidth(false);
  }
  addon.settings.addEventListener("change", () => {
    if (!addon.settings.get("persistence")) {
      setDefaultColorsToSettings();
    }
  });
  const isValidColorToPersist = color => color.primary !== null && color.primary !== MIXED;
  let activatingTool = false;
  addon.tab.redux.initialize();
  addon.tab.redux.addEventListener("statechanged", _ref2 => {
    let {
      detail
    } = _ref2;
    if (addon.self.disabled) {
      return;
    }
    const action = detail.action;
    if (!activatingTool && addon.settings.get("persistence")) {
      // We always want to check for changes instead of filtering to just certain actions because quite a few
      // actions can change these.
      const newFill = fillStyle.get();
      if (fillStyle.get(detail.prev) !== newFill && isValidColorToPersist(newFill)) {
        defaultFillColor = newFill;
      }
      const newStroke = strokeStyle.get();
      if (strokeStyle.get(detail.prev) !== newStroke && isValidColorToPersist(newStroke)) {
        defaultStrokeColor = newStroke;
      }
      const newStrokeWidth = detail.next.scratchPaint.color.strokeWidth;
      if (typeof newStrokeWidth === "number") {
        defaultStrokeWidth = newStrokeWidth;
      }
    }
    if (action.type === "scratch-paint/modes/CHANGE_MODE") {
      activatingTool = true;
      queueMicrotask(() => {
        activatingTool = false;
        if (addon.settings.get("persistence")) {
          // In persistence, we always want to re-apply the previous stroke and fill.
          const toolInfo = getToolInfo();
          if (!toolInfo) {
            console.warn("unknown tool", addon.tab.redux.state.scratchPaint.mode);
            return;
          }
          if (toolInfo.resetsFill) {
            applyFillColor();
          }
          if (toolInfo.resetsStroke) {
            applyStrokeWidth(!!toolInfo.requiresNonZeroStrokeWidth);
            applyStrokeColor();
          }
        } else {
          // In non-persistence, we'll only apply the default colors when Scratch resets them to maintain the same behavior.
          // We have to do this weird redux trick because we can't modify these constants:
          // https://github.com/LLK/scratch-paint/blob/6733e20b56f52d139f9885952a57c7da012a542f/src/reducers/fill-style.js#L7
          // https://github.com/LLK/scratch-paint/blob/6733e20b56f52d139f9885952a57c7da012a542f/src/reducers/stroke-style.js#L7
          const oldFillColor = fillStyle.get(detail.prev);
          if (oldFillColor.primary === null || oldFillColor.primary === MIXED) {
            const newFillColor = fillStyle.get();
            if (newFillColor.primary === SCRATCH_DEFAULT_FILL) {
              applyFillColor();
            }
          }
          const oldStrokeColor = strokeStyle.get(detail.prev);
          if (oldStrokeColor.primary === null || oldStrokeColor.primary === MIXED) {
            const newStrokeColor = strokeStyle.get();
            if (newStrokeColor.primary === SCRATCH_DEFAULT_STROKE) {
              applyStrokeWidth(true);
              applyStrokeColor();
            }
          }
        }
      });
    }
  });
});

/***/ })

}]);
//# sourceMappingURL=addon-entry-default-costume-editor-color.js.map