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
4 changed files with 110 additions and 133 deletions
Showing only changes of commit febe22e487 - Show all commits

View File

@ -1,4 +1,4 @@
Start Maximized: true
Working Scene ID: 86098106
Working Scene ID: 97158628
Window Size: {x: 1920, y: 1013}
Style: 0

View File

@ -26,4 +26,7 @@ constexpr SHEventIdentifier SH_GRAPHICS_LIGHT_ENABLE_SHADOW_EVENT { 17 };
constexpr SHEventIdentifier SH_BUTTON_CLICK_EVENT { 18 };
constexpr SHEventIdentifier SH_PHYSICS_COLLIDER_DRAW_EVENT { 19 };
constexpr SHEventIdentifier SH_WINDOW_RESIZE_EVENT { 20 };
constexpr SHEventIdentifier SH_BUTTON_RELEASE_EVENT { 21 };
constexpr SHEventIdentifier SH_BUTTON_HOVER_ENTER_EVENT { 22 };
constexpr SHEventIdentifier SH_BUTTON_HOVER_EXIT_EVENT { 23 };

View File

@ -146,6 +146,105 @@ namespace SHADE
}
}
bool SHUISystem::CheckButtonHoveredOrClicked(EntityID eid, SHVec2 topExtent, SHVec2 btmExtent, bool& isHovered, bool& isClicked) noexcept
{
auto cameraSystem = SHSystemManager::GetSystem<SHCameraSystem>();
SHVec2 mousePos;
SHVec2 windowSize;
#ifdef SHEDITOR
windowSize = SHEditorWindowManager::GetEditorWindow<SHEditorViewport>()->beginContentRegionAvailable;
mousePos = SHEditorWindowManager::GetEditorWindow<SHEditorViewport>()->viewportMousePos;
//mousePos.y = windowSize.y - mousePos.y;
//SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y)
mousePos /= windowSize;
//SHLOG_INFO("mouse pos normalized: {}, {}", mousePos.x, mousePos.y)
#else
int x, y;
SHInputManager::GetMouseScreenPosition(&x, &y);
mousePos.x = x;
mousePos.y = y;
auto ws = SHSystemManager::GetSystem<SHGraphicsSystem>()->GetWindow()->GetWindowSize();
windowSize = { static_cast<float>(ws.first), static_cast<float>(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);
//SHLOG_INFO("TopExtent: {}, {} Btm Extent: {}, {}", topExtent.x, topExtent.y, btmExtent.x, btmExtent.y)
//comp.isClicked = false;
if (mousePos.x >= topExtent.x && mousePos.x <= btmExtent.x
&& mousePos.y >= topExtent.y && mousePos.y <= btmExtent.y)
{
if (isHovered == false)
{
SHButtonClickEvent clickEvent;
clickEvent.EID = eid;
SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_HOVER_ENTER_EVENT);
}
isHovered = true;
#ifdef SHEDITOR
//if (SHSystemManager::GetSystem<SHEditor>()->editorState == SHEditor::State::PLAY)
{
if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::LMB))
{
isClicked = true;
SHButtonClickEvent clickEvent;
clickEvent.EID = eid;
SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT);
}
}
#else
if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::LMB))
{
comp.isClicked = true;
SHButtonClickEvent clickEvent;
clickEvent.EID = eid;
SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT);
}
#endif
//SHLOG_INFO("HOVERED")
}
else
{
if (isHovered == true)
{
SHButtonClickEvent clickEvent;
clickEvent.EID = eid;
SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_HOVER_EXIT_EVENT);
}
isHovered = false;
//SHLOG_INFO("NOT HOVERED")
}
if (isClicked && SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB))
{
isClicked = false;
SHButtonClickEvent clickEvent;
clickEvent.EID = eid;
SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_RELEASE_EVENT);
return true;
}
return false;
}
void SHUISystem::UpdateButtonComponent(SHButtonComponent& comp) noexcept
{
@ -164,72 +263,7 @@ namespace SHADE
SHVec2 topExtent{ topExtent4.x,topExtent4.y };
SHVec2 btmExtent{ btmExtent4.x,btmExtent4.y };
SHVec2 mousePos;
SHVec2 windowSize;
#ifdef SHEDITOR
windowSize = SHEditorWindowManager::GetEditorWindow<SHEditorViewport>()->beginContentRegionAvailable;
mousePos = SHEditorWindowManager::GetEditorWindow<SHEditorViewport>()->viewportMousePos;
//mousePos.y = windowSize.y - mousePos.y;
//SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y)
mousePos /= windowSize;
//SHLOG_INFO("mouse pos normalized: {}, {}", mousePos.x, mousePos.y)
#else
int x, y;
SHInputManager::GetMouseScreenPosition(&x, &y);
mousePos.x = x;
mousePos.y = y;
auto ws = SHSystemManager::GetSystem<SHGraphicsSystem>()->GetWindow()->GetWindowSize();
windowSize = { static_cast<float>(ws.first), static_cast<float>(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);
//SHLOG_INFO("TopExtent: {}, {} Btm Extent: {}, {}", topExtent.x, topExtent.y, btmExtent.x, btmExtent.y)
//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<SHEditor>()->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;
}
#endif
//SHLOG_INFO("HOVERED")
}
else
{
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);
}
CheckButtonHoveredOrClicked(comp.GetEID(), topExtent, btmExtent, comp.isHovered, comp.isClicked);
if (SHComponentManager::HasComponent<SHRenderable>(comp.GetEID()))
{
@ -295,73 +329,8 @@ namespace SHADE
SHVec2 topExtent{ topExtent4.x,topExtent4.y };
SHVec2 btmExtent{ btmExtent4.x,btmExtent4.y };
SHVec2 mousePos;
SHVec2 windowSize;
#ifdef SHEDITOR
windowSize = SHEditorWindowManager::GetEditorWindow<SHEditorViewport>()->beginContentRegionAvailable;
mousePos = SHEditorWindowManager::GetEditorWindow<SHEditorViewport>()->viewportMousePos;
//mousePos.y = windowSize.y - mousePos.y;
//SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y)
mousePos /= windowSize;
//SHLOG_INFO("mouse pos normalized: {}, {}", mousePos.x, mousePos.y)
#else
int x, y;
SHInputManager::GetMouseScreenPosition(&x, &y);
mousePos.x = x;
mousePos.y = y;
auto ws = SHSystemManager::GetSystem<SHGraphicsSystem>()->GetWindow()->GetWindowSize();
windowSize = { static_cast<float>(ws.first), static_cast<float>(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);
//SHLOG_INFO("TopExtent: {}, {} Btm Extent: {}, {}", topExtent.x, topExtent.y, btmExtent.x, btmExtent.y)
//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<SHEditor>()->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;
}
#endif
//SHLOG_INFO("HOVERED")
}
else
{
comp.isHovered = false;
//SHLOG_INFO("NOT HOVERED")
}
if (comp.isClicked && SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB))
{
comp.isClicked = false;
if (CheckButtonHoveredOrClicked(comp.GetEID(), topExtent, btmExtent, comp.isHovered, comp.isClicked))
comp.value = !comp.value;
SHButtonClickEvent clickEvent;
clickEvent.EID = comp.GetEID();
SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT);
}
if (SHComponentManager::HasComponent<SHRenderable>(comp.GetEID()))
{

View File

@ -72,6 +72,11 @@ namespace SHADE
void UpdateButtonComponent(SHButtonComponent& comp) noexcept;
void UpdateToggleButtonComponent(SHToggleButtonComponent& comp) noexcept;
void UpdateCanvasComponent(SHCanvasComponent& comp) noexcept;
//returns true on button release.
bool CheckButtonHoveredOrClicked(EntityID eid, SHVec2 topExtent, SHVec2 btmExtent, bool& isHovered, bool& isClicked) noexcept;
SHVec2 CanvasToScreenPoint(SHVec2& const canvasPoint, bool normalized) noexcept;