Skip to content

Commit

Permalink
provide image scaling and alignment defaults for ImageComponents.
Browse files Browse the repository at this point in the history
Ensure that new Tweens are instantly begun.
  • Loading branch information
nightm4re94 committed Dec 30, 2020
1 parent fc42199 commit 0f39631
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
13 changes: 8 additions & 5 deletions src/de/gurkenlabs/litiengine/gui/ImageComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public class ImageComponent extends GuiComponent {

private Spritesheet spritesheet;

private ImageScaleMode imageScaleMode;
private Align imageAlign;
private Valign imageValign;
private ImageScaleMode imageScaleMode = ImageScaleMode.NORMAL;
private Align imageAlign = Align.CENTER;
private Valign imageValign = Valign.MIDDLE;

public ImageComponent(final double x, final double y, final Image image) {
super(x, y, image.getWidth(null), image.getHeight(null));
Expand All @@ -54,7 +54,8 @@ public ImageComponent(final double x, final double y, final double width, final
this.setImage(image);
}

public ImageComponent(final double x, final double y, final double width, final double height, final Spritesheet spritesheet, final String text, final Image image) {
public ImageComponent(final double x, final double y, final double width, final double height, final Spritesheet spritesheet, final String text,
final Image image) {
this(x, y, width, height, text);
this.spritesheet = spritesheet;
this.setImageAlign(Align.LEFT);
Expand All @@ -69,7 +70,9 @@ public Image getBackground() {
return null;
}

final String cacheKey = this.getSpritesheet().getName().hashCode() + "_" + this.isHovered() + "_" + this.isPressed() + "_" + this.isEnabled() + "_" + this.getWidth() + "x" + this.getHeight();
final String cacheKey =
this.getSpritesheet().getName().hashCode() + "_" + this.isHovered() + "_" + this.isPressed() + "_" + this.isEnabled() + "_" + this.getWidth()
+ "x" + this.getHeight();
Optional<BufferedImage> opt = Resources.images().tryGet(cacheKey);
if (opt.isPresent()) {
return opt.get();
Expand Down
39 changes: 15 additions & 24 deletions src/de/gurkenlabs/litiengine/tweening/TweenEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ public TweenEngine() {
* Begins a new Tween. If a Tween is already registered for the {@code Tweenable} with the given {@code TweenType}, it is restarted with the given
* duration.
*
* @param target
* the {@code Tweenable} target object
* @param type
* the {@code TweenType} determining which values of the target object will be modified.
* @param duration
* the duration of the Tween in milliseconds.
* @param target the {@code Tweenable} target object
* @param type the {@code TweenType} determining which values of the target object will be modified.
* @param duration the duration of the Tween in milliseconds.
* @return the Tween instance
*/
public Tween begin(final Tweenable target, final TweenType type, final long duration) {
Expand All @@ -40,18 +37,16 @@ public Tween begin(final Tweenable target, final TweenType type, final long dura
this.getTweens().get(target).put(type, tween);
} else {
tween.setDuration(duration);
tween.begin();
}
tween.begin();
return tween;
}

/**
* Attempts to get a previously registered {@code Tween} or registers a new one.
*
* @param target
* the {@code Tweenable} target object
* @param type
* the {@code TweenType} determining which values of the target object will be modified.
* @param target the {@code Tweenable} target object
* @param type the {@code TweenType} determining which values of the target object will be modified.
* @return the Tween instance
*/
public Tween getTween(final Tweenable target, final TweenType type) {
Expand All @@ -75,10 +70,8 @@ public Map<Tweenable, Map<TweenType, Tween>> getTweens() {
* Looks for a registered Tween instance with the given target and type. Attempts to stop the Tween and reset the {@code Tweenable} values to the
* start values.
*
* @param target
* the {@code Tweenable} target object
* @param type
* the {@code TweenType} determining which values of the target object will be modified.
* @param target the {@code Tweenable} target object
* @param type the {@code TweenType} determining which values of the target object will be modified.
* @return the Tween instance
*/
public Tween reset(final Tweenable target, final TweenType type) {
Expand All @@ -93,10 +86,8 @@ public Tween reset(final Tweenable target, final TweenType type) {
/**
* Looks for a registered Tween instance with the given target and type. Attempts to resume the Tween if it was stopped.
*
* @param target
* the {@code Tweenable} target object
* @param type
* the {@code TweenType} determining which values of the target object will be modified.
* @param target the {@code Tweenable} target object
* @param type the {@code TweenType} determining which values of the target object will be modified.
* @return the Tween instance
*/
public Tween resume(final Tweenable target, final TweenType type) {
Expand All @@ -118,10 +109,8 @@ public void start() {
/**
* Looks for a registered Tween instance with the given target and type. Attempts to stop the Tween.
*
* @param target
* the {@code Tweenable} target object
* @param type
* the {@code TweenType} determining which values of the target object will be modified.
* @param target the {@code Tweenable} target object
* @param type the {@code TweenType} determining which values of the target object will be modified.
* @return the Tween instance
*/
public Tween stop(final Tweenable target, final TweenType type) {
Expand Down Expand Up @@ -157,7 +146,9 @@ public void update() {
}
final float[] currentValues = new float[tween.getTargetValues().length];
for (int i = 0; i < tween.getTargetValues().length; i++) {
currentValues[i] = tween.getStartValues()[i] + tween.getEquation().compute(elapsed / (float) tween.getDuration()) * (tween.getTargetValues()[i] - tween.getStartValues()[i]);
currentValues[i] =
tween.getStartValues()[i] + tween.getEquation().compute(elapsed / (float) tween.getDuration()) * (tween.getTargetValues()[i] - tween
.getStartValues()[i]);
}
tween.getTarget().setTweenValues(tween.getType(), currentValues);
}
Expand Down

0 comments on commit 0f39631

Please sign in to comment.