Kurt/couple tweaks #249

Merged
Irev-Dev merged 3 commits from kurt/couple-tweaks into main 2021-03-13 23:52:55 +01:00
6 changed files with 71 additions and 24 deletions

View File

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

View File

@@ -8,13 +8,24 @@ const IdeEditor = () => {
function handleCodeChange(value, _event) {
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 (
<Editor
defaultValue={state.code}
defaultLanguage="javascript"
onChange={handleCodeChange}
/>
<div className="h-full" onKeyDown={handleSaveHotkey}>
<Editor
defaultValue={state.code}
// 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 (
<IdeContext.Provider value={{ state, dispatch }}>
<div className="p-8 border-2">
<div>hi I'm the toolbar</div>
<nav className="flex">
<button
onClick={() => setIdeType('openCascade')}

View File

@@ -15,6 +15,7 @@ function Controls({ onCameraChange, onDragStart }) {
camera.position.x = 200
camera.position.y = 140
camera.position.z = 20
camera.far = 10000
camera.fov = 22.5 // matches default openscad fov
// Order matters with Euler rotations
@@ -105,6 +106,15 @@ function Box(props) {
</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
const IdeViewer = () => {
const { state, dispatch } = useContext(IdeContext)
@@ -120,14 +130,22 @@ const IdeViewer = () => {
setIsDragging(false)
}, [state.objectData])
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 (
<div className="p-8 border-2 m-2">
<div className="relative" style={{ height: '500px', width: '500px' }}>
{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
className="relative"
style={{
height: '500px',
width: '500px',
backgroundColor: openSCADDeepOceanThemeBackground,
}}
>
{image && (
<div
className={`absolute inset-0 transition-opacity duration-500 ${
@@ -137,6 +155,11 @@ const IdeViewer = () => {
<img src={image} className="" />
</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
className={`opacity-0 absolute inset-0 transition-opacity duration-500 ${
isDragging ? 'opacity-100' : 'hover:opacity-50'
@@ -158,9 +181,10 @@ const IdeViewer = () => {
/>
<ambientLight />
<pointLight position={[15, 5, 10]} />
<Box position={[0, 49.5, 0]} size={[1, 100, 1]} color="cyan" />
<Box position={[0, 0, -50.5]} size={[1, 1, 100]} color="orange" />
<Box position={[50.5, 0, 0]} size={[100, 1, 1]} color="hotpink" />
<Sphere position={[0, 0, 0]} color={pink400} />
<Box position={[0, 50, 0]} size={[1, 100, 1]} color={indigo900} />
<Box position={[0, 0, -50]} size={[1, 1, 100]} color={indigo300} />
<Box position={[50, 0, 0]} size={[100, 1, 1]} color={pink400} />
</Canvas>
</div>
</div>

View File

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

View File

@@ -1,17 +1,26 @@
import { useReducer } from 'react'
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 = () => {
const initialState = {
ideType: 'openScad',
consoleMessages: [{ type: 'message', message: 'Initialising OpenSCAD' }],
code: `difference(){
union(){
cube(60);
sphere(25);
}
translate([30,30,30])cylinder(r=25,h=100);
}`,
code: donutInitCode,
objectData: {
type: 'stl',
data: 'some binary',