/* Part of the GUI library for Processing http://www.lagers.org.uk/g4p/index.html http://sourceforge.net/projects/g4p/files/?source=navbar Copyright (c) 2012 Peter Lager This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package automenta.vivisect.gui; import automenta.vivisect.gui.StyledString.TextLayoutInfo; import java.awt.Graphics2D; import java.awt.font.TextLayout; import java.util.LinkedList; import processing.core.PApplet; /** * The label component. * * This control can display text with/without an icon. * * @author Peter Lager * */ public class GLabel extends GTextIconAlignBase { public GLabel(PApplet theApplet, float p0, float p1, float p2, float p3) { this(theApplet, p0, p1, p2, p3, " "); } /** * Create a label control. * * use setIcon to add an icon * * @param theApplet * @param p0 * @param p1 * @param p2 * @param p3 * @param text */ public GLabel(PApplet theApplet, float p0, float p1, float p2, float p3, String text) { super(theApplet, p0, p1, p2, p3); setText(text); opaque = false; // Now register control with applet registeredMethods = DRAW_METHOD; GUI.addControl(this); } public void draw(){ if(!visible) return; // Update buffer if invalid updateBuffer(); winApp.pushStyle(); winApp.pushMatrix(); // Perform the rotation applyTransform(); // Move matrix to line up with top-left corner winApp.translate(-halfWidth, -halfHeight); // Draw buffer winApp.imageMode(PApplet.CORNER); if(alphaLevel < 255) winApp.tint(TINT_FOR_ALPHA, alphaLevel); winApp.image(buffer, 0, 0); winApp.popMatrix(); winApp.popStyle(); } protected void updateBuffer(){ if(bufferInvalid) { Graphics2D g2d = buffer.g2; // Get the latest lines of text LinkedList<TextLayoutInfo> lines = stext.getLines(g2d); bufferInvalid = false; buffer.beginDraw(); // Back ground colour buffer.background(opaque ? palette[6] : palette[2] & 0xFFFFFF); // Calculate text and icon placement calcAlignment(); // If there is an icon draw it if(iconW != 0) buffer.image(bicon[0], siX, siY); float wrapWidth = stext.getWrapWidth(); float sx = 0, tw = 0; buffer.translate(stX, stY); for(TextLayoutInfo lineInfo : lines){ TextLayout layout = lineInfo.layout; buffer.translate(0, layout.getAscent()); switch(textAlignH){ case CENTER: tw = layout.getVisibleAdvance(); tw = (tw > wrapWidth) ? tw - wrapWidth : tw; sx = (wrapWidth - tw)/2; break; case RIGHT: tw = layout.getVisibleAdvance(); tw = (tw > wrapWidth) ? tw - wrapWidth : tw; sx = wrapWidth - tw; break; case LEFT: case JUSTIFY: default: sx = 0; } // display text g2d.setColor(jpalette[2]); lineInfo.layout.draw(g2d, sx, 0); buffer.translate(0, layout.getDescent() + layout.getLeading()); } buffer.endDraw(); } } }