added events

This commit is contained in:
maverickdgg 2023-01-16 11:36:12 +08:00
parent 5aa7bfe03e
commit 5190c490c9
8 changed files with 196 additions and 21 deletions

View File

@ -34,7 +34,7 @@
NumberOfChildren: 0 NumberOfChildren: 0
Components: Components:
Transform Component: Transform Component:
Translate: {x: 153.399994, y: 0, z: 0} Translate: {x: 0, y: -3.9000001, z: 0}
Rotate: {x: 0, y: 0, z: 0} Rotate: {x: 0, y: 0, z: 0}
Scale: {x: 1, y: 1, z: 1} Scale: {x: 1, y: 1, z: 1}
IsActive: true IsActive: true
@ -45,7 +45,7 @@
Toggle Button Component: Toggle Button Component:
Non Toggled Texture: 0 Non Toggled Texture: 0
Toggled Texture: 0 Toggled Texture: 0
Value: false Value: true
IsActive: true IsActive: true
Scripts: ~ Scripts: ~
- EID: 1 - EID: 1

View File

@ -1,3 +1,3 @@
Name: UI Test Name: UI Test
ID: 92992815 ID: 92642496
Type: 5 Type: 5

View File

@ -18,4 +18,5 @@ constexpr SHEventIdentifier SH_PHYSICS_COLLIDER_REMOVED_EVENT { 9 };
constexpr SHEventIdentifier SH_EDITOR_ON_PLAY_EVENT { 10 }; constexpr SHEventIdentifier SH_EDITOR_ON_PLAY_EVENT { 10 };
constexpr SHEventIdentifier SH_EDITOR_ON_PAUSE_EVENT { 11 }; constexpr SHEventIdentifier SH_EDITOR_ON_PAUSE_EVENT { 11 };
constexpr SHEventIdentifier SH_EDITOR_ON_STOP_EVENT { 12 }; constexpr SHEventIdentifier SH_EDITOR_ON_STOP_EVENT { 12 };
constexpr SHEventIdentifier SH_BUTTON_CLICK_EVENT { 13 };

View File

@ -0,0 +1,16 @@
#pragma once
#include "ECS_Base/SHECSMacros.h"
namespace SHADE
{
struct SHButtonClickEvent
{
EntityID EID;
// value of the toggle button, default to false if its a button and not a toggle button
bool value{false};
};
}

View File

@ -0,0 +1,39 @@
#include "SHpch.h"
#include "SHSliderComponent.h"
namespace SHADE
{
SHSliderComponent::SHSliderComponent()
:size(1.0f), isHovered(false), isClicked(false), value(0.0f)
{
}
float SHSliderComponent::GetValue() const noexcept
{
return value;
}
void SHSliderComponent::SetValue(float value) noexcept
{
this->value = value;
}
}
RTTR_REGISTRATION
{
using namespace SHADE;
using namespace rttr;
registration::class_<SHSliderComponent>("Slider Component")
.property("Slider Value", &SHSliderComponent::GetValue, &SHSliderComponent::SetValue)
;
}

View File

@ -0,0 +1,41 @@
#pragma once
#include <rttr/registration>
#include "SH_API.h"
#include "ECS_Base/Components/SHComponent.h"
#include "Math/Vector/SHVec3.h"
#include "Math/Vector/SHVec2.h"
#include "Assets/SHAssetMacros.h"
namespace SHADE
{
class SH_API SHSliderComponent final: public SHComponent
{
public:
SHSliderComponent();
virtual ~SHSliderComponent() = default;
SHVec2 size;
float GetValue() const noexcept;
void SetValue(float value) noexcept;
friend class SHUISystem;
private:
bool isHovered;
bool isClicked;
float value;
RTTR_ENABLE()
};
}

View File

@ -10,6 +10,13 @@
#include "Editor/SHEditor.h" #include "Editor/SHEditor.h"
#include "Resource/SHResourceManager.h" #include "Resource/SHResourceManager.h"
#include "Input/SHInputManager.h" #include "Input/SHInputManager.h"
#include "SHUIComponent.h"
#include "SHButtonComponent.h"
#include "SHToggleButtonComponent.h"
#include "SHSliderComponent.h"
#include "SHCanvasComponent.h"
#include "Events/SHEventManager.hpp"
#include "Events/SHButtonClickEvent.h"
namespace SHADE namespace SHADE
{ {
@ -164,32 +171,60 @@ namespace SHADE
SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y) SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y)
mousePos /= windowSize; mousePos /= windowSize;
SHLOG_INFO("mouse pos normalized: {}, {}", mousePos.x, mousePos.y) 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 = { ws.first,ws.second };
mousePos /= windowSize;
#endif #endif
SHVec2 camSize{ cameraSystem->GetCameraWidthHeight(0)}; SHVec2 camSize{ cameraSystem->GetCameraWidthHeight(0)};
//SHLOG_INFO("TopExtent: {}, {}", topExtent.x, topExtent.y) //SHLOG_INFO("TopExtent: {}, {}", topExtent.x, topExtent.y)
topExtent = CanvasToScreenPoint(topExtent); topExtent = CanvasToScreenPoint(topExtent,true);
btmExtent = CanvasToScreenPoint(btmExtent); btmExtent = CanvasToScreenPoint(btmExtent,true);
//SHLOG_INFO("TopExtent: {}, {} Btm Extent: {}, {}", topExtent.x, topExtent.y, btmExtent.x, btmExtent.y) //SHLOG_INFO("TopExtent: {}, {} Btm Extent: {}, {}", topExtent.x, topExtent.y, btmExtent.x, btmExtent.y)
topExtent /= camSize;
btmExtent /= camSize;
comp.isClicked = false; comp.isClicked = false;
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)
{ {
comp.isHovered = true; comp.isHovered = true;
#ifdef SHEDITOR
if (SHSystemManager::GetSystem<SHEditor>()->editorState == SHEditor::State::PLAY)
{
if (SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) if (SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB))
{ {
comp.isClicked = true; comp.isClicked = true;
SHButtonClickEvent clickEvent;
clickEvent.EID = comp.GetEID();
SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT);
} }
SHLOG_INFO("HOVERED") }
#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
//SHLOG_INFO("HOVERED")
} }
else else
{ {
comp.isHovered = false; comp.isHovered = false;
SHLOG_INFO("NOT HOVERED") //SHLOG_INFO("NOT HOVERED")
} }
@ -222,9 +257,25 @@ namespace SHADE
SHVec2 mousePos; SHVec2 mousePos;
SHVec2 windowSize; SHVec2 windowSize;
#ifdef SHEDITOR #ifdef SHEDITOR
windowSize = SHEditorWindowManager::GetEditorWindow<SHEditorViewport>()->windowSize; windowSize = SHEditorWindowManager::GetEditorWindow<SHEditorViewport>()->beginContentRegionAvailable;
mousePos = SHEditorWindowManager::GetEditorWindow<SHEditorViewport>()->viewportMousePos; 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 = { ws.first,ws.second };
mousePos /= windowSize; mousePos /= windowSize;
#endif #endif
@ -232,10 +283,9 @@ namespace SHADE
topExtent = CanvasToScreenPoint(topExtent); topExtent = CanvasToScreenPoint(topExtent,true);
btmExtent = CanvasToScreenPoint(btmExtent); btmExtent = CanvasToScreenPoint(btmExtent, true);
topExtent /= camSize;
btmExtent /= camSize;
comp.isClicked = false; comp.isClicked = false;
@ -243,12 +293,31 @@ namespace SHADE
&& mousePos.y >= topExtent.y && mousePos.y <= btmExtent.y) && mousePos.y >= topExtent.y && mousePos.y <= btmExtent.y)
{ {
comp.isHovered = true; comp.isHovered = true;
#ifdef SHEDITOR
if (SHSystemManager::GetSystem<SHEditor>()->editorState == SHEditor::State::PLAY)
{
if (SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) if (SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB))
{ {
comp.isClicked = true; comp.isClicked = true;
comp.value = !comp.value; 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))
{
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
}
else else
{ {
comp.isHovered = false; comp.isHovered = false;
@ -284,7 +353,7 @@ namespace SHADE
} }
} }
SHVec2 SHUISystem::CanvasToScreenPoint(SHVec2& const canvasPoint) noexcept SHVec2 SHUISystem::CanvasToScreenPoint(SHVec2& const canvasPoint, bool normalized) noexcept
{ {
SHVec2 result{canvasPoint}; SHVec2 result{canvasPoint};
@ -296,6 +365,9 @@ namespace SHADE
result.y *= -1.0f; result.y *= -1.0f;
result += camSize * 0.5f; result += camSize * 0.5f;
if (normalized)
return result / camSize;
else
return result; return result;
} }

View File

@ -3,15 +3,20 @@
#include "SH_API.h" #include "SH_API.h"
#include "ECS_Base/System/SHSystem.h" #include "ECS_Base/System/SHSystem.h"
#include "ECS_Base/System/SHSystemRoutine.h" #include "ECS_Base/System/SHSystemRoutine.h"
#include "SHUIComponent.h"
#include "SHButtonComponent.h"
#include "SHToggleButtonComponent.h"
#include "SHCanvasComponent.h"
#include "Scene/SHSceneGraph.h" #include "Scene/SHSceneGraph.h"
#include "Scene/SHSceneManager.h" #include "Scene/SHSceneManager.h"
#include "Math/Vector/SHVec2.h"
namespace SHADE namespace SHADE
{ {
class SHButtonComponent;
class SHUIComponent;
class SHToggleButtonComponent;
class SHSliderComponent;
class SHCanvasComponent;
class SH_API SHUISystem final: public SHSystem class SH_API SHUISystem final: public SHSystem
{ {
public: public:
@ -67,7 +72,8 @@ namespace SHADE
void UpdateButtonComponent(SHButtonComponent& comp) noexcept; void UpdateButtonComponent(SHButtonComponent& comp) noexcept;
void UpdateToggleButtonComponent(SHToggleButtonComponent& comp) noexcept; void UpdateToggleButtonComponent(SHToggleButtonComponent& comp) noexcept;
void UpdateCanvasComponent(SHCanvasComponent& comp) noexcept; void UpdateCanvasComponent(SHCanvasComponent& comp) noexcept;
SHVec2 CanvasToScreenPoint(SHVec2& const canvasPoint) noexcept;
SHVec2 CanvasToScreenPoint(SHVec2& const canvasPoint, bool normalized) noexcept;