From 9896c5c913e237188c7c417a5b272aa85b43547a Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Tue, 20 Sep 2022 16:23:03 +0800 Subject: [PATCH] Added CallbackAction and CallbackEvent --- SHADE_Managed/src/Events/CallbackAction.cxx | 48 ++++++++++++ SHADE_Managed/src/Events/CallbackAction.hxx | 57 ++++++++++++++ SHADE_Managed/src/Events/CallbackEvent.cxx | 71 +++++++++++++++++ SHADE_Managed/src/Events/CallbackEvent.hxx | 85 +++++++++++++++++++++ SHADE_Managed/src/Math/Vector2.hxx | 2 +- 5 files changed, 262 insertions(+), 1 deletion(-) create mode 100644 SHADE_Managed/src/Events/CallbackAction.cxx create mode 100644 SHADE_Managed/src/Events/CallbackAction.hxx create mode 100644 SHADE_Managed/src/Events/CallbackEvent.cxx create mode 100644 SHADE_Managed/src/Events/CallbackEvent.hxx diff --git a/SHADE_Managed/src/Events/CallbackAction.cxx b/SHADE_Managed/src/Events/CallbackAction.cxx new file mode 100644 index 00000000..0ad356c1 --- /dev/null +++ b/SHADE_Managed/src/Events/CallbackAction.cxx @@ -0,0 +1,48 @@ +/************************************************************************************//*! +\file CallbackAction.cxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Sep 20, 2022 +\brief Contains the definition of the functions for the CallbackAction managed + class. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2021 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 "CallbackAction.hxx" + +namespace SHADE +{ + /*---------------------------------------------------------------------------------*/ + /* Constructors */ + /*---------------------------------------------------------------------------------*/ + generic + CallbackAction::CallbackAction(System::Reflection::MethodInfo^ method) + : CallbackAction(method, nullptr) + {} + generic + CallbackAction::CallbackAction(System::Reflection::MethodInfo^ method, System::Object^ obj) + : method { method } + , object { obj } + { + paramArray = gcnew cli::array(1); + } + + /*---------------------------------------------------------------------------------*/ + /* Usage Functions */ + /*---------------------------------------------------------------------------------*/ + generic + void CallbackAction::Invoke(T1 param1) + { + // Set parameters + paramArray[0] = safe_cast(param1); + // Invoke + method->Invoke(object, paramArray); + } +} diff --git a/SHADE_Managed/src/Events/CallbackAction.hxx b/SHADE_Managed/src/Events/CallbackAction.hxx new file mode 100644 index 00000000..f8b50266 --- /dev/null +++ b/SHADE_Managed/src/Events/CallbackAction.hxx @@ -0,0 +1,57 @@ +/************************************************************************************//*! +\file CallbackAction.hxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Sep 20, 2021 +\brief Contains the definition of the managed CallbackAction class. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2022 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 + +namespace SHADE +{ + /// + /// Holds a function and their associated object for invoking later. + /// + /// Type of the first parameter. + generic + public ref class CallbackAction + { + public: + /*-----------------------------------------------------------------------------*/ + /* Constructors */ + /*-----------------------------------------------------------------------------*/ + /// + /// Constructs a CallbackAction with the specified static method. + /// + /// MethodInfo representing the method to call. + /// Object to call the method on. Set null if static. + CallbackAction(System::Reflection::MethodInfo^ method); + /// + /// Constructs a CallbackAction with the specified instance method and object + /// which will invoke the method. + /// + /// MethodInfo representing the method to call. + /// Object to call the method on. + CallbackAction(System::Reflection::MethodInfo^ method, System::Object^ obj); + + /*-----------------------------------------------------------------------------*/ + /* Usage Functions */ + /*-----------------------------------------------------------------------------*/ + /// + /// Invokes this action with the specified parameters. + /// + /// + void Invoke(T1 param1); + + private: + System::Reflection::MethodInfo^ method; + cli::array^ paramArray; + System::Object^ object; + }; +} \ No newline at end of file diff --git a/SHADE_Managed/src/Events/CallbackEvent.cxx b/SHADE_Managed/src/Events/CallbackEvent.cxx new file mode 100644 index 00000000..9ba82ef2 --- /dev/null +++ b/SHADE_Managed/src/Events/CallbackEvent.cxx @@ -0,0 +1,71 @@ +/************************************************************************************//*! +\file CallbackEvent.cxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Sep 20, 2022 +\brief Contains the definition of the functions for the CallbackEvent managed + class. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2021 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 "CallbackEvent.hxx" + +namespace SHADE +{ + /*---------------------------------------------------------------------------------*/ + /* Constructors */ + /*---------------------------------------------------------------------------------*/ + generic + CallbackEvent::CallbackEvent() + { + actions = gcnew System::Collections::Generic::List ^>(); + } + + /*---------------------------------------------------------------------------------*/ + /* Properties */ + /*---------------------------------------------------------------------------------*/ + generic + System::Collections::Generic::IEnumerable^>^ CallbackEvent::Actions::get() + { + return actions; + } + + /*---------------------------------------------------------------------------------*/ + /* Usage Functions */ + /*---------------------------------------------------------------------------------*/ + generic + void CallbackEvent::AddAction(CallbackAction^ action) + { + actions->Add(action); + } + generic + void CallbackEvent::AddAction(System::Reflection::MethodInfo^ method) + { + actions->Add(gcnew CallbackAction(method)); + } + generic + void CallbackEvent::AddAction(System::Reflection::MethodInfo^ method, System::Object^ object) + { + actions->Add(gcnew CallbackAction(method, object)); + } + generic + void CallbackEvent::RemoveAction(CallbackAction^ action) + { + actions->Remove(action); + } + generic + void CallbackEvent::Invoke(T1 param1) + { + for each (CallbackAction^ action in actions) + { + action->Invoke(param1); + } + } +} diff --git a/SHADE_Managed/src/Events/CallbackEvent.hxx b/SHADE_Managed/src/Events/CallbackEvent.hxx new file mode 100644 index 00000000..d0d6d2f1 --- /dev/null +++ b/SHADE_Managed/src/Events/CallbackEvent.hxx @@ -0,0 +1,85 @@ +/************************************************************************************//*! +\file CallbackEvent.hxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Sep 20, 2021 +\brief Contains the definition of the managed CallbackEvent class. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2022 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 + +#include "CallbackAction.hxx" + +namespace SHADE +{ + /// + /// Holds a set of CallbackActions that can be invoked together at a later time. + /// + /// Type of the first parameter. + generic + public ref class CallbackEvent + { + public: + /*-----------------------------------------------------------------------------*/ + /* Constructors */ + /*-----------------------------------------------------------------------------*/ + /// + /// Default Constructor + /// + CallbackEvent(); + + /*-----------------------------------------------------------------------------*/ + /* Properties */ + /*-----------------------------------------------------------------------------*/ + /// + /// Read only access to the list of Actions + /// + property System::Collections::Generic::IEnumerable^>^ Actions + { + System::Collections::Generic::IEnumerable^>^ get(); + } + + /*-----------------------------------------------------------------------------*/ + /* Usage Functions */ + /*-----------------------------------------------------------------------------*/ + /// + /// Adds the specified CallbackAction to the CallbackEvent. + /// + /// CallbackAction to add. + void AddAction(CallbackAction^ action); + /// + /// Adds a specified static method as a CallbackAction to the CallbackEvent. + /// + /// Method to add. + void AddAction(System::Reflection::MethodInfo^ method); + /// + /// Adds a specified instance method and associated object as a CallbackAction to + /// the CallbackEvent. + /// + /// Method to add. + /// Object that will call the specified method. + void AddAction(System::Reflection::MethodInfo^ method, System::Object^ object); + /// + /// Removes the specified CallbackAction from the CallbackEvent. + /// + /// CallbackAction to remove. + void RemoveAction(CallbackAction^ action); + /// + /// Invokes all CallbackActions registered with this CallbackEvent with the + /// specified parameters. + /// + /// + void Invoke(T1 param1); + + private: + /*-----------------------------------------------------------------------------*/ + /* Data Members */ + /*-----------------------------------------------------------------------------*/ + System::Collections::Generic::List^>^ actions; + }; +} \ No newline at end of file diff --git a/SHADE_Managed/src/Math/Vector2.hxx b/SHADE_Managed/src/Math/Vector2.hxx index 69a6110f..9253a703 100644 --- a/SHADE_Managed/src/Math/Vector2.hxx +++ b/SHADE_Managed/src/Math/Vector2.hxx @@ -19,7 +19,7 @@ of DigiPen Institute of Technology is prohibited. namespace SHADE { /// - /// CLR version of the the PlushieEngine's Vector2 class that represents a + /// CLR version of the the SHADE Engine's Vector2 class that represents a /// 2-Dimensional Vector. Designed to closely match Unity's Vector2 struct. /// [System::Runtime::InteropServices::StructLayout(System::Runtime::InteropServices::LayoutKind::Sequential)]