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
98#include "YGLayout.cpp"
99#include "YGEnums.cpp"
100#include "YGNodePrint.cpp"
101#include "YGValue.cpp"
102#include "YGConfig.cpp"
103#include "YGNode.cpp"
104#include "YGStyle.cpp"
105#include "Yoga.cpp"
106#include "Utils.cpp"
107#include "log.cpp"
108#include "event/event.cpp"
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