Skip to content

Commit

Permalink
Implement enabled property for GuiComponent
Browse files Browse the repository at this point in the history
It is now possible to define a disabled background image for ImageComponents.
  • Loading branch information
Gurkenlabs committed Sep 28, 2017
1 parent de08094 commit 6b01bb0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 25 deletions.
44 changes: 30 additions & 14 deletions src/de/gurkenlabs/litiengine/gui/GuiComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public abstract class GuiComponent implements MouseListener, MouseMotionListener
private boolean isPressed;
private boolean isSelected;
private boolean suspended;
private boolean enabled;

private Object tag;

Expand Down Expand Up @@ -150,6 +151,18 @@ public boolean drawTextShadow() {
return this.drawTextShadow;
}

public Appearance getAppearance() {
return appearance;
}

public Appearance getAppearanceHovered() {
return hoveredAppearance;
}

public Appearance getAppearanceDisabled() {
return disabledAppearance;
}

/**
* Gets the bounding box.
*
Expand Down Expand Up @@ -272,22 +285,14 @@ public double getY() {
return this.y;
}

public Appearance getAppearance() {
return appearance;
}

public Appearance getAppearanceHovered() {
return hoveredAppearance;
}

public Appearance getAppearanceDisabled() {
return disabledAppearance;
}

public boolean isDragged() {
return this.isDragged();
}

public boolean isEnabled() {
return enabled;
}

/**
* Checks if is hovered.
*
Expand Down Expand Up @@ -556,6 +561,10 @@ public void render(final Graphics2D g) {
currentAppearance = this.getAppearanceHovered();
}

if (!this.isEnabled()) {
currentAppearance = this.getAppearanceDisabled();
}

if (!currentAppearance.isTransparentBackground()) {
g.setPaint(currentAppearance.getBackgroundPaint(this.getWidth(), this.getHeight()));
g.fill(this.getBoundingBox());
Expand Down Expand Up @@ -585,6 +594,13 @@ public void setDimension(final double width, final double height) {
this.height = height;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
for (GuiComponent comp : this.getComponents()) {
comp.setEnabled(this.isEnabled());
}
}

public void setHeight(final double height) {
this.height = height;
}
Expand Down Expand Up @@ -755,7 +771,7 @@ protected void initializeComponents() {
* @return true, if successful
*/
private boolean mouseEventShouldBeForwarded(final MouseEvent e) {
return this.isVisible() && !this.isSuspended() && this.getBoundingBox().contains(e.getPoint());
return this.isVisible() && this.isEnabled() && !this.isSuspended() && this.getBoundingBox().contains(e.getPoint());
}

private void renderText(Graphics2D g) {
Expand Down Expand Up @@ -796,4 +812,4 @@ private void renderText(Graphics2D g) {
}
}
}
}
}
22 changes: 12 additions & 10 deletions src/de/gurkenlabs/litiengine/gui/ImageComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class ImageComponent extends GuiComponent {
public static final int BACKGROUND_INDEX = 0;
public static final int BACKGROUND_HOVER_INDEX = 1;
public static final int BACKGROUND_PRESSED_INDEX = 2;
public static final int BACKGROUND_DISABLED_INDEX = 3;

private Sound hoverSound;

Expand Down Expand Up @@ -58,22 +59,22 @@ public Image getBackground() {
if (this.getSpritesheet() == null) {
return null;
}
final String cacheKey = MessageFormat.format("{0}_{1}_{2}_{3}x{4}", this.getSpritesheet().getName().hashCode(), this.isHovered(), this.isPressed(), this.getWidth(), this.getHeight());

final String cacheKey = MessageFormat.format("{0}_{1}_{2}_{3}_{4}x{5}", this.getSpritesheet().getName().hashCode(), this.isHovered(), this.isPressed(), this.isEnabled(), this.getWidth(), this.getHeight());
if (ImageCache.SPRITES.containsKey(cacheKey)) {
return ImageCache.SPRITES.get(cacheKey);
}

BufferedImage img;
if (this.isHovered() && this.getSpritesheet().getTotalNumberOfSprites() > 1) {
if (this.isPressed()) {
img = ImageProcessing.scaleImage(this.getSpritesheet().getSprite(BACKGROUND_PRESSED_INDEX), (int) this.getWidth(), (int) this.getHeight());
} else {
img = ImageProcessing.scaleImage(this.getSpritesheet().getSprite(BACKGROUND_HOVER_INDEX), (int) this.getWidth(), (int) this.getHeight());
}
} else {
img = ImageProcessing.scaleImage(this.getSpritesheet().getSprite(BACKGROUND_INDEX), (int) this.getWidth(), (int) this.getHeight());
int spriteIndex = BACKGROUND_INDEX;
if (!this.isEnabled() && this.getSpritesheet().getTotalNumberOfSprites() > BACKGROUND_DISABLED_INDEX) {
spriteIndex = BACKGROUND_DISABLED_INDEX;
} else if (this.isPressed() && this.getSpritesheet().getTotalNumberOfSprites() > BACKGROUND_PRESSED_INDEX) {
spriteIndex = BACKGROUND_PRESSED_INDEX;
} else if (this.isHovered() && this.getSpritesheet().getTotalNumberOfSprites() > BACKGROUND_HOVER_INDEX) {
spriteIndex = BACKGROUND_HOVER_INDEX;
}

BufferedImage img = ImageProcessing.scaleImage(this.getSpritesheet().getSprite(spriteIndex), (int) this.getWidth(), (int) this.getHeight());
if (img != null) {
ImageCache.SPRITES.put(cacheKey, img);
}
Expand All @@ -99,6 +100,7 @@ public void render(final Graphics2D g) {
if (this.isSuspended() || !this.isVisible()) {
return;
}

final Image bg = this.getBackground();
if (bg != null) {
RenderEngine.renderImage(g, bg, this.getLocation());
Expand Down
2 changes: 1 addition & 1 deletion src/de/gurkenlabs/litiengine/gui/TextFieldComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public String getText() {
}

public void handleTypedKey(final KeyEvent event) {
if (this.isSuspended() || !this.isSelected() || !this.isVisible()) {
if (this.isSuspended() || !this.isSelected() || !this.isVisible() || !this.isEnabled()) {
return;
}

Expand Down

0 comments on commit 6b01bb0

Please sign in to comment.