Change isHovered and isClicked to be stored in UIComponent instead and made a helper function to check for clicks and hovered
This commit is contained in:
parent
febe22e487
commit
51909071e6
|
@ -6,8 +6,7 @@
|
||||||
namespace SHADE
|
namespace SHADE
|
||||||
{
|
{
|
||||||
SHButtonComponent::SHButtonComponent()
|
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;
|
clickedTexture = texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -17,8 +17,6 @@ namespace SHADE
|
||||||
SHButtonComponent();
|
SHButtonComponent();
|
||||||
virtual ~SHButtonComponent() = default;
|
virtual ~SHButtonComponent() = default;
|
||||||
|
|
||||||
SHVec2 size;
|
|
||||||
|
|
||||||
AssetID GetClickedTexture() const noexcept;
|
AssetID GetClickedTexture() const noexcept;
|
||||||
AssetID GetDefaultTexture() const noexcept;
|
AssetID GetDefaultTexture() const noexcept;
|
||||||
AssetID GetHoveredTexture() const noexcept;
|
AssetID GetHoveredTexture() const noexcept;
|
||||||
|
@ -32,11 +30,7 @@ namespace SHADE
|
||||||
|
|
||||||
friend class SHUISystem;
|
friend class SHUISystem;
|
||||||
private:
|
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 defaultTexture;
|
||||||
AssetID hoveredTexture;
|
AssetID hoveredTexture;
|
||||||
AssetID clickedTexture;
|
AssetID clickedTexture;
|
||||||
|
|
|
@ -5,8 +5,7 @@
|
||||||
namespace SHADE
|
namespace SHADE
|
||||||
{
|
{
|
||||||
SHToggleButtonComponent::SHToggleButtonComponent()
|
SHToggleButtonComponent::SHToggleButtonComponent()
|
||||||
:size(1.0f), isHovered(false), isClicked(false), value(false),
|
:value(false), defaultTexture(0), toggledTexture(0), currentTexture(0)
|
||||||
defaultTexture(0), toggledTexture(0), currentTexture(0)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,8 +17,6 @@ namespace SHADE
|
||||||
SHToggleButtonComponent();
|
SHToggleButtonComponent();
|
||||||
virtual ~SHToggleButtonComponent() = default;
|
virtual ~SHToggleButtonComponent() = default;
|
||||||
|
|
||||||
SHVec2 size;
|
|
||||||
|
|
||||||
AssetID GetToggledTexture() const noexcept;
|
AssetID GetToggledTexture() const noexcept;
|
||||||
AssetID GetDefaultTexture() const noexcept;
|
AssetID GetDefaultTexture() const noexcept;
|
||||||
bool GetValue() const noexcept;
|
bool GetValue() const noexcept;
|
||||||
|
@ -33,11 +31,7 @@ namespace SHADE
|
||||||
friend class SHUISystem;
|
friend class SHUISystem;
|
||||||
private:
|
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;
|
bool value;
|
||||||
AssetID defaultTexture;
|
AssetID defaultTexture;
|
||||||
AssetID toggledTexture;
|
AssetID toggledTexture;
|
||||||
|
|
|
@ -7,6 +7,7 @@ namespace SHADE
|
||||||
{
|
{
|
||||||
|
|
||||||
SHUIComponent::SHUIComponent()
|
SHUIComponent::SHUIComponent()
|
||||||
|
:size(1.0f), isHovered(false), isClicked(false)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -27,6 +28,20 @@ namespace SHADE
|
||||||
(void)id;
|
(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")
|
registration::class_<SHUIComponent>("UI Component")
|
||||||
.property("Canvas ID", &SHUIComponent::GetCanvasID, &SHUIComponent::SetCanvasID)
|
.property("Canvas ID", &SHUIComponent::GetCanvasID, &SHUIComponent::SetCanvasID)
|
||||||
|
.property("Hovered", &SHUIComponent::GetIsHovered, &SHUIComponent::SetEmptyHoveredClick)
|
||||||
|
.property("Clicked", &SHUIComponent::GetIsClicked, &SHUIComponent::SetEmptyHoveredClick)
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#include "SH_API.h"
|
#include "SH_API.h"
|
||||||
#include "ECS_Base/Components/SHComponent.h"
|
#include "ECS_Base/Components/SHComponent.h"
|
||||||
#include "Math/SHMatrix.h"
|
#include "Math/SHMatrix.h"
|
||||||
|
#include "Math/Vector/SHVec2.h"
|
||||||
|
|
||||||
|
|
||||||
namespace SHADE
|
namespace SHADE
|
||||||
|
@ -17,14 +18,29 @@ namespace SHADE
|
||||||
SHUIComponent();
|
SHUIComponent();
|
||||||
~SHUIComponent() = default;
|
~SHUIComponent() = default;
|
||||||
|
|
||||||
|
|
||||||
|
SHVec2 size;
|
||||||
|
|
||||||
|
|
||||||
SHMatrix const& GetMatrix() const noexcept;
|
SHMatrix const& GetMatrix() const noexcept;
|
||||||
EntityID GetCanvasID() const noexcept;
|
EntityID GetCanvasID() const noexcept;
|
||||||
void SetCanvasID(EntityID id) noexcept;
|
void SetCanvasID(EntityID id) noexcept;
|
||||||
|
|
||||||
|
bool GetIsHovered() const noexcept;
|
||||||
|
bool GetIsClicked() const noexcept;
|
||||||
|
|
||||||
|
void SetEmptyHoveredClick(bool value)noexcept;
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SHMatrix localToCanvasMatrix;
|
SHMatrix localToCanvasMatrix;
|
||||||
EntityID canvasID;
|
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()
|
RTTR_ENABLE()
|
||||||
};
|
};
|
||||||
|
|
|
@ -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>();
|
auto cameraSystem = SHSystemManager::GetSystem<SHCameraSystem>();
|
||||||
SHVec2 mousePos;
|
SHVec2 mousePos;
|
||||||
SHVec2 windowSize;
|
SHVec2 windowSize;
|
||||||
|
@ -185,13 +190,13 @@ namespace SHADE
|
||||||
if (mousePos.x >= topExtent.x && mousePos.x <= btmExtent.x
|
if (mousePos.x >= topExtent.x && mousePos.x <= btmExtent.x
|
||||||
&& mousePos.y >= topExtent.y && mousePos.y <= btmExtent.y)
|
&& mousePos.y >= topExtent.y && mousePos.y <= btmExtent.y)
|
||||||
{
|
{
|
||||||
if (isHovered == false)
|
if (comp.isHovered == false)
|
||||||
{
|
{
|
||||||
SHButtonClickEvent clickEvent;
|
SHButtonClickEvent clickEvent;
|
||||||
clickEvent.EID = eid;
|
clickEvent.EID = comp.GetEID();
|
||||||
SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_HOVER_ENTER_EVENT);
|
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))
|
if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::LMB))
|
||||||
{
|
{
|
||||||
isClicked = true;
|
comp.isClicked = true;
|
||||||
SHButtonClickEvent clickEvent;
|
SHButtonClickEvent clickEvent;
|
||||||
clickEvent.EID = eid;
|
clickEvent.EID = comp.GetEID();
|
||||||
SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT);
|
SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -211,7 +216,7 @@ namespace SHADE
|
||||||
{
|
{
|
||||||
comp.isClicked = true;
|
comp.isClicked = true;
|
||||||
SHButtonClickEvent clickEvent;
|
SHButtonClickEvent clickEvent;
|
||||||
clickEvent.EID = eid;
|
clickEvent.EID = comp.GetEID();
|
||||||
SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT);
|
SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -220,21 +225,21 @@ namespace SHADE
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (isHovered == true)
|
if (comp.isHovered == true)
|
||||||
{
|
{
|
||||||
SHButtonClickEvent clickEvent;
|
SHButtonClickEvent clickEvent;
|
||||||
clickEvent.EID = eid;
|
clickEvent.EID = comp.GetEID();
|
||||||
SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_HOVER_EXIT_EVENT);
|
SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_HOVER_EXIT_EVENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
isHovered = false;
|
comp.isHovered = false;
|
||||||
//SHLOG_INFO("NOT HOVERED")
|
//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;
|
SHButtonClickEvent clickEvent;
|
||||||
clickEvent.EID = eid;
|
clickEvent.EID = comp.GetEID();
|
||||||
SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_RELEASE_EVENT);
|
SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_RELEASE_EVENT);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -256,14 +261,9 @@ namespace SHADE
|
||||||
auto uiComp = SHComponentManager::GetComponent<SHUIComponent>(comp.GetEID());
|
auto uiComp = SHComponentManager::GetComponent<SHUIComponent>(comp.GetEID());
|
||||||
//auto canvasComp = SHComponentManager::GetComponent_s<SHCanvasComponent>(uiComp->canvasID);
|
//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 };
|
CheckButtonHoveredOrClicked(*uiComp);
|
||||||
SHVec2 btmExtent{ btmExtent4.x,btmExtent4.y };
|
|
||||||
|
|
||||||
CheckButtonHoveredOrClicked(comp.GetEID(), topExtent, btmExtent, comp.isHovered, comp.isClicked);
|
|
||||||
|
|
||||||
if (SHComponentManager::HasComponent<SHRenderable>(comp.GetEID()))
|
if (SHComponentManager::HasComponent<SHRenderable>(comp.GetEID()))
|
||||||
{
|
{
|
||||||
|
@ -272,7 +272,7 @@ namespace SHADE
|
||||||
|
|
||||||
|
|
||||||
AssetID textureID = 0;
|
AssetID textureID = 0;
|
||||||
if (!comp.isHovered && !comp.isClicked)
|
if (!uiComp->isHovered && !uiComp->isClicked)
|
||||||
{
|
{
|
||||||
if (comp.GetDefaultTexture() != 0 && SHAssetManager::GetType(comp.GetDefaultTexture()) == AssetType::TEXTURE)
|
if (comp.GetDefaultTexture() != 0 && SHAssetManager::GetType(comp.GetDefaultTexture()) == AssetType::TEXTURE)
|
||||||
{
|
{
|
||||||
|
@ -281,7 +281,7 @@ namespace SHADE
|
||||||
//SHLOG_INFO("SETTING DEFAULT TEXTURE")
|
//SHLOG_INFO("SETTING DEFAULT TEXTURE")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (comp.isClicked)
|
else if (uiComp->isClicked)
|
||||||
{
|
{
|
||||||
if (comp.GetClickedTexture() != 0 && SHAssetManager::GetType(comp.GetClickedTexture()) == AssetType::TEXTURE)
|
if (comp.GetClickedTexture() != 0 && SHAssetManager::GetType(comp.GetClickedTexture()) == AssetType::TEXTURE)
|
||||||
{
|
{
|
||||||
|
@ -322,14 +322,9 @@ namespace SHADE
|
||||||
auto uiComp = SHComponentManager::GetComponent<SHUIComponent>(comp.GetEID());
|
auto uiComp = SHComponentManager::GetComponent<SHUIComponent>(comp.GetEID());
|
||||||
//auto canvasComp = SHComponentManager::GetComponent_s<SHCanvasComponent>(uiComp->canvasID);
|
//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 };
|
if (CheckButtonHoveredOrClicked(*uiComp))
|
||||||
SHVec2 btmExtent{ btmExtent4.x,btmExtent4.y };
|
|
||||||
|
|
||||||
if (CheckButtonHoveredOrClicked(comp.GetEID(), topExtent, btmExtent, comp.isHovered, comp.isClicked))
|
|
||||||
comp.value = !comp.value;
|
comp.value = !comp.value;
|
||||||
|
|
||||||
if (SHComponentManager::HasComponent<SHRenderable>(comp.GetEID()))
|
if (SHComponentManager::HasComponent<SHRenderable>(comp.GetEID()))
|
||||||
|
|
|
@ -74,7 +74,7 @@ namespace SHADE
|
||||||
void UpdateCanvasComponent(SHCanvasComponent& comp) noexcept;
|
void UpdateCanvasComponent(SHCanvasComponent& comp) noexcept;
|
||||||
|
|
||||||
//returns true on button release.
|
//returns true on button release.
|
||||||
bool CheckButtonHoveredOrClicked(EntityID eid, SHVec2 topExtent, SHVec2 btmExtent, bool& isHovered, bool& isClicked) noexcept;
|
bool CheckButtonHoveredOrClicked(SHUIComponent& comp) noexcept;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue