From 9e2b914a081098ed4e793af87653c63a6fbe283b Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Mon, 8 Feb 2021 19:53:31 +0100 Subject: [PATCH 1/2] Update control panel for efficient updates, preserving values --- plugins/editor/src/editor/GUIComponents.cpp | 166 ++++++++++---------- plugins/editor/src/editor/GUIComponents.h | 6 + 2 files changed, 88 insertions(+), 84 deletions(-) diff --git a/plugins/editor/src/editor/GUIComponents.cpp b/plugins/editor/src/editor/GUIComponents.cpp index e188dbf5..b349ecef 100644 --- a/plugins/editor/src/editor/GUIComponents.cpp +++ b/plugins/editor/src/editor/GUIComponents.cpp @@ -11,6 +11,7 @@ #include "utility/vstgui_before.h" #include "vstgui/lib/cdrawcontext.h" #include "vstgui/lib/cgraphicspath.h" +#include "vstgui/lib/cvstguitimer.h" #include "vstgui/lib/cframe.h" #include "utility/vstgui_after.h" @@ -527,70 +528,23 @@ SControlsPanel::SControlsPanel(const CRect& size) setBackgroundColor(CColor(0x00, 0x00, 0x00, 0x00)); setScrollbarWidth(10.0); + + relayoutTrigger_ = makeOwned( + [this](CVSTGUITimer* timer) { timer->stop(); updateLayout(); }, + 1, false); } void SControlsPanel::setControlUsed(uint32_t index, bool used) { - bool changed = false; - - if (used) { - if (index + 1 > slots_.size()) - slots_.resize(index + 1); - ControlSlot* slot = slots_[index].get(); - changed = !slot; - if (changed) { - slot = new ControlSlot; - slots_[index].reset(slot); - - // create controls etc... - CCoord knobWidth = 48.0; - CCoord knobHeight = knobWidth; - CCoord labelWidth = 96.0; - CCoord labelHeight = 24.0; - CCoord verticalPadding = 0.0; - - CCoord totalWidth = std::max(knobWidth, labelWidth); - CCoord knobX = (totalWidth - knobWidth) / 2.0; - CCoord labelX = (totalWidth - labelWidth) / 2.0; - - CRect knobBounds(knobX, 0.0, knobX + knobWidth, knobHeight); - CRect labelBounds(labelX, knobHeight + verticalPadding, labelX + labelWidth, knobHeight + verticalPadding + labelHeight); - CRect boxBounds = CRect(knobBounds).unite(labelBounds); - - SharedPointer knob = owned(new SStyledKnob(knobBounds, listener_.get(), index)); - SharedPointer label = owned(new CTextLabel(labelBounds)); - SharedPointer box = owned(new CViewContainer(boxBounds)); - - box->addView(knob); - knob->remember(); - box->addView(label); - label->remember(); - - box->setBackgroundColor(CColor(0x00, 0x00, 0x00, 0x00)); - label->setStyle(CTextLabel::kRoundRectStyle); - label->setRoundRectRadius(5.0); - label->setBackColor(CColor(0x2e, 0x34, 0x36)); - label->setTextTruncateMode(CTextLabel::kTruncateTail); - label->setTextInset({4.0, 0.0}); - label->setText(getDefaultLabelText(index)); - knob->setActiveTrackColor(CColor(0x00, 0xb6, 0x2a)); - knob->setInactiveTrackColor(CColor(0x30, 0x30, 0x30)); - knob->setLineIndicatorColor(CColor(0x00, 0x00, 0x00)); - - slot->knob = knob; - slot->label = label; - slot->box = box; - } + ControlSlot* slot = getSlot(index); + if (!slot && !used) + return; + if (!slot) + slot = getOrCreateSlot(index); + if (used != slot->used) { + slot->used = used; + relayoutTrigger_->start(); } - else { - if (index < slots_.size() && slots_[index]) { - changed = true; - slots_[index].reset(); - } - } - - if (changed) - updateLayout(); } std::string SControlsPanel::getDefaultLabelText(uint32_t index) @@ -598,40 +552,84 @@ std::string SControlsPanel::getDefaultLabelText(uint32_t index) return "CC " + std::to_string(index); } +SControlsPanel::ControlSlot* SControlsPanel::getSlot(uint32_t index) +{ + ControlSlot* slot = nullptr; + if (index < slots_.size()) + slot = slots_[index].get(); + return slot; +} + +SControlsPanel::ControlSlot* SControlsPanel::getOrCreateSlot(uint32_t index) +{ + ControlSlot* slot = getSlot(index); + if (slot) + return slot; + + if (index + 1 > slots_.size()) + slots_.resize(index + 1); + + slot = new ControlSlot; + slots_[index].reset(slot); + + // create controls etc... + CCoord knobWidth = 48.0; + CCoord knobHeight = knobWidth; + CCoord labelWidth = 96.0; + CCoord labelHeight = 24.0; + CCoord verticalPadding = 0.0; + + CCoord totalWidth = std::max(knobWidth, labelWidth); + CCoord knobX = (totalWidth - knobWidth) / 2.0; + CCoord labelX = (totalWidth - labelWidth) / 2.0; + + CRect knobBounds(knobX, 0.0, knobX + knobWidth, knobHeight); + CRect labelBounds(labelX, knobHeight + verticalPadding, labelX + labelWidth, knobHeight + verticalPadding + labelHeight); + CRect boxBounds = CRect(knobBounds).unite(labelBounds); + + SharedPointer knob = owned(new SStyledKnob(knobBounds, listener_.get(), index)); + SharedPointer label = owned(new CTextLabel(labelBounds)); + SharedPointer box = owned(new CViewContainer(boxBounds)); + + box->addView(knob); + knob->remember(); + box->addView(label); + label->remember(); + + box->setBackgroundColor(CColor(0x00, 0x00, 0x00, 0x00)); + label->setStyle(CTextLabel::kRoundRectStyle); + label->setRoundRectRadius(5.0); + label->setBackColor(CColor(0x2e, 0x34, 0x36)); + label->setText(getDefaultLabelText(index)); + knob->setActiveTrackColor(CColor(0x00, 0xb6, 0x2a)); + knob->setInactiveTrackColor(CColor(0x30, 0x30, 0x30)); + knob->setLineIndicatorColor(CColor(0x00, 0x00, 0x00)); + + slot->knob = knob; + slot->label = label; + slot->box = box; + + return slot; +} + void SControlsPanel::setControlValue(uint32_t index, float value) { - if (index >= slots_.size()) - return; - - ControlSlot* slot = slots_[index].get(); - if (!slot) - return; - - slot->knob->setValue(value); - slot->knob->invalid(); + ControlSlot* slot = getOrCreateSlot(index); + CControl* knob = slot->knob; + knob->setValue(value); + knob->invalid(); } void SControlsPanel::setControlDefaultValue(uint32_t index, float value) { - if (index >= slots_.size()) - return; - - ControlSlot* slot = slots_[index].get(); - if (!slot) - return; - - slot->knob->setDefaultValue(value); + ControlSlot* slot = getOrCreateSlot(index); + CControl* knob = slot->knob; + knob->setDefaultValue(value); } void SControlsPanel::setControlLabelText(uint32_t index, UTF8StringPtr text) { - if (index >= slots_.size()) - return; - - ControlSlot* slot = slots_[index].get(); - if (!slot) - return; - + ControlSlot* slot = getOrCreateSlot(index); CTextLabel* label = slot->label; if (text && text[0] != '\0') label->setText(text); @@ -675,7 +673,7 @@ void SControlsPanel::updateLayout() uint32_t numSlots = static_cast(slots_.size()); for (uint32_t i = 0; i < numSlots; ++i) { ControlSlot* slot = slots_[i].get(); - if (!slot) + if (!slot || !slot->used) continue; CViewContainer* box = slot->box; diff --git a/plugins/editor/src/editor/GUIComponents.h b/plugins/editor/src/editor/GUIComponents.h index e2c987f6..8c2237ec 100644 --- a/plugins/editor/src/editor/GUIComponents.h +++ b/plugins/editor/src/editor/GUIComponents.h @@ -237,8 +237,13 @@ private: void updateLayout(); static std::string getDefaultLabelText(uint32_t index); + struct ControlSlot; + ControlSlot* getSlot(uint32_t index); + ControlSlot* getOrCreateSlot(uint32_t index); + private: struct ControlSlot { + bool used = false; SharedPointer knob; SharedPointer label; SharedPointer box; @@ -257,6 +262,7 @@ private: std::vector> slots_; std::unique_ptr listener_; + SharedPointer relayoutTrigger_; }; /// From 6b319c590730d20f46dc8377ef1283f8763a1944 Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Mon, 8 Feb 2021 19:59:38 +0100 Subject: [PATCH 2/2] Remove the unused variable --- plugins/editor/src/editor/GUIComponents.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugins/editor/src/editor/GUIComponents.cpp b/plugins/editor/src/editor/GUIComponents.cpp index b349ecef..bc3d718b 100644 --- a/plugins/editor/src/editor/GUIComponents.cpp +++ b/plugins/editor/src/editor/GUIComponents.cpp @@ -707,8 +707,6 @@ void SControlsPanel::updateLayout() } } - CRect containerSize = getContainerSize(); - containerSize.bottom = containerBottom; setContainerSize(CRect(0.0, 0.0, viewBounds.getWidth(), containerBottom + verticalPadding)); invalid();