iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
IGraphicsFlexBox.cpp
1/*
2 ==============================================================================
3
4 This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5
6 See LICENSE.txt for more info.
7
8 ==============================================================================
9*/
10
11#include "IGraphicsFlexBox.h"
12
13using namespace iplug;
14using namespace igraphics;
15
16IFlexBox::IFlexBox()
17{
18 mConfigRef = YGConfigNew();
19 mRootNodeRef = YGNodeNewWithConfig(mConfigRef);
20}
21
22IFlexBox::~IFlexBox()
23{
24 YGNodeFreeRecursive(mRootNodeRef);
25 YGConfigFree(mConfigRef);
26}
27
28void IFlexBox::Init(const IRECT& r, YGFlexDirection direction, YGJustify justify, YGWrap wrap, float padding, float margin)
29{
30 YGNodeStyleSetWidth(mRootNodeRef, r.W());
31 YGNodeStyleSetHeight(mRootNodeRef, r.H());
32 YGNodeStyleSetFlexDirection(mRootNodeRef, direction);
33 YGNodeStyleSetJustifyContent(mRootNodeRef, justify);
34 YGNodeStyleSetFlexWrap(mRootNodeRef, wrap);
35 YGNodeStyleSetPadding(mRootNodeRef, YGEdgeAll, padding);
36 YGNodeStyleSetMargin(mRootNodeRef, YGEdgeAll, margin);
37}
38
39void IFlexBox::CalcLayout(YGDirection direction)
40{
41 YGNodeCalculateLayout(mRootNodeRef, YGUndefined, YGUndefined, direction);
42}
43
44YGNodeRef IFlexBox::AddItem(float width, float height, YGAlign alignSelf, float grow, float shrink, float margin)
45{
46 int index = mNodeCounter;
47 YGNodeRef child = YGNodeNew();
48
49 if(width == YGUndefined)
50 YGNodeStyleSetWidthAuto(child);
51 else if(width < 0.f)
52 YGNodeStyleSetWidthPercent(child, width * -1.f);
53 else
54 YGNodeStyleSetWidth(child, width);
55
56 if(height == YGUndefined)
57 YGNodeStyleSetHeightAuto(child);
58 else if(height < 0.f)
59 YGNodeStyleSetHeightPercent(child, height * -1.f);
60 else
61 YGNodeStyleSetHeight(child, height);
62
63 YGNodeStyleSetAlignSelf(child, alignSelf);
64 YGNodeStyleSetMargin(child, YGEdgeAll, margin);
65 YGNodeStyleSetFlexGrow(child, grow);
66 YGNodeStyleSetFlexShrink(child, shrink);
67 YGNodeInsertChild(mRootNodeRef, child, index);
68
69 mNodeCounter++;
70
71 return child;
72}
73
74void IFlexBox::AddItem(YGNodeRef child)
75{
76 YGNodeInsertChild(mRootNodeRef, child, mNodeCounter++);
77}
78
80{
81 return IRECT(YGNodeLayoutGetLeft(mRootNodeRef),
82 YGNodeLayoutGetTop(mRootNodeRef),
83 YGNodeLayoutGetLeft(mRootNodeRef) + YGNodeLayoutGetWidth(mRootNodeRef),
84 YGNodeLayoutGetTop(mRootNodeRef) + YGNodeLayoutGetHeight(mRootNodeRef));
85}
86
87IRECT IFlexBox::GetItemBounds(int nodeIndex) const
88{
89 YGNodeRef child = YGNodeGetChild(mRootNodeRef, nodeIndex);
90 return IRECT(YGNodeLayoutGetLeft(mRootNodeRef) + YGNodeLayoutGetLeft(child),
91 YGNodeLayoutGetTop(mRootNodeRef) + YGNodeLayoutGetTop(child),
92 YGNodeLayoutGetLeft(mRootNodeRef) + YGNodeLayoutGetLeft(child) + YGNodeLayoutGetWidth(child),
93 YGNodeLayoutGetTop(mRootNodeRef) + YGNodeLayoutGetTop(child) + YGNodeLayoutGetHeight(child));
94};
95
96// TODO: eventually build Yoga as a static library,
97// for now include Yoga .cpp files here (requires C++20)
98
99// On Apple platforms MacTypes.h declares a global "Style" typedef, which makes the
100// unqualified yoga::Style in YGNodeStyle.cpp (after "using namespace facebook::yoga") ambiguous.
101// Only Yoga's public C headers have been included so far, so renaming here is consistent
102// across all of Yoga's internal headers and sources
103#define Style YGInternalStyle
104#include <yoga/YGConfig.cpp>
105#include <yoga/YGEnums.cpp>
106#include <yoga/YGNode.cpp>
107#include <yoga/YGNodeLayout.cpp>
108#include <yoga/YGNodeStyle.cpp>
109#include <yoga/YGPixelGrid.cpp>
110#include <yoga/YGValue.cpp>
111#include <yoga/algorithm/AbsoluteLayout.cpp>
112#include <yoga/algorithm/Baseline.cpp>
113#include <yoga/algorithm/Cache.cpp>
114#include <yoga/algorithm/CalculateLayout.cpp>
115#include <yoga/algorithm/FlexLine.cpp>
116#include <yoga/algorithm/PixelGrid.cpp>
117#include <yoga/config/Config.cpp>
118#include <yoga/debug/AssertFatal.cpp>
119#include <yoga/debug/Log.cpp>
120#include <yoga/node/LayoutResults.cpp>
121#include <yoga/node/Node.cpp>
122
123// event.cpp declares a file-local "Node" that clashes with yoga::Node in a unity build.
124// All Yoga headers have been included by this point, so the rename only affects event.cpp
125#define Node YGEventSubscriberNode
126#include <yoga/event/event.cpp>
127#undef Node
128#undef Style
YGNodeRef AddItem(float width, float height, YGAlign alignSelf=YGAlignAuto, float grow=0.f, float shrink=1.f, float margin=0.f)
Add a flex item, with some parameters.
void CalcLayout(YGDirection direction=YGDirectionLTR)
Calculate the layout, call after add all items.
IRECT GetItemBounds(int nodeIndex) const
Get the bounds for a particular flex item.
void Init(const IRECT &r, YGFlexDirection direction=YGFlexDirectionRow, YGJustify justify=YGJustifyFlexStart, YGWrap wrap=YGWrapNoWrap, float padding=0.f, float margin=0.f)
Initialize the IFlexBox flex container.
IRECT GetRootBounds() const
Get an IRECT of the root node bounds.
Used to manage a rectangular area, independent of draw class/platform.
float W() const
float H() const