Added script mappings for new button events

This commit is contained in:
Kah Wei 2023-02-22 21:49:10 +08:00
parent 06ad2a4c0c
commit bea179fb0f
4 changed files with 195 additions and 0 deletions

View File

@ -369,6 +369,27 @@ namespace SHADE
return eventData->handle; return eventData->handle;
} }
SHEventHandle SHScriptEngine::onUIElementReleased(SHEventPtr eventPtr)
{
auto eventData = reinterpret_cast<const SHEventSpec<SHButtonClickEvent>*>(eventPtr.get());
csUIElementOnClicked(eventData->data->EID);
return eventData->handle;
}
SHEventHandle SHScriptEngine::onUIElementOnHoverEntered(SHEventPtr eventPtr)
{
auto eventData = reinterpret_cast<const SHEventSpec<SHButtonClickEvent>*>(eventPtr.get());
csUIElementOnClicked(eventData->data->EID);
return eventData->handle;
}
SHEventHandle SHScriptEngine::onUIElementOnHoverExited(SHEventPtr eventPtr)
{
auto eventData = reinterpret_cast<const SHEventSpec<SHButtonClickEvent>*>(eventPtr.get());
csUIElementOnClicked(eventData->data->EID);
return eventData->handle;
}
SHEventHandle SHScriptEngine::onSceneNodeChildrenAdded(SHEventPtr eventPtr) SHEventHandle SHScriptEngine::onSceneNodeChildrenAdded(SHEventPtr eventPtr)
{ {
auto eventData = reinterpret_cast<const SHEventSpec<SHSceneGraphAddChildEvent>*>(eventPtr.get()); auto eventData = reinterpret_cast<const SHEventSpec<SHSceneGraphAddChildEvent>*>(eventPtr.get());
@ -539,6 +560,24 @@ namespace SHADE
DEFAULT_CSHARP_NAMESPACE + ".UIElement", DEFAULT_CSHARP_NAMESPACE + ".UIElement",
"OnClicked" "OnClicked"
); );
csUIElementOnReleased = dotNet.GetFunctionPtr<CsEventRelayFuncPtr>
(
DEFAULT_CSHARP_LIB_NAME,
DEFAULT_CSHARP_NAMESPACE + ".UIElement",
"OnReleased"
);
csUIElementOnHoverEntered = dotNet.GetFunctionPtr<CsEventRelayFuncPtr>
(
DEFAULT_CSHARP_LIB_NAME,
DEFAULT_CSHARP_NAMESPACE + ".UIElement",
"OnHoverEntered"
);
csUIElementOnHoverExited = dotNet.GetFunctionPtr<CsEventRelayFuncPtr>
(
DEFAULT_CSHARP_LIB_NAME,
DEFAULT_CSHARP_NAMESPACE + ".UIElement",
"OnHoverExited"
);
csEditorRenderScripts = dotNet.GetFunctionPtr<CsScriptEditorFuncPtr> csEditorRenderScripts = dotNet.GetFunctionPtr<CsScriptEditorFuncPtr>
( (
DEFAULT_CSHARP_LIB_NAME, DEFAULT_CSHARP_LIB_NAME,
@ -608,6 +647,21 @@ namespace SHADE
std::make_shared<SHEventReceiverSpec<SHScriptEngine>>(this, &SHScriptEngine::onUIElementClicked) std::make_shared<SHEventReceiverSpec<SHScriptEngine>>(this, &SHScriptEngine::onUIElementClicked)
}; };
SHEventManager::SubscribeTo(SH_BUTTON_CLICK_EVENT, std::dynamic_pointer_cast<SHEventReceiver>(clickedUIElementEventReceiver)); SHEventManager::SubscribeTo(SH_BUTTON_CLICK_EVENT, std::dynamic_pointer_cast<SHEventReceiver>(clickedUIElementEventReceiver));
std::shared_ptr<SHEventReceiverSpec<SHScriptEngine>> releasedUIElementEventReceiver
{
std::make_shared<SHEventReceiverSpec<SHScriptEngine>>(this, &SHScriptEngine::onUIElementReleased)
};
SHEventManager::SubscribeTo(SH_BUTTON_RELEASE_EVENT, std::dynamic_pointer_cast<SHEventReceiver>(releasedUIElementEventReceiver));
std::shared_ptr<SHEventReceiverSpec<SHScriptEngine>> hoverEnterUIElementEventReceiver
{
std::make_shared<SHEventReceiverSpec<SHScriptEngine>>(this, &SHScriptEngine::onUIElementOnHoverEntered)
};
SHEventManager::SubscribeTo(SH_BUTTON_HOVER_ENTER_EVENT, std::dynamic_pointer_cast<SHEventReceiver>(hoverEnterUIElementEventReceiver));
std::shared_ptr<SHEventReceiverSpec<SHScriptEngine>> hoverExitedUIElementEventReceiver
{
std::make_shared<SHEventReceiverSpec<SHScriptEngine>>(this, &SHScriptEngine::onUIElementOnHoverExited)
};
SHEventManager::SubscribeTo(SH_BUTTON_HOVER_EXIT_EVENT, std::dynamic_pointer_cast<SHEventReceiver>(hoverExitedUIElementEventReceiver));
/* SceneGraph */ /* SceneGraph */
// Register for SceneNode child added event // Register for SceneNode child added event

View File

@ -292,6 +292,9 @@ namespace SHADE
CsEventRelayFuncPtr csSceneNodeChildrenChanged = nullptr; CsEventRelayFuncPtr csSceneNodeChildrenChanged = nullptr;
CsEventRelayFuncPtr csUIElementOnRemoved = nullptr; CsEventRelayFuncPtr csUIElementOnRemoved = nullptr;
CsEventRelayFuncPtr csUIElementOnClicked = nullptr; CsEventRelayFuncPtr csUIElementOnClicked = nullptr;
CsEventRelayFuncPtr csUIElementOnReleased = nullptr;
CsEventRelayFuncPtr csUIElementOnHoverEntered = nullptr;
CsEventRelayFuncPtr csUIElementOnHoverExited = nullptr;
// - Editor // - Editor
CsScriptEditorFuncPtr csEditorRenderScripts = nullptr; CsScriptEditorFuncPtr csEditorRenderScripts = nullptr;
CsFuncPtr csEditorUndo = nullptr; CsFuncPtr csEditorUndo = nullptr;
@ -306,6 +309,9 @@ namespace SHADE
SHEventHandle onColliderComponentRemoved(SHEventPtr eventPtr); SHEventHandle onColliderComponentRemoved(SHEventPtr eventPtr);
SHEventHandle onUIElementRemoved(SHEventPtr eventPtr); SHEventHandle onUIElementRemoved(SHEventPtr eventPtr);
SHEventHandle onUIElementClicked(SHEventPtr eventPtr); SHEventHandle onUIElementClicked(SHEventPtr eventPtr);
SHEventHandle onUIElementReleased(SHEventPtr eventPtr);
SHEventHandle onUIElementOnHoverEntered(SHEventPtr eventPtr);
SHEventHandle onUIElementOnHoverExited(SHEventPtr eventPtr);
SHEventHandle onSceneNodeChildrenAdded(SHEventPtr eventPtr); SHEventHandle onSceneNodeChildrenAdded(SHEventPtr eventPtr);
SHEventHandle onSceneNodeChildrenRemoved(SHEventPtr eventPtr); SHEventHandle onSceneNodeChildrenRemoved(SHEventPtr eventPtr);
SHEventHandle onSceneDestroyed(SHEventPtr eventPtr); SHEventHandle onSceneDestroyed(SHEventPtr eventPtr);

View File

@ -48,6 +48,57 @@ namespace SHADE
// Return the event // Return the event
return onClickEventMap[owner.EntityId]; return onClickEventMap[owner.EntityId];
} }
CallbackEvent^ UIElement::OnRelease::get()
{
// Create map if it wasn't before
if (onReleasedEventMap == nullptr)
{
onReleasedEventMap = gcnew System::Collections::Generic::Dictionary<Entity, CallbackEvent ^>();
}
// Create event if it wasn't before
if (!onReleasedEventMap->ContainsKey(owner.EntityId))
{
onReleasedEventMap->Add(owner.EntityId, gcnew CallbackEvent());
}
// Return the event
return onClickEventMap[owner.EntityId];
}
CallbackEvent^ UIElement::OnHoverEnter::get()
{
// Create map if it wasn't before
if (onHoverEnterEventMap == nullptr)
{
onHoverEnterEventMap = gcnew System::Collections::Generic::Dictionary<Entity, CallbackEvent ^>();
}
// Create event if it wasn't before
if (!onHoverEnterEventMap->ContainsKey(owner.EntityId))
{
onHoverEnterEventMap->Add(owner.EntityId, gcnew CallbackEvent());
}
// Return the event
return onHoverEnterEventMap[owner.EntityId];
}
CallbackEvent^ UIElement::OnHoverExit::get()
{
// Create map if it wasn't before
if (onHoverExitEventMap == nullptr)
{
onHoverExitEventMap = gcnew System::Collections::Generic::Dictionary<Entity, CallbackEvent ^>();
}
// Create event if it wasn't before
if (!onHoverExitEventMap->ContainsKey(owner.EntityId))
{
onHoverExitEventMap->Add(owner.EntityId, gcnew CallbackEvent());
}
// Return the event
return onHoverExitEventMap[owner.EntityId];
}
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Event Handling Functions */ /* Event Handling Functions */
@ -60,6 +111,18 @@ namespace SHADE
{ {
onClickEventMap->Remove(entity); onClickEventMap->Remove(entity);
} }
if (onReleasedEventMap != nullptr && onReleasedEventMap->ContainsKey(entity))
{
onReleasedEventMap->Remove(entity);
}
if (onHoverEnterEventMap != nullptr && onHoverEnterEventMap->ContainsKey(entity))
{
onHoverEnterEventMap->Remove(entity);
}
if (onHoverExitEventMap != nullptr && onHoverExitEventMap->ContainsKey(entity))
{
onHoverExitEventMap->Remove(entity);
}
SAFE_NATIVE_CALL_END("UIElement.OnComponentRemoved") SAFE_NATIVE_CALL_END("UIElement.OnComponentRemoved")
} }
void UIElement::OnClicked(EntityID entity) void UIElement::OnClicked(EntityID entity)
@ -72,4 +135,34 @@ namespace SHADE
} }
SAFE_NATIVE_CALL_END("UIElement.OnClicked") SAFE_NATIVE_CALL_END("UIElement.OnClicked")
} }
void UIElement::OnReleased(EntityID entity)
{
SAFE_NATIVE_CALL_BEGIN
// Remove the event if it contained an event
if (onReleasedEventMap != nullptr && onReleasedEventMap->ContainsKey(entity))
{
onReleasedEventMap[entity]->Invoke();
}
SAFE_NATIVE_CALL_END("UIElement.OnClicked")
}
void UIElement::OnHoverEntered(EntityID entity)
{
SAFE_NATIVE_CALL_BEGIN
// Remove the event if it contained an event
if (onHoverEnterEventMap != nullptr && onHoverEnterEventMap->ContainsKey(entity))
{
onHoverEnterEventMap[entity]->Invoke();
}
SAFE_NATIVE_CALL_END("UIElement.OnClicked")
}
void UIElement::OnHoverExited(EntityID entity)
{
SAFE_NATIVE_CALL_BEGIN
// Remove the event if it contained an event
if (onHoverExitEventMap != nullptr && onHoverExitEventMap->ContainsKey(entity))
{
onHoverExitEventMap[entity]->Invoke();
}
SAFE_NATIVE_CALL_END("UIElement.OnClicked")
}
} }

View File

@ -50,6 +50,28 @@ namespace SHADE
{ {
CallbackEvent^ get(); CallbackEvent^ get();
} }
/// <summary>
/// Event that is raised when this UIElement is released.
/// </summary>
property CallbackEvent^ OnRelease
{
CallbackEvent^ get();
}
/// <summary>
/// Event that is raised on the first frame when this UIElement is hovered over.
/// </summary>
property CallbackEvent^ OnHoverEnter
{
CallbackEvent^ get();
}
/// <summary>
/// Event that is raised on the first frame when this UIElement is no longer
/// hovered over.
/// </summary>
property CallbackEvent^ OnHoverExit
{
CallbackEvent^ get();
}
private: private:
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
@ -65,11 +87,31 @@ namespace SHADE
/// </summary> /// </summary>
/// <param name="entity">The entity which was clicked.</param> /// <param name="entity">The entity which was clicked.</param>
static void OnClicked(EntityID entity); static void OnClicked(EntityID entity);
/// <summary>
/// To be called from native code when this component is released from clicking.
/// </summary>
/// <param name="entity">The entity which was clicked.</param>
static void OnReleased(EntityID entity);
/// <summary>
/// To be called from native code on the first frame that this component is
/// hovered on.
/// </summary>
/// <param name="entity">The entity which was clicked.</param>
static void OnHoverEntered(EntityID entity);
/// <summary>
/// To be called from native code on the first frame that this component is
/// no longer hovered on.
/// </summary>
/// <param name="entity">The entity which was clicked.</param>
static void OnHoverExited(EntityID entity);
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Static Data Members */ /* Static Data Members */
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
static System::Collections::Generic::Dictionary<Entity, CallbackEvent^>^ onClickEventMap; static System::Collections::Generic::Dictionary<Entity, CallbackEvent^>^ onClickEventMap;
static System::Collections::Generic::Dictionary<Entity, CallbackEvent^>^ onReleasedEventMap;
static System::Collections::Generic::Dictionary<Entity, CallbackEvent^>^ onHoverEnterEventMap;
static System::Collections::Generic::Dictionary<Entity, CallbackEvent^>^ onHoverExitEventMap;
}; };
} }