mirror of
https://github.com/tobspr/shapez.io.git
synced 2025-06-13 13:04:03 +00:00
Use clipboard API
Replace event listener callbacks with clipboard API methods. Added useClipboard to debug options.
This commit is contained in:
parent
0753dc536e
commit
f3381ad43f
@ -113,5 +113,8 @@ export default {
|
|||||||
// Disables slow asserts, useful for debugging performance
|
// Disables slow asserts, useful for debugging performance
|
||||||
// disableSlowAsserts: true,
|
// disableSlowAsserts: true,
|
||||||
// -----------------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------------
|
||||||
|
// Enable copy and paste using system clipboard
|
||||||
|
// useClipboard: true,
|
||||||
|
// -----------------------------------------------------------------------------------
|
||||||
/* dev:end */
|
/* dev:end */
|
||||||
};
|
};
|
||||||
|
@ -13,6 +13,9 @@ import { KEYMAPPINGS } from "../../key_action_mapper";
|
|||||||
import { BaseHUDPart } from "../base_hud_part";
|
import { BaseHUDPart } from "../base_hud_part";
|
||||||
import { Entity } from "../../entity";
|
import { Entity } from "../../entity";
|
||||||
import { DynamicDomAttach } from "../dynamic_dom_attach";
|
import { DynamicDomAttach } from "../dynamic_dom_attach";
|
||||||
|
import { globalConfig } from "../../../core/config";
|
||||||
|
|
||||||
|
const copy = require("clipboard-copy");
|
||||||
|
|
||||||
const logger = createLogger("blueprint_placer");
|
const logger = createLogger("blueprint_placer");
|
||||||
|
|
||||||
@ -55,9 +58,6 @@ export class HUDBlueprintPlacer extends BaseHUDPart {
|
|||||||
this.trackedCanAfford = new TrackedState(this.onCanAffordChanged, this);
|
this.trackedCanAfford = new TrackedState(this.onCanAffordChanged, this);
|
||||||
|
|
||||||
this.serializer = new SerializerInternal();
|
this.serializer = new SerializerInternal();
|
||||||
|
|
||||||
// TODO: This probably belongs at a higher level
|
|
||||||
document.addEventListener("paste", this.pasteFromClipboard.bind(this));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
abortPlacement() {
|
abortPlacement() {
|
||||||
@ -159,6 +159,8 @@ export class HUDBlueprintPlacer extends BaseHUDPart {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.currentBlueprint.set(Blueprint.fromUids(this.root, uids));
|
this.currentBlueprint.set(Blueprint.fromUids(this.root, uids));
|
||||||
|
|
||||||
|
this.copyToClipboard();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -177,16 +179,22 @@ export class HUDBlueprintPlacer extends BaseHUDPart {
|
|||||||
/**
|
/**
|
||||||
* Attempts to paste the last blueprint
|
* Attempts to paste the last blueprint
|
||||||
*/
|
*/
|
||||||
pasteBlueprint() {
|
async pasteBlueprint() {
|
||||||
if (this.lastBlueprintUsed !== null) {
|
/** @type {Blueprint|void} */
|
||||||
if (this.lastBlueprintUsed.layer !== this.root.currentLayer) {
|
let blueprint = null;
|
||||||
|
if (G_IS_DEV && globalConfig.debug.useClipboard) {
|
||||||
|
blueprint = await this.pasteFromClipboard();
|
||||||
|
}
|
||||||
|
blueprint = blueprint || this.lastBlueprintUsed;
|
||||||
|
if (blueprint !== null) {
|
||||||
|
if (blueprint.layer !== this.root.currentLayer) {
|
||||||
// Not compatible
|
// Not compatible
|
||||||
this.root.soundProxy.playUiError();
|
this.root.soundProxy.playUiError();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.root.hud.signals.pasteBlueprintRequested.dispatch();
|
this.root.hud.signals.pasteBlueprintRequested.dispatch();
|
||||||
this.currentBlueprint.set(this.lastBlueprintUsed);
|
this.currentBlueprint.set(blueprint);
|
||||||
} else {
|
} else {
|
||||||
this.root.soundProxy.playUiError();
|
this.root.soundProxy.playUiError();
|
||||||
}
|
}
|
||||||
@ -212,56 +220,82 @@ export class HUDBlueprintPlacer extends BaseHUDPart {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {ClipboardEvent} event
|
* Copy blueprint to system clipboard
|
||||||
*/
|
*/
|
||||||
pasteFromClipboard(event) {
|
async copyToClipboard() {
|
||||||
const data = event.clipboardData.getData("Text");
|
if (!G_IS_DEV || !globalConfig.debug.useClipboard) {
|
||||||
if (!data) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let json;
|
const blueprint = this.currentBlueprint.get();
|
||||||
|
// TODO: Move serialize() to Blueprint?
|
||||||
|
const serializedBP = this.serializer.serializeEntityArray(blueprint.getEntities());
|
||||||
|
const json = JSON.stringify(serializedBP);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
json = JSON.parse(data);
|
await copy(json);
|
||||||
} catch (error) {
|
this.root.soundProxy.playUi(SOUNDS.copy);
|
||||||
logger.error("Unable to parse clipboard data:", error.message);
|
logger.debug("Copied blueprint to clipboard");
|
||||||
return;
|
} catch (e) {
|
||||||
|
logger.error("Copy to clipboard failed:", e.message);
|
||||||
}
|
}
|
||||||
if (!verifyBlueprintData(json)) {
|
}
|
||||||
logger.error("Invalid clipboard data");
|
|
||||||
return;
|
/**
|
||||||
|
* Attempt to get Blueprint from clipboard
|
||||||
|
* @returns {Promise<Blueprint|void>}
|
||||||
|
*/
|
||||||
|
async pasteFromClipboard() {
|
||||||
|
let data;
|
||||||
|
try {
|
||||||
|
data = await paste();
|
||||||
|
// logger.debug("Paste data:", data);
|
||||||
|
} catch (e) {
|
||||||
|
logger.error("Problems with paste:", e.message);
|
||||||
}
|
}
|
||||||
logger.log("Paste blueprint from clipboard");
|
return this.deserializeBlueprint(data);
|
||||||
const entityArray = [];
|
}
|
||||||
for (let i = 0; i < json.length; ++i) {
|
|
||||||
const serializedEntity = json[i];
|
/**
|
||||||
const result = this.serializer.deserializeEntity(this.root, serializedEntity);
|
* @param {string} data
|
||||||
if (typeof result == "string") {
|
* @retruns {Blueprint|void}
|
||||||
logger.error(result);
|
*/
|
||||||
|
deserializeBlueprint(data) {
|
||||||
|
// TODO: Move to Blueprint?
|
||||||
|
try {
|
||||||
|
let json = JSON.parse(data);
|
||||||
|
if (typeof json != "object") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
entityArray.push(result);
|
if (!Array.isArray(json)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
/** @type {Array<Entity>} */
|
||||||
|
const entityArray = [];
|
||||||
|
for (let i = 0; i < json.length; ++i) {
|
||||||
|
/** @type {Entity?} */
|
||||||
|
const value = json[i];
|
||||||
|
if (value.components == undefined || value.components.StaticMapEntity == undefined) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const result = this.serializer.deserializeEntity(this.root, value);
|
||||||
|
if (typeof result === "string") {
|
||||||
|
throw new Error(result);
|
||||||
|
}
|
||||||
|
entityArray.push(result);
|
||||||
|
}
|
||||||
|
const newBP = new Blueprint(entityArray);
|
||||||
|
return newBP;
|
||||||
|
} catch (e) {
|
||||||
|
logger.error("Invalid bluerint data:", e.message);
|
||||||
}
|
}
|
||||||
const newBP = new Blueprint(entityArray);
|
|
||||||
this.currentBlueprint.set(newBP);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* TODO: Move this to a module */
|
||||||
/**
|
/**
|
||||||
* Verify data is a valid serialized blueprint
|
* @returns {Promise<string>}
|
||||||
* @param {Array<Entity>} data
|
|
||||||
*/
|
*/
|
||||||
function verifyBlueprintData(data) {
|
async function paste() {
|
||||||
if (typeof data != "object") {
|
/* TODO: Add fallback method */
|
||||||
return false;
|
return navigator.clipboard.readText();
|
||||||
}
|
|
||||||
if (!Array.isArray(data)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
for (let i = 0; i < data.length; ++i) {
|
|
||||||
const value = data[i];
|
|
||||||
if (value.components == undefined || value.components.StaticMapEntity == undefined) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
@ -45,9 +45,6 @@ export class HUDMassSelector extends BaseHUDPart {
|
|||||||
this.root.signals.editModeChanged.add(this.clearSelection, this);
|
this.root.signals.editModeChanged.add(this.clearSelection, this);
|
||||||
|
|
||||||
this.serializer = new SerializerInternal();
|
this.serializer = new SerializerInternal();
|
||||||
|
|
||||||
// TODO: This probably belongs at a higher level
|
|
||||||
document.addEventListener("copy", this.copyToClipboard.bind(this));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -208,24 +205,6 @@ export class HUDMassSelector extends BaseHUDPart {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {ClipboardEvent} event
|
|
||||||
*/
|
|
||||||
copyToClipboard(event) {
|
|
||||||
if (this.selectedUids.size == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const entityUids = Array.from(this.selectedUids);
|
|
||||||
const blueprint = Blueprint.fromUids(this.root, entityUids);
|
|
||||||
const serializedBP = this.serializer.serializeEntityArray(blueprint.getEntities());
|
|
||||||
const json = JSON.stringify(serializedBP);
|
|
||||||
event.clipboardData.setData("text/plain", json);
|
|
||||||
event.preventDefault();
|
|
||||||
|
|
||||||
logger.log("Copied selection to clipboard");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* mouse down pre handler
|
* mouse down pre handler
|
||||||
* @param {Vector} pos
|
* @param {Vector} pos
|
||||||
|
@ -33,7 +33,7 @@ export class SerializerInternal {
|
|||||||
for (let i = 0; i < array.length; ++i) {
|
for (let i = 0; i < array.length; ++i) {
|
||||||
const serializedEntity = array[i];
|
const serializedEntity = array[i];
|
||||||
const result = this.deserializeEntity(root, serializedEntity);
|
const result = this.deserializeEntity(root, serializedEntity);
|
||||||
if (typeof result == "string") {
|
if (typeof result === "string") {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
result.uid = serializedEntity.uid;
|
result.uid = serializedEntity.uid;
|
||||||
@ -64,7 +64,7 @@ export class SerializerInternal {
|
|||||||
rotationVariant: data.rotationVariant,
|
rotationVariant: data.rotationVariant,
|
||||||
variant: data.variant,
|
variant: data.variant,
|
||||||
});
|
});
|
||||||
|
|
||||||
const errorStatus = this.deserializeComponents(root, entity, payload.components);
|
const errorStatus = this.deserializeComponents(root, entity, payload.components);
|
||||||
|
|
||||||
return errorStatus || entity;
|
return errorStatus || entity;
|
||||||
|
Loading…
Reference in New Issue
Block a user