Skip to content

Commit

Permalink
- added expandable GUI with broadcaster support
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph-hart committed Aug 7, 2024
1 parent 4c0a3b2 commit 37a4796
Show file tree
Hide file tree
Showing 14 changed files with 7,066 additions and 6,907 deletions.
17 changes: 15 additions & 2 deletions hi_backend/backend/BackendEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1235,9 +1235,22 @@ void MainTopBar::togglePopup(PopupType t, bool shouldShow)
int w = (int)((float)content->getContentWidth()*scaleFactor);
int h = (int)((float)content->getContentHeight()*scaleFactor);

c->setSize(w, h);

c->setName("Interface Preview");

content->interfaceSizeBroadcaster.addListener(*c, [mc](Component& oc, int w, int h)
{
auto scaleFactor = dynamic_cast<GlobalSettingManager*>(mc)->getGlobalScaleFactor();

oc.getChildComponent(0)->setSize(w, h);

w = roundToInt((float)w * scaleFactor);
h = roundToInt((float)h * scaleFactor);

oc.setSize(w, h);
oc.resized();
});


}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,10 @@ bool InterfaceContentPanel::connectToScript()
refreshButton->setVisible(false);
}

updateSize();
jsp->getScriptingContent()->interfaceSizeBroadcaster.addListener(*this, [](InterfaceContentPanel& ip, int, int)
{
ip.updateSize();
});

repaint();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class InterfaceContentPanel : public FloatingTileContent,
ScopedPointer<ScriptContentComponent> content;

void updateSize();

JUCE_DECLARE_WEAK_REFERENCEABLE(InterfaceContentPanel);
};


Expand Down
32 changes: 22 additions & 10 deletions hi_frontend/frontend/FrontendProcessorEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,17 @@ AudioProcessorEditor(fp)

startTimer(4125);

originalSizeX = getWidth();
originalSizeY = getHeight();
WeakReference<ScriptingApi::Content> content = jsp->getScriptingContent();

scaleFactor = (float)fp->getGlobalScaleFactor();

content->interfaceSizeBroadcaster.addListener(*this, [content](FrontendProcessorEditor& fp, int w, int h)
{
fp.originalSizeX = w;
fp.originalSizeY = h;
fp.setGlobalScaleFactor(fp.scaleFactor, true);
});

const int availableHeight = Desktop::getInstance().getDisplays().getMainDisplay().userArea.getHeight();
const float displayScaleFactor = (float)Desktop::getInstance().getDisplays().getMainDisplay().scale;
const int unscaledInterfaceHeight = getHeight();
Expand All @@ -178,10 +186,7 @@ AudioProcessorEditor(fp)
{
setGlobalScaleFactor(0.85f);
}
else
{
setGlobalScaleFactor((float)fp->getGlobalScaleFactor());
}

#endif

if (FullInstrumentExpansion::isEnabled(getMainController()))
Expand Down Expand Up @@ -227,8 +232,13 @@ void FrontendProcessorEditor::newHisePresetLoaded()
{
if (auto jsp = JavascriptMidiProcessor::getFirstInterfaceScriptProcessor(getMainController()))
{
originalSizeX = jsp->getScriptingContent()->getContentWidth();
originalSizeY = jsp->getScriptingContent()->getContentHeight();
jsp->getScriptingContent()->interfaceSizeBroadcaster.addListener(*this, [](FrontendProcessorEditor& fp, int w, int h)
{
fp.originalSizeX = w;
fp.originalSizeY = h;

fp.setGlobalScaleFactor(fp.scaleFactor, true);
});
}

setGlobalScaleFactor(scaleFactor, true);
Expand Down Expand Up @@ -258,10 +268,12 @@ void FrontendProcessorEditor::setGlobalScaleFactor(float newScaleFactor, bool fo
{
tl->setSize((int)((float)originalSizeX * scaleFactor), (int)((float)originalSizeY * scaleFactor));
setSize((int)((float)originalSizeX * scaleFactor), (int)((float)originalSizeY * scaleFactor));
resized();
}
else
{
setSize((int)((float)originalSizeX * scaleFactor), (int)((float)originalSizeY * scaleFactor));
resized();
}
}
}
Expand All @@ -274,8 +286,8 @@ void FrontendProcessorEditor::resized()
int width = originalSizeX != 0 ? originalSizeX : getWidth();
int height = originalSizeY != 0 ? originalSizeY : getHeight();
#else
int width = (int)((double)getWidth() / scaleFactor);
int height = (int)((double)getHeight() / scaleFactor);
int width = originalSizeX;
int height = originalSizeY;
#endif

container->setBounds(0, 0, width, height);
Expand Down
2 changes: 2 additions & 0 deletions hi_frontend/frontend/FrontendProcessorEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ class FrontendProcessorEditor: public AudioProcessorEditor,

bool usesOpenGl;
OpenGLContext context;

JUCE_DECLARE_WEAK_REFERENCEABLE(FrontendProcessorEditor);
};


Expand Down
24 changes: 24 additions & 0 deletions hi_scripting/scripting/api/ScriptBroadcaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,13 @@ Array<juce::var> ScriptBroadcaster::ComponentPropertyListener::createChildArray(
return list;
}

ScriptBroadcaster::InterfaceSizeListener::InterfaceSizeListener(ScriptBroadcaster* b, const var& metadata):
ListenerBase(metadata),
parent(b)
{
b->getScriptProcessor()->getScriptingContent()->interfaceSizeBroadcaster.addListener(*this, InterfaceSizeListener::onUpdate);
}

ScriptBroadcaster::ComponentPropertyListener::ComponentPropertyListener(ScriptBroadcaster* b, var componentIds, const Array<Identifier>& propertyIds_, const var& metadata):
ListenerBase(metadata),
propertyIds(propertyIds_)
Expand Down Expand Up @@ -3159,6 +3166,7 @@ struct ScriptBroadcaster::Wrapper
API_VOID_METHOD_WRAPPER_3(ScriptBroadcaster, attachToEqEvents);
API_VOID_METHOD_WRAPPER_4(ScriptBroadcaster, attachToOtherBroadcaster);
API_VOID_METHOD_WRAPPER_1(ScriptBroadcaster, attachToProcessingSpecs);
API_VOID_METHOD_WRAPPER_1(ScriptBroadcaster, attachToInterfaceSize);
API_VOID_METHOD_WRAPPER_3(ScriptBroadcaster, attachToSampleMap);
API_VOID_METHOD_WRAPPER_3(ScriptBroadcaster, callWithDelay);
API_VOID_METHOD_WRAPPER_1(ScriptBroadcaster, setReplaceThisReference);
Expand Down Expand Up @@ -3198,6 +3206,7 @@ ScriptBroadcaster::ScriptBroadcaster(ProcessorWithScriptingContent* p, const var
ADD_API_METHOD_2(attachToComponentValue);
ADD_API_METHOD_2(attachToComponentVisibility);
ADD_API_METHOD_2(attachToRoutingMatrix);
ADD_API_METHOD_1(attachToInterfaceSize);
ADD_API_METHOD_3(attachToModuleParameter);
ADD_API_METHOD_2(attachToRadioGroup);
ADD_API_METHOD_4(attachToComplexData);
Expand Down Expand Up @@ -3843,6 +3852,21 @@ void ScriptBroadcaster::attachToComponentValue(var componentIds, var optionalMet
checkMetadataAndCallWithInitValues(attachedListeners.getLast());
}

void ScriptBroadcaster::attachToInterfaceSize(var optionalMetadata)
{
throwIfAlreadyConnected();

attachedListeners.add(new InterfaceSizeListener(this, optionalMetadata));

if (defaultValues.size() != 2)
{
String e = "If you want to attach a broadcaster to visibility events, it needs two parameters (width and height)";
errorBroadcaster.sendMessage(sendNotificationAsync, attachedListeners.getLast(), e);
reportScriptError(e);
}

checkMetadataAndCallWithInitValues(attachedListeners.getLast());
}


void ScriptBroadcaster::attachToComponentVisibility(var componentIds, var optionalMetadata)
Expand Down
52 changes: 52 additions & 0 deletions hi_scripting/scripting/api/ScriptBroadcaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ struct ScriptBroadcaster : public ConstScriptingObject,
/** Registers this broadcaster to be called when the value of the given components change. */
void attachToComponentValue(var componentIds, var optionalMetadata);

/** Registers the broadcaster to be notified when the interface size changes. */
void attachToInterfaceSize(var optionalMetadata);

/** Registers this broadcaster to be called when the visibility of one of the components (or one of its parent component) changes. */
void attachToComponentVisibility(var componentIds, var optionalMetadata);

Expand Down Expand Up @@ -744,6 +747,55 @@ struct ScriptBroadcaster : public ConstScriptingObject,
OwnedArray<InternalListener> items;
};

struct InterfaceSizeListener : public ListenerBase
{
InterfaceSizeListener(ScriptBroadcaster* b, const var& metadata);

~InterfaceSizeListener()
{
if(auto sc = parent->getScriptProcessor()->getScriptingContent())
{
sc->interfaceSizeBroadcaster.removeListener(*this);
}
}

Identifier getItemId() const override { RETURN_STATIC_IDENTIFIER("InterfaceSizeListener"); }

void registerSpecialBodyItems(ComponentWithPreferredSize::BodyFactory& factory) override {}

int getNumInitialCalls() const override { return 1; }

static void onUpdate(InterfaceSizeListener& il, int w, int h)
{
il.sizeArray.set(0, w);
il.sizeArray.set(1, h);
il.parent->sendAsyncMessage(il.sizeArray);
}

Array<var> getInitialArgs(int callIndex) const override
{
Array<var> size;

auto sp = parent->getScriptProcessor();
size.add(sp->getScriptingContent()->getContentWidth());
size.add(sp->getScriptingContent()->getContentHeight());

return size;
}

Result callItem(TargetBase* n) override
{
return n->callSync(sizeArray);
}

Array<var> createChildArray() const override { return sizeArray; }

Array<var> sizeArray;
ScriptBroadcaster* parent;

JUCE_DECLARE_WEAK_REFERENCEABLE(InterfaceSizeListener);
};

struct ComponentVisibilityListener : public ListenerBase
{
struct InternalListener;
Expand Down
32 changes: 17 additions & 15 deletions hi_scripting/scripting/api/ScriptingApiContent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6770,44 +6770,46 @@ void ScriptingApi::Content::beginInitialization()

void ScriptingApi::Content::setHeight(int newHeight) noexcept
{
if (!allowGuiCreation)
{
reportScriptError("the height can't be changed after onInit()");
return;
}


if (newHeight > 800)
{
reportScriptError("Go easy on the height! (" + String(800) + "px is enough)");
return;
}

height = newHeight;
if(height != newHeight)
{
height = newHeight;

if(width != 0)
interfaceSizeBroadcaster.sendMessage(sendNotificationAsync, width, height);
}
};

void ScriptingApi::Content::setWidth(int newWidth) noexcept
{
if (!allowGuiCreation)
{
reportScriptError("the width can't be changed after onInit()");
return;
}

if (newWidth > 1280)
{
reportScriptError("Go easy on the width! (1280px is enough)");
return;
}

width = newWidth;

if(width != newWidth)
{
width = newWidth;

if(height != 0)
interfaceSizeBroadcaster.sendMessage(sendNotificationAsync, width, height);
}
};

void ScriptingApi::Content::makeFrontInterface(int newWidth, int newHeight)
{
width = newWidth;
height = newHeight;

interfaceSizeBroadcaster.sendMessage(sendNotificationAsync, width, height);

dynamic_cast<JavascriptMidiProcessor*>(getProcessor())->addToFront(true);

}
Expand Down
6 changes: 4 additions & 2 deletions hi_scripting/scripting/api/ScriptingApiContent.h
Original file line number Diff line number Diff line change
Expand Up @@ -2520,7 +2520,8 @@ class ScriptingApi::Content : public ScriptingObject,
};

LambdaBroadcaster<TextInputDataBase::Ptr> textInputBroadcaster;


LambdaBroadcaster<int, int> interfaceSizeBroadcaster;

// ================================================================================================================

Expand Down Expand Up @@ -3035,7 +3036,8 @@ class ScriptingApi::Content : public ScriptingObject,
ScopedPointer<ValueTreeUpdateWatcher> updateWatcher;

bool allowGuiCreation;
int width, height;
int width = 0;
int height = 0;
ReferenceCountedArray<ScriptComponent> components; // This is ref counted to allow anonymous controls
Colour colour;
String name;
Expand Down
Loading

0 comments on commit 37a4796

Please sign in to comment.