import "./hotbar-menu.scss"; import "./hotbar.commands"; import React, { HTMLAttributes, ReactNode, useState } from "react"; import { observer } from "mobx-react"; import { HotbarIcon } from "./hotbar-icon"; import { cssNames, IClassName } from "../../utils"; import { catalogEntityRegistry } from "../../api/catalog-entity-registry"; import { defaultHotbarCells, HotbarItem, HotbarStore } from "../../../common/hotbar-store"; import { CatalogEntity, catalogEntityRunContext } from "../../api/catalog-entity"; import { DragDropContext, Draggable, Droppable, DropResult } from "react-beautiful-dnd"; import { HotbarSelector } from "./hotbar-selector"; interface Props { className?: IClassName; } @observer export class HotbarMenu extends React.Component { get hotbar() { return HotbarStore.getInstance().getActive(); } isActive(item: CatalogEntity) { return catalogEntityRegistry.activeEntity?.metadata?.uid == item.getId(); } getEntity(item: HotbarItem) { const hotbar = HotbarStore.getInstance().getActive(); if (!hotbar) { return null; } return item ? catalogEntityRegistry.items.find((entity) => entity.metadata.uid === item.entity.uid) : null; } onDragEnd(result: DropResult) { const { source, destination } = result; if (!destination) { // Dropped outside of the list return; } const from = parseInt(source.droppableId); const to = parseInt(destination.droppableId); HotbarStore.getInstance().restackItems(from, to); } getMoveAwayDirection(entityId: string, cellIndex: number) { const draggableItemIndex = this.hotbar.items.findIndex(item => item?.entity.uid == entityId); return draggableItemIndex > cellIndex ? "animateDown" : "animateUp"; } renderGrid() { return this.hotbar.items.map((item, index) => { const entity = this.getEntity(item); const isActive = !entity ? false : this.isActive(entity); return ( {(provided, snapshot) => ( {entity && ( {(provided, snapshot) => { const style = { zIndex: defaultHotbarCells - index, position: "absolute", ...provided.draggableProps.style, } as React.CSSProperties; return (
entity.onRun(catalogEntityRunContext)} className={cssNames({ isDragging: snapshot.isDragging })} />
); }}
)} {provided.placeholder}
)}
); }); } render() { const { className } = this.props; const hotbarStore = HotbarStore.getInstance(); const hotbar = hotbarStore.getActive(); return (
{this.renderGrid()}
); } } interface HotbarCellProps extends HTMLAttributes { children?: ReactNode; index: number; innerRef?: React.LegacyRef; } function HotbarCell({ innerRef, children, className, ...rest }: HotbarCellProps) { const [animating, setAnimating] = useState(false); const onAnimationEnd = () => { setAnimating(false); }; const onClick = () => { if (className.includes("isDraggingOver")) { return; } setAnimating(true); }; return (
{children}
); }