-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
web site: improvements to examples page
- Loading branch information
Showing
6 changed files
with
206 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import React, { useEffect, useRef, useState } from 'react' | ||
import CodeMirror from '@uiw/react-codemirror'; | ||
import { javascript } from '@codemirror/lang-javascript'; | ||
import { vscodeDark } from '@uiw/codemirror-theme-vscode'; | ||
|
||
const CurrentExample = ({ currentExample }) => { | ||
const iframeRef= useRef(); | ||
|
||
const [showCode, setShowCode] = useState(false); | ||
const [code, setCode] = useState(''); | ||
|
||
const iframeSrc = `https://wesunwin.github.io/three-game-engine/examples/${currentExample.name}.html`; | ||
|
||
const reloadIframe = () => { | ||
iframeRef.current.src = iframeRef.current.src + ''; | ||
}; | ||
|
||
useEffect(() => { | ||
if (currentExample?.code) { | ||
setCode('Loading...') | ||
window.fetch(currentExample.code) | ||
.then(response => response.text()) | ||
.then(code => setCode(code)) | ||
} | ||
}, [currentExample?.code]) | ||
|
||
return ( | ||
<div className="current-example"> | ||
<h4>{currentExample.label}</h4> | ||
|
||
<div className="current-example-content"> | ||
<div className="current-example-preview"> | ||
<p> | ||
{currentExample.description} | ||
</p> | ||
|
||
<iframe | ||
ref={iframeRef} | ||
className="current-example-iframe" | ||
src={iframeSrc} | ||
/> | ||
</div> | ||
|
||
{currentExample.footer} | ||
|
||
<button onClick={() => setShowCode(!showCode)}> | ||
{showCode ? 'Hide Code' : 'Show Code'} | ||
</button> | ||
| ||
<button onClick={reloadIframe}> | ||
Restart Demo | ||
</button> | ||
|
||
{showCode && | ||
<div className="current-example-code"> | ||
<CodeMirror | ||
theme={vscodeDark} | ||
value={code} | ||
extensions={[javascript({ jsx: true })]} | ||
onChange={() => {}} | ||
/> | ||
</div> | ||
} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default CurrentExample |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import ExamplesSidebar from './ExamplesSidebar.jsx'; | ||
import CurrentExample from './CurrentExample.jsx'; | ||
|
||
const Examples = () => { | ||
const [currentExample, setCurrentExample] = useState(null) | ||
|
||
const examples = [ | ||
{ | ||
thumbnail: 'https://raw.githubusercontent.com/WesUnwin/three-game-engine/main/docs/images/first_person_kinematic_character_controller_example_thumbnail.png', | ||
name: 'first_person_kinematic_character_controller_example', | ||
label: 'First-Person Kinematic Character Controller', | ||
description: 'Demonstrates using the KinematicCharacterController class to create a first-person controlled character game object.', | ||
code: 'https://raw.githubusercontent.com/WesUnwin/three-game-engine/main/examples/first_person_kinematic_character_controller/index.js', | ||
footer: ( | ||
<div className="current-example-footer"> | ||
<h6 style={{ textAlign: 'center' }}>Controls:</h6> | ||
<ul> | ||
<li>Click on the iframe to allow the game to capture your mouse (ESC to escape).</li> | ||
<li>Move your mouse to look around.</li> | ||
<li>Use WSAD (or arrow keys) to move relative to the direction your facing.</li> | ||
<li>Hold shift to run.</li> | ||
</ul> | ||
</div> | ||
) | ||
} | ||
]; | ||
|
||
useEffect(() => { | ||
if (!currentExample) { | ||
setCurrentExample(examples[0]) | ||
} | ||
}, [currentExample?.name]) | ||
|
||
return ( | ||
<div className="examples"> | ||
<ExamplesSidebar | ||
examples={examples} | ||
currentExample={currentExample} | ||
setCurrentExample={setCurrentExample} | ||
/> | ||
|
||
{currentExample && | ||
<CurrentExample currentExample={currentExample} /> | ||
} | ||
</div> | ||
); | ||
}; | ||
|
||
export default Examples; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import React from 'react' | ||
|
||
const ExamplesSidebar = ({ examples, currentExample, setCurrentExample }) => { | ||
return ( | ||
<div className="examples-sidebar"> | ||
{examples.map(example => ( | ||
<div | ||
key={example.name} | ||
className={`examples-sidebar__example ${currentExample?.name === example.name ? 'active' : ''}`} | ||
onClick={() => setCurrentExample(example)} | ||
> | ||
<img src={example.thumbnail} /> | ||
<p className='examples-sidebar__example-label'> | ||
{example.label} | ||
</p> | ||
</div> | ||
))} | ||
</div> | ||
) | ||
} | ||
|
||
export default ExamplesSidebar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters