Skip to content

Commit

Permalink
add aspect ratio scalable vst3 editor class
Browse files Browse the repository at this point in the history
  • Loading branch information
scheffle committed Nov 18, 2023
1 parent 3ec5ba9 commit 1773019
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 6 deletions.
98 changes: 94 additions & 4 deletions vstgui/plugin-bindings/vst3editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,9 +561,15 @@ bool VST3Editor::setEditorSizeConstrains (const CPoint& newMinimumSize, const CP
//-----------------------------------------------------------------------------
double VST3Editor::getAbsScaleFactor () const
{
return zoomFactor * contentScaleFactor;
return getZoomFactor () * getContentScaleFactor ();
}

//-----------------------------------------------------------------------------
double VST3Editor::getContentScaleFactor () const { return contentScaleFactor; }

//-----------------------------------------------------------------------------
double VST3Editor::getZoomFactor () const { return zoomFactor; }

#ifdef VST3_CONTENT_SCALE_SUPPORT
//-----------------------------------------------------------------------------
Steinberg::tresult PLUGIN_API VST3Editor::setContentScaleFactor (ScaleFactor factor)
Expand All @@ -580,7 +586,7 @@ Steinberg::tresult PLUGIN_API VST3Editor::setContentScaleFactor (ScaleFactor fac
//-----------------------------------------------------------------------------
void VST3Editor::setZoomFactor (double factor)
{
if (zoomFactor == factor)
if (getZoomFactor () == factor)
return;

zoomFactor = factor;
Expand Down Expand Up @@ -861,7 +867,7 @@ void VST3Editor::onMouseEvent (MouseEvent& event, CFrame* frame)
static_cast<int> ((*it) * 100));
CMenuItem* item = zoomMenu->addEntry (new CCommandMenuItem (
{zoomFactorString, zoomFactorTag, this, "Zoom", zoomFactorString}));
if (zoomFactor == *it)
if (getZoomFactor () == *it)
item->setChecked (true);
}
CMenuItem* item = controllerMenu->addEntry ("UI Zoom");
Expand Down Expand Up @@ -1072,6 +1078,9 @@ void VST3Editor::requestRecreateView ()
}
}

//-----------------------------------------------------------------------------
bool VST3Editor::inEditMode () const { return editingEnabled; }

#if LINUX
// Map Steinberg Vst Interface to VSTGUI Interface
class RunLoop : public X11::IRunLoop, public AtomicReferenceCounted
Expand Down Expand Up @@ -1891,7 +1900,7 @@ bool VST3Editor::enableEditing (bool state)

getFrame ()->setSize (width, height);
getFrame ()->addView (view);
getFrame()->setZoom (contentScaleFactor);
getFrame ()->setZoom (getContentScaleFactor ());

getFrame ()->enableTooltips (true);
CColor focusColor = kBlueCColor;
Expand Down Expand Up @@ -2046,5 +2055,86 @@ UIDescription* VST3Editor::getUIDescription () const
return description;
}

//------------------------------------------------------------------------
//--- AspectRatioVST3Editor
//------------------------------------------------------------------------
void AspectRatioVST3Editor::setMinZoomFactor (double factor) { minZoomFactor = factor; }

//------------------------------------------------------------------------
double AspectRatioVST3Editor::getMinZoomFactor () const { return minZoomFactor; }

//------------------------------------------------------------------------
bool PLUGIN_API AspectRatioVST3Editor::open (void* parent, const PlatformType& type)
{
if (VST3Editor::open (parent, type) && getFrame ())
{
initialSize = getFrame ()->getViewSize ().getSize ();
calcZoomFactor = getZoomFactor ();
return true;
}
return false;
}

//------------------------------------------------------------------------
Steinberg::tresult PLUGIN_API AspectRatioVST3Editor::onSize (Steinberg::ViewRect* newSize)
{
if (newSize == nullptr)
return Steinberg::kInvalidArgument;

if (!canCalculateAspectRatio ())
return VST3Editor::onSize (newSize);

setZoomFactor (calcZoomFactor);
return Steinberg::kResultTrue;
}

//------------------------------------------------------------------------
Steinberg::tresult PLUGIN_API AspectRatioVST3Editor::checkSizeConstraint (Steinberg::ViewRect* rect)
{
if (rect == nullptr)
return Steinberg::kInvalidArgument;

if (!canCalculateAspectRatio ())
return VST3Editor::checkSizeConstraint (rect);

const CPoint size (rect->getWidth (), rect->getHeight ());
auto diff = size - initialSize;
auto anchor = initialSize.x >= initialSize.y ? initialSize.x : initialSize.y;
auto sizeAnchor = initialSize.x >= initialSize.y ? size.x : size.y;

auto factor = sizeAnchor / anchor;
if (factor < minZoomFactor * getContentScaleFactor ())
factor = minZoomFactor * getContentScaleFactor ();
factor = std::round (factor * anchor) / anchor;
auto newSize = initialSize * factor;
calcZoomFactor = factor / getContentScaleFactor ();
if (newSize != size)
{
rect->right = rect->left + newSize.x;
rect->bottom = rect->top + newSize.y;
}
return Steinberg::kResultTrue;
}

#ifdef VST3_CONTENT_SCALE_SUPPORT
//------------------------------------------------------------------------
Steinberg::tresult PLUGIN_API AspectRatioVST3Editor::setContentScaleFactor (ScaleFactor factor)
{
auto res = VST3Editor::setContentScaleFactor (factor);
if (res == Steinberg::kResultTrue)
setZoomFactor (calcZoomFactor);
return res;
}
#endif

//------------------------------------------------------------------------
bool AspectRatioVST3Editor::canCalculateAspectRatio () const
{
auto f = getFrame ();
if (inEditMode () || (f && f->hasChildren () == false))
return false;
return true;
}

//------------------------------------------------------------------------
} // VSTGUI
34 changes: 32 additions & 2 deletions vstgui/plugin-bindings/vst3editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ class VST3Editor : public Steinberg::Vst::VSTGUIEditor,
bool requestResize (const CPoint& newSize);

void setZoomFactor (double factor);
double getZoomFactor () const { return zoomFactor; }
double getZoomFactor () const;

void setAllowedZoomFactors (std::vector<double> zoomFactors) { allowedZoomFactors = zoomFactors; }

/** set the delegate of the editor. no reference counting is happening here. */
Expand All @@ -141,6 +141,7 @@ class VST3Editor : public Steinberg::Vst::VSTGUIEditor,
~VST3Editor () override;
void init ();
double getAbsScaleFactor () const;
double getContentScaleFactor () const;
ParameterChangeListener* getParameterChangeListener (int32_t tag) const;
void recreateView ();
void requestRecreateView ();
Expand All @@ -152,6 +153,7 @@ class VST3Editor : public Steinberg::Vst::VSTGUIEditor,
bool enableShowEditButton () const;
void enableShowEditButton (bool state);
void showEditButton (bool state);
bool inEditMode () const;

bool PLUGIN_API open (void* parent, const PlatformType& type) override;
void PLUGIN_API close () override;
Expand Down Expand Up @@ -221,5 +223,33 @@ class VST3Editor : public Steinberg::Vst::VSTGUIEditor,
Optional<CPoint> sizeRequest;
};

//-----------------------------------------------------------------------------
//! @brief An extended VST3 Editor which scales its contents when resized
//! @ingroup new_in_4_14
//-----------------------------------------------------------------------------
class AspectRatioVST3Editor : public VST3Editor
{
public:
using VST3Editor::VST3Editor;

void setMinZoomFactor (double factor);
double getMinZoomFactor () const;

protected:
bool canCalculateAspectRatio () const;

bool PLUGIN_API open (void* parent, const PlatformType& type) override;
Steinberg::tresult PLUGIN_API onSize (Steinberg::ViewRect* newSize) override;
Steinberg::tresult PLUGIN_API checkSizeConstraint (Steinberg::ViewRect* rect) override;
#ifdef VST3_CONTENT_SCALE_SUPPORT
Steinberg::tresult PLUGIN_API setContentScaleFactor (ScaleFactor factor) override;
#endif

private:
CPoint initialSize {};
double minZoomFactor {1.};
double calcZoomFactor {1.};
};

//------------------------------------------------------------------------
} // VSTGUI

0 comments on commit 1773019

Please sign in to comment.