v0.6.1b: refactoring + bugfixes to buff indicator

This commit is contained in:
Evan Debenham 2017-08-23 18:07:29 -04:00
parent 95dcced4eb
commit 3ff7ccdbaf

View File

@ -34,7 +34,9 @@ import com.watabou.noosa.TextureFilm;
import com.watabou.noosa.tweeners.AlphaTweener; import com.watabou.noosa.tweeners.AlphaTweener;
import com.watabou.noosa.ui.Button; import com.watabou.noosa.ui.Button;
import com.watabou.noosa.ui.Component; import com.watabou.noosa.ui.Component;
import com.watabou.utils.SparseArray;
import java.util.ArrayList;
import java.util.LinkedHashMap;
public class BuffIndicator extends Component { public class BuffIndicator extends Component {
@ -92,8 +94,8 @@ public class BuffIndicator extends Component {
private SmartTexture texture; private SmartTexture texture;
private TextureFilm film; private TextureFilm film;
private SparseArray<BuffIcon> icons = new SparseArray<BuffIcon>(); private LinkedHashMap<Buff, BuffIcon> buffIcons = new LinkedHashMap<>();
private boolean needsRefresh;
private Char ch; private Char ch;
public BuffIndicator( Char ch ) { public BuffIndicator( Char ch ) {
@ -120,24 +122,29 @@ public class BuffIndicator extends Component {
film = new TextureFilm( texture, SIZE, SIZE ); film = new TextureFilm( texture, SIZE, SIZE );
} }
@Override
public synchronized void update() {
super.update();
if (needsRefresh){
layout();
needsRefresh = false;
}
}
@Override @Override
protected void layout() { protected void layout() {
clear();
SparseArray<BuffIcon> newIcons = new SparseArray<BuffIcon>();
ArrayList<Buff> newBuffs = new ArrayList<>();
for (Buff buff : ch.buffs()) { for (Buff buff : ch.buffs()) {
if (buff.icon() != NONE) { if (buff.icon() != NONE) {
BuffIcon icon = new BuffIcon( buff ); newBuffs.add(buff);
icon.setRect(x + members.size() * (SIZE + 2), y, 9, 12);
add(icon);
newIcons.put( buff.icon(), icon );
} }
} }
for (Integer key : icons.keyArray()) { //remove any icons no longer present
if (newIcons.get( key ) == null) { for (Buff buff : buffIcons.keySet().toArray(new Buff[0])){
Image icon = icons.get( key ).icon; if (!newBuffs.contains(buff)){
Image icon = buffIcons.get( buff ).icon;
icon.origin.set( SIZE / 2 ); icon.origin.set( SIZE / 2 );
add( icon ); add( icon );
add( new AlphaTweener( icon, 0, 0.6f ) { add( new AlphaTweener( icon, 0, 0.6f ) {
@ -145,19 +152,36 @@ public class BuffIndicator extends Component {
protected void updateValues( float progress ) { protected void updateValues( float progress ) {
super.updateValues( progress ); super.updateValues( progress );
image.scale.set( 1 + 5 * progress ); image.scale.set( 1 + 5 * progress );
}; }
@Override @Override
protected void onComplete() { protected void onComplete() {
image.killAndErase(); image.killAndErase();
} }
} ); } );
icons.get( key ).destroy(); buffIcons.get( buff ).destroy();
remove(buffIcons.get( buff ));
buffIcons.remove( buff );
} }
} }
icons = newIcons; //add new icons
for (Buff buff : newBuffs) {
if (!buffIcons.containsKey(buff)) {
BuffIcon icon = new BuffIcon( buff );
add(icon);
buffIcons.put( buff, icon );
}
}
//layout
int pos = 0;
for (BuffIcon icon : buffIcons.values()){
icon.updateIcon();
icon.setRect(x + pos * (SIZE + 2), y, 9, 12);
pos++;
}
} }
private class BuffIcon extends Button { private class BuffIcon extends Button {
@ -174,6 +198,10 @@ public class BuffIndicator extends Component {
icon.frame( film.get( buff.icon() ) ); icon.frame( film.get( buff.icon() ) );
add( icon ); add( icon );
} }
public void updateIcon(){
icon.frame( film.get( buff.icon() ) );
}
@Override @Override
protected void layout() { protected void layout() {
@ -191,7 +219,7 @@ public class BuffIndicator extends Component {
public static void refreshHero() { public static void refreshHero() {
if (heroInstance != null) { if (heroInstance != null) {
heroInstance.layout(); heroInstance.needsRefresh = true;
} }
} }
} }