LoggingBodyActivationListener.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Physics/Body/BodyActivationListener.h>
  5. /// Activation listener that just logs the activations/deactivations
  6. class LoggingBodyActivationListener : public BodyActivationListener
  7. {
  8. public:
  9. // Activation callback type
  10. enum class EType
  11. {
  12. Activated,
  13. Deactivated,
  14. };
  15. // Entry written when an activation callback happens
  16. struct LogEntry
  17. {
  18. EType mType;
  19. BodyID mBodyID;
  20. };
  21. virtual void OnBodyActivated(const BodyID &inBodyID, uint64 inBodyUserData) override
  22. {
  23. lock_guard lock(mLogMutex);
  24. mLog.push_back({ EType::Activated, inBodyID });
  25. }
  26. virtual void OnBodyDeactivated(const BodyID &inBodyID, uint64 inBodyUserData) override
  27. {
  28. lock_guard lock(mLogMutex);
  29. mLog.push_back({ EType::Deactivated, inBodyID });
  30. }
  31. void Clear()
  32. {
  33. mLog.clear();
  34. }
  35. size_t GetEntryCount() const
  36. {
  37. return mLog.size();
  38. }
  39. // Check if we have logged an event with a particular type and involving a particular body
  40. bool Contains(EType inType, const BodyID &inBodyID) const
  41. {
  42. for (const LogEntry &e : mLog)
  43. if (e.mType == inType && e.mBodyID == inBodyID)
  44. return true;
  45. return false;
  46. }
  47. private:
  48. Mutex mLogMutex; // Callbacks are made from a thread, make sure we don't corrupt the log
  49. Array<LogEntry> mLog;
  50. };