You.i Engine
YiLayout.h
Go to the documentation of this file.
1 // © You i Labs Inc. 2000-2020. All rights reserved.
2 #ifndef _YI_LAYOUT_H_
3 #define _YI_LAYOUT_H_
4 
5 #include "framework/YiPredef.h"
8 #include "view/YiSceneView.h"
9 
10 class CYILayoutState;
11 
160 {
161 public:
165  enum class PositioningMode
166  {
171 
176  };
177 
179 
180  CYILayout();
181  virtual ~CYILayout();
182 
190  void AttachTo(CYISceneView *pView);
191 
200 
204  void DetachFromNode();
205 
210 
216  void Measure(const CYISceneNode::MeasureSpec &widthSpec, const CYISceneNode::MeasureSpec &heightSpec, const CYISceneNode::MeasureSpec &depthSpec);
217 
223  void ApplyMeasurements();
224 
230  void SetPositioningMode(PositioningMode positioningMode);
231 
233 
239  virtual void ChildAdded(CYISceneNode *pChild);
240 
246  virtual void ChildRemoved(CYISceneNode *pChild);
247 
253  virtual void ChildVisibilityChanged(CYISceneNode *pChild);
254 
258  void SetPadding(const Padding &padding);
259 
260  const Padding &GetPadding() const;
261 
265  void SetUseReverseChildrenOrder(bool useReverseChildrenOrder);
266 
267  bool IsUsingReverseChildrenOrder() const;
268 
280  void SetGravity(const glm::vec3 &layoutGravity);
281 
288  void SetGravity(CYILayoutConfig::Gravity gravity);
289 
290  const glm::vec3 &GetGravity() const;
291 
295  static void RegisterAllLayouts();
296 
300  static bool IsLayoutable(const CYISceneNode *pNode);
301 
305  static glm::vec3 GetDefaultSize(const CYISceneNode *pNode, const CYISceneNode::MeasureSpec &widthSpec, const CYISceneNode::MeasureSpec &heightSpec, const CYISceneNode::MeasureSpec &depthSpec);
306 
314  static void UpdateMeasuredSizeForFitContent(const CYISceneNode *pNode, glm::vec3 *pMeasuredSize, const glm::vec3 &contentSize, const Padding &padding, const CYISceneNode::MeasureSpec &widthSpec, const CYISceneNode::MeasureSpec &heightSpec, const CYISceneNode::MeasureSpec &depthSpec);
315 
319  static void UnscaleMeasureSpecs(const glm::vec3 &scale, CYISceneNode::MeasureSpec *pWidthSpec, CYISceneNode::MeasureSpec *pHeightSpec, CYISceneNode::MeasureSpec *pDepthSpec);
320 
324  static float CalculateDimension(const CYISceneNode::MeasureSpec &spec, const CYILayoutConfig::SizeConstraint &constraint, float currentValue);
325 
326 protected:
332  virtual void Configure();
333 
337  virtual void OnSceneViewAttached();
338 
348  virtual void OnMeasure(const CYISceneNode::MeasureSpec &widthSpec, const CYISceneNode::MeasureSpec &heightSpec, const CYISceneNode::MeasureSpec &depthSpec) = 0;
349 
359  virtual void OnApplyMeasurements() = 0;
360 
364  virtual void OnMeasurementsCalculated(const CYISceneNode::MeasureSpec &widthSpec, const CYISceneNode::MeasureSpec &heightSpec, const CYISceneNode::MeasureSpec &depthSpec);
365 
369  virtual void OnMeasurementsApplied();
370 
376  virtual const CYIRuntimeTypeInfo &GetLayoutConfigType() const;
377 
385  virtual std::unique_ptr<CYILayoutState> CreateLayoutStateInstance() const;
386 
390  virtual void ApplyPosition(CYISceneNode *pChild, const glm::vec3 &position);
391 
395  virtual void ApplySize(CYISceneNode *pChild, const glm::vec3 &size);
396 
402  static void MeasureChildWithMargins(CYISceneNode *pChild, const CYISceneNode::MeasureSpec &parentWidthSpec, const CYISceneNode::MeasureSpec &parentHeightSpec, const CYISceneNode::MeasureSpec &parentDepthSpec, const Padding &padding);
403 
407  static void MeasureChildWithMargins(CYISceneNode *pChild, const CYISceneNode::MeasureSpec &parentWidthSpec, const CYISceneNode::MeasureSpec &parentHeightSpec, const CYISceneNode::MeasureSpec &parentDepthSpec, const Padding &padding, const glm::vec3 &childScale);
408 
412  static CYISceneNode::MeasureSpec CalculateChildMeasureSpec(const CYISceneNode::MeasureSpec &parentMeasureSpec, float padding, const CYILayoutConfig::SizeConstraint &childConstraint, float childScale);
413 
417  enum class Dimensions
418  {
419  None = 0x0,
420  X = 0x1,
421  Y = 0x2,
422  Z = 0x4,
423  AllButX = Y | Z,
424  AllButY = X | Z,
425  AllButZ = X | Y,
426  All = X | Y | Z,
427  };
428 
441 
447  static void RemeasureChildrenForFillParent(const CYISceneNode *pNode, const glm::vec3 &parentMeasuredSize, const Padding &padding, Dimensions dimensionsToRemeasure);
448 
452  void CreateLayoutObjectsFor(CYISceneNode *pNode) const;
453 
457  CYISceneNode *GetChild(size_t index) const;
458 
462  static glm::vec3 GetAdjustedPosition(const glm::vec3 &desiredTopLeftPosition, const CYISceneNode *pChild);
463 
468  static glm::vec3 GetUnadjustedPosition(const glm::vec3 &currentTopLeftPosition, const CYISceneNode *pChild);
469 
473  static glm::vec3 GetAdjustedMeasuredSize(const CYISceneNode *pChild);
474 
484  static glm::vec3 GetTopLeftPositionWithGravity(const glm::vec3 &layoutGravity, const glm::vec3 &childGravity, const glm::vec3 &containerTopLeft, const glm::vec3 &containerSize, const CYILayout::Padding &padding, const glm::vec3 &childSize, const CYILayoutConfig::Margin &margin, const glm::vec3 &currentChildPosition);
485 
493  static void MeasureBackgroundChildren(CYISceneNode *pNode, const glm::vec3 &size);
494 
501 
507  const Padding &GetPaddingForChild(const CYISceneNode *pChild) const;
508 
510  Padding m_padding;
512  glm::vec3 m_gravity;
514 
515 private:
516  bool m_baseConfigureCalled;
517 
519 
521 };
522 
525 template<>
527 {
528  static const bool enable = true;
529 };
530 
532 {
533  return m_pNode;
534 }
535 
536 inline glm::vec3 CYILayout::GetUnadjustedPosition(const glm::vec3 &currentTopLeftPosition, const CYISceneNode *pChild)
537 {
538  return currentTopLeftPosition - pChild->GetAnchorPoint();
539 }
540 
541 inline glm::vec3 CYILayout::GetAdjustedMeasuredSize(const CYISceneNode *pChild)
542 {
543  return pChild->GetMeasuredSize() * pChild->GetScale();
544 }
545 
546 inline bool CYILayout::IsLayoutable(const CYISceneNode *pNode)
547 {
548  // Non-visible items are not considered for laying-out
549  if (!pNode->IsVisible())
550  {
551  return false;
552  }
553 
555  if (!pConfig)
556  {
557  // Nodes without layout objects can be laid out. This is to support applying measurements to scene node outside of layouts.
558  return true;
559  }
560 
561  return pConfig->IsLayoutable();
562 }
563 
565 {
566  const CYILayoutConfig *pConfig = pChild->GetLayoutConfig();
567  if (pConfig->IsBackground())
568  {
569  return Padding::zeroInstance;
570  }
571  return m_padding;
572 }
573 
574 #endif // _YI_LAYOUT_H_
void DetachFromNode()
#define YI_DISALLOW_COPY_AND_ASSIGN(TypeName)
Delete the copy constructor and assignment operator (and consequently the move constructor as well) ...
Definition: YiPredef.h:122
static void UpdateMeasuredSizeForFitContent(const CYISceneNode *pNode, glm::vec3 *pMeasuredSize, const glm::vec3 &contentSize, const Padding &padding, const CYISceneNode::MeasureSpec &widthSpec, const CYISceneNode::MeasureSpec &heightSpec, const CYISceneNode::MeasureSpec &depthSpec)
A class used to hold a 3D &#39;cube offset&#39;.
Definition: YiLayoutConfig.h:55
const Padding & GetPadding() const
const CYILayoutConfig * GetLayoutConfig(LayoutConfigFetchMode fetchMode=LayoutConfigFetchMode::LazyInstantiate) const
The base class for all view types. Views are containers of renderable elements that often define inte...
Definition: YiSceneView.h:47
static bool IsLayoutable(const CYISceneNode *pNode)
Definition: YiLayout.h:546
void SetPositioningMode(PositioningMode positioningMode)
static void RemeasureChildrenForFillParent(const CYISceneNode *pNode, const glm::vec3 &parentMeasuredSize, const Padding &padding, Dimensions dimensionsToRemeasure)
const Padding & GetPaddingForChild(const CYISceneNode *pChild) const
Definition: YiLayout.h:564
static void UnscaleMeasureSpecs(const glm::vec3 &scale, CYISceneNode::MeasureSpec *pWidthSpec, CYISceneNode::MeasureSpec *pHeightSpec, CYISceneNode::MeasureSpec *pDepthSpec)
virtual void Configure()
virtual void ApplySize(CYISceneNode *pChild, const glm::vec3 &size)
PositioningMode
Definition: YiLayout.h:165
CYISceneNode * GetChild(size_t index) const
Padding m_padding
Definition: YiLayout.h:510
virtual void OnMeasurementsCalculated(const CYISceneNode::MeasureSpec &widthSpec, const CYISceneNode::MeasureSpec &heightSpec, const CYISceneNode::MeasureSpec &depthSpec)
const glm::vec3 & GetAnchorPoint() const
bool IsLayoutable() const
Definition: YiLayoutConfig.h:299
static void MeasureBackgroundChildren(CYISceneNode *pNode, const glm::vec3 &size)
void SetPadding(const Padding &padding)
A struct used to enable bitmask operators for an enum class type.
Definition: YiEnumBitmaskOperators.h:26
static glm::vec3 GetTopLeftPositionWithGravity(const glm::vec3 &layoutGravity, const glm::vec3 &childGravity, const glm::vec3 &containerTopLeft, const glm::vec3 &containerSize, const CYILayout::Padding &padding, const glm::vec3 &childSize, const CYILayoutConfig::Margin &margin, const glm::vec3 &currentChildPosition)
virtual void ChildAdded(CYISceneNode *pChild)
void SetGravity(const glm::vec3 &layoutGravity)
virtual void OnApplyMeasurements()=0
CYISceneView * m_pNode
In most cases, this variable is used as a node (thus the name m_pNode )
Definition: YiLayout.h:509
static void MeasureChildWithMargins(CYISceneNode *pChild, const CYISceneNode::MeasureSpec &parentWidthSpec, const CYISceneNode::MeasureSpec &parentHeightSpec, const CYISceneNode::MeasureSpec &parentDepthSpec, const Padding &padding)
void SetUseReverseChildrenOrder(bool useReverseChildrenOrder)
constexpr std::enable_if< YiEnableBitmaskOperators< E >::enable, bool >::type None(E a)
Definition: YiEnumBitmaskOperators.h:136
virtual std::unique_ptr< CYILayoutState > CreateLayoutStateInstance() const
void CreateLayoutObjectsFor(CYISceneNode *pNode) const
static glm::vec3 GetAdjustedMeasuredSize(const CYISceneNode *pChild)
Definition: YiLayout.h:541
const glm::vec3 & GetScale() const
static glm::vec3 GetDefaultSize(const CYISceneNode *pNode, const CYISceneNode::MeasureSpec &widthSpec, const CYISceneNode::MeasureSpec &heightSpec, const CYISceneNode::MeasureSpec &depthSpec)
Gravity
Definition: YiLayoutConfig.h:34
bool m_reverseChildrenOrder
Definition: YiLayout.h:511
virtual const CYIRuntimeTypeInfo & GetLayoutConfigType() const
A class used to hold a size constraint for a specific dimension.
Definition: YiLayoutConfig.h:124
bool IsBackground() const
Definition: YiLayoutConfig.h:309
CYISceneNode * GetAssociatedSceneNode() const
Definition: YiLayout.h:531
static glm::vec3 GetAdjustedPosition(const glm::vec3 &desiredTopLeftPosition, const CYISceneNode *pChild)
PositioningMode m_positioningMode
Definition: YiLayout.h:513
Dimensions
Definition: YiLayout.h:417
void ApplyMeasurementsToBackgroundChildren()
static float CalculateDimension(const CYISceneNode::MeasureSpec &spec, const CYILayoutConfig::SizeConstraint &constraint, float currentValue)
static bool UpdateMeasureSpecsForFitContent(const CYISceneNode *pNode, CYISceneNode::MeasureSpec *widthSpec, CYISceneNode::MeasureSpec *heightSpec, CYISceneNode::MeasureSpec *depthSpec)
static glm::vec3 GetUnadjustedPosition(const glm::vec3 &currentTopLeftPosition, const CYISceneNode *pChild)
Definition: YiLayout.h:536
A scene node is the base type for all nodes which are used by the scene manager; it is an integral pa...
Definition: YiSceneNode.h:99
Definition: YiSceneNode.h:373
A class used to hold node-specific layout configuration (such as, for example, Margin).
Definition: YiLayoutConfig.h:25
glm::vec3 m_gravity
Definition: YiLayout.h:512
The abstract runtime representation of a C++ type.
Definition: YiRtti.h:56
virtual void OnMeasurementsApplied()
virtual ~CYILayout()
static CYISceneNode::MeasureSpec CalculateChildMeasureSpec(const CYISceneNode::MeasureSpec &parentMeasureSpec, float padding, const CYILayoutConfig::SizeConstraint &childConstraint, float childScale)
virtual void ChildVisibilityChanged(CYISceneNode *pChild)
A class used to hold node-specific layout state.
Definition: YiLayoutState.h:16
This class defines a layout that can be applied to a scene view. The layout class determines how obje...
Definition: YiLayout.h:159
void ApplyMeasurements()
virtual void OnMeasure(const CYISceneNode::MeasureSpec &widthSpec, const CYISceneNode::MeasureSpec &heightSpec, const CYISceneNode::MeasureSpec &depthSpec)=0
#define YI_TYPE_BASES(CLASS, BASES...)
Definition: YiRtti.h:257
virtual void ChildRemoved(CYISceneNode *pChild)
static const CubeOffset zeroInstance
Definition: YiLayoutConfig.h:69
virtual void OnSceneViewAttached()
bool IsUsingReverseChildrenOrder() const
const glm::vec3 & GetGravity() const
void Measure(const CYISceneNode::MeasureSpec &widthSpec, const CYISceneNode::MeasureSpec &heightSpec, const CYISceneNode::MeasureSpec &depthSpec)
bool IsVisible() const
const glm::vec3 & GetMeasuredSize() const
PositioningMode GetPositioningMode() const
static void RegisterAllLayouts()
void AttachTo(CYISceneView *pView)
void ConfigureFromAttachedNode()
CYILayoutConfig::CubeOffset Padding
Definition: YiLayout.h:178
virtual void ApplyPosition(CYISceneNode *pChild, const glm::vec3 &position)