ContainmentCanvas.jsx 6.83 KB
Newer Older
Tamas Kecskes's avatar
Tamas Kecskes committed
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import React from 'react';
import PropTypes from 'prop-types';
import {DropTarget} from 'react-dnd';

import SingleConnectedNode from './SingleConnectedNode';
import DRAG_TYPES from '../../utils/dragTypes';
import ContainmentCanvasItem from './ContainmentCanvasItem';
import ConnectionManager from './ConnectionManager';
import BasicConnectingComponent from './BasicConnectingComponent';
import BasicEventManager from '../BasicEventManager/BasicEventManager';
import getIndexedName from '../../utils/getIndexedName';

import Z_LEVELS from '../../utils/zLevels';
import ContainmentCanvasInfoCard from './ContainmentCanvasInfoCard';

// TODO we only take loaded children into account
function getChildrenNames(gmeClient, nodeId) {
    const container = gmeClient.getNode(nodeId);
    const childrenIds = container.getChildrenIds();
    const names = [];

    childrenIds.forEach((childId) => {
        const node = gmeClient.getNode(childId);
        if (node) {
            names.push(node.getAttribute('name'));
        }
    });

    return names;
}

const canvasTarget = {
    drop(props, monitor, canvas) {
        const dragItem = monitor.getItem();
        if (dragItem.move) {
            const offset = monitor.getDifferenceFromInitialOffset();
            const node = props.gmeClient.getNode(dragItem.gmeId);
            const position = node.getRegistry('position');

            position.x += offset.x / props.scale;
            position.y += offset.y / props.scale;

            position.x = Math.trunc(position.x);
            position.y = Math.trunc(position.y);

            props.gmeClient.setRegistry(dragItem.gmeId, 'position', position);
        } else if (dragItem.create) {
            const dragOffset = monitor.getClientOffset();
            const metaNode = props.gmeClient.getNode(dragItem.gmeId);
            const position = {
                x: ((dragOffset.x - canvas.offset.x) + canvas.props.scrollPos.x) / props.scale,
                y: ((dragOffset.y - canvas.offset.y) + canvas.props.scrollPos.y) / props.scale,
            };
            let name = metaNode.getAttribute('ShortName') || metaNode.getAttribute('name');

            name = getIndexedName(name, getChildrenNames(props.gmeClient, props.activeNode));

            position.x -= dragItem.nodeData.bbox.width / 2;
            position.y -= dragItem.nodeData.bbox.height / 2;

            position.x = Math.trunc(position.x);
            position.y = Math.trunc(position.y);

            // TODO: Fix when client accepts 0
            position.x = position.x > 0 ? position.x : 1;
            position.y = position.y > 0 ? position.y : 1;

            props.gmeClient.createNode({
                parentId: props.activeNode,
                baseId: dragItem.gmeId,
            }, {
                attributes: {
                    name,
                },
                registry: {
                    position,
                },
            });
        }

        canvas.setState({dragMode: 'none'});
    },
    hover(props, monitor, component) {
        const item = monitor.getItem();
        let dragState;
        if (item.create) {
            dragState = 'create';
        }
        if (item.move) {
            dragState = 'move';
        }

        component.setState({dragMode: dragState});
    },
};

function collect(connector, monitor) {
    return {
        connectDropTarget: connector.dropTarget(),
        isOver: monitor.isOver(),
    };
}

class ContainmentCanvas extends SingleConnectedNode {
    static propTypes = {
        gmeClient: PropTypes.object.isRequired,
        scrollPos: PropTypes.object.isRequired,

        activeNode: PropTypes.string.isRequired,
        scale: PropTypes.number.isRequired,

        connectDropTarget: PropTypes.func.isRequired,
        isOver: PropTypes.bool.isRequired,
    };

    state = {
        children: [],
        nodeInfo: {},
        dragMode: 'none',
    };

    cm = null;
    em = null;

    offset = {
        x: 0,
        y: 0,
    };

    constructor(props) {
        super(props);
        this.cm = new ConnectionManager();
        this.em = new BasicEventManager();
    }

    populateChildren(nodeObj) {
        const childrenIds = nodeObj.getChildrenIds();
        const newChildren = childrenIds.map(id => ({id}));

        this.setState({
            children: newChildren,
            nodeInfo: {
                name: nodeObj.getAttribute('name'),
            },
        });
    }

    onNodeLoad(nodeObj) {
        this.populateChildren(nodeObj, true);
    }

    onNodeUpdate(nodeObj) {
        this.populateChildren(nodeObj);
    }

    onMouseClick = (event) => {
        event.stopPropagation();
        event.preventDefault();
        if (this.cm.isConnecting) {
            this.cm.endConnection();
        }
        this.props.clearSelection();
    };

    onMouseLeave = (event) => {
        event.stopPropagation();
        if (this.cm.isConnecting) {
            this.cm.endConnection();
        }
    };

    onMouseMove = (event) => {
        const {scrollPos} = this.props;
        this.cm.onMouseMove({
            x: event.clientX + (scrollPos.x - this.offset.x),
            y: event.clientY + (scrollPos.y - this.offset.y),
        });
    };

    render() {
        const {connectDropTarget, activeNode, gmeClient} = this.props;
        const {children, dragMode} = this.state;

        const childrenItems = children.map(child => (<ContainmentCanvasItem
            key={child.id}
            gmeClient={gmeClient}
            activeNode={child.id}
            contextNode={activeNode}
            connectionManager={this.cm}
            eventManager={this.em}
        />));

        const content = (
            <div
                ref={(canvas) => {
                    if (canvas) {
                        const {offsetLeft, offsetTop} = canvas.offsetParent;

                        this.offset = {
                            x: offsetLeft,
                            y: offsetTop,
                        };
                    }
                }}
                style={{
                    backgroundColor: dragMode === 'create' ? 'lightgreen' : undefined,
                    width: '100%',
                    height: '100%',
                    overflow: 'auto',
                    zIndex: Z_LEVELS.canvas,
                    position: 'absolute',
                }}
                role="presentation"
                onClick={this.onMouseClick}
                onKeyDown={() => {}}
                onContextMenu={this.onMouseClick}
                onMouseLeave={this.onMouseLeave}
                onMouseMove={this.onMouseMove}
            >
                <BasicConnectingComponent connectionManager={this.cm}/>
                {childrenItems.length > 0 ? childrenItems : ContainmentCanvasInfoCard()}
            </div>);

        return connectDropTarget(content);
    }
}

export default (DropTarget(DRAG_TYPES.GME_NODE, canvasTarget, collect)(ContainmentCanvas));