Bladeren bron

Adding first-pass code for a partially functioning settings screen. (#345)

Signed-off-by: Mike Balfour <[email protected]>
Mike Balfour 2 jaren geleden
bovenliggende
commit
7a4f43c6ba

+ 230 - 0
Gem/Code/Source/Components/UI/UiSettingsComponent.cpp

@@ -0,0 +1,230 @@
+/*
+ * Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
+ *
+ * SPDX-License-Identifier: Apache-2.0 OR MIT
+ *
+ */
+
+#include <Atom/RHI/Factory.h>
+#include <AzCore/Console/IConsole.h>
+#include <AzCore/Interface/Interface.h>
+#include <AzCore/Serialization/EditContext.h>
+#include <IAudioSystem.h>
+#include <LyShine/Bus/UiButtonBus.h>
+#include <LyShine/Bus/UiElementBus.h>
+#include <LyShine/Bus/UiTextBus.h>
+#include <Source/Components/UI/UiSettingsComponent.h>
+
+namespace MultiplayerSample
+{
+    void MpsSettings::Reflect(AZ::ReflectContext* context)
+    {
+        if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
+        {
+            serializeContext->Class<MpsSettings>()
+                ->Version(0)
+                ->Field("GraphicsApi", &MpsSettings::m_atomApiType)
+                ->Field("MasterVolume", &MpsSettings::m_masterVolume)
+                ->Field("TextureQuality", &MpsSettings::m_streamingImageMipBias)
+                ;
+        }
+    }
+
+    void UiToggle::Reflect(AZ::ReflectContext* context)
+    {
+        if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
+        {
+            serializeContext->Class<UiToggle>()
+                ->Version(0)
+                ->Field("Label", &UiToggle::m_labelEntity)
+                ->Field("LeftButton", &UiToggle::m_leftButtonEntity)
+                ->Field("RightButton", &UiToggle::m_rightButtonEntity)
+                ;
+
+
+            if (AZ::EditContext* editContext = serializeContext->GetEditContext())
+            {
+                editContext->Class<UiToggle>("Ui Toggle", "Manages the user settings")
+                    ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
+                    ->DataElement(AZ::Edit::UIHandlers::Default, &UiToggle::m_labelEntity, "Label", "The toggle's label entity.")
+                    ->DataElement(AZ::Edit::UIHandlers::Default, &UiToggle::m_leftButtonEntity, "Left Button", "The toggle's left button entity.")
+                    ->DataElement(AZ::Edit::UIHandlers::Default, &UiToggle::m_rightButtonEntity, "Right Button", "The toggle's right button entity.")
+                    ;
+            }
+        }
+    }
+
+    void UiSettingsComponent::Reflect(AZ::ReflectContext* context)
+    {
+        MpsSettings::Reflect(context);
+        UiToggle::Reflect(context);
+
+        if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
+        {
+            serializeContext->Class<UiSettingsComponent, AZ::Component>()
+                ->Version(0)
+                ->Field("GraphicsApi", &UiSettingsComponent::m_graphicsApiToggle)
+                ->Field("TextureQuality", &UiSettingsComponent::m_textureQualityToggle)
+                ->Field("MasterVolume", &UiSettingsComponent::m_masterVolumeToggle)
+                ;
+
+            if (AZ::EditContext* editContext = serializeContext->GetEditContext())
+            {
+                editContext->Class<UiSettingsComponent>("Ui Settings", "Manages the user settings")
+                    ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
+                    ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("CanvasUI"))
+                    ->DataElement(AZ::Edit::UIHandlers::Default, &UiSettingsComponent::m_graphicsApiToggle, "Graphics Api", "The Graphics Api toggle elements.")
+                    ->DataElement(AZ::Edit::UIHandlers::Default, &UiSettingsComponent::m_textureQualityToggle, "Texture Quality", "The Texture Quality toggle elements.")
+                    ->DataElement(AZ::Edit::UIHandlers::Default, &UiSettingsComponent::m_masterVolumeToggle, "Master Volume", "The Master Volume toggle elements.")
+                    ;
+            }
+        }
+    }
+
+    void UiSettingsComponent::Activate()
+    {
+        // Initialize our user settings 
+
+        // Initialize the current streaming image mip bias setting.
+        if (AZ::IConsole* console = AZ::Interface<AZ::IConsole>::Get(); console)
+        {
+            int16_t mipBias = 0;
+            console->GetCvarValue("r_streamingImageMipBias", mipBias);
+            m_settings.m_streamingImageMipBias = aznumeric_cast<uint8_t>(mipBias);
+        }
+
+        // Initialize the graphics API type
+        m_settings.m_atomApiType = AZ::RHI::Factory::Get().GetAPIUniqueIndex();
+
+        // There's currently no way to initialize the master volume, this doesn't seem to be fetchable anywhere.
+
+        // Initialize the toggles to the current values
+        OnGraphicsApiToggle(ToggleDirection::None);
+        OnTextureQualityToggle(ToggleDirection::None);
+        OnMasterVolumeToggle(ToggleDirection::None);
+
+        // Start listening for button presses
+        UiButtonBus::Event(m_graphicsApiToggle.m_leftButtonEntity, &UiButtonInterface::SetOnClickCallback,
+            [this]([[maybe_unused]] AZ::EntityId buttonEntityId, [[maybe_unused]] AZ::Vector2 position) 
+            { 
+                OnGraphicsApiToggle(ToggleDirection::Left); 
+            });
+        UiButtonBus::Event(m_graphicsApiToggle.m_rightButtonEntity, &UiButtonInterface::SetOnClickCallback,
+            [this]([[maybe_unused]] AZ::EntityId buttonEntityId, [[maybe_unused]] AZ::Vector2 position)
+            {
+                OnGraphicsApiToggle(ToggleDirection::Right);
+            });
+        UiButtonBus::Event(m_textureQualityToggle.m_leftButtonEntity, &UiButtonInterface::SetOnClickCallback,
+            [this]([[maybe_unused]] AZ::EntityId buttonEntityId, [[maybe_unused]] AZ::Vector2 position)
+            {
+                OnTextureQualityToggle(ToggleDirection::Left);
+            });
+        UiButtonBus::Event(m_textureQualityToggle.m_rightButtonEntity, &UiButtonInterface::SetOnClickCallback,
+            [this]([[maybe_unused]] AZ::EntityId buttonEntityId, [[maybe_unused]] AZ::Vector2 position)
+            {
+                OnTextureQualityToggle(ToggleDirection::Right);
+            });
+        UiButtonBus::Event(m_masterVolumeToggle.m_leftButtonEntity, &UiButtonInterface::SetOnClickCallback,
+            [this]([[maybe_unused]] AZ::EntityId buttonEntityId, [[maybe_unused]] AZ::Vector2 position)
+            {
+                OnMasterVolumeToggle(ToggleDirection::Left);
+            });
+        UiButtonBus::Event(m_masterVolumeToggle.m_rightButtonEntity, &UiButtonInterface::SetOnClickCallback,
+            [this]([[maybe_unused]] AZ::EntityId buttonEntityId, [[maybe_unused]] AZ::Vector2 position)
+            {
+                OnMasterVolumeToggle(ToggleDirection::Right);
+            });
+    }
+
+    void UiSettingsComponent::Deactivate()
+    {
+    }
+
+    void UiSettingsComponent::OnGraphicsApiToggle(ToggleDirection toggleDirection)
+    {
+        // This list is expected to match the values in AZ::RHI::ApiIndex.
+        const char* labels[] =
+        {
+            "Null",
+            "DirectX 12",
+            "Vulkan",
+            "Metal"
+        };
+
+        const size_t NumLabels = AZ_ARRAY_SIZE(labels);
+
+        if (toggleDirection != ToggleDirection::None)
+        {
+            m_settings.m_atomApiType = (toggleDirection == ToggleDirection::Right)
+                ? (m_settings.m_atomApiType + 1) % NumLabels
+                : (m_settings.m_atomApiType + (NumLabels - 1)) % NumLabels
+                ;
+        }
+
+        UiTextBus::Event(m_graphicsApiToggle.m_labelEntity, &UiTextInterface::SetText, labels[m_settings.m_atomApiType]);
+    }
+
+    void UiSettingsComponent::OnTextureQualityToggle(ToggleDirection toggleDirection)
+    {
+        const char* labels[] =
+        {
+            "Ultra (4K)",
+            "High (2K)",
+            "Medium (1K)",
+            "Low (512)",
+            "Very Low (256)",
+            "Extremely Low (128)",
+            "Rock Bottom (64)"
+        };
+
+        const size_t NumLabels = AZ_ARRAY_SIZE(labels);
+
+        if (toggleDirection != ToggleDirection::None)
+        {
+            // As we go from left to right on our settings, we want our textureQuality number to go from 6 down to 0
+            // because smaller mip bias numbers mean higher-resolution textures.
+            m_settings.m_streamingImageMipBias = (toggleDirection == ToggleDirection::Right)
+                ? (m_settings.m_streamingImageMipBias + (NumLabels - 1)) % NumLabels
+                : (m_settings.m_streamingImageMipBias + 1) % NumLabels
+                ;
+        }
+
+        AZ::IConsole* console = AZ::Interface<AZ::IConsole>::Get();
+        if (console)
+        {
+            AZ::CVarFixedString commandString = AZ::CVarFixedString::format("r_streamingImageMipBias %" PRId16, m_settings.m_streamingImageMipBias);
+            console->PerformCommand(commandString.c_str());
+        }
+
+        UiTextBus::Event(m_textureQualityToggle.m_labelEntity, &UiTextInterface::SetText, labels[m_settings.m_streamingImageMipBias]);
+    }
+
+    void UiSettingsComponent::OnMasterVolumeToggle(ToggleDirection toggleDirection)
+    {
+        if (toggleDirection != ToggleDirection::None)
+        {
+            m_settings.m_masterVolume = (toggleDirection == ToggleDirection::Right)
+                ? (m_settings.m_masterVolume + 10) % 110
+                : (m_settings.m_masterVolume + 100) % 110
+                ;
+        }
+
+        auto audioSystem = AZ::Interface<Audio::IAudioSystem>::Get();
+        if (audioSystem)
+        {
+            Audio::TAudioObjectID rtpcId = audioSystem->GetAudioRtpcID("Volume_Master");
+
+            if (rtpcId != INVALID_AUDIO_CONTROL_ID)
+            {
+                Audio::ObjectRequest::SetParameterValue setParameter;
+                setParameter.m_audioObjectId = INVALID_AUDIO_OBJECT_ID;
+                setParameter.m_parameterId = rtpcId;
+                setParameter.m_value = m_settings.m_masterVolume / 100.0f;
+                AZ::Interface<Audio::IAudioSystem>::Get()->PushRequest(AZStd::move(setParameter));
+            }
+        }
+
+        UiTextBus::Event(m_masterVolumeToggle.m_labelEntity, &UiTextInterface::SetText, AZStd::string::format("%d", m_settings.m_masterVolume));
+    }
+
+}

+ 70 - 0
Gem/Code/Source/Components/UI/UiSettingsComponent.h

@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
+ *
+ * SPDX-License-Identifier: Apache-2.0 OR MIT
+ *
+ */
+
+#pragma once
+
+
+#include <AzCore/Component/Component.h>
+
+namespace MultiplayerSample
+{
+    //! These are all of the user settings that MPS supports.
+    struct MpsSettings
+    {
+        AZ_TYPE_INFO(MpsSettings, "{1E545ABF-6650-41D8-AC69-9C50BB5561F0}");
+        static void Reflect(AZ::ReflectContext* context);
+
+        //! The API type that Atom should use at startup. (This value comes from AZ::RHI::APIIndex)
+        uint32_t m_atomApiType = 0;
+
+        //! The master audio volume (0 - 100). 0 is silent, 100 is max volume.
+        uint8_t m_masterVolume = 100;
+
+        //! The streaming image texture mip bias (0 - N). This affects the max mipmap level that will be loaded for streaming images.
+        //! This doesn't affect other types of images like UI or VFX.
+        uint8_t m_streamingImageMipBias = 1;
+    };
+
+    struct UiToggle
+    {
+        AZ_TYPE_INFO(UiToggle, "{60AD7DDE-1730-41D8-BB82-630FF8008370}");
+        static void Reflect(AZ::ReflectContext* context);
+
+        AZ::EntityId m_labelEntity;
+        AZ::EntityId m_leftButtonEntity;
+        AZ::EntityId m_rightButtonEntity;
+    };
+
+    class UiSettingsComponent
+        : public AZ::Component
+    {
+    public:
+        AZ_COMPONENT(UiSettingsComponent, "{6F0F5495-E766-444C-808E-4EB91AD891D6}");
+
+        static void Reflect(AZ::ReflectContext* context);
+
+        void Activate() override;
+        void Deactivate() override;
+    private:
+        enum class ToggleDirection
+        {
+            None,
+            Left,
+            Right
+        };
+
+        void OnGraphicsApiToggle(ToggleDirection toggleDirection);
+        void OnTextureQualityToggle(ToggleDirection toggleDirection);
+        void OnMasterVolumeToggle(ToggleDirection toggleDirection);
+
+        UiToggle m_graphicsApiToggle;
+        UiToggle m_textureQualityToggle;
+        UiToggle m_masterVolumeToggle;
+
+        MpsSettings m_settings;
+    };
+}

+ 2 - 0
Gem/Code/Source/MultiplayerSampleModule.cpp

@@ -20,6 +20,7 @@
     #include <Components/UI/HUDComponent.h>
     #include <Components/UI/UiMatchPlayerCoinCountsComponent.h>
     #include <Components/UI/UiRestBetweenRoundsComponent.h>
+    #include <Components/UI/UiSettingsComponent.h>
     #include <Components/UI/UiStartMenuComponent.h>
 #endif
 
@@ -53,6 +54,7 @@ namespace MultiplayerSample
                     UiPlayerArmorComponent::CreateDescriptor(),
                     UiMatchPlayerCoinCountsComponent::CreateDescriptor(),
                     UiRestBetweenRoundsComponent::CreateDescriptor(),
+                    UiSettingsComponent::CreateDescriptor(),
                     UiStartMenuComponent::CreateDescriptor(),
                 #endif
             });

+ 2 - 0
Gem/Code/multiplayersample_client_files.cmake

@@ -10,6 +10,8 @@ set(FILES
 
     Source/Components/UI/UiGameOverComponent.cpp
     Source/Components/UI/UiGameOverComponent.h
+    Source/Components/UI/UiSettingsComponent.cpp
+    Source/Components/UI/UiSettingsComponent.h
     Source/Components/UI/HUDComponent.cpp
     Source/Components/UI/HUDComponent.h
     Source/Components/UI/UiMatchPlayerCoinCountsComponent.cpp

+ 1 - 1
Registry/multiplayersample.setreg

@@ -17,7 +17,7 @@
 			"RPI": {
 				"Initialization": {
 					"ImageSystemDescriptor": {
-						"SystemStreamingImagePoolSize": 750000000,
+						"SystemStreamingImagePoolSize": 0,
 						"SystemAttachmentImagePoolSize": 0,
                         "SystemStreamingImagePoolMipBias": 1
 					}

File diff suppressed because it is too large
+ 284 - 212
UICanvases/Settings.uicanvas


+ 18 - 30
scriptcanvas/SettingsScreen.scriptcanvas

@@ -5,7 +5,7 @@
     "ClassData": {
         "m_scriptCanvas": {
             "Id": {
-                "id": 395297430607332
+                "id": 41396755622365
             },
             "Name": "Script Canvas Graph",
             "Components": {
@@ -16,7 +16,7 @@
                         "m_nodes": [
                             {
                                 "Id": {
-                                    "id": 395301725574628
+                                    "id": 41401050589661
                                 },
                                 "Name": "EBusEventHandler",
                                 "Components": {
@@ -257,7 +257,7 @@
                             },
                             {
                                 "Id": {
-                                    "id": 395306020541924
+                                    "id": 41405345556957
                                 },
                                 "Name": "EBusEventHandler",
                                 "Components": {
@@ -422,7 +422,7 @@
                             },
                             {
                                 "Id": {
-                                    "id": 395310315509220
+                                    "id": 41409640524253
                                 },
                                 "Name": "SendScriptEvent",
                                 "Components": {
@@ -521,7 +521,7 @@
                         "m_connections": [
                             {
                                 "Id": {
-                                    "id": 395318905443812
+                                    "id": 41413935491549
                                 },
                                 "Name": "srcEndpoint=(EntityBus Handler: ExecutionSlot:OnEntityActivated), destEndpoint=(UiButtonNotificationBus Handler: Connect)",
                                 "Components": {
@@ -530,7 +530,7 @@
                                         "Id": 14321063264493109140,
                                         "sourceEndpoint": {
                                             "nodeId": {
-                                                "id": 395301725574628
+                                                "id": 41401050589661
                                             },
                                             "slotId": {
                                                 "m_id": "{74C7529D-94A1-434D-83E2-CC1BE55F6697}"
@@ -538,7 +538,7 @@
                                         },
                                         "targetEndpoint": {
                                             "nodeId": {
-                                                "id": 395306020541924
+                                                "id": 41405345556957
                                             },
                                             "slotId": {
                                                 "m_id": "{6447E883-AE5D-4099-BF0C-E410581D83C0}"
@@ -549,7 +549,7 @@
                             },
                             {
                                 "Id": {
-                                    "id": 399738426791396
+                                    "id": 41418230458845
                                 },
                                 "Name": "srcEndpoint=(UiButtonNotificationBus Handler: ExecutionSlot:OnButtonClick), destEndpoint=(Send Script Event: In)",
                                 "Components": {
@@ -558,7 +558,7 @@
                                         "Id": 14650327928600045226,
                                         "sourceEndpoint": {
                                             "nodeId": {
-                                                "id": 395306020541924
+                                                "id": 41405345556957
                                             },
                                             "slotId": {
                                                 "m_id": "{F5A4728B-04AB-4BCB-9227-0AC4620D6952}"
@@ -566,7 +566,7 @@
                                         },
                                         "targetEndpoint": {
                                             "nodeId": {
-                                                "id": 395310315509220
+                                                "id": 41409640524253
                                             },
                                             "slotId": {
                                                 "m_id": "{AB7A3D11-7CDD-40DA-83F8-7227E43C4F7C}"
@@ -579,37 +579,25 @@
                         "m_scriptEventAssets": [
                             [
                                 {
-                                    "id": 395310315509220
+                                    "id": 41409640524253
                                 },
                                 {}
                             ],
                             [
                                 {
-                                    "id": 395310315509220
+                                    "id": 41409640524253
                                 },
                                 {}
                             ],
                             [
                                 {
-                                    "id": 395310315509220
+                                    "id": 41409640524253
                                 },
                                 {}
                             ],
                             [
                                 {
-                                    "id": 395314610476516
-                                },
-                                {}
-                            ],
-                            [
-                                {
-                                    "id": 395310315509220
-                                },
-                                {}
-                            ],
-                            [
-                                {
-                                    "id": 395314610476516
+                                    "id": 41409640524253
                                 },
                                 {}
                             ]
@@ -623,7 +611,7 @@
                     "GraphCanvasData": [
                         {
                             "Key": {
-                                "id": 395297430607332
+                                "id": 41396755622365
                             },
                             "Value": {
                                 "ComponentData": {
@@ -640,7 +628,7 @@
                         },
                         {
                             "Key": {
-                                "id": 395301725574628
+                                "id": 41401050589661
                             },
                             "Value": {
                                 "ComponentData": {
@@ -674,7 +662,7 @@
                         },
                         {
                             "Key": {
-                                "id": 395306020541924
+                                "id": 41405345556957
                             },
                             "Value": {
                                 "ComponentData": {
@@ -708,7 +696,7 @@
                         },
                         {
                             "Key": {
-                                "id": 395310315509220
+                                "id": 41409640524253
                             },
                             "Value": {
                                 "ComponentData": {

Some files were not shown because too many files changed in this diff