diff --git a/Assets/Materials/UIMat_Slider.shmat b/Assets/Materials/UIMat_Slider.shmat new file mode 100644 index 00000000..ec792a29 --- /dev/null +++ b/Assets/Materials/UIMat_Slider.shmat @@ -0,0 +1,12 @@ +- VertexShader: 46580970 + FragmentShader: 48832081 + SubPass: UI + Properties: + data.color: {x: 1, y: 1, z: 1, w: 1} + data.textureIndex: 64651793 + data.alpha: 0 + data.beta: {x: 1, y: 1, z: 1} + data.sliderThreshold: 1 + data.sliderStartColor: {x: 0, y: 1, z: 0, w: 1} + data.sliderEndColor: {x: 1, y: 0, z: 0, w: 1} + data.sliderBarColor: {x: 1, y: 1, z: 1, w: 1} \ No newline at end of file diff --git a/Assets/Materials/UIMat_Slider.shmat.shmeta b/Assets/Materials/UIMat_Slider.shmat.shmeta new file mode 100644 index 00000000..6cd24549 --- /dev/null +++ b/Assets/Materials/UIMat_Slider.shmat.shmeta @@ -0,0 +1,3 @@ +Name: UIMat_Slider +ID: 128676209 +Type: 7 diff --git a/Assets/Scenes/MainMenu.shade b/Assets/Scenes/MainMenu.shade index 8d1bbcf3..c2c35c38 100644 --- a/Assets/Scenes/MainMenu.shade +++ b/Assets/Scenes/MainMenu.shade @@ -1,7 +1,7 @@ - EID: 0 - Name: Canvas + Name: MainMenu Canvas IsActive: true - NumberOfChildren: 3 + NumberOfChildren: 4 Components: Canvas Component: Canvas Width: 1920 @@ -15,7 +15,7 @@ NumberOfChildren: 0 Components: Transform Component: - Translate: {x: 0, y: 0, z: 0.5} + Translate: {x: 0, y: 5, z: 0.5} Rotate: {x: 0, y: 0, z: 0} Scale: {x: 1920, y: 1080, z: 1} IsActive: true @@ -84,6 +84,29 @@ Scripts: - Type: QuitButton Enabled: true +- EID: 7 + Name: Slider + IsActive: false + NumberOfChildren: 0 + Components: + Transform Component: + Translate: {x: 0, y: -56, z: 0} + Rotate: {x: 0, y: 0, z: 0} + Scale: {x: 500, y: 100, z: 1} + IsActive: false + Renderable Component: + Mesh: 141771688 + Material: 128676209 + IsActive: false + Slider Component: + Slider Value: 0 + IsActive: false + UI Component: + Canvas ID: 0 + Hovered: false + Clicked: false + IsActive: false + Scripts: ~ - EID: 2 Name: Light IsActive: true diff --git a/Assets/Shaders/UI_Slider_FS.glsl b/Assets/Shaders/UI_Slider_FS.glsl new file mode 100644 index 00000000..48e301cc --- /dev/null +++ b/Assets/Shaders/UI_Slider_FS.glsl @@ -0,0 +1,60 @@ +#version 450 +#extension GL_ARB_separate_shader_objects : enable +#extension GL_ARB_shading_language_420pack : enable +#extension GL_EXT_nonuniform_qualifier : require + +struct MatPropData +{ + int textureIndex; + float alpha; + float sliderThreshold; + vec4 sliderStartColor; + vec4 sliderEndColor; + vec4 sliderBarColor; +}; + +layout(location = 0) in struct +{ + vec4 vertPos; // location 0 + vec2 uv; // location = 1 + vec4 normal; // location = 2 + +} In; + +// material stuff +layout(location = 3) flat in struct +{ + int materialIndex; + uint eid; + uint lightLayerIndex; + +} In2; + +layout (set = 0, binding = 1) uniform sampler2D textures[]; // for textures (global) +layout (std430, set = 2, binding = 0) buffer MaterialProperties // For materials +{ + MatPropData data[]; +} MatProp; + +layout(location = 0) out vec4 fragColor; +layout(location = 1) out uint outEntityID; + +void main() +{ + //fragColor = texture(textures[nonuniformEXT(MatProp.data[In2.materialIndex].textureIndex)], In.uv); + if (fragColor.a < 0.01f) + { + discard; + } + if (In.uv.x > MatProp.data[In2.materialIndex].sliderThreshold) + fragColor = MatProp.data[In2.materialIndex].sliderBarColor; + else + fragColor = (1.0f - In.uv.x) * MatProp.data[In2.materialIndex].sliderStartColor + In.uv.x * MatProp.data[In2.materialIndex].sliderEndColor; + //fragColor = texture(textures[nonuniformEXT(MatProp.data[In2.materialIndex].textureIndex)], In.uv); + + + fragColor.a = MatProp.data[In2.materialIndex].alpha; + + // fragColor.a = 1.0f; + outEntityID = In2.eid; +} \ No newline at end of file diff --git a/Assets/Shaders/UI_Slider_FS.shshaderb b/Assets/Shaders/UI_Slider_FS.shshaderb new file mode 100644 index 00000000..40e321f8 Binary files /dev/null and b/Assets/Shaders/UI_Slider_FS.shshaderb differ diff --git a/Assets/Shaders/UI_Slider_FS.shshaderb.shmeta b/Assets/Shaders/UI_Slider_FS.shshaderb.shmeta new file mode 100644 index 00000000..51d3222f --- /dev/null +++ b/Assets/Shaders/UI_Slider_FS.shshaderb.shmeta @@ -0,0 +1,3 @@ +Name: UI_Slider_FS +ID: 48832081 +Type: 2 diff --git a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorInspector.cpp b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorInspector.cpp index 81882742..391e0be2 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorInspector.cpp +++ b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorInspector.cpp @@ -22,6 +22,7 @@ #include "UI/SHCanvasComponent.h" #include "UI/SHButtonComponent.h" #include "UI/SHToggleButtonComponent.h" +#include "UI/SHSliderComponent.h" #include "SHEditorComponentView.h" #include "AudioSystem/SHAudioListenerComponent.h" #include "Graphics/MiddleEnd/TextRendering/SHTextRenderableComponent.h" @@ -170,6 +171,9 @@ namespace SHADE if (auto toggleButton = SHComponentManager::GetComponent_s(eid)) { DrawComponent(toggleButton); + }if (auto slider = SHComponentManager::GetComponent_s(eid)) + { + DrawComponent(slider); } ImGui::Separator(); // Render Scripts @@ -185,6 +189,7 @@ namespace SHADE DrawAddComponentButton(eid); DrawAddComponentButton(eid); DrawAddComponentButton(eid); + DrawAddComponentButton(eid); // Components that require Transforms diff --git a/SHADE_Engine/src/Serialization/SHSerialization.cpp b/SHADE_Engine/src/Serialization/SHSerialization.cpp index febca024..f689d9b4 100644 --- a/SHADE_Engine/src/Serialization/SHSerialization.cpp +++ b/SHADE_Engine/src/Serialization/SHSerialization.cpp @@ -209,6 +209,7 @@ namespace SHADE AddComponentToComponentNode(components, eid); AddComponentToComponentNode(components, eid); AddComponentToComponentNode(components, eid); + AddComponentToComponentNode(components, eid); AddComponentToComponentNode(components, eid); AddComponentToComponentNode(components, eid); @@ -269,6 +270,7 @@ namespace SHADE AddComponentID(componentIDList, componentsNode); AddComponentID(componentIDList, componentsNode); AddComponentID(componentIDList, componentsNode); + AddComponentID(componentIDList, componentsNode); AddComponentID(componentIDList, componentsNode); AddComponentID(componentIDList, componentsNode); AddComponentID(componentIDList, componentsNode); @@ -353,6 +355,7 @@ namespace SHADE SHSerializationHelper::InitializeComponentFromNode(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode(componentsNode, eid); + SHSerializationHelper::InitializeComponentFromNode(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode(componentsNode, eid); diff --git a/SHADE_Engine/src/UI/SHSliderComponent.cpp b/SHADE_Engine/src/UI/SHSliderComponent.cpp index 56d1d89b..d25fbb6f 100644 --- a/SHADE_Engine/src/UI/SHSliderComponent.cpp +++ b/SHADE_Engine/src/UI/SHSliderComponent.cpp @@ -4,7 +4,7 @@ namespace SHADE { SHSliderComponent::SHSliderComponent() - :size(1.0f), isHovered(false), isClicked(false), value(0.0f) + :value(0.0f) { } diff --git a/SHADE_Engine/src/UI/SHSliderComponent.h b/SHADE_Engine/src/UI/SHSliderComponent.h index bdc57c7e..1eb64205 100644 --- a/SHADE_Engine/src/UI/SHSliderComponent.h +++ b/SHADE_Engine/src/UI/SHSliderComponent.h @@ -17,8 +17,6 @@ namespace SHADE SHSliderComponent(); virtual ~SHSliderComponent() = default; - SHVec2 size; - float GetValue() const noexcept; @@ -29,8 +27,7 @@ namespace SHADE friend class SHUISystem; private: - bool isHovered; - bool isClicked; + float value; diff --git a/SHADE_Engine/src/UI/SHUISystem.cpp b/SHADE_Engine/src/UI/SHUISystem.cpp index e27c00a2..a8d69377 100644 --- a/SHADE_Engine/src/UI/SHUISystem.cpp +++ b/SHADE_Engine/src/UI/SHUISystem.cpp @@ -377,6 +377,77 @@ namespace SHADE } + void SHUISystem::UpdateSliderComponent(SHSliderComponent& comp) noexcept + { + + + if (!SHComponentManager::HasComponent(comp.GetEID())) + { + return; + } + auto cameraSystem = SHSystemManager::GetSystem(); + auto uiComp = SHComponentManager::GetComponent(comp.GetEID()); + //auto canvasComp = SHComponentManager::GetComponent_s(uiComp->canvasID); + + float tempValue = comp.GetValue(); + + CheckButtonHoveredOrClicked(*uiComp); + + if (uiComp->GetIsClicked() == true) + { + SHVec4 topExtent4 = SHMatrix::Translate(-uiComp->size.x * 0.5f, uiComp->size.y * 0.5f, 0.0f) * uiComp->GetMatrix() * SHVec4(0.0f, 0.0f, 0.0f, 1.0f); + SHVec4 btmExtent4 = SHMatrix::Translate(uiComp->size.x * 0.5f, -uiComp->size.y * 0.5f, 0.0f) * uiComp->GetMatrix() * SHVec4(0.0f, 0.0f, 0.0f, 1.0f); + + + SHVec2 topExtent{ topExtent4.x,topExtent4.y }; + SHVec2 btmExtent{ btmExtent4.x,btmExtent4.y }; + auto cameraSystem = SHSystemManager::GetSystem(); + SHVec2 mousePos; + SHVec2 windowSize; +#ifdef SHEDITOR + windowSize = SHEditorWindowManager::GetEditorWindow()->beginContentRegionAvailable; + mousePos = SHEditorWindowManager::GetEditorWindow()->viewportMousePos; + mousePos /= windowSize; +#else + int x, y; + SHInputManager::GetMouseScreenPosition(&x, &y); + mousePos.x = x; + mousePos.y = y; + auto ws = SHSystemManager::GetSystem()->GetWindow()->GetWindowSize(); + windowSize = { static_cast(ws.first), static_cast(ws.second) }; + mousePos /= windowSize; +#endif + + SHVec2 camSize{ cameraSystem->GetCameraWidthHeight(0) }; + //SHLOG_INFO("TopExtent: {}, {}", topExtent.x, topExtent.y) + + topExtent = CanvasToScreenPoint(topExtent, true); + btmExtent = CanvasToScreenPoint(btmExtent, true); + + + comp.value = (mousePos.x - topExtent.x) / (btmExtent.x - topExtent.x); + + if (comp.GetValue() > 1.0f) + comp.value = 1.0f; + if (comp.GetValue() < 0.0f) + comp.value = 0.0f; + } + + if (comp.GetValue() != tempValue) + { + //Set shader value. + + auto renderable = SHComponentManager::GetComponent_s(comp.GetEID()); + //auto texture = SHResourceManager::Get(comp.GetDefaultTexture()); + auto material = renderable->GetModifiableMaterial(); + + material->SetProperty("data.sliderThreshold", comp.GetValue()); + + } + + } + + void SHUISystem::UpdateButtonsRoutine::Execute(double dt) noexcept { SHUISystem* system = (SHUISystem*)GetSystem(); @@ -395,6 +466,13 @@ namespace SHADE if (SHSceneManager::CheckNodeAndComponentsActive(comp.GetEID())) system->UpdateToggleButtonComponent(comp); } + + auto& sliderDense = SHComponentManager::GetDense(); + for (auto& comp : sliderDense) + { + if (SHSceneManager::CheckNodeAndComponentsActive(comp.GetEID())) + system->UpdateSliderComponent(comp); + } } SHVec2 SHUISystem::CanvasToScreenPoint(SHVec2& const canvasPoint, bool normalized) noexcept diff --git a/SHADE_Engine/src/UI/SHUISystem.h b/SHADE_Engine/src/UI/SHUISystem.h index f3f7847e..a1fde5db 100644 --- a/SHADE_Engine/src/UI/SHUISystem.h +++ b/SHADE_Engine/src/UI/SHUISystem.h @@ -74,7 +74,7 @@ namespace SHADE void UpdateButtonComponent(SHButtonComponent& comp) noexcept; void UpdateToggleButtonComponent(SHToggleButtonComponent& comp) noexcept; void UpdateCanvasComponent(SHCanvasComponent& comp) noexcept; - + void UpdateSliderComponent(SHSliderComponent& comp) noexcept; //returns true on button release. bool CheckButtonHoveredOrClicked(SHUIComponent& comp) noexcept;