context_listener.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * context_listener.h - Context-dependent entity
  3. * Copyright (C) 2017 caryoscelus
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef __STUDIO__CONTEXT_LISTENER_H__9E128DC4
  19. #define __STUDIO__CONTEXT_LISTENER_H__9E128DC4
  20. #include <memory>
  21. #include <type_traits>
  22. #include "editor_context.h"
  23. namespace studio {
  24. class ContextListener {
  25. public:
  26. ContextListener(std::shared_ptr<EditorContext> context_=nullptr);
  27. virtual ~ContextListener() = default;
  28. public:
  29. std::shared_ptr<core::Context> get_core_context() const {
  30. return context->get_context();
  31. }
  32. std::shared_ptr<EditorContext> get_context() const {
  33. return context;
  34. }
  35. void set_core_context(std::shared_ptr<core::Context> core_context) {
  36. set_context(std::make_shared<EditorContext>(core_context));
  37. }
  38. virtual void set_context(std::shared_ptr<EditorContext> context_);
  39. protected:
  40. virtual core::Time get_time() {
  41. return time;
  42. }
  43. virtual void time_changed(core::Time time_) {
  44. time = time_;
  45. }
  46. virtual void fps_changed(core::Time::fps_type) {}
  47. virtual void active_node_changed(std::shared_ptr<core::AbstractValue>) {}
  48. template <class S, class F>
  49. void connect_boost(S& signal, F lambda) {
  50. auto slot = typename S::slot_type(lambda);
  51. slot.track_foreign(destroy_detector);
  52. signal.connect(slot);
  53. }
  54. private:
  55. std::shared_ptr<EditorContext> context;
  56. core::Time time;
  57. struct Null {};
  58. std::shared_ptr<Null> destroy_detector;
  59. };
  60. }
  61. #endif