CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
UIViewController.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "UIResponder.hpp"
11#include "UIView.hpp"
12
15
16namespace CeresEngine {
17
18 class UIViewController;
19
22
24 class UIViewController : public UIResponder, public RefCounted<UIViewController> {
25 private:
34 UIViewPtr mView = nullptr;
35
38 bool mViewLoaded = false;
39
46
54
57
58 public:
59 UIViewController() = default;
60 explicit UIViewController(UIView& view);
61 explicit UIViewController(UIView* view);
62
64
65 public:
67 [[nodiscard]] UIView* getView() const;
68
71
84 virtual void didLoad() {}
85
103 virtual void willAppear() {}
104
120 virtual void didAppear() {}
121
142 virtual void willDisappear() {}
143
157 virtual void didDisappear() {}
158
168 virtual void willLayout() {}
169
179 virtual void didLayout() {}
180
192
204
205 protected:
220
234 virtual void loadView(UIView& view);
235
236 public:
239
242
249
252
259
262
268
269 public:
272
275
276 public: // UIResponder
279
280 private:
282 };
283
294
295 public:
298
301
302 public:
305 template<typename... Args>
306 explicit TUIViewController(Args&&... args) : TUIViewController(ce_counted_new<ViewType>(std::forward<Args>(args)...).get()) {}
307
309 explicit TUIViewController(ViewType* const view) : TParentViewControllerType(view) {}
310
311 public:
313 [[nodiscard]] ViewType* getView() const { return static_cast<ViewType*>(ParentViewControllerType::getView()); }
314
317 if constexpr(std::is_default_constructible_v<ViewType>) {
319 } else {
320 throw std::runtime_error("createView not implemented for ViewType.");
321 }
322 }
323
325 void loadView(UIView& view) final { loadView(static_cast<ViewType&>(view)); }
326
328 virtual void loadView(ViewType& view) { super::loadView(view); }
329 };
330
332 template<typename TModelType, typename TViewControllerType = UIViewController> class UIModelViewController : public TViewControllerType {
333 public:
335 using ModelType = TModelType;
336
338 using ViewControllerType = TViewControllerType;
339
340 public:
341 using TViewControllerType::TViewControllerType;
342
343 public:
345 [[nodiscard]] const ModelType& getRepresentedObject() const noexcept { return ViewControllerType::mRepresentedObject.template get<ModelType>(); }
346
348 [[nodiscard]] ModelType& getRepresentedObject() noexcept { return ViewControllerType::mRepresentedObject.template get<ModelType>(); }
349
351 void setRepresentedObject(const ModelType& representedObject) { ViewControllerType::mRepresentedObject = representedObject; }
352
354 void setRepresentedObject(ModelType&& representedObject) { ViewControllerType::mRepresentedObject = std::forward<ModelType>(representedObject); }
355 };
356
357} // namespace CeresEngine
358
#define CE_RCPTR_TRAIT_DECL(ClassName)
Definition SmartPtr.hpp:375
Definition Any.hpp:13
A retain-release type of smart pointer.
Definition SmartPtr.hpp:132
A simple reference counter base class.
Definition SmartPtr.hpp:438
A templated UIViewController sub-class that specializes it for a specific view type.
Definition UIViewController.hpp:292
void loadView(UIView &view) final
Instantiates a view.
Definition UIViewController.hpp:325
TUIViewController(ViewType *const view)
Creates a new TUIViewController from an existing view instance.
Definition UIViewController.hpp:309
TViewType ViewType
The type of view controlled by the UIViewController.
Definition UIViewController.hpp:297
virtual void loadView(ViewType &view)
Instantiates a view.
Definition UIViewController.hpp:328
TUIViewController(Args &&... args)
Creates a new TUIViewController and instantiates a new view of type ViewType by passing args to the v...
Definition UIViewController.hpp:306
TParentViewControllerType ParentViewControllerType
The parent type of the view controller, usually UIViewController.
Definition UIViewController.hpp:300
UIViewPtr createView() override
Instantiates a view.
Definition UIViewController.hpp:316
TParentViewControllerType super
Definition UIViewController.hpp:293
ViewType * getView() const
The view controller’s primary view.
Definition UIViewController.hpp:313
A UIViewController sub-class that uses a typed represented object.
Definition UIViewController.hpp:332
TViewControllerType ViewControllerType
The view controller type this view controller sub-classes from.
Definition UIViewController.hpp:338
void setRepresentedObject(ModelType &&representedObject)
The object whose value is presented in the receiver’s primary view.
Definition UIViewController.hpp:354
const ModelType & getRepresentedObject() const noexcept
The object whose value is presented in the receiver’s primary view.
Definition UIViewController.hpp:345
ModelType & getRepresentedObject() noexcept
The object whose value is presented in the receiver’s primary view.
Definition UIViewController.hpp:348
TModelType ModelType
The type of model object to be used by the represented object.
Definition UIViewController.hpp:335
void setRepresentedObject(const ModelType &representedObject)
The object whose value is presented in the receiver’s primary view.
Definition UIViewController.hpp:351
An abstract class that forms the basis of event and command processing in the UI framework.
Definition UIResponder.hpp:33
A controller that manages a view.
Definition UIViewController.hpp:24
virtual void willAppear()
Called after the view controller’s view has been loaded into memory is about to be added to the view ...
Definition UIViewController.hpp:103
UIViewPtr mView
The view controller’s primary view.
Definition UIViewController.hpp:34
virtual void didRemoveChildViewController(UIViewController *const childViewController)
Called immediately after a child UIViewController is removed from the hierarchy.
Definition UIViewController.hpp:203
const Vector< UIViewControllerPtr > & getChildViewControllers() const noexcept
An array of view controllers that are hierarchical children of the view controller.
Definition UIViewController.hpp:241
const Any & getRepresentedObject() const noexcept
The object whose value is presented in the receiver’s primary view.
Definition UIViewController.hpp:271
void removeChildViewController(const UIViewControllerPtr &childViewController)
A convenience method for removing a child view controller from the childViewControllers array.
Definition UIViewController.hpp:261
UIView * getView() const
The view controller’s primary view.
UIResponder * getNextResponder() const final
The next responder after this one, or nil if it has none.
virtual void loadView(UIView &view)
Instantiates a view.
virtual void willLayout()
Called just before the layout method of the view controller's view is called.
Definition UIViewController.hpp:168
void addChildViewController(const UIViewControllerPtr &childViewController)
A convenience method for adding a child view controller at the end of the childViewControllers array.
Definition UIViewController.hpp:251
virtual void didAppear()
Called when the view controller’s view is fully transitioned onto the screen.
Definition UIViewController.hpp:120
bool mViewLoaded
A Boolean value indicating whether the view controller’s view is loaded into memory.
Definition UIViewController.hpp:38
virtual void didLoad()
Called after the view controller’s view has been loaded into memory.
Definition UIViewController.hpp:84
Vector< UIViewControllerPtr > mChildViewControllers
An array of view controllers that are hierarchical children of the view controller.
Definition UIViewController.hpp:53
UIViewController * mParentViewController
The immediate ancestor view controller of the view controller.
Definition UIViewController.hpp:45
virtual UIViewPtr createView()
Instantiates a view.
void removeChildViewController(UIViewController *childViewController)
A convenience method for removing a child view controller from the childViewControllers array.
UIViewController * getParentViewController() const noexcept
The immediate ancestor view controller of the view controller.
Definition UIViewController.hpp:238
void removeFromParentViewController()
Removes the called view controller from its parent view controller.
void setRepresentedObject(const Any &representedObject)
The object whose value is presented in the receiver’s primary view.
virtual void didAddChildViewController(UIViewController *const childViewController)
Called immediately after a child UIViewController is added to the hierarchy.
Definition UIViewController.hpp:191
Any mRepresentedObject
The object whose value is presented in the receiver’s primary view.
Definition UIViewController.hpp:56
bool isViewLoaded() const noexcept
A Boolean value indicating whether the view controller’s view is loaded into memory.
Definition UIViewController.hpp:70
void addChildViewController(UIViewController *childViewController)
A convenience method for adding a child view controller at the end of the childViewControllers array.
virtual void willDisappear()
Called when the view controller’s view is about to be removed from the view hierarchy in the window.
Definition UIViewController.hpp:142
virtual void didDisappear()
Called after the view controller’s view is removed from the view hierarchy in a window.
Definition UIViewController.hpp:157
virtual void didLayout()
Called immediately after the layout method of the view controller's view is called.
Definition UIViewController.hpp:179
The infrastructure for drawing and handling events in a UI.
Definition UIView.hpp:153
Definition Application.hpp:19
decltype(auto) get(BezierPath::Element &element) noexcept
Decomposes a bezier path element.
Definition BezierPath.hpp:723
std::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
RC< T > ce_counted_new(Args &&... args)
Definition SmartPtr.hpp:391
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Definition Span.hpp:668