mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-10 17:22:09 +10:00
Allow multi-line text in label components
This commit is contained in:
@@ -3526,6 +3526,10 @@
|
|||||||
<releaseDate>2021-01-17 00:00:00.000 CET</releaseDate>
|
<releaseDate>2021-01-17 00:00:00.000 CET</releaseDate>
|
||||||
<name></name>
|
<name></name>
|
||||||
<changes>
|
<changes>
|
||||||
|
<org.diylc.appframework.update.Change>
|
||||||
|
<changeType>NEW_FEATURE</changeType>
|
||||||
|
<description>Allow multi-line text in label components</description>
|
||||||
|
</org.diylc.appframework.update.Change>
|
||||||
<org.diylc.appframework.update.Change>
|
<org.diylc.appframework.update.Change>
|
||||||
<changeType>IMPROVEMENT</changeType>
|
<changeType>IMPROVEMENT</changeType>
|
||||||
<description>Slightly fancier splash screen :)</description>
|
<description>Slightly fancier splash screen :)</description>
|
||||||
|
|||||||
@@ -28,8 +28,10 @@ import java.awt.Rectangle;
|
|||||||
import java.awt.font.FontRenderContext;
|
import java.awt.font.FontRenderContext;
|
||||||
import java.awt.font.GlyphVector;
|
import java.awt.font.GlyphVector;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.diylc.common.HorizontalAlignment;
|
import org.diylc.common.HorizontalAlignment;
|
||||||
import org.diylc.common.VerticalAlignment;
|
import org.diylc.common.VerticalAlignment;
|
||||||
@@ -70,7 +72,8 @@ public class StringUtils {
|
|||||||
* @return a non-empty list of strings
|
* @return a non-empty list of strings
|
||||||
*/
|
*/
|
||||||
public static List<String> wrap(String str, FontMetrics fm, int maxWidth) {
|
public static List<String> wrap(String str, FontMetrics fm, int maxWidth) {
|
||||||
List<String> lines = splitIntoLines(str);
|
String[] split = str.split("\\<br\\>");
|
||||||
|
List<String> lines = Arrays.stream(split).flatMap(s -> splitIntoLines(s).stream()).collect(Collectors.toList());
|
||||||
if (lines.size() == 0)
|
if (lines.size() == 0)
|
||||||
return lines;
|
return lines;
|
||||||
|
|
||||||
@@ -203,7 +206,7 @@ public class StringUtils {
|
|||||||
|
|
||||||
public static void drawCenteredText(Graphics2D g2d, String text, double x, double y, HorizontalAlignment horizontalAlignment,
|
public static void drawCenteredText(Graphics2D g2d, String text, double x, double y, HorizontalAlignment horizontalAlignment,
|
||||||
VerticalAlignment verticalAlignment) {
|
VerticalAlignment verticalAlignment) {
|
||||||
String[] parts = text.split("\n");
|
String[] parts = text.split("\\<br\\>");
|
||||||
if (parts.length > 1) {
|
if (parts.length > 1) {
|
||||||
FontMetrics fontMetrics = g2d.getFontMetrics();
|
FontMetrics fontMetrics = g2d.getFontMetrics();
|
||||||
Rectangle stringBounds = fontMetrics.getStringBounds(parts[0], g2d).getBounds();
|
Rectangle stringBounds = fontMetrics.getStringBounds(parts[0], g2d).getBounds();
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ import java.awt.Graphics2D;
|
|||||||
import java.awt.geom.Point2D;
|
import java.awt.geom.Point2D;
|
||||||
import java.awt.geom.Rectangle2D;
|
import java.awt.geom.Rectangle2D;
|
||||||
|
|
||||||
|
import org.diylc.awt.StringUtils;
|
||||||
import org.diylc.common.HorizontalAlignment;
|
import org.diylc.common.HorizontalAlignment;
|
||||||
import org.diylc.common.Orientation;
|
import org.diylc.common.Orientation;
|
||||||
import org.diylc.common.VerticalAlignment;
|
import org.diylc.common.VerticalAlignment;
|
||||||
@@ -41,6 +42,7 @@ import org.diylc.core.VisibilityPolicy;
|
|||||||
import org.diylc.core.annotations.BomPolicy;
|
import org.diylc.core.annotations.BomPolicy;
|
||||||
import org.diylc.core.annotations.ComponentDescriptor;
|
import org.diylc.core.annotations.ComponentDescriptor;
|
||||||
import org.diylc.core.annotations.EditableProperty;
|
import org.diylc.core.annotations.EditableProperty;
|
||||||
|
import org.diylc.core.annotations.MultiLineText;
|
||||||
|
|
||||||
@ComponentDescriptor(name = "Label", author = "Branislav Stojkovic", category = "Misc",
|
@ComponentDescriptor(name = "Label", author = "Branislav Stojkovic", category = "Misc",
|
||||||
description = "User defined label", instanceNamePrefix = "L", zOrder = IDIYComponent.TEXT, flexibleZOrder = true,
|
description = "User defined label", instanceNamePrefix = "L", zOrder = IDIYComponent.TEXT, flexibleZOrder = true,
|
||||||
@@ -68,40 +70,9 @@ public class Label extends AbstractComponent<String> {
|
|||||||
IDrawingObserver drawingObserver) {
|
IDrawingObserver drawingObserver) {
|
||||||
g2d.setColor(componentState == ComponentState.SELECTED ? LABEL_COLOR_SELECTED : color);
|
g2d.setColor(componentState == ComponentState.SELECTED ? LABEL_COLOR_SELECTED : color);
|
||||||
g2d.setFont(font);
|
g2d.setFont(font);
|
||||||
FontMetrics fontMetrics = g2d.getFontMetrics();
|
|
||||||
Rectangle2D rect = fontMetrics.getStringBounds(text, g2d);
|
|
||||||
|
|
||||||
int textHeight = (int) rect.getHeight();
|
|
||||||
int textWidth = (int) rect.getWidth();
|
|
||||||
|
|
||||||
double x = point.getX();
|
double x = point.getX();
|
||||||
double y = point.getY();
|
double y = point.getY();
|
||||||
switch (getVerticalAlignment()) {
|
|
||||||
case CENTER:
|
|
||||||
y = point.getY() - textHeight / 2 + fontMetrics.getAscent();
|
|
||||||
break;
|
|
||||||
case TOP:
|
|
||||||
y = point.getY() - textHeight + fontMetrics.getAscent();
|
|
||||||
break;
|
|
||||||
case BOTTOM:
|
|
||||||
y = point.getY() + fontMetrics.getAscent();
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new RuntimeException("Unexpected alignment: " + getVerticalAlignment());
|
|
||||||
}
|
|
||||||
switch (getHorizontalAlignment()) {
|
|
||||||
case CENTER:
|
|
||||||
x = point.getX() - textWidth / 2;
|
|
||||||
break;
|
|
||||||
case LEFT:
|
|
||||||
x = point.getX();
|
|
||||||
break;
|
|
||||||
case RIGHT:
|
|
||||||
x = point.getX() - textWidth;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new RuntimeException("Unexpected alignment: " + getHorizontalAlignment());
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (getOrientation()) {
|
switch (getOrientation()) {
|
||||||
case _90:
|
case _90:
|
||||||
@@ -114,7 +85,8 @@ public class Label extends AbstractComponent<String> {
|
|||||||
g2d.rotate(Math.PI * 3 / 2, point.getX(), point.getY());
|
g2d.rotate(Math.PI * 3 / 2, point.getX(), point.getY());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
g2d.drawString(text, (int)x, (int)y);
|
|
||||||
|
StringUtils.drawCenteredText(g2d, text, x, y, getHorizontalAlignment(), getVerticalAlignment());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -274,6 +246,7 @@ public class Label extends AbstractComponent<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@EditableProperty(name="Text", defaultable = false)
|
@EditableProperty(name="Text", defaultable = false)
|
||||||
|
@MultiLineText
|
||||||
@Override
|
@Override
|
||||||
public String getValue() {
|
public String getValue() {
|
||||||
return text;
|
return text;
|
||||||
|
|||||||
Reference in New Issue
Block a user