aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/imageEditor/imageMeshTool/imageMeshToolButton.tsx
blob: eb68410b01247cfe5cba6c48dd0083567726f563 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import './MeshTransformButton.scss';
import * as React from 'react';
import ReactLoading from 'react-loading';
import { Button, IconButton, Type } from '@dash/components';
import { AiOutlineInfo } from 'react-icons/ai';
import { SettingsManager } from '../../../../util/SettingsManager';
import MeshTransformGrid from './imageMesh';

interface ButtonContainerProps {
    onClick: () => Promise<void>;
    loading: boolean;
    onReset: () => void;
    btnText: string;
    imageWidth: number;
    imageHeight: number;
    gridXSize: number;  // X subdivisions
    gridYSize: number;  // Y subdivisions
}

export function MeshTransformButton({
    loading,
    onClick: startMeshTransform,
    onReset,
    btnText,
    imageWidth,
    imageHeight,
    gridXSize,
    gridYSize
}: ButtonContainerProps) {
    const [showGrid, setShowGrid] = React.useState(false);
    const [isGridInteractive, setIsGridInteractive] = React.useState(false); // Controls the dragging of control points
    const imageRef = React.useRef<HTMLImageElement>(null); // Reference to the image element

    const handleGridToggle = () => {
        if (showGrid) {
            setShowGrid(false); // Hide the grid
            setIsGridInteractive(false); // Disable control points manipulation
        } else {
            setShowGrid(true); // Show the grid
            setIsGridInteractive(true); // Enable control points manipulation
        }
    };

    return (
        <div className="meshTransformBtnContainer">
            <Button text="RESET" type={Type.PRIM} color={SettingsManager.userVariantColor} onClick={onReset} />
            {loading ? (
                <Button
                    text={btnText}
                    type={Type.TERT}
                    color={SettingsManager.userVariantColor}
                    icon={<ReactLoading type="spin" color="#ffffff" width={20} height={20} />}
                    iconPlacement="right"
                    onClick={() => {
                        if (!loading) handleGridToggle(); // Toggle the grid visibility and control points manipulation
                    }}
                />
            ) : (
                <Button
                    text={btnText}
                    type={Type.TERT}
                    color={SettingsManager.userVariantColor}
                    onClick={() => {
                        if (!loading) handleGridToggle(); // Toggle the grid visibility and control points manipulation
                    }}
                />
            )}

            {/* The IconButton will toggle the grid */}
            <IconButton
                type={Type.SEC}
                color={SettingsManager.userVariantColor}
                tooltip="Toggle Grid"
                icon={<AiOutlineInfo size="16px" />}
                onClick={handleGridToggle} // Toggle the grid when clicked
            />

            {/* Only show the grid if `showGrid` is true */}
            {showGrid && (
                <MeshTransformGrid
                    imageRef={imageRef}
                    gridXSize={gridXSize}
                    gridYSize={gridYSize}
                    isInteractive={isGridInteractive} // Pass the interactive flag to control point manipulation
                />
            )}
            <img ref={imageRef} src="your-image-source.jpg" alt="Mesh" style={{ width: imageWidth, height: imageHeight }} />
        </div>
    );
}