Torch
Loading...
Searching...
No Matches
View.h
1#pragma once
2
3#include <memory>
4
5#define IMGUI_DEFINE_MATH_OPERATORS
6#include "imgui.h"
7
8class ViewManager;
9
10// A single screen of the UI, swapped in through the ViewManager.
11class View {
12public:
13 virtual ~View() = default;
14
15 virtual void Init() {}
16 virtual void Update() {}
17 virtual void Render() = 0;
18
19 void InternalInit(const std::shared_ptr<ViewManager>& manager) {
20 // Weak to avoid a cycle: the ViewManager owns the View.
21 this->mManager = manager;
22 this->Init();
23 }
24
25protected:
26 std::shared_ptr<ViewManager> Manager() const {
27 return mManager.lock();
28 }
29
30private:
31 std::weak_ptr<ViewManager> mManager;
32};
33
34// Owns the active View and drives its per-frame Update/Render.
35class ViewManager : public std::enable_shared_from_this<ViewManager> {
36public:
37 void SetView(const std::shared_ptr<View>& view) {
38 mCurrent = view;
39 if (mCurrent != nullptr) {
40 mCurrent->InternalInit(shared_from_this());
41 }
42 }
43
44 const std::shared_ptr<View>& Current() const {
45 return mCurrent;
46 }
47
48 void Render() {
49 if (mCurrent != nullptr) {
50 mCurrent->Update();
51 mCurrent->Render();
52 }
53 }
54
55private:
56 std::shared_ptr<View> mCurrent = nullptr;
57};
Definition View.h:35
Definition View.h:11