iPlug2 - C++ Audio Plug-in Framework
IGraphicsFlexBox.cpp
1 #include "IGraphicsFlexBox.h"
2 
3 using namespace iplug;
4 using namespace igraphics;
5 
6 IFlexBox::IFlexBox()
7 {
8  mConfigRef = YGConfigNew();
9  mRootNodeRef = YGNodeNewWithConfig(mConfigRef);
10 }
11 
12 IFlexBox::~IFlexBox()
13 {
14  YGNodeFreeRecursive(mRootNodeRef);
15  YGConfigFree(mConfigRef);
16 }
17 
18 void IFlexBox::Init(const IRECT& r, YGFlexDirection direction, YGJustify justify, YGWrap wrap, float padding, float margin)
19 {
20  YGNodeStyleSetWidth(mRootNodeRef, r.W());
21  YGNodeStyleSetHeight(mRootNodeRef, r.H());
22  YGNodeStyleSetFlexDirection(mRootNodeRef, direction);
23  YGNodeStyleSetJustifyContent(mRootNodeRef, justify);
24  YGNodeStyleSetFlexWrap(mRootNodeRef, wrap);
25  YGNodeStyleSetPadding(mRootNodeRef, YGEdgeAll, padding);
26  YGNodeStyleSetMargin(mRootNodeRef, YGEdgeAll, margin);
27 }
28 
29 void IFlexBox::CalcLayout(YGDirection direction)
30 {
31  YGNodeCalculateLayout(mRootNodeRef, YGUndefined, YGUndefined, direction);
32 }
33 
34 YGNodeRef IFlexBox::AddItem(float width, float height, YGAlign alignSelf, float grow, float shrink, float margin)
35 {
36  int index = mNodeCounter;
37  YGNodeRef child = YGNodeNew();
38 
39  if(width == YGUndefined)
40  YGNodeStyleSetWidthAuto(child);
41  else if(width < 0.f)
42  YGNodeStyleSetWidthPercent(child, width * -1.f);
43  else
44  YGNodeStyleSetWidth(child, width);
45 
46  if(height == YGUndefined)
47  YGNodeStyleSetHeightAuto(child);
48  else if(height < 0.f)
49  YGNodeStyleSetHeightPercent(child, height * -1.f);
50  else
51  YGNodeStyleSetHeight(child, height);
52 
53  YGNodeStyleSetAlignSelf(child, alignSelf);
54  YGNodeStyleSetMargin(child, YGEdgeAll, margin);
55  YGNodeStyleSetFlexGrow(child, grow);
56  YGNodeStyleSetFlexShrink(child, shrink);
57  YGNodeInsertChild(mRootNodeRef, child, index);
58 
59  mNodeCounter++;
60 
61  return child;
62 }
63 
64 void IFlexBox::AddItem(YGNodeRef child)
65 {
66  YGNodeInsertChild(mRootNodeRef, child, mNodeCounter++);
67 }
68 
70 {
71  return IRECT(YGNodeLayoutGetLeft(mRootNodeRef),
72  YGNodeLayoutGetTop(mRootNodeRef),
73  YGNodeLayoutGetLeft(mRootNodeRef) + YGNodeLayoutGetWidth(mRootNodeRef),
74  YGNodeLayoutGetTop(mRootNodeRef) + YGNodeLayoutGetHeight(mRootNodeRef));
75 }
76 
77 IRECT IFlexBox::GetItemBounds(int nodeIndex) const
78 {
79  YGNodeRef child = YGNodeGetChild(mRootNodeRef, nodeIndex);
80  return IRECT(YGNodeLayoutGetLeft(mRootNodeRef) + YGNodeLayoutGetLeft(child),
81  YGNodeLayoutGetTop(mRootNodeRef) + YGNodeLayoutGetTop(child),
82  YGNodeLayoutGetLeft(mRootNodeRef) + YGNodeLayoutGetLeft(child) + YGNodeLayoutGetWidth(child),
83  YGNodeLayoutGetTop(mRootNodeRef) + YGNodeLayoutGetTop(child) + YGNodeLayoutGetHeight(child));
84 };
85 
86 // TODO: eventually build Yoga as a static library,
87 // for now include Yoga .cpp files here
88 #include "YGLayout.cpp"
89 #include "YGEnums.cpp"
90 #include "YGNodePrint.cpp"
91 #include "YGValue.cpp"
92 #include "YGConfig.cpp"
93 #include "YGNode.cpp"
94 #include "YGStyle.cpp"
95 #include "Yoga.cpp"
96 #include "Utils.cpp"
97 #include "log.cpp"
98 #include "event/event.cpp"
Used to manage a rectangular area, independent of draw class/platform.
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.
float H() const
float W() const
IRECT GetItemBounds(int nodeIndex) const
Get the bounds for a particular flex item.
IRECT GetRootBounds() const
Get an IRECT of the root node bounds.
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.
void CalcLayout(YGDirection direction=YGDirectionLTR)
Calculate the layout, call after add all items.