File size: 11,488 Bytes
6bcb42f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import log from '../lib/log';

const ADD_MONITOR_RECT = 'scratch-gui/monitors/ADD_MONITOR_RECT';
const MOVE_MONITOR_RECT = 'scratch-gui/monitors/MOVE_MONITOR_RECT';
const RESIZE_MONITOR_RECT = 'scratch-gui/monitors/RESIZE_MONITOR_RECT';
const REMOVE_MONITOR_RECT = 'scratch-gui/monitors/REMOVE_MONITOR_RECT';
const RESET_MONITOR_LAYOUT = 'scratch-gui/monitors/RESET_MONITOR_LAYOUT';

const initialState = {
    monitors: {},
    savedMonitorPositions: {}
};

// Verify that the rectangle formed by the 2 points is well-formed
const _verifyRect = function (upperStart, lowerEnd) {
    if (isNaN(upperStart.x) || isNaN(upperStart.y) || isNaN(lowerEnd.x) || isNaN(lowerEnd.y)) {
        return false;
    }
    if (!(upperStart.x < lowerEnd.x)) {
        return false;
    }
    if (!(upperStart.y < lowerEnd.y)) {
        return false;
    }
    return true;
};

const _addMonitorRect = function (state, action) {
    if (state.monitors.hasOwnProperty(action.monitorId)) {
        log.error(`Can't add monitor, monitor with id ${action.monitorId} already exists.`);
        return state;
    }
    if (!_verifyRect(action.upperStart, action.lowerEnd)) {
        log.error(`Monitor rectangle not formatted correctly`);
        return state;
    }
    return {
        monitors: Object.assign({}, state.monitors, {
            [action.monitorId]: {
                upperStart: action.upperStart,
                lowerEnd: action.lowerEnd
            }
        }),
        savedMonitorPositions: action.savePosition ?
            Object.assign({}, state.savedMonitorPositions, {
                [action.monitorId]: {x: action.upperStart.x, y: action.upperStart.y}
            }) :
            state.savedMonitorPositions
    };
};

const _moveMonitorRect = function (state, action) {
    if (!state.monitors.hasOwnProperty(action.monitorId)) {
        log.error(`Can't move monitor, monitor with id ${action.monitorId} does not exist.`);
        return state;
    }
    if (isNaN(action.newX) || isNaN(action.newY)) {
        log.error(`Monitor rectangle not formatted correctly`);
        return state;
    }
    
    const oldMonitor = state.monitors[action.monitorId];
    if (oldMonitor.upperStart.x === action.newX &&
            oldMonitor.upperStart.y === action.newY) {
        // Hasn't moved
        return state;
    }
    const monitorWidth = oldMonitor.lowerEnd.x - oldMonitor.upperStart.x;
    const monitorHeight = oldMonitor.lowerEnd.y - oldMonitor.upperStart.y;
    return {
        monitors: Object.assign({}, state.monitors, {
            [action.monitorId]: {
                upperStart: {x: action.newX, y: action.newY},
                lowerEnd: {x: action.newX + monitorWidth, y: action.newY + monitorHeight}
            }
        }),
        // User generated position is saved
        savedMonitorPositions: Object.assign({}, state.savedMonitorPositions, {
            [action.monitorId]: {x: action.newX, y: action.newY}
        })
    };
};

const _resizeMonitorRect = function (state, action) {
    if (!state.monitors.hasOwnProperty(action.monitorId)) {
        log.error(`Can't resize monitor, monitor with id ${action.monitorId} does not exist.`);
        return state;
    }
    if (isNaN(action.newWidth) || isNaN(action.newHeight) ||
            action.newWidth <= 0 || action.newHeight <= 0) {
        log.error(`Monitor rectangle not formatted correctly`);
        return state;
    }

    const oldMonitor = state.monitors[action.monitorId];
    const newMonitor = {
        upperStart: oldMonitor.upperStart,
        lowerEnd: {
            x: oldMonitor.upperStart.x + action.newWidth,
            y: oldMonitor.upperStart.y + action.newHeight
        }
    };
    if (newMonitor.lowerEnd.x === oldMonitor.lowerEnd.x &&
            newMonitor.lowerEnd.y === oldMonitor.lowerEnd.y) {
        // no change
        return state;
    }

    return {
        monitors: Object.assign({}, state.monitors, {[action.monitorId]: newMonitor}),
        savedMonitorPositions: state.savedMonitorPositions
    };

};

const _removeMonitorRect = function (state, action) {
    if (!state.monitors.hasOwnProperty(action.monitorId)) {
        log.error(`Can't remove monitor, monitor with id ${action.monitorId} does not exist.`);
        return state;
    }

    const newMonitors = Object.assign({}, state.monitors);
    delete newMonitors[action.monitorId];
    return {
        monitors: newMonitors,
        savedMonitorPositions: state.savedMonitorPositions
    };
};

const reducer = function (state, action) {
    if (typeof state === 'undefined') state = initialState;
    switch (action.type) {
    case ADD_MONITOR_RECT:
        return _addMonitorRect(state, action);
    case MOVE_MONITOR_RECT:
        return _moveMonitorRect(state, action);
    case RESIZE_MONITOR_RECT:
        return _resizeMonitorRect(state, action);
    case REMOVE_MONITOR_RECT:
        return _removeMonitorRect(state, action);
    case RESET_MONITOR_LAYOUT:
        return initialState;
    default:
        return state;
    }
};

// Init position --------------------------
const PADDING = 5;
// @todo fix these numbers when we fix https://github.com/LLK/scratch-gui/issues/980
const SCREEN_WIDTH = 400;
const SCREEN_HEIGHT = 300;
const SCREEN_EDGE_BUFFER = 40;

const _rectsIntersect = function (rect1, rect2) {
    // If one rectangle is on left side of other
    if (rect1.upperStart.x >= rect2.lowerEnd.x || rect2.upperStart.x >= rect1.lowerEnd.x) return false;
    // If one rectangle is above other
    if (rect1.upperStart.y >= rect2.lowerEnd.y || rect2.upperStart.y >= rect1.lowerEnd.y) return false;
    return true;
};

// We need to place a monitor with the given width and height. Return a rect defining where it should be placed.
const getInitialPosition = function (state, monitorId, eltWidth, eltHeight) {
    // If this monitor was purposefully moved to a certain position before, put it back in that position
    if (state.savedMonitorPositions.hasOwnProperty(monitorId)) {
        const saved = state.savedMonitorPositions[monitorId];
        return {
            upperStart: saved,
            lowerEnd: {x: saved.x + eltWidth, y: saved.y + eltHeight}
        };
    }

    // Try all starting positions for the new monitor to find one that doesn't intersect others
    const endXs = [0];
    const endYs = [0];
    let lastX = null;
    let lastY = null;
    for (const monitor in state.monitors) {
        let x = state.monitors[monitor].lowerEnd.x;
        x = Math.ceil(x / 50) * 50; // Try to choose a sensible "tab width" so more monitors line up
        endXs.push(x);
        endYs.push(Math.ceil(state.monitors[monitor].lowerEnd.y));
    }
    endXs.sort((a, b) => a - b);
    endYs.sort((a, b) => a - b);
    // We'll use plan B if the monitor doesn't fit anywhere (too long or tall)
    let planB = null;
    for (const x of endXs) {
        if (x === lastX) {
            continue;
        }
        lastX = x;
        outer:
        for (const y of endYs) {
            if (y === lastY) {
                continue;
            }
            lastY = y;
            const monitorRect = {
                upperStart: {x: x + PADDING, y: y + PADDING},
                lowerEnd: {x: x + PADDING + eltWidth, y: y + PADDING + eltHeight}
            };
            // Intersection testing rect that includes padding
            const rect = {
                upperStart: {x, y},
                lowerEnd: {x: x + eltWidth + (2 * PADDING), y: y + eltHeight + (2 * PADDING)}
            };
            for (const monitor in state.monitors) {
                if (_rectsIntersect(state.monitors[monitor], rect)) {
                    continue outer;
                }
            }
            // If the rect overlaps the ends of the screen
            if (rect.lowerEnd.x > SCREEN_WIDTH || rect.lowerEnd.y > SCREEN_HEIGHT) {
                // If rect is not too close to completely off screen, set it as plan B
                if (!planB &&
                        !(rect.upperStart.x + SCREEN_EDGE_BUFFER > SCREEN_WIDTH ||
                            rect.upperStart.y + SCREEN_EDGE_BUFFER > SCREEN_HEIGHT)) {
                    planB = monitorRect;
                }
                continue;
            }
            return monitorRect;
        }
    }
    // If the monitor is too long to fit anywhere, put it in the leftmost spot available
    // that intersects the right or bottom edge and isn't too close to the edge.
    if (planB) {
        return planB;
    }

    // If plan B fails and there's nowhere reasonable to put it, plan C is to place the monitor randomly
    const randX = Math.ceil(Math.random() * (SCREEN_WIDTH / 2));
    const randY = Math.ceil(Math.random() * (SCREEN_HEIGHT - SCREEN_EDGE_BUFFER));
    return {
        upperStart: {
            x: randX,
            y: randY
        },
        lowerEnd: {
            x: randX + eltWidth,
            y: randY + eltHeight
        }
    };
};

// Action creators ------------------------
/**
 * @param {!string} monitorId Id to add
 * @param {!object} upperStart upper point defining the rectangle
 * @param {!number} upperStart.x X of top point that defines the monitor location
 * @param {!number} upperStart.y Y of top point that defines the monitor location
 * @param {!object} lowerEnd lower point defining the rectangle
 * @param {!number} lowerEnd.x X of bottom point that defines the monitor location
 * @param {!number} lowerEnd.y Y of bottom point that defines the monitor location
 * @param {?boolean} savePosition True if the placement should be saved when adding the monitor
 * @returns {object} action to add a new monitor at the location
 */
const addMonitorRect = function (monitorId, upperStart, lowerEnd, savePosition) {
    return {
        type: ADD_MONITOR_RECT,
        monitorId: monitorId,
        upperStart: upperStart,
        lowerEnd: lowerEnd,
        savePosition: savePosition
    };
};

/**
 * @param {!string} monitorId Id for monitor to move
 * @param {!number} newX X of top point that defines the monitor location
 * @param {!number} newY Y of top point that defines the monitor location
 * @returns {object} action to move an existing monitor to the location
 */
const moveMonitorRect = function (monitorId, newX, newY) {
    return {
        type: MOVE_MONITOR_RECT,
        monitorId: monitorId,
        newX: newX,
        newY: newY
    };
};

/**
 * @param {!string} monitorId Id for monitor to resize
 * @param {!number} newWidth Width to set monitor to
 * @param {!number} newHeight Height to set monitor to
 * @returns {object} action to resize an existing monitor to the given dimensions
 */
const resizeMonitorRect = function (monitorId, newWidth, newHeight) {
    return {
        type: RESIZE_MONITOR_RECT,
        monitorId: monitorId,
        newWidth: newWidth,
        newHeight: newHeight
    };
};

/**
 * @param {!string} monitorId Id for monitor to remove
 * @returns {object} action to remove an existing monitor
 */
const removeMonitorRect = function (monitorId) {
    return {
        type: REMOVE_MONITOR_RECT,
        monitorId: monitorId
    };
};

const resetMonitorLayout = function () {
    return {
        type: RESET_MONITOR_LAYOUT
    };
};

export {
    reducer as default,
    initialState as monitorLayoutInitialState,
    addMonitorRect,
    getInitialPosition,
    moveMonitorRect,
    resizeMonitorRect,
    removeMonitorRect,
    resetMonitorLayout,
    PADDING,
    SCREEN_HEIGHT,
    SCREEN_WIDTH
};