diff --git a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp index 96f1539d..6582ecf0 100644 --- a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp +++ b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp @@ -31,6 +31,8 @@ of DigiPen Institute of Technology is prohibited. #include "Assets/SHAssetMacros.h" #include "Tools/Utilities/SHExecUtilities.h" #include "SHVSUtilities.h" +#include "UI/Events/SHButtonClickEvent.h" +#include "UI/SHUIComponent.h" namespace SHADE { @@ -346,6 +348,21 @@ namespace SHADE return eventData->handle; } + SHEventHandle SHScriptEngine::onUIElementRemoved(SHEventPtr eventPtr) + { + auto eventData = reinterpret_cast*>(eventPtr.get()); + if (eventData->data->removedComponentType == ComponentFamily::GetID()) + csUIElementOnRemoved(eventData->data->eid); + return eventData->handle; + } + + SHEventHandle SHScriptEngine::onUIElementClicked(SHEventPtr eventPtr) + { + auto eventData = reinterpret_cast*>(eventPtr.get()); + csUIElementOnRemoved(eventData->data->EID); + return eventData->handle; + } + SHEventHandle SHScriptEngine::onSceneNodeChildrenAdded(SHEventPtr eventPtr) { auto eventData = reinterpret_cast*>(eventPtr.get()); @@ -490,6 +507,18 @@ namespace SHADE DEFAULT_CSHARP_NAMESPACE + ".ChildListCache", "OnChildrenChanged" ); + csUIElementOnRemoved = dotNet.GetFunctionPtr + ( + DEFAULT_CSHARP_LIB_NAME, + DEFAULT_CSHARP_NAMESPACE + ".UIElement", + "OnComponentRemoved" + ); + csUIElementOnClicked = dotNet.GetFunctionPtr + ( + DEFAULT_CSHARP_LIB_NAME, + DEFAULT_CSHARP_NAMESPACE + ".UIElement", + "OnClicked" + ); csEditorRenderScripts = dotNet.GetFunctionPtr ( DEFAULT_CSHARP_LIB_NAME, @@ -540,6 +569,18 @@ namespace SHADE }; SHEventManager::SubscribeTo(SH_COMPONENT_REMOVED_EVENT, std::dynamic_pointer_cast(removedColliderComponentEventReceiver)); + /* UI Element */ + std::shared_ptr> removedUIElementEventReceiver + { + std::make_shared>(this, &SHScriptEngine::onUIElementRemoved) + }; + SHEventManager::SubscribeTo(SH_COMPONENT_REMOVED_EVENT, std::dynamic_pointer_cast(removedUIElementEventReceiver)); + std::shared_ptr> clickedUIElementEventReceiver + { + std::make_shared>(this, &SHScriptEngine::onUIElementClicked) + }; + SHEventManager::SubscribeTo(SH_BUTTON_CLICK_EVENT, std::dynamic_pointer_cast(clickedUIElementEventReceiver)); + /* SceneGraph */ // Register for SceneNode child added event std::shared_ptr> addChildEventReceiver diff --git a/SHADE_Engine/src/Scripting/SHScriptEngine.h b/SHADE_Engine/src/Scripting/SHScriptEngine.h index 9710f5c5..5f104482 100644 --- a/SHADE_Engine/src/Scripting/SHScriptEngine.h +++ b/SHADE_Engine/src/Scripting/SHScriptEngine.h @@ -277,6 +277,8 @@ namespace SHADE CsEventRelayFuncPtr csColliderOnListChanged = nullptr; CsEventRelayFuncPtr csColliderOnRemoved = nullptr; CsEventRelayFuncPtr csSceneNodeChildrenChanged = nullptr; + CsEventRelayFuncPtr csUIElementOnRemoved = nullptr; + CsEventRelayFuncPtr csUIElementOnClicked = nullptr; // - Editor CsScriptEditorFuncPtr csEditorRenderScripts = nullptr; CsFuncPtr csEditorUndo = nullptr; @@ -289,6 +291,8 @@ namespace SHADE SHEventHandle onColliderAdded(SHEventPtr eventPtr); SHEventHandle onColliderRemoved(SHEventPtr eventPtr); SHEventHandle onColliderComponentRemoved(SHEventPtr eventPtr); + SHEventHandle onUIElementRemoved(SHEventPtr eventPtr); + SHEventHandle onUIElementClicked(SHEventPtr eventPtr); SHEventHandle onSceneNodeChildrenAdded(SHEventPtr eventPtr); SHEventHandle onSceneNodeChildrenRemoved(SHEventPtr eventPtr); diff --git a/SHADE_Managed/src/Components/UIElement.cxx b/SHADE_Managed/src/Components/UIElement.cxx new file mode 100644 index 00000000..d76d6d42 --- /dev/null +++ b/SHADE_Managed/src/Components/UIElement.cxx @@ -0,0 +1,75 @@ +/************************************************************************************//*! +\file UIElement.cxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Jan 30, 2023 +\brief Contains the definition of the functions of the managed UIElement class. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2023 DigiPen Institute of Technology. +Reproduction or disclosure of this file or its contents without the prior written consent +of DigiPen Institute of Technology is prohibited. +*//*************************************************************************************/ +// Precompiled Headers +#include "SHpch.h" +// Primary Header +#include "UIElement.hxx" +#include "Assets/NativeAsset.hxx" +#include "Utility/Convert.hxx" +#include "Utility/Debug.hxx" + +namespace SHADE +{ + /*---------------------------------------------------------------------------------*/ + /* Constructors */ + /*---------------------------------------------------------------------------------*/ + UIElement::UIElement(Entity entity) + : Component(entity) + {} + + /*---------------------------------------------------------------------------------*/ + /* Properties */ + /*---------------------------------------------------------------------------------*/ + CallbackEvent^ UIElement::OnClick::get() + { + // Create map if it wasn't before + if (onClickEventMap == nullptr) + { + onClickEventMap = gcnew System::Collections::Generic::Dictionary(); + } + + // Create event if it wasn't before + if (!onClickEventMap->ContainsKey(owner.EntityId)) + { + onClickEventMap->Add(owner.EntityId, gcnew CallbackEvent()); + } + + // Return the event + return onClickEventMap[owner.EntityId]; + } + + /*---------------------------------------------------------------------------------*/ + /* Event Handling Functions */ + /*-----------------------------------------------------------------------------a----*/ + void UIElement::OnComponentRemoved(EntityID entity) + { + SAFE_NATIVE_CALL_BEGIN + // Remove the event if it contained an event + if (onClickEventMap != nullptr && onClickEventMap->ContainsKey(entity)) + { + onClickEventMap->Remove(entity); + } + SAFE_NATIVE_CALL_END("UIElement.OnComponentRemoved") + } + void UIElement::OnClicked(EntityID entity) + { + SAFE_NATIVE_CALL_BEGIN + // Remove the event if it contained an event + if (onClickEventMap != nullptr && onClickEventMap->ContainsKey(entity)) + { + onClickEventMap[entity]->Invoke(); + } + SAFE_NATIVE_CALL_END("UIElement.OnClicked") + } +} diff --git a/SHADE_Managed/src/Components/UIElement.hxx b/SHADE_Managed/src/Components/UIElement.hxx new file mode 100644 index 00000000..a969e4b5 --- /dev/null +++ b/SHADE_Managed/src/Components/UIElement.hxx @@ -0,0 +1,75 @@ +/************************************************************************************//*! +\file UIElement.hxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Jan 30, 2023 +\brief Contains the definition of the managed UIElement class with the + declaration of functions for working with it. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2023 DigiPen Institute of Technology. +Reproduction or disclosure of this file or its contents without the prior written consent +of DigiPen Institute of Technology is prohibited. +*//*************************************************************************************/ +#pragma once + +// Project Includes +#include "Components/Component.hxx" +#include "Math/Vector3.hxx" +#include "Math/Quaternion.hxx" +// External Dependencies +#include "UI/SHUIComponent.h" + +namespace SHADE +{ + /// + /// CLR version of the SHADE Engine's SHUIComponent. + /// + public ref class UIElement : public Component + { + internal: + /*-----------------------------------------------------------------------------*/ + /* Constructors */ + /*-----------------------------------------------------------------------------*/ + /// + /// Constructs a UIElement Component that represents a native SHUIComponent + /// tied to the specified Entity. + /// + /// Entity that this Component will be tied to. + UIElement(Entity entity); + + public: + /*-----------------------------------------------------------------------------*/ + /* Properties */ + /*-----------------------------------------------------------------------------*/ + /// + /// Event that is raised when this UIElement is clicked. + /// + property CallbackEvent^ OnClick + { + CallbackEvent^ get(); + } + + private: + /*-----------------------------------------------------------------------------*/ + /* Event Handling Functions */ + /*-----------------------------------------------------------------------------*/ + /// + /// To be called from native code when this component is removed. + /// + /// The entity which has it's component removed. + static void OnComponentRemoved(EntityID entity); + /// + /// To be called from native code when this component is clicked. + /// + /// The entity which was clicked. + static void OnClicked(EntityID entity); + + /*-----------------------------------------------------------------------------*/ + /* Static Data Members */ + /*-----------------------------------------------------------------------------*/ + static System::Collections::Generic::Dictionary^ onClickEventMap; + }; +} + diff --git a/SHADE_Managed/src/Engine/ECS.cxx b/SHADE_Managed/src/Engine/ECS.cxx index c388f0cd..ecd3784d 100644 --- a/SHADE_Managed/src/Engine/ECS.cxx +++ b/SHADE_Managed/src/Engine/ECS.cxx @@ -29,6 +29,7 @@ of DigiPen Institute of Technology is prohibited. #include "Tools/Logger/SHLog.h" #include "Graphics\MiddleEnd\Interface\SHRenderable.h" #include "Graphics\MiddleEnd\TextRendering\SHTextRenderableComponent.h" +#include "UI\SHUIComponent.h" // Project Headers #include "Utility/Convert.hxx" #include "Utility/Debug.hxx" @@ -40,6 +41,7 @@ of DigiPen Institute of Technology is prohibited. #include "Components/Light.hxx" #include "Components\Renderable.hxx" #include "Components\TextRenderable.hxx" +#include "Components\UIElement.hxx" namespace SHADE { @@ -324,6 +326,7 @@ namespace SHADE componentMap.Add(createComponentSet()); componentMap.Add(createComponentSet()); componentMap.Add(createComponentSet()); + componentMap.Add(createComponentSet()); } /*---------------------------------------------------------------------------------*/