gearmulator

Emulation of classic VA synths of the late 90s/2000s that are based on Motorola 56300 family DSPs
Log | Files | Refs | Submodules | README | LICENSE

info.cpp (5960B)


      1 #include "info.h"
      2 
      3 #include "patchmanager.h"
      4 #include "defaultskin.h"
      5 
      6 #include "jucePluginLib/patchdb/patch.h"
      7 
      8 #include "juceUiLib/uiObject.h"
      9 
     10 #include "../pluginEditor.h"
     11 
     12 namespace jucePluginEditorLib::patchManager
     13 {
     14 	Info::Info(PatchManager& _pm) : m_patchManager(_pm)
     15 	{
     16 		addAndMakeVisible(m_content);
     17 
     18 		m_name = addChild(new juce::Label());
     19 		m_lbSource = addChild(new juce::Label("", "Source"));
     20 		m_source = addChild(new juce::Label());
     21 		m_lbCategories = addChild(new juce::Label("", "Categories"));
     22 		m_categories = addChild(new juce::Label());
     23 		m_lbTags = addChild(new juce::Label("", "Tags"));
     24 		m_tags = addChild(new juce::Label());
     25 
     26 		if(const auto& t = _pm.getTemplate("pm_info_label"))
     27 		{
     28 			t->apply(_pm.getEditor(), *m_lbSource);
     29 			t->apply(_pm.getEditor(), *m_lbCategories);
     30 			t->apply(_pm.getEditor(), *m_lbTags);
     31 		}
     32 		else
     33 		{
     34 			m_lbSource->setColour(juce::Label::textColourId, juce::Colour(defaultSkin::colors::infoLabel));
     35 			m_lbCategories->setColour(juce::Label::textColourId, juce::Colour(defaultSkin::colors::infoLabel));
     36 			m_lbTags->setColour(juce::Label::textColourId, juce::Colour(defaultSkin::colors::infoLabel));
     37 
     38 			m_lbSource->setJustificationType(juce::Justification::bottomLeft);
     39 			m_lbCategories->setJustificationType(juce::Justification::bottomLeft);
     40 			m_lbTags->setJustificationType(juce::Justification::bottomLeft);
     41 		}
     42 
     43 		if (const auto& t = _pm.getTemplate("pm_info_text"))
     44 		{
     45 			t->apply(_pm.getEditor(), *m_source);
     46 			t->apply(_pm.getEditor(), *m_categories);
     47 			t->apply(_pm.getEditor(), *m_tags);
     48 		}
     49 		else
     50 		{
     51 			m_source->setColour(juce::Label::textColourId, juce::Colour(defaultSkin::colors::infoText));
     52 			m_categories->setColour(juce::Label::textColourId, juce::Colour(defaultSkin::colors::infoText));
     53 			m_tags->setColour(juce::Label::textColourId, juce::Colour(defaultSkin::colors::infoText));
     54 
     55 			m_source->setJustificationType(juce::Justification::topLeft);
     56 			m_categories->setJustificationType(juce::Justification::topLeft);
     57 			m_tags->setJustificationType(juce::Justification::topLeft);
     58 		}
     59 
     60 		if (const auto& t = _pm.getTemplate("pm_info_name"))
     61 		{
     62 			t->apply(_pm.getEditor(), *m_name);
     63 		}
     64 		else
     65 		{
     66 			auto f = m_name->getFont();
     67 			f.setHeight(f.getHeight() * 2);
     68 			f.setBold(true);
     69 			m_name->setFont(f);
     70 			m_name->setJustificationType(juce::Justification::topLeft);
     71 			m_name->setColour(juce::Label::textColourId, juce::Colour(defaultSkin::colors::infoHeadline));
     72 			m_name->setColour(juce::Label::backgroundColourId, juce::Colour(defaultSkin::colors::background));
     73 		}
     74 	}
     75 
     76 	Info::~Info()
     77 	{
     78 		m_content.deleteAllChildren();
     79 	}
     80 
     81 	void Info::setPatch(const pluginLib::patchDB::PatchPtr& _patch)
     82 	{
     83 		if (!_patch)
     84 		{
     85 			clear();
     86 			return;
     87 		}
     88 
     89 		if(_patch != m_patch)
     90 		{
     91 			m_patchManager.cancelSearch(m_searchHandle);
     92 
     93 			pluginLib::patchDB::SearchRequest req;
     94 			req.patch = _patch;
     95 
     96 			m_searchHandle = m_patchManager.search(std::move(req));
     97 
     98 			m_patch = _patch;
     99 		}
    100 
    101 		m_name->setText(_patch->getName(), juce::sendNotification);
    102 		m_source->setText(toText(_patch->source.lock()), juce::sendNotification);
    103 		m_categories->setText(toText(_patch->getTags().get(pluginLib::patchDB::TagType::Category)), juce::sendNotification);
    104 		m_tags->setText(toText(_patch->getTags().get(pluginLib::patchDB::TagType::Tag)), juce::sendNotification);
    105 
    106 		doLayout();
    107 	}
    108 
    109 	void Info::clear()
    110 	{
    111 		m_patch.reset();
    112 		m_patchManager.cancelSearch(m_searchHandle);
    113 		m_searchHandle = pluginLib::patchDB::g_invalidSearchHandle;
    114 
    115 		m_name->setText({}, juce::sendNotification);
    116 		m_source->setText({}, juce::sendNotification);
    117 		m_categories->setText({}, juce::sendNotification);
    118 		m_tags->setText({}, juce::sendNotification);
    119 
    120 		doLayout();
    121 	}
    122 
    123 	std::string Info::toText(const pluginLib::patchDB::Tags& _tags)
    124 	{
    125 		const auto& tags = _tags.getAdded();
    126 		std::stringstream ss;
    127 
    128 		size_t i = 0;
    129 		for (const auto& tag : tags)
    130 		{
    131 			if (i)
    132 				ss << ", ";
    133 			ss << tag;
    134 			++i;
    135 		}
    136 		return ss.str();
    137 	}
    138 
    139 	std::string Info::toText(const pluginLib::patchDB::DataSourceNodePtr& _source)
    140 	{
    141 		if (!_source)
    142 			return {};
    143 
    144 		switch (_source->type)
    145 		{
    146 		case pluginLib::patchDB::SourceType::Invalid:
    147 		case pluginLib::patchDB::SourceType::Count:
    148 			return {};
    149 		case pluginLib::patchDB::SourceType::Rom:
    150 		case pluginLib::patchDB::SourceType::Folder:
    151 			return _source->name;
    152 		case pluginLib::patchDB::SourceType::LocalStorage:
    153 			return _source->name;
    154 		case pluginLib::patchDB::SourceType::File:
    155 			{
    156 				auto t = _source->name;
    157 				const auto pos = t.find_last_of("\\/");
    158 				if (pos != std::string::npos)
    159 					return t.substr(pos + 1);
    160 				return t;
    161 			}
    162 		}
    163 		return {};
    164 	}
    165 
    166 	void Info::paint(juce::Graphics& g)
    167 	{
    168 		g.fillAll(m_name->findColour(juce::Label::backgroundColourId));
    169 	}
    170 
    171 	void Info::processDirty(const pluginLib::patchDB::Dirty& _dirty)
    172 	{
    173 		if(_dirty.searches.find(m_searchHandle) == _dirty.searches.end())
    174 			return;
    175 
    176 		setPatch(m_patch);
    177 	}
    178 
    179 	juce::Label* Info::addChild(juce::Label* _label)
    180 	{
    181 		m_content.addAndMakeVisible(_label);
    182 		return _label;
    183 	}
    184 
    185 	void Info::doLayout() const
    186 	{
    187 		juce::FlexBox fb;
    188 		fb.flexWrap = juce::FlexBox::Wrap::noWrap;
    189 		fb.justifyContent = juce::FlexBox::JustifyContent::flexStart;
    190 		fb.alignContent = juce::FlexBox::AlignContent::flexStart;
    191 		fb.flexDirection = juce::FlexBox::Direction::column;
    192 
    193 		for (const auto& cChild : m_content.getChildren())
    194 		{
    195 			juce::FlexItem item(*cChild);
    196 			item = item.withWidth(static_cast<float>(getWidth()));
    197 
    198 			const auto* label = dynamic_cast<const juce::Label*>(cChild);
    199 			if (label)
    200 			{
    201 				const auto t = label->getText();
    202 				int lineCount = 1;
    203 				for (const auto ch : t)
    204 				{
    205 					if (ch == '\n')
    206 						++lineCount;
    207 				}
    208 				item = item.withHeight(label->getFont().getHeight() * (static_cast<float>(lineCount) + 1.5f));
    209 			}
    210 			fb.items.add(item);
    211 		}
    212 
    213 		fb.performLayout(m_content.getLocalBounds());
    214 	}
    215 
    216 	void Info::resized()
    217 	{
    218 		m_content.setSize(getWidth(), getHeight());
    219 		doLayout();
    220 	}
    221 }