Add window size as plugin state

This commit is contained in:
falkTX 2021-10-27 03:20:18 +01:00
parent a5c42b5460
commit 541a994adf
2 changed files with 44 additions and 4 deletions

View file

@ -310,6 +310,7 @@ class CardinalPlugin : public CardinalBasePlugin
float* fAudioBufferIn;
float* fAudioBufferOut;
std::string fAutosavePath;
String fWindowSize;
// for base/context handling
bool fIsActive;
@ -323,7 +324,7 @@ class CardinalPlugin : public CardinalBasePlugin
public:
CardinalPlugin()
: CardinalBasePlugin(kModuleParameters + kWindowParameterCount, 0, 1),
: CardinalBasePlugin(kModuleParameters + kWindowParameterCount, 0, 2),
fInitializer(this),
fAudioBufferIn(nullptr),
fAudioBufferOut(nullptr),
@ -642,10 +643,17 @@ protected:
void initState(const uint32_t index, String& stateKey, String& defaultStateValue) override
{
DISTRHO_SAFE_ASSERT_RETURN(index == 0,);
stateKey = "patch";
defaultStateValue = "";
switch (index)
{
case 0:
stateKey = "patch";
break;
case 1:
stateKey = "windowSize";
break;
}
}
/* --------------------------------------------------------------------------------------------------------
@ -683,6 +691,9 @@ protected:
String getState(const char* const key) const override
{
if (std::strcmp(key, "windowSize") == 0)
return fWindowSize;
if (std::strcmp(key, "patch") != 0)
return String();
if (fAutosavePath.empty())
@ -707,6 +718,12 @@ protected:
void setState(const char* const key, const char* const value) override
{
if (std::strcmp(key, "windowSize") == 0)
{
fWindowSize = value;
return;
}
if (std::strcmp(key, "patch") != 0)
return;
if (fAutosavePath.empty())

View file

@ -273,6 +273,18 @@ protected:
void stateChanged(const char* key, const char* value) override
{
if (std::strcmp(key, "windowSize") != 0)
return;
int width = 0;
int height = 0;
std::sscanf(value, "%i:%i", &width, &height);
if (width > 0 && height > 0)
{
const double scaleFactor = getScaleFactor();
setSize(width * scaleFactor, height * scaleFactor);
}
}
// -------------------------------------------------------------------------------------------------------
@ -479,6 +491,17 @@ protected:
return fContext->event->handleKey(fLastMousePos, key, ev.keycode, action, mods);
}
void onResize(const ResizeEvent& ev) override
{
UI::onResize(ev);
const double scaleFactor = getScaleFactor();
char sizeString[64];
std::snprintf(sizeString, sizeof(sizeString), "%d:%d",
(int)(ev.size.getWidth() / scaleFactor), (int)(ev.size.getHeight() / scaleFactor));
setState("windowSize", sizeString);
}
void uiFocus(const bool focus, CrossingMode) override
{
if (focus)