Browse Source

Add SoundSource & SoundSource3D component creation, add autoPlay for sound sources, fix project loading issue, added AudioImporter

Josh Engebretson 10 years ago
parent
commit
f3c8e6e1d3

+ 51 - 1
Script/AtomicEditor/ui/inspector/ComponentInspector.ts

@@ -125,6 +125,11 @@ class ComponentInspector extends Atomic.UISection {
             this.addModelUI(attrsVerticalLayout, component.typeName);
         }
 
+        if (component.typeName == "SoundSource" || component.typeName == "SoundSource3D") {
+            this.addSoundSourceUI(attrsVerticalLayout, component.typeName);
+        }
+
+
         var deleteButton = new Atomic.UIButton();
         deleteButton.text = "Delete Component";
         deleteButton.fontDescription = fd;
@@ -197,7 +202,7 @@ class ComponentInspector extends Atomic.UISection {
 
     }
 
-    addModelUI(layout: Atomic.UILayout, typeName:string) {
+    addModelUI(layout: Atomic.UILayout, typeName: string) {
 
         var staticModel = <Atomic.StaticModel> this.component;
         var cacheModel = staticModel.model;
@@ -371,6 +376,51 @@ class ComponentInspector extends Atomic.UISection {
     }
 
 
+    addSoundSourceUI(layout: Atomic.UILayout, typeName: string) {
+
+        var sndSource = <Atomic.SoundSource> this.component;
+
+        var o = InspectorUtils.createAttrEditFieldWithSelectButton("Sound", layout);
+        var field = o.editField;
+        field.readOnly = true;
+        field.text = sndSource.sound ? sndSource.sound.name : "";
+
+        var select = o.selectButton;
+
+        select.onClick = () => {
+
+            EditorUI.getModelOps().showResourceSelection("Select Sound", "AudioImporter", function(asset: ToolCore.Asset) {
+
+                sndSource.sound = <Atomic.Sound> Atomic.cache.getResource("Sound", asset.path);
+                if (sndSource.sound)
+                    field.text = sndSource.sound.name;
+
+            });
+
+        }
+
+        // handle dropping of component on field
+        field.subscribeToEvent(field, "DragEnded", (ev: Atomic.DragEndedEvent) => {
+
+            if (ev.target == field) {
+
+                var importer = this.acceptAssetDrag("AudioImporter", ev);
+
+                if (importer) {
+
+                    sndSource.sound = <Atomic.Sound> Atomic.cache.getResource("Sound", importer.asset.path);
+                    if (sndSource.sound)
+                        field.text = sndSource.sound.name;
+
+                }
+            }
+
+        });
+
+
+    }
+
+
     addLightCascadeParametersUI(layout: Atomic.UILayout) {
 
         var light = <Atomic.Light> this.component;

+ 3 - 3
Script/AtomicEditor/ui/inspector/CreateComponentButton.ts

@@ -4,9 +4,9 @@ import ComponentInspector = require("./ComponentInspector");
 
 var audioCreateSource = new Atomic.UIMenuItemSource();
 
-audioCreateSource.addItem(new Atomic.UIMenuItem("SoundListener", "create component"));
-audioCreateSource.addItem(new Atomic.UIMenuItem("SoundSource", "create component"));
-audioCreateSource.addItem(new Atomic.UIMenuItem("SoundSource3D", "create component"));
+audioCreateSource.addItem(new Atomic.UIMenuItem("SoundListener", "SoundListener"));
+audioCreateSource.addItem(new Atomic.UIMenuItem("SoundSource", "SoundSource"));
+audioCreateSource.addItem(new Atomic.UIMenuItem("SoundSource3D", "SoundSource3D"));
 
 var geometryCreateSource = new Atomic.UIMenuItemSource();
 

+ 1 - 1
Script/Packages/ToolCore/ToolCore.json

@@ -4,7 +4,7 @@
 							 "Source/ToolCore/Import", "Source/ToolCore/Assets", "Source/ToolCore/License"],
 	"classes" : ["ToolEnvironment", "ToolSystem", "Project", "ProjectFile", "Platform", "PlatformMac", "PlatformWeb",
 							 "PlatformWindows", "Command", "PlayCmd", "OpenAssetImporter",
-							 "Asset", "AssetDatabase", "AssetImporter", "ModelImporter", "MaterialImporter", "AnimationImportInfo",
+							 "Asset", "AssetDatabase", "AssetImporter", "AudioImporter", "ModelImporter", "MaterialImporter", "AnimationImportInfo",
 							 "PrefabImporter", "JavascriptImporter", "TextureImporter", "LicenseSystem"],
 	"typescript_decl" : {
 

+ 2 - 0
Script/TypeScript/Atomic.d.ts

@@ -6192,6 +6192,8 @@ declare module Atomic {
       // Set whether sound source will be automatically removed from the scene node when playback stops.
       setAutoRemove(enable: boolean): void;
       // Return sound.
+      setSound(sound: Sound): void;
+      // Return sound.
       getSound(): Sound;
       // Return sound type, determines the master gain group.
       getSoundType(): string;

+ 10 - 0
Script/TypeScript/ToolCore.d.ts

@@ -305,6 +305,16 @@ declare module ToolCore {
 
    }
 
+   export class AudioImporter extends AssetImporter {
+
+      // Construct.
+      constructor(asset: Asset);
+
+      setDefaults(): void;
+      import(guid: string): boolean;
+
+   }
+
    export class JavascriptImporter extends AssetImporter {
 
       // Construct.

+ 11 - 0
Source/Atomic/Audio/SoundSource.cpp

@@ -106,6 +106,8 @@ SoundSource::SoundSource(Context* context) :
     panning_(0.0f),
     autoRemoveTimer_(0.0f),
     autoRemove_(false),
+    autoPlay_(false),
+    hasAutoPlayed_(false),
     position_(0),
     fractPosition_(0),
     timePosition_(0.0f),
@@ -139,6 +141,8 @@ void SoundSource::RegisterObject(Context* context)
     ACCESSOR_ATTRIBUTE("Is Playing", IsPlaying, SetPlayingAttr, bool, false, AM_DEFAULT);
     ATTRIBUTE("Autoremove on Stop", bool, autoRemove_, false, AM_FILE);
     ACCESSOR_ATTRIBUTE("Play Position", GetPositionAttr, SetPositionAttr, int, 0, AM_FILE);
+
+    ATTRIBUTE("Autoplay", bool, autoPlay_, false, AM_FILE);
 }
 
 void SoundSource::Play(Sound* sound)
@@ -293,6 +297,13 @@ void SoundSource::Update(float timeStep)
     if (!audio_->IsInitialized())
         MixNull(timeStep);
 
+    // check for autoPlay
+    if (autoPlay_ && sound_.NotNull() && !hasAutoPlayed_ && !context_->GetEditorContext())
+    {
+        hasAutoPlayed_ = true;
+        Play(sound_);
+    }
+
     // Free the stream if playback has stopped
     if (soundStream_ && !position_)
         StopLockless();

+ 12 - 0
Source/Atomic/Audio/SoundSource.h

@@ -75,6 +75,13 @@ public:
     /// Set new playback position.
     void SetPlayPosition(signed char* pos);
 
+    // BEGIN ATOMIC
+
+    /// Return sound.
+    void SetSound(Sound* sound) { if (sound) SetSoundAttr(GetResourceRef(sound, "")); }
+
+    // END ATOMIC
+
     /// Return sound.
     Sound* GetSound() const { return sound_; }
 
@@ -145,6 +152,11 @@ protected:
     /// Autoremove flag.
     bool autoRemove_;
 
+    // BEGIN ATOMIC
+    bool autoPlay_;
+    bool hasAutoPlayed_;
+    // END ATOMIC
+
 private:
     /// Play a sound without locking the audio mutex. Called internally.
     void PlayLockless(Sound* sound);

+ 2 - 0
Source/AtomicEditorWork/Application/AEPlayerApp.cpp

@@ -113,6 +113,8 @@ void AEPlayerApplication::Setup()
                 // This works for a local dev build, --editor-resource-paths command below is for
                 // launching from AtomicEditor (IPC)
 
+                value = AddTrailingSlash(value);
+
                 String resourcePaths = ToString("%s/Resources/CoreData;%s/Resources/PlayerData;%s/;%s/Resources;%s;%sCache",
                          ATOMIC_ROOT_SOURCE_DIR, ATOMIC_ROOT_SOURCE_DIR, value.CString(), value.CString(), value.CString(), value.CString());
 

+ 8 - 1
Source/ToolCore/Assets/Asset.cpp

@@ -7,6 +7,7 @@
 #include "../Project/Project.h"
 
 #include "AssetDatabase.h"
+#include "AudioImporter.h"
 #include "ModelImporter.h"
 #include "FolderImporter.h"
 #include "SceneImporter.h"
@@ -89,7 +90,9 @@ bool Asset::Preload()
     if (importer_.Null())
         return true;
 
-    return importer_->Preload();
+    // disabled preload for now, as this is on a background thread and causing init problems
+    return true;
+    //return importer_->Preload();
 }
 
 // load .asset
@@ -207,6 +210,10 @@ bool Asset::CreateImporter()
         {
             importer_ = new ModelImporter(context_, this);
         }
+        if (ext == ".ogg" || ext == ".wav")
+        {
+            importer_ = new AudioImporter(context_, this);
+        }
         else if (ext == ".prefab")
         {
             importer_ = new PrefabImporter(context_, this);

+ 59 - 0
Source/ToolCore/Assets/AudioImporter.cpp

@@ -0,0 +1,59 @@
+
+#include <Atomic/Resource/ResourceCache.h>
+#include <Atomic/Resource/Image.h>
+
+#include "Asset.h"
+#include "AssetDatabase.h"
+#include "AudioImporter.h"
+
+namespace ToolCore
+{
+
+AudioImporter::AudioImporter(Context* context, Asset *asset) : AssetImporter(context, asset)
+{
+
+}
+
+AudioImporter::~AudioImporter()
+{
+
+}
+
+void AudioImporter::SetDefaults()
+{
+    AssetImporter::SetDefaults();
+}
+
+bool AudioImporter::Import(const String& guid)
+{
+    AssetDatabase* db = GetSubsystem<AssetDatabase>();
+    Asset* asset = db->GetAssetByGUID(guid);
+
+    if (!asset)
+        return false;
+
+    return true;
+}
+
+bool AudioImporter::LoadSettingsInternal()
+{
+    if (!AssetImporter::LoadSettingsInternal())
+        return false;
+
+    JSONValue import = jsonRoot_.GetChild("AudioImporter", JSON_OBJECT);
+
+    return true;
+}
+
+bool AudioImporter::SaveSettingsInternal()
+{
+    if (!AssetImporter::SaveSettingsInternal())
+        return false;
+
+    JSONValue import = jsonRoot_.CreateChild("AudioImporter");
+
+    return true;
+}
+
+
+}

+ 29 - 0
Source/ToolCore/Assets/AudioImporter.h

@@ -0,0 +1,29 @@
+
+#pragma once
+
+#include "AssetImporter.h"
+
+namespace ToolCore
+{
+
+class AudioImporter : public AssetImporter
+{
+    OBJECT(AudioImporter);
+
+public:
+    /// Construct.
+    AudioImporter(Context* context, Asset* asset);
+    virtual ~AudioImporter();
+
+    virtual void SetDefaults();
+
+    bool Import(const String& guid);
+
+protected:
+
+    virtual bool LoadSettingsInternal();
+    virtual bool SaveSettingsInternal();
+
+};
+
+}

+ 13 - 2
Source/ToolCore/Project/Project.cpp

@@ -104,8 +104,19 @@ bool Project::Load(const String& fullpath)
 {
     loading_ = true;
 
-    projectPath_ = GetPath(fullpath);
-    projectFilePath_ = fullpath;
+    if (fullpath.Contains(".atomic")) {
+
+        projectPath_ = AddTrailingSlash(GetPath(fullpath));
+        projectFilePath_ = fullpath;
+
+    }
+    else
+    {
+        projectPath_ = AddTrailingSlash(fullpath);
+        projectFilePath_ = GetFileName(RemoveTrailingSlash(projectPath_) + ".atomic");
+
+    }
+
 
     SharedPtr<ProjectFile> pfile(new ProjectFile(context_));
     bool result = pfile->Load(this);