humus-old/client/ui/debug/index.js

72 lines
2.0 KiB
JavaScript
Raw Normal View History

2019-04-14 00:24:21 -05:00
// 3rd party.
import React, {useEffect, useState} from 'react';
import {hot} from 'react-hot-loader/root';
// 2nd party.
import {compose} from '@avocado/core';
2019-04-14 01:03:09 -05:00
import {Vector} from '@avocado/math';
2019-04-14 00:24:21 -05:00
import contempo from 'contempo';
// 1st party.
import {usePropertyChange} from '../hooks/use-property-change';
2019-04-28 22:35:20 -05:00
import Connection from './connection';
2019-04-14 01:03:09 -05:00
import SelfEntity from './self-entity';
2019-04-23 15:27:55 -05:00
import Timers from './timers';
2019-04-14 00:24:21 -05:00
const decorate = compose(
contempo(``),
hot,
);
2019-04-14 01:03:09 -05:00
const DebugUi = ({
2019-04-14 16:33:26 -05:00
app,
2019-04-14 01:03:09 -05:00
}) => {
2019-04-14 00:24:21 -05:00
const [size, setSize] = useState([0, 0]);
const [position, setPosition] = useState([0, 0]);
const [transformRatio, setTransformRatio] = useState(1);
const isDebugging = usePropertyChange(app, 'isDebugging', false);
2019-04-14 00:24:21 -05:00
// Sync debug UI size with stage size.
useEffect(() => {
const onStageResized = (size) => {
2019-04-25 00:09:28 -05:00
const rect = app.stage.element.getBoundingClientRect();
2019-04-14 00:24:21 -05:00
setPosition([rect.left, rect.top]);
setSize(size);
2019-04-25 00:09:28 -05:00
setTransformRatio(app.stage.transformRatio);
2019-04-14 00:24:21 -05:00
};
2019-04-25 00:09:28 -05:00
app.stage.on('displaySizeChanged', onStageResized);
onStageResized(app.stage.displaySize);
2019-04-14 00:24:21 -05:00
return () => {
2019-04-25 00:09:28 -05:00
app.stage.off('displaySizeChanged', onStageResized);
2019-04-14 00:24:21 -05:00
};
}, []);
// Scale stage when debugging.
useEffect(() => {
2019-04-25 00:09:28 -05:00
app.stage.element.style.transform = `scale(${isDebugging ? '0.5' : '1'})`;
2019-04-14 00:24:21 -05:00
}, [isDebugging]);
return isDebugging && <div
2019-04-14 00:24:21 -05:00
className="debug"
style={{
2019-04-14 01:03:09 -05:00
display: Vector.isNull(size) ? 'none': 'block',
2019-04-14 00:24:21 -05:00
left: position[0],
top: position[1],
width: size[0],
height: size[1],
position: 'absolute',
}}
>
<div
className="ui"
style={{
transform: `scale(${1 / transformRatio})`,
transformOrigin: '0 0',
2019-04-25 00:09:28 -05:00
width: app.stage.size[0],
height: app.stage.size[1],
2019-04-14 00:24:21 -05:00
}}
>
2019-04-23 15:27:55 -05:00
<Timers app={app} />
2019-04-28 22:35:20 -05:00
<Connection Parser={app.AugmentedParser} socket={app.socket} />
2019-04-24 17:01:11 -05:00
<SelfEntity app={app} />
2019-04-14 00:24:21 -05:00
</div>
</div>;
};
export default decorate(DebugUi);