Fixed Buttons, Added Button events, Added Toggle Buttons, Added Sliders(WIP no graphics), Rework backend #308

Merged
maverickdgg merged 13 commits from SP3-20-UI-System into main 2023-01-16 14:11:51 +08:00
8 changed files with 196 additions and 21 deletions
Showing only changes of commit 5190c490c9 - Show all commits

View File

@ -34,7 +34,7 @@
NumberOfChildren: 0
Components:
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}
Scale: {x: 1, y: 1, z: 1}
IsActive: true
@ -45,7 +45,7 @@
Toggle Button Component:
Non Toggled Texture: 0
Toggled Texture: 0
Value: false
Value: true
IsActive: true
Scripts: ~
- EID: 1

View File

@ -1,3 +1,3 @@
Name: UI Test
ID: 92992815
ID: 92642496
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_PAUSE_EVENT { 11 };
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 "Resource/SHResourceManager.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
{
@ -164,32 +171,60 @@ namespace SHADE
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;
#endif
SHVec2 camSize{ cameraSystem->GetCameraWidthHeight(0)};
//SHLOG_INFO("TopExtent: {}, {}", topExtent.x, topExtent.y)
topExtent = CanvasToScreenPoint(topExtent);
btmExtent = CanvasToScreenPoint(btmExtent);
topExtent = CanvasToScreenPoint(topExtent,true);
btmExtent = CanvasToScreenPoint(btmExtent,true);
//SHLOG_INFO("TopExtent: {}, {} Btm Extent: {}, {}", topExtent.x, topExtent.y, btmExtent.x, btmExtent.y)
topExtent /= camSize;
btmExtent /= camSize;
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::GetKeyUp(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);
}
SHLOG_INFO("HOVERED")
#endif
//SHLOG_INFO("HOVERED")
}
else
{
comp.isHovered = false;
SHLOG_INFO("NOT HOVERED")
//SHLOG_INFO("NOT HOVERED")
}
@ -222,9 +257,25 @@ namespace SHADE
SHVec2 mousePos;
SHVec2 windowSize;
#ifdef SHEDITOR
windowSize = SHEditorWindowManager::GetEditorWindow<SHEditorViewport>()->windowSize;
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 = { ws.first,ws.second };
mousePos /= windowSize;
#endif
@ -232,10 +283,9 @@ namespace SHADE
topExtent = CanvasToScreenPoint(topExtent);
btmExtent = CanvasToScreenPoint(btmExtent);
topExtent /= camSize;
btmExtent /= camSize;
topExtent = CanvasToScreenPoint(topExtent,true);
btmExtent = CanvasToScreenPoint(btmExtent, true);
comp.isClicked = false;
@ -243,11 +293,30 @@ namespace SHADE
&& mousePos.y >= topExtent.y && mousePos.y <= btmExtent.y)
{
comp.isHovered = true;
#ifdef SHEDITOR
if (SHSystemManager::GetSystem<SHEditor>()->editorState == SHEditor::State::PLAY)
{
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);
}
}
#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
{
@ -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};
@ -296,7 +365,10 @@ namespace SHADE
result.y *= -1.0f;
result += camSize * 0.5f;
return result;
if (normalized)
return result / camSize;
else
return result;
}

View File

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