brockcarson / controlp5

Automatically exported from code.google.com/p/controlp5
0 stars 0 forks source link

ControlFont PFont overrides parent sketch font #56

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Sketch the includes text with size 30 font
2. Create control with new ControlFont, size 8 PFont
3. Text drawn in the sketch gets set to the size 8 PFont

What is the expected output? What do you see instead?

The control's font should not effect the font specified in the sketch.

Please provide any additional information below.

It looks like the issue is in the PFontLabel in ControlFont.java.
In the instance of a PFont, it sets theApplet.textFont(), but this never gets 
reset.  It appears you can get the current PFont from theApplet like this:

    PFont loadedFont = theApplet.g.textFont;
    float loadedSize = theApplet.g.textSize;

So whenever PFontLabel sets the textFont, it needs to change it back when it's 
done.

I haven't tested the performance impact on doing this at a label level, might 
be best done before / after the controller draws (since a control could have 
hundreds of labels), perhaps checking isControlFont and the instance PFontLabel.

But, here is what it might look like if directly applied to the draw of the 
label:

            @Override
            public void draw(PApplet theApplet, Label theLabel) {
                PFont loadedFont = theApplet.g.textFont;
                float loadedSize = theApplet.g.textSize;
                theApplet.textFont(pfont, size);
                theApplet.fill(0xffff0000); //theLabel.getColor()
                if (theLabel.isMultiline()) {
                    // theApplet.fill(255, 128, 0);
                    // theApplet.rect(0, 0, theLabel.getWidth(), theLabel.getHeight());
                    theApplet.fill(theLabel.getColor());
                    theApplet.textLeading(theLabel.getLineHeight());
                    theApplet.text(s, 0, 0, theLabel.getWidth(), theLabel.getHeight());
                } else {
                    theApplet.translate(0, -top + 1);
                    debug(theApplet, theLabel);
                    theApplet.fill(theLabel.getColor());
                    theApplet.text(theLabel.getTextFormatted(), 0, 0);
                    if (RENDER_2X) {
                        theApplet.text(theLabel.getTextFormatted(), 0, 0);
                    }
                }
                theApplet.textFont(loadedFont, loadedSize);
            }

This would prevent the PFont loaded in the ControlP5 control from overriding 
the sketch's font.

Original issue reported on code.google.com by jeffgem...@gmail.com on 23 Aug 2012 at 2:41

GoogleCodeExporter commented 9 years ago
Probably should also grab and set the alignment.  If the user has the textAlign 
set to CENTER, the label is going to be in the wrong place.

            PFont loadedFont = theApplet.g.textFont;
            float loadedSize = theApplet.g.textSize;
            int loadedAlign = theApplet.g.textAlign;
            theApplet.textAlign(theApplet.LEFT);
            ....
            theApplet.textFont(loadedFont, loadedSize);
            theApplet.textAlign(loadedAlign);

Don't know if mode would also be needed.  Not sure that get's changed much.

Original comment by jeffgem...@gmail.com on 23 Aug 2012 at 5:37

GoogleCodeExporter commented 9 years ago
Found out you also have to check to make sure the PFont is not null.  I did 
this, which will force the "defaultFontOrDeath()" to run if it's null. Looks 
like this (file attached):

            PFont loadedFont = theApplet.g.textFont;
            float loadedSize = theApplet.g.textSize;
            if (loadedFont == null) {
                theApplet.textSize(loadedSize); //forces default font
                loadedFont = theApplet.g.textFont;
            }

Original comment by jeffgem...@gmail.com on 23 Aug 2012 at 7:41

Attachments:

GoogleCodeExporter commented 9 years ago
implemented with 0.7.6

Original comment by soj...@gmail.com on 26 Aug 2012 at 3:01