-
Notifications
You must be signed in to change notification settings - Fork 936
/
Copy pathplanetInterface.js
333 lines (300 loc) · 12 KB
/
planetInterface.js
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
331
332
333
// Copyright (c) 2021 Walter Bender
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the The GNU Affero General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// You should have received a copy of the GNU Affero General Public
// License along with this library; if not, write to the Free Software
// Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
//
// Note: This code is inspired by the Python Turtle Blocks project
// (https://github.com/walterbender/turtleart), but implemented from
// scratch. -- Walter Bender, October 2014.
/*
globals _, docById, platformColor, doSVG, createjs, _THIS_IS_MUSIC_BLOCKS_
*/
/*
exported PlanetInterface
*/
/**
* Represents an interface for interacting with a planet.
* @param {object} activity - The activity object associated with the interface.
*/
class PlanetInterface {
constructor(activity) {
this.planet = null;
this.iframe = null;
this.mainCanvas = null;
this.activity = activity;
/**
* Hides music blocks and related elements.
*/
this.hideMusicBlocks = () => {
this.activity.hideSearchWidget();
window.widgetWindows.hideAllWindows();
this.activity.logo.doStopTurtles();
docById("helpElem").style.visibility = "hidden";
document.querySelector(".canvasHolder").classList.add("hide");
document.querySelector("#canvas").style.display = "none";
document.querySelector("#theme-color").content = "#8bc34a";
const that = this;
setTimeout(() => {
// Time to release the mouse
that.activity.stage.enableDOMEvents(false);
}, 250);
window.scroll(0, 0);
};
/**
* Shows music blocks and related elements.
*/
this.showMusicBlocks = () => {
document.title = this.activity.planet.getCurrentProjectName();
document.getElementById("toolbars").style.display = "block";
document.getElementById("palette").style.display = "block";
this.activity.prepSearchWidget();
window.widgetWindows.showWindows();
document.querySelector(".canvasHolder").classList.remove("hide");
document.querySelector("#canvas").style.display = "";
document.querySelector("#theme-color").content = platformColor.header;
this.activity.stage.enableDOMEvents(true);
window.scroll(0, 0);
docById("buttoncontainerBOTTOM").style.display = "block";
docById("buttoncontainerTOP").style.display = "block";
};
/**
* Shows the planet interface.
*/
this.showPlanet = () => {
const png = docById("overlayCanvas").toDataURL("image/png");
this.planet.open(png); // this.mainCanvas.toDataURL("image/png"));
this.iframe.style.display = "block";
this.iframe.style.zIndex = "9999" ;
try {
this.iframe.contentWindow.document.getElementById("local-tab").click();
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
}
};
/**
* Hides the planet interface.
*/
this.hidePlanet = () => {
this.iframe.style.display = "none";
};
/**
* Opens the planet interface.
*/
this.openPlanet = () => {
this.saveLocally();
this.hideMusicBlocks();
this.showPlanet();
};
/**
* Closes the planet interface.
*/
this.closePlanet = () => {
this.hidePlanet();
this.showMusicBlocks();
};
/**
* Loads a project from data.
* @param {string} data - The project data to load.
* @param {boolean} [merge=false] - Whether to merge with existing project data.
*/
this.loadProjectFromData = (data, merge) => {
if (merge === undefined) {
merge = false;
}
this.closePlanet();
if (!merge) {
this.activity.sendAllToTrash(false, true);
}
if (data === undefined) {
this.errorMsg(_("project undefined"));
return;
}
this.activity.textMsg(this.getCurrentProjectName());
this.activity.loading = true;
document.body.style.cursor = "wait";
this.activity.doLoadAnimation();
this.activity._allClear(false);
// First, hide the palettes as they will need updating.
this.activity.blocks.palettes._hideMenus(true);
const __afterLoad = () => {
document.removeEventListener("finishedLoading", __afterLoad);
};
if (document.addEventListener) {
document.addEventListener("finishedLoading", __afterLoad);
} else {
document.attachEvent("finishedLoading", __afterLoad);
}
try {
const obj = JSON.parse(data);
this.activity.blocks.loadNewBlocks(obj);
} catch (e) {
this.errorMsg(e);
}
this.activity.loading = false;
document.body.style.cursor = "default";
};
/**
* Function to trigger loading a project from a file.
* Focuses on the file input element and clicks it to trigger file selection.
* Scrolls the window to the top.
*/
this.loadProjectFromFile = () => {
document.querySelector("#myOpenFile").focus();
document.querySelector("#myOpenFile").click();
window.scroll(0, 0);
};
/**
* Function to create a new project.
* Closes the current project if open, initializes a new project, loads the start page, and saves the project locally.
*/
this.newProject = () => {
this.closePlanet();
this.initialiseNewProject();
this.activity._loadStart();
this.saveLocally();
};
/**
* Initializes a new project with the provided name or a default name.
* Clears the canvas, resets project data, and refreshes the canvas.
* @param {string} [name] - The name of the new project.
*/
this.initialiseNewProject = (name) => {
this.planet.ProjectStorage.initialiseNewProject(name);
this.activity.sendAllToTrash();
this.activity.refreshCanvas();
this.activity.blocks.trashStacks = [];
};
/**
* Function to save the current project locally.
* Prepares project data for export, generates SVG data, and saves the project data locally.
*/
this.saveLocally = () => {
this.activity.stage.update(event);
const data = this.activity.prepareExport();
const svgData = doSVG(
this.activity.canvas,
this.activity.logo,
this.activity.turtles,
320,
240,
320 / this.activity.canvas.width
);
try {
if (svgData == null || svgData === "") {
this.planet.ProjectStorage.saveLocally(data, null);
} else {
const img = new Image();
const t = this;
img.onload = () => {
const bitmap = new createjs.Bitmap(img);
const bounds = bitmap.getBounds();
bitmap.cache(bounds.x, bounds.y, bounds.width, bounds.height);
t.planet.ProjectStorage.saveLocally(data, bitmap.bitmapCache.getCacheDataURL());
};
img.src =
"data:image/svg+xml;base64," +
window.btoa(base64Encode(svgData));
}
} catch (e) {
if (
e.code === DOMException.QUOTA_EXCEEDED_ERR ||
e.message === "Not enough space to save locally"
)
this.activity.textMsg(
_(
"Error: Unable to save because you ran out of local storage. Try deleting some saved projects."
)
);
else {
// eslint-disable-next-line no-console
console.error(e);
throw e;
}
}
};
/**
* Function to asynchronously open the current project.
* @returns {Promise} - A promise that resolves with the current project data.
*/
this.openCurrentProject = async () => {
return await this.planet.ProjectStorage.getCurrentProjectData();
};
/**
* Opens a project from the Planet by its ID.
* @param {string} id - The ID of the project to open.
* @param {string} [error] - Error message if project opening fails.
*/
this.openProjectFromPlanet = (id, error) => {
this.planet.openProjectFromPlanet(id, error);
};
/**
* Function to execute when the converter is loaded.
* Sets the Converter object in the window context.
*/
this.onConverterLoad = () => {
window.Converter = this.planet.Converter;
};
/**
* Retrieves the name of the current project.
* @returns {string} - The name of the current project.
*/
this.getCurrentProjectName = () => {
return this.planet.ProjectStorage.getCurrentProjectName();
};
/**
* Retrieves the description of the current project.
* @returns {string} - The description of the current project.
*/
this.getCurrentProjectDescription = () => {
return this.planet.ProjectStorage.getCurrentProjectDescription();
};
/**
* Retrieves the image associated with the current project.
* @returns {string} - The URL of the image associated with the current project.
*/
this.getCurrentProjectImage = () => {
return this.planet.ProjectStorage.getCurrentProjectImage();
};
/**
* Retrieves the timestamp of the last time the project was saved.
* @returns {Date} - The timestamp of the last save operation.
*/
this.getTimeLastSaved = () => {
return this.planet.ProjectStorage.TimeLastSaved;
};
/**
* Initializes the Planet iframe and sets up communication between the main window and the iframe.
* Sets up event handlers and initializes Converter.
*/
this.init = async () => {
this.iframe = document.getElementById("planet-iframe");
this.iframe.style.backgroundColor = platformColor.background;
try {
await this.iframe.contentWindow.makePlanet(
_THIS_IS_MUSIC_BLOCKS_,
this.activity.storage,
window._
);
this.planet = this.iframe.contentWindow.p;
this.planet.setLoadProjectFromData(this.loadProjectFromData.bind(this));
this.planet.setPlanetClose(this.closePlanet.bind(this));
this.planet.setLoadNewProject(this.newProject.bind(this));
this.planet.setLoadProjectFromFile(this.loadProjectFromFile.bind(this));
this.planet.setOnConverterLoad(this.onConverterLoad.bind(this));
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
this.planet = null;
}
window.Converter = this.planet.Converter;
this.mainCanvas = this.activity.canvas;
};
}
}