From 5d85664b4093745a9633448385d1c6331404ad9e Mon Sep 17 00:00:00 2001 From: marauder2k7 Date: Mon, 29 Aug 2022 23:40:51 +0100 Subject: [PATCH] Change GuiProfile to use Sound Asset -Added SoundAssetInspector to bypass circular issue -Added SoundAsset to GuiTypes --- Engine/source/T3D/assets/SoundAsset.cpp | 4 + Engine/source/T3D/assets/SoundAsset.h | 27 ------- .../source/T3D/assets/SoundAssetInspectors.h | 32 ++++++++ .../source/gui/buttons/guiButtonBaseCtrl.cpp | 12 +-- Engine/source/gui/core/guiTypes.cpp | 77 +++++-------------- Engine/source/gui/core/guiTypes.h | 16 ++-- Templates/BaseGame/game/data/UI/UI.tscript | 3 - .../game/data/UI/scripts/profiles.tscript | 4 +- .../data/UI/sounds/buttonClick.asset.taml | 11 ++- .../data/UI/sounds/buttonHover.asset.taml | 11 ++- 10 files changed, 91 insertions(+), 106 deletions(-) create mode 100644 Engine/source/T3D/assets/SoundAssetInspectors.h diff --git a/Engine/source/T3D/assets/SoundAsset.cpp b/Engine/source/T3D/assets/SoundAsset.cpp index b783b6858..515ecacd5 100644 --- a/Engine/source/T3D/assets/SoundAsset.cpp +++ b/Engine/source/T3D/assets/SoundAsset.cpp @@ -48,6 +48,8 @@ #include "platform/profiler.h" #include "sfx/sfxTypes.h" +#include "SoundAssetInspectors.h" + //----------------------------------------------------------------------------- IMPLEMENT_CONOBJECT(SoundAsset); @@ -225,6 +227,8 @@ bool SoundAsset::loadSound() } else {// = new SFXProfile(mProfileDesc, mSoundFile, mPreload); + if (mProfileDesc.mSourceGroup == NULL) + mProfileDesc.mSourceGroup = dynamic_cast(Sim::findObject("AudioChannelMaster")); mSFXProfile.setDescription(&mProfileDesc); mSFXProfile.setSoundFileName(mSoundPath); mSFXProfile.setPreload(mPreload); diff --git a/Engine/source/T3D/assets/SoundAsset.h b/Engine/source/T3D/assets/SoundAsset.h index 7722826d8..43e424c30 100644 --- a/Engine/source/T3D/assets/SoundAsset.h +++ b/Engine/source/T3D/assets/SoundAsset.h @@ -39,7 +39,6 @@ #include "assets/assetFieldTypes.h" #endif -#include "gui/editor/guiInspectorTypes.h" #ifndef _ASSET_PTR_H_ #include "assets/assetPtr.h" #endif @@ -145,33 +144,7 @@ protected: DefineConsoleType(TypeSoundAssetPtr, SoundAsset) DefineConsoleType(TypeSoundAssetId, String) -#ifdef TORQUE_TOOLS -//----------------------------------------------------------------------------- -// TypeAssetId GuiInspectorField Class -//----------------------------------------------------------------------------- -class GuiInspectorTypeSoundAssetPtr : public GuiInspectorTypeFileName -{ - typedef GuiInspectorTypeFileName Parent; -public: - GuiBitmapButtonCtrl* mEditButton; - - DECLARE_CONOBJECT(GuiInspectorTypeSoundAssetPtr); - static void consoleInit(); - - virtual GuiControl* constructEditControl(); - virtual bool updateRects(); -}; - -class GuiInspectorTypeSoundAssetId : public GuiInspectorTypeSoundAssetPtr -{ - typedef GuiInspectorTypeSoundAssetPtr Parent; -public: - - DECLARE_CONOBJECT(GuiInspectorTypeSoundAssetId); - static void consoleInit(); -}; -#endif #pragma region Singular Asset Macros //Singular assets diff --git a/Engine/source/T3D/assets/SoundAssetInspectors.h b/Engine/source/T3D/assets/SoundAssetInspectors.h new file mode 100644 index 000000000..23262151f --- /dev/null +++ b/Engine/source/T3D/assets/SoundAssetInspectors.h @@ -0,0 +1,32 @@ +#pragma once + +#include "Soundasset.h" + +#ifndef _GUI_INSPECTOR_TYPES_H_ +#include "gui/editor/guiInspectorTypes.h" +#endif + +#ifdef TORQUE_TOOLS +class GuiInspectorTypeSoundAssetPtr : public GuiInspectorTypeFileName +{ + typedef GuiInspectorTypeFileName Parent; +public: + + GuiBitmapButtonCtrl* mEditButton; + + DECLARE_CONOBJECT(GuiInspectorTypeSoundAssetPtr); + static void consoleInit(); + + virtual GuiControl* constructEditControl(); + virtual bool updateRects(); +}; + +class GuiInspectorTypeSoundAssetId : public GuiInspectorTypeSoundAssetPtr +{ + typedef GuiInspectorTypeSoundAssetPtr Parent; +public: + + DECLARE_CONOBJECT(GuiInspectorTypeSoundAssetId); + static void consoleInit(); +}; +#endif diff --git a/Engine/source/gui/buttons/guiButtonBaseCtrl.cpp b/Engine/source/gui/buttons/guiButtonBaseCtrl.cpp index b72f1b280..c4c3da811 100644 --- a/Engine/source/gui/buttons/guiButtonBaseCtrl.cpp +++ b/Engine/source/gui/buttons/guiButtonBaseCtrl.cpp @@ -252,8 +252,8 @@ void GuiButtonBaseCtrl::onMouseDown(const GuiEvent &event) if (mProfile->mCanKeyFocus) setFirstResponder(); - if (mProfile->mSoundButtonDown) - SFX->playOnce(mProfile->mSoundButtonDown); + if (mProfile->getSoundButtonDown()) + SFX->playOnce(mProfile->getSoundButtonDownProfile()); mMouseDownPoint = event.mousePoint; mMouseDragged = false; @@ -292,8 +292,8 @@ void GuiButtonBaseCtrl::onMouseEnter(const GuiEvent &event) } else { - if ( mActive && mProfile->mSoundButtonOver ) - SFX->playOnce(mProfile->mSoundButtonOver); + if (mProfile->getSoundButtonOver()) + SFX->playOnce(mProfile->getSoundButtonOverProfile()); mHighlighted = true; } @@ -377,8 +377,8 @@ bool GuiButtonBaseCtrl::onKeyDown(const GuiEvent &event) if ((event.keyCode == KEY_RETURN || event.keyCode == KEY_SPACE) && event.modifier == 0) { - if ( mProfile->mSoundButtonDown ) - SFX->playOnce( mProfile->mSoundButtonDown); + if (mProfile->getSoundButtonDown()) + SFX->playOnce(mProfile->getSoundButtonDownProfile()); return true; } diff --git a/Engine/source/gui/core/guiTypes.cpp b/Engine/source/gui/core/guiTypes.cpp index dd5874ee9..6a99eb5bf 100644 --- a/Engine/source/gui/core/guiTypes.cpp +++ b/Engine/source/gui/core/guiTypes.cpp @@ -218,60 +218,6 @@ bool GuiControlProfile::protectedSetBitmap( void *object, const char *index, con return false; } -const char* GuiControlProfile::protectedGetSoundButtonDown( void* object, const char* data ) -{ - GuiControlProfile* profile = reinterpret_cast< GuiControlProfile* >( object ); - - SFXTrack* track = profile->mSoundButtonDown; - if( !track ) - return ""; - - return track->getName(); -} - -bool GuiControlProfile::protectedSetSoundButtonDown( void* object, const char* index, const char* data ) -{ - GuiControlProfile* profile = reinterpret_cast< GuiControlProfile* >( object ); - - SFXTrack* track = NULL; - if( data && data[ 0] && !Sim::findObject( data, track ) ) - { - Con::errorf( "GuiControlProfile::protectedSetSoundButtonDown - no SFXTrack '%s'", data ); - return false; - } - - profile->mSoundButtonDown = track; - - return false; -} - -const char* GuiControlProfile::protectedGetSoundButtonOver( void* object, const char* data ) -{ - GuiControlProfile* profile = reinterpret_cast< GuiControlProfile* >( object ); - - SFXTrack* track = profile->mSoundButtonOver; - if( !track ) - return ""; - - return track->getName(); -} - -bool GuiControlProfile::protectedSetSoundButtonOver( void* object, const char* index, const char* data ) -{ - GuiControlProfile* profile = reinterpret_cast< GuiControlProfile* >( object ); - - SFXTrack* track = NULL; - if( data && data[ 0] && !Sim::findObject( data, track ) ) - { - Con::errorf( "GuiControlProfile::protectedSetSoundButtonOver - no SFXTrack '%s'", data ); - return false; - } - - profile->mSoundButtonOver = track; - - return false; -} - GuiControlProfile::GuiControlProfile(void) : mFillColor(255,0,255,255), mFillColorHL(255,0,255,255), @@ -293,6 +239,8 @@ GuiControlProfile::GuiControlProfile(void) : mTextOffset(0,0), mBitmapArrayRects(0) { + INIT_ASSET(SoundButtonDown); + INIT_ASSET(SoundButtonOver); mLoadCount = 0; mUseCount = 0; @@ -367,8 +315,19 @@ GuiControlProfile::GuiControlProfile(void) : mTextOffset = def->mTextOffset; // default sound - mSoundButtonDown = def->mSoundButtonDown; - mSoundButtonOver = def->mSoundButtonOver; + _setSoundButtonDown(def->getSoundButtonDown()); + if (getSoundButtonDown() != StringTable->EmptyString()) + { + if (!getSoundButtonDownProfile()) + Con::errorf(ConsoleLogEntry::General, "GuiControlProfile: Can't get default button pressed sound asset."); + } + + _setSoundButtonOver(def->getSoundButtonOver()); + if (getSoundButtonOver() != StringTable->EmptyString()) + { + if (!getSoundButtonOverProfile()) + Con::errorf(ConsoleLogEntry::General, "GuiControlProfile: Can't get default button hover sound asset."); + } //used by GuiTextCtrl mModal = def->mModal; @@ -478,9 +437,9 @@ void GuiControlProfile::initPersistFields() addField("hasBitmapArray", TypeBool, Offset(mUseBitmapArray, GuiControlProfile), "If true, 'bitmap' is an array of images." ); - addProtectedField( "soundButtonDown", TypeSFXTrackName, Offset(mSoundButtonDown, GuiControlProfile), - &GuiControlProfile::protectedSetSoundButtonDown, &GuiControlProfile::protectedGetSoundButtonDown, - "Sound to play when mouse has been pressed on control." ); + INITPERSISTFIELD_SOUNDASSET(SoundButtonDown, GuiControlProfile, "The sound button down."); + INITPERSISTFIELD_SOUNDASSET(SoundButtonOver, GuiControlProfile, "The sound button down."); + addProtectedField( "soundButtonOver", TypeSFXTrackName, Offset(mSoundButtonOver, GuiControlProfile), &GuiControlProfile::protectedSetSoundButtonOver, &GuiControlProfile::protectedGetSoundButtonOver, "Sound to play when mouse is hovering over control." ); diff --git a/Engine/source/gui/core/guiTypes.h b/Engine/source/gui/core/guiTypes.h index 37b77d57d..df3d6e9e6 100644 --- a/Engine/source/gui/core/guiTypes.h +++ b/Engine/source/gui/core/guiTypes.h @@ -36,6 +36,10 @@ #include "console/dynamicTypes.h" #endif +#ifndef SOUND_ASSET_H_ +#include "T3D/assets/SoundAsset.h" +#endif + #include "T3D/assets/ImageAsset.h" #include "gfx/gfxDevice.h" @@ -562,9 +566,11 @@ public: bool mUseBitmapArray; ///< Flag to use the bitmap array or to fallback to non-array rendering Vector mBitmapArrayRects; ///< Used for controls which use an array of bitmaps such as checkboxes - // sound members - SimObjectPtr< SFXTrack > mSoundButtonDown; ///< Sound played when the object is "down" ie a button is pushed - SimObjectPtr< SFXTrack > mSoundButtonOver; ///< Sound played when the mouse is over the object + DECLARE_SOUNDASSET(GuiControlProfile, SoundButtonDown); ///< Sound played when a button is pressed. + DECLARE_ASSET_SETGET(GuiControlProfile, SoundButtonDown); + + DECLARE_SOUNDASSET(GuiControlProfile, SoundButtonOver); ///< Sound played when a button is hovered. + DECLARE_ASSET_SETGET(GuiControlProfile, SoundButtonOver); StringTableEntry mChildrenProfileName; ///< The name of the profile to use for the children controls @@ -583,10 +589,6 @@ protected: GuiControlProfile* mChildrenProfile; ///< Profile used with children controls (such as the scroll bar on a popup menu) when defined. static bool protectedSetBitmap( void *object, const char *index, const char *data ); - static bool protectedSetSoundButtonDown( void* object, const char* index, const char* data ); - static bool protectedSetSoundButtonOver( void* object, const char* index, const char* data ); - static const char* protectedGetSoundButtonDown( void* object, const char* data ); - static const char* protectedGetSoundButtonOver( void* object, const char* data ); public: DECLARE_CONOBJECT(GuiControlProfile); diff --git a/Templates/BaseGame/game/data/UI/UI.tscript b/Templates/BaseGame/game/data/UI/UI.tscript index 9dae2fe5d..352cd91bd 100644 --- a/Templates/BaseGame/game/data/UI/UI.tscript +++ b/Templates/BaseGame/game/data/UI/UI.tscript @@ -29,9 +29,6 @@ function UI::onDestroyGameServer(%this){} function UI::initClient(%this) { //Load UI stuff - //we need to load this because some of the menu profiles use the sounds here - //%this.queueExec("./datablocks/guiSounds"); - //Profiles %this.queueExec("./scripts/profiles"); diff --git a/Templates/BaseGame/game/data/UI/scripts/profiles.tscript b/Templates/BaseGame/game/data/UI/scripts/profiles.tscript index 92a7720d1..64019e345 100644 --- a/Templates/BaseGame/game/data/UI/scripts/profiles.tscript +++ b/Templates/BaseGame/game/data/UI/scripts/profiles.tscript @@ -100,8 +100,8 @@ new GuiControlProfile( GuiMenuButtonProfile ) canKeyFocus = false; //bitmapAsset = "UI:menu_button_image"; hasBitmapArray = false; - soundButtonDown = menuButtonPressed; - soundButtonOver = menuButtonHover; + soundButtonDown = "UI:buttonClick"; + soundButtonOver = "UI:buttonHover"; category = "Core"; }; diff --git a/Templates/BaseGame/game/data/UI/sounds/buttonClick.asset.taml b/Templates/BaseGame/game/data/UI/sounds/buttonClick.asset.taml index 99773fd2d..63079c087 100644 --- a/Templates/BaseGame/game/data/UI/sounds/buttonClick.asset.taml +++ b/Templates/BaseGame/game/data/UI/sounds/buttonClick.asset.taml @@ -1 +1,10 @@ - + diff --git a/Templates/BaseGame/game/data/UI/sounds/buttonHover.asset.taml b/Templates/BaseGame/game/data/UI/sounds/buttonHover.asset.taml index 00b1af405..e270dda00 100644 --- a/Templates/BaseGame/game/data/UI/sounds/buttonHover.asset.taml +++ b/Templates/BaseGame/game/data/UI/sounds/buttonHover.asset.taml @@ -1 +1,10 @@ - +