From a41354f2cedf88697a94b3cb2e4668c693688712 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Mon, 16 Jan 2023 14:35:16 +0800 Subject: [PATCH 1/3] Added changing texture of buttons --- Assets/Application.SHConfig | 2 +- Assets/Scenes/UI_Test.shade | 2 +- SHADE_Engine/src/UI/SHButtonComponent.h | 4 +- SHADE_Engine/src/UI/SHToggleButtonComponent.h | 3 + SHADE_Engine/src/UI/SHUISystem.cpp | 120 ++++++++++++------ 5 files changed, 89 insertions(+), 42 deletions(-) diff --git a/Assets/Application.SHConfig b/Assets/Application.SHConfig index 5673556d..ee5e42a8 100644 --- a/Assets/Application.SHConfig +++ b/Assets/Application.SHConfig @@ -1,4 +1,4 @@ Start in Fullscreen: false -Starting Scene ID: 97158628 +Starting Scene ID: 87244611 Window Size: {x: 1920, y: 1080} Window Title: SHADE Engine \ No newline at end of file diff --git a/Assets/Scenes/UI_Test.shade b/Assets/Scenes/UI_Test.shade index d9ce5026..0026a48b 100644 --- a/Assets/Scenes/UI_Test.shade +++ b/Assets/Scenes/UI_Test.shade @@ -14,7 +14,7 @@ NumberOfChildren: 0 Components: Transform Component: - Translate: {x: -3.5999999, y: 3.0999999, z: 0} + Translate: {x: 0, y: 0, z: 0} Rotate: {x: 0, y: 0, z: 0} Scale: {x: 1, y: 1, z: 1} IsActive: true diff --git a/SHADE_Engine/src/UI/SHButtonComponent.h b/SHADE_Engine/src/UI/SHButtonComponent.h index be6ada3e..3aac09e6 100644 --- a/SHADE_Engine/src/UI/SHButtonComponent.h +++ b/SHADE_Engine/src/UI/SHButtonComponent.h @@ -32,8 +32,10 @@ namespace SHADE friend class SHUISystem; private: - + //Set to true when mouse is hovering over the button. bool isHovered; + //This is set to true when the mouse clicks down, and set back to false when mouse releases. + //The event for the button click will be broadcasted when mouse release. bool isClicked; AssetID defaultTexture; AssetID hoveredTexture; diff --git a/SHADE_Engine/src/UI/SHToggleButtonComponent.h b/SHADE_Engine/src/UI/SHToggleButtonComponent.h index 3217c892..2c77f3ba 100644 --- a/SHADE_Engine/src/UI/SHToggleButtonComponent.h +++ b/SHADE_Engine/src/UI/SHToggleButtonComponent.h @@ -33,7 +33,10 @@ namespace SHADE friend class SHUISystem; private: + //Set to true when mouse is hovering over the button. bool isHovered; + //This is set to true when the mouse clicks down, and set back to false when mouse releases. + //The event for the button click will be broadcasted when mouse release. bool isClicked; bool value; AssetID defaultTexture; diff --git a/SHADE_Engine/src/UI/SHUISystem.cpp b/SHADE_Engine/src/UI/SHUISystem.cpp index bb98939b..84f6a21c 100644 --- a/SHADE_Engine/src/UI/SHUISystem.cpp +++ b/SHADE_Engine/src/UI/SHUISystem.cpp @@ -9,6 +9,7 @@ #include "Editor/EditorWindow/ViewportWindow/SHEditorViewport.h" #include "Editor/SHEditor.h" #include "Resource/SHResourceManager.h" +#include "Assets/SHAssetManager.h" #include "Input/SHInputManager.h" #include "SHUIComponent.h" #include "SHButtonComponent.h" @@ -168,9 +169,9 @@ namespace SHADE windowSize = SHEditorWindowManager::GetEditorWindow()->beginContentRegionAvailable; mousePos = SHEditorWindowManager::GetEditorWindow()->viewportMousePos; //mousePos.y = windowSize.y - mousePos.y; - SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y) + //SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y) mousePos /= windowSize; - SHLOG_INFO("mouse pos normalized: {}, {}", mousePos.x, mousePos.y) + //SHLOG_INFO("mouse pos normalized: {}, {}", mousePos.x, mousePos.y) @@ -193,31 +194,25 @@ namespace SHADE //SHLOG_INFO("TopExtent: {}, {} Btm Extent: {}, {}", topExtent.x, topExtent.y, btmExtent.x, btmExtent.y) - comp.isClicked = false; + //comp.isClicked = false; if (mousePos.x >= topExtent.x && mousePos.x <= btmExtent.x && mousePos.y >= topExtent.y && mousePos.y <= btmExtent.y) { comp.isHovered = true; -#ifdef SHEDITOR - if (SHSystemManager::GetSystem()->editorState == SHEditor::State::PLAY) - { - if (SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) + #ifdef SHEDITOR + //if (SHSystemManager::GetSystem()->editorState == SHEditor::State::PLAY) + { + if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::LMB)) + { + comp.isClicked = true; + } + } + #else + if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::LMB)) { comp.isClicked = true; - SHButtonClickEvent clickEvent; - clickEvent.EID = comp.GetEID(); - SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT); } - } -#else - if (SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) - { - comp.isClicked = true; - SHButtonClickEvent clickEvent; - clickEvent.EID = comp.GetEID(); - SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT); - } -#endif + #endif //SHLOG_INFO("HOVERED") } @@ -226,15 +221,46 @@ namespace SHADE comp.isHovered = false; //SHLOG_INFO("NOT HOVERED") } - + if (comp.isClicked && SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) + { + comp.isClicked = false; + SHButtonClickEvent clickEvent; + clickEvent.EID = comp.GetEID(); + SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT); + } if (SHComponentManager::HasComponent(comp.GetEID())) { - /*auto renderable = SHComponentManager::GetComponent_s(comp.GetEID()); + auto renderable = SHComponentManager::GetComponent_s(comp.GetEID()); auto texture = SHResourceManager::Get(comp.GetDefaultTexture()); auto material = renderable->GetModifiableMaterial(); - material->SetProperty("texture", comp.GetDefaultTexture());*/ + if(!comp.isHovered && !comp.isClicked) + if (SHAssetManager::GetType(comp.GetDefaultTexture()) == AssetType::TEXTURE) + { + material->SetProperty("data.textureIndex", comp.GetDefaultTexture()); + //SHLOG_INFO("SETTING DEFAULT TEXTURE") + } + + if (comp.isHovered) + { + if (SHAssetManager::GetType(comp.GetHoveredTexture()) == AssetType::TEXTURE) + { + material->SetProperty("data.textureIndex", comp.GetHoveredTexture()); + //SHLOG_INFO("SETTING HOVERED TEXTURE") + } + } + else + { + if (SHAssetManager::GetType(comp.GetClickedTexture()) == AssetType::TEXTURE) + { + material->SetProperty("data.textureIndex", comp.GetClickedTexture()); + SHLOG_INFO("SETTING CLICKED TEXTURE") + } + } + + + } } @@ -262,9 +288,9 @@ namespace SHADE windowSize = SHEditorWindowManager::GetEditorWindow()->beginContentRegionAvailable; mousePos = SHEditorWindowManager::GetEditorWindow()->viewportMousePos; //mousePos.y = windowSize.y - mousePos.y; - SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y) + //SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y) mousePos /= windowSize; - SHLOG_INFO("mouse pos normalized: {}, {}", mousePos.x, mousePos.y) + //SHLOG_INFO("mouse pos normalized: {}, {}", mousePos.x, mousePos.y) @@ -296,25 +322,15 @@ namespace SHADE #ifdef SHEDITOR if (SHSystemManager::GetSystem()->editorState == SHEditor::State::PLAY) { - if (SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) + if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::LMB)) { comp.isClicked = true; - comp.value = !comp.value; - SHButtonClickEvent clickEvent; - clickEvent.EID = comp.GetEID(); - clickEvent.value = comp.value; - SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT); } } #else - if (SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) + if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::LMB)) { comp.isClicked = true; - comp.value = !comp.value; - SHButtonClickEvent clickEvent; - clickEvent.EID = comp.GetEID(); - clickEvent.value = comp.value; - SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT); } #endif } @@ -324,13 +340,39 @@ namespace SHADE } + if (comp.isClicked && SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) + { + comp.isClicked = false; + comp.value = !comp.value; + SHButtonClickEvent clickEvent; + clickEvent.EID = comp.GetEID(); + clickEvent.value = comp.value; + SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT); + } + if (SHComponentManager::HasComponent(comp.GetEID())) { - /*auto renderable = SHComponentManager::GetComponent_s(comp.GetEID()); + auto renderable = SHComponentManager::GetComponent_s(comp.GetEID()); auto texture = SHResourceManager::Get(comp.GetDefaultTexture()); auto material = renderable->GetModifiableMaterial(); - material->SetProperty("texture", comp.GetDefaultTexture());*/ + if (comp.GetValue() == false) + { + if (SHAssetManager::GetType(comp.GetDefaultTexture()) == AssetType::TEXTURE) + { + material->SetProperty("data.textureIndex", comp.GetDefaultTexture()); + //SHLOG_INFO("SETTING DEFAULT TEXTURE") + } + } + else + { + if (SHAssetManager::GetType(comp.GetToggledTexture()) == AssetType::TEXTURE) + { + material->SetProperty("data.textureIndex", comp.GetToggledTexture()); + //SHLOG_INFO("SETTING DEFAULT TEXTURE") + } + } + } } From 1e351366afccf40c5723f087ad2b4fd4fd7be9ec Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Mon, 16 Jan 2023 14:44:20 +0800 Subject: [PATCH 2/3] fix clicked texture to take priority over hovered texture --- SHADE_Engine/src/UI/SHUISystem.cpp | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/SHADE_Engine/src/UI/SHUISystem.cpp b/SHADE_Engine/src/UI/SHUISystem.cpp index 84f6a21c..11808038 100644 --- a/SHADE_Engine/src/UI/SHUISystem.cpp +++ b/SHADE_Engine/src/UI/SHUISystem.cpp @@ -232,7 +232,7 @@ namespace SHADE if (SHComponentManager::HasComponent(comp.GetEID())) { auto renderable = SHComponentManager::GetComponent_s(comp.GetEID()); - auto texture = SHResourceManager::Get(comp.GetDefaultTexture()); + //auto texture = SHResourceManager::Get(comp.GetDefaultTexture()); auto material = renderable->GetModifiableMaterial(); if(!comp.isHovered && !comp.isClicked) @@ -240,9 +240,16 @@ namespace SHADE { material->SetProperty("data.textureIndex", comp.GetDefaultTexture()); //SHLOG_INFO("SETTING DEFAULT TEXTURE") + } + else if (comp.isClicked) + { + if (SHAssetManager::GetType(comp.GetClickedTexture()) == AssetType::TEXTURE) + { + material->SetProperty("data.textureIndex", comp.GetClickedTexture()); + //SHLOG_INFO("SETTING CLICKED TEXTURE") + } } - - if (comp.isHovered) + else { if (SHAssetManager::GetType(comp.GetHoveredTexture()) == AssetType::TEXTURE) { @@ -250,14 +257,6 @@ namespace SHADE //SHLOG_INFO("SETTING HOVERED TEXTURE") } } - else - { - if (SHAssetManager::GetType(comp.GetClickedTexture()) == AssetType::TEXTURE) - { - material->SetProperty("data.textureIndex", comp.GetClickedTexture()); - SHLOG_INFO("SETTING CLICKED TEXTURE") - } - } @@ -353,7 +352,7 @@ namespace SHADE if (SHComponentManager::HasComponent(comp.GetEID())) { auto renderable = SHComponentManager::GetComponent_s(comp.GetEID()); - auto texture = SHResourceManager::Get(comp.GetDefaultTexture()); + //auto texture = SHResourceManager::Get(comp.GetDefaultTexture()); auto material = renderable->GetModifiableMaterial(); if (comp.GetValue() == false) From cdb5102630f84e10757bb2b01860cd880b067fd8 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Mon, 16 Jan 2023 14:51:06 +0800 Subject: [PATCH 3/3] Added a 0 check for button textures. --- SHADE_Engine/src/UI/SHUISystem.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/SHADE_Engine/src/UI/SHUISystem.cpp b/SHADE_Engine/src/UI/SHUISystem.cpp index 11808038..c75af66f 100644 --- a/SHADE_Engine/src/UI/SHUISystem.cpp +++ b/SHADE_Engine/src/UI/SHUISystem.cpp @@ -236,14 +236,14 @@ namespace SHADE auto material = renderable->GetModifiableMaterial(); if(!comp.isHovered && !comp.isClicked) - if (SHAssetManager::GetType(comp.GetDefaultTexture()) == AssetType::TEXTURE) + if (comp.GetDefaultTexture() != 0 && SHAssetManager::GetType(comp.GetDefaultTexture()) == AssetType::TEXTURE) { material->SetProperty("data.textureIndex", comp.GetDefaultTexture()); //SHLOG_INFO("SETTING DEFAULT TEXTURE") } else if (comp.isClicked) { - if (SHAssetManager::GetType(comp.GetClickedTexture()) == AssetType::TEXTURE) + if (comp.GetClickedTexture() != 0 && SHAssetManager::GetType(comp.GetClickedTexture()) == AssetType::TEXTURE) { material->SetProperty("data.textureIndex", comp.GetClickedTexture()); //SHLOG_INFO("SETTING CLICKED TEXTURE") @@ -251,7 +251,7 @@ namespace SHADE } else { - if (SHAssetManager::GetType(comp.GetHoveredTexture()) == AssetType::TEXTURE) + if (comp.GetHoveredTexture() != 0 && SHAssetManager::GetType(comp.GetHoveredTexture()) == AssetType::TEXTURE) { material->SetProperty("data.textureIndex", comp.GetHoveredTexture()); //SHLOG_INFO("SETTING HOVERED TEXTURE") @@ -357,7 +357,7 @@ namespace SHADE auto material = renderable->GetModifiableMaterial(); if (comp.GetValue() == false) { - if (SHAssetManager::GetType(comp.GetDefaultTexture()) == AssetType::TEXTURE) + if (comp.GetDefaultTexture()!= 0 && SHAssetManager::GetType(comp.GetDefaultTexture()) == AssetType::TEXTURE) { material->SetProperty("data.textureIndex", comp.GetDefaultTexture()); //SHLOG_INFO("SETTING DEFAULT TEXTURE") @@ -365,7 +365,7 @@ namespace SHADE } else { - if (SHAssetManager::GetType(comp.GetToggledTexture()) == AssetType::TEXTURE) + if (comp.GetToggledTexture() != 0 && SHAssetManager::GetType(comp.GetToggledTexture()) == AssetType::TEXTURE) { material->SetProperty("data.textureIndex", comp.GetToggledTexture()); //SHLOG_INFO("SETTING DEFAULT TEXTURE")