Skip to content

Commit

Permalink
Challenges | Step 1 (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
LiamGallagher737 authored Jul 17, 2024
1 parent c96bf7a commit 500101b
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
import { formatCode } from "$lib/format";
import { editorCode } from "$lib/components/editor";
import { toast } from "svelte-sonner";
import { settings } from "./Settings.svelte";
import { goto } from "$app/navigation";
import { DEFAULT_VERSION, type Version } from "$lib/versions";
import { DEFAULT_CHANNEL, type Channel } from "$lib/channels";
export let version: Version = DEFAULT_VERSION;
export let channel: Channel = DEFAULT_CHANNEL;
async function copyCodeToClipboard() {
await navigator.clipboard.writeText($editorCode);
Expand All @@ -21,8 +25,8 @@
method: "POST",
body: JSON.stringify({
code: $editorCode,
version: $settings.version,
channel: $settings.channel,
version,
channel,
}),
});
if (!result.ok) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,25 @@
import Image from "lucide-svelte/icons/images";
import Package from "lucide-svelte/icons/package";
import { writable } from "svelte/store";
export let tabs: Tab[];
</script>

<div class="flex flex-col">
<Button variant="ghost" class="h-12 rounded-b-none" on:click={() => selectedTab.set("editor")}>
<Code />
</Button>

<Separator />

<Button variant="ghost" class="h-12 rounded-none" on:click={() => selectedTab.set("assets")}>
<Image />
</Button>

<Separator />

<Button variant="ghost" class="h-12 rounded-none" on:click={() => selectedTab.set("crates")}>
<Package />
</Button>

<Separator />
{#each tabs as tab, n}
<Button
variant="ghost"
class={`h-12 ${n === 0 ? "rounded-b-none" : "rounded-none"}`}
on:click={() => selectedTab.set(tab)}
>
{#if tab === "editor"}
<Code />
{:else if tab === "assets"}
<Image />
{:else if tab === "crates"}
<Package />
{/if}
</Button>
<Separator />
{/each}
</div>
124 changes: 124 additions & 0 deletions www/src/routes/challenges/[slug]/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<script lang="ts">
import Editor from "$lib/components/Editor.svelte";
import Sidebar, { selectedTab } from "$lib/components/Sidebar.svelte";
import Console from "$lib/components/Console.svelte";
import { Button } from "$lib/components/ui/button";
import { Card } from "$lib/components/ui/card";
import * as Resizable from "$lib/components/ui/resizable";
import { play as load } from "$lib/play";
import { toast } from "svelte-sonner";
import { consoleItems } from "$lib/components/console";
import { editorCode } from "$lib/components/editor";
import { onMount, tick } from "svelte";
import { DEFAULT_VERSION } from "$lib/versions";
import { DEFAULT_CHANNEL } from "$lib/channels";
import Actions from "$lib/components/Actions.svelte";
const gameCanvasParentId = "game-container";
let gameCanvasParent: HTMLDivElement;
let processingRequest = false;
let editor: Editor;
onMount(() => {
selectedTab.subscribe(async (newValue) => {
if (newValue !== "editor") return;
await tick();
editor.layout();
});
});
let gameCanvas: HTMLCanvasElement | null = null;
let wasm: any | null = null;
async function play() {
if (wasm) wasm.__exit();
if (gameCanvas) gameCanvas.remove();
consoleItems.set([]);
processingRequest = true;
const promise: Promise<void> = new Promise(async (resolve, reject) => {
let result = await load({
code: $editorCode,
version: DEFAULT_VERSION,
channel: DEFAULT_CHANNEL,
parentId: gameCanvasParentId,
});
processingRequest = false;
if (result.kind === "Failed") {
if (result.stderr) consoleItems.set([{ kind: "Stdout", text: result.stderr }]);
reject(result.message);
} else {
if (result.kind === "Success") gameCanvas = result.gameCanvas;
wasm = result.wasm;
consoleItems.set([{ kind: "Stdout", text: result.stderr }]);
resolve();
}
});
toast.promise(promise, {
loading: "Loading...",
success: "Built successfully",
error: (err) => {
return err as string;
},
});
}
function resizeGameCanvas() {
if (!gameCanvas) return;
gameCanvas.style.width = `${gameCanvasParent.clientWidth}px`;
gameCanvas.style.height = `${gameCanvasParent.clientWidth * (9 / 16)}px`;
}
</script>

<svelte:head>
<title>Bevy Playground</title>
<meta name="description" content="Experiment with Bevy apps in your browser" />
</svelte:head>

<div class="h-screen p-4">
<Resizable.PaneGroup direction="horizontal">
<Resizable.Pane
defaultSize={70}
minSize={40}
onResize={() => {
if ($selectedTab === "editor") editor.layout();
resizeGameCanvas();
}}
class="flex flex-col gap-4"
>
<Card class="flex flex-row justify-between p-4">
<Button
class="font-semibold transition"
bind:disabled={processingRequest}
on:click={play}>Play</Button
>
<Actions />
</Card>
<div class="flex h-full w-full gap-4 overflow-hidden">
<Card class="h-full w-12">
<Sidebar tabs={["editor"]} />
</Card>
<!-- The 4rem in calc() comes from 3rem sidebar + 1rem gap,
flex-grow won't work because of the editor -->
<Card class="h-full w-[calc(100%-4rem)] p-4">
{#if $selectedTab === "editor"}
<Editor bind:this={editor} />
{/if}
</Card>
</div>
</Resizable.Pane>
<Resizable.Handle withHandle class="mx-4" />
<Resizable.Pane defaultSize={30} minSize={20} class="flex flex-col gap-4">
<Card class="aspect-video">
<div
bind:this={gameCanvasParent}
id={gameCanvasParentId}
class="h-full w-full"
></div>
</Card>
<Card class="flex-grow overflow-auto p-4 font-mono text-sm">
<Console />
</Card>
</Resizable.Pane>
</Resizable.PaneGroup>
</div>
8 changes: 4 additions & 4 deletions www/src/routes/playground/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import AssetExplorer from "./AssetExplorer.svelte";
import CrateList from "./CrateList.svelte";
import Editor from "$lib/components/Editor.svelte";
import Actions from "./Actions.svelte";
import Sidebar, { selectedTab } from "./Sidebar.svelte";
import Actions from "$lib/components/Actions.svelte";
import Sidebar, { selectedTab } from "$lib/components/Sidebar.svelte";
import Settings, { settings } from "./Settings.svelte";
import Examples from "./Examples.svelte";
import Console from "$lib/components/Console.svelte";
Expand Down Expand Up @@ -105,13 +105,13 @@
>
<div class="flex flex-row gap-4">
<Examples />
<Actions />
<Actions version={$settings.version} channel={$settings.channel} />
<Settings />
</div>
</Card>
<div class="flex h-full w-full gap-4 overflow-hidden">
<Card class="h-full w-12">
<Sidebar />
<Sidebar tabs={["editor", "assets", "crates"]} />
</Card>
<!-- The 4rem in calc() comes from 3rem sidebar + 1rem gap,
flex-grow won't work because of the editor -->
Expand Down

0 comments on commit 500101b

Please sign in to comment.