2018-03-18 12:54:41 +01:00

131 lines
4.2 KiB
Java

package de.frajul.endlessroll.views;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import de.frajul.endlessroll.R;
import de.frajul.endlessroll.entities.tools.ToolSlot;
import de.frajul.endlessroll.entities.tools.ToolType;
import de.frajul.endlessroll.entities.tools.ToolUpgradeType;
import de.frajul.endlessroll.main.game.Game;
/**
* Created by Julian on 15.01.2016.
*/
public class ToolButton {
private Game game;
private boolean locked;
private ToolType toolType;
private Context context;
private float progress = 100;
private boolean active = false;
private RelativeLayout layout;
private ProgressBar progressBar;
private ImageView backgroundView;
private ImageView animationView;
private Animation scaleAnimation;
public ToolButton(ToolSlot slot, Game game, Context context, RelativeLayout layout) {
this.context = context;
this.game = game;
this.layout = layout;
progressBar = (ProgressBar) layout.findViewById(R.id.tool_button_progress_bar);
scaleAnimation = AnimationUtils.loadAnimation(context, R.anim.scale_up);
changeToolSlot(slot);
}
public void changeToolSlot(ToolSlot toolSlot) {
this.toolType = toolSlot.getToolType();
this.locked = toolSlot.isLocked();
backgroundView = createImageView(toolType, R.id.tool_button_background_layer);
animationView = createImageView(toolType, R.id.tool_button_animation_layer);
}
private ImageView createImageView(ToolType type, int id) {
ImageView view = (ImageView) layout.findViewById(id);
if (locked)
view.setImageResource(R.drawable.tools_button_locked);
else if (type != null)
view.setImageResource(type.getButtonDrawable());
else
view.setImageResource(R.drawable.tools_button_empty);
view.setBackgroundDrawable(createColoredBackground());
return view;
}
private Drawable createColoredBackground() {
GradientDrawable gd = new GradientDrawable();
gd.setColor(context.getResources().getColor(R.color.toolbuttonInactiveNotReady));
if (locked || toolType == null) {
gd.setColor(context.getResources().getColor(R.color.toolbuttonLocked));
}
gd.setCornerRadius(15);
return gd;
}
public void update(float frameTime) {
if (progress != 100) {
progress += (frameTime) / (toolType
.getCurrentUpgradeValue(ToolUpgradeType.COOLDOWN) / 100);
progress = Math.min(progress, 100);
if (progress == 100)
animationView.startAnimation(scaleAnimation);
}
progressBar.setProgress(100 - (int) progress);
if (!locked && toolType != null) {
if (active) {
if (progress == 100) {
game.onToolButtonFinishedLoading(toolType);
setColor(R.color.toolbuttonActiveReady);
}else
setColor(R.color.toolbuttonActiveNotReady);
} else {
if (progress == 100)
setColor(R.color.toolbuttonInactiveReady);
else
setColor(R.color.toolbuttonInactiveNotReady);
}
}
}
private void setColor(int id) {
GradientDrawable gd = (GradientDrawable) backgroundView.getBackground();
GradientDrawable animGd = (GradientDrawable) animationView.getBackground();
gd.setColor(context.getResources().getColor(id));
animGd.setColor(context.getResources().getColor(id));
}
public void setActive(boolean active) {
this.active = active;
}
public boolean isActive() {
return active;
}
public void setProgress(int progress) {
this.progress = progress;
}
public boolean finishedLoading() {
return progress == 100;
}
public ToolType getToolType() {
return toolType;
}
public boolean isLocked() {
return locked;
}
}