humus-old/client/ui/debug/self-entity.js

77 lines
1.7 KiB
JavaScript
Raw Normal View History

2019-04-14 01:03:09 -05:00
// 3rd party.
import React, {useEffect, useState} from 'react';
// 2nd party.
import {compose} from '@avocado/core';
import {Vector} from '@avocado/math';
import contempo from 'contempo';
const decorate = compose(
contempo(`
.self-entity {
background-color: rgba(0, 0, 0, .3);
border: 1px solid white;
color: white;
font-size: 0.8em;
position: absolute;
top: calc(180px + 0.5em);
left: 0.5em;
line-height: 1em;
2019-04-14 13:28:50 -05:00
width: 10em;
2019-04-14 01:03:09 -05:00
padding: 0.125em;
font-family: monospace;
}
.location {
}
.x {
display: inline-block;
2019-04-14 13:28:50 -05:00
width: 5em;
2019-04-14 01:03:09 -05:00
}
.y {
display: inline-block;
2019-04-14 13:28:50 -05:00
width: 4.125em;
2019-04-14 01:03:09 -05:00
}
.x:before {
content: 'x: ';
}
.y:before {
content: 'y: ';
}
`),
);
2019-04-14 16:33:26 -05:00
const SelfEntityComponent = ({app}) => {
2019-04-14 01:03:09 -05:00
const [selfEntity, setSelfEntity] = useState();
const [position, setPosition] = useState([0, 0]);
useEffect(() => {
2019-04-14 16:33:26 -05:00
const onSelfEntityChanged = () => {
setSelfEntity(app.selfEntity);
setPosition(app.selfEntity.position);
};
app.on('selfEntityChanged', onSelfEntityChanged);
return () => {
app.off('selfEntityChanged', onSelfEntityChanged);
}
2019-04-14 01:03:09 -05:00
}, [selfEntity]);
useEffect(() => {
if (!selfEntity) {
return;
}
const onPositionChanged = () => {
setPosition(selfEntity.position);
};
selfEntity.on('positionChanged', onPositionChanged);
return () => {
selfEntity.off('positionChanged', onPositionChanged);
};
}, [position]);
2019-04-14 13:28:50 -05:00
const roundedPosition = Vector.round(position);
2019-04-14 01:03:09 -05:00
return <div className="self-entity unselectable">
<div className="location">
<div className="x">{roundedPosition[0]}</div>
<div className="y">{roundedPosition[1]}</div>
</div>
</div>;
}
export default decorate(SelfEntityComponent);