Skip to content

Commit

Permalink
Modified event listener to pass event info by reference, etc...
Browse files Browse the repository at this point in the history
Modified event listener to pass event info by reference,
Allow listener to modify event info to request deleting listener
  • Loading branch information
Neko-Box-Coder committed Jul 22, 2023
1 parent 5cae4ba commit cda8a64
Show file tree
Hide file tree
Showing 62 changed files with 115 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ int main()
buttonEventCallback->AddEventListener
(
"listenerKey",
[textId](ssGUI::EventInfo info)
[textId](ssGUI::EventInfo& info)
{
auto* textToChange = info.References->GetObjectReference<ssGUI::Text>(textId);
auto* currentButton = static_cast<ssGUI::StandardButton*>(info.Container);
Expand Down
4 changes: 4 additions & 0 deletions Include/ssGUI/DataClasses/EventInfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ namespace ssGUI
//var: References
//<ObjectsReferences> that this event callback has
ObjectsReferences* References = nullptr;

//var: DeleteCurrentListener
//If current listener is not needed, setting it to true will delete the current listener.
bool DeleteCurrentListener = false;

//var: CustomInfo
void* CustomInfo = nullptr;
Expand Down
1 change: 1 addition & 0 deletions Include/ssGUI/Enums/EventType.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define SSGUI_EVENT_TYPE_H

#include "ssGUI/HelperClasses/EnumToStringMacro.hpp"
#include <cstdint>
#include <string>

//namespace: ssGUI
Expand Down
10 changes: 5 additions & 5 deletions Include/ssGUI/EventCallback.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace ssGUI
"AnyKey",
//source is what *triggered* the callback
//container is the GUI Object that holds this event callback
[](ssGUI::EventInfo info)
[](ssGUI::EventInfo& info)
{
//Do something
}
Expand All @@ -36,7 +36,7 @@ namespace ssGUI
============================== C++ ==============================
private:
//Events
std::unordered_map<std::string, std::function<void(EventInfo)>> EventListeners; //See <AddEventListener>
std::unordered_map<std::string, std::function<void(EventInfo&)>> EventListeners; //See <AddEventListener>
std::vector<std::string> EventListenersOrder; //See <GetEventListenerOrder>
ssGUI::GUIObject* Container; //See <BindToObject>
ssGUI::ObjectsReferences CurrentObjectsReferences; //See <GetObjectReference> and <Internal_GetObjectsReferences>
Expand All @@ -58,7 +58,7 @@ namespace ssGUI

private:
//Events
std::unordered_map<std::string, std::function<void(EventInfo)>> EventListeners; //See <AddEventListener>
std::unordered_map<std::string, std::function<void(EventInfo&)>> EventListeners; //See <AddEventListener>
std::vector<std::string> EventListenersOrder; //See <GetEventListenerOrder>
ssGUI::GUIObject* Container; //See <BindToObject>
ssGUI::ObjectsReferences CurrentObjectsReferences; //See <GetObjectReference> and <Internal_GetObjectsReferences>
Expand All @@ -77,11 +77,11 @@ namespace ssGUI
public:
//function: AddEventListener
//Adds a callback listener function with key and adder for unique identification which triggers when <Notify> is called
virtual void AddEventListener(std::string key, ssGUI::GUIObject* adder, std::function<void(EventInfo)> callback);
virtual void AddEventListener(std::string key, ssGUI::GUIObject* adder, std::function<void(EventInfo&)> callback);

//function: AddEventListener
//See <AddEventListener> where adder is nullptr
virtual void AddEventListener(std::string key, std::function<void(EventInfo)> callback);
virtual void AddEventListener(std::string key, std::function<void(EventInfo&)> callback);

//function: IsEventListenerExist
//Checks if the callback listener function with unique identification key and adder exists
Expand Down
2 changes: 1 addition & 1 deletion Include/ssGUI/GUIObjectClasses/Button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace ssGUI
auto stateChangedEventCallback = AddEventCallback(ssGUI::Enums::EventType::BUTTON_STATE_CHANGED);
stateChangedEventCallback->AddEventListener(
ListenerKey, this,
[](ssGUI::EventInfo info)
[](ssGUI::EventInfo& info)
{
ssGUI::Button* btn = static_cast<ssGUI::Button*>(info.EventSource);
glm::u8vec4 btnColor = btn->GetButtonColor();
Expand Down
2 changes: 1 addition & 1 deletion Include/ssGUI/GUIObjectClasses/Checkbox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace ssGUI
buttonEvent->ClearEventListeners();
buttonEvent->AddEventListener(
ListenerKey, this,
[](ssGUI::EventInfo info)
[](ssGUI::EventInfo& info)
{
ssGUI::Checkbox* btn = static_cast<ssGUI::Checkbox*>(info.EventSource);
glm::u8vec4 bgcolor = btn->GetBackgroundColor();
Expand Down
4 changes: 2 additions & 2 deletions Include/ssGUI/GUIObjectClasses/CompositeClasses/Dropdown.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ namespace ssGUI
(
ListenerKey,
this,
[](ssGUI::EventInfo info)
[](ssGUI::EventInfo& info)
{
auto dropdownContainer = static_cast<ssGUI::Dropdown*>(info.Container);
Expand Down Expand Up @@ -118,7 +118,7 @@ namespace ssGUI
(
ListenerKey,
this,
[](ssGUI::EventInfo info)
[](ssGUI::EventInfo& info)
{
auto* dropdown = static_cast<ssGUI::Dropdown*>(info.Container);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ namespace ssGUI
ecb->AddEventListener
(
ListenerKey, this,
[index](ssGUI::EventInfo info)
[index](ssGUI::EventInfo& info)
{
auto imageCanvas = static_cast<ssGUI::ImageCanvas*>(info.References->GetObjectReference(index));
if(imageCanvas == nullptr)
Expand Down Expand Up @@ -139,7 +139,7 @@ namespace ssGUI
ecb->AddEventListener
(
ListenerKey, this,
[index](ssGUI::EventInfo info)
[index](ssGUI::EventInfo& info)
{
auto imageCanvas = static_cast<ssGUI::ImageCanvas*>(info.References->GetObjectReference(index));
if(imageCanvas == nullptr)
Expand Down Expand Up @@ -179,7 +179,7 @@ namespace ssGUI
(
ListenerKey,
this,
[](ssGUI::EventInfo info)
[](ssGUI::EventInfo& info)
{
auto* imageCanvas = static_cast<ssGUI::ImageCanvas*>(info.Container);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace ssGUI
buttonEventCallback->AddEventListener
(
ListenerKey, this,
[](ssGUI::EventInfo info)
[](ssGUI::EventInfo& info)
{
ssGUI::StandardButton* btn = static_cast<ssGUI::StandardButton*>(info.EventCallbackContainer);
int buttonReactAmount = 20;
Expand Down
2 changes: 1 addition & 1 deletion Include/ssGUI/GUIObjectClasses/CompositeClasses/Slider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ namespace ssGUI
ecb->AddEventListener
(
ListenerKey, this,
[](ssGUI::EventInfo info)
[](ssGUI::EventInfo& info)
{
ssGUI::Button* btn = static_cast<ssGUI::Button*>(info.EventSource);
glm::u8vec4 btnColor = btn->GetButtonColor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ namespace ssGUI
buttonEventCallback->AddEventListener
(
ListenerKey, this,
[](ssGUI::EventInfo info)
[](ssGUI::EventInfo& info)
{
ssGUI::StandardButton* btn = static_cast<ssGUI::StandardButton*>(info.Container);
auto iconImage = btn->GetButtonIconObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ namespace ssGUI
buttonEvent->AddEventListener
(
ListenerKey, this,
[circleId](ssGUI::EventInfo info)
[circleId](ssGUI::EventInfo& info)
{
auto closeButtonObj = static_cast<ssGUI::Button*>(info.EventSource);
auto shape = static_cast<ssGUI::Extensions::Shape*>(info.EventSource->GetExtension(ssGUI::Extensions::Shape::EXTENSION_NAME));
Expand Down Expand Up @@ -138,7 +138,7 @@ namespace ssGUI
shapeEvent->AddEventListener
(
ListenerKey, this,
[circleId](ssGUI::EventInfo info)
[circleId](ssGUI::EventInfo& info)
{
auto shape = static_cast<ssGUI::Extensions::Shape*>(info.EventSource->GetExtension(ssGUI::Extensions::Shape::EXTENSION_NAME));
shape->SetAdditionalCircle(circleId, glm::vec2(), info.EventSource->GetSize(), glm::u8vec4(255, 127, 127, 255), false);
Expand Down Expand Up @@ -179,7 +179,7 @@ namespace ssGUI
(
ListenerKey,
this,
[](ssGUI::EventInfo info)
[](ssGUI::EventInfo& info)
{
auto* standardWindow = static_cast<ssGUI::StandardWindow*>(info.Container);
Expand Down
2 changes: 1 addition & 1 deletion Include/ssGUI/GUIObjectClasses/Image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace ssGUI
(
ListenerKey,
this,
[](ssGUI::EventInfo info)
[](ssGUI::EventInfo& info)
{
auto* image = static_cast<ssGUI::Image*>(info.Container);
Expand Down
4 changes: 2 additions & 2 deletions Include/ssGUI/GUIObjectClasses/Text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ namespace ssGUI
sizeChangedCallback->AddEventListener
(
ListenerKey, this,
[](ssGUI::EventInfo info)
[](ssGUI::EventInfo& info)
{
static_cast<ssGUI::Text*>(info.EventSource)->RecalculateTextNeeded = true;
}
Expand All @@ -109,7 +109,7 @@ namespace ssGUI
(
ListenerKey,
this,
[](ssGUI::EventInfo info)
[](ssGUI::EventInfo& info)
{
auto* text = static_cast<ssGUI::Text*>(info.Container);
Expand Down
2 changes: 1 addition & 1 deletion Src/Examples/CloningExampleV3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ int main()
buttonEventCallback->AddEventListener
(
"listenerKey",
[textId](ssGUI::EventInfo info)
[textId](ssGUI::EventInfo& info)
{
auto* textToChange = info.References->GetObjectReference<ssGUI::Text>(textId);
auto* currentButton = static_cast<ssGUI::StandardButton*>(info.Container);
Expand Down
2 changes: 1 addition & 1 deletion Src/Examples/EventCallbackExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ int main()
callback->AddEventListener
(
"AnyKey",
[](ssGUI::EventInfo info)
[](ssGUI::EventInfo& info)
{
std::cout<<"child added\n";
}
Expand Down
8 changes: 4 additions & 4 deletions Src/Examples/ExtensionExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int main()
borderButton->GetEventCallback(ssGUI::Enums::EventType::BUTTON_STATE_CHANGED)->AddEventListener
(
"AnyKey",
[&](ssGUI::EventInfo info)
[&](ssGUI::EventInfo& info)
{
if(static_cast<ssGUI::Button*>(info.Container)->GetButtonState() == ssGUI::Enums::ButtonState::CLICKED)
{
Expand All @@ -61,7 +61,7 @@ int main()
boxShadowButton->GetEventCallback(ssGUI::Enums::EventType::BUTTON_STATE_CHANGED)->AddEventListener
(
"AnyKey",
[&](ssGUI::EventInfo info)
[&](ssGUI::EventInfo& info)
{
if(static_cast<ssGUI::Button*>(info.Container)->GetButtonState() == ssGUI::Enums::ButtonState::CLICKED)
{
Expand All @@ -78,7 +78,7 @@ int main()
roundedCornersButton->GetEventCallback(ssGUI::Enums::EventType::BUTTON_STATE_CHANGED)->AddEventListener
(
"AnyKey",
[&](ssGUI::EventInfo info)
[&](ssGUI::EventInfo& info)
{
if(static_cast<ssGUI::Button*>(info.Container)->GetButtonState() == ssGUI::Enums::ButtonState::CLICKED)
{
Expand Down Expand Up @@ -108,7 +108,7 @@ int main()
outlineButton->GetEventCallback(ssGUI::Enums::EventType::BUTTON_STATE_CHANGED)->AddEventListener
(
"AnyKey",
[&](ssGUI::EventInfo info)
[&](ssGUI::EventInfo& info)
{
if(static_cast<ssGUI::Button*>(info.Container)->GetButtonState() == ssGUI::Enums::ButtonState::CLICKED)
{
Expand Down
2 changes: 1 addition & 1 deletion Src/Examples/ShowMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ int main()
button.GetEventCallback(ssGUI::Enums::EventType::BUTTON_STATE_CHANGED)->AddEventListener
(
"AnyKey",
[&](ssGUI::EventInfo info)
[&](ssGUI::EventInfo& info)
{
if(((ssGUI::Button*)info.EventSource)->GetButtonState() == ssGUI::Enums::ButtonState::CLICKED)
{
Expand Down
2 changes: 1 addition & 1 deletion Src/Examples/TextAlignmentExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ int main()
button->GetEventCallback(ssGUI::Enums::EventType::BUTTON_STATE_CHANGED)->AddEventListener
(
"AnyKey",
[&text, i](ssGUI::EventInfo info)
[&text, i](ssGUI::EventInfo& info)
{
if(((ssGUI::Button*)info.EventSource)->GetButtonState() != ssGUI::Enums::ButtonState::CLICKED)
return;
Expand Down
2 changes: 1 addition & 1 deletion Src/Examples/old/CloningExampleV2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ int main()
buttonEventCallback->AddEventListener
(
"listenerKey",
[textId](ssGUI::EventInfo info)
[textId](ssGUI::EventInfo& info)
{
auto* textToChange = info.References->GetObjectReference<ssGUI::Text>(textId);
auto* currentButton = static_cast<ssGUI::StandardButton*>(info.Container);
Expand Down
2 changes: 1 addition & 1 deletion Src/Examples/old/CloningExampleWithOR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int main()
ecb->AddEventListener
(
"AnyKey",
[textIndex](ssGUI::EventInfo info)
[textIndex](ssGUI::EventInfo& info)
{
//When the button is clicked, sets the text
if(static_cast<ssGUI::Button*>(info.Container)->GetButtonState() == ssGUI::Enums::ButtonState::CLICKED)
Expand Down
4 changes: 2 additions & 2 deletions Src/Examples/old/CustomGraphicsExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ int main()
ecb->AddEventListener
(
"pyramidControl",
[&](ssGUI::EventInfo info)
[&](ssGUI::EventInfo& info)
{
PyramidRotateSpeed = static_cast<ssGUI::Slider*>(info.EventCallbackContainer)->GetSliderValue() * 3;
}
Expand All @@ -208,7 +208,7 @@ int main()
ecb->AddEventListener
(
"cubeControl",
[&](ssGUI::EventInfo info)
[&](ssGUI::EventInfo& info)
{
CubeRotateSpeed = static_cast<ssGUI::Slider*>(info.EventCallbackContainer)->GetSliderValue() * 3;
}
Expand Down
2 changes: 1 addition & 1 deletion Src/Examples/old/ReadmeExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ int main()
button.GetEventCallback(ssGUI::Enums::EventType::BUTTON_STATE_CHANGED)->AddEventListener
(
"AnyKey",
[&](ssGUI::EventInfo info)
[&](ssGUI::EventInfo& info)
{
if(((ssGUI::Button*)info.EventSource)->GetButtonState() == ssGUI::Enums::ButtonState::CLICKED)
{
Expand Down
2 changes: 1 addition & 1 deletion Src/Examples/old/ReadmeExampleV2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ int main()
button.GetEventCallback(ssGUI::Enums::EventType::BUTTON_STATE_CHANGED)->AddEventListener
(
"AnyKey",
[&](ssGUI::EventInfo info)
[&](ssGUI::EventInfo& info)
{
if(((ssGUI::Button*)info.EventSource)->GetButtonState() == ssGUI::Enums::ButtonState::CLICKED)
{
Expand Down
2 changes: 1 addition & 1 deletion Src/Examples/old/ReadmeExampleV3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ int main()
button.GetEventCallback(ssGUI::Enums::EventType::BUTTON_STATE_CHANGED)->AddEventListener
(
"AnyKey",
[&](ssGUI::EventInfo info)
[&](ssGUI::EventInfo& info)
{
if(((ssGUI::Button*)info.EventSource)->GetButtonState() == ssGUI::Enums::ButtonState::CLICKED)
text.SetText("(`oωo´)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ int main()
TestWindow = ssGUI::Factory::Create<ssGUI::Window>();

//Timing is making sure the listener is triggered **after** the event
Callback->AddEventListener( "key", [&](ssGUI::EventInfo info)
Callback->AddEventListener( "key", [&](ssGUI::EventInfo& info)
{
ssTEST_OUTPUT_ASSERT("Timing", info.EventSource->GetBackgroundColor() == ChangeBGColor);
ListenerNum = 1;
Expand Down
Loading

0 comments on commit cda8a64

Please sign in to comment.