iPlug2 - C++ Audio Plug-in Framework
TestFlexBoxControl.h
Go to the documentation of this file.
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 #pragma once
12 
18 #include "IControl.h"
19 #include "IGraphicsFlexBox.h"
20 
21 static const std::initializer_list<const char*> alignStrs = { "YGAlignAuto", "YGAlignFlexStart", "YGAlignCenter", "YGAlignFlexEnd", "YGAlignStretch", "YGAlignBaseline", "YGAlignSpaceBetween", "YGAlignSpaceAround" };
22 static const std::initializer_list<const char*> dirStrs = { "YGFlexDirectionColumn", "YGFlexDirectionColumnReverse", "YGFlexDirectionRow", "YGFlexDirectionRowReverse" };
23 static const std::initializer_list<const char*> justifyStrs = { "YGJustifyFlexStart", "YGJustifyCenter", "YGJustifyFlexEnd", "YGJustifySpaceBetween", "YGJustifySpaceAround", "YGJustifySpaceEvenly" };
24 static const std::initializer_list<const char*> wrapStrs = { "YGWrapNoWrap", "YGWrapWrap", "YGWrapWrapReverse" };
25 
29 {
30 public:
31  TestFlexBoxControl(const IRECT& rect)
32  : IControl(rect)
33  {
34  mMenu.GetItem(0)->SetSubmenu(new IPopupMenu("Align", alignStrs));
35  mMenu.GetItem(1)->SetSubmenu(new IPopupMenu("Direction", dirStrs));
36  mMenu.GetItem(2)->SetSubmenu(new IPopupMenu("Justify", justifyStrs));
37  mMenu.GetItem(3)->SetSubmenu(new IPopupMenu("Wrap", wrapStrs));
38 
39  mText = IText(70.f, EVAlign::Middle);
40  SetTooltip("TestFlexboxControl");
41  }
42 
43  void Draw(IGraphics& g) override
44  {
45  g.FillRect(COLOR_WHITE, mRootRect);
46 
47  WDL_String str;
48  for (int i=0; i<7; i++)
49  {
50  g.FillRect(GetRainbow(i), mItemRects[i]);
51  g.DrawRect(COLOR_BLACK, mItemRects[i]);
52  str.SetFormatted(2, "%i", i);
53  g.DrawText(mText, str.Get(), mItemRects[i]);
54  }
55 
56  IRECT textRect;
57  g.MeasureText(mText.WithSize(14.f), mSettingsStr.Get(), textRect);
58  IRECT adjusted = mRECT.GetCentredInside(textRect);
59  g.FillRect(COLOR_WHITE.WithOpacity(0.5f), adjusted);
60  g.DrawRect(COLOR_BLACK, adjusted);
61  g.DrawText(mText.WithSize(14.f), mSettingsStr.Get(), adjusted);
62  }
63 
64  void OnResize() override
65  {
66  DoLayout();
67  }
68 
69  void DoLayout()
70  {
71  mItemRects.clear();
72 
73  for (int i=0; i<7; i++)
74  {
75  mItemRects.push_back(IRECT());
76  }
77 
78  IFlexBox f;
79  f.Init(mRECT, YGFlexDirection(mDirection), YGJustify(mJustify), YGWrap(mWrap));
80 
81  YGNodeRef r;
82 
83  for (int i=0; i<7; i++)
84  {
85  r = f.AddItem(100.f, 100.f, YGAlign(mAlign));
86  }
87 
88  f.CalcLayout();
89 
90  for (int i=0; i<7; i++)
91  {
92  mItemRects[i] = mRECT.Inset(f.GetItemBounds(i));
93  }
94 
95  mRootRect = mRECT.Inset(f.GetRootBounds());
96  mSettingsStr.SetFormatted(256, "%s, %s, %s, %s", alignStrs.begin()[mAlign], dirStrs.begin()[mDirection], justifyStrs.begin()[mJustify], wrapStrs.begin()[mWrap]);
97  }
98 
99  void OnMouseDown(float x, float y, const IMouseMod& mod) override
100  {
101  mMenu.GetItem(0)->GetSubmenu()->CheckItemAlone(mAlign);
102  mMenu.GetItem(1)->GetSubmenu()->CheckItemAlone(mDirection);
103  mMenu.GetItem(2)->GetSubmenu()->CheckItemAlone(mJustify);
104  mMenu.GetItem(3)->GetSubmenu()->CheckItemAlone(mWrap);
105 
106  GetUI()->CreatePopupMenu(*this, mMenu, x, y);
107  }
108 
109  void OnPopupMenuSelection(IPopupMenu* pSelectedMenu, int) override
110  {
111  if(pSelectedMenu)
112  {
113  int idx = pSelectedMenu->GetChosenItemIdx();
114  if (strcmp(pSelectedMenu->GetRootTitle(), "Align") == 0) { mAlign = idx; }
115  else if(strcmp(pSelectedMenu->GetRootTitle(), "Direction") == 0) { mDirection = idx; }
116  else if(strcmp(pSelectedMenu->GetRootTitle(), "Justify") == 0) { mJustify = idx; }
117  else if(strcmp(pSelectedMenu->GetRootTitle(), "Wrap") == 0) { mWrap = idx; }
118  }
119 
120  DoLayout();
121  SetDirty(false);
122  }
123 
124 private:
125  WDL_String mSettingsStr;
126  IPopupMenu mMenu {"FlexBox", {"Align", "Direction", "Justify", "Wrap"}};
127  IRECT mRootRect;
128  int mAlign = YGAlignAuto;
129  int mDirection = YGFlexDirectionColumn;
130  int mJustify = YGJustifyFlexStart;
131  int mWrap = YGWrapNoWrap;
132  std::vector<IRECT> mItemRects;
133 };
IRECT Inset(const IRECT &rhs) const
Offsets the input IRECT based on the parent.
The lowest level base class of an IGraphics control.
Definition: IControl.h:42
Used to manage a rectangular area, independent of draw class/platform.
virtual void DrawRect(const IColor &color, const IRECT &bounds, const IBlend *pBlend=0, float thickness=1.f)
Draw a rectangle to the graphics context.
Definition: IGraphics.cpp:2475
Control to test IGraphicsFlexBox.
Used to manage mouse modifiers i.e.
void OnResize() override
Called when IControl is constructed or resized using SetRect().
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 OnPopupMenuSelection(IPopupMenu *pSelectedMenu, int) override
Implement this method to handle popup menu selection after IGraphics::CreatePopupMenu/IControl::Promp...
void SetTooltip(const char *str)
Set a tooltip for the control.
Definition: IControl.h:210
void DrawText(const IText &text, const char *str, const IRECT &bounds, const IBlend *pBlend=0)
Draw some text to the graphics context in a specific rectangle.
Definition: IGraphics.cpp:631
void Draw(IGraphics &g) override
Draw the control to the graphics context.
This file contains the base IControl implementation, along with some base classes for specific types ...
void CreatePopupMenu(IControl &control, IPopupMenu &menu, const IRECT &bounds, int valIdx=0)
Shows a pop up/contextual menu in relation to a rectangular region of the graphics context...
Definition: IGraphics.cpp:1937
IColor WithOpacity(float alpha) const
Returns a new IColor with a different opacity.
A class for setting the contents of a pop up menu.
IText is used to manage font and text/text entry style for a piece of text on the UI...
IControl(const IRECT &bounds, int paramIdx=kNoParameter, IActionFunction actionFunc=nullptr)
Constructor.
Definition: IControl.cpp:81
IRECT GetItemBounds(int nodeIndex) const
Get the bounds for a particular flex item.
IRECT GetRootBounds() const
Get an IRECT of the root node bounds.
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:86
virtual void FillRect(const IColor &color, const IRECT &bounds, const IBlend *pBlend=0)
Fill a rectangular region of the graphics context with a color.
Definition: IGraphics.cpp:2547
IFlexBox is a basic C++ helper for Yoga https://yogalayout.com.
void OnMouseDown(float x, float y, const IMouseMod &mod) override
Implement this method to respond to a mouse down event on this control.
IGraphics * GetUI()
Definition: IControl.h:452
IRECT GetCentredInside(const IRECT &sr) const
Get a rectangle the size of sr but with the same center point as this rectangle.
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.
virtual float MeasureText(const IText &text, const char *str, IRECT &bounds) const
Measure the rectangular region that some text will occupy.
Definition: IGraphics.cpp:639
virtual void SetDirty(bool triggerAction=true, int valIdx=kNoValIdx)
Mark the control as dirty, i.e.
Definition: IControl.cpp:196