Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Prefab System #10047

Draft
wants to merge 10 commits into
base: dev
Choose a base branch
from
8 changes: 8 additions & 0 deletions packages/client-core/i18n/en/editor.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@
"lbl-nodeHelpers": "Toggle Node Helpers",
"info-nodeHelpers": "Helper geometry that helps components have visibility in the scene when inactive."
},
"studioModeToggle":{
"lbl-simple": "Simple",
"info-simple": "simple mode",
"lbl-advanced": "Advanced",
"info-advanced": "advanced mode"
},
"sceneScreenshot": {
"lbl": "Screenshot",
"info": "Takes a screenshot of your scene at the current view."
Expand Down Expand Up @@ -1001,6 +1007,8 @@
"copyURL": "Copy URL",
"openInNewTab": "Open URL in New Tab",
"deleteAsset": "Delete Asset",
"prefab": "Prefab",
"prefab-search" : "Search Prefabs ...",
"components": "Components",
"components-search": "Search Components ...",
"component-detail": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { NameComponent } from '@etherealengine/spatial/src/common/NameComponent'
import { EntityTreeComponent, traverseEntityNode } from '@etherealengine/spatial/src/transform/components/EntityTree'

import MenuItem from '@mui/material/MenuItem'
import { PopoverPosition } from '@mui/material/Popover'
import Popover, { PopoverPosition } from '@mui/material/Popover'

import { NotificationService } from '@etherealengine/client-core/src/common/services/NotificationService'
import { Engine, EntityUUID, UUIDComponent } from '@etherealengine/ecs'
Expand All @@ -52,14 +52,17 @@ import { EditorControlFunctions } from '../../functions/EditorControlFunctions'
import { addMediaNode } from '../../functions/addMediaNode'
import { isAncestor } from '../../functions/getDetachedObjectsRoots'
import { cmdOrCtrlString } from '../../functions/utils'
import { EditorHelperState } from '../../services/EditorHelperState'
import { EditorState } from '../../services/EditorServices'
import { SelectionState } from '../../services/SelectionServices'
import Search from '../Search/Search'
import useUpload from '../assets/useUpload'
import { PopoverContext } from '../element/PopoverContext'
import { PropertiesPanelButton } from '../inputs/Button'
import { ContextMenu } from '../layout/ContextMenu'
import { HeirarchyTreeNodeType, heirarchyTreeWalker } from './HeirarchyTreeWalker'
import { HierarchyTreeNode, HierarchyTreeNodeProps, RenameNodeData, getNodeElId } from './HierarchyTreeNode'
import PrefabList from './PrefabList'
import styles from './styles.module.scss'

/**
Expand Down Expand Up @@ -463,7 +466,8 @@ function HierarchyPanelContents({ rootEntityUUID }: { rootEntityUUID: EntityUUID
{MemoTreeNode}
</FixedSizeList>
)

const anchorElement = useHookstate<HTMLButtonElement | null>(null)
const open = !!anchorElement.value
return (
<>
<div className={styles.panelContainer}>
Expand All @@ -473,6 +477,7 @@ function HierarchyPanelContents({ rootEntityUUID }: { rootEntityUUID: EntityUUID
<div style={{ height: '100%' }}>
<AutoSizer onResize={HierarchyList}>{HierarchyList}</AutoSizer>
</div>

<PropertiesPanelButton
variant="contained"
// TODO see why we have to specify capitalize here
Expand All @@ -483,11 +488,40 @@ function HierarchyPanelContents({ rootEntityUUID }: { rootEntityUUID: EntityUUID
fontSize: '12px',
lineHeight: '0.5'
}}
onClick={() => EditorControlFunctions.createObjectFromSceneElement()}
onClick={(event) => {
anchorElement.set(event.currentTarget)
if (getMutableState(EditorHelperState).studioMode.value == 'Advanced') {
EditorControlFunctions.createObjectFromSceneElement()
}
}}
>
{t('editor:hierarchy.lbl-addEntity')}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should change the language here for Add Entity in simple mode to Add Object

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This lines mean if user select Advanced mode the studio will be same.
When select simple mode the fuctions of add object are in PrefabList.tsx which was called in HierarchyPanelContainer.tsx line 523.

</PropertiesPanelButton>
</div>
<PopoverContext.Provider
value={{
handlePopoverClose: () => {
anchorElement.set(null)
}
}}
>
<Popover
id={open ? 'add-component-popover' : undefined}
open={open && getMutableState(EditorHelperState).studioMode.value == 'Simple'}
HexaField marked this conversation as resolved.
Show resolved Hide resolved
anchorEl={anchorElement.value}
onClose={() => anchorElement.set(null)}
anchorOrigin={{
vertical: 'center',
horizontal: 'left'
}}
transformOrigin={{
vertical: 'center',
horizontal: 'right'
}}
>
<PrefabList />
</Popover>
</PopoverContext.Provider>
<ContextMenu open={!!anchorEl} anchorEl={anchorEl} anchorPosition={anchorPosition} onClose={handleClose}>
<MenuItem onClick={() => onRenameNode(contextSelectedItem!)}>{t('editor:hierarchy.lbl-rename')}</MenuItem>
<Hotkeys
Expand Down
61 changes: 61 additions & 0 deletions packages/editor/src/components/hierarchy/PrefabEditors.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
CPAL-1.0 License

The contents of this file are subject to the Common Public Attribution License
Version 1.0. (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
https://github.com/EtherealEngine/etherealengine/blob/dev/LICENSE.
The License is based on the Mozilla Public License Version 1.1, but Sections 14
and 15 have been added to cover use of software over a computer network and
provide for limited attribution for the Original Developer. In addition,
Exhibit A has been modified to be consistent with Exhibit B.

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
specific language governing rights and limitations under the License.

The Original Code is Ethereal Engine.

The Original Developer is the Initial Developer. The Initial Developer of the
Original Code is the Ethereal Engine team.

All portions of the code written by the Ethereal Engine team are Copyright © 2021-2023
Ethereal Engine. All Rights Reserved.
*/
import { Component } from '@etherealengine/ecs/src/ComponentFunctions'
import { LoopAnimationComponent } from '@etherealengine/engine/src/avatar/components/LoopAnimationComponent'
import { EnvmapComponent } from '@etherealengine/engine/src/scene/components/EnvmapComponent'
import { ModelComponent } from '@etherealengine/engine/src/scene/components/ModelComponent'
import { PostProcessingComponent } from '@etherealengine/engine/src/scene/components/PostProcessingComponent'
import { ShadowComponent } from '@etherealengine/engine/src/scene/components/ShadowComponent'
import { SkyboxComponent } from '@etherealengine/engine/src/scene/components/SkyboxComponent'
import { VariantComponent } from '@etherealengine/engine/src/scene/components/VariantComponent'
import { defineState } from '@etherealengine/hyperflux'
import { ColliderComponent } from '@etherealengine/spatial/src/physics/components/ColliderComponent'
import { RigidBodyComponent } from '@etherealengine/spatial/src/physics/components/RigidBodyComponent'
import { TriggerComponent } from '@etherealengine/spatial/src/physics/components/TriggerComponent'
import { AmbientLightComponent } from '@etherealengine/spatial/src/renderer/components/AmbientLightComponent'
import { DirectionalLightComponent } from '@etherealengine/spatial/src/renderer/components/DirectionalLightComponent'
import { FogSettingsComponent } from '@etherealengine/spatial/src/renderer/components/FogSettingsComponent'
import { HemisphereLightComponent } from '@etherealengine/spatial/src/renderer/components/HemisphereLightComponent'
import { PointLightComponent } from '@etherealengine/spatial/src/renderer/components/PointLightComponent'
import { SpotLightComponent } from '@etherealengine/spatial/src/renderer/components/SpotLightComponent'

export const PrefabShelfCategories = defineState({
name: 'ee.editor.PrefabShelfCategories',
initial: () => {
return {
'Light Prefab': [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beyond this state, we need to also have the infrastructure to swap the component used inside the light editor, this alone doesn't make sense. We only want one of these components on an entity, and we want to have a dropdown in the properties editor for this. Same goes for the others.

AmbientLightComponent,
DirectionalLightComponent,
HemisphereLightComponent,
PointLightComponent,
SpotLightComponent
],
'Physics Prefab': [ColliderComponent, RigidBodyComponent, TriggerComponent],
'Model Prefab': [ModelComponent, LoopAnimationComponent, VariantComponent, EnvmapComponent, ShadowComponent],
'Setting Prefab': [SkyboxComponent, FogSettingsComponent, PostProcessingComponent, FogSettingsComponent]
//will continue to add more prefabs
} as Record<string, Component[]>
}
})
149 changes: 149 additions & 0 deletions packages/editor/src/components/hierarchy/PrefabList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
CPAL-1.0 License

The contents of this file are subject to the Common Public Attribution License
Version 1.0. (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
https://github.com/EtherealEngine/etherealengine/blob/dev/LICENSE.
The License is based on the Mozilla Public License Version 1.1, but Sections 14
and 15 have been added to cover use of software over a computer network and
provide for limited attribution for the Original Developer. In addition,
Exhibit A has been modified to be consistent with Exhibit B.

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
specific language governing rights and limitations under the License.

The Original Code is Ethereal Engine.

The Original Developer is the Initial Developer. The Initial Developer of the
Original Code is the Ethereal Engine team.

All portions of the code written by the Ethereal Engine team are Copyright © 2021-2023
Ethereal Engine. All Rights Reserved.
*/

import { startCase } from 'lodash'
import React, { useRef } from 'react'
import { useTranslation } from 'react-i18next'

import { getMutableState, getState, useHookstate } from '@etherealengine/hyperflux'

import PlaceHolderIcon from '@mui/icons-material/GroupAddOutlined'
import { List, ListItemButton, ListItemIcon, ListItemText } from '@mui/material'

import InputText from '@etherealengine/client-core/src/common/components/InputText'
import { Component } from '@etherealengine/ecs/src/ComponentFunctions'
import { ComponentJsonType } from '@etherealengine/engine/src/scene/types/SceneTypes'
import Typography from '@etherealengine/ui/src/primitives/mui/Typography'
import { ComponentEditorsState } from '../../functions/ComponentEditors'
import { EditorControlFunctions } from '../../functions/EditorControlFunctions'
import { usePopoverContextClose } from '../element/PopoverContext'
import { PrefabShelfCategories } from './PrefabEditors'

const PrefabListItem = ({ item }: { item: string }) => {
const { t } = useTranslation()
const Icon = getState(ComponentEditorsState)[item]?.iconComponent ?? PlaceHolderIcon
const handleClosePopover = usePopoverContextClose()

return (
<ListItemButton
sx={{ pl: 4, bgcolor: 'var(--dockBackground)' }}
onClick={() => {
const PrefabNameShelfCategories = getState(PrefabShelfCategories)
const componentJsons: ComponentJsonType[] = []
PrefabNameShelfCategories[item].forEach((component) => {
componentJsons.push({ name: component.jsonID as string })
})
EditorControlFunctions.createObjectFromSceneElement(componentJsons)
handleClosePopover()
}}
>
<ListItemIcon style={{ color: 'var(--textColor)' }}>
<Icon />
</ListItemIcon>
<ListItemText
primary={
<Typography variant="subtitle1" color={'var(--textColor)'}>
{startCase(item.replace('-', ' ').toLowerCase())}
</Typography>
}
secondary={
<Typography variant="caption" color={'var(--textColor)'}>
{t(`editor:layout.assetGrid.component-detail.${item}`)}
</Typography>
}
/>
</ListItemButton>
)
}
const ScenePrefabListItem = ({ categoryItems }: { categoryItems: string[]; isCollapsed: boolean }) => {
return (
<>
<List component={'div'} sx={{ bgcolor: 'var(--dockBackground)', width: '100%' }} disablePadding>
{categoryItems.map((item) => (
<PrefabListItem item={item} />
))}
</List>
</>
)
}

const usePrefabShelfCategories = (search: string) => {
useHookstate(getMutableState(PrefabShelfCategories)).value

if (!search) {
return Object.entries(getState(PrefabShelfCategories))
}

const searchRegExp = new RegExp(search, 'gi')

return Object.entries(getState(PrefabShelfCategories))
.map(([category, items]) => {
const filteredcategory = category.match(searchRegExp)?.length ? category : ''
return [filteredcategory, items] as [string, Component[]]
})
.filter(([_, items]) => !!items.length)
}

export function PrefabList() {
const { t } = useTranslation()
const search = useHookstate({ local: '', query: '' })
const searchTimeout = useRef<ReturnType<typeof setTimeout> | null>(null)
const shelves = usePrefabShelfCategories(search.query.value)
const shelveslist: string[] = []
shelves.map(([category, items]) => {
shelveslist.push(category)
})

const onSearch = (text: string) => {
search.local.set(text)
if (searchTimeout.current) clearTimeout(searchTimeout.current)
searchTimeout.current = setTimeout(() => {
search.query.set(text)
}, 50)
}

return (
<List
sx={{ width: 300, height: 900, bgcolor: 'var(--dockBackground)' }}
subheader={
<div style={{ padding: '0.5rem' }}>
<Typography style={{ color: 'var(--textColor)', textAlign: 'center', textTransform: 'uppercase' }}>
{t('editor:layout.assetGrid.prefab')}
</Typography>
<InputText
placeholder={t('editor:layout.assetGrid.prefab-search')}
value={search.local.value}
sx={{ mt: 1 }}
onChange={(e) => onSearch(e.target.value)}
/>
</div>
}
>
<ScenePrefabListItem categoryItems={shelveslist} isCollapsed={!!search.query.value} />
</List>
)
}

export default PrefabList
2 changes: 2 additions & 0 deletions packages/editor/src/components/toolbar/ToolBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import PublishLocation from './tools/PublishLocation'
import RenderModeTool from './tools/RenderModeTool'
import SceneScreenshot from './tools/SceneScreenshot'
import StatsTool from './tools/StatsTool'
import StudioModeTool from './tools/StudioModeTool'
import TransformPivotTool from './tools/TransformPivotTool'
import TransformSnapTool from './tools/TransformSnapTool'
import TransformSpaceTool from './tools/TransformSpaceTool'
Expand All @@ -65,6 +66,7 @@ export const ToolBar = (props: ToolBarProps) => {
<StatsTool />
<HelperToggleTool />
<SceneScreenshot />
<StudioModeTool />
<PublishLocation />
<EditorNavbarProfile />
</div>
Expand Down