Skip to content

Commit

Permalink
fix delete (#744)
Browse files Browse the repository at this point in the history
  • Loading branch information
ostatni5 authored Dec 7, 2022
1 parent 493b8f8 commit 1281cc4
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 164 deletions.
8 changes: 8 additions & 0 deletions src/ThreeEditor/ThreeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { EditorMenu } from './components/EditorMenu/EditorMenu';
import useDocumentTitle from '../util/useDocumentTitle';
import { useTheme } from '@mui/material/styles';
import { EditorSidebar } from './components/Sidebar/EditorSidebar';
import { useKeyboardEditorControls } from './util/hooks/useKeyboardEditorControls';
declare global {
interface Window {
editor: Editor;
Expand All @@ -23,11 +24,14 @@ interface ThreeEditorProps {
}

function ThreeEditor(props: ThreeEditorProps) {
const threeEditorRef = useRef<HTMLDivElement>(null);
const [editor, setEditor] = useState<Editor>();
const [title, setTitle] = useState<string>(editor?.config.getKey('project/title'));
const containerEl = useRef<HTMLDivElement>(null);
const theme = useTheme();

useKeyboardEditorControls(editor, threeEditorRef);

useEffect(() => {
editor?.signals.titleChanged.add(setTitle);
return () => {
Expand Down Expand Up @@ -66,6 +70,8 @@ function ThreeEditor(props: ThreeEditorProps) {

return (
<Box
ref={threeEditorRef}
tabIndex={-1}
sx={{
width: '100%',
height: '100vh',
Expand Down Expand Up @@ -100,6 +106,7 @@ function ThreeEditor(props: ThreeEditorProps) {
</Box>
{editor && (
<AppBar
className='ThreeEditorSidebar'
position='static'
color='secondary'
sx={{
Expand All @@ -119,3 +126,4 @@ function ThreeEditor(props: ThreeEditorProps) {
}

export default ThreeEditor;

159 changes: 0 additions & 159 deletions src/ThreeEditor/js/menubar/Menubar.Settings.Shortcuts.js

This file was deleted.

2 changes: 0 additions & 2 deletions src/ThreeEditor/js/menubar/Menubar.Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { UIButton, UIColor, UIPanel, UIRow, UISelect, UIText } from '../libs/ui.
import { UITexture } from '../libs/ui.three.js';
import { SidebarProjectRenderer } from '../sidebar/Sidebar.Project.Renderer.js';
import { MenubarSettingsHistory } from './Menubar.Settings.History.js';
import { MenubarSettingsShortcuts } from './Menubar.Settings.Shortcuts.js';
import { MenubarSettingsViewport } from './Menubar.Settings.Viewport.js';

const MenubarSettingsEvent = {
Expand Down Expand Up @@ -131,7 +130,6 @@ function MenubarSettings(editor) {
//

container.add(new MenubarSettingsViewport(editor));
container.add(new MenubarSettingsShortcuts(editor));
container.add(new MenubarSettingsHistory(editor));
container.add(new SidebarProjectRenderer(editor))

Expand Down
1 change: 0 additions & 1 deletion src/ThreeEditor/util/hooks/signals.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { type } from 'os';
import { useCallback, useEffect, useRef, useState } from 'react';
import { Object3D } from 'three';
import { Editor } from '../../js/Editor';
Expand Down
121 changes: 121 additions & 0 deletions src/ThreeEditor/util/hooks/useKeyboardEditorControls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { RefObject, useEffect } from "react";
import { Object3D } from "three";
import { RemoveDetectGeometryCommand } from "../../js/commands/RemoveDetectGeometryCommand";
import { RemoveDifferentialModifierCommand } from "../../js/commands/RemoveDifferentialModifierCommand";
import { RemoveFilterCommand } from "../../js/commands/RemoveFilterCommand";
import { RemoveObjectCommand } from "../../js/commands/RemoveObjectCommand";
import { RemoveQuantityCommand } from "../../js/commands/RemoveQuantityCommand";
import { RemoveZoneCommand } from "../../js/commands/RemoveZoneCommand";
import { SetFilterRuleCommand } from "../../js/commands/SetFilterRuleCommand";
import { Editor } from "../../js/Editor";
import { isZone } from "../CSG/CSGZone";
import { isDetectFilter } from "../Detect/DetectFilter";
import { isDetectGeometry } from "../Detect/DetectGeometry";
import { isOutput } from "../Scoring/ScoringOutput";
import { isQuantity } from "../Scoring/ScoringQuantity";


export const useKeyboardEditorControls = (editor: Editor | undefined, containerRef: RefObject<HTMLElement>) => {
useEffect(() => {
if (!editor || !containerRef.current) return;

const container = containerRef.current;

const { config } = editor;

const IS_MAC = navigator.platform.toUpperCase().indexOf('MAC') >= 0;

const getRemoveCommand = (object: Object3D) => {

if (isDetectGeometry(object)) {
return new RemoveDetectGeometryCommand(editor, object);

} else if (isZone(object)) {
return new RemoveZoneCommand(editor, object);

}
else if (isDetectFilter(object)) {
if (object.selectedRule) return new SetFilterRuleCommand(editor, object);
return new RemoveFilterCommand(editor, object);

} else if (isQuantity(object)) {
if (object.selectedModifier)
return new RemoveDifferentialModifierCommand(
editor,
object,
object.selectedModifier
);

if (isOutput(object.parent))
return new RemoveQuantityCommand(editor, object, object.parent);
else throw new Error('Quantity has no parent output');
}

return new RemoveObjectCommand(editor, object);


};

const onKeyDown = (event: KeyboardEvent) => {
const eventFromSidebar = (event.target as any).closest('.ThreeEditorSidebar') !== null;

if (eventFromSidebar) return;

switch (event.key.toLowerCase()) {
case 'delete':
const object = editor.selected;

if (object === null || object.notRemovable === true) return;

const parent = object.parent;
if (parent !== null) editor.execute(getRemoveCommand(object));
break;

// Disabled features
// case config.getKey('settings/shortcuts/translate'):
// signals.transformModeChanged.dispatch('translate');

// break;

// case config.getKey('settings/shortcuts/rotate'):
// signals.transformModeChanged.dispatch('rotate');

// break;

// case config.getKey('settings/shortcuts/scale'):
// signals.transformModeChanged.dispatch('scale');

// break;

case config.getKey('settings/shortcuts/undo'):
if (IS_MAC ? event.metaKey : event.ctrlKey) {
event.preventDefault(); // Prevent browser specific hotkeys

if (event.shiftKey) {
editor.redo();
} else {
editor.undo();
}
}

break;

case config.getKey('settings/shortcuts/focus'):
if (editor.selected !== null) {
editor.focus(editor.selected);
}

break;

default:
break;
}
}

container.addEventListener("keydown", onKeyDown);
return () => {
container.removeEventListener("keydown", onKeyDown);
};
}, [containerRef, editor]);

}
2 changes: 0 additions & 2 deletions src/WrapperApp/components/InputEditor/InputEditorPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import { DEMO_MODE } from '../../../util/Config';
import { useSnackbar } from 'notistack';
import { throttle } from 'throttle-debounce';
import { EditorJson } from '../../../ThreeEditor/js/EditorJson';
import useTheme from '@mui/system/useTheme';

interface InputEditorPanelProps {
goToRun?: () => void;
}
Expand Down

0 comments on commit 1281cc4

Please sign in to comment.