Added changing texture of buttons

This commit is contained in:
maverickdgg 2023-01-16 14:35:16 +08:00
parent 02ba0c6dc9
commit a41354f2ce
5 changed files with 89 additions and 42 deletions

View File

@ -1,4 +1,4 @@
Start in Fullscreen: false
Starting Scene ID: 97158628
Starting Scene ID: 87244611
Window Size: {x: 1920, y: 1080}
Window Title: SHADE Engine

View File

@ -14,7 +14,7 @@
NumberOfChildren: 0
Components:
Transform Component:
Translate: {x: -3.5999999, y: 3.0999999, z: 0}
Translate: {x: 0, y: 0, z: 0}
Rotate: {x: 0, y: 0, z: 0}
Scale: {x: 1, y: 1, z: 1}
IsActive: true

View File

@ -32,8 +32,10 @@ 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;

View File

@ -33,7 +33,10 @@ 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;

View File

@ -9,6 +9,7 @@
#include "Editor/EditorWindow/ViewportWindow/SHEditorViewport.h"
#include "Editor/SHEditor.h"
#include "Resource/SHResourceManager.h"
#include "Assets/SHAssetManager.h"
#include "Input/SHInputManager.h"
#include "SHUIComponent.h"
#include "SHButtonComponent.h"
@ -168,9 +169,9 @@ namespace SHADE
windowSize = SHEditorWindowManager::GetEditorWindow<SHEditorViewport>()->beginContentRegionAvailable;
mousePos = SHEditorWindowManager::GetEditorWindow<SHEditorViewport>()->viewportMousePos;
//mousePos.y = windowSize.y - mousePos.y;
SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y)
//SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y)
mousePos /= windowSize;
SHLOG_INFO("mouse pos normalized: {}, {}", mousePos.x, mousePos.y)
//SHLOG_INFO("mouse pos normalized: {}, {}", mousePos.x, mousePos.y)
@ -193,31 +194,25 @@ namespace SHADE
//SHLOG_INFO("TopExtent: {}, {} Btm Extent: {}, {}", topExtent.x, topExtent.y, btmExtent.x, btmExtent.y)
comp.isClicked = false;
//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)
#ifdef SHEDITOR
//if (SHSystemManager::GetSystem<SHEditor>()->editorState == SHEditor::State::PLAY)
{
if (SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB))
if (SHInputManager::GetKeyDown(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))
#else
if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::LMB))
{
comp.isClicked = true;
SHButtonClickEvent clickEvent;
clickEvent.EID = comp.GetEID();
SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT);
}
#endif
#endif
//SHLOG_INFO("HOVERED")
}
@ -226,15 +221,46 @@ namespace SHADE
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);
}
if (SHComponentManager::HasComponent<SHRenderable>(comp.GetEID()))
{
/*auto renderable = SHComponentManager::GetComponent_s<SHRenderable>(comp.GetEID());
auto renderable = SHComponentManager::GetComponent_s<SHRenderable>(comp.GetEID());
auto texture = SHResourceManager::Get<SHTexture>(comp.GetDefaultTexture());
auto material = renderable->GetModifiableMaterial();
material->SetProperty("texture", comp.GetDefaultTexture());*/
if(!comp.isHovered && !comp.isClicked)
if (SHAssetManager::GetType(comp.GetDefaultTexture()) == AssetType::TEXTURE)
{
material->SetProperty("data.textureIndex", comp.GetDefaultTexture());
//SHLOG_INFO("SETTING DEFAULT TEXTURE")
}
if (comp.isHovered)
{
if (SHAssetManager::GetType(comp.GetHoveredTexture()) == AssetType::TEXTURE)
{
material->SetProperty("data.textureIndex", comp.GetHoveredTexture());
//SHLOG_INFO("SETTING HOVERED TEXTURE")
}
}
else
{
if (SHAssetManager::GetType(comp.GetClickedTexture()) == AssetType::TEXTURE)
{
material->SetProperty("data.textureIndex", comp.GetClickedTexture());
SHLOG_INFO("SETTING CLICKED TEXTURE")
}
}
}
}
@ -262,9 +288,9 @@ namespace SHADE
windowSize = SHEditorWindowManager::GetEditorWindow<SHEditorViewport>()->beginContentRegionAvailable;
mousePos = SHEditorWindowManager::GetEditorWindow<SHEditorViewport>()->viewportMousePos;
//mousePos.y = windowSize.y - mousePos.y;
SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y)
//SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y)
mousePos /= windowSize;
SHLOG_INFO("mouse pos normalized: {}, {}", mousePos.x, mousePos.y)
//SHLOG_INFO("mouse pos normalized: {}, {}", mousePos.x, mousePos.y)
@ -296,25 +322,15 @@ namespace SHADE
#ifdef SHEDITOR
if (SHSystemManager::GetSystem<SHEditor>()->editorState == SHEditor::State::PLAY)
{
if (SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB))
if (SHInputManager::GetKeyDown(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))
if (SHInputManager::GetKeyDown(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
}
@ -324,13 +340,39 @@ namespace SHADE
}
if (comp.isClicked && SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB))
{
comp.isClicked = false;
comp.value = !comp.value;
SHButtonClickEvent clickEvent;
clickEvent.EID = comp.GetEID();
clickEvent.value = comp.value;
SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT);
}
if (SHComponentManager::HasComponent<SHRenderable>(comp.GetEID()))
{
/*auto renderable = SHComponentManager::GetComponent_s<SHRenderable>(comp.GetEID());
auto renderable = SHComponentManager::GetComponent_s<SHRenderable>(comp.GetEID());
auto texture = SHResourceManager::Get<SHTexture>(comp.GetDefaultTexture());
auto material = renderable->GetModifiableMaterial();
material->SetProperty("texture", comp.GetDefaultTexture());*/
if (comp.GetValue() == false)
{
if (SHAssetManager::GetType(comp.GetDefaultTexture()) == AssetType::TEXTURE)
{
material->SetProperty("data.textureIndex", comp.GetDefaultTexture());
//SHLOG_INFO("SETTING DEFAULT TEXTURE")
}
}
else
{
if (SHAssetManager::GetType(comp.GetToggledTexture()) == AssetType::TEXTURE)
{
material->SetProperty("data.textureIndex", comp.GetToggledTexture());
//SHLOG_INFO("SETTING DEFAULT TEXTURE")
}
}
}
}