Skip to content

Commit

Permalink
Pull GuiComponent text antialiasing out of the Appearance again.
Browse files Browse the repository at this point in the history
Everything font-related is happening on the GuiComponent directly and in most cases it just doesn't make sense to have differences in AntiAliasing when hovering/clicking a GuiComponent.
  • Loading branch information
nightm4re94 committed Dec 29, 2020
1 parent f2bfbbb commit 179f192
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 41 deletions.
4 changes: 2 additions & 2 deletions src/de/gurkenlabs/litiengine/graphics/TextRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private TextRenderer() {
* @param y the min y coordinate
*/
public static void render(final Graphics2D g, final String text, final double x, final double y) {
render(g, text, x, y, GuiProperties.getDefaultAppearance().getTextAntialiasing());
render(g, text, x, y, true);
}

public static void render(final Graphics2D g, final String text, Point2D location) {
Expand Down Expand Up @@ -162,7 +162,7 @@ public static void renderRotated(final Graphics2D g, final String text, Point2D
* @param lineWidth the max line width
*/
public static void renderWithLinebreaks(final Graphics2D g, final String text, final double x, final double y, final double lineWidth) {
renderWithLinebreaks(g, text, x, y, lineWidth, GuiProperties.getDefaultAppearance().getTextAntialiasing());
renderWithLinebreaks(g, text, x, y, lineWidth, true);
}

public static void renderWithLinebreaks(final Graphics2D g, final String text, Point2D location, final double lineWidth) {
Expand Down
22 changes: 2 additions & 20 deletions src/de/gurkenlabs/litiengine/gui/Appearance.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ public class Appearance {
private float borderRadius;
private boolean horizontalBackgroundGradient;
private boolean transparentBackground;
private boolean textAntialiasing;

public Appearance() {
this.changedConsumer = new CopyOnWriteArrayList<>();
this.textAntialiasing = false;
}

public Appearance(Color foreColor) {
Expand All @@ -49,8 +47,7 @@ public boolean equals(Object obj) {
return false;
}
Appearance other = (Appearance) obj;
return this.textAntialiasing == other.textAntialiasing
&& this.transparentBackground == other.transparentBackground
return this.transparentBackground == other.transparentBackground
&& this.horizontalBackgroundGradient == other.horizontalBackgroundGradient
&& Float.floatToIntBits(this.borderRadius) == Float.floatToIntBits(other.borderRadius)
&& Objects.equals(this.borderColor, other.borderColor)
Expand All @@ -62,7 +59,7 @@ public boolean equals(Object obj) {

@Override
public int hashCode() {
return Objects.hash(this.textAntialiasing, this.transparentBackground, this.horizontalBackgroundGradient, this.borderRadius,
return Objects.hash(this.transparentBackground, this.horizontalBackgroundGradient, this.borderRadius,
this.borderColor, this.borderStyle, this.backgroundColor1, this.backgroundColor2, this.foreColor);
}

Expand Down Expand Up @@ -107,10 +104,6 @@ public float getBorderRadius() {
return this.borderRadius;
}

public boolean getTextAntialiasing() {
return this.textAntialiasing;
}

public boolean isHorizontalBackgroundGradient() {
return this.horizontalBackgroundGradient;
}
Expand Down Expand Up @@ -156,16 +149,6 @@ public void setTransparentBackground(boolean transparentBackground) {
this.fireOnChangeEvent();
}

/**
* Sets the {@link RenderingHints#KEY_TEXT_ANTIALIASING} settings for the rendered text.
*
* @param antialiasing
* Either {@link RenderingHints#VALUE_TEXT_ANTIALIAS_ON} or {@link RenderingHints#VALUE_TEXT_ANTIALIAS_OFF}
*/
public void setTextAntialiasing(boolean antialiasing) {
this.textAntialiasing = antialiasing;
}

public void onChange(Consumer<Appearance> cons) {
this.changedConsumer.add(cons);
}
Expand All @@ -176,7 +159,6 @@ public void update(Appearance updateAppearance) {
this.setForeColor(updateAppearance.getForeColor());
this.setHorizontalBackgroundGradient(updateAppearance.isHorizontalBackgroundGradient());
this.setTransparentBackground(updateAppearance.isTransparentBackground());
this.setTextAntialiasing(updateAppearance.getTextAntialiasing());
}

protected void fireOnChangeEvent() {
Expand Down
38 changes: 19 additions & 19 deletions src/de/gurkenlabs/litiengine/gui/GuiComponent.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package de.gurkenlabs.litiengine.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
Expand Down Expand Up @@ -71,9 +66,8 @@ public abstract class GuiComponent implements MouseListener, MouseMotionListener
private double height;

private Sound hoverSound;

private boolean textAntialiasing;
private boolean isHovered;

private boolean isPressed;
private boolean isSelected;
private String name;
Expand Down Expand Up @@ -323,6 +317,10 @@ public int getTextAngle() {
return this.textAngle;
}

public boolean hasTextAntialiasing() {
return this.textAntialiasing;
}

/**
* Gets the text shadow color.
*
Expand Down Expand Up @@ -971,6 +969,15 @@ public void setText(final String text) {
}
}

/**
* Sets the {@link RenderingHints#KEY_TEXT_ANTIALIASING} settings for the rendered text.
*
* @param antialiasing Either {@link RenderingHints#VALUE_TEXT_ANTIALIAS_ON} or {@link RenderingHints#VALUE_TEXT_ANTIALIAS_OFF}
*/
public void setTextAntialiasing(boolean antialiasing) {
this.textAntialiasing = antialiasing;
}

/**
* Sets the horizontal text alignment.
*
Expand Down Expand Up @@ -1253,29 +1260,22 @@ private void renderText(final Graphics2D g) {
this.getTextX();
double yCoord = this.getTextValign() != null ?
this.getY() + this.getTextValign().getLocation(this.getHeight(), textHeight) : this.getTextY();
boolean antialiasing = this.getAppearance().getTextAntialiasing();
if (this.isHovered()) {
antialiasing = this.getAppearanceHovered().getTextAntialiasing();
}
if (!this.isEnabled()) {
antialiasing = this.getAppearanceDisabled().getTextAntialiasing();
}
if (this.getTextAngle() == 0) {
if (this.drawTextShadow()) {
TextRenderer
.renderWithOutline(g, this.getTextToRender(g), this.getX(), yCoord + fm.getLeading(), this.getWidth(),
this.getHeight() + textHeight,
this.getTextShadowColor(),
this.getTextShadowStroke(), this.getTextAlign(), this.getTextValign(), antialiasing);
this.getTextShadowStroke(), this.getTextAlign(), this.getTextValign(), this.hasTextAntialiasing());
} else {
TextRenderer.render(g, this.getTextToRender(g), xCoord, yCoord + textHeight * 3 / 4d, antialiasing);
TextRenderer.render(g, this.getTextToRender(g), xCoord, yCoord + textHeight - fm.getLeading() * 2, this.hasTextAntialiasing());
}
} else if (this.getTextAngle() == 90) {
TextRenderer.renderRotated(g, this.getTextToRender(g), xCoord,
yCoord - fm.stringWidth(this.getTextToRender(g)), this.getTextAngle(), antialiasing);
yCoord - fm.stringWidth(this.getTextToRender(g)), this.getTextAngle(), this.hasTextAntialiasing());
} else {
TextRenderer
.renderRotated(g, this.getTextToRender(g), xCoord, yCoord, this.getTextAngle(), antialiasing);
.renderRotated(g, this.getTextToRender(g), xCoord, yCoord, this.getTextAngle(), this.hasTextAntialiasing());
}
}
}

0 comments on commit 179f192

Please sign in to comment.