Added 3 button event. Changed hovered and clicked boolean to be stored in UIComponent instead. Fixed Canvas Scaling matrix. #356

Merged
maverickdgg merged 3 commits from SP3-20-UI-System into main 2023-02-21 15:49:28 +08:00
8 changed files with 64 additions and 47 deletions
Showing only changes of commit 51909071e6 - Show all commits

View File

@ -6,8 +6,7 @@
namespace SHADE
{
SHButtonComponent::SHButtonComponent()
:size(1.0f), isHovered(false), isClicked(false),
defaultTexture(0), hoveredTexture(0), clickedTexture(0), currentTexture(0)
: defaultTexture(0), hoveredTexture(0), clickedTexture(0), currentTexture(0)
{
}
@ -41,6 +40,9 @@ namespace SHADE
clickedTexture = texture;
}
}

View File

@ -17,8 +17,6 @@ namespace SHADE
SHButtonComponent();
virtual ~SHButtonComponent() = default;
SHVec2 size;
AssetID GetClickedTexture() const noexcept;
AssetID GetDefaultTexture() const noexcept;
AssetID GetHoveredTexture() const noexcept;
@ -32,11 +30,7 @@ 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;
AssetID clickedTexture;

View File

@ -5,8 +5,7 @@
namespace SHADE
{
SHToggleButtonComponent::SHToggleButtonComponent()
:size(1.0f), isHovered(false), isClicked(false), value(false),
defaultTexture(0), toggledTexture(0), currentTexture(0)
:value(false), defaultTexture(0), toggledTexture(0), currentTexture(0)
{
}

View File

@ -17,8 +17,6 @@ namespace SHADE
SHToggleButtonComponent();
virtual ~SHToggleButtonComponent() = default;
SHVec2 size;
AssetID GetToggledTexture() const noexcept;
AssetID GetDefaultTexture() const noexcept;
bool GetValue() const noexcept;
@ -33,11 +31,7 @@ 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;
AssetID toggledTexture;

View File

@ -7,6 +7,7 @@ namespace SHADE
{
SHUIComponent::SHUIComponent()
:size(1.0f), isHovered(false), isClicked(false)
{
}
@ -27,6 +28,20 @@ namespace SHADE
(void)id;
}
bool SHUIComponent::GetIsHovered() const noexcept
{
return isHovered;
}
bool SHUIComponent::GetIsClicked() const noexcept
{
return isClicked;
}
void SHUIComponent::SetEmptyHoveredClick(bool value) noexcept
{
(void)value;
}
}
@ -37,6 +52,8 @@ RTTR_REGISTRATION
registration::class_<SHUIComponent>("UI Component")
.property("Canvas ID", &SHUIComponent::GetCanvasID, &SHUIComponent::SetCanvasID)
.property("Hovered", &SHUIComponent::GetIsHovered, &SHUIComponent::SetEmptyHoveredClick)
.property("Clicked", &SHUIComponent::GetIsClicked, &SHUIComponent::SetEmptyHoveredClick)
;

View File

@ -5,6 +5,7 @@
#include "SH_API.h"
#include "ECS_Base/Components/SHComponent.h"
#include "Math/SHMatrix.h"
#include "Math/Vector/SHVec2.h"
namespace SHADE
@ -17,14 +18,29 @@ namespace SHADE
SHUIComponent();
~SHUIComponent() = default;
SHVec2 size;
SHMatrix const& GetMatrix() const noexcept;
EntityID GetCanvasID() const noexcept;
void SetCanvasID(EntityID id) noexcept;
bool GetIsHovered() const noexcept;
bool GetIsClicked() const noexcept;
void SetEmptyHoveredClick(bool value)noexcept;
private:
SHMatrix localToCanvasMatrix;
EntityID canvasID;
//Set to true when mouse is hovering over the button. Only set if there is a Button/Slider/ToggleButton comp.
bool isHovered;
//This is set to true when the mouse clicks down, and set back to false when mouse releases. Only set if there is a Button/Slider/ToggleButton comp.
bool isClicked;
RTTR_ENABLE()
};

View File

@ -146,9 +146,14 @@ namespace SHADE
}
}
bool SHUISystem::CheckButtonHoveredOrClicked(EntityID eid, SHVec2 topExtent, SHVec2 btmExtent, bool& isHovered, bool& isClicked) noexcept
bool SHUISystem::CheckButtonHoveredOrClicked(SHUIComponent& comp) noexcept
{
SHVec4 topExtent4 = SHMatrix::Translate(-comp.size.x * 0.5f, comp.size.y * 0.5f, 0.0f) * comp.GetMatrix() * SHVec4(0.0f, 0.0f, 0.0f, 1.0f);
SHVec4 btmExtent4 = SHMatrix::Translate(comp.size.x * 0.5f, -comp.size.y * 0.5f, 0.0f) * comp.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<SHCameraSystem>();
SHVec2 mousePos;
SHVec2 windowSize;
@ -185,13 +190,13 @@ namespace SHADE
if (mousePos.x >= topExtent.x && mousePos.x <= btmExtent.x
&& mousePos.y >= topExtent.y && mousePos.y <= btmExtent.y)
{
if (isHovered == false)
if (comp.isHovered == false)
{
SHButtonClickEvent clickEvent;
clickEvent.EID = eid;
clickEvent.EID = comp.GetEID();
SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_HOVER_ENTER_EVENT);
}
isHovered = true;
comp.isHovered = true;
@ -200,9 +205,9 @@ namespace SHADE
{
if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::LMB))
{
isClicked = true;
comp.isClicked = true;
SHButtonClickEvent clickEvent;
clickEvent.EID = eid;
clickEvent.EID = comp.GetEID();
SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT);
}
}
@ -211,7 +216,7 @@ namespace SHADE
{
comp.isClicked = true;
SHButtonClickEvent clickEvent;
clickEvent.EID = eid;
clickEvent.EID = comp.GetEID();
SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT);
}
#endif
@ -220,21 +225,21 @@ namespace SHADE
}
else
{
if (isHovered == true)
if (comp.isHovered == true)
{
SHButtonClickEvent clickEvent;
clickEvent.EID = eid;
clickEvent.EID = comp.GetEID();
SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_HOVER_EXIT_EVENT);
}
isHovered = false;
comp.isHovered = false;
//SHLOG_INFO("NOT HOVERED")
}
if (isClicked && SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB))
if (comp.isClicked && SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB))
{
isClicked = false;
comp.isClicked = false;
SHButtonClickEvent clickEvent;
clickEvent.EID = eid;
clickEvent.EID = comp.GetEID();
SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_RELEASE_EVENT);
return true;
@ -256,14 +261,9 @@ namespace SHADE
auto uiComp = SHComponentManager::GetComponent<SHUIComponent>(comp.GetEID());
//auto canvasComp = SHComponentManager::GetComponent_s<SHCanvasComponent>(uiComp->canvasID);
SHVec4 topExtent4 = SHMatrix::Translate(-comp.size.x * 0.5f, comp.size.y * 0.5f, 0.0f) * uiComp->GetMatrix() * SHVec4(0.0f, 0.0f, 0.0f, 1.0f);
SHVec4 btmExtent4 = SHMatrix::Translate(comp.size.x * 0.5f, -comp.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 };
CheckButtonHoveredOrClicked(comp.GetEID(), topExtent, btmExtent, comp.isHovered, comp.isClicked);
CheckButtonHoveredOrClicked(*uiComp);
if (SHComponentManager::HasComponent<SHRenderable>(comp.GetEID()))
{
@ -272,7 +272,7 @@ namespace SHADE
AssetID textureID = 0;
if (!comp.isHovered && !comp.isClicked)
if (!uiComp->isHovered && !uiComp->isClicked)
{
if (comp.GetDefaultTexture() != 0 && SHAssetManager::GetType(comp.GetDefaultTexture()) == AssetType::TEXTURE)
{
@ -281,7 +281,7 @@ namespace SHADE
//SHLOG_INFO("SETTING DEFAULT TEXTURE")
}
}
else if (comp.isClicked)
else if (uiComp->isClicked)
{
if (comp.GetClickedTexture() != 0 && SHAssetManager::GetType(comp.GetClickedTexture()) == AssetType::TEXTURE)
{
@ -322,14 +322,9 @@ namespace SHADE
auto uiComp = SHComponentManager::GetComponent<SHUIComponent>(comp.GetEID());
//auto canvasComp = SHComponentManager::GetComponent_s<SHCanvasComponent>(uiComp->canvasID);
SHVec4 topExtent4 = SHMatrix::Translate(-comp.size.x * 0.5f, comp.size.y * 0.5f, 0.0f) * uiComp->GetMatrix() * SHVec4(0.0f, 0.0f, 0.0f, 1.0f);
SHVec4 btmExtent4 = SHMatrix::Translate(comp.size.x * 0.5f, -comp.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 };
if (CheckButtonHoveredOrClicked(comp.GetEID(), topExtent, btmExtent, comp.isHovered, comp.isClicked))
if (CheckButtonHoveredOrClicked(*uiComp))
comp.value = !comp.value;
if (SHComponentManager::HasComponent<SHRenderable>(comp.GetEID()))

View File

@ -74,7 +74,7 @@ namespace SHADE
void UpdateCanvasComponent(SHCanvasComponent& comp) noexcept;
//returns true on button release.
bool CheckButtonHoveredOrClicked(EntityID eid, SHVec2 topExtent, SHVec2 btmExtent, bool& isHovered, bool& isClicked) noexcept;
bool CheckButtonHoveredOrClicked(SHUIComponent& comp) noexcept;