HoverRace  2.0
ViewModel.h
Go to the documentation of this file.
1 
2 // ViewModel.h
3 //
4 // Copyright (c) 2013 Michael Imamura.
5 //
6 // Licensed under GrokkSoft HoverRace SourceCode License v1.0(the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // A copy of the license should have been attached to the package from which
10 // you have taken this file. If you can not find the license you can not use
11 // this file.
12 //
13 //
14 // The author makes no representations about the suitability of
15 // this software for any purpose. It is provided "as is" "AS IS",
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17 // implied.
18 //
19 // See the License for the specific language governing permissions
20 // and limitations under the License.
21 
22 #pragma once
23 
24 #include "Display.h"
25 #include "View.h"
26 
27 #include "ViewAttacher.h"
28 
29 #if defined(_WIN32) && defined(HR_ENGINE_SHARED)
30 # ifdef MR_ENGINE
31 # define MR_DllDeclare __declspec( dllexport )
32 # else
33 # define MR_DllDeclare __declspec( dllimport )
34 # endif
35 #else
36 # define MR_DllDeclare
37 #endif
38 
39 namespace HoverRace {
40  namespace Display {
41  class Display;
42  }
43 }
44 
45 namespace HoverRace {
46 namespace Display {
47 
56 {
57  public:
58  ViewModel() : needsLayout(true) { }
59  virtual ~ViewModel() { }
60 
61  public:
62  virtual void AttachView(Display &disp) = 0;
63  void SetView(std::unique_ptr<View> &&view) { this->view = std::move(view); }
64  View *GetView() const { return view.get(); }
65 
66  protected:
67  template<class T>
68  void AttachViewDynamic(Display &disp, T *self)
69  {
70  dynamic_cast<ViewAttacher<T>&>(disp).AttachView(*self);
71  }
72 
73  protected:
79  void RequestLayout() { needsLayout = true; }
80 
91  virtual void Layout() { }
92 
93  public:
103  virtual Vec3 Measure() { return view ? view->Measure() : Vec3(0, 0, 0); }
104 
106  {
107  if (needsLayout) {
108  Layout();
109  needsLayout = false;
110  }
111  if (view) view->PrepareRender();
112  }
113 
114  void Render() { if (view) view->Render(); }
115 
116  protected:
122  virtual void FireModelUpdate(int prop) { if (view) view->OnModelUpdate(prop); }
123 
124  private:
126  std::unique_ptr<View> view;
127 };
128 
129 } // namespace Display
130 } // namespace HoverRace
131 
132 #undef MR_DllDeclare
Base class for renderable components.
Definition: ViewModel.h:55
virtual Vec3 Measure()
Calculate the size of the component.
Definition: ViewModel.h:103
std::unique_ptr< View > view
Definition: ViewModel.h:126
Interface for classes that can attach a view to a model.
Definition: ViewAttacher.h:43
virtual void Layout()
Adjust the size and position of any child elements.
Definition: ViewModel.h:91
void SetView(std::unique_ptr< View > &&view)
Definition: ViewModel.h:63
Base class for display managers.
Definition: Display.h:73
virtual void FireModelUpdate(int prop)
Indicate that a model property has changed.
Definition: ViewModel.h:122
bool needsLayout
Definition: ViewModel.h:125
virtual ~ViewModel()
Definition: ViewModel.h:59
void PrepareRender()
Definition: ViewModel.h:105
#define MR_DllDeclare
Definition: ViewModel.h:36
Base class for views.
Definition: View.h:42
View * GetView() const
Definition: ViewModel.h:64
ViewModel()
Definition: ViewModel.h:58
Definition: Announcement.h:24
Definition: Vec.h:114
void AttachViewDynamic(Display &disp, T *self)
Definition: ViewModel.h:68
void Render()
Definition: ViewModel.h:114
void RequestLayout()
Indicate that the current layout is out-of-date and needs to be adjusted.
Definition: ViewModel.h:79