Skip to content

Commit

Permalink
Invader sample: Clean up event listeners, see #605
Browse files Browse the repository at this point in the history
  • Loading branch information
mikke89 committed Mar 31, 2024
1 parent 0c030e4 commit 386820b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
12 changes: 12 additions & 0 deletions Samples/invaders/src/ElementGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,15 @@ void ElementGame::OnChildAdd(Rml::Element* element)
GetOwnerDocument()->AddEventListener(Rml::EventId::Keyup, this);
}
}

void ElementGame::OnChildRemove(Rml::Element* element)
{
Rml::Element::OnChildRemove(element);

if (element == this)
{
GetOwnerDocument()->RemoveEventListener(Rml::EventId::Load, this);
GetOwnerDocument()->RemoveEventListener(Rml::EventId::Keydown, this);
GetOwnerDocument()->RemoveEventListener(Rml::EventId::Keyup, this);
}
}
7 changes: 4 additions & 3 deletions Samples/invaders/src/ElementGame.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ class ElementGame : public Rml::Element, public Rml::EventListener {
/// Intercepts and handles key events.
void ProcessEvent(Rml::Event& event) override;

/// Receive notifications when child elements are added
/// This will only get called when we're added to the tree,
/// which allows us to bind to onload
/// This will get called when we're added to the tree, which allows us to bind to events.
void OnChildAdd(Rml::Element* element) override;

/// This will get called when we're removed from the tree, which allows us to clean up the event listeners previously added.
void OnChildRemove(Element* element) override;

protected:
/// Updates the game.
void OnUpdate() override;
Expand Down
12 changes: 12 additions & 0 deletions Samples/luainvaders/src/ElementGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,15 @@ void ElementGame::OnChildAdd(Rml::Element* element)
GetOwnerDocument()->AddEventListener(Rml::EventId::Keyup, this);
}
}

void ElementGame::OnChildRemove(Rml::Element* element)
{
Rml::Element::OnChildRemove(element);

if (element == this)
{
GetOwnerDocument()->RemoveEventListener(Rml::EventId::Load, this);
GetOwnerDocument()->RemoveEventListener(Rml::EventId::Keydown, this);
GetOwnerDocument()->RemoveEventListener(Rml::EventId::Keyup, this);
}
}
7 changes: 4 additions & 3 deletions Samples/luainvaders/src/ElementGame.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ class ElementGame : public Rml::Element, public Rml::EventListener {
/// Intercepts and handles key events.
void ProcessEvent(Rml::Event& event) override;

/// Receive notifications when child elements are added
/// This will only get called when we're added to the tree,
/// which allows us to bind to onload
/// This will get called when we're added to the tree, which allows us to bind to events.
void OnChildAdd(Rml::Element* element) override;

/// This will get called when we're removed from the tree, which allows us to clean up the event listeners previously added.
void OnChildRemove(Element* element) override;

protected:
/// Updates the game.
void OnUpdate() override;
Expand Down

0 comments on commit 386820b

Please sign in to comment.