Skip to content

Commit

Permalink
- added image drawing to custom look and feel
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph-hart committed Nov 13, 2021
1 parent d503b36 commit f4b0b17
Show file tree
Hide file tree
Showing 7 changed files with 1,270 additions and 1,202 deletions.
4 changes: 2 additions & 2 deletions JUCE/modules/juce_gui_basics/components/juce_Component.cpp
Expand Up @@ -3307,7 +3307,7 @@ UnblurryGraphics::UnblurryGraphics(Graphics& g_, Component& componentToDrawOn, b
// sure that the physical scale factor is a multiple of 0.25.
// (I am not aware of OS that use a smaller resolution for their scale factor
// steps).
physicalScaleFactor -= fmod(physicalScaleFactor, 0.25f);
//physicalScaleFactor = (float)roundToInt(physicalScaleFactor * 100.0f) * 0.01f;
sf = juceScaleFactor * physicalScaleFactor;

pixelSizeInFloat = 1.0f / sf;
Expand All @@ -3328,7 +3328,7 @@ UnblurryGraphics::UnblurryGraphics(Graphics& g_, Component& componentToDrawOn, b
// sure that the physical scale factor is a multiple of 0.25.
// (I am not aware of OS that use a smaller resolution for their scale factor
// steps).
physicalScaleFactor -= fmod(physicalScaleFactor, 0.25f);
//physicalScaleFactor = (float)roundToInt(physicalScaleFactor * 100.0f) * 0.01f;
sf = juceScaleFactor * physicalScaleFactor;

pixelSizeInFloat = 1.0f / sf;
Expand Down
6 changes: 5 additions & 1 deletion hi_backend/backend/debug_components/PatchBrowser.cpp
Expand Up @@ -1599,7 +1599,11 @@ void PatchBrowser::MiniPeak::paint(Graphics& g)
g.setColour(Colours::black.withAlpha(0.4f));
g.fillEllipse(b.toFloat());

g.setColour(p->getColour().withAlpha(channelValues[0]));
float value = channelValues[0];
FloatSanitizers::sanitizeFloatNumber(value);
value = jlimit(0.0f, 1.0f, value);

g.setColour(p->getColour().withAlpha(value));
g.fillEllipse(b.toFloat().reduced(1.0f));
break;
}
Expand Down
2 changes: 2 additions & 0 deletions hi_scripting/scripting/api/ScriptingApiObjects.cpp
Expand Up @@ -4653,6 +4653,7 @@ struct ScriptingObjects::ScriptedLookAndFeel::Wrapper
{
API_VOID_METHOD_WRAPPER_2(ScriptedLookAndFeel, registerFunction);
API_VOID_METHOD_WRAPPER_2(ScriptedLookAndFeel, setGlobalFont);
API_VOID_METHOD_WRAPPER_2(ScriptedLookAndFeel, loadImage);
};


Expand All @@ -4663,6 +4664,7 @@ ScriptingObjects::ScriptedLookAndFeel::ScriptedLookAndFeel(ProcessorWithScriptin
{
ADD_API_METHOD_2(registerFunction);
ADD_API_METHOD_2(setGlobalFont);
ADD_API_METHOD_2(loadImage);

getScriptProcessor()->getMainController_()->setCurrentScriptLookAndFeel(this);
}
Expand Down
80 changes: 55 additions & 25 deletions hi_scripting/scripting/api/ScriptingGraphics.cpp
Expand Up @@ -1275,41 +1275,42 @@ void ScriptingObjects::GraphicsObject::fillEllipse(var area)

void ScriptingObjects::GraphicsObject::drawImage(String imageName, var area, int /*xOffset*/, int yOffset)
{
Image img;

if (auto sc = dynamic_cast<ScriptingApi::Content::ScriptPanel*>(parent))
{
const Image img = sc->getLoadedImage(imageName);

if (img.isValid())
{
Rectangle<float> r = getRectangleFromVar(area);
img = sc->getLoadedImage(imageName);
}
else if (auto laf = dynamic_cast<ScriptingObjects::ScriptedLookAndFeel*>(parent))
{
img = laf->getLoadedImage(imageName);
}
else
{
reportScriptError("drawImage is only allowed in a panel's paint routine");
}

if (r.getWidth() != 0)
{
const double scaleFactor = (double)img.getWidth() / (double)r.getWidth();
if (img.isValid())
{
Rectangle<float> r = getRectangleFromVar(area);

drawActionHandler.addDrawAction(new ScriptedDrawActions::drawImage(img, r, (float)scaleFactor, yOffset));
}
}
else
if (r.getWidth() != 0)
{
drawActionHandler.addDrawAction(new ScriptedDrawActions::setColour(Colours::grey));
drawActionHandler.addDrawAction(new ScriptedDrawActions::fillRect(getRectangleFromVar(area)));

drawActionHandler.addDrawAction(new ScriptedDrawActions::setColour(Colours::black));
drawActionHandler.addDrawAction(new ScriptedDrawActions::drawRect(getRectangleFromVar(area), 1.0f));
drawActionHandler.addDrawAction(new ScriptedDrawActions::setFont(GLOBAL_BOLD_FONT()));
drawActionHandler.addDrawAction(new ScriptedDrawActions::drawText("XXX", getRectangleFromVar(area), Justification::centred));

debugError(dynamic_cast<Processor*>(getScriptProcessor()), "Image " + imageName + " not found");
const double scaleFactor = (double)img.getWidth() / (double)r.getWidth();
drawActionHandler.addDrawAction(new ScriptedDrawActions::drawImage(img, r, (float)scaleFactor, yOffset));
}


}
else
{
reportScriptError("drawImage is only allowed in a panel's paint routine");
drawActionHandler.addDrawAction(new ScriptedDrawActions::setColour(Colours::grey));
drawActionHandler.addDrawAction(new ScriptedDrawActions::fillRect(getRectangleFromVar(area)));
drawActionHandler.addDrawAction(new ScriptedDrawActions::setColour(Colours::black));
drawActionHandler.addDrawAction(new ScriptedDrawActions::drawRect(getRectangleFromVar(area), 1.0f));
drawActionHandler.addDrawAction(new ScriptedDrawActions::setFont(GLOBAL_BOLD_FONT()));
drawActionHandler.addDrawAction(new ScriptedDrawActions::drawText("XXX", getRectangleFromVar(area), Justification::centred));

debugError(dynamic_cast<Processor*>(getScriptProcessor()), "Image " + imageName + " not found");
}

}

void ScriptingObjects::GraphicsObject::drawDropShadow(var area, var colour, int radius)
Expand Down Expand Up @@ -1479,4 +1480,33 @@ Rectangle<int> ScriptingObjects::GraphicsObject::getIntRectangleFromVar(const va



void ScriptingObjects::ScriptedLookAndFeel::loadImage(String imageName, String prettyName)
{
// It's a bit ugly to just copy that code from the script panel...
PoolReference ref(getProcessor()->getMainController(), imageName, ProjectHandler::SubDirectories::Images);

for (auto& img : loadedImages)
{
if (img.prettyName == prettyName)
{
if (img.image.getRef() != ref)
{
HiseJavascriptEngine::TimeoutExtender xt(dynamic_cast<JavascriptProcessor*>(getScriptProcessor())->getScriptEngine());
img.image = getProcessor()->getMainController()->getExpansionHandler().loadImageReference(ref);
}

return;
}
}

HiseJavascriptEngine::TimeoutExtender xt(dynamic_cast<JavascriptProcessor*>(getScriptProcessor())->getScriptEngine());

if (auto newImage = getProcessor()->getMainController()->getExpansionHandler().loadImageReference(ref))
loadedImages.add({ newImage, prettyName });
else
{
debugToConsole(dynamic_cast<Processor*>(getScriptProcessor()), "Image " + imageName + " not found. ");
}
}

}
28 changes: 28 additions & 0 deletions hi_scripting/scripting/api/ScriptingGraphics.h
Expand Up @@ -602,12 +602,19 @@ namespace ScriptingObjects

Identifier getObjectName() const override { return "ScriptLookAndFeel"; }

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

/** Registers a function that will be used for the custom look and feel. */
void registerFunction(var functionName, var function);

/** Set a global font. */
void setGlobalFont(const String& fontName, float fontSize);

/** Loads an image that can be used by the look and feel functions. */
void loadImage(String imageFile, String prettyName);

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

bool callWithGraphics(Graphics& g_, const Identifier& functionname, var argsObject);

var callDefinedFunction(const Identifier& name, var* args, int numArgs);
Expand Down Expand Up @@ -657,6 +664,27 @@ namespace ScriptingObjects

var functions;

Image getLoadedImage(const String& prettyName)
{
for (auto& img : loadedImages)
{
if (img.prettyName == prettyName)
{
return img.image ? *img.image.getData() : Image();
}
}

return Image();
}

struct NamedImage
{
PooledImage image;
String prettyName;
};

Array<NamedImage> loadedImages;

JUCE_DECLARE_WEAK_REFERENCEABLE(ScriptedLookAndFeel);
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ScriptedLookAndFeel);
};
Expand Down

0 comments on commit f4b0b17

Please sign in to comment.