From 24b07c3557a173fa898477eb8ce88a9aaf7112bc Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sun, 10 Apr 2022 17:41:09 -0400 Subject: [PATCH] new movable action --- .../Output/Print/MainSystems/movable.js | 67 ++++++++++++++++ src/lib/components/Output/Print/movable.js | 79 +++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 src/lib/components/Output/Print/MainSystems/movable.js create mode 100644 src/lib/components/Output/Print/movable.js diff --git a/src/lib/components/Output/Print/MainSystems/movable.js b/src/lib/components/Output/Print/MainSystems/movable.js new file mode 100644 index 0000000..8b08a50 --- /dev/null +++ b/src/lib/components/Output/Print/MainSystems/movable.js @@ -0,0 +1,67 @@ +import VanillaMoveable, { PROPERTIES, EVENTS } from "moveable"; +import { camelize, isUndefined } from "@daybrush/utils"; + +function createMoveable(node,options) { + + let translate = [0,0]; + + options = { + originDraggable: true, + originRelative: true, + draggable: true, + throttleDrag: 0, + zoom: 1, + origin: false, + onDrag(e) { + translate = e.beforeTranslate; + node.dispatchEvent(new CustomEvent('translate', { detail: translate})); + }, + target: node, + ...options, + }; + + const moveable = new VanillaMoveable(document.body, options ); + + EVENTS.forEach((name) => { + const onName = camelize(`on ${name}`); + moveable.on(name, (e) => { + const result = options[onName] && options[onName](e); + const result2 = node.dispatchEvent(new CustomEvent(name, { detail: e })); + + return !isUndefined(result) + ? result + : !isUndefined(result2) + ? result2 + : undefined; + }); + }); + + return moveable; +} + +export function movable(node, options) { + + let moveable = options.disabled ? undefined : createMoveable(options); + + + const destroy = () => { + if(!moveable) return; + moveable.destroy(); + moveable = undefined; + }; + + const update = async (params) => { + if (params.disabled) { + destroy(); + } else { + if (!moveable) { + moveable = createMoveable(node, params); + } + } + }; + + return { + destroy, + update, + }; +} diff --git a/src/lib/components/Output/Print/movable.js b/src/lib/components/Output/Print/movable.js new file mode 100644 index 0000000..234a4a0 --- /dev/null +++ b/src/lib/components/Output/Print/movable.js @@ -0,0 +1,79 @@ +import VanillaMoveable, { PROPERTIES, EVENTS } from "moveable"; +import { camelize, isUndefined } from "@daybrush/utils"; + +function createMoveable(node, options) { + let translate = [0, 0]; + + let ship = options.ship; + let system = options.system; + delete options.ship; + delete options.system; + + if( Array.isArray(system) ) { + system = { system: system[0], systemId: system[1] }; + } + else { system = {system}; } + + options = { + originDraggable: true, + originRelative: true, + draggable: true, + throttleDrag: 0, + zoom: 1, + origin: false, + onDrag(e) { + translate = e.beforeTranslate; + node.dispatchEvent( + new CustomEvent("translate", { detail: translate }) + ); + ship.dispatch.setUITransform({ ...system, translate }); + }, + target: node, + ...options, + }; + + const moveable = new VanillaMoveable(document.body, options); + + EVENTS.forEach((name) => { + const onName = camelize(`on ${name}`); + moveable.on(name, (e) => { + const result = options[onName] && options[onName](e); + const result2 = node.dispatchEvent( + new CustomEvent(name, { detail: e }) + ); + + return !isUndefined(result) + ? result + : !isUndefined(result2) + ? result2 + : undefined; + }); + }); + + return moveable; +} + +export function movable(node, options) { + let moveable = options.disabled ? undefined : createMoveable(options); + + const destroy = () => { + if (!moveable) return; + moveable.destroy(); + moveable = undefined; + }; + + const update = async (params) => { + if (params.disabled) { + destroy(); + } else { + if (!moveable) { + moveable = createMoveable(node, params); + } + } + }; + + return { + destroy, + update, + }; +}