Skip to content
Snippets Groups Projects
Commit 6c9300d0 authored by Alexander Rose's avatar Alexander Rose
Browse files

fix useBehavior handling

parent 9e4c820e
Branches
No related tags found
No related merge requests found
/** /**
* Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info. * Copyright (c) 2020-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
* *
* @author David Sehnal <david.sehnal@gmail.com> * @author David Sehnal <david.sehnal@gmail.com>
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/ */
import * as React from 'react'; import * as React from 'react';
...@@ -24,9 +25,8 @@ export interface ScreenshotPreviewProps { ...@@ -24,9 +25,8 @@ export interface ScreenshotPreviewProps {
const _ScreenshotPreview = (props: ScreenshotPreviewProps) => { const _ScreenshotPreview = (props: ScreenshotPreviewProps) => {
const { plugin, cropFrameColor } = props; const { plugin, cropFrameColor } = props;
const helper = plugin.helpers.viewportScreenshot;
if (!helper) return null;
const helper = plugin.helpers.viewportScreenshot;
const [currentCanvas, setCurrentCanvas] = useState<HTMLCanvasElement | null>(null); const [currentCanvas, setCurrentCanvas] = useState<HTMLCanvasElement | null>(null);
const canvasRef = useRef<HTMLCanvasElement | null>(null); const canvasRef = useRef<HTMLCanvasElement | null>(null);
const propsRef = useRef(props); const propsRef = useRef(props);
...@@ -53,7 +53,7 @@ const _ScreenshotPreview = (props: ScreenshotPreviewProps) => { ...@@ -53,7 +53,7 @@ const _ScreenshotPreview = (props: ScreenshotPreviewProps) => {
function preview() { function preview() {
const p = propsRef.current; const p = propsRef.current;
if (!p.suspend && canvasRef.current) { if (!p.suspend && canvasRef.current) {
drawPreview(helper!, canvasRef.current, p.customBackground, p.borderColor, p.borderWidth); drawPreview(helper, canvasRef.current, p.customBackground, p.borderColor, p.borderWidth);
} }
if (!canvasRef.current) isDirty = true; if (!canvasRef.current) isDirty = true;
...@@ -71,8 +71,8 @@ const _ScreenshotPreview = (props: ScreenshotPreviewProps) => { ...@@ -71,8 +71,8 @@ const _ScreenshotPreview = (props: ScreenshotPreviewProps) => {
subscribe(plugin.state.data.behaviors.isUpdating, v => { subscribe(plugin.state.data.behaviors.isUpdating, v => {
if (!v) isDirty = true; if (!v) isDirty = true;
}); });
subscribe(helper.behaviors.values, () => isDirty = true); subscribe(helper?.behaviors.values, () => isDirty = true);
subscribe(helper.behaviors.cropParams, () => isDirty = true); subscribe(helper?.behaviors.cropParams, () => isDirty = true);
let resizeObserver: any = void 0; let resizeObserver: any = void 0;
if (typeof ResizeObserver !== 'undefined') { if (typeof ResizeObserver !== 'undefined') {
...@@ -109,7 +109,9 @@ export const ScreenshotPreview = React.memo(_ScreenshotPreview, (prev, next) => ...@@ -109,7 +109,9 @@ export const ScreenshotPreview = React.memo(_ScreenshotPreview, (prev, next) =>
declare const ResizeObserver: any; declare const ResizeObserver: any;
function drawPreview(helper: ViewportScreenshotHelper, target: HTMLCanvasElement, customBackground?: string, borderColor?: string, borderWidth?: number) { function drawPreview(helper: ViewportScreenshotHelper | undefined, target: HTMLCanvasElement, customBackground?: string, borderColor?: string, borderWidth?: number) {
if (!helper) return;
const { canvas, width, height } = helper.getPreview()!; const { canvas, width, height } = helper.getPreview()!;
const ctx = target.getContext('2d'); const ctx = target.getContext('2d');
if (!ctx) return; if (!ctx) return;
...@@ -152,11 +154,9 @@ function drawPreview(helper: ViewportScreenshotHelper, target: HTMLCanvasElement ...@@ -152,11 +154,9 @@ function drawPreview(helper: ViewportScreenshotHelper, target: HTMLCanvasElement
function ViewportFrame({ plugin, canvas, color = 'rgba(255, 87, 45, 0.75)' }: { plugin: PluginContext, canvas: HTMLCanvasElement | null, color?: string }) { function ViewportFrame({ plugin, canvas, color = 'rgba(255, 87, 45, 0.75)' }: { plugin: PluginContext, canvas: HTMLCanvasElement | null, color?: string }) {
const helper = plugin.helpers.viewportScreenshot; const helper = plugin.helpers.viewportScreenshot;
if (!helper || !canvas) return null; const params = useBehavior(helper?.behaviors.values);
const cropParams = useBehavior(helper?.behaviors.cropParams);
const params = useBehavior(helper.behaviors.values); const crop = useBehavior(helper?.behaviors.relativeCrop);
const cropParams = useBehavior(helper.behaviors.cropParams);
const crop = useBehavior(helper.behaviors.relativeCrop!);
const cropFrameRef = useRef<Viewport>({ x: 0, y: 0, width: 0, height: 0 }); const cropFrameRef = useRef<Viewport>({ x: 0, y: 0, width: 0, height: 0 });
useBehavior(params?.resolution.name === 'viewport' ? plugin.canvas3d?.resized : void 0); useBehavior(params?.resolution.name === 'viewport' ? plugin.canvas3d?.resized : void 0);
...@@ -164,6 +164,8 @@ function ViewportFrame({ plugin, canvas, color = 'rgba(255, 87, 45, 0.75)' }: { ...@@ -164,6 +164,8 @@ function ViewportFrame({ plugin, canvas, color = 'rgba(255, 87, 45, 0.75)' }: {
const [start, setStart] = useState([0, 0]); const [start, setStart] = useState([0, 0]);
const [current, setCurrent] = useState([0, 0]); const [current, setCurrent] = useState([0, 0]);
if (!helper || !canvas || !crop) return null;
const { width, height } = helper.getSizeAndViewport(); const { width, height } = helper.getSizeAndViewport();
const frame = getViewportFrame(width, height, canvas.clientWidth, canvas.clientHeight); const frame = getViewportFrame(width, height, canvas.clientWidth, canvas.clientHeight);
...@@ -268,7 +270,7 @@ function ViewportFrame({ plugin, canvas, color = 'rgba(255, 87, 45, 0.75)' }: { ...@@ -268,7 +270,7 @@ function ViewportFrame({ plugin, canvas, color = 'rgba(255, 87, 45, 0.75)' }: {
function finish() { function finish() {
const cropFrame = cropFrameRef.current; const cropFrame = cropFrameRef.current;
if (cropParams.auto) { if (cropParams?.auto) {
helper?.behaviors.cropParams.next({ ...cropParams, auto: false }); helper?.behaviors.cropParams.next({ ...cropParams, auto: false });
} }
helper?.behaviors.relativeCrop.next({ helper?.behaviors.relativeCrop.next({
......
...@@ -141,12 +141,14 @@ class FullSettings extends PluginUIComponent { ...@@ -141,12 +141,14 @@ class FullSettings extends PluginUIComponent {
this.subscribe(this.plugin.events.canvas3d.settingsUpdated, () => this.forceUpdate()); this.subscribe(this.plugin.events.canvas3d.settingsUpdated, () => this.forceUpdate());
this.subscribe(this.plugin.layout.events.updated, () => this.forceUpdate()); this.subscribe(this.plugin.layout.events.updated, () => this.forceUpdate());
this.subscribe(this.plugin.canvas3d!.camera.stateChanged, state => { if (this.plugin.canvas3d) {
this.subscribe(this.plugin.canvas3d.camera.stateChanged, state => {
if (state.radiusMax !== undefined || state.radius !== undefined) { if (state.radiusMax !== undefined || state.radius !== undefined) {
this.forceUpdate(); this.forceUpdate();
} }
}); });
} }
}
render() { render() {
return <> return <>
......
/** /**
* Copyright (c) 2019-2020 mol* contributors, licensed under MIT, See LICENSE file for more info. * Copyright (c) 2019-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
* *
* @author Alexander Rose <alexander.rose@weirdbyte.de> * @author Alexander Rose <alexander.rose@weirdbyte.de>
* @author David Sehnal <david.sehnal@gmail.com> * @author David Sehnal <david.sehnal@gmail.com>
...@@ -97,20 +97,20 @@ export class DownloadScreenshotControls extends PluginUIComponent<{ close: () => ...@@ -97,20 +97,20 @@ export class DownloadScreenshotControls extends PluginUIComponent<{ close: () =>
function ScreenshotParams({ plugin, isDisabled }: { plugin: PluginContext, isDisabled: boolean }) { function ScreenshotParams({ plugin, isDisabled }: { plugin: PluginContext, isDisabled: boolean }) {
const helper = plugin.helpers.viewportScreenshot; const helper = plugin.helpers.viewportScreenshot;
const values = useBehavior(helper?.behaviors.values);
if (!helper) return null; if (!helper) return null;
const values = useBehavior(helper.behaviors.values);
return <ParameterControls params={helper.params} values={values} onChangeValues={v => helper.behaviors.values.next(v)} isDisabled={isDisabled} />; return <ParameterControls params={helper.params} values={values} onChangeValues={v => helper.behaviors.values.next(v)} isDisabled={isDisabled} />;
} }
function CropControls({ plugin }: { plugin: PluginContext }) { function CropControls({ plugin }: { plugin: PluginContext }) {
const helper = plugin.helpers.viewportScreenshot; const helper = plugin.helpers.viewportScreenshot;
if (!helper) return null;
const cropParams = useBehavior(helper.behaviors.cropParams); const cropParams = useBehavior(helper?.behaviors.cropParams);
useBehavior(helper?.behaviors.relativeCrop); useBehavior(helper?.behaviors.relativeCrop);
if (!helper) return null; if (!helper || !cropParams) return null;
return <div style={{ width: '100%', height: '24px', marginTop: '8px' }}> return <div style={{ width: '100%', height: '24px', marginTop: '8px' }}>
<ToggleButton icon={CropOrginalSvg} title='Auto-crop' inline isSelected={cropParams.auto} <ToggleButton icon={CropOrginalSvg} title='Auto-crop' inline isSelected={cropParams.auto}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment