import * as React from "react"; import { Play, Bug, StepForward, RotateCcw, Square, ChevronUp, ChevronDown, LayoutGrid } from "lucide-react"; import { CodeBlockContainer, INIT_CODEBLOCKS, CodeBlock, INSERT_CODEBLOCK, REMOVE_CODEBLOCK, SET_DROPZONE_Z_INDEX } from "../store/codeBlocks/types"; import { LevelInitializer, IPlayerData } from "../game/levels"; import { initCodeBlocks, insertCodeBlock, removeCodeBlock } from "../store/codeBlocks/actions"; import { AppState } from "../store"; import game from "../game/game" import { connect } from "react-redux"; import { Link } from "react-router-dom"; import { START_EXECUTING, STEP_EXECUTION, TOGGLE_AUTOSTEP, TOGGLE_SPEED } from "../store/executionState/types"; const CodeBlockComponent = ({ type, blockIdx, remove, container, setDropZoneZIdx }) => { return (
e.preventDefault()} onDragStart={(e) => { // e.preventDefault() for (let e of document.getElementsByClassName("opdropzone")) { (e as HTMLElement).style.background = "" } // console.log("BOOM START DRAGGOING"); (e.target as HTMLElement).style.zIndex = "3" setDropZoneZIdx(2) dragged = type }} onDragEnd={(e) => { e.preventDefault() for (let e of document.getElementsByClassName("opdropzone")) { (e as HTMLElement).style.background = "" } (e.target as HTMLElement).style.zIndex = "" setDropZoneZIdx(-1) remove(container, blockIdx) // console.log("LE END OF DRAG") }} >
) } // show how many operations are possible const OpIndicators = ({ n }) => { let ops = [] for (let i = 0; i < n; i++) { ops.push(
) } return (
{ops}
) } // dropzones which determine how to insert const OpDropZones = ({ n, cidx, insert, style }) => { let dropZones = [] for (let i = 0; i < n; i++) { dropZones.push(
{ e.preventDefault(); (e.target as HTMLElement).style.background = "purple" }} onDragLeave={(e) => { e.preventDefault(); (e.target as HTMLElement).style.background = "" }} onDragOver={(e) => { e.preventDefault() }} onDrop={(e) => { e.preventDefault() let dragOver = ((e.target as HTMLElement).className); // console.log(dragOver) // console.log("DRAGGED", dragged) insert({ name: dragged }, cidx, i, true) }}>
{ (e.target as HTMLElement).style.background = "purple" }} onDragOver={e => { e.preventDefault() }} onDragLeave={e => { (e.target as HTMLElement).style.background = "" }} onDrop={e => { (e.target as HTMLElement).style.background = "" insert({ name: dragged }, cidx, i, false) }}>
) } return (
{dropZones}
) } const FnContainers = ({ code, dropZoneZIdx, setDropZoneZIdx, remove, insert }) => { return (
{ code.map((cv, idx) => { return (

{cv.name + "(){"}

{cv.blocks.map((b, i) => { return () })}

{"}"}

) }) }
) } var dragged: string; const OpsPalette = ({ ops, setDropZoneZIdx }) => { let allowedOps = ops.map((e, i) => { return ( undefined} setDropZoneZIdx={setDropZoneZIdx} />) }) return (
{allowedOps}
) } const RuntimeControls = ({ start, fast, step, running, level, code, autostep, toggleAutoStep, toggleSpeed, finished }) => { let DebugButton = () => (
{ start(level, code, level.playerPos) }}> debug
) let StepButton = () => (
step
) let StartButton = () => (
{ start(level, code, level.playerPos) toggleAutoStep() step() }}> run
) let LevelsButton = () => ( levels ) let ResetButton = () => (
game.reset()}> reset
) let ToggleSpeedButton = () => (
{fast ? : } {fast ? "slower" : "faster"}
) let AbortButton = () => (
console.log("TODO")}> abort
) if (running) { if (autostep) { return (
) } return (
) } if (finished) { return (
) } return (
) } export interface EditorProps { level: LevelInitializer, code?: Array, init: typeof initCodeBlocks, insert: typeof insertCodeBlock, remove: typeof removeCodeBlock, setDropZoneZIdx: (n: number) => void dropZoneZIdx: number, start: (level: LevelInitializer, code: Array>, playerPos: IPlayerData) => void, running: boolean, step: () => void, toggleSpeed: () => void, autostep: boolean, fast: boolean, toggleAutoStep: () => void, finished: boolean } class Editor extends React.Component { constructor(props: EditorProps) { super(props) let { init, level } = props // console.log(props) init(level) // initCodeBlocks(props.level) } componentDidMount() { // console.log("YES IT MOUNTS") } componentWillUnmount() { // console.log("ME NOW UNMOUNT!") } render() { return (

{this.props.level.name}

); } } const mapStateToProps = (state: AppState, ownProps?: { level: LevelInitializer }) => { // console.log(ownProps) return { ...ownProps, code: state.codeBlocks, dropZoneZIdx: state.opDropZoneZIdx, running: state.executionState.running, autostep: state.executionState.autostep, finished: state.executionState.finished, fast: state.executionState.fast } } const mapDispatchToProps = (dispatch) => { return { init: (level: LevelInitializer) => dispatch({ type: INIT_CODEBLOCKS, level: level }), insert: (cb: CodeBlock, cidx: number, bidx: number, ov: boolean) => dispatch({ type: INSERT_CODEBLOCK, block: cb, containerIdx: cidx, blocksIdx: bidx, overwrite: ov }), remove: (cidx: number, bidx: number) => { dispatch({ type: REMOVE_CODEBLOCK, containerIdx: cidx, blocksIdx: bidx }) }, setDropZoneZIdx: (idx: number) => { dispatch({ type: SET_DROPZONE_Z_INDEX, zIdx: idx }) }, start: (level: LevelInitializer, code: Array>, playerPos: IPlayerData) => { dispatch({ type: START_EXECUTING, level: level, code: code, playerPos: playerPos }) }, step: () => dispatch({ type: STEP_EXECUTION }), toggleAutoStep: () => dispatch({ type: TOGGLE_AUTOSTEP }), toggleSpeed: () => dispatch({ type: TOGGLE_SPEED }) } } export default connect(mapStateToProps, mapDispatchToProps)(Editor);