This commit is contained in:
Yanick Champoux 2022-04-10 18:21:44 -04:00
parent 423ac25a90
commit f3310f97fa
4 changed files with 120 additions and 121 deletions

View File

@ -1,9 +1,8 @@
import VanillaMoveable, { PROPERTIES, EVENTS } from "moveable"; import VanillaMoveable, { PROPERTIES, EVENTS } from "moveable";
import { camelize, isUndefined } from "@daybrush/utils"; import { camelize, isUndefined } from "@daybrush/utils";
function createMoveable(node,options) { function createMoveable(node, options) {
let translate = [0, 0];
let translate = [0,0];
options = { options = {
originDraggable: true, originDraggable: true,
@ -14,13 +13,13 @@ function createMoveable(node,options) {
origin: false, origin: false,
onDrag(e) { onDrag(e) {
translate = e.beforeTranslate; translate = e.beforeTranslate;
node.dispatchEvent(new CustomEvent('translate', { detail: translate})); node.dispatchEvent(new CustomEvent("translate", { detail: translate }));
}, },
target: node, target: node,
...options, ...options,
}; };
const moveable = new VanillaMoveable(document.body, options ); const moveable = new VanillaMoveable(document.body, options);
EVENTS.forEach((name) => { EVENTS.forEach((name) => {
const onName = camelize(`on ${name}`); const onName = camelize(`on ${name}`);
@ -40,12 +39,10 @@ function createMoveable(node,options) {
} }
export function movable(node, options) { export function movable(node, options) {
let moveable = options.disabled ? undefined : createMoveable(options); let moveable = options.disabled ? undefined : createMoveable(options);
const destroy = () => { const destroy = () => {
if(!moveable) return; if (!moveable) return;
moveable.destroy(); moveable.destroy();
moveable = undefined; moveable = undefined;
}; };

View File

@ -41,7 +41,7 @@
<div class="notice">Printing this page will only prints the ship sheet.</div> <div class="notice">Printing this page will only prints the ship sheet.</div>
<script> <script>
import { getContext } from 'svelte'; import { getContext } from "svelte";
import Identification from "./Identification/index.svelte"; import Identification from "./Identification/index.svelte";
import MainSystems from "./MainSystems/index.svelte"; import MainSystems from "./MainSystems/index.svelte";
import Hull from "./Hull/index.svelte"; import Hull from "./Hull/index.svelte";
@ -51,9 +51,12 @@ import { getContext } from 'svelte';
export let ship = {}; export let ship = {};
export let isMovable = false; export let isMovable = false;
const { dispatch } = getContext('ship'); const { dispatch } = getContext("ship");
const resetLayout = () => { isMovable = false; dispatch.resetLayout(); } const resetLayout = () => {
isMovable = false;
dispatch.resetLayout();
};
</script> </script>
<style> <style>

View File

@ -9,10 +9,11 @@ function createMoveable(node, options) {
delete options.ship; delete options.ship;
delete options.system; delete options.system;
if( Array.isArray(system) ) { if (Array.isArray(system)) {
system = { system: system[0], systemId: system[1] }; system = { system: system[0], systemId: system[1] };
} else {
system = { system };
} }
else { system = {system}; }
options = { options = {
originDraggable: true, originDraggable: true,
@ -23,9 +24,7 @@ function createMoveable(node, options) {
origin: false, origin: false,
onDrag(e) { onDrag(e) {
translate = e.beforeTranslate; translate = e.beforeTranslate;
node.dispatchEvent( node.dispatchEvent(new CustomEvent("translate", { detail: translate }));
new CustomEvent("translate", { detail: translate })
);
ship.dispatch.setUITransform({ ...system, translate }); ship.dispatch.setUITransform({ ...system, translate });
}, },
target: node, target: node,
@ -38,9 +37,7 @@ function createMoveable(node, options) {
const onName = camelize(`on ${name}`); const onName = camelize(`on ${name}`);
moveable.on(name, (e) => { moveable.on(name, (e) => {
const result = options[onName] && options[onName](e); const result = options[onName] && options[onName](e);
const result2 = node.dispatchEvent( const result2 = node.dispatchEvent(new CustomEvent(name, { detail: e }));
new CustomEvent(name, { detail: e })
);
return !isUndefined(result) return !isUndefined(result)
? result ? result

View File

@ -29,13 +29,15 @@ const dux = new Updux({
}); });
function resetUITransform(thing) { function resetUITransform(thing) {
if(typeof thing !== 'object') return thing; if (typeof thing !== "object") return thing;
return u.map((v,k) => k === 'uiTransform' ? '' : resetUITransform(v) , thing )
return u.map(
(v, k) => (k === "uiTransform" ? "" : resetUITransform(v)),
thing
);
} }
dux.setMutation( 'resetLayout', () => resetUITransform ); dux.setMutation("resetLayout", () => resetUITransform);
dux.setMutation("setShipMass", (mass) => u({ reqs: { mass } })); dux.setMutation("setShipMass", (mass) => u({ reqs: { mass } }));
dux.setMutation("setShipReqs", (reqs) => u({ reqs })); dux.setMutation("setShipReqs", (reqs) => u({ reqs }));