From 83bbcd850e228e39e8ce795c5fd607753ceb715f Mon Sep 17 00:00:00 2001 From: Alexander Bentkamp Date: Wed, 14 Dec 2022 12:22:46 +0100 Subject: [PATCH] use svg for overview --- client/src/components/Welcome.tsx | 105 +++++++++++++++--------------- client/src/game/api.ts | 2 +- 2 files changed, 52 insertions(+), 55 deletions(-) diff --git a/client/src/components/Welcome.tsx b/client/src/components/Welcome.tsx index 7966c56..33a5bad 100644 --- a/client/src/components/Welcome.tsx +++ b/client/src/components/Welcome.tsx @@ -6,7 +6,7 @@ import '@fontsource/roboto/300.css'; import '@fontsource/roboto/400.css'; import '@fontsource/roboto/500.css'; import '@fontsource/roboto/700.css'; -import cytoscape from 'cytoscape' +import cytoscape, { LayoutOptions } from 'cytoscape' import klay from 'cytoscape-klay'; import { Link as RouterLink, useNavigate } from 'react-router-dom'; @@ -15,70 +15,22 @@ cytoscape.use( klay ); import { Box, Typography, Button, CircularProgress, Grid } from '@mui/material'; import { useGetGameInfoQuery } from '../game/api'; +import { Link } from 'react-router-dom'; function Welcome() { const navigate = useNavigate(); - const worldsRef = useRef(null) - - const drawWorlds = (worlds) => { - let elements = [] - for (let node of worlds.nodes) { - elements.push({ data: { id: node } }) - } - for (let edge of worlds.edges) { - elements.push({ - data: { - id: edge[0] + " --edge-to--> " + edge[1], - source: edge[0], - target: edge[1] - } - }) - } - const layout : any = {name: "klay", klay: {direction: "DOWN"}} - const cy = cytoscape({ container: worldsRef.current!, elements, layout, - style: [ // the stylesheet for the graph - { - selector: 'node', - style: { - 'background-color': '#666', - 'label': 'data(id)' - } - }, - - { - selector: 'edge', - style: { - 'width': 3, - 'line-color': '#ccc', - 'target-arrow-color': '#ccc', - 'target-arrow-shape': 'triangle', - 'curve-style': 'bezier' - } - } - ], - userPanningEnabled: false, - userZoomingEnabled: false, - autoungrabify: true, - autounselectify: true, - }) - - cy.on('click', 'node', function(evt){ - navigate(`/world/${this.id()}/level/1`); - }); - } - const gameInfo = useGetGameInfoQuery() - useEffect(() => { - if (gameInfo.data?.worlds) { drawWorlds(gameInfo.data.worlds); } - }, [gameInfo.data?.worlds]) + const { nodes, bounds }: any = gameInfo.data ? computeWorldLayout(gameInfo.data?.worlds) : {nodes: []} useEffect(() => { if (gameInfo.data?.title) window.document.title = gameInfo.data.title }, [gameInfo.data?.title]) + const padding = 10 + return
{ gameInfo.isLoading? @@ -94,7 +46,18 @@ function Welcome() { -
+ + + {gameInfo.data ? gameInfo.data.worlds.edges.map((edge) => + ) : null} + {Object.entries(nodes).map(([id, position]) => + + + {id} + )} + +
} @@ -102,3 +65,37 @@ function Welcome() { } export default Welcome + +function computeWorldLayout(worlds) { + + let elements = [] + for (let node of worlds.nodes) { + elements.push({ data: { id: node } }) + } + for (let edge of worlds.edges) { + elements.push({ + data: { + id: edge[0] + " --edge-to--> " + edge[1], + source: edge[0], + target: edge[1] + } + }) + } + + const cy = cytoscape({ + container: null, + elements, + headless: true, + styleEnabled: false + }) + + const layout = cy.layout({name: "klay", klay: {direction: "DOWN"}} as LayoutOptions).run() + let nodes = {} + cy.nodes().forEach((node, id) => { + nodes[node.id()] = node.position() + console.log(node.position()) + }) + const bounds = cy.nodes().boundingBox() + console.log(bounds) + return { nodes, bounds } +} diff --git a/client/src/game/api.ts b/client/src/game/api.ts index 784cfd7..d4f1684 100644 --- a/client/src/game/api.ts +++ b/client/src/game/api.ts @@ -4,7 +4,7 @@ import { Connection } from '../connection' interface GameInfo { title: null|string, introduction: null|string, - worlds: null|{nodes: string[], edges: string[][2]}, + worlds: null|{nodes: string[], edges: string[][]}, authors: null|string[], conclusion: null|string, }