aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/RadialMenuItem.tsx
blob: 6f10e7b65bdff50472c2c2d5f0beeb5546bd045e (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* eslint-disable react/require-default-props */
import { IconProp } from '@fortawesome/fontawesome-svg-core';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { observer } from 'mobx-react';
import * as React from 'react';
import { UndoManager } from '../../util/UndoManager';

export interface RadialMenuProps {
    description: string;
    event: (stuff?: any) => void;
    undoable?: boolean;
    icon: IconProp;
    closeMenu?: () => void;
    min?: number;
    max?: number;
    selected: number;
}

@observer
export class RadialMenuItem extends React.Component<RadialMenuProps> {
    componentDidMount() {
        this.setcircle();
    }

    componentDidUpdate() {
        this.setcircle();
    }

    handleEvent = async (e: React.PointerEvent) => {
        this.props.closeMenu && this.props.closeMenu();
        let batch: UndoManager.Batch | undefined;
        if (this.props.undoable !== false) {
            batch = UndoManager.StartBatch(`Context menu event: ${this.props.description}`);
        }
        await this.props.event({ x: e.clientX, y: e.clientY });
        batch && batch.end();
    };

    setcircle() {
        let circlemin = 0;
        let circlemax = 1;
        this.props.min ? (circlemin = this.props.min) : null;
        this.props.max ? (circlemax = this.props.max) : null;
        if (document.getElementById('myCanvas') !== null) {
            const c: any = document.getElementById('myCanvas');
            let color = 'white';
            switch (circlemin % 3) {
                case 1:
                    color = '#c2c2c5';
                    break;
                case 0:
                    color = '#f1efeb';
                    break;
                case 2:
                    color = 'lightgray';
                    break;
                default:
            }
            if (circlemax % 3 === 1 && circlemin === circlemax - 1) {
                color = '#c2c2c5';
            }

            if (this.props.selected === this.props.min) {
                color = '#808080';
            }
            if (c.getContext) {
                const ctx = c.getContext('2d');
                ctx.beginPath();
                ctx.arc(150, 150, 150, (circlemin / circlemax) * 2 * Math.PI, ((circlemin + 1) / circlemax) * 2 * Math.PI);
                ctx.arc(150, 150, 50, ((circlemin + 1) / circlemax) * 2 * Math.PI, (circlemin / circlemax) * 2 * Math.PI, true);
                ctx.fillStyle = color;
                ctx.fill();
            }
        }
    }

    calculatorx() {
        let circlemin = 0;
        let circlemax = 1;
        this.props.min ? (circlemin = this.props.min) : null;
        this.props.max ? (circlemax = this.props.max) : null;
        const avg = (circlemin / circlemax + (circlemin + 1) / circlemax) / 2;
        const degrees = 360 * avg;
        const x = 100 * Math.cos((degrees * Math.PI) / 180);
        return x;
    }

    calculatory() {
        let circlemin = 0;
        let circlemax = 1;
        this.props.min ? (circlemin = this.props.min) : null;
        this.props.max ? (circlemax = this.props.max) : null;
        const avg = (circlemin / circlemax + (circlemin + 1) / circlemax) / 2;
        const degrees = 360 * avg;
        const y = -100 * Math.sin((degrees * Math.PI) / 180);
        return y;
    }

    render() {
        return (
            <div className={'radialMenu-item' + (this.props.selected ? ' radialMenu-itemSelected' : '')} onPointerUp={this.handleEvent}>
                <canvas id="myCanvas" height="300" width="300">
                    {' '}
                    Your browser does not support the HTML5 canvas tag.
                </canvas>
                <FontAwesomeIcon icon={this.props.icon} size="3x" style={{ position: 'absolute', left: this.calculatorx() + 150 - 19, top: this.calculatory() + 150 - 19 }} />
            </div>
        );
    }
}