Merge pull request #249 from Irev-Dev/kurt/couple-tweaks

Kurt/couple tweaks
This commit was merged in pull request #249.
This commit is contained in:
Kurt Hutten
2021-03-14 09:52:55 +11:00
committed by GitHub
6 changed files with 71 additions and 24 deletions

View File

@@ -5,7 +5,6 @@ const IdeConsole = () => {
const { state } = useContext(IdeContext) const { state } = useContext(IdeContext)
return ( return (
<div className="p-8 border-2 m-2 overflow-y-auto"> <div className="p-8 border-2 m-2 overflow-y-auto">
<div className="pb-4">hi I'm console</div>
<div> <div>
{state.consoleMessages?.map(({ type, message }, index) => ( {state.consoleMessages?.map(({ type, message }, index) => (
<div <div

View File

@@ -8,13 +8,24 @@ const IdeEditor = () => {
function handleCodeChange(value, _event) { function handleCodeChange(value, _event) {
dispatch({ type: 'updateCode', payload: value }) dispatch({ type: 'updateCode', payload: value })
} }
function handleSaveHotkey(event) {
//ctrl|meta + s is very intuitive for most devs
const { key, ctrlKey, metaKey } = event
if (key === 's' && (ctrlKey || metaKey)) {
event.preventDefault()
dispatch({ type: 'render', payload: { code: state.code } })
}
}
return ( return (
<Editor <div className="h-full" onKeyDown={handleSaveHotkey}>
defaultValue={state.code} <Editor
defaultLanguage="javascript" defaultValue={state.code}
onChange={handleCodeChange} // TODO #247 cpp seems better than js for the time being
/> defaultLanguage="cpp"
onChange={handleCodeChange}
/>
</div>
) )
} }

View File

@@ -16,7 +16,6 @@ const IdeToolbarNew = () => {
return ( return (
<IdeContext.Provider value={{ state, dispatch }}> <IdeContext.Provider value={{ state, dispatch }}>
<div className="p-8 border-2"> <div className="p-8 border-2">
<div>hi I'm the toolbar</div>
<nav className="flex"> <nav className="flex">
<button <button
onClick={() => setIdeType('openCascade')} onClick={() => setIdeType('openCascade')}

View File

@@ -15,6 +15,7 @@ function Controls({ onCameraChange, onDragStart }) {
camera.position.x = 200 camera.position.x = 200
camera.position.y = 140 camera.position.y = 140
camera.position.z = 20 camera.position.z = 20
camera.far = 10000
camera.fov = 22.5 // matches default openscad fov camera.fov = 22.5 // matches default openscad fov
// Order matters with Euler rotations // Order matters with Euler rotations
@@ -105,6 +106,15 @@ function Box(props) {
</mesh> </mesh>
) )
} }
function Sphere(props) {
const mesh = useRef()
return (
<mesh {...props} ref={mesh} scale={[1, 1, 1]}>
<sphereBufferGeometry args={[2, 30, 30]} />
<meshStandardMaterial color={props.color} />
</mesh>
)
}
let currentCode // I have no idea why this works and using state.code is the dispatch doesn't but it was always stale let currentCode // I have no idea why this works and using state.code is the dispatch doesn't but it was always stale
const IdeViewer = () => { const IdeViewer = () => {
const { state, dispatch } = useContext(IdeContext) const { state, dispatch } = useContext(IdeContext)
@@ -120,14 +130,22 @@ const IdeViewer = () => {
setIsDragging(false) setIsDragging(false)
}, [state.objectData]) }, [state.objectData])
currentCode = state.code currentCode = state.code
const openSCADDeepOceanThemeBackground = '#323232'
// the following are tailwind colors in hex, can't use these classes to color three.js meshes.
const pink400 = '#F472B6'
const indigo300 = '#A5B4FC'
const indigo900 = '#312E81'
return ( return (
<div className="p-8 border-2 m-2"> <div className="p-8 border-2 m-2">
<div className="relative" style={{ height: '500px', width: '500px' }}> <div
{state.isLoading && ( className="relative"
<div className="inset-0 absolute flex items-center justify-center"> style={{
<div className="h-16 w-16 bg-pink-600 rounded-full animate-ping"></div> height: '500px',
</div> width: '500px',
)} backgroundColor: openSCADDeepOceanThemeBackground,
}}
>
{image && ( {image && (
<div <div
className={`absolute inset-0 transition-opacity duration-500 ${ className={`absolute inset-0 transition-opacity duration-500 ${
@@ -137,6 +155,11 @@ const IdeViewer = () => {
<img src={image} className="" /> <img src={image} className="" />
</div> </div>
)} )}
{state.isLoading && (
<div className="inset-0 absolute flex items-center justify-center">
<div className="h-16 w-16 bg-pink-600 rounded-full animate-ping"></div>
</div>
)}
<div <div
className={`opacity-0 absolute inset-0 transition-opacity duration-500 ${ className={`opacity-0 absolute inset-0 transition-opacity duration-500 ${
isDragging ? 'opacity-100' : 'hover:opacity-50' isDragging ? 'opacity-100' : 'hover:opacity-50'
@@ -158,9 +181,10 @@ const IdeViewer = () => {
/> />
<ambientLight /> <ambientLight />
<pointLight position={[15, 5, 10]} /> <pointLight position={[15, 5, 10]} />
<Box position={[0, 49.5, 0]} size={[1, 100, 1]} color="cyan" /> <Sphere position={[0, 0, 0]} color={pink400} />
<Box position={[0, 0, -50.5]} size={[1, 1, 100]} color="orange" /> <Box position={[0, 50, 0]} size={[1, 100, 1]} color={indigo900} />
<Box position={[50.5, 0, 0]} size={[100, 1, 1]} color="hotpink" /> <Box position={[0, 0, -50]} size={[1, 1, 100]} color={indigo300} />
<Box position={[50, 0, 0]} size={[100, 1, 1]} color={pink400} />
</Canvas> </Canvas>
</div> </div>
</div> </div>

View File

@@ -2,6 +2,8 @@ let openScadBaseURL =
process.env.OPENSCAD_BASE_URL || process.env.OPENSCAD_BASE_URL ||
'https://x2wvhihk56.execute-api.us-east-1.amazonaws.com/dev' 'https://x2wvhihk56.execute-api.us-east-1.amazonaws.com/dev'
let lastCameraSettings
export const render = async ({ code, settings }) => { export const render = async ({ code, settings }) => {
const body = JSON.stringify({ const body = JSON.stringify({
settings: { settings: {
@@ -9,10 +11,13 @@ export const render = async ({ code, settings }) => {
x: 500, x: 500,
y: 500, y: 500,
}, },
camera: settings.camera, camera: settings.camera || lastCameraSettings,
}, },
file: code, file: code,
}) })
if (settings.camera) {
lastCameraSettings = settings.camera
}
try { try {
const response = await fetch(openScadBaseURL + '/render', { const response = await fetch(openScadBaseURL + '/render', {
method: 'POST', method: 'POST',

View File

@@ -1,17 +1,26 @@
import { useReducer } from 'react' import { useReducer } from 'react'
import { cadPackages } from 'src/helpers/cadPackages' import { cadPackages } from 'src/helpers/cadPackages'
const donutInitCode = `
color(c="DarkGoldenrod")rotate_extrude()translate([20,0])circle(d=30);
donut();
module donut() {
for(i=[1:360]){
rotate(i*13.751)stick(20,i*1.351);
}
}
module stick(basewid, angl){
translate([basewid,0,0])rotate([angl,angl,angl*2])color(c="hotpink")hull(){
sphere(7);
translate([0,0,10])sphere(9);
}
}`
export const useIdeState = () => { export const useIdeState = () => {
const initialState = { const initialState = {
ideType: 'openScad', ideType: 'openScad',
consoleMessages: [{ type: 'message', message: 'Initialising OpenSCAD' }], consoleMessages: [{ type: 'message', message: 'Initialising OpenSCAD' }],
code: `difference(){ code: donutInitCode,
union(){
cube(60);
sphere(25);
}
translate([30,30,30])cylinder(r=25,h=100);
}`,
objectData: { objectData: {
type: 'stl', type: 'stl',
data: 'some binary', data: 'some binary',