SVG Button


#1

I want to display an SVG as the image for a button. I saw DrawableButton and Drawable and assumed that's the correct classes to use. I attached an .svg as a resource to the component class, that's fine, that gives me a string (char*). However, I can't figure out how to give that to Drawable to use. Drawable->createFromSVG doesn't take char*.

Must be an example somewhere but I can't find it. I also ran across the BinaryBuilder stuff and was wondering if I need to be using that somehow instead?


#2

Just parse your string into XML.. If you grep for "createFromSVG" there are examples in the codebase.


#3

Not all svg files are correctly painted with juce, if you get a black image instead of your svg image try to open your svg file with Inkscape and save it again in Plain SVG (*.svg) format.

Here is an example code to draw a svg as a component:

ScopedPointer<XmlElement> svg (XmlDocument::parse (BinaryData::file_svg));

if (svg != nullptr)
    drawable = Drawable::createFromSVG (*svg);

addAndMakeVisible(drawable);

http://www.juce.com/api/classDrawableButton.html#a234fdfb0b05c4b2fe42b22366377829a


#4

I don't have a string, I have an .svg file. I attached it as a resource to my .cpp file. Which again, I don't know if this is the right thing to do, but seems preferable for finding the "file".

Grepping for createFromSVG returns jucer_Icons.cpp that lists it in a commented-out section (its not complete). The other grep result has it parsed from a file on the file system. So, I'm not understanding the proper conversion from the "attached resource" (which lists a bunch of char[] numbers, not an xml string).

jucer_Icons.cpp: seems to have an appropriate example, but I have no idea how to get uint8[] arrays rather than the char[] that attaching the resource gave me.


#5

I did get your code snippet to work which is good because the codebase doesn't seem to have this. I just used it with a DrawableButton. I didn't know how to get BinaryData to compile, but apparently just putting a file in an appropriately named subfolder worked. Hopefully this will help the next person.

 

 


#6

I don’t get how to use the Drawable class in the code snippet of Theadd! It looks to be abstract but the function Drawable::createFromSVG returns a pointer to an instance of that class. I’m quite sure it is my lack of C++ knowledge and if anybody could help I would appreciate!
Thanks in advance


#7

Having a pointer to an abstract class is completely fine - this is exactly how abstract classes meant to be used.

I’d suggest looking up C++ abstract classes and polymorphism in a book or an online resource. Here’s one to start with: https://en.wikibooks.org/wiki/C%2B%2B_Programming/Classes/Abstract_Classes