HoverRace  2.0
Container.h
Go to the documentation of this file.
1 
2 // Container.h
3 //
4 // Copyright (c) 2015 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 "BaseContainer.h"
25 
26 #if defined(_WIN32) && defined(HR_ENGINE_SHARED)
27 # ifdef MR_ENGINE
28 # define MR_DllDeclare __declspec( dllexport )
29 # else
30 # define MR_DllDeclare __declspec( dllimport )
31 # endif
32 #else
33 # define MR_DllDeclare
34 #endif
35 
36 namespace HoverRace {
37  namespace Display {
38  class Display;
39  }
40 }
41 
42 namespace HoverRace {
43 namespace Display {
44 
50 {
52 
53 public:
54  Container(Display &display, uiLayoutFlags_t layoutFlags = 0) :
55  SUPER(display, layoutFlags), focusedChild(nullptr) { }
56  Container(Display &display, const Vec2 &size, bool clip = true,
57  uiLayoutFlags_t layoutFlags = 0) :
58  SUPER(display, size, clip, layoutFlags), focusedChild(nullptr) { }
59  virtual ~Container() { }
60 
61 public:
62  bool OnAction() override
63  {
64  if (focusedChild) {
65  return focusedChild->OnAction();
66  }
67  return false;
68  }
69 
70  bool OnNavigate(const Control::Nav &nav) override
71  {
72  if (focusedChild) {
73  return focusedChild->OnNavigate(nav);
74  }
75  return false;
76  }
77 
78 protected:
79  void OnChildRequestedFocus(UiViewModel &child) override;
80  void OnChildRelinquishedFocus(UiViewModel &child,
81  const Control::Nav &nav) override;
82 
83 private:
84  void FocusPrevFrom(children_t::iterator startingPoint,
85  const Control::Nav &nav);
86  void FocusNextFrom(children_t::iterator startingPoint,
87  const Control::Nav &nav);
88 
89 public:
90  bool TryFocus(const Control::Nav &nav = Control::Nav::NEUTRAL) override;
91  void DropFocus() override;
92 
93 public:
94  // Public forwards of container manipulation functions.
95 
97  template<class T, class... Args>
98  typename std::enable_if<std::is_base_of<UiViewModel, T>::value,
99  std::shared_ptr<T>>::type
100  NewChild(Args&&... args)
101  {
102  return SUPER::NewChild<T>(std::forward<Args>(args)...);
103  }
104 
106  template<class T>
107  typename std::enable_if<std::is_base_of<UiViewModel, T>::value,
108  std::shared_ptr<T>
109  >::type
110  RemoveChild(const std::shared_ptr<T> &child)
111  {
112  if (child.get() == focusedChild) {
113  child->DropFocus();
114  focusedChild = nullptr;
115  RelinquishFocus(Control::Nav::NEUTRAL);
116  }
117  return SUPER::RemoveChild(child);
118  }
119 
121  template<class T>
122  typename std::enable_if<std::is_base_of<UiViewModel, T>::value,
123  std::shared_ptr<T>
124  >::type
125  ReorderChild(const std::shared_ptr<T> &child, size_t idx)
126  {
127  return SUPER::ReorderChild(child, idx);
128  }
129 
130  void Clear() override;
131 
132  void Reserve(size_t capacity) override { SUPER::Reserve(capacity); }
133 
134 private:
136 };
137 
138 } // namespace Display
139 } // namespace HoverRace
140 
141 #undef MR_DllDeclare
std::enable_if< std::is_base_of< UiViewModel, T >::value, std::shared_ptr< T > >::type NewChild(Args &&...args)
Create and append a new child widget to the end of the list.
Definition: Container.h:100
Container(Display &display, uiLayoutFlags_t layoutFlags=0)
Definition: Container.h:54
virtual ~Container()
Definition: Container.h:59
Base class for UI (2D) components.
Definition: UiViewModel.h:56
UiViewModel * focusedChild
Definition: Container.h:135
bool OnNavigate(const Control::Nav &nav) override
Definition: Container.h:70
Base class for widgets that contain other widgets.
Definition: BaseContainer.h:63
Definition: Vec.h:38
void Reserve(size_t capacity) override
Increase the capacity of the of this container.
Definition: Container.h:132
MR_UInt32 uiLayoutFlags_t
Definition: UiLayoutFlags.h:53
Base class for display managers.
Definition: Display.h:73
std::enable_if< std::is_base_of< UiViewModel, T >::value, std::shared_ptr< T > >::type ReorderChild(const std::shared_ptr< T > &child, size_t idx)
Move a widget to a different position in the list.
Definition: Container.h:125
Container(Display &display, const Vec2 &size, bool clip=true, uiLayoutFlags_t layoutFlags=0)
Definition: Container.h:56
#define MR_DllDeclare
Definition: Container.h:33
An invisible container for other UI widgets.
Definition: Container.h:49
A navigation direction.
Definition: Nav.h:45
std::enable_if< std::is_base_of< UiViewModel, T >::value, std::shared_ptr< T > >::type RemoveChild(const std::shared_ptr< T > &child)
Remove a child element.
Definition: Container.h:110
int idx
Definition: SdlDisplay.cpp:254
Definition: Announcement.h:24
bool OnAction() override
Definition: Container.h:62