|
1 |
| -import { ipcRenderer } from "electron"; |
2 |
| -import { basename, join, dirname } from "path/posix"; |
3 |
| -import { pathExists, readJSON, writeJSON } from "fs-extra"; |
4 |
| - |
5 |
| -import { ReactNode } from "react"; |
6 |
| - |
7 |
| -import { FaRegClone } from "react-icons/fa6"; |
8 |
| -import { GiMaterialsScience } from "react-icons/gi"; |
9 |
| - |
10 |
| -import { Tools } from "babylonjs"; |
11 |
| - |
12 |
| -import { UniqueNumber } from "../../../../tools/tools"; |
13 |
| - |
14 |
| -import { showAlert, showPrompt } from "../../../../ui/dialog"; |
15 |
| -import { ContextMenuItem } from "../../../../ui/shadcn/ui/context-menu"; |
16 |
| - |
17 |
| -import { openMaterialViewer } from "../viewers/material-viewer"; |
18 |
| - |
19 |
| -import { AssetsBrowserItem } from "./item"; |
20 |
| - |
21 |
| -export class AssetBrowserMaterialItem extends AssetsBrowserItem { |
22 |
| - /** |
23 |
| - * @override |
24 |
| - */ |
25 |
| - protected getIcon(): ReactNode { |
26 |
| - return <GiMaterialsScience size="64px" />; |
27 |
| - } |
28 |
| - |
29 |
| - /** |
30 |
| - * @override |
31 |
| - */ |
32 |
| - protected async onDoubleClick(): Promise<void> { |
33 |
| - const data = await readJSON(this.props.absolutePath); |
34 |
| - if (data.customType === "BABYLON.NodeMaterial") { |
35 |
| - ipcRenderer.send("window:open", "build/src/editor/windows/nme", { |
36 |
| - filePath: this.props.absolutePath, |
37 |
| - }); |
38 |
| - } else { |
39 |
| - openMaterialViewer(this.props.editor, this.props.absolutePath); |
40 |
| - } |
41 |
| - } |
42 |
| - |
43 |
| - /** |
44 |
| - * Returns the context menu content for the current item. |
45 |
| - * To be overriden by the specialized items implementations. |
46 |
| - */ |
47 |
| - protected getContextMenuContent(): ReactNode { |
48 |
| - return ( |
49 |
| - <> |
50 |
| - <ContextMenuItem className="flex items-center gap-2" onClick={() => this._handleClone()}> |
51 |
| - <FaRegClone className="w-4 h-4" /> Clone... |
52 |
| - </ContextMenuItem> |
53 |
| - </> |
54 |
| - ); |
55 |
| - } |
56 |
| - |
57 |
| - private async _handleClone(): Promise<unknown> { |
58 |
| - const data = await readJSON(this.props.absolutePath); |
59 |
| - data.id = Tools.RandomId(); |
60 |
| - data.uniqueId = UniqueNumber.Get(); |
61 |
| - |
62 |
| - let name = await showPrompt("Enter the name for the cloned material", undefined, basename(this.props.absolutePath).replace(".material", "")); |
63 |
| - |
64 |
| - if (!name) { |
65 |
| - return; |
66 |
| - } |
67 |
| - |
68 |
| - if (!name.endsWith(".material")) { |
69 |
| - name += ".material"; |
70 |
| - } |
71 |
| - |
72 |
| - const absoluteDestination = join(dirname(this.props.absolutePath), name); |
73 |
| - |
74 |
| - if (await pathExists(absoluteDestination)) { |
75 |
| - return showAlert("Can't clone material", `A material with name ("${name}") already exists in the current folder.`); |
76 |
| - } |
77 |
| - |
78 |
| - await writeJSON(absoluteDestination, data, { |
79 |
| - spaces: "\t", |
80 |
| - encoding: "utf-8", |
81 |
| - }); |
82 |
| - |
83 |
| - this.props.editor.layout.assets.refresh(); |
84 |
| - this.props.editor.layout.assets.setSelectedFile(absoluteDestination); |
85 |
| - } |
86 |
| -} |
| 1 | +import { ipcRenderer } from "electron"; |
| 2 | +import { basename, join, dirname } from "path/posix"; |
| 3 | +import { pathExists, readJSON, writeJSON } from "fs-extra"; |
| 4 | + |
| 5 | +import { ReactNode } from "react"; |
| 6 | + |
| 7 | +import { FaRegClone } from "react-icons/fa6"; |
| 8 | + |
| 9 | +import { Tools } from "babylonjs"; |
| 10 | + |
| 11 | +import { UniqueNumber } from "../../../../tools/tools"; |
| 12 | + |
| 13 | +import { showAlert, showPrompt } from "../../../../ui/dialog"; |
| 14 | +import { ContextMenuItem } from "../../../../ui/shadcn/ui/context-menu"; |
| 15 | + |
| 16 | +import { openMaterialViewer } from "../viewers/material-viewer"; |
| 17 | +import { MaterialThumbnailRenderer } from "../renderers/material-thumbnail"; |
| 18 | + |
| 19 | +import { AssetsBrowserItem } from "./item"; |
| 20 | + |
| 21 | +export class AssetBrowserMaterialItem extends AssetsBrowserItem { |
| 22 | + /** |
| 23 | + * @override |
| 24 | + */ |
| 25 | + protected getIcon(): ReactNode { |
| 26 | + return ( |
| 27 | + <div className="w-full h-full pointer-events-none"> |
| 28 | + <MaterialThumbnailRenderer absolutePath={this.props.absolutePath} /> |
| 29 | + </div> |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @override |
| 35 | + */ |
| 36 | + protected async onDoubleClick(): Promise<void> { |
| 37 | + const data = await readJSON(this.props.absolutePath); |
| 38 | + if (data.customType === "BABYLON.NodeMaterial") { |
| 39 | + ipcRenderer.send("window:open", "build/src/editor/windows/nme", { |
| 40 | + filePath: this.props.absolutePath, |
| 41 | + }); |
| 42 | + } else { |
| 43 | + openMaterialViewer(this.props.editor, this.props.absolutePath); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Returns the context menu content for the current item. |
| 49 | + * To be overriden by the specialized items implementations. |
| 50 | + */ |
| 51 | + protected getContextMenuContent(): ReactNode { |
| 52 | + return ( |
| 53 | + <> |
| 54 | + <ContextMenuItem className="flex items-center gap-2" onClick={() => this._handleClone()}> |
| 55 | + <FaRegClone className="w-4 h-4" /> Clone... |
| 56 | + </ContextMenuItem> |
| 57 | + </> |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + private async _handleClone(): Promise<unknown> { |
| 62 | + const data = await readJSON(this.props.absolutePath); |
| 63 | + data.id = Tools.RandomId(); |
| 64 | + data.uniqueId = UniqueNumber.Get(); |
| 65 | + |
| 66 | + let name = await showPrompt("Enter the name for the cloned material", undefined, basename(this.props.absolutePath).replace(".material", "")); |
| 67 | + |
| 68 | + if (!name) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + if (!name.endsWith(".material")) { |
| 73 | + name += ".material"; |
| 74 | + } |
| 75 | + |
| 76 | + const absoluteDestination = join(dirname(this.props.absolutePath), name); |
| 77 | + |
| 78 | + if (await pathExists(absoluteDestination)) { |
| 79 | + return showAlert("Can't clone material", `A material with name ("${name}") already exists in the current folder.`); |
| 80 | + } |
| 81 | + |
| 82 | + await writeJSON(absoluteDestination, data, { |
| 83 | + spaces: "\t", |
| 84 | + encoding: "utf-8", |
| 85 | + }); |
| 86 | + |
| 87 | + this.props.editor.layout.assets.refresh(); |
| 88 | + this.props.editor.layout.assets.setSelectedFile(absoluteDestination); |
| 89 | + } |
| 90 | +} |
0 commit comments