File size: 4,826 Bytes
1e92f2d |
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 |
import React from 'react';
import Immutable from 'immutable';
import propTypes from 'prop-types';
import style from './index.less';
import Button from './button';
import store from '../../store';
import todo from '../../control/todo';
import { i18n, lan } from '../../unit/const';
export default class Keyboard extends React.Component {
componentDidMount() {
const touchEventCatch = {}; // 对于手机操作, 触发了touchstart, 将作出记录, 不再触发后面的mouse事件
// 在鼠标触发mousedown时, 移除元素时可以不触发mouseup, 这里做一个兼容, 以mouseout模拟mouseup
const mouseDownEventCatch = {};
document.addEventListener('touchstart', (e) => {
if (e.preventDefault) {
e.preventDefault();
}
}, true);
// 解决issue: https://github.com/chvin/react-tetris/issues/24
document.addEventListener('touchend', (e) => {
if (e.preventDefault) {
e.preventDefault();
}
}, true);
// 阻止双指放大
document.addEventListener('gesturestart', (e) => {
if (e.preventDefault) {
event.preventDefault();
}
});
document.addEventListener('mousedown', (e) => {
if (e.preventDefault) {
e.preventDefault();
}
}, true);
Object.keys(todo).forEach((key) => {
this[`dom_${key}`].dom.addEventListener('mousedown', () => {
if (touchEventCatch[key] === true) {
return;
}
todo[key].down(store);
mouseDownEventCatch[key] = true;
}, true);
this[`dom_${key}`].dom.addEventListener('mouseup', () => {
if (touchEventCatch[key] === true) {
touchEventCatch[key] = false;
return;
}
todo[key].up(store);
mouseDownEventCatch[key] = false;
}, true);
this[`dom_${key}`].dom.addEventListener('mouseout', () => {
if (mouseDownEventCatch[key] === true) {
todo[key].up(store);
}
}, true);
this[`dom_${key}`].dom.addEventListener('touchstart', () => {
touchEventCatch[key] = true;
todo[key].down(store);
}, true);
this[`dom_${key}`].dom.addEventListener('touchend', () => {
todo[key].up(store);
}, true);
});
}
shouldComponentUpdate({ keyboard, filling }) {
return !Immutable.is(keyboard, this.props.keyboard) || filling !== this.props.filling;
}
render() {
const keyboard = this.props.keyboard;
return (
<div
className={style.keyboard}
style={{
marginTop: 20 + this.props.filling,
}}
>
<Button
color="blue"
size="s1"
top={0}
left={374}
label={i18n.rotation[lan]}
arrow="translate(0, 63px)"
position
active={keyboard.get('rotate')}
ref={(c) => { this.dom_rotate = c; }}
/>
<Button
color="blue"
size="s1"
top={180}
left={374}
label={i18n.down[lan]}
arrow="translate(0,-71px) rotate(180deg)"
active={keyboard.get('down')}
ref={(c) => { this.dom_down = c; }}
/>
<Button
color="blue"
size="s1"
top={90}
left={284}
label={i18n.left[lan]}
arrow="translate(60px, -12px) rotate(270deg)"
active={keyboard.get('left')}
ref={(c) => { this.dom_left = c; }}
/>
<Button
color="blue"
size="s1"
top={90}
left={464}
label={i18n.right[lan]}
arrow="translate(-60px, -12px) rotate(90deg)"
active={keyboard.get('right')}
ref={(c) => { this.dom_right = c; }}
/>
<Button
color="blue"
size="s0"
top={100}
left={52}
label={`${i18n.drop[lan]} (SPACE)`}
active={keyboard.get('drop')}
ref={(c) => { this.dom_space = c; }}
/>
<Button
color="red"
size="s2"
top={0}
left={196}
label={`${i18n.reset[lan]}(R)`}
active={keyboard.get('reset')}
ref={(c) => { this.dom_r = c; }}
/>
<Button
color="green"
size="s2"
top={0}
left={106}
label={`${i18n.sound[lan]}(S)`}
active={keyboard.get('music')}
ref={(c) => { this.dom_s = c; }}
/>
<Button
color="green"
size="s2"
top={0}
left={16}
label={`${i18n.pause[lan]}(P)`}
active={keyboard.get('pause')}
ref={(c) => { this.dom_p = c; }}
/>
</div>
);
}
}
Keyboard.propTypes = {
filling: propTypes.number.isRequired,
keyboard: propTypes.object.isRequired,
};
|