Skip to content

Commit

Permalink
Add optional automatic Linebreaks for GuiComponents.
Browse files Browse the repository at this point in the history
This resolves #117. Use GuiComponent.setAutomaticLineBreaks(...) to toggle between automatic line breaks and cropped text.
  • Loading branch information
nightm4re94 committed Dec 29, 2020
1 parent 179f192 commit fc42199
Showing 1 changed file with 15 additions and 40 deletions.
55 changes: 15 additions & 40 deletions src/de/gurkenlabs/litiengine/gui/GuiComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public abstract class GuiComponent implements MouseListener, MouseMotionListener
private final Appearance disabledAppearance;

private boolean drawTextShadow = false;
private boolean autoAdjustTextPosition = true;
private boolean enabled;
private Font font;
private boolean forwardMouseEvents = true;
Expand All @@ -76,6 +75,7 @@ public abstract class GuiComponent implements MouseListener, MouseMotionListener
private String text;
private Align textAlign = Align.CENTER;
private Valign textValign = Valign.MIDDLE;
private boolean automaticLineBreaks;
private int textAngle = 0;

private Color textShadowColor;
Expand All @@ -85,7 +85,6 @@ public abstract class GuiComponent implements MouseListener, MouseMotionListener
private boolean visible;
private double width;
private double x;
private double xMargin;
private double y;

/**
Expand Down Expand Up @@ -146,8 +145,6 @@ protected GuiComponent(final double x, final double y, final double width, final

this.setLocation(x, y);
this.setDimension(width, height);

this.setHorizontalTextMargin(this.getWidth() / 16);
this.setFont(GuiProperties.getDefaultFont());
this.setSelected(false);
this.setEnabled(true);
Expand Down Expand Up @@ -235,15 +232,6 @@ public double getHeight() {
return this.height;
}

/**
* Gets the margin size between the GuiComponent's left and right border and the Text bounds.
*
* @return the horizontal text margin
*/
public double getHorizontalTextMargin() {
return this.xMargin;
}

/**
* Gets the sound that is played when hovering the GuiComponent.
*
Expand Down Expand Up @@ -321,6 +309,10 @@ public boolean hasTextAntialiasing() {
return this.textAntialiasing;
}

public boolean hasAutomaticLineBreaks() {
return this.automaticLineBreaks;
}

/**
* Gets the text shadow color.
*
Expand Down Expand Up @@ -349,11 +341,13 @@ public float getTextShadowStroke() {
public String getTextToRender(final Graphics2D g) {
if (this.getText() == null) {
return "";
} else if (this.hasAutomaticLineBreaks()) {
return this.getText();
}
final FontMetrics fm = g.getFontMetrics();
String newText = this.getText();

while (newText.length() > 1 && fm.stringWidth(newText) >= this.getWidth() - this.getHorizontalTextMargin()) {
while (newText.length() > 1 && fm.stringWidth(newText) >= this.getWidth()) {
newText = newText.substring(1);
}
return newText;
Expand Down Expand Up @@ -423,13 +417,6 @@ public boolean isForwardMouseEvents() {
return this.forwardMouseEvents;
}

/**
* Checks if the text position is automatically adjusted when the component is resized
*/
public boolean isAutoAdjustingTextPosition() {
return this.autoAdjustTextPosition;
}

/**
* Checks if the cursor bounding box intersects with this GuiComponent's bounding box.
*
Expand Down Expand Up @@ -815,15 +802,6 @@ public RectangularShape getShape() {
this.getCurrentAppearance().getBorderRadius());
}

/**
* Setter for the automatic adjustment of text position when the component is resized
*
* @param autoAdjustTextPosition if {@code true}, adjust the text position automatically when the component is resized
*/
public void setAutoAdjustTextPosition(boolean autoAdjustTextPosition) {
this.autoAdjustTextPosition = autoAdjustTextPosition;
}

/**
* Sets the width and height of this GuiComponent.
*
Expand Down Expand Up @@ -883,15 +861,6 @@ public void setHeight(final double height) {
this.height = height;
}

/**
* Sets the margin size between the GuiComponent's left and right border and the Text bounds.
*
* @param xMargin the new text X margin
*/
public void setHorizontalTextMargin(final double xMargin) {
this.xMargin = xMargin;
}

/**
* Sets the "enabled" property on this GuiComponent.
*
Expand Down Expand Up @@ -978,6 +947,10 @@ public void setTextAntialiasing(boolean antialiasing) {
this.textAntialiasing = antialiasing;
}

public void setAutomaticLineBreaks(boolean automaticLineBreaks) {
this.automaticLineBreaks = automaticLineBreaks;
}

/**
* Sets the horizontal text alignment.
*
Expand Down Expand Up @@ -1268,7 +1241,9 @@ private void renderText(final Graphics2D g) {
this.getTextShadowColor(),
this.getTextShadowStroke(), this.getTextAlign(), this.getTextValign(), this.hasTextAntialiasing());
} else {
TextRenderer.render(g, this.getTextToRender(g), xCoord, yCoord + textHeight - fm.getLeading() * 2, this.hasTextAntialiasing());
TextRenderer
.renderWithLinebreaks(g, this.getTextToRender(g), this.getTextAlign(), this.getTextValign(), this.getX(), this.getY(), this.getWidth(),
this.getHeight(), this.hasTextAntialiasing());
}
} else if (this.getTextAngle() == 90) {
TextRenderer.renderRotated(g, this.getTextToRender(g), xCoord,
Expand Down

0 comments on commit fc42199

Please sign in to comment.