CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
UIUtility.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
11
13
16
17namespace CeresEngine {
18 class AnyInputEvent;
19}
20
21namespace CeresEngine {
22 class UIView;
23 class UIWindow;
24 struct UIMouseEvent;
25
27 using UIPoint = Point2;
28
30 using UISize = Size2;
31
33 using UIRect = Rect2;
34
42 using UIEdgeInsets = Rect2Edge;
43
45 double topLeft = 0.0;
46 double topRight = 0.0;
47 double bottomLeft = 0.0;
48 double bottomRight = 0.0;
49
50 friend bool operator==(const UICornerRadius& lhs, const UICornerRadius& rhs) {
51 return lhs.topLeft == rhs.topLeft && lhs.topRight == rhs.topRight && lhs.bottomLeft == rhs.bottomLeft && lhs.bottomRight == rhs.bottomRight;
52 }
53 friend bool operator!=(const UICornerRadius& lhs, const UICornerRadius& rhs) { return !(rhs == lhs); }
54 };
55
58 enum class UITexturePosition {
61
64
67
70
73
76
79 };
80
98
100 enum class UITextAlignment {
102 Left,
103
105 Right,
106
108 Center,
109
111 Justified,
112 };
113
114 template<typename View, typename Signature> class UIDelegate;
115
120 template<typename View, typename R, typename... Args> class UIDelegate<View, R(Args...)> {
121 friend View;
122
123 private:
124 using DelegateType = UniqueFunction<R(View&, Args...) const>;
125 DelegateType mDelegate = nullptr;
126
127 public:
131
134
136 CE_EXPLICIT(false) UIDelegate(Delegate&& delegate) // NOLINT
137 : mDelegate(std::forward<Delegate>(delegate)) {}
138
139 CE_EXPLICIT(false) UIDelegate(R&& value) // NOLINT
140 : mDelegate([value = std::forward<R>(value)](View&, Args...) { return value; }) {}
141
142 public:
143 explicit operator bool() const noexcept { return mDelegate != nullptr; }
144
145 private:
146 R operator()(View* const view, Args... args) const {
147 CE_ASSERT(view != nullptr);
148 CE_ASSERT(mDelegate != nullptr);
149 return mDelegate(*view, std::forward<Args>(args)...);
150 }
151 };
152
156 template<typename View, typename... Args> class UIDelegate<View, void(Args...)> {
157 friend View;
158
159 private:
160 using DelegateType = UniqueFunction<void(View&, Args...) const>;
161 DelegateType mDelegate = nullptr;
162
163 public:
167
170
172 CE_EXPLICIT(false) UIDelegate(Delegate&& delegate) // NOLINT
173 : mDelegate(delegate) {}
174
175 public:
176 explicit operator bool() const noexcept { return mDelegate != nullptr; }
177
178 private:
179 void operator()(View* const view, Args... args) const {
180 CE_ASSERT(view != nullptr);
181 if(mDelegate) {
182 mDelegate(*view, std::forward<Args>(args)...);
183 }
184 }
185 };
186
187 using UIColor = Color;
188
190 struct UIUtility {
197 static bool makeFirstResponder(UIView* view);
198
206 static bool isOver(const UIView* view, const UIMouseEvent& event);
207
210 static bool forwardInputEvent(UIWindow* window, const AnyInputEvent& event);
211 };
212
214 static void layoutX(const UIView* views);
215 static void layoutY(const UIView* views);
216
217 static void sameWidth(std::initializer_list<UIView*> views);
218 static void sameHeight(std::initializer_list<UIView*> views);
219 };
220
228 template<typename MemberPtr, typename T, typename... Args> void broadcast(MemberPtr&& member, const Vector<T>& list, Args&&... args) {
229 for(auto& value : list) {
230 if constexpr(std::is_pointer_v<T>) {
231 (value->*member)(args...);
232 } else {
233 (value.*member)(args...);
234 }
235 }
236 }
237
247 template<typename MemberPtr, typename T, typename... Args> auto broadcastIf(MemberPtr&& member, const Vector<T>& list, Args&&... args) {
248 for(auto& value : list) {
249 auto invoke = [&]() {
250 if constexpr(std::is_pointer_v<T>) {
251 return (value->*member)(args...);
252 } else {
253 return (value.*member)(args...);
254 }
255 };
256
257 if(auto ret = invoke()) {
258 return ret;
259 }
260 }
261
262 return false;
263 }
264
266 extern const UIRect UIRectZero;
267
268} // namespace CeresEngine
#define CE_ASSERT(...)
Definition Macros.hpp:323
#define CE_EXPLICIT(EXPR)
Definition Macros.hpp:413
A type that describes any possible event type.
Definition InputEvent.hpp:142
UniqueFunction< R(View &, Args...) const > DelegateType
Definition UIUtility.hpp:124
R operator()(View *const view, Args... args) const
Definition UIUtility.hpp:146
friend View
Definition UIUtility.hpp:121
void operator()(View *const view, Args... args) const
Definition UIUtility.hpp:179
UniqueFunction< void(View &, Args...) const > DelegateType
Definition UIUtility.hpp:160
friend View
Definition UIUtility.hpp:157
Definition UIUtility.hpp:114
The infrastructure for drawing and handling events in a UI.
Definition UIView.hpp:153
A window that an app displays on the screen.
Definition UIWindow.hpp:42
Definition Application.hpp:19
Color UIColor
Definition UIUtility.hpp:187
Size2 UISize
A type that contains width and height values.
Definition UIUtility.hpp:30
Rect2Edge UIEdgeInsets
The inset distances for views.
Definition UIUtility.hpp:42
void broadcast(MemberPtr &&member, const Vector< T > &list, Args &&... args)
Helper function that calls MemberPtr on all elements in list.
Definition UIUtility.hpp:228
const UIRect UIRectZero
A zero UI rectangle.
std::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
auto broadcastIf(MemberPtr &&member, const Vector< T > &list, Args &&... args)
Helper function that calls MemberPtr on all elements in list.
Definition UIUtility.hpp:247
UITextAlignment
These constants specify text alignment.
Definition UIUtility.hpp:100
@ Justified
Text is justified.
@ Center
Text is visually center aligned.
Rect2 UIRect
A structure that contains the location and dimensions of a rectangle.
Definition UIUtility.hpp:33
@ Color
Attachment is used for color output.
UITextureScaling
Constants that specify a view's texture scaling behavior.
Definition UIUtility.hpp:82
@ ProportionallyUpOrDown
Scale the texture to its maximum possible dimensions while both staying within the destination area a...
@ AxesIndependently
Scale each dimension to exactly fit destination.
@ ProportionallyDown
If it is too large for the destination, scale the texture down while preserving the aspect ratio.
Point2 UIPoint
A type that contains a point in a two-dimensional coordinate system.
Definition UIUtility.hpp:27
FunctionBase< true, false, fu2::capacity_default, true, false, Signatures... > UniqueFunction
An owning non copyable function wrapper for arbitrary callable types.
Definition Function.hpp:59
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
UITexturePosition
Constants for specifying the position of a view texture relative to its title.
Definition UIUtility.hpp:58
@ NoTexture
The view doesn't display an texture.
@ TextureAbove
The texture is above the title.
@ TextureOverlaps
The texture overlaps the title.
@ TextureRight
The texture is to the right of the title.
@ TextureOnly
The cell displays an texture, but not a title.
@ TextureBelow
The texture is below the title.
@ TextureLeft
The texture is to the left of the title.
Definition Span.hpp:668
Definition UIUtility.hpp:44
double topLeft
Definition UIUtility.hpp:45
double topRight
Definition UIUtility.hpp:46
friend bool operator==(const UICornerRadius &lhs, const UICornerRadius &rhs)
Definition UIUtility.hpp:50
double bottomLeft
Definition UIUtility.hpp:47
double bottomRight
Definition UIUtility.hpp:48
friend bool operator!=(const UICornerRadius &lhs, const UICornerRadius &rhs)
Definition UIUtility.hpp:53
Definition UIUtility.hpp:213
static void layoutX(const UIView *views)
static void layoutY(const UIView *views)
static void sameHeight(std::initializer_list< UIView * > views)
static void sameWidth(std::initializer_list< UIView * > views)
Definition UIEvent.hpp:173
A set of common and useful functions for implementing UIs.
Definition UIUtility.hpp:190
static bool forwardInputEvent(UIWindow *window, const AnyInputEvent &event)
A utility method that forwards a generic AnyEvent to the given window.
static bool isOver(const UIView *view, const UIMouseEvent &event)
A utility method that checks if the event occurred over the view.
static bool makeFirstResponder(UIView *view)
Makes the view the first responder in it's window.