variabletime/bin/web/js/artboard.js

179 lines
6.2 KiB
JavaScript
Raw Normal View History

2023-09-24 18:39:52 +02:00
import {
makeEven,
mapValue,
} from './utils.js';
const Artboard = function(tp, domElement = false, autoInit = true) {
//private
let animationFrameId = false;
let width = window.innerWidth;
let height = window.innerHeight;
let x = 0;
let y = 0;
let props = {
backgroundColor: tp.core.types.rgba({
r: 74,
g: 94,
b: 181,
a: 1
}),
x: tp.core.types.number(x),
y: tp.core.types.number(y),
width: tp.core.types.number(width),
height: tp.core.types.number(height),
zoom: tp.core.types.number(1, {
range: [config.artboard.minimumZoom, config.artboard.maximumZoom],
nudgeMultiplier: config.artboard.incrementZoom
}),
pixelDensity: tp.core.types.number(1.0, {
range: [config.artboard.incrementPixelDensity, config.artboard.maximumPixelDensity],
nudgeMultiplier: config.artboard.incrementPixelDensity
}),
};
const onValuesChange = (values) => {
window.isRenderDirty = true;
//window.cancelAnimationFrame(animationFrameId);
//animationFrameId = window.requestAnimationFrame(() => {
//domElement.style.backgroundColor = `${values.backgroundColor.toString()}`;
//});
// backward compatibility
if (values.zoom < config.artboard.minimumZoom) {
values.zoom = config.artboard.minimumZoom;
}
// makes sure that the number
// is both integer and even
//let makeEven = (n) => {
//let nr = Math.round(n);
//return nr - nr % 2;
//}
values.width = makeEven(values.width);
values.height = makeEven(values.height);
// We are absolutely aware that swearwords in code
// can be considered unprofessional bad practice, but
// (smiley) fuck this:
//const nw = makeRight(values.width);
//const nh = makeRight(values.height);
//const np = [];
//if (nw !== values.width) {
//values.width = nw;
//np.push({'key': 'width', 'value': nw});
//}
//if (nh !== values.height) {
//values.height = nh;
//np.push({'key': 'height', 'value': nh});
//}
//if (np.length > 0 && typeof this.theatreObject !== 'undefined') {
//tp.studio.transaction(({
//set
//}) => {
//np.forEach((e) => {
//set(this.theatreObject.props[e.key], e.value);
//});
//});
let cppProps = props2cppProps(values);
Module.setArtboardProps(cppProps);
};
const init = () => {
props.width.default = width;
props.height.default = height;
if (window.devicePixelRatio > 1) {
const pixelDensity = mapValue(window.devicePixelRatio, 1, 2, 1, 1.4);
const zoom = pixelDensity;
props.pixelDensity.default = pixelDensity;
props.zoom.default = zoom;
}
//this.theatreObject = tp.addObject('artboard', this.props, this.onValuesChange);
this.theatreObject = tp.addObject('artboard', this.props, this.onValuesChange);
tp.studio.transaction(({
set
}) => {
set(this.theatreObject.props, this.theatreObject.value);
});
//tp.studio.transaction(({ set }) => {
//set(this.theatreObject.props.width, width);
//set(this.theatreObject.props.height, height);
//});
};
const props2cppProps = (_props) => {
let cppProps = JSON.parse(JSON.stringify(_props));
let bgIsArray = Array.isArray(cppProps.backgroundColor);
if (bgIsArray && cppProps.backgroundColor.length === 4) {
// nothing to do
} else if (!bgIsArray && cppProps.backgroundColor.hasOwnProperty('r')) {
cppProps.backgroundColor = [
cppProps.backgroundColor.r,
cppProps.backgroundColor.g,
cppProps.backgroundColor.b,
cppProps.backgroundColor.a
];
} else if (!bgIsArray && cppProps.backgroundColor.default.hasOwnProperty('r')) {
cppProps.backgroundColor = [
cppProps.backgroundColor.default.r,
cppProps.backgroundColor.default.g,
cppProps.backgroundColor.default.b,
cppProps.backgroundColor.default.a
];
} else {
console.error('js::layer::props2cppProps', 'color could not be translated');
}
return cppProps;
};
this.values2cppProps = props2cppProps;
// public
this.props = props;
this.onValuesChange = onValuesChange;
this.init = init;
let panelFinderTimeout = false;
this.findInjectPanel = () => {
let doItAgain = true;
if (tp.studio.selection.length === 0 || (tp.studio.selection.length > 0 && tp.studio.selection[0].address.objectKey !== this.id())) {
// do it again
} else {
const panel = tp.getPanel();
if (panel !== null) {
tp.setPanelClasses();
// adjust label text from unfriendly to friendly
Object.keys(config.artboard.friendlyNames).forEach((unfriendlyName) => {
const friendlyName = config.artboard.friendlyNames[unfriendlyName];
const panelPropTitle = tp.getPanelPropTitle(unfriendlyName);
if (panelPropTitle !== null &&
friendlyName !== '') {
panelPropTitle.innerHTML = friendlyName;
}
});
doItAgain = false;
}
}
if (doItAgain) {
clearTimeout(panelFinderTimeout);
panelFinderTimeout = setTimeout(() => {
this.findInjectPanel();
}, 30);
window.artboardPanelFinderTimeout = panelFinderTimeout;
}
};
this.id = ()=> {
return 'artboard';
};
this.hide = () => {
// nothing to do
};
// action
if (typeof domElement !== 'object') {
console.error('whoops, please pass a domElement to Artboard');
}
if (autoInit) {
this.init();
}
};
export {
Artboard
}