From 95943d97a4f47a89a2885f16f3e778d3b4125576 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Patrick=20H=C3=A4rtl?= <haertl@ux3d.io>
Date: Tue, 15 Oct 2024 13:09:59 +0200
Subject: [PATCH 1/2] Add environment credits

---
 index.html                 |  2 ++
 src/logic/uimodel.js       | 35 +++++++++++++++++++++++++++++++----
 src/main.js                |  4 ++--
 src/model_path_provider.js |  4 +++-
 src/ui/ui.js               |  2 +-
 5 files changed, 39 insertions(+), 8 deletions(-)

diff --git a/index.html b/index.html
index 23c59b54..ef6f1c03 100644
--- a/index.html
+++ b/index.html
@@ -376,6 +376,8 @@ <h2 class="title is-spaced">Animation</h2>
                                 <h2 class="title">Model Credits</h2>
                                 <div class="modelCredit"><i>Copyright:</i><br/>{{ assetCopyright }}</div>
                                 <div class="modelCredit"><i>Generated by:</i><br/>{{ assetGenerator }}</div>
+                                <h2 class="title">Environment Credits</h2>
+                                <div class="modelCredit"><i>Copyright:</i><br/><p v-html="environmentLicense"></p></div>
                                 <h3 class="title">{{ xmp ? "XMP" : "" }}</h3>
                                 <json-to-ui-template v-bind:data="xmp" v-bind:isinner="false"></json-to-ui-template>
                             </div>
diff --git a/src/logic/uimodel.js b/src/logic/uimodel.js
index df736140..9745e81f 100644
--- a/src/logic/uimodel.js
+++ b/src/logic/uimodel.js
@@ -24,7 +24,7 @@ class UIModel
         this.environmentRotation = app.environmentRotationChanged.pipe();
         this.app.environments = environments;
         const selectedEnvironment = app.selectedEnvironmentChanged.pipe(
-            map(environmentName => this.app.environments[environmentName].hdr_path)
+            map(environmentName => this.app.environments[environmentName])
         );
         const initialEnvironment = "Cannon_Exterior";
         this.app.selectedEnvironment = initialEnvironment;
@@ -106,12 +106,38 @@ class UIModel
         );
 
         this.model = merge(dropdownGltfChanged, dropdownFlavourChanged, inputObservables.droppedGltf);
+
         this.hdr = merge(selectedEnvironment, this.addEnvironment, inputObservables.droppedHdr).pipe(
-            startWith(environments[initialEnvironment].hdr_path)
+            startWith(environments[initialEnvironment])
         );
 
+        this.hdr.subscribe(async hdr => {
+            if (hdr.license_path !== undefined) {
+                try {
+                    const response = await fetch(hdr.license_path);
+                    if (!response.ok) {
+                        throw new Error("License file not found");
+                    }
+                    let text = await response.text();
+                    const license = text.split("SPDX-License-Identifier: ")[1];
+                    console.log(license);
+                    text = text.replace("SPDX-FileCopyrightText: ", "");
+                    text = text.replace(/SPDX-License-Identifier:(.)*/g, `, <a href="${hdr.hdr_path}">${hdr.hdr_path}</a>, License: `);
+                    text += `<a href="${hdr.base_path}/LICENSES/${license}.txt">${license}</a>`;
+                    text = "(c) " + text;
+                    this.app.environmentLicense = text;
+                } catch (error) {
+                    this.app.environmentLicense = "N/A";
+                }
+                
+            } else {
+                this.app.environmentLicense = "N/A";
+            }
+        });
+
         merge(this.addEnvironment, inputObservables.droppedHdr)
-            .subscribe(hdrPath => {
+            .subscribe(hdr => {
+                const hdrPath = hdr.hdr_path;
                 this.app.environments[hdrPath.name] = {
                     title: hdrPath.name,
                     hdr_path: hdrPath,
@@ -280,7 +306,8 @@ const getInputObservables = (inputElement, app) => {
     observables.droppedHdr = droppedFiles.pipe(
         map(files => files.find(([path]) => path.endsWith(".hdr"))),
         filter(file => file !== undefined),
-        pluck("1")
+        pluck("1"),
+        map(file => ({hdr_path: file}))
     );
 
     const mouseMove = fromEvent(document, 'mousemove');
diff --git a/src/main.js b/src/main.js
index 8d2cd05a..f973b4b1 100644
--- a/src/main.js
+++ b/src/main.js
@@ -420,8 +420,8 @@ export default async () => {
     );
     listenForRedraw(uiModel.activeAnimations);
 
-    uiModel.hdr.subscribe((hdrFile) => {
-        resourceLoader.loadEnvironment(hdrFile).then((environment) => {
+    uiModel.hdr.subscribe((hdr) => {
+        resourceLoader.loadEnvironment(hdr.hdr_path).then((environment) => {
             state.environment = environment;
             // We need to wait until the environment is loaded to redraw
             redraw = true;
diff --git a/src/model_path_provider.js b/src/model_path_provider.js
index 3d7e78c3..f0596851 100644
--- a/src/model_path_provider.js
+++ b/src/model_path_provider.js
@@ -68,7 +68,9 @@ export function fillEnvironmentWithPaths(environmentNames, environmentsBasePath)
             index: index,
             title: title,
             hdr_path: environmentsBasePath + name + ".hdr",
-            jpg_path: environmentsBasePath + name + ".jpg"
+            jpg_path: environmentsBasePath + name + ".jpg",
+            license_path: environmentsBasePath + name + ".hdr.license",
+            base_path: environmentsBasePath
         };
     });
     return environmentNames;
diff --git a/src/ui/ui.js b/src/ui/ui.js
index 33298f56..dcf8bbda 100644
--- a/src/ui/ui.js
+++ b/src/ui/ui.js
@@ -324,7 +324,7 @@ const appCreated = createApp({
         },
         onFileChange(e) {
             const file = e.target.files[0];
-            this.addEnvironmentChanged.next(file);
+            this.addEnvironmentChanged.next({hdr_path: file});
         },
 
         toggleUI() {

From ead6619022501a1cf4778de292b2e870c1551e34 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Patrick=20H=C3=A4rtl?= <haertl@ux3d.io>
Date: Tue, 15 Oct 2024 13:18:03 +0200
Subject: [PATCH 2/2] Nicer formatting

---
 src/logic/uimodel.js | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/logic/uimodel.js b/src/logic/uimodel.js
index 9745e81f..f4c2bf9c 100644
--- a/src/logic/uimodel.js
+++ b/src/logic/uimodel.js
@@ -122,9 +122,11 @@ class UIModel
                     const license = text.split("SPDX-License-Identifier: ")[1];
                     console.log(license);
                     text = text.replace("SPDX-FileCopyrightText: ", "");
-                    text = text.replace(/SPDX-License-Identifier:(.)*/g, `, <a href="${hdr.hdr_path}">${hdr.hdr_path}</a>, License: `);
+                    text = text.replace(/SPDX-License-Identifier:(.)*/g, `, <a href="${hdr.hdr_path}">Source</a>, License: `);
                     text += `<a href="${hdr.base_path}/LICENSES/${license}.txt">${license}</a>`;
                     text = "(c) " + text;
+                    text = text.replaceAll("\n","");
+                    text = text.replaceAll(" ,", ",");
                     this.app.environmentLicense = text;
                 } catch (error) {
                     this.app.environmentLicense = "N/A";