


HIGH LEVEL CONTROLS


namespace gui
{
	struct HighLevelKnobsGroup :
		public Comp
	{
		HighLevelKnobsGroup(Utils& u) :
			Comp(u, "", CursorType::Default),
			knobs
			{
#if PPDHasGainIn
				Knob(u, "> Gain", PID::GainIn),
#endif
				Knob(u, "Mix", PID::Mix),
#if PPDHasLookahead
				Knob(u, "Lookahead", PID::Lookahead),
#endif
				Knob(u, "Gain >", PID::Gain)
			},
			meters
			{
#if PPDHasGainIn
				KnobMeter(knobs.front(), u.getMeter(audio::Meters::Type::In)),
#endif
				KnobMeter(knobs.back(), u.getMeter(audio::Meters::Type::Out))
			}
		{
			for (auto& k : knobs)
				addAndMakeVisible(k);
		}
	protected:
		std::array<Knob, param::NumHighLevelKnobs> knobs;
		std::array<KnobMeter, param::NumHighLevelGainKnobs> meters;

		void paint(Graphics&) override {}

		void resized() override
		{
			auto x = 0.f;
			auto y = 0.f;
			auto w = static_cast<float>(getWidth()) / static_cast<float>(knobs.size());
			auto h = static_cast<float>(getHeight());

			for (auto& knob : knobs)
			{
				knob.setBounds(maxQuadIn({ x,y,w,h }).toNearestInt());
				x += w;
			}
		}
	};

	struct HighLevelButtonsGroup :
		public Comp
	{
		static constexpr int NumButtons = param::NumHighLevelParams - param::NumHighLevelKnobs - 1;

		HighLevelButtonsGroup(Utils& u) :
			Comp(u, "", CursorType::Default),
			buttons
			{
#if PPDHasUnityGain
				Button(u, param::toTooltip(PID::UnityGain)),
#endif
#if PPDHasHQ
				Button(u, param::toTooltip(PID::HQ)),
#endif
				Button(u, param::toTooltip(PID::Polarity)),
				Button(u, param::toTooltip(PID::StereoConfig)),
				Button(u, param::toTooltip(PID::Bypass))
			}
		{
			auto idx = NumButtons - 1;
			makeParameterSwitchButton(buttons[idx], PID::Bypass, ButtonSymbol::Bypass);
			--idx;
			makeParameterSwitchButton(buttons[idx], PID::StereoConfig, ButtonSymbol::StereoConfig);
			--idx;
			makeParameterSwitchButton(buttons[idx], PID::Polarity, ButtonSymbol::Polarity);
#if PPDHasHQ
			--idx;
			makeParameterSwitchButton(buttons[idx], PID::HQ, "HQ");
#endif
#if PPDHasUnityGain
			--idx;
			makeParameterSwitchButton(buttons[idx], PID::UnityGain, ButtonSymbol::UnityGain);
#endif
			
			for (auto& b : buttons)
				addAndMakeVisible(b);
		}
	protected:
		std::array<Button, NumButtons> buttons;

		void paint(Graphics&) override {}

		void resized() override
		{
			auto x = 0.f;
			auto y = 0.f;
			auto w = static_cast<float>(getWidth()) / static_cast<float>(buttons.size());
			auto h = static_cast<float>(getHeight());

			for (auto& button : buttons)
			{
				button.setBounds(maxQuadIn({ x,y,w,h }).toNearestInt());
				x += w;
			}
		}
	};

	struct HighLevel :
		public Comp
	{
		HighLevel(Utils& u) :
			Comp(u, "", CursorType::Default),
			layout(*this),
			pluginTitle(u, static_cast<String>(JucePlugin_Name)),
			options(u, "you can find additional plugin options here."),
			knobsGroup(u),
			buttonsGroup(u),
			patchXSelect(u, "Select Patch X to modify its parameter values."),
			patchYSelect(u, "Select Patch Y to modify its parameter values."),
			patchXSolo(u, param::toTooltip(PID::PatchXSolo)),
			patchYSolo(u, param::toTooltip(PID::PatchYSolo)),
			macro(u, "", "Interpolate between Patch X and Y.", PID::Macro)
		{
			setInterceptsMouseClicks(false, true);

			pluginTitle.outlineCID = ColourID::Transp;

			makeTextButton(options, "opt", true);

			patchXSelect.setHitBoxType(HitBox::Type::ConvexClockwise);
			patchYSelect.setHitBoxType(HitBox::Type::ConvexClockwise);
			makeTextButton(patchXSelect, "X", true, true);
			makeTextButton(patchYSelect, "Y", true, true);

			makeParameterSwitchButton(patchXSolo, PID::PatchXSolo, "S");
			makeParameterSwitchButton(patchYSolo, PID::PatchYSolo, "S");

			addAndMakeVisible(pluginTitle);
			addAndMakeVisible(options);
			addAndMakeVisible(knobsGroup);
			addAndMakeVisible(buttonsGroup);
			addAndMakeVisible(patchXSelect);
			addAndMakeVisible(patchYSelect);
			addAndMakeVisible(patchXSolo);
			addAndMakeVisible(patchYSolo);
			addAndMakeVisible(macro);

			layout.init(
				{ 8, 12, 10, 50, 10, 12, 8 },
				{ 5, 10, 20, 8, 8, 70 }
			);
		}

	protected:
		Layout layout;
		Label pluginTitle;
		Button options;
		HighLevelKnobsGroup knobsGroup;
		HighLevelButtonsGroup buttonsGroup;
		Button patchXSelect, patchYSelect, patchXSolo, patchYSolo;
		Dial macro;

		void paint(Graphics& g) override
		{
			const auto thicc = utils.thicc();

			//Comp::paint(g);
			//layout.paint(g);

			g.setColour(Colours::c(ColourID::Txt));
			Path path;
			make(path, layout, { {0,2}, {2,2}, {3,4}, {4,4}, {5,2}, {7,2} });
			g.strokePath(path, juce::PathStrokeType(thicc));
		}

		void resized() override
		{
			layout.resized();

			const auto thicc = utils.thicc();
			const auto thicc3 = thicc * 3.f;

			pluginTitle.setBounds(layout(3, 0, 1, 2, false).reduced(thicc3).toNearestInt());
			options.setBounds(layout(0, 0, 1, 2, true).reduced(thicc).toNearestInt());
			layout.place(knobsGroup, 3, 2, 1, 1, false);
			layout.place(buttonsGroup, 3, 3, 1, 1, false);
			layout.place(macro, 3, 4, 1, 1, false);

			layout.place(patchXSelect, 0, 2, 3, 2, false);
			layout.place(patchYSelect, 4, 2, 3, 2, false);

			{
				const auto off = PointF(0.f, 0.f) - layout(4, 2);
				{
					Path path;
					make(path, layout, { { 0,2 }, { 2,2 }, { 3,4 }, { 0,4 } });
					path.applyTransform(Affine::translation(0.f, off.y));
					path.closeSubPath();

					patchXSelect.updateHitBox(path);
				}
				{
					Path path;
					make(path, layout, { { 4,4 }, { 5,2 }, { 7,2 }, { 7,4 } });
					path.applyTransform(Affine::translation(off));
					path.closeSubPath();

					patchYSelect.updateHitBox(path);
				}
			}

			layout.place(patchXSolo, 2, 4, 1, 1, true);
			layout.place(patchYSolo, 4, 4, 1, 1, true);
		}
	};
}











HITBOX


struct HitBox
	{
		enum class Type { Rect, ConvexClockwise, NumTypes };

		HitBox(const Component& _comp) :
			comp(_comp),
			points(),
			area(),
			type(Type::Rect)
		{}

		void setType(Type t) noexcept { type = t; }

		void update(const Path& _area)
		{
			const auto w = comp.getWidth();
			const auto h = comp.getHeight();

			points.resize(w);
			for (auto& pt : points)
				pt.resize(h, true);

			if (type == Type::ConvexClockwise) // right hand turn
			{
				area = _area;

				for (auto x = 0; x < w; ++x)
				{
					for (auto y = 0; y < h; ++y)
					{
						bool inShape = true;

						juce::PathFlatteningIterator it(area);
						while (it.next())
						{
							Point a(it.x1, it.y1);
							Point b(it.x2, it.y2);
							Point c(x, y);

							const auto cp = ((b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x));

							if (cp == 0.f) // colinear
							{
								inShape = true;
								break;
							}
							else if (cp < 0.f) // left-hand turn
							{
								inShape = false;
								break;
							}
						}

						points[x][y] = inShape;
					}
				}
			}
		}

		bool operator()(int x, int y) noexcept
		{
			if (type == Type::Rect)
				return true;
			return points[x][y];
		}

		const Path& getArea() noexcept
		{
			if (type == Type::Rect)
			{
				area.clear();
				area.addRectangle(comp.getLocalBounds().toFloat());
			}
			return area;
		}

	protected:
		const Component& comp;
		std::vector<std::vector<bool>> points;
		Path area;
		Type type;
	};








// LAYOUT OF HIGHLEVEL COMPONENT:

struct HighLevel :
		public Comp
	{
		HighLevel(Utils& u, LowLevel* _lowLevel) :
			Comp(u, "", CursorType::Default),
			pluginTitle(u, JucePlugin_Name),
			macro(u, "Macro", PID::Macro, false),
#if PPDHasGainIn
			gainIn(u, "Gain >", PID::GainIn, false),
			meterIn(gainIn, u.getMeter(0)),
#endif
			gainOut(u, "< Gain", PID::Gain, false),
			meterOut(gainOut, u.getMeter(PPDHasGainIn ? 1 : 0)),
			mix(u, "Mix", PID::Mix, false),
#if PPDHasUnityGain
			unityGain(u, param::toTooltip(PID::UnityGain)),
#endif
#if PPDHasHQ
			hq(u, param::toTooltip(PID::HQ)),
#endif
			stereoConfig(u, param::toTooltip(PID::StereoConfig)),
			power(u, param::toTooltip(PID::Power)),
			polarity(u, param::toTooltip(PID::Polarity)),
			patchSelect{
				Button(u, param::toTooltip(PID::PatchSelect)),
				Button(u, param::toTooltip(PID::PatchSelect))
			},
			patchMode(u, param::toTooltip(PID::PatchMode)),

			lowLevel(_lowLevel),

			menu(nullptr),
			menuButton(u, "Click here to open or close the panel with the advanced settings.")
		{
			layout.init(
				{ 5, 30, 30, 30, 5, 5 },
				{ 5, 15, 30, 30, 40, 15, 5, 15, 15, 5 }
			);

			pluginTitle.font = getFontNEL();

			addAndMakeVisible(pluginTitle);
			addAndMakeVisible(macro);
#if PPDHasGainIn
			addAndMakeVisible(gainIn);
#endif
			addAndMakeVisible(gainOut);
			addAndMakeVisible(mix);
#if PPDHasUnityGain
			makeParameterSwitchButton(unityGain, PID::UnityGain, ButtonSymbol::UnityGain);
			addAndMakeVisible(unityGain);
#endif
#if PPDHasHQ
			makeParameterSwitchButton(hq, PID::HQ, "HQ");
			addAndMakeVisible(hq);
#endif
			makeParameterSwitchButton(stereoConfig, PID::StereoConfig, ButtonSymbol::StereoConfig);
			addAndMakeVisible(stereoConfig);

			makeParameterSwitchButton(power, PID::Power, ButtonSymbol::Power);
			addAndMakeVisible(power);

			makeParameterSwitchButton(polarity, PID::Polarity, ButtonSymbol::Polarity);
			addAndMakeVisible(polarity);

			makeParameterButtonsGroup(patchSelect, PID::PatchSelect, "AB", true);
			for(auto i = 0; i < 2; ++i)
			{
				auto& ab = patchSelect[i];
				auto& label = ab.getLabel();
				label.textCID = ColourID::Mod;
				addAndMakeVisible(ab);
			}

			makeParameterSwitchButton(patchMode, PID::PatchMode, ButtonSymbol::PatchMode);
			addAndMakeVisible(patchMode);

			makeSymbolButton(menuButton, ButtonSymbol::Settings);
			menuButton.toggleState = 0;
			menuButton.onClick.push_back([this]()
			{
				auto& btn = menuButton;

				auto& ts = btn.toggleState;
				ts = ts == 0 ? 1 : 0;
				repaintWithChildren(&btn);

				if (ts == 1)
				{
					auto& pluginTop = utils.pluginTop;

					const auto xml = loadXML(BinaryData::menu_xml, BinaryData::menu_xmlSize);
					if (xml == nullptr)
						return;
					const auto vt = ValueTree::fromXml(*xml);
					if (!vt.isValid())
						return;

					menu.reset(new Menu(utils, vt));
					pluginTop.addAndMakeVisible(*menu);

					const auto bounds1 = lowLevel->getBounds().toFloat();
					const auto bounds0 = bounds1.withLeft(static_cast<float>(pluginTop.getRight()));

					menu->defineBounds(bounds0, bounds1);
					menu->initWidget(.1f, false);
				}
				else
				{
					menu.reset(nullptr);
				}
			});
			addAndMakeVisible(menuButton);

			setInterceptsMouseClicks(false, true);
		}

		void paint(Graphics& g) override
		{
			//Comp::paint(g);
			//layout.paint(g);

			g.setColour(Colours::c(ColourID::Hover));
			layout.label(g, "delta", 3.f, 1.25f, .5f, .75f, true);
			layout.label(g, "<", 1.25f, 5, .25f, 1, false);
			layout.label(g, "preset browser", 1.5f, 5, 1.5f, 1, false);
			layout.label(g, ">", 3.f, 5, .25f, 1, false);
			layout.label(g, "save", 3.25f, 5, .5f, 1, false);
			layout.label(g, "<\n<\n<", 5, 0, 1, 10, false);
			
			const auto thicc = utils.thicc();
			auto thiccI = static_cast<int>(thicc) / 2;
			if (thiccI == 0)
				thiccI = 1;
			g.setColour(Colours::c(ColourID::Txt));
			{
				const auto y = static_cast<int>(layout.getY(4.5f));
				const auto left = layout.getX(1.5f);
				const auto right = layout.getX(2.125f);
				for (auto i = -thiccI; i < thiccI; ++i)
					g.drawHorizontalLine(y + i, left, right);
			}
#if PPDHasUnityGain
			{
				const auto y = static_cast<int>(layout.getY(3.5f));
				{
					const auto left = layout.getX(2.f);
					const auto right = layout.getX(2.125f);
					for (auto i = -thiccI; i < thiccI; ++i)
						g.drawHorizontalLine(y + i, left, right);
				}
				{
					const auto right = layout.getX(3.f);
					const auto left = layout.getX(2.875f);
					for (auto i = -thiccI; i < thiccI; ++i)
						g.drawHorizontalLine(y + i, left, right);
				}
			}
#endif
		}

		void resized() override
		{
			layout.resized();

			layout.place(pluginTitle, 1.25f, 8, 2.5f, 1, false);

			layout.place(macro, 2, 4, 1, 1, true);
#if PPDHasGainIn
			layout.place(gainIn, 1, 3, 1, 1, true);
#endif
			layout.place(gainOut, 3, 3, 1, 1, true);
			layout.place(mix, 2, 2, 1, 1, true);
#if PPDHasUnityGain
			layout.place(unityGain, 2.25f, 3.25f, .5f, .5f, true);
#endif
#if PPDHasHQ
			layout.place(hq, 2.f, 7, 1.f, 1, true);
#endif
			layout.place(stereoConfig, 3.f, 7, 1.f, 1, true);
			layout.place(power, 2.25f, 1.f, .5f, .75f, true);
			layout.place(polarity, 1.5f, 1.25f, .5f, .75f, true);
			layout.place(patchSelect[0], 1.5f + .125f, 4.5f, .5f, .5f, true);
			layout.place(patchSelect[1], 2.75f + .125f, 4.5f, .5f, .5f, true);
			layout.place(patchMode, 1.125f, 4.25f, .5f, .5f, true);

			layout.place(menuButton, 1.f, 7, 1.f, 1, true);

			if (menu != nullptr)
			{
				menu->defineBounds(menu->getBounds().toFloat(), lowLevel->getBounds().toFloat());
				menu->initWidget(.3f, false);
			}
		}

	protected:
		Label pluginTitle;
		Knob macro;
#if PPDHasGainIn
		Knob gainIn;
		KnobMeter meterIn;
#endif
		Knob gainOut;
		KnobMeter meterOut;
		Knob mix;
#if PPDHasUnityGain
		Button unityGain;
#endif
#if PPDHasHQ
		Button hq;
#endif
		Button stereoConfig;
		Button power;
		Button polarity;
		std::array<Button, 2> patchSelect;
		Button patchMode;

		LowLevel* lowLevel;
		std::unique_ptr<Menu> menu;
		Button menuButton;
	};






;





