Skip to content
This repository was archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
Added functionality to copy scene to another project.
Browse files Browse the repository at this point in the history
  • Loading branch information
barankyle committed Apr 3, 2024
1 parent 8345d3b commit 8e2b92c
Show file tree
Hide file tree
Showing 8 changed files with 316 additions and 32 deletions.
9 changes: 9 additions & 0 deletions packages/client-core/i18n/en/editor.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"menubar": {
"newScene": "New Scene",
"saveScene": "Save Scene",
"copyScene": "Copy Scene to Project",
"saveAs": "Save As",
"importAsset": "Import Asset",
"importSettings": "Import Settings",
Expand Down Expand Up @@ -1175,6 +1176,14 @@
"info-name": "Name must be between 4 and 64 characters and cannot contain underscores",
"lbl-confirm": "Save Scene"
},
"copyScene": {
"title": "Copy Scene to Project",
"lbl-name": "New Scene Name",
"info-name": "Name must be between 4 and 64 characters and cannot contain underscores",
"lbl-thumbnail": "Scene copied",
"lbl-confirm": "Copy Scene to Project",
"project": "Destination Project"
},
"support": {
"title": "Support",
"header": "Need to report a problem?",
Expand Down
10 changes: 10 additions & 0 deletions packages/common/src/schemas/projects/scene.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ export const scenePatchSchema = Type.Object(
{
newSceneName: Type.Optional(Type.String()),
oldSceneName: Type.Optional(Type.String()),
isCopy: Type.Optional(Type.Boolean()),
storageProviderName: Type.Optional(Type.String()),
project: Type.Optional(Type.String()),
oldProjectName: Type.Optional(Type.String()),
directory: Type.Optional(Type.String()),
localDirectory: Type.Optional(Type.String())
},
Expand Down Expand Up @@ -170,6 +172,14 @@ export const sceneQuerySchema = Type.Intersect(
)
export interface SceneQuery extends Static<typeof sceneQuerySchema> {}

export interface SaveSceneOptions {
projectName: string
sceneName: string
signal: AbortSignal
oldProjectName?: string
oldSceneName?: string
}

// export const componentJsonValidator = /* @__PURE__ */ getValidator(componentJsonSchema, dataValidator)
// export const entityJsonValidator = /* @__PURE__ */ getValidator(entityJsonSchema, dataValidator)
// export const sceneJsonValidator = /* @__PURE__ */ getValidator(sceneJsonSchema, dataValidator)
Expand Down
44 changes: 41 additions & 3 deletions packages/editor/src/components/EditorContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { getMutableState, getState, useHookstate } from '@etherealengine/hyperfl
import Dialog from '@mui/material/Dialog'

import { NotificationService } from '@etherealengine/client-core/src/common/services/NotificationService'
import { SceneDataType, scenePath } from '@etherealengine/common/src/schema.type.module'
import { SaveSceneOptions, SceneDataType, scenePath } from '@etherealengine/common/src/schema.type.module'
import { useQuery } from '@etherealengine/ecs/src/QueryFunctions'
import { SceneServices, SceneState } from '@etherealengine/engine/src/scene/Scene'
import { SceneAssetPendingTagComponent } from '@etherealengine/engine/src/scene/components/SceneAssetPendingTagComponent'
Expand All @@ -57,6 +57,7 @@ import { ProjectBrowserPanelTab } from './assets/ProjectBrowserPanel'
import { SceneAssetsPanelTab } from './assets/SceneAssetsPanel'
import { ScenePanelTab } from './assets/ScenesPanel'
import { ControlText } from './controlText/ControlText'
import CopySceneDialog from './dialogs/CopySceneDialog'
import { DialogState } from './dialogs/DialogState'
import ErrorDialog from './dialogs/ErrorDialog'
import { ProgressDialog } from './dialogs/ProgressDialog'
Expand Down Expand Up @@ -183,7 +184,14 @@ const onSaveAs = async () => {
})
DialogState.setDialog(null)
if (result?.name && projectName) {
await saveScene(projectName, result.name, abortController.signal)
const saveObject = {
projectName,
sceneName: result.name,
signal: abortController.signal,
oldProjectName: projectName
} as SaveSceneOptions
if (sceneName) saveObject.oldSceneName = sceneName
await saveScene(saveObject)
getMutableState(SceneState).sceneModified.set(false)
const newSceneData = (await Engine.instance.api
.service(scenePath)
Expand All @@ -199,6 +207,32 @@ const onSaveAs = async () => {
}
}

const onCopyScene = async () => {
const { projectName, sceneName } = getState(EditorState)
const abortController = new AbortController()
const result = (await new Promise((resolve) => {
DialogState.setDialog(
<CopySceneDialog
currentSceneName={sceneName!}
currentProjectName={projectName!}
onConfirm={resolve}
onCancel={resolve}
/>
)
})) as { name: string; projectName: string }
DialogState.setDialog(null)
if (result?.name && result?.projectName) {
const saveSceneObject = {
projectName: result.projectName,
sceneName: result.name,
signal: abortController.signal
} as SaveSceneOptions
if (sceneName) saveSceneObject.oldSceneName = sceneName
if (projectName) saveSceneObject.oldProjectName = projectName
await saveScene(saveSceneObject)
}
}

const onImportSettings = () => {
DialogState.setDialog(<ImportSettingsPanel />)
}
Expand Down Expand Up @@ -260,7 +294,7 @@ const onSaveScene = async () => {
await new Promise((resolve) => setTimeout(resolve, 5))

try {
await saveScene(projectName, sceneName, abortController.signal)
await saveScene({ projectName, sceneName, signal: abortController.signal })

getMutableState(SceneState).sceneModified.set(false)

Expand Down Expand Up @@ -289,6 +323,10 @@ const generateToolbarMenu = () => {
name: t('editor:menubar.saveAs'),
action: onSaveAs
},
{
name: t('editor:menubar.copyScene'),
action: onCopyScene
},
{
name: t('editor:menubar.importSettings'),
action: onImportSettings
Expand Down
139 changes: 139 additions & 0 deletions packages/editor/src/components/dialogs/CopySceneDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
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 { useHookstate } from '@hookstate/core'
import React, { useCallback } from 'react'
import { useTranslation } from 'react-i18next'

import InputSelect, { InputMenuItem } from '@etherealengine/client-core/src/common/components/InputSelect'
import { projectPath } from '@etherealengine/common/src/schema.type.module'
import { useFind } from '@etherealengine/spatial/src/common/functions/FeathersHooks'
import FormField from '../inputs/FormField'
import StringInput from '../inputs/StringInput'
import Dialog from './Dialog'

/**
* CopySceneDialog used to show dialog for saving scene to another project
*/
export function CopySceneDialog({
currentSceneName,
currentProjectName,
onConfirm,
onCancel
}: {
currentSceneName: string
currentProjectName: string
onConfirm: (val: { name: string; projectName: string }) => void
onCancel: (val?: {}) => void
}) {
const name = useHookstate('')
const { t } = useTranslation()

const projectsQuery = useFind(projectPath, {
query: {
allowed: true,
$limit: 100,
action: 'admin',
$sort: {
name: 1
}
}
})

const projectsMenu: InputMenuItem[] = projectsQuery.data.map((el) => {
return {
label: el.name,
value: el.name
}
})
const selectedProject = useHookstate('')
const handleProjectChange = (e) => {
const { value } = e.target
selectedProject.set(value)
}
/**
* onConfirmCallback callback function is used handle confirm dialog.
*
* @type {function}
*/
const onConfirmCallback = useCallback(
(e) => {
e.preventDefault()
onConfirm({ name: name.value, projectName: selectedProject.value })
},
[onConfirm]
)

/**
* onCancelCallback callback function used to handle cancel of dialog.
*
* @type {function}
*/
const onCancelCallback = useCallback(
(e) => {
e.preventDefault()
onCancel()
},
[onCancel]
)

//returning view for dialog view.
return (
<Dialog
title={t('editor:dialog.copyScene.title')}
onConfirm={onConfirmCallback}
onCancel={onCancelCallback}
confirmLabel={t('editor:dialog.copyScene.lbl-confirm')}
disabled={selectedProject.value === '' || name.value.length === 0}
>
<div style={{ width: '100%' }}>
<div style={{ display: 'flex', flexDirection: 'column', justifyContent: 'center', alignItems: 'center' }}>
<div>Current Scene: {currentSceneName}</div>
<div>Current Project: {currentProjectName}</div>
<FormField>
<label htmlFor="name">{t('editor:dialog.copyScene.lbl-name')}</label>
<StringInput
id="name"
required
pattern={'[A-Za-z0-9-\':"!@#$%^&*(),.?~ ]{4,64}'}
title={t('editor:dialog.copyScene.info-name')}
value={name.value}
onChange={(newName) => name.set(newName)}
/>
</FormField>
<InputSelect
name="selectProject"
label={t('editor:dialog.copyScene.project')}
value={selectedProject.value}
menu={projectsMenu}
onChange={handleProjectChange}
/>
</div>
</div>
</Dialog>
)
}

export default CopySceneDialog
15 changes: 10 additions & 5 deletions packages/editor/src/components/dialogs/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@ const dialogHeaderStyles = {
display: 'flex',
flexDirection: 'row',
justifyContent: 'space-between',
padding: '0 8px',
fontSize: '12px',
padding: '8px',
fontSize: '20px',
overflow: 'hidden',
height: '32px',
background: 'black',
borderTopLeftRadius: 'inherit',
borderTopRightRadius: 'inherit',
color: 'white'
Expand Down Expand Up @@ -95,6 +93,7 @@ interface Props {
confirmLabel?: string
onCancel?: any
onConfirm?: any
disabled?: boolean
}

const Dialog = (props: Props) => {
Expand All @@ -113,6 +112,7 @@ const Dialog = (props: Props) => {
return (
<DialogForm tag={props.tag} onSubmit={onSubmitForm}>
<div style={dialogContainerStyles as React.CSSProperties}>
<div style={dialogHeaderStyles as React.CSSProperties}>{props.title}</div>
<div style={dialogContentStyles as React.CSSProperties}>{props.children}</div>
{(props.onConfirm || props.onCancel || props.bottomNav) && (
<div style={dialogBottomNavStyles}>
Expand All @@ -123,7 +123,12 @@ const Dialog = (props: Props) => {
</SecondaryButton>
)}
{props.onConfirm && (
<Button type="submit" onClick={props.tag === 'form' ? null : props.onConfirm} style={buttonStyles}>
<Button
disabled={props.disabled}
type="submit"
onClick={props.tag === 'form' ? null : props.onConfirm}
style={buttonStyles}
>
{props.confirmLabel}
</Button>
)}
Expand Down
Loading

0 comments on commit 8e2b92c

Please sign in to comment.