source: js/human_3d_alignment/src/widgets/similviewer.jsx @ 1225

Last change on this file since 1225 was 911, checked in by Maciej Komosinski, 5 years ago

Added the actual functionality of the app in place of previous draft

File size: 8.4 KB
RevLine 
[881]1/*global Module*/
2"use strict";
3import React from 'react';
4import ReactDOM from 'react-dom';
[911]5import * as THREE from 'three';
[881]6import Sviewer from '../visualization/sviewer';
7import { Md3dRotation, MdYoutubeSearchedFor } from 'react-icons/lib/md';
8
9/**
10 * Component for assesing genotypes matching.
11 */
12class GenoViewer extends React.Component {
13    /**
14     * Basic constructor.
15     * @param {any} props properties of Component
16     */
17    constructor(props) {
18        super(props);
19        this.props = props;
20        this.state = {
21            autorotate: false,
22            rezoom: true,
23            valid: true,
[911]24            clicked: false,
25            controlMode: 'translate',
26            round: 0
[881]27        };
28        this.start = this.start.bind(this);
29        this.stop = this.stop.bind(this);
30        this.animate = this.animate.bind(this);
31    }
32
33    /**
34     * Method initializes all visualizations and binds their methods and events with SViewer.
35     */
36    componentDidMount() {
37        let temp = ReactDOM.findDOMNode(this.mount);
38        let temp2 = ReactDOM.findDOMNode(this.mount).parentNode;
39        this.viewer = new Sviewer(temp2.clientWidth, temp2.clientHeight, temp);
40        this.viewer.setAutoRotate(this.state.autorotate);
41        this.viewer.setRezoom(this.state.rezoom);
[911]42
[881]43        this.renderScene = this.viewer.renderScene.bind(this.viewer);
44        this.lastw = temp2.clientWidth;
45        this.lasth = temp2.clientHeight;
46        this.start();
47    }
48
49    /**
50     * Method stops animation of Framsticks.
51     */
52    componentWillUnmount() {
53        this.stop();
54    }
55
56    /**
57     * After update of state all visualizations are refreshed
58     */
59    componentDidUpdate() {
60        let temp = ReactDOM.findDOMNode(this.mount).parentNode;
61        this.viewer.resizeView(temp.clientWidth, temp.clientHeight);
62    }
63
64    /**
65     * Method starts animation process. Internal method, used by componentDidMount.
66     */
67    start() {
68        if (!this.frameId) {
69            this.frameId = requestAnimationFrame(this.animate);
70        }
71    }
72
73    /**
74     * Main animation loop. If any change was applied to genotype, this method loads new
75     * models and brains.
76     */
77    animate() {
78        let temp = ReactDOM.findDOMNode(this.mount).parentNode;
79        if (this.lasth != temp.clientHeight || this.lastw != temp.clientWidth) {
80            this.viewer.resizeView(temp.clientWidth, temp.clientHeight);
81            this.lastw = temp.clientWidth;
82            this.lasth = temp.clientHeight;
83        }
84       
85        this.viewer.camera.getCameraControl().update();
86        this.frameId = window.requestAnimationFrame(this.animate);
87        this.viewer.scene.render(
88            this.viewer.renderer,
89            this.viewer.camera.getPerspectiveCamera());
[911]90           
91        if (!this.props.blockView) {
92
93            if (this.props.round != this.state.round) {
94                this.setState({ round: this.props.round }, function() {
95                    console.log(`Round in sim: `, this.state.round);
96                });
97                this.viewer.add2Genotypes(this.props.genotype1, this.props.genotype2);
98                this.viewer.addLines(this.props.selected1, this.props.selected2);
99            }
100            if (this.props.controlMode != this.state.controlMode) {
101                this.setState({ controlMode: this.props.controlMode }, function() {
102                    console.log('Control mode: ', this.state.controlMode);
103                    this.viewer.setMode(this.state.controlMode);
104                })
105            }
106            if (this.viewer.selected1 != this.props.selected1) {
107                this.viewer.selected1 = [];
108                this.viewer.selected1 = this.props.selected1;
109            }
110            if (this.viewer.selected2 != this.props.selected2) {
111                this.viewer.selected2 = [];
112                this.viewer.selected2 = this.props.selected2;
113            }
114            if (this.props.round == this.state.round) {
115                this.viewer.addText();
116                this.viewer.addLines(this.props.selected1, this.props.selected2);
117            } else {
118                this.viewer.clearView();
119            }
120        }
[881]121    }
122
123    /**
124     * Method cancels animation.
125     */
126    stop() {
127        cancelAnimationFrame(this.frameId);
128    }
129
130    /**
131     * Turns on/off rezoom property of viewer
132     */
133    switchRezoom() {
[911]134        this.setState({rezoom: !this.state.rezoom}, function() {
135            this.viewer.setRezoom(this.state.rezoom);
136        });
[881]137    }
138
139    /**
140     * Turns on/off autorotate property of viewer
141     */
142    switchAutorotate() {
[911]143        if (this.state.rezoom) {
144            this.switchRezoom();
145        }
146        this.setState({autorotate: !this.state.autorotate}, function() {
147            console.log(this.state.autorotate);
148            this.viewer.setAutoRotate(this.state.autorotate);
149        });
[881]150    }
151
152    /**
153     * Gets position of mouse
154     */
155    getMousePosition(evt){
156        let bound = this.mount.getBoundingClientRect();
157        let mousePosition = {x: 0, y: 0};
158        mousePosition.x = ((evt.clientX - bound.left) / bound.width) * 2 - 1;
159        mousePosition.y = -((evt.clientY - bound.top) / bound.height) * 2 + 1;
160        let vector = new THREE.Vector2(mousePosition.x, mousePosition.y);
161        return vector;
162    }
163
164       /**
165     * Event function for using mouse within viewer.
166     * @param {any} evt object holding event data
167     */
168    handleMouseDown(evt) {
[911]169        if (this.state.rezoom) {
170            this.switchRezoom();
171        }
172
173        if (this.state.autorotate) {
174            this.switchAutorotate();
175        }
176
[881]177        evt.preventDefault();
178        let mousePosition = this.getMousePosition(evt);
179        this.viewer.handleMouseDown(mousePosition);
180    }
181
182           /**
183     * Event function for using mouse within viewer.
184     * @param {any} evt object holding event data
185     */
186    handleMouseUp(evt) {
[911]187
188        let position1 = [];
189        let position2 = [];
190        let rotation1 = [];
191        let rotation2 = [];
192        this.viewer.framstick1.mesh.position.toArray(position1);
193        this.viewer.framstick2.mesh.position.toArray(position2);
194        this.viewer.framstick1.mesh.rotation.toArray(rotation1);
195        this.viewer.framstick2.mesh.rotation.toArray(rotation2);
196
197        this.props.handleChangePosition1(position1);
198        this.props.handleChangePosition2(position2);
199        this.props.handleChangeRotation1(rotation1);
200        this.props.handleChangeRotation2(rotation2);
201
[881]202        evt.preventDefault();
203        let mousePosition = this.getMousePosition(evt);
204        this.viewer.handleMouseUp(mousePosition);
205    }
206
207               /**
208     * Event function for using mouse within viewer.
209     * @param {any} evt object holding event data
210     */
211    handleMouseMove(evt) {
212        evt.preventDefault();
213        let mousePosition = this.getMousePosition(evt);
214        this.viewer.handleMouseMove(mousePosition);
[911]215    }   
[881]216     
217    render() {
218        return (
219            <table {...this.props} style={{height: '100%', width: '100%'}}>
220                <tbody>
221                    <tr onMouseDown={ev => {if (ev) ev.stopPropagation();}}>
222                        <td rowSpan={2} style={{position:'relative', verticalAlign: 'top', alignContent: 'left'}}>
[911]223                            <div id="simcontainer" style={{height: '100%', width: '100%', position: 'absolute', transform: 'translateZ(0)'}} onMouseDown={ev => {if (ev) ev.stopPropagation();}} onTouchStart={ev => {if (ev) ev.stopPropagation();}}>
[881]224                                <canvas ref={(mount) => {this.mount = mount;}} onMouseDown={(evt) => this.handleMouseDown(evt)} onMouseUp={(evt) => this.handleMouseUp(evt)} onMouseMove={(evt) => this.handleMouseMove(evt)} style={{position: 'absolute', zIndex: 1}}></canvas>
225                                <Md3dRotation onClick={() => this.switchAutorotate()}  style={{zIndex: 2, position: 'absolute', left: '5%', bottom: 0, width: '20%', height: '20%', maxWidth: '40px', maxHeight: '40px', minWidth: '15px', minHeight: '15px', color: this.state.autorotate ? 'green' : 'black'}} />
226                                <MdYoutubeSearchedFor onClick={() => this.switchRezoom()} style={{zIndex: 2, position: 'absolute', right: '5%', bottom: 0, width: '20%', height: '20%', maxWidth: '40px', maxHeight: '40px', minWidth: '15px', minHeight: '15px', color: this.state.rezoom ? 'green' : 'black' }} />
227                            </div>
228                        </td>
229                    </tr> 
230                </tbody>
231            </table>
232        );
233    }
234}
235
236export default GenoViewer;
Note: See TracBrowser for help on using the repository browser.